Statistics
| Branch: | Tag: | Revision:

root / lib / client / gnt_job.py @ 1fa2c40b

History | View | Annotate | Download (15.8 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
_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

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

76
  @param args: list of strings
77
  @return: list of integers
78
  @raise OpPrereqError: in case of invalid values
79

80
  """
81
  try:
82
    return [int(a) for a in args]
83
  except (ValueError, TypeError), err:
84
    raise errors.OpPrereqError("Invalid job ID passed: %s" % err,
85
                               errors.ECODE_INVAL)
86

    
87

    
88
def ListJobs(opts, args):
89
  """List the jobs
90

91
  @param opts: the command line options selected by the user
92
  @type args: list
93
  @param args: should be an empty list
94
  @rtype: int
95
  @return: the desired exit code
96

97
  """
98
  selected_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
99

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

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

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

    
111

    
112
def ListJobFields(opts, args):
113
  """List job fields.
114

115
  @param opts: the command line options selected by the user
116
  @type args: list
117
  @param args: fields to list, or empty for all
118
  @rtype: int
119
  @return: the desired exit code
120

121
  """
122
  return GenericListFields(constants.QR_JOB, args, opts.separator,
123
                           not opts.no_headers)
124

    
125

    
126
def ArchiveJobs(opts, args):
127
  """Archive jobs.
128

129
  @param opts: the command line options selected by the user
130
  @type args: list
131
  @param args: should contain the job IDs to be archived
132
  @rtype: int
133
  @return: the desired exit code
134

135
  """
136
  client = GetClient()
137

    
138
  rcode = 0
139
  for job_id in args:
140
    if not client.ArchiveJob(job_id):
141
      ToStderr("Failed to archive job with ID '%s'", job_id)
142
      rcode = 1
143

    
144
  return rcode
145

    
146

    
147
def AutoArchiveJobs(opts, args):
148
  """Archive jobs based on age.
149

150
  This will archive jobs based on their age, or all jobs if a 'all' is
151
  passed.
152

153
  @param opts: the command line options selected by the user
154
  @type args: list
155
  @param args: should contain only one element, the age as a time spec
156
      that can be parsed by L{ganeti.cli.ParseTimespec} or the
157
      keyword I{all}, which will cause all jobs to be archived
158
  @rtype: int
159
  @return: the desired exit code
160

161
  """
162
  client = GetClient()
163

    
164
  age = args[0]
165

    
166
  if age == "all":
167
    age = -1
168
  else:
169
    age = ParseTimespec(age)
170

    
171
  (archived_count, jobs_left) = client.AutoArchiveJobs(age)
172
  ToStdout("Archived %s jobs, %s unchecked left", archived_count, jobs_left)
173

    
174
  return 0
175

    
176

    
177
def _MultiJobAction(opts, args, cl, stdout_fn, ask_fn, question, action_fn):
178
  """Applies a function to multipe jobs.
179

180
  @param opts: Command line options
181
  @type args: list
182
  @param args: Job IDs
183
  @rtype: int
184
  @return: Exit code
185

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

    
190
  if stdout_fn is None:
191
    stdout_fn = ToStdout
192

    
193
  if ask_fn is None:
194
    ask_fn = AskUser
195

    
196
  result = constants.EXIT_SUCCESS
197

    
198
  if bool(args) ^ (opts.status_filter is None):
199
    raise errors.OpPrereqError("Either a status filter or job ID(s) must be"
200
                               " specified and never both", errors.ECODE_INVAL)
201

    
202
  if opts.status_filter is not None:
203
    response = cl.Query(constants.QR_JOB, ["id", "status", "summary"],
204
                        qlang.MakeSimpleFilter("status", opts.status_filter))
205

    
206
    jobs = [i for ((_, i), _, _) in response.data]
207
    if not jobs:
208
      raise errors.OpPrereqError("No jobs with the requested status have been"
209
                                 " found", errors.ECODE_STATE)
210

    
211
    if not opts.force:
212
      (_, table) = FormatQueryResult(response, header=True,
213
                                     format_override=_JOB_LIST_FORMAT)
214
      for line in table:
215
        stdout_fn(line)
216

    
217
      if not ask_fn(question):
218
        return constants.EXIT_CONFIRMATION
219
  else:
220
    jobs = args
221

    
222
  for job_id in jobs:
223
    (success, msg) = action_fn(cl, job_id)
224

    
225
    if not success:
226
      result = constants.EXIT_FAILURE
227

    
228
    stdout_fn(msg)
229

    
230
  return result
231

    
232

    
233
def CancelJobs(opts, args, cl=None, _stdout_fn=ToStdout, _ask_fn=AskUser):
234
  """Cancel not-yet-started jobs.
235

236
  @param opts: the command line options selected by the user
237
  @type args: list
238
  @param args: should contain the job IDs to be cancelled
239
  @rtype: int
240
  @return: the desired exit code
241

242
  """
243
  return _MultiJobAction(opts, args, cl, _stdout_fn, _ask_fn,
244
                         "Cancel job(s) listed above?",
245
                         lambda cl, job_id: cl.CancelJob(job_id))
246

    
247

    
248
def ChangePriority(opts, args):
249
  """Change priority of jobs.
250

251
  @param opts: Command line options
252
  @type args: list
253
  @param args: Job IDs
254
  @rtype: int
255
  @return: Exit code
256

257
  """
258
  if opts.priority is None:
259
    ToStderr("--priority option must be given.")
260
    return constants.EXIT_FAILURE
261

    
262
  return _MultiJobAction(opts, args, None, None, None,
263
                         "Change priority of job(s) listed above?",
264
                         lambda cl, job_id:
265
                           cl.ChangeJobPriority(job_id, opts.priority))
266

    
267

    
268
def ShowJobs(opts, args):
269
  """Show detailed information about jobs.
270

271
  @param opts: the command line options selected by the user
272
  @type args: list
273
  @param args: should contain the job IDs to be queried
274
  @rtype: int
275
  @return: the desired exit code
276

277
  """
278
  def format_msg(level, text):
279
    """Display the text indented."""
280
    ToStdout("%s%s", "  " * level, text)
281

    
282
  def result_helper(value):
283
    """Format a result field in a nice way."""
284
    if isinstance(value, (tuple, list)):
285
      return "[%s]" % utils.CommaJoin(value)
286
    else:
287
      return str(value)
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
  result = GetClient().Query(constants.QR_JOB, selected_fields, qfilter).data
296

    
297
  first = True
298

    
299
  for entry in result:
300
    if not first:
301
      format_msg(0, "")
302
    else:
303
      first = False
304

    
305
    ((_, job_id), (rs_status, status), (_, ops), (_, opresult), (_, opstatus),
306
     (_, oplog), (_, opstart), (_, opexec), (_, opend), (_, recv_ts),
307
     (_, start_ts), (_, end_ts)) = entry
308

    
309
    # Detect non-normal results
310
    if rs_status != constants.RS_NORMAL:
311
      format_msg(0, "Job ID %s not found" % job_id)
312
      continue
313

    
314
    format_msg(0, "Job ID: %s" % job_id)
315
    if status in _USER_JOB_STATUS:
316
      status = _USER_JOB_STATUS[status]
317
    else:
318
      raise errors.ProgrammerError("Unknown job status code '%s'" % status)
319

    
320
    format_msg(1, "Status: %s" % status)
321

    
322
    if recv_ts is not None:
323
      format_msg(1, "Received:         %s" % FormatTimestamp(recv_ts))
324
    else:
325
      format_msg(1, "Missing received timestamp (%s)" % str(recv_ts))
326

    
327
    if start_ts is not None:
328
      if recv_ts is not None:
329
        d1 = start_ts[0] - recv_ts[0] + (start_ts[1] - recv_ts[1]) / 1000000.0
330
        delta = " (delta %.6fs)" % d1
331
      else:
332
        delta = ""
333
      format_msg(1, "Processing start: %s%s" %
334
                 (FormatTimestamp(start_ts), delta))
335
    else:
336
      format_msg(1, "Processing start: unknown (%s)" % str(start_ts))
337

    
338
    if end_ts is not None:
339
      if start_ts is not None:
340
        d2 = end_ts[0] - start_ts[0] + (end_ts[1] - start_ts[1]) / 1000000.0
341
        delta = " (delta %.6fs)" % d2
342
      else:
343
        delta = ""
344
      format_msg(1, "Processing end:   %s%s" %
345
                 (FormatTimestamp(end_ts), delta))
346
    else:
347
      format_msg(1, "Processing end:   unknown (%s)" % str(end_ts))
348

    
349
    if end_ts is not None and recv_ts is not None:
350
      d3 = end_ts[0] - recv_ts[0] + (end_ts[1] - recv_ts[1]) / 1000000.0
351
      format_msg(1, "Total processing time: %.6f seconds" % d3)
352
    else:
353
      format_msg(1, "Total processing time: N/A")
354
    format_msg(1, "Opcodes:")
355
    for (opcode, result, status, log, s_ts, x_ts, e_ts) in \
356
            zip(ops, opresult, opstatus, oplog, opstart, opexec, opend):
357
      format_msg(2, "%s" % opcode["OP_ID"])
358
      format_msg(3, "Status: %s" % status)
359
      if isinstance(s_ts, (tuple, list)):
360
        format_msg(3, "Processing start: %s" % FormatTimestamp(s_ts))
361
      else:
362
        format_msg(3, "No processing start time")
363
      if isinstance(x_ts, (tuple, list)):
364
        format_msg(3, "Execution start:  %s" % FormatTimestamp(x_ts))
365
      else:
366
        format_msg(3, "No execution start time")
367
      if isinstance(e_ts, (tuple, list)):
368
        format_msg(3, "Processing end:   %s" % FormatTimestamp(e_ts))
369
      else:
370
        format_msg(3, "No processing end time")
371
      format_msg(3, "Input fields:")
372
      for key in utils.NiceSort(opcode.keys()):
373
        if key == "OP_ID":
374
          continue
375
        val = opcode[key]
376
        if isinstance(val, (tuple, list)):
377
          val = ",".join([str(item) for item in val])
378
        format_msg(4, "%s: %s" % (key, val))
379
      if result is None:
380
        format_msg(3, "No output data")
381
      elif isinstance(result, (tuple, list)):
382
        if not result:
383
          format_msg(3, "Result: empty sequence")
384
        else:
385
          format_msg(3, "Result:")
386
          for elem in result:
387
            format_msg(4, result_helper(elem))
388
      elif isinstance(result, dict):
389
        if not result:
390
          format_msg(3, "Result: empty dictionary")
391
        else:
392
          format_msg(3, "Result:")
393
          for key, val in result.iteritems():
394
            format_msg(4, "%s: %s" % (key, result_helper(val)))
395
      else:
396
        format_msg(3, "Result: %s" % result)
397
      format_msg(3, "Execution log:")
398
      for serial, log_ts, log_type, log_msg in log:
399
        time_txt = FormatTimestamp(log_ts)
400
        encoded = FormatLogMessage(log_type, log_msg)
401
        format_msg(4, "%s:%s:%s %s" % (serial, time_txt, log_type, encoded))
402
  return 0
403

    
404

    
405
def WatchJob(opts, args):
406
  """Follow a job and print its output as it arrives.
407

408
  @param opts: the command line options selected by the user
409
  @type args: list
410
  @param args: Contains the job ID
411
  @rtype: int
412
  @return: the desired exit code
413

414
  """
415
  job_id = args[0]
416

    
417
  msg = ("Output from job %s follows" % job_id)
418
  ToStdout(msg)
419
  ToStdout("-" * len(msg))
420

    
421
  retcode = 0
422
  try:
423
    cli.PollJob(job_id)
424
  except errors.GenericError, err:
425
    (retcode, job_result) = cli.FormatError(err)
426
    ToStderr("Job %s failed: %s", job_id, job_result)
427

    
428
  return retcode
429

    
430

    
431
_PENDING_OPT = \
432
  cli_option("--pending", default=None,
433
             action="store_const", dest="status_filter",
434
             const=constants.JOBS_PENDING,
435
             help="Select jobs pending execution or being cancelled")
436

    
437
_RUNNING_OPT = \
438
  cli_option("--running", default=None,
439
             action="store_const", dest="status_filter",
440
             const=frozenset([
441
               constants.JOB_STATUS_RUNNING,
442
               ]),
443
             help="Show jobs currently running only")
444

    
445
_ERROR_OPT = \
446
  cli_option("--error", default=None,
447
             action="store_const", dest="status_filter",
448
             const=frozenset([
449
               constants.JOB_STATUS_ERROR,
450
               ]),
451
             help="Show failed jobs only")
452

    
453
_FINISHED_OPT = \
454
  cli_option("--finished", default=None,
455
             action="store_const", dest="status_filter",
456
             const=constants.JOBS_FINALIZED,
457
             help="Show finished jobs only")
458

    
459
_ARCHIVED_OPT = \
460
  cli_option("--archived", default=False,
461
             action="store_true", dest="archived",
462
             help="Include archived jobs in list (slow and expensive)")
463

    
464
_QUEUED_OPT = \
465
  cli_option("--queued", default=None,
466
             action="store_const", dest="status_filter",
467
             const=frozenset([
468
               constants.JOB_STATUS_QUEUED,
469
               ]),
470
             help="Select queued jobs only")
471

    
472
_WAITING_OPT = \
473
  cli_option("--waiting", default=None,
474
             action="store_const", dest="status_filter",
475
             const=frozenset([
476
               constants.JOB_STATUS_WAITING,
477
               ]),
478
             help="Select waiting jobs only")
479

    
480

    
481
commands = {
482
  "list": (
483
    ListJobs, [ArgJobId()],
484
    [NOHDR_OPT, SEP_OPT, FIELDS_OPT, VERBOSE_OPT, FORCE_FILTER_OPT,
485
     _PENDING_OPT, _RUNNING_OPT, _ERROR_OPT, _FINISHED_OPT, _ARCHIVED_OPT],
486
    "[job_id ...]",
487
    "Lists the jobs and their status. The available fields can be shown"
488
    " using the \"list-fields\" command (see the man page for details)."
489
    " The default field list is (in order): %s." %
490
    utils.CommaJoin(_LIST_DEF_FIELDS)),
491
  "list-fields": (
492
    ListJobFields, [ArgUnknown()],
493
    [NOHDR_OPT, SEP_OPT],
494
    "[fields...]",
495
    "Lists all available fields for jobs"),
496
  "archive": (
497
    ArchiveJobs, [ArgJobId(min=1)], [],
498
    "<job-id> [<job-id> ...]", "Archive specified jobs"),
499
  "autoarchive": (
500
    AutoArchiveJobs,
501
    [ArgSuggest(min=1, max=1, choices=["1d", "1w", "4w", "all"])],
502
    [],
503
    "<age>", "Auto archive jobs older than the given age"),
504
  "cancel": (
505
    CancelJobs, [ArgJobId()],
506
    [FORCE_OPT, _PENDING_OPT, _QUEUED_OPT, _WAITING_OPT],
507
    "{[--force] {--pending | --queued | --waiting} |"
508
    " <job-id> [<job-id> ...]}",
509
    "Cancel jobs"),
510
  "info": (
511
    ShowJobs, [ArgJobId(min=1)], [],
512
    "<job-id> [<job-id> ...]",
513
    "Show detailed information about the specified jobs"),
514
  "watch": (
515
    WatchJob, [ArgJobId(min=1, max=1)], [],
516
    "<job-id>", "Follows a job and prints its output as it arrives"),
517
  "change-priority": (
518
    ChangePriority, [ArgJobId()],
519
    [PRIORITY_OPT, FORCE_OPT, _PENDING_OPT, _QUEUED_OPT, _WAITING_OPT],
520
    "--priority <priority> {[--force] {--pending | --queued | --waiting} |"
521
    " <job-id> [<job-id> ...]}",
522
    "Change the priority of jobs"),
523
  }
524

    
525

    
526
#: dictionary with aliases for commands
527
aliases = {
528
  "show": "info",
529
  }
530

    
531

    
532
def Main():
533
  return GenericMain(commands, aliases=aliases)