Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-job @ c04bc777

History | View | Annotate | Download (11.2 kB)

1
#!/usr/bin/python
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-msg=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
import sys
30

    
31
from ganeti.cli import *
32
from ganeti import constants
33
from ganeti import errors
34
from ganeti import utils
35
from ganeti import cli
36

    
37

    
38
#: default list of fields for L{ListJobs}
39
_LIST_DEF_FIELDS = ["id", "status", "summary"]
40

    
41
#: map converting the job status contants to user-visible
42
#: names
43
_USER_JOB_STATUS = {
44
  constants.JOB_STATUS_QUEUED: "queued",
45
  constants.JOB_STATUS_WAITLOCK: "waiting",
46
  constants.JOB_STATUS_CANCELING: "canceling",
47
  constants.JOB_STATUS_RUNNING: "running",
48
  constants.JOB_STATUS_CANCELED: "canceled",
49
  constants.JOB_STATUS_SUCCESS: "success",
50
  constants.JOB_STATUS_ERROR: "error",
51
  }
52

    
53

    
54
def ListJobs(opts, args):
55
  """List the jobs
56

    
57
  @param opts: the command line options selected by the user
58
  @type args: list
59
  @param args: should be an empty list
60
  @rtype: int
61
  @return: the desired exit code
62

    
63
  """
64
  if opts.output is None:
65
    selected_fields = _LIST_DEF_FIELDS
66
  elif opts.output.startswith("+"):
67
    selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",")
68
  else:
69
    selected_fields = opts.output.split(",")
70

    
71
  output = GetClient().QueryJobs(args, selected_fields)
72
  if not opts.no_headers:
73
    # TODO: Implement more fields
74
    headers = {
75
      "id": "ID",
76
      "status": "Status",
77
      "ops": "OpCodes",
78
      "opresult": "OpCode_result",
79
      "opstatus": "OpCode_status",
80
      "oplog": "OpCode_log",
81
      "summary": "Summary",
82
      "opstart": "OpCode_start",
83
      "opexec": "OpCode_exec",
84
      "opend": "OpCode_end",
85
      "start_ts": "Start",
86
      "end_ts": "End",
87
      "received_ts": "Received",
88
      "lock_status": "LockStatus",
89
      }
90
  else:
91
    headers = None
92

    
93
  # change raw values to nicer strings
94
  for row_id, row in enumerate(output):
95
    if row is None:
96
      ToStderr("No such job: %s" % args[row_id])
97
      continue
98

    
99
    for idx, field in enumerate(selected_fields):
100
      val = row[idx]
101
      if field == "status":
102
        if val in _USER_JOB_STATUS:
103
          val = _USER_JOB_STATUS[val]
104
        else:
105
          raise errors.ProgrammerError("Unknown job status code '%s'" % val)
106
      elif field == "summary":
107
        val = ",".join(val)
108
      elif field in ("start_ts", "end_ts", "received_ts"):
109
        val = FormatTimestamp(val)
110
      elif field in ("opstart", "opexec", "opend"):
111
        val = [FormatTimestamp(entry) for entry in val]
112
      elif field == "lock_status" and not val:
113
        val = "-"
114

    
115
      row[idx] = str(val)
116

    
117
  data = GenerateTable(separator=opts.separator, headers=headers,
118
                       fields=selected_fields, data=output)
119
  for line in data:
120
    ToStdout(line)
121

    
122
  return 0
123

    
124

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

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

    
134
  """
135
  client = GetClient()
136

    
137
  for job_id in args:
138
    client.ArchiveJob(job_id)
139

    
140
  return 0
141

    
142

    
143
def AutoArchiveJobs(opts, args):
144
  """Archive jobs based on age.
145

    
146
  This will archive jobs based on their age, or all jobs if a 'all' is
147
  passed.
148

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

    
157
  """
158
  client = GetClient()
159

    
160
  age = args[0]
161

    
162
  if age == 'all':
163
    age = -1
164
  else:
165
    age = ParseTimespec(age)
166

    
167
  (archived_count, jobs_left) = client.AutoArchiveJobs(age)
168
  ToStdout("Archived %s jobs, %s unchecked left", archived_count, jobs_left)
169

    
170
  return 0
171

    
172

    
173
def CancelJobs(opts, args):
174
  """Cancel not-yet-started jobs.
175

    
176
  @param opts: the command line options selected by the user
177
  @type args: list
178
  @param args: should contain the job IDs to be cancelled
179
  @rtype: int
180
  @return: the desired exit code
181

    
182
  """
183
  client = GetClient()
184

    
185
  for job_id in args:
186
    (_, msg) = client.CancelJob(job_id)
187
    ToStdout(msg)
188

    
189
  # TODO: Different exit value if not all jobs were canceled?
190
  return 0
191

    
192

    
193
def ShowJobs(opts, args):
194
  """Show detailed information about jobs.
195

    
196
  @param opts: the command line options selected by the user
197
  @type args: list
198
  @param args: should contain the job IDs to be queried
199
  @rtype: int
200
  @return: the desired exit code
201

    
202
  """
203
  def format_msg(level, text):
204
    """Display the text indented."""
205
    ToStdout("%s%s", "  " * level, text)
206

    
207
  def result_helper(value):
208
    """Format a result field in a nice way."""
209
    if isinstance(value, (tuple, list)):
210
      return "[%s]" % utils.CommaJoin(value)
211
    else:
212
      return str(value)
213

    
214
  selected_fields = [
215
    "id", "status", "ops", "opresult", "opstatus", "oplog",
216
    "opstart", "opexec", "opend", "received_ts", "start_ts", "end_ts",
217
    ]
218

    
219
  result = GetClient().QueryJobs(args, selected_fields)
220

    
221
  first = True
222

    
223
  for idx, entry in enumerate(result):
224
    if not first:
225
      format_msg(0, "")
226
    else:
227
      first = False
228

    
229
    if entry is None:
230
      if idx <= len(args):
231
        format_msg(0, "Job ID %s not found" % args[idx])
232
      else:
233
        # this should not happen, when we don't pass args it will be a
234
        # valid job returned
235
        format_msg(0, "Job ID requested as argument %s not found" % (idx + 1))
236
      continue
237

    
238
    (job_id, status, ops, opresult, opstatus, oplog,
239
     opstart, opexec, opend, recv_ts, start_ts, end_ts) = entry
240
    format_msg(0, "Job ID: %s" % job_id)
241
    if status in _USER_JOB_STATUS:
242
      status = _USER_JOB_STATUS[status]
243
    else:
244
      raise errors.ProgrammerError("Unknown job status code '%s'" % status)
245

    
246
    format_msg(1, "Status: %s" % status)
247

    
248
    if recv_ts is not None:
249
      format_msg(1, "Received:         %s" % FormatTimestamp(recv_ts))
250
    else:
251
      format_msg(1, "Missing received timestamp (%s)" % str(recv_ts))
252

    
253
    if start_ts is not None:
254
      if recv_ts is not None:
255
        d1 = start_ts[0] - recv_ts[0] + (start_ts[1] - recv_ts[1]) / 1000000.0
256
        delta = " (delta %.6fs)" % d1
257
      else:
258
        delta = ""
259
      format_msg(1, "Processing start: %s%s" %
260
                 (FormatTimestamp(start_ts), delta))
261
    else:
262
      format_msg(1, "Processing start: unknown (%s)" % str(start_ts))
263

    
264
    if end_ts is not None:
265
      if start_ts is not None:
266
        d2 = end_ts[0] - start_ts[0] + (end_ts[1] - start_ts[1]) / 1000000.0
267
        delta = " (delta %.6fs)" % d2
268
      else:
269
        delta = ""
270
      format_msg(1, "Processing end:   %s%s" %
271
                 (FormatTimestamp(end_ts), delta))
272
    else:
273
      format_msg(1, "Processing end:   unknown (%s)" % str(end_ts))
274

    
275
    if end_ts is not None and recv_ts is not None:
276
      d3 = end_ts[0] - recv_ts[0] + (end_ts[1] - recv_ts[1]) / 1000000.0
277
      format_msg(1, "Total processing time: %.6f seconds" % d3)
278
    else:
279
      format_msg(1, "Total processing time: N/A")
280
    format_msg(1, "Opcodes:")
281
    for (opcode, result, status, log, s_ts, x_ts, e_ts) in \
282
            zip(ops, opresult, opstatus, oplog, opstart, opexec, opend):
283
      format_msg(2, "%s" % opcode["OP_ID"])
284
      format_msg(3, "Status: %s" % status)
285
      if isinstance(s_ts, (tuple, list)):
286
        format_msg(3, "Processing start: %s" % FormatTimestamp(s_ts))
287
      else:
288
        format_msg(3, "No processing start time")
289
      if isinstance(x_ts, (tuple, list)):
290
        format_msg(3, "Execution start:  %s" % FormatTimestamp(x_ts))
291
      else:
292
        format_msg(3, "No execution start time")
293
      if isinstance(e_ts, (tuple, list)):
294
        format_msg(3, "Processing end:   %s" % FormatTimestamp(e_ts))
295
      else:
296
        format_msg(3, "No processing end time")
297
      format_msg(3, "Input fields:")
298
      for key, val in opcode.iteritems():
299
        if key == "OP_ID":
300
          continue
301
        if isinstance(val, (tuple, list)):
302
          val = ",".join([str(item) for item in val])
303
        format_msg(4, "%s: %s" % (key, val))
304
      if result is None:
305
        format_msg(3, "No output data")
306
      elif isinstance(result, (tuple, list)):
307
        if not result:
308
          format_msg(3, "Result: empty sequence")
309
        else:
310
          format_msg(3, "Result:")
311
          for elem in result:
312
            format_msg(4, result_helper(elem))
313
      elif isinstance(result, dict):
314
        if not result:
315
          format_msg(3, "Result: empty dictionary")
316
        else:
317
          for key, val in result.iteritems():
318
            format_msg(4, "%s: %s" % (key, result_helper(val)))
319
      else:
320
        format_msg(3, "Result: %s" % result)
321
      format_msg(3, "Execution log:")
322
      for serial, log_ts, log_type, log_msg in log:
323
        time_txt = FormatTimestamp(log_ts)
324
        encoded = utils.SafeEncode(log_msg)
325
        format_msg(4, "%s:%s:%s %s" % (serial, time_txt, log_type, encoded))
326
  return 0
327

    
328

    
329
def WatchJob(opts, args):
330
  """Follow a job and print its output as it arrives.
331

    
332
  @param opts: the command line options selected by the user
333
  @type args: list
334
  @param args: Contains the job ID
335
  @rtype: int
336
  @return: the desired exit code
337

    
338
  """
339
  job_id = args[0]
340

    
341
  msg = ("Output from job %s follows" % job_id)
342
  ToStdout(msg)
343
  ToStdout("-" * len(msg))
344

    
345
  retcode = 0
346
  try:
347
    cli.PollJob(job_id)
348
  except errors.GenericError, err:
349
    (retcode, job_result) = cli.FormatError(err)
350
    ToStderr("Job %s failed: %s", job_id, job_result)
351

    
352
  return retcode
353

    
354

    
355
commands = {
356
  'list': (
357
    ListJobs, [ArgJobId()],
358
    [NOHDR_OPT, SEP_OPT, FIELDS_OPT],
359
    "[job_id ...]",
360
    "List the jobs and their status. The available fields are"
361
    " (see the man page for details): id, status, op_list,"
362
    " op_status, op_result."
363
    " The default field"
364
    " list is (in order): %s." % utils.CommaJoin(_LIST_DEF_FIELDS)),
365
  'archive': (
366
    ArchiveJobs, [ArgJobId(min=1)], [],
367
    "<job-id> [<job-id> ...]", "Archive specified jobs"),
368
  'autoarchive': (
369
    AutoArchiveJobs,
370
    [ArgSuggest(min=1, max=1, choices=["1d", "1w", "4w"])],
371
    [],
372
    "<age>", "Auto archive jobs older than the given age"),
373
  'cancel': (
374
    CancelJobs, [ArgJobId(min=1)], [],
375
    "<job-id> [<job-id> ...]", "Cancel specified jobs"),
376
  'info': (
377
    ShowJobs, [ArgJobId(min=1)], [],
378
    "<job-id> [<job-id> ...]",
379
    "Show detailed information about the specified jobs"),
380
  'watch': (
381
    WatchJob, [ArgJobId(min=1, max=1)], [],
382
    "<job-id>", "Follows a job and prints its output as it arrives"),
383
  }
384

    
385

    
386
if __name__ == '__main__':
387
  sys.exit(GenericMain(commands))