Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (2.9 kB)

1
#
2
#
3

    
4
# Copyright (C) 2010 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
"""Node group related commands"""
22

    
23
# pylint: disable-msg=W0401,W0614
24
# W0401: Wildcard import ganeti.cli
25
# W0614: Unused import %s from wildcard import (since we need cli)
26

    
27
from ganeti.cli import *
28
from ganeti import compat
29
from ganeti import utils
30

    
31

    
32
#: default list of fields for L{ListGroups}
33
_LIST_DEF_FIELDS = ["name", "node_cnt", "pinst_cnt"]
34

    
35

    
36
#: headers (and full field list) for L{ListGroups}
37
_LIST_HEADERS = {
38
  "name": "Group", "uuid": "UUID",
39
  "node_cnt": "Nodes", "node_list": "NodeList",
40
  "pinst_cnt": "Instances", "pinst_list": "InstanceList",
41
  "ctime": "CTime", "mtime": "MTime", "serial_no": "SerialNo",
42
}
43

    
44

    
45
def ListGroups(opts, args):
46
  """List node groups and their properties.
47

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

54
  """
55
  desired_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
56

    
57
  output = GetClient().QueryGroups(args, desired_fields, opts.do_locking)
58

    
59
  if opts.no_headers:
60
    headers = None
61
  else:
62
    headers = _LIST_HEADERS
63

    
64
  int_type_fields = frozenset(["node_cnt", "pinst_cnt", "serial_no"])
65
  list_type_fields = frozenset(["node_list", "pinst_list"])
66
  date_type_fields = frozenset(["mtime", "ctime"])
67

    
68
  for row in output:
69
    for idx, field in enumerate(desired_fields):
70
      val = row[idx]
71

    
72
      if field in list_type_fields:
73
        val = ",".join(val)
74
      elif opts.roman_integers and field in int_type_fields:
75
        val = compat.TryToRoman(val)
76
      elif field in date_type_fields:
77
        val = utils.FormatTime(val)
78
      elif val is None:
79
        val = "?"
80

    
81
      row[idx] = str(val)
82

    
83
  data = GenerateTable(separator=opts.separator, headers=headers,
84
                       fields=desired_fields, data=output)
85

    
86
  for line in data:
87
    ToStdout(line)
88

    
89
  return 0
90

    
91

    
92
commands = {
93
  "list": (
94
    ListGroups, ARGS_MANY_GROUPS,
95
    [NOHDR_OPT, SEP_OPT, FIELDS_OPT, SYNC_OPT, ROMAN_OPT],
96
    "[<group_name>...]",
97
    "Lists the node groups in the cluster. The available fields are (see"
98
    " the man page for details): %s. The default list is (in order): %s." %
99
    (utils.CommaJoin(_LIST_HEADERS), utils.CommaJoin(_LIST_DEF_FIELDS))),
100
}
101

    
102

    
103
def Main():
104
  return GenericMain(commands)