4 # Copyright (C) 2006, 2007 Google Inc.
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.
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.
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
26 from optparse import make_option
27 from cStringIO import StringIO
29 from ganeti.cli import *
30 from ganeti import opcodes
31 from ganeti import constants
32 from ganeti import utils
33 from ganeti import errors
36 _LIST_DEF_FIELDS = ["id", "status", "summary"]
39 constants.JOB_STATUS_QUEUED: "queued",
40 constants.JOB_STATUS_WAITLOCK: "waiting",
41 constants.JOB_STATUS_RUNNING: "running",
42 constants.JOB_STATUS_CANCELED: "canceled",
43 constants.JOB_STATUS_SUCCESS: "success",
44 constants.JOB_STATUS_ERROR: "error",
48 def ListJobs(opts, args):
52 if opts.output is None:
53 selected_fields = _LIST_DEF_FIELDS
54 elif opts.output.startswith("+"):
55 selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",")
57 selected_fields = opts.output.split(",")
59 output = GetClient().QueryJobs(None, selected_fields)
60 if not opts.no_headers:
61 # TODO: Implement more fields
66 "opresult": "OpCode_result",
67 "opstatus": "OpCode_status",
68 "oplog": "OpCode_log",
70 "opstart": "OpCode_start",
71 "opend": "OpCode_end",
74 "received_ts": "Received",
79 # we don't have yet unitfields here
83 # change raw values to nicer strings
85 for idx, field in enumerate(selected_fields):
88 if val in _USER_JOB_STATUS:
89 val = _USER_JOB_STATUS[val]
91 raise errors.ProgrammerError("Unknown job status code '%s'" % val)
92 elif field == "summary":
94 elif field in ("start_ts", "end_ts", "received_ts"):
95 val = FormatTimestamp(val)
96 elif field in ("opstart", "opend"):
97 val = [FormatTimestamp(entry) for entry in val]
101 data = GenerateTable(separator=opts.separator, headers=headers,
102 fields=selected_fields, unitfields=unitfields,
103 numfields=numfields, data=output)
110 def ArchiveJobs(opts, args):
114 client.ArchiveJob(job_id)
119 def AutoArchiveJobs(opts, args):
127 age = ParseTimespec(age)
129 client.AutoArchiveJobs(age)
133 def CancelJobs(opts, args):
137 client.CancelJob(job_id)
142 def ShowJobs(opts, args):
146 def format(level, text):
147 """Display the text indented."""
148 ToStdout("%s%s", " " * level, text)
150 def result_helper(value):
151 """Format a result field in a nice way."""
152 if isinstance(value, (tuple, list)):
153 return "[%s]" % (", ".join(str(elem) for elem in value))
158 "id", "status", "ops", "opresult", "opstatus", "oplog",
159 "opstart", "opend", "received_ts", "start_ts", "end_ts",
162 result = GetClient().QueryJobs(args, selected_fields)
166 for idx, entry in enumerate(result):
174 format(0, "Job ID %s not found" % args[idx])
176 # this should not happen, when we don't pass args it will be a
178 format(0, "Job ID requested as argument %s not found" % (idx + 1))
181 (job_id, status, ops, opresult, opstatus, oplog,
182 opstart, opend, recv_ts, start_ts, end_ts) = entry
183 format(0, "Job ID: %s" % job_id)
184 if status in _USER_JOB_STATUS:
185 status = _USER_JOB_STATUS[status]
187 raise errors.ProgrammerError("Unknown job status code '%s'" % val)
189 format(1, "Status: %s" % status)
191 if recv_ts is not None:
192 format(1, "Received: %s" % FormatTimestamp(recv_ts))
194 format(1, "Missing received timestamp (%s)" % str(recv_ts))
196 if start_ts is not None:
197 if recv_ts is not None:
198 d1 = start_ts[0] - recv_ts[0] + (start_ts[1] - recv_ts[1]) / 1000000.0
199 delta = " (delta %.6fs)" % d1
202 format(1, "Processing start: %s%s" % (FormatTimestamp(start_ts), delta))
204 format(1, "Processing start: unknown (%s)" % str(start_ts))
206 if end_ts is not None:
207 if start_ts is not None:
208 d2 = end_ts[0] - start_ts[0] + (end_ts[1] - start_ts[1]) / 1000000.0
209 delta = " (delta %.6fs)" % d2
212 format(1, "Processing end: %s%s" % (FormatTimestamp(end_ts), delta))
214 format(1, "Processing end: unknown (%s)" % str(end_ts))
216 if end_ts is not None and recv_ts is not None:
217 d3 = end_ts[0] - recv_ts[0] + (end_ts[1] - recv_ts[1]) / 1000000.0
218 format(1, "Total processing time: %.6f seconds" % d3)
220 format(1, "Total processing time: N/A")
221 format(1, "Opcodes:")
222 for (opcode, result, status, log, s_ts, e_ts) in \
223 zip(ops, opresult, opstatus, oplog, opstart, opend):
224 format(2, "%s" % opcode["OP_ID"])
225 format(3, "Status: %s" % status)
226 if isinstance(s_ts, (tuple, list)):
227 format(3, "Processing start: %s" % FormatTimestamp(s_ts))
229 format(3, "No processing start time")
230 if isinstance(e_ts, (tuple, list)):
231 format(3, "Processing end: %s" % FormatTimestamp(e_ts))
233 format(3, "No processing end time")
234 format(3, "Input fields:")
235 for key, val in opcode.iteritems():
238 if isinstance(val, (tuple, list)):
240 format(4, "%s: %s" % (key, val))
242 format(3, "No output data")
243 elif isinstance(result, (tuple, list)):
245 format(3, "Result: empty sequence")
249 format(4, result_helper(elem))
250 elif isinstance(result, dict):
252 format(3, "Result: empty dictionary")
254 for key, val in result.iteritems():
255 format(4, "%s: %s" % (key, result_helper(val)))
257 format(3, "Result: %s" % result)
258 format(3, "Execution log:")
259 for serial, log_ts, log_type, log_msg in log:
260 time_txt = FormatTimestamp(log_ts)
261 encoded = str(log_msg).encode('string_escape')
262 format(4, "%s:%s:%s %s" % (serial, time_txt, log_type, encoded))
267 'list': (ListJobs, ARGS_NONE,
268 [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
269 "", "List the jobs and their status. The available fields are"
270 " (see the man page for details): id, status, op_list,"
271 " op_status, op_result."
273 " list is (in order): %s." % ", ".join(_LIST_DEF_FIELDS)),
274 'archive': (ArchiveJobs, ARGS_ANY,
276 "<job-id> [<job-id> ...]",
277 "Archive specified jobs"),
278 'autoarchive': (AutoArchiveJobs, ARGS_ONE,
281 "Auto archive jobs older than the given age"),
282 'cancel': (CancelJobs, ARGS_ANY,
284 "<job-id> [<job-id> ...]",
285 "Cancel specified jobs"),
286 'info': (ShowJobs, ARGS_ANY, [DEBUG_OPT],
287 "<job-id> [<job-id> ...]",
288 "Show detailed information about the specified jobs"),
292 if __name__ == '__main__':
293 sys.exit(GenericMain(commands))