Statistics
| Branch: | Tag: | Revision:

root / lib / client / gnt_job.py @ 3086220e

History | View | Annotate | Download (11 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007 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
def ListJobs(opts, args):
64
  """List the jobs
65

66
  @param opts: the command line options selected by the user
67
  @type args: list
68
  @param args: should be an empty list
69
  @rtype: int
70
  @return: the desired exit code
71

72
  """
73
  selected_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
74

    
75
  fmtoverride = {
76
    "status": (_FormatStatus, False),
77
    "summary": (lambda value: ",".join(str(item) for item in value), False),
78
    }
79
  fmtoverride.update(dict.fromkeys(["opstart", "opexec", "opend"],
80
    (lambda value: map(FormatTimestamp, value), None)))
81

    
82
  return GenericList(constants.QR_JOB, selected_fields, args, None,
83
                     opts.separator, not opts.no_headers,
84
                     format_override=fmtoverride, verbose=opts.verbose,
85
                     force_filter=opts.force_filter, namefield="id")
86

    
87

    
88
def ListJobFields(opts, args):
89
  """List job fields.
90

91
  @param opts: the command line options selected by the user
92
  @type args: list
93
  @param args: fields to list, or empty for all
94
  @rtype: int
95
  @return: the desired exit code
96

97
  """
98
  return GenericListFields(constants.QR_JOB, args, opts.separator,
99
                           not opts.no_headers)
100

    
101

    
102
def ArchiveJobs(opts, args):
103
  """Archive jobs.
104

105
  @param opts: the command line options selected by the user
106
  @type args: list
107
  @param args: should contain the job IDs to be archived
108
  @rtype: int
109
  @return: the desired exit code
110

111
  """
112
  client = GetClient()
113

    
114
  rcode = 0
115
  for job_id in args:
116
    if not client.ArchiveJob(job_id):
117
      ToStderr("Failed to archive job with ID '%s'", job_id)
118
      rcode = 1
119

    
120
  return rcode
121

    
122

    
123
def AutoArchiveJobs(opts, args):
124
  """Archive jobs based on age.
125

126
  This will archive jobs based on their age, or all jobs if a 'all' is
127
  passed.
128

129
  @param opts: the command line options selected by the user
130
  @type args: list
131
  @param args: should contain only one element, the age as a time spec
132
      that can be parsed by L{ganeti.cli.ParseTimespec} or the
133
      keyword I{all}, which will cause all jobs to be archived
134
  @rtype: int
135
  @return: the desired exit code
136

137
  """
138
  client = GetClient()
139

    
140
  age = args[0]
141

    
142
  if age == "all":
143
    age = -1
144
  else:
145
    age = ParseTimespec(age)
146

    
147
  (archived_count, jobs_left) = client.AutoArchiveJobs(age)
148
  ToStdout("Archived %s jobs, %s unchecked left", archived_count, jobs_left)
149

    
150
  return 0
151

    
152

    
153
def CancelJobs(opts, args):
154
  """Cancel not-yet-started jobs.
155

156
  @param opts: the command line options selected by the user
157
  @type args: list
158
  @param args: should contain the job IDs to be cancelled
159
  @rtype: int
160
  @return: the desired exit code
161

162
  """
163
  client = GetClient()
164
  result = constants.EXIT_SUCCESS
165

    
166
  for job_id in args:
167
    (success, msg) = client.CancelJob(job_id)
168

    
169
    if not success:
170
      result = constants.EXIT_FAILURE
171

    
172
    ToStdout(msg)
173

    
174
  return result
175

    
176

    
177
def ShowJobs(opts, args):
178
  """Show detailed information about jobs.
179

180
  @param opts: the command line options selected by the user
181
  @type args: list
182
  @param args: should contain the job IDs to be queried
183
  @rtype: int
184
  @return: the desired exit code
185

186
  """
187
  def format_msg(level, text):
188
    """Display the text indented."""
189
    ToStdout("%s%s", "  " * level, text)
190

    
191
  def result_helper(value):
192
    """Format a result field in a nice way."""
193
    if isinstance(value, (tuple, list)):
194
      return "[%s]" % utils.CommaJoin(value)
195
    else:
196
      return str(value)
197

    
198
  selected_fields = [
199
    "id", "status", "ops", "opresult", "opstatus", "oplog",
200
    "opstart", "opexec", "opend", "received_ts", "start_ts", "end_ts",
201
    ]
202

    
203
  result = GetClient().Query(constants.QR_JOB, selected_fields,
204
                             qlang.MakeSimpleFilter("id", args)).data
205

    
206
  first = True
207

    
208
  for entry in result:
209
    if not first:
210
      format_msg(0, "")
211
    else:
212
      first = False
213

    
214
    ((_, job_id), (rs_status, status), (_, ops), (_, opresult), (_, opstatus),
215
     (_, oplog), (_, opstart), (_, opexec), (_, opend), (_, recv_ts),
216
     (_, start_ts), (_, end_ts)) = entry
217

    
218
    # Detect non-normal results
219
    if rs_status != constants.RS_NORMAL:
220
      format_msg(0, "Job ID %s not found" % job_id)
221
      continue
222

    
223
    format_msg(0, "Job ID: %s" % job_id)
224
    if status in _USER_JOB_STATUS:
225
      status = _USER_JOB_STATUS[status]
226
    else:
227
      raise errors.ProgrammerError("Unknown job status code '%s'" % status)
228

    
229
    format_msg(1, "Status: %s" % status)
230

    
231
    if recv_ts is not None:
232
      format_msg(1, "Received:         %s" % FormatTimestamp(recv_ts))
233
    else:
234
      format_msg(1, "Missing received timestamp (%s)" % str(recv_ts))
235

    
236
    if start_ts is not None:
237
      if recv_ts is not None:
238
        d1 = start_ts[0] - recv_ts[0] + (start_ts[1] - recv_ts[1]) / 1000000.0
239
        delta = " (delta %.6fs)" % d1
240
      else:
241
        delta = ""
242
      format_msg(1, "Processing start: %s%s" %
243
                 (FormatTimestamp(start_ts), delta))
244
    else:
245
      format_msg(1, "Processing start: unknown (%s)" % str(start_ts))
246

    
247
    if end_ts is not None:
248
      if start_ts is not None:
249
        d2 = end_ts[0] - start_ts[0] + (end_ts[1] - start_ts[1]) / 1000000.0
250
        delta = " (delta %.6fs)" % d2
251
      else:
252
        delta = ""
253
      format_msg(1, "Processing end:   %s%s" %
254
                 (FormatTimestamp(end_ts), delta))
255
    else:
256
      format_msg(1, "Processing end:   unknown (%s)" % str(end_ts))
257

    
258
    if end_ts is not None and recv_ts is not None:
259
      d3 = end_ts[0] - recv_ts[0] + (end_ts[1] - recv_ts[1]) / 1000000.0
260
      format_msg(1, "Total processing time: %.6f seconds" % d3)
261
    else:
262
      format_msg(1, "Total processing time: N/A")
263
    format_msg(1, "Opcodes:")
264
    for (opcode, result, status, log, s_ts, x_ts, e_ts) in \
265
            zip(ops, opresult, opstatus, oplog, opstart, opexec, opend):
266
      format_msg(2, "%s" % opcode["OP_ID"])
267
      format_msg(3, "Status: %s" % status)
268
      if isinstance(s_ts, (tuple, list)):
269
        format_msg(3, "Processing start: %s" % FormatTimestamp(s_ts))
270
      else:
271
        format_msg(3, "No processing start time")
272
      if isinstance(x_ts, (tuple, list)):
273
        format_msg(3, "Execution start:  %s" % FormatTimestamp(x_ts))
274
      else:
275
        format_msg(3, "No execution start time")
276
      if isinstance(e_ts, (tuple, list)):
277
        format_msg(3, "Processing end:   %s" % FormatTimestamp(e_ts))
278
      else:
279
        format_msg(3, "No processing end time")
280
      format_msg(3, "Input fields:")
281
      for key in utils.NiceSort(opcode.keys()):
282
        if key == "OP_ID":
283
          continue
284
        val = opcode[key]
285
        if isinstance(val, (tuple, list)):
286
          val = ",".join([str(item) for item in val])
287
        format_msg(4, "%s: %s" % (key, val))
288
      if result is None:
289
        format_msg(3, "No output data")
290
      elif isinstance(result, (tuple, list)):
291
        if not result:
292
          format_msg(3, "Result: empty sequence")
293
        else:
294
          format_msg(3, "Result:")
295
          for elem in result:
296
            format_msg(4, result_helper(elem))
297
      elif isinstance(result, dict):
298
        if not result:
299
          format_msg(3, "Result: empty dictionary")
300
        else:
301
          format_msg(3, "Result:")
302
          for key, val in result.iteritems():
303
            format_msg(4, "%s: %s" % (key, result_helper(val)))
304
      else:
305
        format_msg(3, "Result: %s" % result)
306
      format_msg(3, "Execution log:")
307
      for serial, log_ts, log_type, log_msg in log:
308
        time_txt = FormatTimestamp(log_ts)
309
        encoded = FormatLogMessage(log_type, log_msg)
310
        format_msg(4, "%s:%s:%s %s" % (serial, time_txt, log_type, encoded))
311
  return 0
312

    
313

    
314
def WatchJob(opts, args):
315
  """Follow a job and print its output as it arrives.
316

317
  @param opts: the command line options selected by the user
318
  @type args: list
319
  @param args: Contains the job ID
320
  @rtype: int
321
  @return: the desired exit code
322

323
  """
324
  job_id = args[0]
325

    
326
  msg = ("Output from job %s follows" % job_id)
327
  ToStdout(msg)
328
  ToStdout("-" * len(msg))
329

    
330
  retcode = 0
331
  try:
332
    cli.PollJob(job_id)
333
  except errors.GenericError, err:
334
    (retcode, job_result) = cli.FormatError(err)
335
    ToStderr("Job %s failed: %s", job_id, job_result)
336

    
337
  return retcode
338

    
339

    
340
commands = {
341
  "list": (
342
    ListJobs, [ArgJobId()],
343
    [NOHDR_OPT, SEP_OPT, FIELDS_OPT, VERBOSE_OPT, FORCE_FILTER_OPT],
344
    "[job_id ...]",
345
    "Lists the jobs and their status. The available fields can be shown"
346
    " using the \"list-fields\" command (see the man page for details)."
347
    " The default field list is (in order): %s." %
348
    utils.CommaJoin(_LIST_DEF_FIELDS)),
349
  "list-fields": (
350
    ListJobFields, [ArgUnknown()],
351
    [NOHDR_OPT, SEP_OPT],
352
    "[fields...]",
353
    "Lists all available fields for jobs"),
354
  "archive": (
355
    ArchiveJobs, [ArgJobId(min=1)], [],
356
    "<job-id> [<job-id> ...]", "Archive specified jobs"),
357
  "autoarchive": (
358
    AutoArchiveJobs,
359
    [ArgSuggest(min=1, max=1, choices=["1d", "1w", "4w", "all"])],
360
    [],
361
    "<age>", "Auto archive jobs older than the given age"),
362
  "cancel": (
363
    CancelJobs, [ArgJobId(min=1)], [],
364
    "<job-id> [<job-id> ...]", "Cancel specified jobs"),
365
  "info": (
366
    ShowJobs, [ArgJobId(min=1)], [],
367
    "<job-id> [<job-id> ...]",
368
    "Show detailed information about the specified jobs"),
369
  "watch": (
370
    WatchJob, [ArgJobId(min=1, max=1)], [],
371
    "<job-id>", "Follows a job and prints its output as it arrives"),
372
  }
373

    
374

    
375
#: dictionary with aliases for commands
376
aliases = {
377
  "show": "info",
378
  }
379

    
380

    
381
def Main():
382
  return GenericMain(commands, aliases=aliases)