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)
63 def _FormatSummary(value):
64 """Formats a job's summary. Takes possible non-ascii encoding into account.
67 return ','.encode('utf-8').join(item.encode('utf-8') for item in value)
71 "status": (_FormatStatus, False),
72 "summary": (_FormatSummary, False),
74 _JOB_LIST_FORMAT.update(dict.fromkeys(["opstart", "opexec", "opend"],
75 (lambda value: map(FormatTimestamp,
80 def _ParseJobIds(args):
81 """Parses a list of string job IDs into integers.
83 @param args: list of strings
84 @return: list of integers
85 @raise OpPrereqError: in case of invalid values
89 return [int(a) for a in args]
90 except (ValueError, TypeError), err:
91 raise errors.OpPrereqError("Invalid job ID passed: %s" % err,
95 def ListJobs(opts, args):
98 @param opts: the command line options selected by the user
100 @param args: should be an empty list
102 @return: the desired exit code
105 selected_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
107 if opts.archived and "archived" not in selected_fields:
108 selected_fields.append("archived")
110 qfilter = qlang.MakeSimpleFilter("status", opts.status_filter)
112 cl = GetClient(query=True)
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)
121 def ListJobFields(opts, args):
124 @param opts: the command line options selected by the user
126 @param args: fields to list, or empty for all
128 @return: the desired exit code
131 cl = GetClient(query=True)
133 return GenericListFields(constants.QR_JOB, args, opts.separator,
134 not opts.no_headers, cl=cl)
137 def ArchiveJobs(opts, args):
140 @param opts: the command line options selected by the user
142 @param args: should contain the job IDs to be archived
144 @return: the desired exit code
151 if not client.ArchiveJob(job_id):
152 ToStderr("Failed to archive job with ID '%s'", job_id)
158 def AutoArchiveJobs(opts, args):
159 """Archive jobs based on age.
161 This will archive jobs based on their age, or all jobs if a 'all' is
164 @param opts: the command line options selected by the user
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
170 @return: the desired exit code
180 age = ParseTimespec(age)
182 (archived_count, jobs_left) = client.AutoArchiveJobs(age)
183 ToStdout("Archived %s jobs, %s unchecked left", archived_count, jobs_left)
188 def _MultiJobAction(opts, args, cl, stdout_fn, ask_fn, question, action_fn):
189 """Applies a function to multipe jobs.
191 @param opts: Command line options
201 if stdout_fn is None:
207 result = constants.EXIT_SUCCESS
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)
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))
217 jobs = [i for ((_, i), _, _) in response.data]
219 raise errors.OpPrereqError("No jobs with the requested status have been"
220 " found", errors.ECODE_STATE)
223 (_, table) = FormatQueryResult(response, header=True,
224 format_override=_JOB_LIST_FORMAT)
228 if not ask_fn(question):
229 return constants.EXIT_CONFIRMATION
234 (success, msg) = action_fn(cl, job_id)
237 result = constants.EXIT_FAILURE
244 def CancelJobs(opts, args, cl=None, _stdout_fn=ToStdout, _ask_fn=AskUser):
245 """Cancel not-yet-started jobs.
247 @param opts: the command line options selected by the user
249 @param args: should contain the job IDs to be cancelled
251 @return: the desired exit code
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))
259 def ChangePriority(opts, args):
260 """Change priority of jobs.
262 @param opts: Command line options
269 if opts.priority is None:
270 ToStderr("--priority option must be given.")
271 return constants.EXIT_FAILURE
273 return _MultiJobAction(opts, args, None, None, None,
274 "Change priority of job(s) listed above?",
276 cl.ChangeJobPriority(job_id, opts.priority))
279 def ShowJobs(opts, args):
280 """Show detailed information about jobs.
282 @param opts: the command line options selected by the user
284 @param args: should contain the job IDs to be queried
286 @return: the desired exit code
289 def format_msg(level, text):
290 """Display the text indented."""
291 ToStdout("%s%s", " " * level, text)
293 def result_helper(value):
294 """Format a result field in a nice way."""
295 if isinstance(value, (tuple, list)):
296 return "[%s]" % utils.CommaJoin(value)
301 "id", "status", "ops", "opresult", "opstatus", "oplog",
302 "opstart", "opexec", "opend", "received_ts", "start_ts", "end_ts",
305 qfilter = qlang.MakeSimpleFilter("id", _ParseJobIds(args))
306 cl = GetClient(query=True)
307 result = cl.Query(constants.QR_JOB, selected_fields, qfilter).data
317 ((_, job_id), (rs_status, status), (_, ops), (_, opresult), (_, opstatus),
318 (_, oplog), (_, opstart), (_, opexec), (_, opend), (_, recv_ts),
319 (_, start_ts), (_, end_ts)) = entry
321 # Detect non-normal results
322 if rs_status != constants.RS_NORMAL:
323 format_msg(0, "Job ID %s not found" % job_id)
326 format_msg(0, "Job ID: %s" % job_id)
327 if status in _USER_JOB_STATUS:
328 status = _USER_JOB_STATUS[status]
330 raise errors.ProgrammerError("Unknown job status code '%s'" % status)
332 format_msg(1, "Status: %s" % status)
334 if recv_ts is not None:
335 format_msg(1, "Received: %s" % FormatTimestamp(recv_ts))
337 format_msg(1, "Missing received timestamp (%s)" % str(recv_ts))
339 if start_ts is not None:
340 if recv_ts is not None:
341 d1 = start_ts[0] - recv_ts[0] + (start_ts[1] - recv_ts[1]) / 1000000.0
342 delta = " (delta %.6fs)" % d1
345 format_msg(1, "Processing start: %s%s" %
346 (FormatTimestamp(start_ts), delta))
348 format_msg(1, "Processing start: unknown (%s)" % str(start_ts))
350 if end_ts is not None:
351 if start_ts is not None:
352 d2 = end_ts[0] - start_ts[0] + (end_ts[1] - start_ts[1]) / 1000000.0
353 delta = " (delta %.6fs)" % d2
356 format_msg(1, "Processing end: %s%s" %
357 (FormatTimestamp(end_ts), delta))
359 format_msg(1, "Processing end: unknown (%s)" % str(end_ts))
361 if end_ts is not None and recv_ts is not None:
362 d3 = end_ts[0] - recv_ts[0] + (end_ts[1] - recv_ts[1]) / 1000000.0
363 format_msg(1, "Total processing time: %.6f seconds" % d3)
365 format_msg(1, "Total processing time: N/A")
366 format_msg(1, "Opcodes:")
367 for (opcode, result, status, log, s_ts, x_ts, e_ts) in \
368 zip(ops, opresult, opstatus, oplog, opstart, opexec, opend):
369 format_msg(2, "%s" % opcode["OP_ID"])
370 format_msg(3, "Status: %s" % status)
371 if isinstance(s_ts, (tuple, list)):
372 format_msg(3, "Processing start: %s" % FormatTimestamp(s_ts))
374 format_msg(3, "No processing start time")
375 if isinstance(x_ts, (tuple, list)):
376 format_msg(3, "Execution start: %s" % FormatTimestamp(x_ts))
378 format_msg(3, "No execution start time")
379 if isinstance(e_ts, (tuple, list)):
380 format_msg(3, "Processing end: %s" % FormatTimestamp(e_ts))
382 format_msg(3, "No processing end time")
383 format_msg(3, "Input fields:")
384 for key in utils.NiceSort(opcode.keys()):
388 if isinstance(val, (tuple, list)):
389 val = ",".join([str(item) for item in val])
390 format_msg(4, "%s: %s" % (key, val))
392 format_msg(3, "No output data")
393 elif isinstance(result, (tuple, list)):
395 format_msg(3, "Result: empty sequence")
397 format_msg(3, "Result:")
399 format_msg(4, result_helper(elem))
400 elif isinstance(result, dict):
402 format_msg(3, "Result: empty dictionary")
404 format_msg(3, "Result:")
405 for key, val in result.iteritems():
406 format_msg(4, "%s: %s" % (key, result_helper(val)))
408 format_msg(3, "Result: %s" % result)
409 format_msg(3, "Execution log:")
410 for serial, log_ts, log_type, log_msg in log:
411 time_txt = FormatTimestamp(log_ts)
412 encoded = FormatLogMessage(log_type, log_msg)
413 format_msg(4, "%s:%s:%s %s" % (serial, time_txt, log_type, encoded))
417 def WatchJob(opts, args):
418 """Follow a job and print its output as it arrives.
420 @param opts: the command line options selected by the user
422 @param args: Contains the job ID
424 @return: the desired exit code
429 msg = ("Output from job %s follows" % job_id)
431 ToStdout("-" * len(msg))
436 except errors.GenericError, err:
437 (retcode, job_result) = cli.FormatError(err)
438 ToStderr("Job %s failed: %s", job_id, job_result)
444 cli_option("--pending", default=None,
445 action="store_const", dest="status_filter",
446 const=constants.JOBS_PENDING,
447 help="Select jobs pending execution or being cancelled")
450 cli_option("--running", default=None,
451 action="store_const", dest="status_filter",
453 constants.JOB_STATUS_RUNNING,
455 help="Show jobs currently running only")
458 cli_option("--error", default=None,
459 action="store_const", dest="status_filter",
461 constants.JOB_STATUS_ERROR,
463 help="Show failed jobs only")
466 cli_option("--finished", default=None,
467 action="store_const", dest="status_filter",
468 const=constants.JOBS_FINALIZED,
469 help="Show finished jobs only")
472 cli_option("--archived", default=False,
473 action="store_true", dest="archived",
474 help="Include archived jobs in list (slow and expensive)")
477 cli_option("--queued", default=None,
478 action="store_const", dest="status_filter",
480 constants.JOB_STATUS_QUEUED,
482 help="Select queued jobs only")
485 cli_option("--waiting", default=None,
486 action="store_const", dest="status_filter",
488 constants.JOB_STATUS_WAITING,
490 help="Select waiting jobs only")
495 ListJobs, [ArgJobId()],
496 [NOHDR_OPT, SEP_OPT, FIELDS_OPT, VERBOSE_OPT, FORCE_FILTER_OPT,
497 _PENDING_OPT, _RUNNING_OPT, _ERROR_OPT, _FINISHED_OPT, _ARCHIVED_OPT],
499 "Lists the jobs and their status. The available fields can be shown"
500 " using the \"list-fields\" command (see the man page for details)."
501 " The default field list is (in order): %s." %
502 utils.CommaJoin(_LIST_DEF_FIELDS)),
504 ListJobFields, [ArgUnknown()],
505 [NOHDR_OPT, SEP_OPT],
507 "Lists all available fields for jobs"),
509 ArchiveJobs, [ArgJobId(min=1)], [],
510 "<job-id> [<job-id> ...]", "Archive specified jobs"),
513 [ArgSuggest(min=1, max=1, choices=["1d", "1w", "4w", "all"])],
515 "<age>", "Auto archive jobs older than the given age"),
517 CancelJobs, [ArgJobId()],
518 [FORCE_OPT, _PENDING_OPT, _QUEUED_OPT, _WAITING_OPT],
519 "{[--force] {--pending | --queued | --waiting} |"
520 " <job-id> [<job-id> ...]}",
523 ShowJobs, [ArgJobId(min=1)], [],
524 "<job-id> [<job-id> ...]",
525 "Show detailed information about the specified jobs"),
527 WatchJob, [ArgJobId(min=1, max=1)], [],
528 "<job-id>", "Follows a job and prints its output as it arrives"),
530 ChangePriority, [ArgJobId()],
531 [PRIORITY_OPT, FORCE_OPT, _PENDING_OPT, _QUEUED_OPT, _WAITING_OPT],
532 "--priority <priority> {[--force] {--pending | --queued | --waiting} |"
533 " <job-id> [<job-id> ...]}",
534 "Change the priority of jobs"),
538 #: dictionary with aliases for commands
545 return GenericMain(commands, aliases=aliases)