Statistics
| Branch: | Tag: | Revision:

root / lib / client / gnt_group.py @ 4edc512c

History | View | Annotate | Download (2.9 kB)

1 667dbd6b Adeodato Simo
#
2 667dbd6b Adeodato Simo
#
3 667dbd6b Adeodato Simo
4 667dbd6b Adeodato Simo
# Copyright (C) 2010 Google Inc.
5 667dbd6b Adeodato Simo
#
6 667dbd6b Adeodato Simo
# This program is free software; you can redistribute it and/or modify
7 667dbd6b Adeodato Simo
# it under the terms of the GNU General Public License as published by
8 667dbd6b Adeodato Simo
# the Free Software Foundation; either version 2 of the License, or
9 667dbd6b Adeodato Simo
# (at your option) any later version.
10 667dbd6b Adeodato Simo
#
11 667dbd6b Adeodato Simo
# This program is distributed in the hope that it will be useful, but
12 667dbd6b Adeodato Simo
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 667dbd6b Adeodato Simo
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 667dbd6b Adeodato Simo
# General Public License for more details.
15 667dbd6b Adeodato Simo
#
16 667dbd6b Adeodato Simo
# You should have received a copy of the GNU General Public License
17 667dbd6b Adeodato Simo
# along with this program; if not, write to the Free Software
18 667dbd6b Adeodato Simo
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 667dbd6b Adeodato Simo
# 02110-1301, USA.
20 667dbd6b Adeodato Simo
21 667dbd6b Adeodato Simo
"""Node group related commands"""
22 667dbd6b Adeodato Simo
23 667dbd6b Adeodato Simo
# pylint: disable-msg=W0401,W0614
24 667dbd6b Adeodato Simo
# W0401: Wildcard import ganeti.cli
25 667dbd6b Adeodato Simo
# W0614: Unused import %s from wildcard import (since we need cli)
26 667dbd6b Adeodato Simo
27 667dbd6b Adeodato Simo
from ganeti.cli import *
28 667dbd6b Adeodato Simo
from ganeti import compat
29 4edc512c Adeodato Simo
from ganeti import utils
30 667dbd6b Adeodato Simo
31 667dbd6b Adeodato Simo
32 667dbd6b Adeodato Simo
#: default list of fields for L{ListGroups}
33 667dbd6b Adeodato Simo
_LIST_DEF_FIELDS = ["name", "node_cnt", "pinst_cnt"]
34 667dbd6b Adeodato Simo
35 667dbd6b Adeodato Simo
36 667dbd6b Adeodato Simo
#: headers (and full field list) for L{ListGroups}
37 667dbd6b Adeodato Simo
_LIST_HEADERS = {
38 667dbd6b Adeodato Simo
  "name": "Group", "uuid": "UUID",
39 667dbd6b Adeodato Simo
  "node_cnt": "Nodes", "node_list": "NodeList",
40 667dbd6b Adeodato Simo
  "pinst_cnt": "Instances", "pinst_list": "InstanceList",
41 4edc512c Adeodato Simo
  "ctime": "CTime", "mtime": "MTime", "serial_no": "SerialNo",
42 667dbd6b Adeodato Simo
}
43 667dbd6b Adeodato Simo
44 667dbd6b Adeodato Simo
45 667dbd6b Adeodato Simo
def ListGroups(opts, args):
46 667dbd6b Adeodato Simo
  """List node groups and their properties.
47 667dbd6b Adeodato Simo

48 667dbd6b Adeodato Simo
  @param opts: the command line options selected by the user
49 667dbd6b Adeodato Simo
  @type args: list
50 667dbd6b Adeodato Simo
  @param args: groups to list, or empty for all
51 667dbd6b Adeodato Simo
  @rtype: int
52 667dbd6b Adeodato Simo
  @return: the desired exit code
53 667dbd6b Adeodato Simo

54 667dbd6b Adeodato Simo
  """
55 667dbd6b Adeodato Simo
  desired_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
56 667dbd6b Adeodato Simo
57 667dbd6b Adeodato Simo
  output = GetClient().QueryGroups(args, desired_fields, opts.do_locking)
58 667dbd6b Adeodato Simo
59 667dbd6b Adeodato Simo
  if opts.no_headers:
60 667dbd6b Adeodato Simo
    headers = None
61 667dbd6b Adeodato Simo
  else:
62 667dbd6b Adeodato Simo
    headers = _LIST_HEADERS
63 667dbd6b Adeodato Simo
64 4edc512c Adeodato Simo
  int_type_fields = frozenset(["node_cnt", "pinst_cnt", "serial_no"])
65 667dbd6b Adeodato Simo
  list_type_fields = frozenset(["node_list", "pinst_list"])
66 4edc512c Adeodato Simo
  date_type_fields = frozenset(["mtime", "ctime"])
67 667dbd6b Adeodato Simo
68 667dbd6b Adeodato Simo
  for row in output:
69 667dbd6b Adeodato Simo
    for idx, field in enumerate(desired_fields):
70 667dbd6b Adeodato Simo
      val = row[idx]
71 667dbd6b Adeodato Simo
72 667dbd6b Adeodato Simo
      if field in list_type_fields:
73 667dbd6b Adeodato Simo
        val = ",".join(val)
74 667dbd6b Adeodato Simo
      elif opts.roman_integers and field in int_type_fields:
75 667dbd6b Adeodato Simo
        val = compat.TryToRoman(val)
76 4edc512c Adeodato Simo
      elif field in date_type_fields:
77 4edc512c Adeodato Simo
        val = utils.FormatTime(val)
78 667dbd6b Adeodato Simo
      elif val is None:
79 667dbd6b Adeodato Simo
        val = "?"
80 667dbd6b Adeodato Simo
81 667dbd6b Adeodato Simo
      row[idx] = str(val)
82 667dbd6b Adeodato Simo
83 667dbd6b Adeodato Simo
  data = GenerateTable(separator=opts.separator, headers=headers,
84 667dbd6b Adeodato Simo
                       fields=desired_fields, data=output)
85 667dbd6b Adeodato Simo
86 667dbd6b Adeodato Simo
  for line in data:
87 667dbd6b Adeodato Simo
    ToStdout(line)
88 667dbd6b Adeodato Simo
89 667dbd6b Adeodato Simo
  return 0
90 667dbd6b Adeodato Simo
91 667dbd6b Adeodato Simo
92 667dbd6b Adeodato Simo
commands = {
93 667dbd6b Adeodato Simo
  "list": (
94 667dbd6b Adeodato Simo
    ListGroups, ARGS_MANY_GROUPS,
95 667dbd6b Adeodato Simo
    [NOHDR_OPT, SEP_OPT, FIELDS_OPT, SYNC_OPT, ROMAN_OPT],
96 4edc512c Adeodato Simo
    "[<group_name>...]",
97 4edc512c Adeodato Simo
    "Lists the node groups in the cluster. The available fields are (see"
98 4edc512c Adeodato Simo
    " the man page for details): %s. The default list is (in order): %s." %
99 4edc512c Adeodato Simo
    (utils.CommaJoin(_LIST_HEADERS), utils.CommaJoin(_LIST_DEF_FIELDS))),
100 667dbd6b Adeodato Simo
}
101 667dbd6b Adeodato Simo
102 667dbd6b Adeodato Simo
103 667dbd6b Adeodato Simo
def Main():
104 667dbd6b Adeodato Simo
  return GenericMain(commands)