Statistics
| Branch: | Tag: | Revision:

root / lib / client / gnt_group.py @ 667dbd6b

History | View | Annotate | Download (2.5 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 667dbd6b Adeodato Simo
30 667dbd6b Adeodato Simo
31 667dbd6b Adeodato Simo
#: default list of fields for L{ListGroups}
32 667dbd6b Adeodato Simo
_LIST_DEF_FIELDS = ["name", "node_cnt", "pinst_cnt"]
33 667dbd6b Adeodato Simo
34 667dbd6b Adeodato Simo
35 667dbd6b Adeodato Simo
#: headers (and full field list) for L{ListGroups}
36 667dbd6b Adeodato Simo
_LIST_HEADERS = {
37 667dbd6b Adeodato Simo
  "name": "Group", "uuid": "UUID",
38 667dbd6b Adeodato Simo
  "node_cnt": "Nodes", "node_list": "NodeList",
39 667dbd6b Adeodato Simo
  "pinst_cnt": "Instances", "pinst_list": "InstanceList",
40 667dbd6b Adeodato Simo
}
41 667dbd6b Adeodato Simo
42 667dbd6b Adeodato Simo
43 667dbd6b Adeodato Simo
def ListGroups(opts, args):
44 667dbd6b Adeodato Simo
  """List node groups and their properties.
45 667dbd6b Adeodato Simo

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

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