Small style fixes
[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 def ListJobs(opts, args):
37   """List the jobs
38
39   """
40   if opts.output is None:
41     selected_fields = ["id", "status"]
42   else:
43     selected_fields = opts.output.split(",")
44
45   query = {
46     "object": "jobs",
47     "fields": selected_fields,
48     "names": [],
49     }
50
51   output = SubmitQuery(query)
52   if not opts.no_headers:
53     headers = {
54       "id": "ID",
55       "status": "Status",
56       "op_list": "OpCodes",
57       "op_status": "OpStatus",
58       "op_result": "OpResult",
59       }
60   else:
61     headers = None
62
63   # we don't have yet unitfields here
64   unitfields = None
65   numfields = ["id"]
66
67   # change raw values to nicer strings
68   for row in output:
69     for idx, field in enumerate(selected_fields):
70       val = row[idx]
71       if field == "status":
72         if val == opcodes.Job.STATUS_PENDING:
73           val = "pending"
74         elif val == opcodes.Job.STATUS_RUNNING:
75           val = "running"
76         elif val == opcodes.Job.STATUS_SUCCESS:
77           val = "finished"
78         elif val == opcodes.Job.STATUS_FAIL:
79           val = "failed"
80         elif val == opcodes.Job.STATUS_ABORT:
81           val = "aborted"
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 commands = {
97   'list': (ListJobs, ARGS_NONE,
98             [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
99             "", "List the jobs and their status. The available fields are"
100            " (see the man page for details): id, status, op_list,"
101            " op_status, op_result."
102            " The default field"
103            " list is (in order): id, status."),
104   }
105
106
107 if __name__ == '__main__':
108   sys.exit(GenericMain(commands))