Revision e1c701e7 lib/client/gnt_job.py

b/lib/client/gnt_job.py
60 60
    raise errors.ProgrammerError("Unknown job status code '%s'" % value)
61 61

  
62 62

  
63
_JOB_LIST_FORMAT = {
64
  "status": (_FormatStatus, False),
65
  "summary": (lambda value: ",".join(str(item) for item in value), False),
66
  }
67
_JOB_LIST_FORMAT.update(dict.fromkeys(["opstart", "opexec", "opend"],
68
                                      (lambda value: map(FormatTimestamp,
69
                                                         value),
70
                                       None)))
71

  
72

  
63 73
def _ParseJobIds(args):
64 74
  """Parses a list of string job IDs into integers.
65 75

  
......
90 100
  if opts.archived and "archived" not in selected_fields:
91 101
    selected_fields.append("archived")
92 102

  
93
  fmtoverride = {
94
    "status": (_FormatStatus, False),
95
    "summary": (lambda value: ",".join(str(item) for item in value), False),
96
    }
97
  fmtoverride.update(dict.fromkeys(["opstart", "opexec", "opend"],
98
                                   (lambda value: map(FormatTimestamp, value),
99
                                    None)))
100

  
101 103
  qfilter = qlang.MakeSimpleFilter("status", opts.status_filter)
102 104

  
103 105
  return GenericList(constants.QR_JOB, selected_fields, args, None,
104 106
                     opts.separator, not opts.no_headers,
105
                     format_override=fmtoverride, verbose=opts.verbose,
107
                     format_override=_JOB_LIST_FORMAT, verbose=opts.verbose,
106 108
                     force_filter=opts.force_filter, namefield="id",
107 109
                     qfilter=qfilter, isnumeric=True)
108 110

  
......
172 174
  return 0
173 175

  
174 176

  
175
def CancelJobs(opts, args):
177
def CancelJobs(opts, args, cl=None, _stdout_fn=ToStdout, _ask_fn=AskUser):
176 178
  """Cancel not-yet-started jobs.
177 179

  
178 180
  @param opts: the command line options selected by the user
......
182 184
  @return: the desired exit code
183 185

  
184 186
  """
185
  client = GetClient()
187
  if cl is None:
188
    cl = GetClient()
189

  
186 190
  result = constants.EXIT_SUCCESS
187 191

  
188
  for job_id in args:
189
    (success, msg) = client.CancelJob(job_id)
192
  if bool(args) ^ (opts.status_filter is None):
193
    raise errors.OpPrereqError("Either a status filter or job ID(s) must be"
194
                               " specified and never both", errors.ECODE_INVAL)
195

  
196
  if opts.status_filter is not None:
197
    response = cl.Query(constants.QR_JOB, ["id", "status", "summary"],
198
                        qlang.MakeSimpleFilter("status", opts.status_filter))
199

  
200
    jobs = [i for ((_, i), _, _) in response.data]
201
    if not jobs:
202
      raise errors.OpPrereqError("No jobs with the requested status have been"
203
                                 " found", errors.ECODE_STATE)
204

  
205
    if not opts.force:
206
      (_, table) = FormatQueryResult(response, header=True,
207
                                     format_override=_JOB_LIST_FORMAT)
208
      for line in table:
209
        _stdout_fn(line)
210

  
211
      if not _ask_fn("Cancel job(s) listed above?"):
212
        return constants.EXIT_CONFIRMATION
213
  else:
214
    jobs = args
215

  
216
  for job_id in jobs:
217
    (success, msg) = cl.CancelJob(job_id)
190 218

  
191 219
    if not success:
192 220
      result = constants.EXIT_FAILURE
193 221

  
194
    ToStdout(msg)
222
    _stdout_fn(msg)
195 223

  
196 224
  return result
197 225

  
......
362 390
_PENDING_OPT = \
363 391
  cli_option("--pending", default=None,
364 392
             action="store_const", dest="status_filter",
365
             const=frozenset([
366
               constants.JOB_STATUS_QUEUED,
367
               constants.JOB_STATUS_WAITING,
368
               ]),
369
             help="Show only jobs pending execution")
393
             const=constants.JOBS_PENDING,
394
             help="Select jobs pending execution or being cancelled")
370 395

  
371 396
_RUNNING_OPT = \
372 397
  cli_option("--running", default=None,
......
395 420
             action="store_true", dest="archived",
396 421
             help="Include archived jobs in list (slow and expensive)")
397 422

  
423
_QUEUED_OPT = \
424
  cli_option("--queued", default=None,
425
             action="store_const", dest="status_filter",
426
             const=frozenset([
427
               constants.JOB_STATUS_QUEUED,
428
               ]),
429
             help="Select queued jobs only")
430

  
431
_WAITING_OPT = \
432
  cli_option("--waiting", default=None,
433
             action="store_const", dest="status_filter",
434
             const=frozenset([
435
               constants.JOB_STATUS_WAITING,
436
               ]),
437
             help="Select waiting jobs only")
438

  
398 439

  
399 440
commands = {
400 441
  "list": (
......
420 461
    [],
421 462
    "<age>", "Auto archive jobs older than the given age"),
422 463
  "cancel": (
423
    CancelJobs, [ArgJobId(min=1)], [],
424
    "<job-id> [<job-id> ...]", "Cancel specified jobs"),
464
    CancelJobs, [ArgJobId()],
465
    [FORCE_OPT, _PENDING_OPT, _QUEUED_OPT, _WAITING_OPT],
466
    "{[--force] {--pending | --queued | --waiting} |"
467
    " <job-id> [<job-id> ...]}",
468
    "Cancel jobs"),
425 469
  "info": (
426 470
    ShowJobs, [ArgJobId(min=1)], [],
427 471
    "<job-id> [<job-id> ...]",

Also available in: Unified diff