Add a simple gnt-job script
[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       "opcodes": "OpCodes",
57       }
58   else:
59     headers = None
60
61   # we don't have yet unitfields here
62   unitfields = None
63   numfields = ["id"]
64
65   # change raw values to nicer strings
66   for row in output:
67     for idx, field in enumerate(selected_fields):
68       val = row[idx]
69       if field == "status":
70         if val == opcodes.Job.STATUS_PENDING:
71           val = "pending"
72         elif val == opcodes.Job.STATUS_RUNNING:
73           val = "running"
74         elif val == opcodes.Job.STATUS_FINISHED:
75           val = "finished"
76         else:
77           raise errors.ProgrammerError("Unknown job status code '%s'" % val)
78
79       row[idx] = str(val)
80
81   data = GenerateTable(separator=opts.separator, headers=headers,
82                        fields=selected_fields, unitfields=unitfields,
83                        numfields=numfields, data=output)
84   for line in data:
85     print line
86
87   return 0
88
89
90 commands = {
91   'list': (ListJobs, ARGS_NONE,
92             [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
93             "", "List the jobs and their status. The available fields are"
94            " (see the man page for details): id, status, opcodes."
95            " The default field"
96            " list is (in order): id, status."),
97   }
98
99
100 if __name__ == '__main__':
101   sys.exit(GenericMain(commands))