root / scripts / gnt-job @ 66348f29
History | View | Annotate | Download (8.4 kB)
1 |
#!/usr/bin/python |
---|---|
2 |
# |
3 |
|
4 |
# Copyright (C) 2006, 2007 Google Inc. |
5 |
# |
6 |
# This program is free software; you can redistribute it and/or modify |
7 |
# it under the terms of the GNU General Public License as published by |
8 |
# the Free Software Foundation; either version 2 of the License, or |
9 |
# (at your option) any later version. |
10 |
# |
11 |
# This program is distributed in the hope that it will be useful, but |
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
# General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License |
17 |
# along with this program; if not, write to the Free Software |
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 |
# 02110-1301, USA. |
20 |
|
21 |
|
22 |
# pylint: disable-msg=W0401,W0614 |
23 |
# W0401: Wildcard import ganeti.cli |
24 |
# W0614: Unused import %s from wildcard import (since we need cli) |
25 |
|
26 |
import sys |
27 |
|
28 |
from ganeti.cli import * |
29 |
from ganeti import constants |
30 |
from ganeti import errors |
31 |
|
32 |
|
33 |
_LIST_DEF_FIELDS = ["id", "status", "summary"] |
34 |
|
35 |
_USER_JOB_STATUS = { |
36 |
constants.JOB_STATUS_QUEUED: "queued", |
37 |
constants.JOB_STATUS_WAITLOCK: "waiting", |
38 |
constants.JOB_STATUS_RUNNING: "running", |
39 |
constants.JOB_STATUS_CANCELED: "canceled", |
40 |
constants.JOB_STATUS_SUCCESS: "success", |
41 |
constants.JOB_STATUS_ERROR: "error", |
42 |
} |
43 |
|
44 |
|
45 |
def ListJobs(opts, args): |
46 |
"""List the jobs |
47 |
|
48 |
""" |
49 |
if opts.output is None: |
50 |
selected_fields = _LIST_DEF_FIELDS |
51 |
elif opts.output.startswith("+"): |
52 |
selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",") |
53 |
else: |
54 |
selected_fields = opts.output.split(",") |
55 |
|
56 |
output = GetClient().QueryJobs(None, selected_fields) |
57 |
if not opts.no_headers: |
58 |
# TODO: Implement more fields |
59 |
headers = { |
60 |
"id": "ID", |
61 |
"status": "Status", |
62 |
"ops": "OpCodes", |
63 |
"opresult": "OpCode_result", |
64 |
"opstatus": "OpCode_status", |
65 |
"oplog": "OpCode_log", |
66 |
"summary": "Summary", |
67 |
"opstart": "OpCode_start", |
68 |
"opend": "OpCode_end", |
69 |
"start_ts": "Start", |
70 |
"end_ts": "End", |
71 |
"received_ts": "Received", |
72 |
} |
73 |
else: |
74 |
headers = None |
75 |
|
76 |
# we don't have yet unitfields here |
77 |
unitfields = None |
78 |
numfields = None |
79 |
|
80 |
# change raw values to nicer strings |
81 |
for row in output: |
82 |
for idx, field in enumerate(selected_fields): |
83 |
val = row[idx] |
84 |
if field == "status": |
85 |
if val in _USER_JOB_STATUS: |
86 |
val = _USER_JOB_STATUS[val] |
87 |
else: |
88 |
raise errors.ProgrammerError("Unknown job status code '%s'" % val) |
89 |
elif field == "summary": |
90 |
val = ",".join(val) |
91 |
elif field in ("start_ts", "end_ts", "received_ts"): |
92 |
val = FormatTimestamp(val) |
93 |
elif field in ("opstart", "opend"): |
94 |
val = [FormatTimestamp(entry) for entry in val] |
95 |
|
96 |
row[idx] = str(val) |
97 |
|
98 |
data = GenerateTable(separator=opts.separator, headers=headers, |
99 |
fields=selected_fields, unitfields=unitfields, |
100 |
numfields=numfields, data=output) |
101 |
for line in data: |
102 |
ToStdout(line) |
103 |
|
104 |
return 0 |
105 |
|
106 |
|
107 |
def ArchiveJobs(opts, args): |
108 |
client = GetClient() |
109 |
|
110 |
for job_id in args: |
111 |
client.ArchiveJob(job_id) |
112 |
|
113 |
return 0 |
114 |
|
115 |
|
116 |
def AutoArchiveJobs(opts, args): |
117 |
client = GetClient() |
118 |
|
119 |
age = args[0] |
120 |
|
121 |
if age == 'all': |
122 |
age = -1 |
123 |
else: |
124 |
age = ParseTimespec(age) |
125 |
|
126 |
client.AutoArchiveJobs(age) |
127 |
return 0 |
128 |
|
129 |
|
130 |
def CancelJobs(opts, args): |
131 |
client = GetClient() |
132 |
|
133 |
for job_id in args: |
134 |
client.CancelJob(job_id) |
135 |
|
136 |
return 0 |
137 |
|
138 |
|
139 |
def ShowJobs(opts, args): |
140 |
"""List the jobs |
141 |
|
142 |
""" |
143 |
def format(level, text): |
144 |
"""Display the text indented.""" |
145 |
ToStdout("%s%s", " " * level, text) |
146 |
|
147 |
def result_helper(value): |
148 |
"""Format a result field in a nice way.""" |
149 |
if isinstance(value, (tuple, list)): |
150 |
return "[%s]" % (", ".join(str(elem) for elem in value)) |
151 |
else: |
152 |
return str(value) |
153 |
|
154 |
selected_fields = [ |
155 |
"id", "status", "ops", "opresult", "opstatus", "oplog", |
156 |
"opstart", "opend", "received_ts", "start_ts", "end_ts", |
157 |
] |
158 |
|
159 |
result = GetClient().QueryJobs(args, selected_fields) |
160 |
|
161 |
first = True |
162 |
|
163 |
for idx, entry in enumerate(result): |
164 |
if not first: |
165 |
format(0, "") |
166 |
else: |
167 |
first = False |
168 |
|
169 |
if entry is None: |
170 |
if idx <= len(args): |
171 |
format(0, "Job ID %s not found" % args[idx]) |
172 |
else: |
173 |
# this should not happen, when we don't pass args it will be a |
174 |
# valid job returned |
175 |
format(0, "Job ID requested as argument %s not found" % (idx + 1)) |
176 |
continue |
177 |
|
178 |
(job_id, status, ops, opresult, opstatus, oplog, |
179 |
opstart, opend, recv_ts, start_ts, end_ts) = entry |
180 |
format(0, "Job ID: %s" % job_id) |
181 |
if status in _USER_JOB_STATUS: |
182 |
status = _USER_JOB_STATUS[status] |
183 |
else: |
184 |
raise errors.ProgrammerError("Unknown job status code '%s'" % status) |
185 |
|
186 |
format(1, "Status: %s" % status) |
187 |
|
188 |
if recv_ts is not None: |
189 |
format(1, "Received: %s" % FormatTimestamp(recv_ts)) |
190 |
else: |
191 |
format(1, "Missing received timestamp (%s)" % str(recv_ts)) |
192 |
|
193 |
if start_ts is not None: |
194 |
if recv_ts is not None: |
195 |
d1 = start_ts[0] - recv_ts[0] + (start_ts[1] - recv_ts[1]) / 1000000.0 |
196 |
delta = " (delta %.6fs)" % d1 |
197 |
else: |
198 |
delta = "" |
199 |
format(1, "Processing start: %s%s" % (FormatTimestamp(start_ts), delta)) |
200 |
else: |
201 |
format(1, "Processing start: unknown (%s)" % str(start_ts)) |
202 |
|
203 |
if end_ts is not None: |
204 |
if start_ts is not None: |
205 |
d2 = end_ts[0] - start_ts[0] + (end_ts[1] - start_ts[1]) / 1000000.0 |
206 |
delta = " (delta %.6fs)" % d2 |
207 |
else: |
208 |
delta = "" |
209 |
format(1, "Processing end: %s%s" % (FormatTimestamp(end_ts), delta)) |
210 |
else: |
211 |
format(1, "Processing end: unknown (%s)" % str(end_ts)) |
212 |
|
213 |
if end_ts is not None and recv_ts is not None: |
214 |
d3 = end_ts[0] - recv_ts[0] + (end_ts[1] - recv_ts[1]) / 1000000.0 |
215 |
format(1, "Total processing time: %.6f seconds" % d3) |
216 |
else: |
217 |
format(1, "Total processing time: N/A") |
218 |
format(1, "Opcodes:") |
219 |
for (opcode, result, status, log, s_ts, e_ts) in \ |
220 |
zip(ops, opresult, opstatus, oplog, opstart, opend): |
221 |
format(2, "%s" % opcode["OP_ID"]) |
222 |
format(3, "Status: %s" % status) |
223 |
if isinstance(s_ts, (tuple, list)): |
224 |
format(3, "Processing start: %s" % FormatTimestamp(s_ts)) |
225 |
else: |
226 |
format(3, "No processing start time") |
227 |
if isinstance(e_ts, (tuple, list)): |
228 |
format(3, "Processing end: %s" % FormatTimestamp(e_ts)) |
229 |
else: |
230 |
format(3, "No processing end time") |
231 |
format(3, "Input fields:") |
232 |
for key, val in opcode.iteritems(): |
233 |
if key == "OP_ID": |
234 |
continue |
235 |
if isinstance(val, (tuple, list)): |
236 |
val = ",".join(val) |
237 |
format(4, "%s: %s" % (key, val)) |
238 |
if result is None: |
239 |
format(3, "No output data") |
240 |
elif isinstance(result, (tuple, list)): |
241 |
if not result: |
242 |
format(3, "Result: empty sequence") |
243 |
else: |
244 |
format(3, "Result:") |
245 |
for elem in result: |
246 |
format(4, result_helper(elem)) |
247 |
elif isinstance(result, dict): |
248 |
if not result: |
249 |
format(3, "Result: empty dictionary") |
250 |
else: |
251 |
for key, val in result.iteritems(): |
252 |
format(4, "%s: %s" % (key, result_helper(val))) |
253 |
else: |
254 |
format(3, "Result: %s" % result) |
255 |
format(3, "Execution log:") |
256 |
for serial, log_ts, log_type, log_msg in log: |
257 |
time_txt = FormatTimestamp(log_ts) |
258 |
encoded = str(log_msg).encode('string_escape') |
259 |
format(4, "%s:%s:%s %s" % (serial, time_txt, log_type, encoded)) |
260 |
return 0 |
261 |
|
262 |
|
263 |
commands = { |
264 |
'list': (ListJobs, ARGS_NONE, |
265 |
[DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT], |
266 |
"", "List the jobs and their status. The available fields are" |
267 |
" (see the man page for details): id, status, op_list," |
268 |
" op_status, op_result." |
269 |
" The default field" |
270 |
" list is (in order): %s." % ", ".join(_LIST_DEF_FIELDS)), |
271 |
'archive': (ArchiveJobs, ARGS_ANY, |
272 |
[DEBUG_OPT], |
273 |
"<job-id> [<job-id> ...]", |
274 |
"Archive specified jobs"), |
275 |
'autoarchive': (AutoArchiveJobs, ARGS_ONE, |
276 |
[DEBUG_OPT], |
277 |
"<age>", |
278 |
"Auto archive jobs older than the given age"), |
279 |
'cancel': (CancelJobs, ARGS_ANY, |
280 |
[DEBUG_OPT], |
281 |
"<job-id> [<job-id> ...]", |
282 |
"Cancel specified jobs"), |
283 |
'info': (ShowJobs, ARGS_ANY, [DEBUG_OPT], |
284 |
"<job-id> [<job-id> ...]", |
285 |
"Show detailed information about the specified jobs"), |
286 |
} |
287 |
|
288 |
|
289 |
if __name__ == '__main__': |
290 |
sys.exit(GenericMain(commands)) |