gnt-job 3.21 KB
Newer Older
Iustin Pop's avatar
Iustin Pop committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/python
#

# Copyright (C) 2006, 2007 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.


import sys
import os
import itertools
from optparse import make_option
from cStringIO import StringIO

from ganeti.cli import *
from ganeti import opcodes
from ganeti import logger
from ganeti import constants
from ganeti import utils
from ganeti import errors


36
37
_LIST_DEF_FIELDS = ["id", "status"]

38
39
40
41
42
43
44
45
_USER_JOB_STATUS = {
  constants.JOB_STATUS_QUEUED: "queued",
  constants.JOB_STATUS_RUNNING: "running",
  constants.JOB_STATUS_CANCELED: "canceled",
  constants.JOB_STATUS_SUCCESS: "success",
  constants.JOB_STATUS_ERROR: "error",
  }

46

Iustin Pop's avatar
Iustin Pop committed
47
48
49
50
51
def ListJobs(opts, args):
  """List the jobs

  """
  if opts.output is None:
52
53
54
    selected_fields = _LIST_DEF_FIELDS
  elif opts.output.startswith("+"):
    selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",")
Iustin Pop's avatar
Iustin Pop committed
55
56
57
  else:
    selected_fields = opts.output.split(",")

58
  output = GetClient().QueryJobs(None, selected_fields)
Iustin Pop's avatar
Iustin Pop committed
59
  if not opts.no_headers:
60
    # TODO: Implement more fields
Iustin Pop's avatar
Iustin Pop committed
61
62
63
    headers = {
      "id": "ID",
      "status": "Status",
64
65
66
      "ops": "OpCodes",
      "opresult": "OpCode_result",
      "opstatus": "OpCode_status",
Iustin Pop's avatar
Iustin Pop committed
67
68
69
70
71
72
      }
  else:
    headers = None

  # we don't have yet unitfields here
  unitfields = None
73
  numfields = None
Iustin Pop's avatar
Iustin Pop committed
74
75
76
77
78
79

  # change raw values to nicer strings
  for row in output:
    for idx, field in enumerate(selected_fields):
      val = row[idx]
      if field == "status":
80
81
        if val in _USER_JOB_STATUS:
          val = _USER_JOB_STATUS[val]
Iustin Pop's avatar
Iustin Pop committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
        else:
          raise errors.ProgrammerError("Unknown job status code '%s'" % val)

      row[idx] = str(val)

  data = GenerateTable(separator=opts.separator, headers=headers,
                       fields=selected_fields, unitfields=unitfields,
                       numfields=numfields, data=output)
  for line in data:
    print line

  return 0


96
97
98
99
100
101
102
103
104
def ArchiveJobs(opts, args):
  client = GetClient()

  for job_id in args:
    client.ArchiveJob(job_id)

  return 0


Iustin Pop's avatar
Iustin Pop committed
105
106
commands = {
  'list': (ListJobs, ARGS_NONE,
107
108
            [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
            "", "List the jobs and their status. The available fields are"
109
110
           " (see the man page for details): id, status, op_list,"
           " op_status, op_result."
Iustin Pop's avatar
Iustin Pop committed
111
           " The default field"
112
113
114
115
116
           " list is (in order): %s." % ", ".join(_LIST_DEF_FIELDS)),
  'archive': (ArchiveJobs, ARGS_ANY,
              [DEBUG_OPT],
              "<job-id> [<job-id> ...]",
              "Archive specified jobs"),
Iustin Pop's avatar
Iustin Pop committed
117
118
119
120
121
  }


if __name__ == '__main__':
  sys.exit(GenericMain(commands))