Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-job @ 064c21f8

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

    
22
# pylint: disable-msg=W0401,W0614
23
# W0401: Wildcard import ganeti.cli
24
# W0614: Unused import %s from wildcard import (since we need cli)
25

    
26
import sys
27

    
28
from ganeti.cli import *
29
from ganeti import constants
30
from ganeti import errors
31
from ganeti import utils
32
from ganeti import cli
33

    
34

    
35
#: default list of fields for L{ListJobs}
36
_LIST_DEF_FIELDS = ["id", "status", "summary"]
37

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

    
50

    
51
def ListJobs(opts, args):
52
  """List the jobs
53

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

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

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

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

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

    
111
      row[idx] = str(val)
112

    
113
  data = GenerateTable(separator=opts.separator, headers=headers,
114
                       fields=selected_fields, data=output)
115
  for line in data:
116
    ToStdout(line)
117

    
118
  return 0
119

    
120

    
121
def ArchiveJobs(opts, args):
122
  """Archive jobs.
123

    
124
  @param opts: the command line options selected by the user
125
  @type args: list
126
  @param args: should contain the job IDs to be archived
127
  @rtype: int
128
  @return: the desired exit code
129

    
130
  """
131
  client = GetClient()
132

    
133
  for job_id in args:
134
    client.ArchiveJob(job_id)
135

    
136
  return 0
137

    
138

    
139
def AutoArchiveJobs(opts, args):
140
  """Archive jobs based on age.
141

    
142
  This will archive jobs based on their age, or all jobs if a 'all' is
143
  passed.
144

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

    
153
  """
154
  client = GetClient()
155

    
156
  age = args[0]
157

    
158
  if age == 'all':
159
    age = -1
160
  else:
161
    age = ParseTimespec(age)
162

    
163
  (archived_count, jobs_left) = client.AutoArchiveJobs(age)
164
  ToStdout("Archived %s jobs, %s unchecked left", archived_count, jobs_left)
165

    
166
  return 0
167

    
168

    
169
def CancelJobs(opts, args):
170
  """Cancel not-yet-started jobs.
171

    
172
  @param opts: the command line options selected by the user
173
  @type args: list
174
  @param args: should contain the job IDs to be cancelled
175
  @rtype: int
176
  @return: the desired exit code
177

    
178
  """
179
  client = GetClient()
180

    
181
  for job_id in args:
182
    (success, msg) = client.CancelJob(job_id)
183
    ToStdout(msg)
184

    
185
  # TODO: Different exit value if not all jobs were canceled?
186
  return 0
187

    
188

    
189
def ShowJobs(opts, args):
190
  """Show detailed information about jobs.
191

    
192
  @param opts: the command line options selected by the user
193
  @type args: list
194
  @param args: should contain the job IDs to be queried
195
  @rtype: int
196
  @return: the desired exit code
197

    
198
  """
199
  def format(level, text):
200
    """Display the text indented."""
201
    ToStdout("%s%s", "  " * level, text)
202

    
203
  def result_helper(value):
204
    """Format a result field in a nice way."""
205
    if isinstance(value, (tuple, list)):
206
      return "[%s]" % (", ".join(str(elem) for elem in value))
207
    else:
208
      return str(value)
209

    
210
  selected_fields = [
211
    "id", "status", "ops", "opresult", "opstatus", "oplog",
212
    "opstart", "opend", "received_ts", "start_ts", "end_ts",
213
    ]
214

    
215
  result = GetClient().QueryJobs(args, selected_fields)
216

    
217
  first = True
218

    
219
  for idx, entry in enumerate(result):
220
    if not first:
221
      format(0, "")
222
    else:
223
      first = False
224

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

    
234
    (job_id, status, ops, opresult, opstatus, oplog,
235
     opstart, opend, recv_ts, start_ts, end_ts) = entry
236
    format(0, "Job ID: %s" % job_id)
237
    if status in _USER_JOB_STATUS:
238
      status = _USER_JOB_STATUS[status]
239
    else:
240
      raise errors.ProgrammerError("Unknown job status code '%s'" % status)
241

    
242
    format(1, "Status: %s" % status)
243

    
244
    if recv_ts is not None:
245
      format(1, "Received:         %s" % FormatTimestamp(recv_ts))
246
    else:
247
      format(1, "Missing received timestamp (%s)" % str(recv_ts))
248

    
249
    if start_ts is not None:
250
      if recv_ts is not None:
251
        d1 = start_ts[0] - recv_ts[0] + (start_ts[1] - recv_ts[1]) / 1000000.0
252
        delta = " (delta %.6fs)" % d1
253
      else:
254
        delta = ""
255
      format(1, "Processing start: %s%s" % (FormatTimestamp(start_ts), delta))
256
    else:
257
      format(1, "Processing start: unknown (%s)" % str(start_ts))
258

    
259
    if end_ts is not None:
260
      if start_ts is not None:
261
        d2 = end_ts[0] - start_ts[0] + (end_ts[1] - start_ts[1]) / 1000000.0
262
        delta = " (delta %.6fs)" % d2
263
      else:
264
        delta = ""
265
      format(1, "Processing end:   %s%s" % (FormatTimestamp(end_ts), delta))
266
    else:
267
      format(1, "Processing end:   unknown (%s)" % str(end_ts))
268

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

    
318

    
319
def WatchJob(opts, args):
320
  """Follow a job and print its output as it arrives.
321

    
322
  @param opts: the command line options selected by the user
323
  @type args: list
324
  @param args: Contains the job ID
325
  @rtype: int
326
  @return: the desired exit code
327

    
328
  """
329
  job_id = args[0]
330

    
331
  msg = ("Output from job %s follows" % job_id)
332
  ToStdout(msg)
333
  ToStdout("-" * len(msg))
334

    
335
  retcode = 0
336
  try:
337
    cli.PollJob(job_id)
338
  except errors.GenericError, err:
339
    (retcode, job_result) = cli.FormatError(err)
340
    ToStderr("Job %s failed: %s", job_id, job_result)
341

    
342
  return retcode
343

    
344

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

    
375

    
376
if __name__ == '__main__':
377
  sys.exit(GenericMain(commands))