Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-job @ 746ea1da

History | View | Annotate | Download (3.5 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 7a5d3bbd Iustin Pop
_LIST_DEF_FIELDS = ["id", "status"]
37 7a5d3bbd Iustin Pop
38 af30b2fd Michael Hanselmann
_USER_JOB_STATUS = {
39 af30b2fd Michael Hanselmann
  constants.JOB_STATUS_QUEUED: "queued",
40 af30b2fd Michael Hanselmann
  constants.JOB_STATUS_RUNNING: "running",
41 af30b2fd Michael Hanselmann
  constants.JOB_STATUS_CANCELED: "canceled",
42 af30b2fd Michael Hanselmann
  constants.JOB_STATUS_SUCCESS: "success",
43 af30b2fd Michael Hanselmann
  constants.JOB_STATUS_ERROR: "error",
44 af30b2fd Michael Hanselmann
  }
45 af30b2fd Michael Hanselmann
46 0ad64cf8 Michael Hanselmann
47 7a1ecaed Iustin Pop
def ListJobs(opts, args):
48 7a1ecaed Iustin Pop
  """List the jobs
49 7a1ecaed Iustin Pop
50 7a1ecaed Iustin Pop
  """
51 7a1ecaed Iustin Pop
  if opts.output is None:
52 7a5d3bbd Iustin Pop
    selected_fields = _LIST_DEF_FIELDS
53 7a5d3bbd Iustin Pop
  elif opts.output.startswith("+"):
54 7a5d3bbd Iustin Pop
    selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",")
55 7a1ecaed Iustin Pop
  else:
56 7a1ecaed Iustin Pop
    selected_fields = opts.output.split(",")
57 7a1ecaed Iustin Pop
58 af30b2fd Michael Hanselmann
  output = GetClient().QueryJobs(None, selected_fields)
59 7a1ecaed Iustin Pop
  if not opts.no_headers:
60 af30b2fd Michael Hanselmann
    # TODO: Implement more fields
61 7a1ecaed Iustin Pop
    headers = {
62 7a1ecaed Iustin Pop
      "id": "ID",
63 7a1ecaed Iustin Pop
      "status": "Status",
64 af30b2fd Michael Hanselmann
      "ops": "OpCodes",
65 af30b2fd Michael Hanselmann
      "opresult": "OpCode_result",
66 af30b2fd Michael Hanselmann
      "opstatus": "OpCode_status",
67 7a1ecaed Iustin Pop
      }
68 7a1ecaed Iustin Pop
  else:
69 7a1ecaed Iustin Pop
    headers = None
70 7a1ecaed Iustin Pop
71 7a1ecaed Iustin Pop
  # we don't have yet unitfields here
72 7a1ecaed Iustin Pop
  unitfields = None
73 5ce81b28 Michael Hanselmann
  numfields = None
74 7a1ecaed Iustin Pop
75 7a1ecaed Iustin Pop
  # change raw values to nicer strings
76 7a1ecaed Iustin Pop
  for row in output:
77 7a1ecaed Iustin Pop
    for idx, field in enumerate(selected_fields):
78 7a1ecaed Iustin Pop
      val = row[idx]
79 7a1ecaed Iustin Pop
      if field == "status":
80 af30b2fd Michael Hanselmann
        if val in _USER_JOB_STATUS:
81 af30b2fd Michael Hanselmann
          val = _USER_JOB_STATUS[val]
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 0ad64cf8 Michael Hanselmann
def ArchiveJobs(opts, args):
97 0ad64cf8 Michael Hanselmann
  client = GetClient()
98 0ad64cf8 Michael Hanselmann
99 0ad64cf8 Michael Hanselmann
  for job_id in args:
100 0ad64cf8 Michael Hanselmann
    client.ArchiveJob(job_id)
101 0ad64cf8 Michael Hanselmann
102 0ad64cf8 Michael Hanselmann
  return 0
103 0ad64cf8 Michael Hanselmann
104 0ad64cf8 Michael Hanselmann
105 d2b92ffc Michael Hanselmann
def CancelJobs(opts, args):
106 d2b92ffc Michael Hanselmann
  client = GetClient()
107 d2b92ffc Michael Hanselmann
108 d2b92ffc Michael Hanselmann
  for job_id in args:
109 d2b92ffc Michael Hanselmann
    client.CancelJob(job_id)
110 d2b92ffc Michael Hanselmann
111 d2b92ffc Michael Hanselmann
  return 0
112 d2b92ffc Michael Hanselmann
113 d2b92ffc Michael Hanselmann
114 7a1ecaed Iustin Pop
commands = {
115 7a1ecaed Iustin Pop
  'list': (ListJobs, ARGS_NONE,
116 9a033156 Iustin Pop
            [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
117 9a033156 Iustin Pop
            "", "List the jobs and their status. The available fields are"
118 35049ff2 Iustin Pop
           " (see the man page for details): id, status, op_list,"
119 35049ff2 Iustin Pop
           " op_status, op_result."
120 7a1ecaed Iustin Pop
           " The default field"
121 0ad64cf8 Michael Hanselmann
           " list is (in order): %s." % ", ".join(_LIST_DEF_FIELDS)),
122 0ad64cf8 Michael Hanselmann
  'archive': (ArchiveJobs, ARGS_ANY,
123 0ad64cf8 Michael Hanselmann
              [DEBUG_OPT],
124 0ad64cf8 Michael Hanselmann
              "<job-id> [<job-id> ...]",
125 0ad64cf8 Michael Hanselmann
              "Archive specified jobs"),
126 d2b92ffc Michael Hanselmann
  'cancel': (CancelJobs, ARGS_ANY,
127 d2b92ffc Michael Hanselmann
             [DEBUG_OPT],
128 d2b92ffc Michael Hanselmann
             "<job-id> [<job-id> ...]",
129 d2b92ffc Michael Hanselmann
             "Cancel specified jobs"),
130 7a1ecaed Iustin Pop
  }
131 7a1ecaed Iustin Pop
132 7a1ecaed Iustin Pop
133 7a1ecaed Iustin Pop
if __name__ == '__main__':
134 7a1ecaed Iustin Pop
  sys.exit(GenericMain(commands))