4 # Copyright (C) 2006, 2007, 2012 Google Inc.
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.
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.
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
21 """Job related commands"""
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
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
37 #: default list of fields for L{ListJobs}
38 _LIST_DEF_FIELDS = ["id", "status", "summary"]
40 #: map converting the job status contants to user-visible
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",
53 def _FormatStatus(value):
54 """Formats a job status.
58 return _USER_JOB_STATUS[value]
60 raise errors.ProgrammerError("Unknown job status code '%s'" % value)
64 "status": (_FormatStatus, False),
65 "summary": (lambda value: ",".join(str(item) for item in value), False),
67 _JOB_LIST_FORMAT.update(dict.fromkeys(["opstart", "opexec", "opend"],
68 (lambda value: map(FormatTimestamp,
73 def _ParseJobIds(args):
74 """Parses a list of string job IDs into integers.
76 @param args: list of strings
77 @return: list of integers
78 @raise OpPrereqError: in case of invalid values
82 return [int(a) for a in args]
83 except (ValueError, TypeError), err:
84 raise errors.OpPrereqError("Invalid job ID passed: %s" % err,
88 def ListJobs(opts, args):
91 @param opts: the command line options selected by the user
93 @param args: should be an empty list
95 @return: the desired exit code
98 selected_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
100 if opts.archived and "archived" not in selected_fields:
101 selected_fields.append("archived")
103 qfilter = qlang.MakeSimpleFilter("status", opts.status_filter)
105 cl = GetClient(query=True)
107 return GenericList(constants.QR_JOB, selected_fields, args, None,
108 opts.separator, not opts.no_headers,
109 format_override=_JOB_LIST_FORMAT, verbose=opts.verbose,
110 force_filter=opts.force_filter, namefield="id",
111 qfilter=qfilter, isnumeric=True, cl=cl)
114 def ListJobFields(opts, args):
117 @param opts: the command line options selected by the user
119 @param args: fields to list, or empty for all
121 @return: the desired exit code
124 cl = GetClient(query=True)
126 return GenericListFields(constants.QR_JOB, args, opts.separator,
127 not opts.no_headers, cl=cl)
130 def ArchiveJobs(opts, args):
133 @param opts: the command line options selected by the user
135 @param args: should contain the job IDs to be archived
137 @return: the desired exit code
144 if not client.ArchiveJob(job_id):
145 ToStderr("Failed to archive job with ID '%s'", job_id)
151 def AutoArchiveJobs(opts, args):
152 """Archive jobs based on age.
154 This will archive jobs based on their age, or all jobs if a 'all' is
157 @param opts: the command line options selected by the user
159 @param args: should contain only one element, the age as a time spec
160 that can be parsed by L{ganeti.cli.ParseTimespec} or the
161 keyword I{all}, which will cause all jobs to be archived
163 @return: the desired exit code
173 age = ParseTimespec(age)
175 (archived_count, jobs_left) = client.AutoArchiveJobs(age)
176 ToStdout("Archived %s jobs, %s unchecked left", archived_count, jobs_left)
181 def _MultiJobAction(opts, args, cl, stdout_fn, ask_fn, question, action_fn):
182 """Applies a function to multipe jobs.
184 @param opts: Command line options
194 if stdout_fn is None:
200 result = constants.EXIT_SUCCESS
202 if bool(args) ^ (opts.status_filter is None):
203 raise errors.OpPrereqError("Either a status filter or job ID(s) must be"
204 " specified and never both", errors.ECODE_INVAL)
206 if opts.status_filter is not None:
207 response = cl.Query(constants.QR_JOB, ["id", "status", "summary"],
208 qlang.MakeSimpleFilter("status", opts.status_filter))
210 jobs = [i for ((_, i), _, _) in response.data]
212 raise errors.OpPrereqError("No jobs with the requested status have been"
213 " found", errors.ECODE_STATE)
216 (_, table) = FormatQueryResult(response, header=True,
217 format_override=_JOB_LIST_FORMAT)
221 if not ask_fn(question):
222 return constants.EXIT_CONFIRMATION
227 (success, msg) = action_fn(cl, job_id)
230 result = constants.EXIT_FAILURE
237 def CancelJobs(opts, args, cl=None, _stdout_fn=ToStdout, _ask_fn=AskUser):
238 """Cancel not-yet-started jobs.
240 @param opts: the command line options selected by the user
242 @param args: should contain the job IDs to be cancelled
244 @return: the desired exit code
247 return _MultiJobAction(opts, args, cl, _stdout_fn, _ask_fn,
248 "Cancel job(s) listed above?",
249 lambda cl, job_id: cl.CancelJob(job_id))
252 def ChangePriority(opts, args):
253 """Change priority of jobs.
255 @param opts: Command line options
262 if opts.priority is None:
263 ToStderr("--priority option must be given.")
264 return constants.EXIT_FAILURE
266 return _MultiJobAction(opts, args, None, None, None,
267 "Change priority of job(s) listed above?",
269 cl.ChangeJobPriority(job_id, opts.priority))
272 def ShowJobs(opts, args):
273 """Show detailed information about jobs.
275 @param opts: the command line options selected by the user
277 @param args: should contain the job IDs to be queried
279 @return: the desired exit code
282 def format_msg(level, text):
283 """Display the text indented."""
284 ToStdout("%s%s", " " * level, text)
286 def result_helper(value):
287 """Format a result field in a nice way."""
288 if isinstance(value, (tuple, list)):
289 return "[%s]" % utils.CommaJoin(value)
294 "id", "status", "ops", "opresult", "opstatus", "oplog",
295 "opstart", "opexec", "opend", "received_ts", "start_ts", "end_ts",
298 qfilter = qlang.MakeSimpleFilter("id", _ParseJobIds(args))
299 cl = GetClient(query=True)
300 result = cl.Query(constants.QR_JOB, selected_fields, qfilter).data
310 ((_, job_id), (rs_status, status), (_, ops), (_, opresult), (_, opstatus),
311 (_, oplog), (_, opstart), (_, opexec), (_, opend), (_, recv_ts),
312 (_, start_ts), (_, end_ts)) = entry
314 # Detect non-normal results
315 if rs_status != constants.RS_NORMAL:
316 format_msg(0, "Job ID %s not found" % job_id)
319 format_msg(0, "Job ID: %s" % job_id)
320 if status in _USER_JOB_STATUS:
321 status = _USER_JOB_STATUS[status]
323 raise errors.ProgrammerError("Unknown job status code '%s'" % status)
325 format_msg(1, "Status: %s" % status)
327 if recv_ts is not None:
328 format_msg(1, "Received: %s" % FormatTimestamp(recv_ts))
330 format_msg(1, "Missing received timestamp (%s)" % str(recv_ts))
332 if start_ts is not None:
333 if recv_ts is not None:
334 d1 = start_ts[0] - recv_ts[0] + (start_ts[1] - recv_ts[1]) / 1000000.0
335 delta = " (delta %.6fs)" % d1
338 format_msg(1, "Processing start: %s%s" %
339 (FormatTimestamp(start_ts), delta))
341 format_msg(1, "Processing start: unknown (%s)" % str(start_ts))
343 if end_ts is not None:
344 if start_ts is not None:
345 d2 = end_ts[0] - start_ts[0] + (end_ts[1] - start_ts[1]) / 1000000.0
346 delta = " (delta %.6fs)" % d2
349 format_msg(1, "Processing end: %s%s" %
350 (FormatTimestamp(end_ts), delta))
352 format_msg(1, "Processing end: unknown (%s)" % str(end_ts))
354 if end_ts is not None and recv_ts is not None:
355 d3 = end_ts[0] - recv_ts[0] + (end_ts[1] - recv_ts[1]) / 1000000.0
356 format_msg(1, "Total processing time: %.6f seconds" % d3)
358 format_msg(1, "Total processing time: N/A")
359 format_msg(1, "Opcodes:")
360 for (opcode, result, status, log, s_ts, x_ts, e_ts) in \
361 zip(ops, opresult, opstatus, oplog, opstart, opexec, opend):
362 format_msg(2, "%s" % opcode["OP_ID"])
363 format_msg(3, "Status: %s" % status)
364 if isinstance(s_ts, (tuple, list)):
365 format_msg(3, "Processing start: %s" % FormatTimestamp(s_ts))
367 format_msg(3, "No processing start time")
368 if isinstance(x_ts, (tuple, list)):
369 format_msg(3, "Execution start: %s" % FormatTimestamp(x_ts))
371 format_msg(3, "No execution start time")
372 if isinstance(e_ts, (tuple, list)):
373 format_msg(3, "Processing end: %s" % FormatTimestamp(e_ts))
375 format_msg(3, "No processing end time")
376 format_msg(3, "Input fields:")
377 for key in utils.NiceSort(opcode.keys()):
381 if isinstance(val, (tuple, list)):
382 val = ",".join([str(item) for item in val])
383 format_msg(4, "%s: %s" % (key, val))
385 format_msg(3, "No output data")
386 elif isinstance(result, (tuple, list)):
388 format_msg(3, "Result: empty sequence")
390 format_msg(3, "Result:")
392 format_msg(4, result_helper(elem))
393 elif isinstance(result, dict):
395 format_msg(3, "Result: empty dictionary")
397 format_msg(3, "Result:")
398 for key, val in result.iteritems():
399 format_msg(4, "%s: %s" % (key, result_helper(val)))
401 format_msg(3, "Result: %s" % result)
402 format_msg(3, "Execution log:")
403 for serial, log_ts, log_type, log_msg in log:
404 time_txt = FormatTimestamp(log_ts)
405 encoded = FormatLogMessage(log_type, log_msg)
406 format_msg(4, "%s:%s:%s %s" % (serial, time_txt, log_type, encoded))
410 def WatchJob(opts, args):
411 """Follow a job and print its output as it arrives.
413 @param opts: the command line options selected by the user
415 @param args: Contains the job ID
417 @return: the desired exit code
422 msg = ("Output from job %s follows" % job_id)
424 ToStdout("-" * len(msg))
429 except errors.GenericError, err:
430 (retcode, job_result) = cli.FormatError(err)
431 ToStderr("Job %s failed: %s", job_id, job_result)
437 cli_option("--pending", default=None,
438 action="store_const", dest="status_filter",
439 const=constants.JOBS_PENDING,
440 help="Select jobs pending execution or being cancelled")
443 cli_option("--running", default=None,
444 action="store_const", dest="status_filter",
446 constants.JOB_STATUS_RUNNING,
448 help="Show jobs currently running only")
451 cli_option("--error", default=None,
452 action="store_const", dest="status_filter",
454 constants.JOB_STATUS_ERROR,
456 help="Show failed jobs only")
459 cli_option("--finished", default=None,
460 action="store_const", dest="status_filter",
461 const=constants.JOBS_FINALIZED,
462 help="Show finished jobs only")
465 cli_option("--archived", default=False,
466 action="store_true", dest="archived",
467 help="Include archived jobs in list (slow and expensive)")
470 cli_option("--queued", default=None,
471 action="store_const", dest="status_filter",
473 constants.JOB_STATUS_QUEUED,
475 help="Select queued jobs only")
478 cli_option("--waiting", default=None,
479 action="store_const", dest="status_filter",
481 constants.JOB_STATUS_WAITING,
483 help="Select waiting jobs only")
488 ListJobs, [ArgJobId()],
489 [NOHDR_OPT, SEP_OPT, FIELDS_OPT, VERBOSE_OPT, FORCE_FILTER_OPT,
490 _PENDING_OPT, _RUNNING_OPT, _ERROR_OPT, _FINISHED_OPT, _ARCHIVED_OPT],
492 "Lists the jobs and their status. The available fields can be shown"
493 " using the \"list-fields\" command (see the man page for details)."
494 " The default field list is (in order): %s." %
495 utils.CommaJoin(_LIST_DEF_FIELDS)),
497 ListJobFields, [ArgUnknown()],
498 [NOHDR_OPT, SEP_OPT],
500 "Lists all available fields for jobs"),
502 ArchiveJobs, [ArgJobId(min=1)], [],
503 "<job-id> [<job-id> ...]", "Archive specified jobs"),
506 [ArgSuggest(min=1, max=1, choices=["1d", "1w", "4w", "all"])],
508 "<age>", "Auto archive jobs older than the given age"),
510 CancelJobs, [ArgJobId()],
511 [FORCE_OPT, _PENDING_OPT, _QUEUED_OPT, _WAITING_OPT],
512 "{[--force] {--pending | --queued | --waiting} |"
513 " <job-id> [<job-id> ...]}",
516 ShowJobs, [ArgJobId(min=1)], [],
517 "<job-id> [<job-id> ...]",
518 "Show detailed information about the specified jobs"),
520 WatchJob, [ArgJobId(min=1, max=1)], [],
521 "<job-id>", "Follows a job and prints its output as it arrives"),
523 ChangePriority, [ArgJobId()],
524 [PRIORITY_OPT, FORCE_OPT, _PENDING_OPT, _QUEUED_OPT, _WAITING_OPT],
525 "--priority <priority> {[--force] {--pending | --queued | --waiting} |"
526 " <job-id> [<job-id> ...]}",
527 "Change the priority of jobs"),
531 #: dictionary with aliases for commands
538 return GenericMain(commands, aliases=aliases)