root / scripts / gnt-job @ 2da55f7c
History | View | Annotate | Download (3 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"] |
37 |
|
38 |
def ListJobs(opts, args): |
39 |
"""List the jobs |
40 |
|
41 |
""" |
42 |
if opts.output is None: |
43 |
selected_fields = _LIST_DEF_FIELDS |
44 |
elif opts.output.startswith("+"): |
45 |
selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",") |
46 |
else: |
47 |
selected_fields = opts.output.split(",") |
48 |
|
49 |
query = { |
50 |
"object": "jobs", |
51 |
"fields": selected_fields, |
52 |
"names": [], |
53 |
} |
54 |
|
55 |
output = SubmitQuery(query) |
56 |
if not opts.no_headers: |
57 |
headers = { |
58 |
"id": "ID", |
59 |
"status": "Status", |
60 |
"op_list": "OpCodes", |
61 |
"op_status": "OpStatus", |
62 |
"op_result": "OpResult", |
63 |
} |
64 |
else: |
65 |
headers = None |
66 |
|
67 |
# we don't have yet unitfields here |
68 |
unitfields = None |
69 |
numfields = ["id"] |
70 |
|
71 |
# change raw values to nicer strings |
72 |
for row in output: |
73 |
for idx, field in enumerate(selected_fields): |
74 |
val = row[idx] |
75 |
if field == "status": |
76 |
if val == opcodes.Job.STATUS_PENDING: |
77 |
val = "pending" |
78 |
elif val == opcodes.Job.STATUS_RUNNING: |
79 |
val = "running" |
80 |
elif val == opcodes.Job.STATUS_SUCCESS: |
81 |
val = "finished" |
82 |
elif val == opcodes.Job.STATUS_FAIL: |
83 |
val = "failed" |
84 |
elif val == opcodes.Job.STATUS_ABORT: |
85 |
val = "aborted" |
86 |
else: |
87 |
raise errors.ProgrammerError("Unknown job status code '%s'" % val) |
88 |
|
89 |
row[idx] = str(val) |
90 |
|
91 |
data = GenerateTable(separator=opts.separator, headers=headers, |
92 |
fields=selected_fields, unitfields=unitfields, |
93 |
numfields=numfields, data=output) |
94 |
for line in data: |
95 |
print line |
96 |
|
97 |
return 0 |
98 |
|
99 |
|
100 |
commands = { |
101 |
'list': (ListJobs, ARGS_NONE, |
102 |
[DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT], |
103 |
"", "List the jobs and their status. The available fields are" |
104 |
" (see the man page for details): id, status, op_list," |
105 |
" op_status, op_result." |
106 |
" The default field" |
107 |
" list is (in order): %s." % ", ".join(_LIST_DEF_FIELDS)) |
108 |
} |
109 |
|
110 |
|
111 |
if __name__ == '__main__': |
112 |
sys.exit(GenericMain(commands)) |