root / scripts / gnt-job @ 191712c0
History | View | Annotate | Download (5.5 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 |
import sys |
23 |
import os |
24 |
import itertools |
25 |
from optparse import make_option |
26 |
from cStringIO import StringIO |
27 |
|
28 |
from ganeti.cli import * |
29 |
from ganeti import opcodes |
30 |
from ganeti import logger |
31 |
from ganeti import constants |
32 |
from ganeti import utils |
33 |
from ganeti import errors |
34 |
|
35 |
|
36 |
_LIST_DEF_FIELDS = ["id", "status", "summary"] |
37 |
|
38 |
_USER_JOB_STATUS = { |
39 |
constants.JOB_STATUS_QUEUED: "queued", |
40 |
constants.JOB_STATUS_RUNNING: "running", |
41 |
constants.JOB_STATUS_CANCELED: "canceled", |
42 |
constants.JOB_STATUS_SUCCESS: "success", |
43 |
constants.JOB_STATUS_ERROR: "error", |
44 |
} |
45 |
|
46 |
|
47 |
def ListJobs(opts, args): |
48 |
"""List the jobs |
49 |
|
50 |
""" |
51 |
if opts.output is None: |
52 |
selected_fields = _LIST_DEF_FIELDS |
53 |
elif opts.output.startswith("+"): |
54 |
selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",") |
55 |
else: |
56 |
selected_fields = opts.output.split(",") |
57 |
|
58 |
output = GetClient().QueryJobs(None, selected_fields) |
59 |
if not opts.no_headers: |
60 |
# TODO: Implement more fields |
61 |
headers = { |
62 |
"id": "ID", |
63 |
"status": "Status", |
64 |
"ops": "OpCodes", |
65 |
"opresult": "OpCode_result", |
66 |
"opstatus": "OpCode_status", |
67 |
"summary": "Summary", |
68 |
} |
69 |
else: |
70 |
headers = None |
71 |
|
72 |
# we don't have yet unitfields here |
73 |
unitfields = None |
74 |
numfields = None |
75 |
|
76 |
# change raw values to nicer strings |
77 |
for row in output: |
78 |
for idx, field in enumerate(selected_fields): |
79 |
val = row[idx] |
80 |
if field == "status": |
81 |
if val in _USER_JOB_STATUS: |
82 |
val = _USER_JOB_STATUS[val] |
83 |
else: |
84 |
raise errors.ProgrammerError("Unknown job status code '%s'" % val) |
85 |
elif field == "summary": |
86 |
val = ",".join(val) |
87 |
|
88 |
row[idx] = str(val) |
89 |
|
90 |
data = GenerateTable(separator=opts.separator, headers=headers, |
91 |
fields=selected_fields, unitfields=unitfields, |
92 |
numfields=numfields, data=output) |
93 |
for line in data: |
94 |
print line |
95 |
|
96 |
return 0 |
97 |
|
98 |
|
99 |
def ArchiveJobs(opts, args): |
100 |
client = GetClient() |
101 |
|
102 |
for job_id in args: |
103 |
client.ArchiveJob(job_id) |
104 |
|
105 |
return 0 |
106 |
|
107 |
|
108 |
def CancelJobs(opts, args): |
109 |
client = GetClient() |
110 |
|
111 |
for job_id in args: |
112 |
client.CancelJob(job_id) |
113 |
|
114 |
return 0 |
115 |
|
116 |
|
117 |
def ShowJobs(opts, args): |
118 |
"""List the jobs |
119 |
|
120 |
""" |
121 |
def format(level, text): |
122 |
"""Display the text indented.""" |
123 |
print "%s%s" % (" " * level, text) |
124 |
|
125 |
def result_helper(value): |
126 |
"""Format a result field in a nice way.""" |
127 |
if isinstance(value, (tuple, list)): |
128 |
return "[%s]" % (", ".join(str(elem) for elem in value)) |
129 |
else: |
130 |
return str(value) |
131 |
|
132 |
selected_fields = ["id", "status", "ops", "opresult", "opstatus"] |
133 |
|
134 |
result = GetClient().QueryJobs(args, selected_fields) |
135 |
|
136 |
first = True |
137 |
|
138 |
for job_id, status, ops, opresult, opstatus in result: |
139 |
if not first: |
140 |
format(0, "") |
141 |
else: |
142 |
first = False |
143 |
format(0, "Job ID: %s" % job_id) |
144 |
if status in _USER_JOB_STATUS: |
145 |
status = _USER_JOB_STATUS[status] |
146 |
else: |
147 |
raise errors.ProgrammerError("Unknown job status code '%s'" % val) |
148 |
|
149 |
format(1, "Status: %s" % status) |
150 |
format(1, "Opcodes:") |
151 |
for opcode, result, status in zip(ops, opresult, opstatus): |
152 |
format(2, "%s" % opcode["OP_ID"]) |
153 |
format(3, "Status: %s" % status) |
154 |
format(3, "Input fields:") |
155 |
for key, val in opcode.iteritems(): |
156 |
if key == "OP_ID": |
157 |
continue |
158 |
if isinstance(val, (tuple, list)): |
159 |
val = ",".join(val) |
160 |
format(4, "%s: %s" % (key, val)) |
161 |
if result is None: |
162 |
format(3, "No output data") |
163 |
elif isinstance(result, (tuple, list)): |
164 |
if not result: |
165 |
format(3, "Result: empty sequence") |
166 |
else: |
167 |
format(3, "Result:") |
168 |
for elem in result: |
169 |
format(4, result_helper(elem)) |
170 |
elif isinstance(result, dict): |
171 |
if not result: |
172 |
format(3, "Result: empty dictionary") |
173 |
else: |
174 |
for key, val in result.iteritems(): |
175 |
format(4, "%s: %s" % (key, result_helper(val))) |
176 |
else: |
177 |
format(3, "Result: %s" % result) |
178 |
return 0 |
179 |
|
180 |
|
181 |
commands = { |
182 |
'list': (ListJobs, ARGS_NONE, |
183 |
[DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT], |
184 |
"", "List the jobs and their status. The available fields are" |
185 |
" (see the man page for details): id, status, op_list," |
186 |
" op_status, op_result." |
187 |
" The default field" |
188 |
" list is (in order): %s." % ", ".join(_LIST_DEF_FIELDS)), |
189 |
'archive': (ArchiveJobs, ARGS_ANY, |
190 |
[DEBUG_OPT], |
191 |
"<job-id> [<job-id> ...]", |
192 |
"Archive specified jobs"), |
193 |
'cancel': (CancelJobs, ARGS_ANY, |
194 |
[DEBUG_OPT], |
195 |
"<job-id> [<job-id> ...]", |
196 |
"Cancel specified jobs"), |
197 |
'info': (ShowJobs, ARGS_ANY, [DEBUG_OPT], |
198 |
"<job-id> [<job-id> ...]", |
199 |
"Show detailed information about the specified jobs"), |
200 |
} |
201 |
|
202 |
|
203 |
if __name__ == '__main__': |
204 |
sys.exit(GenericMain(commands)) |