6a82561de2b2b698137021cf548c16e3ffbbadde
[ganeti-local] / scripts / gnt-job
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 def ListJobs(opts, args):
47   """List the jobs
48
49   """
50   if opts.output is None:
51     selected_fields = _LIST_DEF_FIELDS
52   elif opts.output.startswith("+"):
53     selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",")
54   else:
55     selected_fields = opts.output.split(",")
56
57   output = GetClient().QueryJobs(None, selected_fields)
58   if not opts.no_headers:
59     # TODO: Implement more fields
60     headers = {
61       "id": "ID",
62       "status": "Status",
63       "ops": "OpCodes",
64       "opresult": "OpCode_result",
65       "opstatus": "OpCode_status",
66       }
67   else:
68     headers = None
69
70   # we don't have yet unitfields here
71   unitfields = None
72   numfields = None
73
74   # change raw values to nicer strings
75   for row in output:
76     for idx, field in enumerate(selected_fields):
77       val = row[idx]
78       if field == "status":
79         if val in _USER_JOB_STATUS:
80           val = _USER_JOB_STATUS[val]
81         else:
82           raise errors.ProgrammerError("Unknown job status code '%s'" % val)
83
84       row[idx] = str(val)
85
86   data = GenerateTable(separator=opts.separator, headers=headers,
87                        fields=selected_fields, unitfields=unitfields,
88                        numfields=numfields, data=output)
89   for line in data:
90     print line
91
92   return 0
93
94
95 commands = {
96   'list': (ListJobs, ARGS_NONE,
97             [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
98             "", "List the jobs and their status. The available fields are"
99            " (see the man page for details): id, status, op_list,"
100            " op_status, op_result."
101            " The default field"
102            " list is (in order): %s." % ", ".join(_LIST_DEF_FIELDS))
103   }
104
105
106 if __name__ == '__main__':
107   sys.exit(GenericMain(commands))