root / scripts / gnt-job @ 66348f29
History | View | Annotate | Download (8.4 kB)
1 | 7a1ecaed | Iustin Pop | #!/usr/bin/python |
---|---|---|---|
2 | 7a1ecaed | Iustin Pop | # |
3 | 7a1ecaed | Iustin Pop | |
4 | 7a1ecaed | Iustin Pop | # Copyright (C) 2006, 2007 Google Inc. |
5 | 7a1ecaed | Iustin Pop | # |
6 | 7a1ecaed | Iustin Pop | # This program is free software; you can redistribute it and/or modify |
7 | 7a1ecaed | Iustin Pop | # it under the terms of the GNU General Public License as published by |
8 | 7a1ecaed | Iustin Pop | # the Free Software Foundation; either version 2 of the License, or |
9 | 7a1ecaed | Iustin Pop | # (at your option) any later version. |
10 | 7a1ecaed | Iustin Pop | # |
11 | 7a1ecaed | Iustin Pop | # This program is distributed in the hope that it will be useful, but |
12 | 7a1ecaed | Iustin Pop | # WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | 7a1ecaed | Iustin Pop | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | 7a1ecaed | Iustin Pop | # General Public License for more details. |
15 | 7a1ecaed | Iustin Pop | # |
16 | 7a1ecaed | Iustin Pop | # You should have received a copy of the GNU General Public License |
17 | 7a1ecaed | Iustin Pop | # along with this program; if not, write to the Free Software |
18 | 7a1ecaed | Iustin Pop | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 | 7a1ecaed | Iustin Pop | # 02110-1301, USA. |
20 | 7a1ecaed | Iustin Pop | |
21 | 7a1ecaed | Iustin Pop | |
22 | 2f79bd34 | Iustin Pop | # pylint: disable-msg=W0401,W0614 |
23 | 2f79bd34 | Iustin Pop | # W0401: Wildcard import ganeti.cli |
24 | 2f79bd34 | Iustin Pop | # W0614: Unused import %s from wildcard import (since we need cli) |
25 | 2f79bd34 | Iustin Pop | |
26 | 7a1ecaed | Iustin Pop | import sys |
27 | 7a1ecaed | Iustin Pop | |
28 | 7a1ecaed | Iustin Pop | from ganeti.cli import * |
29 | 7a1ecaed | Iustin Pop | from ganeti import constants |
30 | 7a1ecaed | Iustin Pop | from ganeti import errors |
31 | 7a1ecaed | Iustin Pop | |
32 | 7a1ecaed | Iustin Pop | |
33 | 60dd1473 | Iustin Pop | _LIST_DEF_FIELDS = ["id", "status", "summary"] |
34 | 7a5d3bbd | Iustin Pop | |
35 | af30b2fd | Michael Hanselmann | _USER_JOB_STATUS = { |
36 | af30b2fd | Michael Hanselmann | constants.JOB_STATUS_QUEUED: "queued", |
37 | e92376d7 | Iustin Pop | constants.JOB_STATUS_WAITLOCK: "waiting", |
38 | af30b2fd | Michael Hanselmann | constants.JOB_STATUS_RUNNING: "running", |
39 | af30b2fd | Michael Hanselmann | constants.JOB_STATUS_CANCELED: "canceled", |
40 | af30b2fd | Michael Hanselmann | constants.JOB_STATUS_SUCCESS: "success", |
41 | af30b2fd | Michael Hanselmann | constants.JOB_STATUS_ERROR: "error", |
42 | af30b2fd | Michael Hanselmann | } |
43 | af30b2fd | Michael Hanselmann | |
44 | 0ad64cf8 | Michael Hanselmann | |
45 | 7a1ecaed | Iustin Pop | def ListJobs(opts, args): |
46 | 7a1ecaed | Iustin Pop | """List the jobs |
47 | 7a1ecaed | Iustin Pop | |
48 | 7a1ecaed | Iustin Pop | """ |
49 | 7a1ecaed | Iustin Pop | if opts.output is None: |
50 | 7a5d3bbd | Iustin Pop | selected_fields = _LIST_DEF_FIELDS |
51 | 7a5d3bbd | Iustin Pop | elif opts.output.startswith("+"): |
52 | 7a5d3bbd | Iustin Pop | selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",") |
53 | 7a1ecaed | Iustin Pop | else: |
54 | 7a1ecaed | Iustin Pop | selected_fields = opts.output.split(",") |
55 | 7a1ecaed | Iustin Pop | |
56 | af30b2fd | Michael Hanselmann | output = GetClient().QueryJobs(None, selected_fields) |
57 | 7a1ecaed | Iustin Pop | if not opts.no_headers: |
58 | af30b2fd | Michael Hanselmann | # TODO: Implement more fields |
59 | 7a1ecaed | Iustin Pop | headers = { |
60 | 7a1ecaed | Iustin Pop | "id": "ID", |
61 | 7a1ecaed | Iustin Pop | "status": "Status", |
62 | af30b2fd | Michael Hanselmann | "ops": "OpCodes", |
63 | af30b2fd | Michael Hanselmann | "opresult": "OpCode_result", |
64 | af30b2fd | Michael Hanselmann | "opstatus": "OpCode_status", |
65 | 5b23c34c | Iustin Pop | "oplog": "OpCode_log", |
66 | 60dd1473 | Iustin Pop | "summary": "Summary", |
67 | aad81f98 | Iustin Pop | "opstart": "OpCode_start", |
68 | aad81f98 | Iustin Pop | "opend": "OpCode_end", |
69 | aad81f98 | Iustin Pop | "start_ts": "Start", |
70 | aad81f98 | Iustin Pop | "end_ts": "End", |
71 | aad81f98 | Iustin Pop | "received_ts": "Received", |
72 | 7a1ecaed | Iustin Pop | } |
73 | 7a1ecaed | Iustin Pop | else: |
74 | 7a1ecaed | Iustin Pop | headers = None |
75 | 7a1ecaed | Iustin Pop | |
76 | 7a1ecaed | Iustin Pop | # we don't have yet unitfields here |
77 | 7a1ecaed | Iustin Pop | unitfields = None |
78 | 5ce81b28 | Michael Hanselmann | numfields = None |
79 | 7a1ecaed | Iustin Pop | |
80 | 7a1ecaed | Iustin Pop | # change raw values to nicer strings |
81 | 7a1ecaed | Iustin Pop | for row in output: |
82 | 7a1ecaed | Iustin Pop | for idx, field in enumerate(selected_fields): |
83 | 7a1ecaed | Iustin Pop | val = row[idx] |
84 | 7a1ecaed | Iustin Pop | if field == "status": |
85 | af30b2fd | Michael Hanselmann | if val in _USER_JOB_STATUS: |
86 | af30b2fd | Michael Hanselmann | val = _USER_JOB_STATUS[val] |
87 | 7a1ecaed | Iustin Pop | else: |
88 | 7a1ecaed | Iustin Pop | raise errors.ProgrammerError("Unknown job status code '%s'" % val) |
89 | 60dd1473 | Iustin Pop | elif field == "summary": |
90 | 60dd1473 | Iustin Pop | val = ",".join(val) |
91 | aad81f98 | Iustin Pop | elif field in ("start_ts", "end_ts", "received_ts"): |
92 | aad81f98 | Iustin Pop | val = FormatTimestamp(val) |
93 | aad81f98 | Iustin Pop | elif field in ("opstart", "opend"): |
94 | aad81f98 | Iustin Pop | val = [FormatTimestamp(entry) for entry in val] |
95 | 7a1ecaed | Iustin Pop | |
96 | 7a1ecaed | Iustin Pop | row[idx] = str(val) |
97 | 7a1ecaed | Iustin Pop | |
98 | 7a1ecaed | Iustin Pop | data = GenerateTable(separator=opts.separator, headers=headers, |
99 | 7a1ecaed | Iustin Pop | fields=selected_fields, unitfields=unitfields, |
100 | 7a1ecaed | Iustin Pop | numfields=numfields, data=output) |
101 | 7a1ecaed | Iustin Pop | for line in data: |
102 | 3a24c527 | Iustin Pop | ToStdout(line) |
103 | 7a1ecaed | Iustin Pop | |
104 | 7a1ecaed | Iustin Pop | return 0 |
105 | 7a1ecaed | Iustin Pop | |
106 | 7a1ecaed | Iustin Pop | |
107 | 0ad64cf8 | Michael Hanselmann | def ArchiveJobs(opts, args): |
108 | 0ad64cf8 | Michael Hanselmann | client = GetClient() |
109 | 0ad64cf8 | Michael Hanselmann | |
110 | 0ad64cf8 | Michael Hanselmann | for job_id in args: |
111 | 0ad64cf8 | Michael Hanselmann | client.ArchiveJob(job_id) |
112 | 0ad64cf8 | Michael Hanselmann | |
113 | 0ad64cf8 | Michael Hanselmann | return 0 |
114 | 0ad64cf8 | Michael Hanselmann | |
115 | 0ad64cf8 | Michael Hanselmann | |
116 | 07cd723a | Iustin Pop | def AutoArchiveJobs(opts, args): |
117 | 07cd723a | Iustin Pop | client = GetClient() |
118 | 07cd723a | Iustin Pop | |
119 | 07cd723a | Iustin Pop | age = args[0] |
120 | 07cd723a | Iustin Pop | |
121 | 07cd723a | Iustin Pop | if age == 'all': |
122 | 07cd723a | Iustin Pop | age = -1 |
123 | 07cd723a | Iustin Pop | else: |
124 | 07cd723a | Iustin Pop | age = ParseTimespec(age) |
125 | 07cd723a | Iustin Pop | |
126 | 07cd723a | Iustin Pop | client.AutoArchiveJobs(age) |
127 | 07cd723a | Iustin Pop | return 0 |
128 | 07cd723a | Iustin Pop | |
129 | 07cd723a | Iustin Pop | |
130 | d2b92ffc | Michael Hanselmann | def CancelJobs(opts, args): |
131 | d2b92ffc | Michael Hanselmann | client = GetClient() |
132 | d2b92ffc | Michael Hanselmann | |
133 | d2b92ffc | Michael Hanselmann | for job_id in args: |
134 | d2b92ffc | Michael Hanselmann | client.CancelJob(job_id) |
135 | d2b92ffc | Michael Hanselmann | |
136 | d2b92ffc | Michael Hanselmann | return 0 |
137 | d2b92ffc | Michael Hanselmann | |
138 | d2b92ffc | Michael Hanselmann | |
139 | 191712c0 | Iustin Pop | def ShowJobs(opts, args): |
140 | 191712c0 | Iustin Pop | """List the jobs |
141 | 191712c0 | Iustin Pop | |
142 | 191712c0 | Iustin Pop | """ |
143 | 191712c0 | Iustin Pop | def format(level, text): |
144 | 191712c0 | Iustin Pop | """Display the text indented.""" |
145 | 3a24c527 | Iustin Pop | ToStdout("%s%s", " " * level, text) |
146 | 191712c0 | Iustin Pop | |
147 | 191712c0 | Iustin Pop | def result_helper(value): |
148 | 191712c0 | Iustin Pop | """Format a result field in a nice way.""" |
149 | 191712c0 | Iustin Pop | if isinstance(value, (tuple, list)): |
150 | 191712c0 | Iustin Pop | return "[%s]" % (", ".join(str(elem) for elem in value)) |
151 | 191712c0 | Iustin Pop | else: |
152 | 191712c0 | Iustin Pop | return str(value) |
153 | 191712c0 | Iustin Pop | |
154 | aad81f98 | Iustin Pop | selected_fields = [ |
155 | aad81f98 | Iustin Pop | "id", "status", "ops", "opresult", "opstatus", "oplog", |
156 | aad81f98 | Iustin Pop | "opstart", "opend", "received_ts", "start_ts", "end_ts", |
157 | aad81f98 | Iustin Pop | ] |
158 | 191712c0 | Iustin Pop | |
159 | 191712c0 | Iustin Pop | result = GetClient().QueryJobs(args, selected_fields) |
160 | 191712c0 | Iustin Pop | |
161 | 191712c0 | Iustin Pop | first = True |
162 | 191712c0 | Iustin Pop | |
163 | b27b39b0 | Iustin Pop | for idx, entry in enumerate(result): |
164 | 191712c0 | Iustin Pop | if not first: |
165 | 191712c0 | Iustin Pop | format(0, "") |
166 | 191712c0 | Iustin Pop | else: |
167 | 191712c0 | Iustin Pop | first = False |
168 | aad81f98 | Iustin Pop | |
169 | aad81f98 | Iustin Pop | if entry is None: |
170 | b27b39b0 | Iustin Pop | if idx <= len(args): |
171 | b27b39b0 | Iustin Pop | format(0, "Job ID %s not found" % args[idx]) |
172 | b27b39b0 | Iustin Pop | else: |
173 | b27b39b0 | Iustin Pop | # this should not happen, when we don't pass args it will be a |
174 | b27b39b0 | Iustin Pop | # valid job returned |
175 | b27b39b0 | Iustin Pop | format(0, "Job ID requested as argument %s not found" % (idx + 1)) |
176 | aad81f98 | Iustin Pop | continue |
177 | aad81f98 | Iustin Pop | |
178 | aad81f98 | Iustin Pop | (job_id, status, ops, opresult, opstatus, oplog, |
179 | aad81f98 | Iustin Pop | opstart, opend, recv_ts, start_ts, end_ts) = entry |
180 | 191712c0 | Iustin Pop | format(0, "Job ID: %s" % job_id) |
181 | 191712c0 | Iustin Pop | if status in _USER_JOB_STATUS: |
182 | 191712c0 | Iustin Pop | status = _USER_JOB_STATUS[status] |
183 | 191712c0 | Iustin Pop | else: |
184 | 2f79bd34 | Iustin Pop | raise errors.ProgrammerError("Unknown job status code '%s'" % status) |
185 | 191712c0 | Iustin Pop | |
186 | 191712c0 | Iustin Pop | format(1, "Status: %s" % status) |
187 | aad81f98 | Iustin Pop | |
188 | aad81f98 | Iustin Pop | if recv_ts is not None: |
189 | aad81f98 | Iustin Pop | format(1, "Received: %s" % FormatTimestamp(recv_ts)) |
190 | aad81f98 | Iustin Pop | else: |
191 | aad81f98 | Iustin Pop | format(1, "Missing received timestamp (%s)" % str(recv_ts)) |
192 | aad81f98 | Iustin Pop | |
193 | aad81f98 | Iustin Pop | if start_ts is not None: |
194 | aad81f98 | Iustin Pop | if recv_ts is not None: |
195 | aad81f98 | Iustin Pop | d1 = start_ts[0] - recv_ts[0] + (start_ts[1] - recv_ts[1]) / 1000000.0 |
196 | aad81f98 | Iustin Pop | delta = " (delta %.6fs)" % d1 |
197 | aad81f98 | Iustin Pop | else: |
198 | aad81f98 | Iustin Pop | delta = "" |
199 | aad81f98 | Iustin Pop | format(1, "Processing start: %s%s" % (FormatTimestamp(start_ts), delta)) |
200 | aad81f98 | Iustin Pop | else: |
201 | aad81f98 | Iustin Pop | format(1, "Processing start: unknown (%s)" % str(start_ts)) |
202 | aad81f98 | Iustin Pop | |
203 | aad81f98 | Iustin Pop | if end_ts is not None: |
204 | aad81f98 | Iustin Pop | if start_ts is not None: |
205 | aad81f98 | Iustin Pop | d2 = end_ts[0] - start_ts[0] + (end_ts[1] - start_ts[1]) / 1000000.0 |
206 | aad81f98 | Iustin Pop | delta = " (delta %.6fs)" % d2 |
207 | aad81f98 | Iustin Pop | else: |
208 | aad81f98 | Iustin Pop | delta = "" |
209 | aad81f98 | Iustin Pop | format(1, "Processing end: %s%s" % (FormatTimestamp(end_ts), delta)) |
210 | aad81f98 | Iustin Pop | else: |
211 | aad81f98 | Iustin Pop | format(1, "Processing end: unknown (%s)" % str(end_ts)) |
212 | aad81f98 | Iustin Pop | |
213 | aad81f98 | Iustin Pop | if end_ts is not None and recv_ts is not None: |
214 | aad81f98 | Iustin Pop | d3 = end_ts[0] - recv_ts[0] + (end_ts[1] - recv_ts[1]) / 1000000.0 |
215 | aad81f98 | Iustin Pop | format(1, "Total processing time: %.6f seconds" % d3) |
216 | aad81f98 | Iustin Pop | else: |
217 | aad81f98 | Iustin Pop | format(1, "Total processing time: N/A") |
218 | 191712c0 | Iustin Pop | format(1, "Opcodes:") |
219 | aad81f98 | Iustin Pop | for (opcode, result, status, log, s_ts, e_ts) in \ |
220 | aad81f98 | Iustin Pop | zip(ops, opresult, opstatus, oplog, opstart, opend): |
221 | 191712c0 | Iustin Pop | format(2, "%s" % opcode["OP_ID"]) |
222 | 191712c0 | Iustin Pop | format(3, "Status: %s" % status) |
223 | aad81f98 | Iustin Pop | if isinstance(s_ts, (tuple, list)): |
224 | aad81f98 | Iustin Pop | format(3, "Processing start: %s" % FormatTimestamp(s_ts)) |
225 | aad81f98 | Iustin Pop | else: |
226 | aad81f98 | Iustin Pop | format(3, "No processing start time") |
227 | aad81f98 | Iustin Pop | if isinstance(e_ts, (tuple, list)): |
228 | aad81f98 | Iustin Pop | format(3, "Processing end: %s" % FormatTimestamp(e_ts)) |
229 | aad81f98 | Iustin Pop | else: |
230 | aad81f98 | Iustin Pop | format(3, "No processing end time") |
231 | 191712c0 | Iustin Pop | format(3, "Input fields:") |
232 | 191712c0 | Iustin Pop | for key, val in opcode.iteritems(): |
233 | 191712c0 | Iustin Pop | if key == "OP_ID": |
234 | 191712c0 | Iustin Pop | continue |
235 | 191712c0 | Iustin Pop | if isinstance(val, (tuple, list)): |
236 | 191712c0 | Iustin Pop | val = ",".join(val) |
237 | 191712c0 | Iustin Pop | format(4, "%s: %s" % (key, val)) |
238 | 191712c0 | Iustin Pop | if result is None: |
239 | 191712c0 | Iustin Pop | format(3, "No output data") |
240 | 191712c0 | Iustin Pop | elif isinstance(result, (tuple, list)): |
241 | 191712c0 | Iustin Pop | if not result: |
242 | 191712c0 | Iustin Pop | format(3, "Result: empty sequence") |
243 | 191712c0 | Iustin Pop | else: |
244 | 191712c0 | Iustin Pop | format(3, "Result:") |
245 | 191712c0 | Iustin Pop | for elem in result: |
246 | 191712c0 | Iustin Pop | format(4, result_helper(elem)) |
247 | 191712c0 | Iustin Pop | elif isinstance(result, dict): |
248 | 191712c0 | Iustin Pop | if not result: |
249 | 191712c0 | Iustin Pop | format(3, "Result: empty dictionary") |
250 | 191712c0 | Iustin Pop | else: |
251 | 191712c0 | Iustin Pop | for key, val in result.iteritems(): |
252 | 191712c0 | Iustin Pop | format(4, "%s: %s" % (key, result_helper(val))) |
253 | 191712c0 | Iustin Pop | else: |
254 | 191712c0 | Iustin Pop | format(3, "Result: %s" % result) |
255 | 5b23c34c | Iustin Pop | format(3, "Execution log:") |
256 | 3386e7a9 | Iustin Pop | for serial, log_ts, log_type, log_msg in log: |
257 | 3386e7a9 | Iustin Pop | time_txt = FormatTimestamp(log_ts) |
258 | 5b23c34c | Iustin Pop | encoded = str(log_msg).encode('string_escape') |
259 | 5b23c34c | Iustin Pop | format(4, "%s:%s:%s %s" % (serial, time_txt, log_type, encoded)) |
260 | 191712c0 | Iustin Pop | return 0 |
261 | 191712c0 | Iustin Pop | |
262 | 191712c0 | Iustin Pop | |
263 | 7a1ecaed | Iustin Pop | commands = { |
264 | 7a1ecaed | Iustin Pop | 'list': (ListJobs, ARGS_NONE, |
265 | 9a033156 | Iustin Pop | [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT], |
266 | 9a033156 | Iustin Pop | "", "List the jobs and their status. The available fields are" |
267 | 35049ff2 | Iustin Pop | " (see the man page for details): id, status, op_list," |
268 | 35049ff2 | Iustin Pop | " op_status, op_result." |
269 | 7a1ecaed | Iustin Pop | " The default field" |
270 | 0ad64cf8 | Michael Hanselmann | " list is (in order): %s." % ", ".join(_LIST_DEF_FIELDS)), |
271 | 0ad64cf8 | Michael Hanselmann | 'archive': (ArchiveJobs, ARGS_ANY, |
272 | 0ad64cf8 | Michael Hanselmann | [DEBUG_OPT], |
273 | 0ad64cf8 | Michael Hanselmann | "<job-id> [<job-id> ...]", |
274 | 0ad64cf8 | Michael Hanselmann | "Archive specified jobs"), |
275 | 07cd723a | Iustin Pop | 'autoarchive': (AutoArchiveJobs, ARGS_ONE, |
276 | 07cd723a | Iustin Pop | [DEBUG_OPT], |
277 | 07cd723a | Iustin Pop | "<age>", |
278 | 07cd723a | Iustin Pop | "Auto archive jobs older than the given age"), |
279 | d2b92ffc | Michael Hanselmann | 'cancel': (CancelJobs, ARGS_ANY, |
280 | d2b92ffc | Michael Hanselmann | [DEBUG_OPT], |
281 | d2b92ffc | Michael Hanselmann | "<job-id> [<job-id> ...]", |
282 | d2b92ffc | Michael Hanselmann | "Cancel specified jobs"), |
283 | 191712c0 | Iustin Pop | 'info': (ShowJobs, ARGS_ANY, [DEBUG_OPT], |
284 | 191712c0 | Iustin Pop | "<job-id> [<job-id> ...]", |
285 | 191712c0 | Iustin Pop | "Show detailed information about the specified jobs"), |
286 | 7a1ecaed | Iustin Pop | } |
287 | 7a1ecaed | Iustin Pop | |
288 | 7a1ecaed | Iustin Pop | |
289 | 7a1ecaed | Iustin Pop | if __name__ == '__main__': |
290 | 7a1ecaed | Iustin Pop | sys.exit(GenericMain(commands)) |