Revision a09b9e3d

b/.gitignore
69 69
# scripts
70 70
/scripts/gnt-cluster
71 71
/scripts/gnt-debug
72
/scripts/gnt-job
72 73
/scripts/gnt-node
73 74
/scripts/gnt-os
b/Makefile.am
141 141
	lib/client/__init__.py \
142 142
	lib/client/gnt_cluster.py \
143 143
	lib/client/gnt_debug.py \
144
	lib/client/gnt_job.py \
144 145
	lib/client/gnt_node.py \
145 146
	lib/client/gnt_os.py
146 147

  
......
261 262
PYTHON_BOOTSTRAP = \
262 263
	scripts/gnt-cluster \
263 264
	scripts/gnt-debug \
265
	scripts/gnt-job \
264 266
	scripts/gnt-node \
265 267
	scripts/gnt-os
266 268

  
......
271 273
	daemons/ganeti-confd \
272 274
	daemons/ganeti-rapi \
273 275
	scripts/gnt-backup \
274
	scripts/gnt-instance \
275
	scripts/gnt-job
276
	scripts/gnt-instance
276 277

  
277 278
nodist_sbin_SCRIPTS = \
278 279
	$(PYTHON_BOOTSTRAP) \
b/lib/client/gnt_job.py
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-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
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

  
35

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

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

  
51

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

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

  
61
  """
62
  selected_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
63

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

  
87
  numfields = ["priority"]
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", "opexec", "opend"):
107
        val = [FormatTimestamp(entry) for entry in val]
108

  
109
      row[idx] = str(val)
110

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

  
117
  return 0
118

  
119

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

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

  
129
  """
130
  client = GetClient()
131

  
132
  rcode = 0
133
  for job_id in args:
134
    if not client.ArchiveJob(job_id):
135
      ToStderr("Failed to archive job with ID '%s'", job_id)
136
      rcode = 1
137

  
138
  return rcode
139

  
140

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

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

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

  
155
  """
156
  client = GetClient()
157

  
158
  age = args[0]
159

  
160
  if age == 'all':
161
    age = -1
162
  else:
163
    age = ParseTimespec(age)
164

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

  
168
  return 0
169

  
170

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

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

  
180
  """
181
  client = GetClient()
182
  result = constants.EXIT_SUCCESS
183

  
184
  for job_id in args:
185
    (success, msg) = client.CancelJob(job_id)
186

  
187
    if not success:
188
      result = constants.EXIT_FAILURE
189

  
190
    ToStdout(msg)
191

  
192
  return result
193

  
194

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

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

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

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

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

  
221
  result = GetClient().QueryJobs(args, selected_fields)
222

  
223
  first = True
224

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

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

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

  
248
    format_msg(1, "Status: %s" % status)
249

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

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

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

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

  
331

  
332
def WatchJob(opts, args):
333
  """Follow a job and print its output as it arrives.
334

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

  
341
  """
342
  job_id = args[0]
343

  
344
  msg = ("Output from job %s follows" % job_id)
345
  ToStdout(msg)
346
  ToStdout("-" * len(msg))
347

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

  
355
  return retcode
356

  
357

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

  
388

  
389
def Main():
390
  return GenericMain(commands)
/dev/null
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
  selected_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
65

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

  
89
  numfields = ["priority"]
90

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

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

  
111
      row[idx] = str(val)
112

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

  
119
  return 0
120

  
121

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

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

  
131
  """
132
  client = GetClient()
133

  
134
  rcode = 0
135
  for job_id in args:
136
    if not client.ArchiveJob(job_id):
137
      ToStderr("Failed to archive job with ID '%s'", job_id)
138
      rcode = 1
139

  
140
  return rcode
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
  result = constants.EXIT_SUCCESS
185

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

  
189
    if not success:
190
      result = constants.EXIT_FAILURE
191

  
192
    ToStdout(msg)
193

  
194
  return result
195

  
196

  
197
def ShowJobs(opts, args):
198
  """Show detailed information about jobs.
199

  
200
  @param opts: the command line options selected by the user
201
  @type args: list
202
  @param args: should contain the job IDs to be queried
203
  @rtype: int
204
  @return: the desired exit code
205

  
206
  """
207
  def format_msg(level, text):
208
    """Display the text indented."""
209
    ToStdout("%s%s", "  " * level, text)
210

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

  
218
  selected_fields = [
219
    "id", "status", "ops", "opresult", "opstatus", "oplog",
220
    "opstart", "opexec", "opend", "received_ts", "start_ts", "end_ts",
221
    ]
222

  
223
  result = GetClient().QueryJobs(args, selected_fields)
224

  
225
  first = True
226

  
227
  for idx, entry in enumerate(result):
228
    if not first:
229
      format_msg(0, "")
230
    else:
231
      first = False
232

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

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

  
250
    format_msg(1, "Status: %s" % status)
251

  
252
    if recv_ts is not None:
253
      format_msg(1, "Received:         %s" % FormatTimestamp(recv_ts))
254
    else:
255
      format_msg(1, "Missing received timestamp (%s)" % str(recv_ts))
256

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

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

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

  
333

  
334
def WatchJob(opts, args):
335
  """Follow a job and print its output as it arrives.
336

  
337
  @param opts: the command line options selected by the user
338
  @type args: list
339
  @param args: Contains the job ID
340
  @rtype: int
341
  @return: the desired exit code
342

  
343
  """
344
  job_id = args[0]
345

  
346
  msg = ("Output from job %s follows" % job_id)
347
  ToStdout(msg)
348
  ToStdout("-" * len(msg))
349

  
350
  retcode = 0
351
  try:
352
    cli.PollJob(job_id)
353
  except errors.GenericError, err:
354
    (retcode, job_result) = cli.FormatError(err)
355
    ToStderr("Job %s failed: %s", job_id, job_result)
356

  
357
  return retcode
358

  
359

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

  
390

  
391
if __name__ == '__main__':
392
  sys.exit(GenericMain(commands))

Also available in: Unified diff