root / lib / client / gnt_job.py @ 087f5520
History | View | Annotate | Download (16.1 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2006, 2007, 2012 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=W0401,W0613,W0614,C0103
|
24 |
# W0401: Wildcard import ganeti.cli
|
25 |
# W0613: Unused argument, since all functions follow the same API
|
26 |
# W0614: Unused import %s from wildcard import (since we need cli)
|
27 |
# C0103: Invalid name gnt-job
|
28 |
|
29 |
from ganeti.cli import * |
30 |
from ganeti import constants |
31 |
from ganeti import errors |
32 |
from ganeti import utils |
33 |
from ganeti import cli |
34 |
from ganeti import qlang |
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_WAITING: "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 _FormatStatus(value): |
54 |
"""Formats a job status.
|
55 |
|
56 |
"""
|
57 |
try:
|
58 |
return _USER_JOB_STATUS[value]
|
59 |
except KeyError: |
60 |
raise errors.ProgrammerError("Unknown job status code '%s'" % value) |
61 |
|
62 |
|
63 |
def _FormatSummary(value): |
64 |
"""Formats a job's summary. Takes possible non-ascii encoding into account.
|
65 |
|
66 |
"""
|
67 |
return ','.encode('utf-8').join(item.encode('utf-8') for item in value) |
68 |
|
69 |
|
70 |
_JOB_LIST_FORMAT = { |
71 |
"status": (_FormatStatus, False), |
72 |
"summary": (_FormatSummary, False), |
73 |
} |
74 |
_JOB_LIST_FORMAT.update(dict.fromkeys(["opstart", "opexec", "opend"], |
75 |
(lambda value: map(FormatTimestamp, |
76 |
value), |
77 |
None)))
|
78 |
|
79 |
|
80 |
def _ParseJobIds(args): |
81 |
"""Parses a list of string job IDs into integers.
|
82 |
|
83 |
@param args: list of strings
|
84 |
@return: list of integers
|
85 |
@raise OpPrereqError: in case of invalid values
|
86 |
|
87 |
"""
|
88 |
try:
|
89 |
return [int(a) for a in args] |
90 |
except (ValueError, TypeError), err: |
91 |
raise errors.OpPrereqError("Invalid job ID passed: %s" % err, |
92 |
errors.ECODE_INVAL) |
93 |
|
94 |
|
95 |
def ListJobs(opts, args): |
96 |
"""List the jobs
|
97 |
|
98 |
@param opts: the command line options selected by the user
|
99 |
@type args: list
|
100 |
@param args: should be an empty list
|
101 |
@rtype: int
|
102 |
@return: the desired exit code
|
103 |
|
104 |
"""
|
105 |
selected_fields = ParseFields(opts.output, _LIST_DEF_FIELDS) |
106 |
|
107 |
if opts.archived and "archived" not in selected_fields: |
108 |
selected_fields.append("archived")
|
109 |
|
110 |
qfilter = qlang.MakeSimpleFilter("status", opts.status_filter)
|
111 |
|
112 |
cl = GetClient() |
113 |
|
114 |
return GenericList(constants.QR_JOB, selected_fields, args, None, |
115 |
opts.separator, not opts.no_headers,
|
116 |
format_override=_JOB_LIST_FORMAT, verbose=opts.verbose, |
117 |
force_filter=opts.force_filter, namefield="id",
|
118 |
qfilter=qfilter, isnumeric=True, cl=cl)
|
119 |
|
120 |
|
121 |
def ListJobFields(opts, args): |
122 |
"""List job fields.
|
123 |
|
124 |
@param opts: the command line options selected by the user
|
125 |
@type args: list
|
126 |
@param args: fields to list, or empty for all
|
127 |
@rtype: int
|
128 |
@return: the desired exit code
|
129 |
|
130 |
"""
|
131 |
cl = GetClient() |
132 |
|
133 |
return GenericListFields(constants.QR_JOB, args, opts.separator,
|
134 |
not opts.no_headers, cl=cl)
|
135 |
|
136 |
|
137 |
def ArchiveJobs(opts, args): |
138 |
"""Archive jobs.
|
139 |
|
140 |
@param opts: the command line options selected by the user
|
141 |
@type args: list
|
142 |
@param args: should contain the job IDs to be archived
|
143 |
@rtype: int
|
144 |
@return: the desired exit code
|
145 |
|
146 |
"""
|
147 |
client = GetClient() |
148 |
|
149 |
rcode = 0
|
150 |
for job_id in args: |
151 |
if not client.ArchiveJob(job_id): |
152 |
ToStderr("Failed to archive job with ID '%s'", job_id)
|
153 |
rcode = 1
|
154 |
|
155 |
return rcode
|
156 |
|
157 |
|
158 |
def AutoArchiveJobs(opts, args): |
159 |
"""Archive jobs based on age.
|
160 |
|
161 |
This will archive jobs based on their age, or all jobs if a 'all' is
|
162 |
passed.
|
163 |
|
164 |
@param opts: the command line options selected by the user
|
165 |
@type args: list
|
166 |
@param args: should contain only one element, the age as a time spec
|
167 |
that can be parsed by L{ganeti.cli.ParseTimespec} or the
|
168 |
keyword I{all}, which will cause all jobs to be archived
|
169 |
@rtype: int
|
170 |
@return: the desired exit code
|
171 |
|
172 |
"""
|
173 |
client = GetClient() |
174 |
|
175 |
age = args[0]
|
176 |
|
177 |
if age == "all": |
178 |
age = -1
|
179 |
else:
|
180 |
age = ParseTimespec(age) |
181 |
|
182 |
(archived_count, jobs_left) = client.AutoArchiveJobs(age) |
183 |
ToStdout("Archived %s jobs, %s unchecked left", archived_count, jobs_left)
|
184 |
|
185 |
return 0 |
186 |
|
187 |
|
188 |
def _MultiJobAction(opts, args, cl, stdout_fn, ask_fn, question, action_fn): |
189 |
"""Applies a function to multipe jobs.
|
190 |
|
191 |
@param opts: Command line options
|
192 |
@type args: list
|
193 |
@param args: Job IDs
|
194 |
@rtype: int
|
195 |
@return: Exit code
|
196 |
|
197 |
"""
|
198 |
if cl is None: |
199 |
cl = GetClient() |
200 |
|
201 |
if stdout_fn is None: |
202 |
stdout_fn = ToStdout |
203 |
|
204 |
if ask_fn is None: |
205 |
ask_fn = AskUser |
206 |
|
207 |
result = constants.EXIT_SUCCESS |
208 |
|
209 |
if bool(args) ^ (opts.status_filter is None): |
210 |
raise errors.OpPrereqError("Either a status filter or job ID(s) must be" |
211 |
" specified and never both", errors.ECODE_INVAL)
|
212 |
|
213 |
if opts.status_filter is not None: |
214 |
response = cl.Query(constants.QR_JOB, ["id", "status", "summary"], |
215 |
qlang.MakeSimpleFilter("status", opts.status_filter))
|
216 |
|
217 |
jobs = [i for ((_, i), _, _) in response.data] |
218 |
if not jobs: |
219 |
raise errors.OpPrereqError("No jobs with the requested status have been" |
220 |
" found", errors.ECODE_STATE)
|
221 |
|
222 |
if not opts.force: |
223 |
(_, table) = FormatQueryResult(response, header=True,
|
224 |
format_override=_JOB_LIST_FORMAT) |
225 |
for line in table: |
226 |
stdout_fn(line) |
227 |
|
228 |
if not ask_fn(question): |
229 |
return constants.EXIT_CONFIRMATION
|
230 |
else:
|
231 |
jobs = args |
232 |
|
233 |
for job_id in jobs: |
234 |
(success, msg) = action_fn(cl, job_id) |
235 |
|
236 |
if not success: |
237 |
result = constants.EXIT_FAILURE |
238 |
|
239 |
stdout_fn(msg) |
240 |
|
241 |
return result
|
242 |
|
243 |
|
244 |
def CancelJobs(opts, args, cl=None, _stdout_fn=ToStdout, _ask_fn=AskUser): |
245 |
"""Cancel not-yet-started jobs.
|
246 |
|
247 |
@param opts: the command line options selected by the user
|
248 |
@type args: list
|
249 |
@param args: should contain the job IDs to be cancelled
|
250 |
@rtype: int
|
251 |
@return: the desired exit code
|
252 |
|
253 |
"""
|
254 |
return _MultiJobAction(opts, args, cl, _stdout_fn, _ask_fn,
|
255 |
"Cancel job(s) listed above?",
|
256 |
lambda cl, job_id: cl.CancelJob(job_id))
|
257 |
|
258 |
|
259 |
def ChangePriority(opts, args): |
260 |
"""Change priority of jobs.
|
261 |
|
262 |
@param opts: Command line options
|
263 |
@type args: list
|
264 |
@param args: Job IDs
|
265 |
@rtype: int
|
266 |
@return: Exit code
|
267 |
|
268 |
"""
|
269 |
if opts.priority is None: |
270 |
ToStderr("--priority option must be given.")
|
271 |
return constants.EXIT_FAILURE
|
272 |
|
273 |
return _MultiJobAction(opts, args, None, None, None, |
274 |
"Change priority of job(s) listed above?",
|
275 |
lambda cl, job_id:
|
276 |
cl.ChangeJobPriority(job_id, opts.priority)) |
277 |
|
278 |
|
279 |
def ShowJobs(opts, args): |
280 |
"""Show detailed information about jobs.
|
281 |
|
282 |
@param opts: the command line options selected by the user
|
283 |
@type args: list
|
284 |
@param args: should contain the job IDs to be queried
|
285 |
@rtype: int
|
286 |
@return: the desired exit code
|
287 |
|
288 |
"""
|
289 |
selected_fields = [ |
290 |
"id", "status", "ops", "opresult", "opstatus", "oplog", |
291 |
"opstart", "opexec", "opend", "received_ts", "start_ts", "end_ts", |
292 |
] |
293 |
|
294 |
qfilter = qlang.MakeSimpleFilter("id", _ParseJobIds(args))
|
295 |
cl = GetClient() |
296 |
result = cl.Query(constants.QR_JOB, selected_fields, qfilter).data |
297 |
|
298 |
job_info_container = [] |
299 |
|
300 |
for entry in result: |
301 |
((_, job_id), (rs_status, status), (_, ops), (_, opresult), (_, opstatus), |
302 |
(_, oplog), (_, opstart), (_, opexec), (_, opend), (_, recv_ts), |
303 |
(_, start_ts), (_, end_ts)) = entry |
304 |
|
305 |
# Detect non-normal results
|
306 |
if rs_status != constants.RS_NORMAL:
|
307 |
job_info_container.append("Job ID %s not found" % job_id)
|
308 |
continue
|
309 |
|
310 |
# Container for produced data
|
311 |
job_info = [("Job ID", job_id)]
|
312 |
|
313 |
if status in _USER_JOB_STATUS: |
314 |
status = _USER_JOB_STATUS[status] |
315 |
else:
|
316 |
raise errors.ProgrammerError("Unknown job status code '%s'" % status) |
317 |
|
318 |
job_info.append(("Status", status))
|
319 |
|
320 |
if recv_ts is not None: |
321 |
job_info.append(("Received", FormatTimestamp(recv_ts)))
|
322 |
else:
|
323 |
job_info.append(("Received", "unknown (%s)" % str(recv_ts))) |
324 |
|
325 |
if start_ts is not None: |
326 |
if recv_ts is not None: |
327 |
d1 = start_ts[0] - recv_ts[0] + (start_ts[1] - recv_ts[1]) / 1000000.0 |
328 |
delta = " (delta %.6fs)" % d1
|
329 |
else:
|
330 |
delta = ""
|
331 |
job_info.append(("Processing start", "%s%s" % |
332 |
(FormatTimestamp(start_ts), delta))) |
333 |
else:
|
334 |
job_info.append(("Processing start", "unknown (%s)" % str(start_ts))) |
335 |
|
336 |
if end_ts is not None: |
337 |
if start_ts is not None: |
338 |
d2 = end_ts[0] - start_ts[0] + (end_ts[1] - start_ts[1]) / 1000000.0 |
339 |
delta = " (delta %.6fs)" % d2
|
340 |
else:
|
341 |
delta = ""
|
342 |
job_info.append(("Processing end", "%s%s" % |
343 |
(FormatTimestamp(end_ts), delta))) |
344 |
else:
|
345 |
job_info.append(("Processing end", "unknown (%s)" % str(end_ts))) |
346 |
|
347 |
if end_ts is not None and recv_ts is not None: |
348 |
d3 = end_ts[0] - recv_ts[0] + (end_ts[1] - recv_ts[1]) / 1000000.0 |
349 |
job_info.append(("Total processing time", "%.6f seconds" % d3)) |
350 |
else:
|
351 |
job_info.append(("Total processing time", "N/A")) |
352 |
|
353 |
opcode_container = [] |
354 |
for (opcode, result, status, log, s_ts, x_ts, e_ts) in \ |
355 |
zip(ops, opresult, opstatus, oplog, opstart, opexec, opend):
|
356 |
opcode_info = [] |
357 |
opcode_info.append(("Opcode", opcode["OP_ID"])) |
358 |
opcode_info.append(("Status", status))
|
359 |
|
360 |
if isinstance(s_ts, (tuple, list)): |
361 |
opcode_info.append(("Processing start", FormatTimestamp(s_ts)))
|
362 |
else:
|
363 |
opcode_info.append(("Processing start", "N/A")) |
364 |
|
365 |
if isinstance(x_ts, (tuple, list)): |
366 |
opcode_info.append(("Execution start", FormatTimestamp(x_ts)))
|
367 |
else:
|
368 |
opcode_info.append(("Execution start", "N/A")) |
369 |
|
370 |
if isinstance(e_ts, (tuple, list)): |
371 |
opcode_info.append(("Processing end", FormatTimestamp(e_ts)))
|
372 |
else:
|
373 |
opcode_info.append(("Processing end", "N/A")) |
374 |
|
375 |
opcode_info.append(("Input fields", opcode))
|
376 |
opcode_info.append(("Result", result))
|
377 |
|
378 |
exec_log_container = [] |
379 |
for serial, log_ts, log_type, log_msg in log: |
380 |
time_txt = FormatTimestamp(log_ts) |
381 |
encoded = FormatLogMessage(log_type, log_msg) |
382 |
|
383 |
# Arranged in this curious way to preserve the brevity for multiple
|
384 |
# logs. This content cannot be exposed as a 4-tuple, as time contains
|
385 |
# the colon, causing some YAML parsers to fail.
|
386 |
exec_log_info = [("Time", time_txt),
|
387 |
("Content", (serial, log_type, encoded,)),
|
388 |
] |
389 |
exec_log_container.append(exec_log_info) |
390 |
opcode_info.append(("Execution log", exec_log_container))
|
391 |
|
392 |
opcode_container.append(opcode_info) |
393 |
|
394 |
job_info.append(("Opcodes", opcode_container))
|
395 |
job_info_container.append(job_info) |
396 |
|
397 |
PrintGenericInfo(job_info_container) |
398 |
|
399 |
return 0 |
400 |
|
401 |
|
402 |
def WatchJob(opts, args): |
403 |
"""Follow a job and print its output as it arrives.
|
404 |
|
405 |
@param opts: the command line options selected by the user
|
406 |
@type args: list
|
407 |
@param args: Contains the job ID
|
408 |
@rtype: int
|
409 |
@return: the desired exit code
|
410 |
|
411 |
"""
|
412 |
job_id = args[0]
|
413 |
|
414 |
msg = ("Output from job %s follows" % job_id)
|
415 |
ToStdout(msg) |
416 |
ToStdout("-" * len(msg)) |
417 |
|
418 |
retcode = 0
|
419 |
try:
|
420 |
cli.PollJob(job_id) |
421 |
except errors.GenericError, err:
|
422 |
(retcode, job_result) = cli.FormatError(err) |
423 |
ToStderr("Job %s failed: %s", job_id, job_result)
|
424 |
|
425 |
return retcode
|
426 |
|
427 |
|
428 |
def WaitJob(opts, args): |
429 |
"""Wait for a job to finish, not producing any output.
|
430 |
|
431 |
@param opts: the command line options selected by the user
|
432 |
@type args: list
|
433 |
@param args: Contains the job ID
|
434 |
@rtype: int
|
435 |
@return: the desired exit code
|
436 |
|
437 |
"""
|
438 |
job_id = args[0]
|
439 |
|
440 |
retcode = 0
|
441 |
try:
|
442 |
cli.PollJob(job_id, feedback_fn=lambda _: None) |
443 |
except errors.GenericError, err:
|
444 |
(retcode, job_result) = cli.FormatError(err) |
445 |
ToStderr("Job %s failed: %s", job_id, job_result)
|
446 |
|
447 |
return retcode
|
448 |
|
449 |
|
450 |
_PENDING_OPT = \ |
451 |
cli_option("--pending", default=None, |
452 |
action="store_const", dest="status_filter", |
453 |
const=constants.JOBS_PENDING, |
454 |
help="Select jobs pending execution or being cancelled")
|
455 |
|
456 |
_RUNNING_OPT = \ |
457 |
cli_option("--running", default=None, |
458 |
action="store_const", dest="status_filter", |
459 |
const=frozenset([
|
460 |
constants.JOB_STATUS_RUNNING, |
461 |
]), |
462 |
help="Show jobs currently running only")
|
463 |
|
464 |
_ERROR_OPT = \ |
465 |
cli_option("--error", default=None, |
466 |
action="store_const", dest="status_filter", |
467 |
const=frozenset([
|
468 |
constants.JOB_STATUS_ERROR, |
469 |
]), |
470 |
help="Show failed jobs only")
|
471 |
|
472 |
_FINISHED_OPT = \ |
473 |
cli_option("--finished", default=None, |
474 |
action="store_const", dest="status_filter", |
475 |
const=constants.JOBS_FINALIZED, |
476 |
help="Show finished jobs only")
|
477 |
|
478 |
_ARCHIVED_OPT = \ |
479 |
cli_option("--archived", default=False, |
480 |
action="store_true", dest="archived", |
481 |
help="Include archived jobs in list (slow and expensive)")
|
482 |
|
483 |
_QUEUED_OPT = \ |
484 |
cli_option("--queued", default=None, |
485 |
action="store_const", dest="status_filter", |
486 |
const=frozenset([
|
487 |
constants.JOB_STATUS_QUEUED, |
488 |
]), |
489 |
help="Select queued jobs only")
|
490 |
|
491 |
_WAITING_OPT = \ |
492 |
cli_option("--waiting", default=None, |
493 |
action="store_const", dest="status_filter", |
494 |
const=frozenset([
|
495 |
constants.JOB_STATUS_WAITING, |
496 |
]), |
497 |
help="Select waiting jobs only")
|
498 |
|
499 |
|
500 |
commands = { |
501 |
"list": (
|
502 |
ListJobs, [ArgJobId()], |
503 |
[NOHDR_OPT, SEP_OPT, FIELDS_OPT, VERBOSE_OPT, FORCE_FILTER_OPT, |
504 |
_PENDING_OPT, _RUNNING_OPT, _ERROR_OPT, _FINISHED_OPT, _ARCHIVED_OPT], |
505 |
"[job_id ...]",
|
506 |
"Lists the jobs and their status. The available fields can be shown"
|
507 |
" using the \"list-fields\" command (see the man page for details)."
|
508 |
" The default field list is (in order): %s." %
|
509 |
utils.CommaJoin(_LIST_DEF_FIELDS)), |
510 |
"list-fields": (
|
511 |
ListJobFields, [ArgUnknown()], |
512 |
[NOHDR_OPT, SEP_OPT], |
513 |
"[fields...]",
|
514 |
"Lists all available fields for jobs"),
|
515 |
"archive": (
|
516 |
ArchiveJobs, [ArgJobId(min=1)], [],
|
517 |
"<job-id> [<job-id> ...]", "Archive specified jobs"), |
518 |
"autoarchive": (
|
519 |
AutoArchiveJobs, |
520 |
[ArgSuggest(min=1, max=1, choices=["1d", "1w", "4w", "all"])], |
521 |
[], |
522 |
"<age>", "Auto archive jobs older than the given age"), |
523 |
"cancel": (
|
524 |
CancelJobs, [ArgJobId()], |
525 |
[FORCE_OPT, _PENDING_OPT, _QUEUED_OPT, _WAITING_OPT], |
526 |
"{[--force] {--pending | --queued | --waiting} |"
|
527 |
" <job-id> [<job-id> ...]}",
|
528 |
"Cancel jobs"),
|
529 |
"info": (
|
530 |
ShowJobs, [ArgJobId(min=1)], [],
|
531 |
"<job-id> [<job-id> ...]",
|
532 |
"Show detailed information about the specified jobs"),
|
533 |
"wait": (
|
534 |
WaitJob, [ArgJobId(min=1, max=1)], [], |
535 |
"<job-id>", "Wait for a job to finish"), |
536 |
"watch": (
|
537 |
WatchJob, [ArgJobId(min=1, max=1)], [], |
538 |
"<job-id>", "Follows a job and prints its output as it arrives"), |
539 |
"change-priority": (
|
540 |
ChangePriority, [ArgJobId()], |
541 |
[PRIORITY_OPT, FORCE_OPT, _PENDING_OPT, _QUEUED_OPT, _WAITING_OPT], |
542 |
"--priority <priority> {[--force] {--pending | --queued | --waiting} |"
|
543 |
" <job-id> [<job-id> ...]}",
|
544 |
"Change the priority of jobs"),
|
545 |
} |
546 |
|
547 |
|
548 |
#: dictionary with aliases for commands
|
549 |
aliases = { |
550 |
"show": "info", |
551 |
} |
552 |
|
553 |
|
554 |
def Main(): |
555 |
return GenericMain(commands, aliases=aliases)
|