Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-job @ 00fb8246

History | View | Annotate | Download (2.9 kB)

1 7a1ecaed Iustin Pop
#!/usr/bin/python
2 7a1ecaed Iustin Pop
#
3 7a1ecaed Iustin Pop
4 7a1ecaed Iustin Pop
# Copyright (C) 2006, 2007 Google Inc.
5 7a1ecaed Iustin Pop
#
6 7a1ecaed Iustin Pop
# This program is free software; you can redistribute it and/or modify
7 7a1ecaed Iustin Pop
# it under the terms of the GNU General Public License as published by
8 7a1ecaed Iustin Pop
# the Free Software Foundation; either version 2 of the License, or
9 7a1ecaed Iustin Pop
# (at your option) any later version.
10 7a1ecaed Iustin Pop
#
11 7a1ecaed Iustin Pop
# This program is distributed in the hope that it will be useful, but
12 7a1ecaed Iustin Pop
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 7a1ecaed Iustin Pop
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 7a1ecaed Iustin Pop
# General Public License for more details.
15 7a1ecaed Iustin Pop
#
16 7a1ecaed Iustin Pop
# You should have received a copy of the GNU General Public License
17 7a1ecaed Iustin Pop
# along with this program; if not, write to the Free Software
18 7a1ecaed Iustin Pop
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 7a1ecaed Iustin Pop
# 02110-1301, USA.
20 7a1ecaed Iustin Pop
21 7a1ecaed Iustin Pop
22 7a1ecaed Iustin Pop
import sys
23 7a1ecaed Iustin Pop
import os
24 7a1ecaed Iustin Pop
import itertools
25 7a1ecaed Iustin Pop
from optparse import make_option
26 7a1ecaed Iustin Pop
from cStringIO import StringIO
27 7a1ecaed Iustin Pop
28 7a1ecaed Iustin Pop
from ganeti.cli import *
29 7a1ecaed Iustin Pop
from ganeti import opcodes
30 7a1ecaed Iustin Pop
from ganeti import logger
31 7a1ecaed Iustin Pop
from ganeti import constants
32 7a1ecaed Iustin Pop
from ganeti import utils
33 7a1ecaed Iustin Pop
from ganeti import errors
34 7a1ecaed Iustin Pop
35 7a1ecaed Iustin Pop
36 7a1ecaed Iustin Pop
def ListJobs(opts, args):
37 7a1ecaed Iustin Pop
  """List the jobs
38 7a1ecaed Iustin Pop
39 7a1ecaed Iustin Pop
  """
40 7a1ecaed Iustin Pop
  if opts.output is None:
41 7a1ecaed Iustin Pop
    selected_fields = ["id", "status"]
42 7a1ecaed Iustin Pop
  else:
43 7a1ecaed Iustin Pop
    selected_fields = opts.output.split(",")
44 7a1ecaed Iustin Pop
45 7a1ecaed Iustin Pop
  query = {
46 7a1ecaed Iustin Pop
    "object": "jobs",
47 7a1ecaed Iustin Pop
    "fields": selected_fields,
48 7a1ecaed Iustin Pop
    "names": [],
49 7a1ecaed Iustin Pop
    }
50 7a1ecaed Iustin Pop
51 7a1ecaed Iustin Pop
  output = SubmitQuery(query)
52 7a1ecaed Iustin Pop
  if not opts.no_headers:
53 7a1ecaed Iustin Pop
    headers = {
54 7a1ecaed Iustin Pop
      "id": "ID",
55 7a1ecaed Iustin Pop
      "status": "Status",
56 35049ff2 Iustin Pop
      "op_list": "OpCodes",
57 35049ff2 Iustin Pop
      "op_status": "OpStatus",
58 35049ff2 Iustin Pop
      "op_result": "OpResult",
59 7a1ecaed Iustin Pop
      }
60 7a1ecaed Iustin Pop
  else:
61 7a1ecaed Iustin Pop
    headers = None
62 7a1ecaed Iustin Pop
63 7a1ecaed Iustin Pop
  # we don't have yet unitfields here
64 7a1ecaed Iustin Pop
  unitfields = None
65 7a1ecaed Iustin Pop
  numfields = ["id"]
66 7a1ecaed Iustin Pop
67 7a1ecaed Iustin Pop
  # change raw values to nicer strings
68 7a1ecaed Iustin Pop
  for row in output:
69 7a1ecaed Iustin Pop
    for idx, field in enumerate(selected_fields):
70 7a1ecaed Iustin Pop
      val = row[idx]
71 7a1ecaed Iustin Pop
      if field == "status":
72 7a1ecaed Iustin Pop
        if val == opcodes.Job.STATUS_PENDING:
73 7a1ecaed Iustin Pop
          val = "pending"
74 7a1ecaed Iustin Pop
        elif val == opcodes.Job.STATUS_RUNNING:
75 7a1ecaed Iustin Pop
          val = "running"
76 35049ff2 Iustin Pop
        elif val == opcodes.Job.STATUS_SUCCESS:
77 7a1ecaed Iustin Pop
          val = "finished"
78 35049ff2 Iustin Pop
        elif val == opcodes.Job.STATUS_FAIL:
79 35049ff2 Iustin Pop
          val = "failed"
80 35049ff2 Iustin Pop
        elif val == opcodes.Job.STATUS_ABORT:
81 35049ff2 Iustin Pop
          val = "aborted"
82 7a1ecaed Iustin Pop
        else:
83 7a1ecaed Iustin Pop
          raise errors.ProgrammerError("Unknown job status code '%s'" % val)
84 7a1ecaed Iustin Pop
85 7a1ecaed Iustin Pop
      row[idx] = str(val)
86 7a1ecaed Iustin Pop
87 7a1ecaed Iustin Pop
  data = GenerateTable(separator=opts.separator, headers=headers,
88 7a1ecaed Iustin Pop
                       fields=selected_fields, unitfields=unitfields,
89 7a1ecaed Iustin Pop
                       numfields=numfields, data=output)
90 7a1ecaed Iustin Pop
  for line in data:
91 7a1ecaed Iustin Pop
    print line
92 7a1ecaed Iustin Pop
93 7a1ecaed Iustin Pop
  return 0
94 7a1ecaed Iustin Pop
95 7a1ecaed Iustin Pop
96 7a1ecaed Iustin Pop
commands = {
97 7a1ecaed Iustin Pop
  'list': (ListJobs, ARGS_NONE,
98 9a033156 Iustin Pop
            [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
99 9a033156 Iustin Pop
            "", "List the jobs and their status. The available fields are"
100 35049ff2 Iustin Pop
           " (see the man page for details): id, status, op_list,"
101 35049ff2 Iustin Pop
           " op_status, op_result."
102 7a1ecaed Iustin Pop
           " The default field"
103 7a1ecaed Iustin Pop
           " list is (in order): id, status."),
104 7a1ecaed Iustin Pop
  }
105 7a1ecaed Iustin Pop
106 7a1ecaed Iustin Pop
107 7a1ecaed Iustin Pop
if __name__ == '__main__':
108 7a1ecaed Iustin Pop
  sys.exit(GenericMain(commands))