root / scripts / gnt-job @ 1f05af2b
History | View | Annotate | Download (3.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"] |
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 |
} |
68 |
else: |
69 |
headers = None |
70 |
|
71 |
# we don't have yet unitfields here |
72 |
unitfields = None |
73 |
numfields = None |
74 |
|
75 |
# change raw values to nicer strings |
76 |
for row in output: |
77 |
for idx, field in enumerate(selected_fields): |
78 |
val = row[idx] |
79 |
if field == "status": |
80 |
if val in _USER_JOB_STATUS: |
81 |
val = _USER_JOB_STATUS[val] |
82 |
else: |
83 |
raise errors.ProgrammerError("Unknown job status code '%s'" % val) |
84 |
|
85 |
row[idx] = str(val) |
86 |
|
87 |
data = GenerateTable(separator=opts.separator, headers=headers, |
88 |
fields=selected_fields, unitfields=unitfields, |
89 |
numfields=numfields, data=output) |
90 |
for line in data: |
91 |
print line |
92 |
|
93 |
return 0 |
94 |
|
95 |
|
96 |
def ArchiveJobs(opts, args): |
97 |
client = GetClient() |
98 |
|
99 |
for job_id in args: |
100 |
client.ArchiveJob(job_id) |
101 |
|
102 |
return 0 |
103 |
|
104 |
|
105 |
def CancelJobs(opts, args): |
106 |
client = GetClient() |
107 |
|
108 |
for job_id in args: |
109 |
client.CancelJob(job_id) |
110 |
|
111 |
return 0 |
112 |
|
113 |
|
114 |
commands = { |
115 |
'list': (ListJobs, ARGS_NONE, |
116 |
[DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT], |
117 |
"", "List the jobs and their status. The available fields are" |
118 |
" (see the man page for details): id, status, op_list," |
119 |
" op_status, op_result." |
120 |
" The default field" |
121 |
" list is (in order): %s." % ", ".join(_LIST_DEF_FIELDS)), |
122 |
'archive': (ArchiveJobs, ARGS_ANY, |
123 |
[DEBUG_OPT], |
124 |
"<job-id> [<job-id> ...]", |
125 |
"Archive specified jobs"), |
126 |
'cancel': (CancelJobs, ARGS_ANY, |
127 |
[DEBUG_OPT], |
128 |
"<job-id> [<job-id> ...]", |
129 |
"Cancel specified jobs"), |
130 |
} |
131 |
|
132 |
|
133 |
if __name__ == '__main__': |
134 |
sys.exit(GenericMain(commands)) |