root / scripts / gnt-job @ f4ad2ef0
History | View | Annotate | Download (10.8 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 |
"""Job related commands""" |
22 |
|
23 |
# pylint: disable-msg=W0401,W0614,C0103 |
24 |
# W0401: Wildcard import ganeti.cli |
25 |
# W0614: Unused import %s from wildcard import (since we need cli) |
26 |
# C0103: Invalid name gnt-job |
27 |
|
28 |
import sys |
29 |
|
30 |
from ganeti.cli import * |
31 |
from ganeti import constants |
32 |
from ganeti import errors |
33 |
from ganeti import utils |
34 |
from ganeti import cli |
35 |
|
36 |
|
37 |
#: default list of fields for L{ListJobs} |
38 |
_LIST_DEF_FIELDS = ["id", "status", "summary"] |
39 |
|
40 |
#: map converting the job status contants to user-visible |
41 |
#: names |
42 |
_USER_JOB_STATUS = { |
43 |
constants.JOB_STATUS_QUEUED: "queued", |
44 |
constants.JOB_STATUS_WAITLOCK: "waiting", |
45 |
constants.JOB_STATUS_CANCELING: "canceling", |
46 |
constants.JOB_STATUS_RUNNING: "running", |
47 |
constants.JOB_STATUS_CANCELED: "canceled", |
48 |
constants.JOB_STATUS_SUCCESS: "success", |
49 |
constants.JOB_STATUS_ERROR: "error", |
50 |
} |
51 |
|
52 |
|
53 |
def ListJobs(opts, args): |
54 |
"""List the jobs |
55 |
|
56 |
@param opts: the command line options selected by the user |
57 |
@type args: list |
58 |
@param args: should be an empty list |
59 |
@rtype: int |
60 |
@return: the desired exit code |
61 |
|
62 |
""" |
63 |
if opts.output is None: |
64 |
selected_fields = _LIST_DEF_FIELDS |
65 |
elif opts.output.startswith("+"): |
66 |
selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",") |
67 |
else: |
68 |
selected_fields = opts.output.split(",") |
69 |
|
70 |
output = GetClient().QueryJobs(args, selected_fields) |
71 |
if not opts.no_headers: |
72 |
# TODO: Implement more fields |
73 |
headers = { |
74 |
"id": "ID", |
75 |
"status": "Status", |
76 |
"ops": "OpCodes", |
77 |
"opresult": "OpCode_result", |
78 |
"opstatus": "OpCode_status", |
79 |
"oplog": "OpCode_log", |
80 |
"summary": "Summary", |
81 |
"opstart": "OpCode_start", |
82 |
"opend": "OpCode_end", |
83 |
"start_ts": "Start", |
84 |
"end_ts": "End", |
85 |
"received_ts": "Received", |
86 |
"lock_status": "LockStatus", |
87 |
} |
88 |
else: |
89 |
headers = None |
90 |
|
91 |
# change raw values to nicer strings |
92 |
for row_id, row in enumerate(output): |
93 |
if row is None: |
94 |
ToStderr("No such job: %s" % args[row_id]) |
95 |
continue |
96 |
|
97 |
for idx, field in enumerate(selected_fields): |
98 |
val = row[idx] |
99 |
if field == "status": |
100 |
if val in _USER_JOB_STATUS: |
101 |
val = _USER_JOB_STATUS[val] |
102 |
else: |
103 |
raise errors.ProgrammerError("Unknown job status code '%s'" % val) |
104 |
elif field == "summary": |
105 |
val = ",".join(val) |
106 |
elif field in ("start_ts", "end_ts", "received_ts"): |
107 |
val = FormatTimestamp(val) |
108 |
elif field in ("opstart", "opend"): |
109 |
val = [FormatTimestamp(entry) for entry in val] |
110 |
elif field == "lock_status" and not val: |
111 |
val = "-" |
112 |
|
113 |
row[idx] = str(val) |
114 |
|
115 |
data = GenerateTable(separator=opts.separator, headers=headers, |
116 |
fields=selected_fields, data=output) |
117 |
for line in data: |
118 |
ToStdout(line) |
119 |
|
120 |
return 0 |
121 |
|
122 |
|
123 |
def ArchiveJobs(opts, args): |
124 |
"""Archive jobs. |
125 |
|
126 |
@param opts: the command line options selected by the user |
127 |
@type args: list |
128 |
@param args: should contain the job IDs to be archived |
129 |
@rtype: int |
130 |
@return: the desired exit code |
131 |
|
132 |
""" |
133 |
client = GetClient() |
134 |
|
135 |
for job_id in args: |
136 |
client.ArchiveJob(job_id) |
137 |
|
138 |
return 0 |
139 |
|
140 |
|
141 |
def AutoArchiveJobs(opts, args): |
142 |
"""Archive jobs based on age. |
143 |
|
144 |
This will archive jobs based on their age, or all jobs if a 'all' is |
145 |
passed. |
146 |
|
147 |
@param opts: the command line options selected by the user |
148 |
@type args: list |
149 |
@param args: should contain only one element, the age as a time spec |
150 |
that can be parsed by L{ganeti.cli.ParseTimespec} or the |
151 |
keyword I{all}, which will cause all jobs to be archived |
152 |
@rtype: int |
153 |
@return: the desired exit code |
154 |
|
155 |
""" |
156 |
client = GetClient() |
157 |
|
158 |
age = args[0] |
159 |
|
160 |
if age == 'all': |
161 |
age = -1 |
162 |
else: |
163 |
age = ParseTimespec(age) |
164 |
|
165 |
(archived_count, jobs_left) = client.AutoArchiveJobs(age) |
166 |
ToStdout("Archived %s jobs, %s unchecked left", archived_count, jobs_left) |
167 |
|
168 |
return 0 |
169 |
|
170 |
|
171 |
def CancelJobs(opts, args): |
172 |
"""Cancel not-yet-started jobs. |
173 |
|
174 |
@param opts: the command line options selected by the user |
175 |
@type args: list |
176 |
@param args: should contain the job IDs to be cancelled |
177 |
@rtype: int |
178 |
@return: the desired exit code |
179 |
|
180 |
""" |
181 |
client = GetClient() |
182 |
|
183 |
for job_id in args: |
184 |
(_, msg) = client.CancelJob(job_id) |
185 |
ToStdout(msg) |
186 |
|
187 |
# TODO: Different exit value if not all jobs were canceled? |
188 |
return 0 |
189 |
|
190 |
|
191 |
def ShowJobs(opts, args): |
192 |
"""Show detailed information about jobs. |
193 |
|
194 |
@param opts: the command line options selected by the user |
195 |
@type args: list |
196 |
@param args: should contain the job IDs to be queried |
197 |
@rtype: int |
198 |
@return: the desired exit code |
199 |
|
200 |
""" |
201 |
def format(level, text): |
202 |
"""Display the text indented.""" |
203 |
ToStdout("%s%s", " " * level, text) |
204 |
|
205 |
def result_helper(value): |
206 |
"""Format a result field in a nice way.""" |
207 |
if isinstance(value, (tuple, list)): |
208 |
return "[%s]" % utils.CommaJoin(value) |
209 |
else: |
210 |
return str(value) |
211 |
|
212 |
selected_fields = [ |
213 |
"id", "status", "ops", "opresult", "opstatus", "oplog", |
214 |
"opstart", "opend", "received_ts", "start_ts", "end_ts", |
215 |
] |
216 |
|
217 |
result = GetClient().QueryJobs(args, selected_fields) |
218 |
|
219 |
first = True |
220 |
|
221 |
for idx, entry in enumerate(result): |
222 |
if not first: |
223 |
format(0, "") |
224 |
else: |
225 |
first = False |
226 |
|
227 |
if entry is None: |
228 |
if idx <= len(args): |
229 |
format(0, "Job ID %s not found" % args[idx]) |
230 |
else: |
231 |
# this should not happen, when we don't pass args it will be a |
232 |
# valid job returned |
233 |
format(0, "Job ID requested as argument %s not found" % (idx + 1)) |
234 |
continue |
235 |
|
236 |
(job_id, status, ops, opresult, opstatus, oplog, |
237 |
opstart, opend, recv_ts, start_ts, end_ts) = entry |
238 |
format(0, "Job ID: %s" % job_id) |
239 |
if status in _USER_JOB_STATUS: |
240 |
status = _USER_JOB_STATUS[status] |
241 |
else: |
242 |
raise errors.ProgrammerError("Unknown job status code '%s'" % status) |
243 |
|
244 |
format(1, "Status: %s" % status) |
245 |
|
246 |
if recv_ts is not None: |
247 |
format(1, "Received: %s" % FormatTimestamp(recv_ts)) |
248 |
else: |
249 |
format(1, "Missing received timestamp (%s)" % str(recv_ts)) |
250 |
|
251 |
if start_ts is not None: |
252 |
if recv_ts is not None: |
253 |
d1 = start_ts[0] - recv_ts[0] + (start_ts[1] - recv_ts[1]) / 1000000.0 |
254 |
delta = " (delta %.6fs)" % d1 |
255 |
else: |
256 |
delta = "" |
257 |
format(1, "Processing start: %s%s" % (FormatTimestamp(start_ts), delta)) |
258 |
else: |
259 |
format(1, "Processing start: unknown (%s)" % str(start_ts)) |
260 |
|
261 |
if end_ts is not None: |
262 |
if start_ts is not None: |
263 |
d2 = end_ts[0] - start_ts[0] + (end_ts[1] - start_ts[1]) / 1000000.0 |
264 |
delta = " (delta %.6fs)" % d2 |
265 |
else: |
266 |
delta = "" |
267 |
format(1, "Processing end: %s%s" % (FormatTimestamp(end_ts), delta)) |
268 |
else: |
269 |
format(1, "Processing end: unknown (%s)" % str(end_ts)) |
270 |
|
271 |
if end_ts is not None and recv_ts is not None: |
272 |
d3 = end_ts[0] - recv_ts[0] + (end_ts[1] - recv_ts[1]) / 1000000.0 |
273 |
format(1, "Total processing time: %.6f seconds" % d3) |
274 |
else: |
275 |
format(1, "Total processing time: N/A") |
276 |
format(1, "Opcodes:") |
277 |
for (opcode, result, status, log, s_ts, e_ts) in \ |
278 |
zip(ops, opresult, opstatus, oplog, opstart, opend): |
279 |
format(2, "%s" % opcode["OP_ID"]) |
280 |
format(3, "Status: %s" % status) |
281 |
if isinstance(s_ts, (tuple, list)): |
282 |
format(3, "Processing start: %s" % FormatTimestamp(s_ts)) |
283 |
else: |
284 |
format(3, "No processing start time") |
285 |
if isinstance(e_ts, (tuple, list)): |
286 |
format(3, "Processing end: %s" % FormatTimestamp(e_ts)) |
287 |
else: |
288 |
format(3, "No processing end time") |
289 |
format(3, "Input fields:") |
290 |
for key, val in opcode.iteritems(): |
291 |
if key == "OP_ID": |
292 |
continue |
293 |
if isinstance(val, (tuple, list)): |
294 |
val = ",".join([str(item) for item in val]) |
295 |
format(4, "%s: %s" % (key, val)) |
296 |
if result is None: |
297 |
format(3, "No output data") |
298 |
elif isinstance(result, (tuple, list)): |
299 |
if not result: |
300 |
format(3, "Result: empty sequence") |
301 |
else: |
302 |
format(3, "Result:") |
303 |
for elem in result: |
304 |
format(4, result_helper(elem)) |
305 |
elif isinstance(result, dict): |
306 |
if not result: |
307 |
format(3, "Result: empty dictionary") |
308 |
else: |
309 |
for key, val in result.iteritems(): |
310 |
format(4, "%s: %s" % (key, result_helper(val))) |
311 |
else: |
312 |
format(3, "Result: %s" % result) |
313 |
format(3, "Execution log:") |
314 |
for serial, log_ts, log_type, log_msg in log: |
315 |
time_txt = FormatTimestamp(log_ts) |
316 |
encoded = utils.SafeEncode(log_msg) |
317 |
format(4, "%s:%s:%s %s" % (serial, time_txt, log_type, encoded)) |
318 |
return 0 |
319 |
|
320 |
|
321 |
def WatchJob(opts, args): |
322 |
"""Follow a job and print its output as it arrives. |
323 |
|
324 |
@param opts: the command line options selected by the user |
325 |
@type args: list |
326 |
@param args: Contains the job ID |
327 |
@rtype: int |
328 |
@return: the desired exit code |
329 |
|
330 |
""" |
331 |
job_id = args[0] |
332 |
|
333 |
msg = ("Output from job %s follows" % job_id) |
334 |
ToStdout(msg) |
335 |
ToStdout("-" * len(msg)) |
336 |
|
337 |
retcode = 0 |
338 |
try: |
339 |
cli.PollJob(job_id) |
340 |
except errors.GenericError, err: |
341 |
(retcode, job_result) = cli.FormatError(err) |
342 |
ToStderr("Job %s failed: %s", job_id, job_result) |
343 |
|
344 |
return retcode |
345 |
|
346 |
|
347 |
commands = { |
348 |
'list': ( |
349 |
ListJobs, [ArgJobId()], |
350 |
[NOHDR_OPT, SEP_OPT, FIELDS_OPT], |
351 |
"[job_id ...]", |
352 |
"List the jobs and their status. The available fields are" |
353 |
" (see the man page for details): id, status, op_list," |
354 |
" op_status, op_result." |
355 |
" The default field" |
356 |
" list is (in order): %s." % utils.CommaJoin(_LIST_DEF_FIELDS)), |
357 |
'archive': ( |
358 |
ArchiveJobs, [ArgJobId(min=1)], [], |
359 |
"<job-id> [<job-id> ...]", "Archive specified jobs"), |
360 |
'autoarchive': ( |
361 |
AutoArchiveJobs, |
362 |
[ArgSuggest(min=1, max=1, choices=["1d", "1w", "4w"])], |
363 |
[], |
364 |
"<age>", "Auto archive jobs older than the given age"), |
365 |
'cancel': ( |
366 |
CancelJobs, [ArgJobId(min=1)], [], |
367 |
"<job-id> [<job-id> ...]", "Cancel specified jobs"), |
368 |
'info': ( |
369 |
ShowJobs, [ArgJobId(min=1)], [], |
370 |
"<job-id> [<job-id> ...]", |
371 |
"Show detailed information about the specified jobs"), |
372 |
'watch': ( |
373 |
WatchJob, [ArgJobId(min=1, max=1)], [], |
374 |
"<job-id>", "Follows a job and prints its output as it arrives"), |
375 |
} |
376 |
|
377 |
|
378 |
if __name__ == '__main__': |
379 |
sys.exit(GenericMain(commands)) |