Handle the result of QueryGroups() correctly
[ganeti-local] / lib / client / gnt_job.py
index 7d12ab3..a7c76a2 100644 (file)
@@ -1,7 +1,7 @@
 #
 #
 
-# Copyright (C) 2006, 2007 Google Inc.
+# Copyright (C) 2006, 2007, 2012 Google Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -60,6 +60,31 @@ def _FormatStatus(value):
     raise errors.ProgrammerError("Unknown job status code '%s'" % value)
 
 
+_JOB_LIST_FORMAT = {
+  "status": (_FormatStatus, False),
+  "summary": (lambda value: ",".join(str(item) for item in value), False),
+  }
+_JOB_LIST_FORMAT.update(dict.fromkeys(["opstart", "opexec", "opend"],
+                                      (lambda value: map(FormatTimestamp,
+                                                         value),
+                                       None)))
+
+
+def _ParseJobIds(args):
+  """Parses a list of string job IDs into integers.
+
+  @param args: list of strings
+  @return: list of integers
+  @raise OpPrereqError: in case of invalid values
+
+  """
+  try:
+    return [int(a) for a in args]
+  except (ValueError, TypeError), err:
+    raise errors.OpPrereqError("Invalid job ID passed: %s" % err,
+                               errors.ECODE_INVAL)
+
+
 def ListJobs(opts, args):
   """List the jobs
 
@@ -72,20 +97,18 @@ def ListJobs(opts, args):
   """
   selected_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
 
-  fmtoverride = {
-    "status": (_FormatStatus, False),
-    "summary": (lambda value: ",".join(str(item) for item in value), False),
-    }
-  fmtoverride.update(dict.fromkeys(["opstart", "opexec", "opend"],
-    (lambda value: map(FormatTimestamp, value), None)))
+  if opts.archived and "archived" not in selected_fields:
+    selected_fields.append("archived")
 
   qfilter = qlang.MakeSimpleFilter("status", opts.status_filter)
 
+  cl = GetClient(query=True)
+
   return GenericList(constants.QR_JOB, selected_fields, args, None,
                      opts.separator, not opts.no_headers,
-                     format_override=fmtoverride, verbose=opts.verbose,
+                     format_override=_JOB_LIST_FORMAT, verbose=opts.verbose,
                      force_filter=opts.force_filter, namefield="id",
-                     qfilter=qfilter)
+                     qfilter=qfilter, isnumeric=True, cl=cl)
 
 
 def ListJobFields(opts, args):
@@ -98,8 +121,10 @@ def ListJobFields(opts, args):
   @return: the desired exit code
 
   """
+  cl = GetClient(query=True)
+
   return GenericListFields(constants.QR_JOB, args, opts.separator,
-                           not opts.no_headers)
+                           not opts.no_headers, cl=cl)
 
 
 def ArchiveJobs(opts, args):
@@ -153,30 +178,97 @@ def AutoArchiveJobs(opts, args):
   return 0
 
 
-def CancelJobs(opts, args):
-  """Cancel not-yet-started jobs.
+def _MultiJobAction(opts, args, cl, stdout_fn, ask_fn, question, action_fn):
+  """Applies a function to multipe jobs.
 
-  @param opts: the command line options selected by the user
+  @param opts: Command line options
   @type args: list
-  @param args: should contain the job IDs to be cancelled
+  @param args: Job IDs
   @rtype: int
-  @return: the desired exit code
+  @return: Exit code
 
   """
-  client = GetClient()
+  if cl is None:
+    cl = GetClient()
+
+  if stdout_fn is None:
+    stdout_fn = ToStdout
+
+  if ask_fn is None:
+    ask_fn = AskUser
+
   result = constants.EXIT_SUCCESS
 
-  for job_id in args:
-    (success, msg) = client.CancelJob(job_id)
+  if bool(args) ^ (opts.status_filter is None):
+    raise errors.OpPrereqError("Either a status filter or job ID(s) must be"
+                               " specified and never both", errors.ECODE_INVAL)
+
+  if opts.status_filter is not None:
+    response = cl.Query(constants.QR_JOB, ["id", "status", "summary"],
+                        qlang.MakeSimpleFilter("status", opts.status_filter))
+
+    jobs = [i for ((_, i), _, _) in response.data]
+    if not jobs:
+      raise errors.OpPrereqError("No jobs with the requested status have been"
+                                 " found", errors.ECODE_STATE)
+
+    if not opts.force:
+      (_, table) = FormatQueryResult(response, header=True,
+                                     format_override=_JOB_LIST_FORMAT)
+      for line in table:
+        stdout_fn(line)
+
+      if not ask_fn(question):
+        return constants.EXIT_CONFIRMATION
+  else:
+    jobs = args
+
+  for job_id in jobs:
+    (success, msg) = action_fn(cl, job_id)
 
     if not success:
       result = constants.EXIT_FAILURE
 
-    ToStdout(msg)
+    stdout_fn(msg)
 
   return result
 
 
+def CancelJobs(opts, args, cl=None, _stdout_fn=ToStdout, _ask_fn=AskUser):
+  """Cancel not-yet-started jobs.
+
+  @param opts: the command line options selected by the user
+  @type args: list
+  @param args: should contain the job IDs to be cancelled
+  @rtype: int
+  @return: the desired exit code
+
+  """
+  return _MultiJobAction(opts, args, cl, _stdout_fn, _ask_fn,
+                         "Cancel job(s) listed above?",
+                         lambda cl, job_id: cl.CancelJob(job_id))
+
+
+def ChangePriority(opts, args):
+  """Change priority of jobs.
+
+  @param opts: Command line options
+  @type args: list
+  @param args: Job IDs
+  @rtype: int
+  @return: Exit code
+
+  """
+  if opts.priority is None:
+    ToStderr("--priority option must be given.")
+    return constants.EXIT_FAILURE
+
+  return _MultiJobAction(opts, args, None, None, None,
+                         "Change priority of job(s) listed above?",
+                         lambda cl, job_id:
+                           cl.ChangeJobPriority(job_id, opts.priority))
+
+
 def ShowJobs(opts, args):
   """Show detailed information about jobs.
 
@@ -203,8 +295,9 @@ def ShowJobs(opts, args):
     "opstart", "opexec", "opend", "received_ts", "start_ts", "end_ts",
     ]
 
-  result = GetClient().Query(constants.QR_JOB, selected_fields,
-                             qlang.MakeSimpleFilter("id", args)).data
+  qfilter = qlang.MakeSimpleFilter("id", _ParseJobIds(args))
+  cl = GetClient(query=True)
+  result = cl.Query(constants.QR_JOB, selected_fields, qfilter).data
 
   first = True
 
@@ -343,11 +436,8 @@ def WatchJob(opts, args):
 _PENDING_OPT = \
   cli_option("--pending", default=None,
              action="store_const", dest="status_filter",
-             const=frozenset([
-               constants.JOB_STATUS_QUEUED,
-               constants.JOB_STATUS_WAITING,
-               ]),
-             help="Show only jobs pending execution")
+             const=constants.JOBS_PENDING,
+             help="Select jobs pending execution or being cancelled")
 
 _RUNNING_OPT = \
   cli_option("--running", default=None,
@@ -371,12 +461,33 @@ _FINISHED_OPT = \
              const=constants.JOBS_FINALIZED,
              help="Show finished jobs only")
 
+_ARCHIVED_OPT = \
+  cli_option("--archived", default=False,
+             action="store_true", dest="archived",
+             help="Include archived jobs in list (slow and expensive)")
+
+_QUEUED_OPT = \
+  cli_option("--queued", default=None,
+             action="store_const", dest="status_filter",
+             const=frozenset([
+               constants.JOB_STATUS_QUEUED,
+               ]),
+             help="Select queued jobs only")
+
+_WAITING_OPT = \
+  cli_option("--waiting", default=None,
+             action="store_const", dest="status_filter",
+             const=frozenset([
+               constants.JOB_STATUS_WAITING,
+               ]),
+             help="Select waiting jobs only")
+
 
 commands = {
   "list": (
     ListJobs, [ArgJobId()],
     [NOHDR_OPT, SEP_OPT, FIELDS_OPT, VERBOSE_OPT, FORCE_FILTER_OPT,
-     _PENDING_OPT, _RUNNING_OPT, _ERROR_OPT, _FINISHED_OPT],
+     _PENDING_OPT, _RUNNING_OPT, _ERROR_OPT, _FINISHED_OPT, _ARCHIVED_OPT],
     "[job_id ...]",
     "Lists the jobs and their status. The available fields can be shown"
     " using the \"list-fields\" command (see the man page for details)."
@@ -396,8 +507,11 @@ commands = {
     [],
     "<age>", "Auto archive jobs older than the given age"),
   "cancel": (
-    CancelJobs, [ArgJobId(min=1)], [],
-    "<job-id> [<job-id> ...]", "Cancel specified jobs"),
+    CancelJobs, [ArgJobId()],
+    [FORCE_OPT, _PENDING_OPT, _QUEUED_OPT, _WAITING_OPT],
+    "{[--force] {--pending | --queued | --waiting} |"
+    " <job-id> [<job-id> ...]}",
+    "Cancel jobs"),
   "info": (
     ShowJobs, [ArgJobId(min=1)], [],
     "<job-id> [<job-id> ...]",
@@ -405,6 +519,12 @@ commands = {
   "watch": (
     WatchJob, [ArgJobId(min=1, max=1)], [],
     "<job-id>", "Follows a job and prints its output as it arrives"),
+  "change-priority": (
+    ChangePriority, [ArgJobId()],
+    [PRIORITY_OPT, FORCE_OPT, _PENDING_OPT, _QUEUED_OPT, _WAITING_OPT],
+    "--priority <priority> {[--force] {--pending | --queued | --waiting} |"
+    " <job-id> [<job-id> ...]}",
+    "Change the priority of jobs"),
   }