gnt_group.py 4.51 KB
Newer Older
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
#
#

# Copyright (C) 2010 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.

"""Node group related commands"""

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

from ganeti.cli import *
from ganeti import compat
29
from ganeti import opcodes
30
from ganeti import utils
31
32
33
34
35
36
37
38
39
40
41


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


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


46
47
48
49
50
51
52
53
54
55
56
def AddGroup(opts, args):
  """Add a node group to the cluster.

  @param opts: the command line options selected by the user
  @type args: list
  @param args: a list of length 1 with the name of the group to create
  @rtype: int
  @return: the desired exit code

  """
  (group_name,) = args
57
  op = opcodes.OpAddGroup(group_name=group_name, ndparams=opts.ndparams)
58
59
60
  SubmitOpCode(op, opts=opts)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def ListGroups(opts, args):
  """List node groups and their properties.

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

  """
  desired_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)

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

  if opts.no_headers:
    headers = None
  else:
    headers = _LIST_HEADERS

80
  int_type_fields = frozenset(["node_cnt", "pinst_cnt", "serial_no"])
81
  list_type_fields = frozenset(["node_list", "pinst_list"])
82
  date_type_fields = frozenset(["mtime", "ctime"])
83
84
85
86
87
88
89
90
91

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

      if field in list_type_fields:
        val = ",".join(val)
      elif opts.roman_integers and field in int_type_fields:
        val = compat.TryToRoman(val)
92
93
      elif field in date_type_fields:
        val = utils.FormatTime(val)
94
95
96
97
98
99
100
101
102
103
104
105
106
107
      elif val is None:
        val = "?"

      row[idx] = str(val)

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

  for line in data:
    ToStdout(line)

  return 0


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
def RemoveGroup(opts, args):
  """Remove a node group from the cluster.

  @param opts: the command line options selected by the user
  @type args: list
  @param args: a list of length 1 with the name of the group to remove
  @rtype: int
  @return: the desired exit code

  """
  (group_name,) = args
  op = opcodes.OpRemoveGroup(group_name=group_name)
  SubmitOpCode(op, opts=opts)


def RenameGroup(opts, args):
  """Rename a node group.

  @param opts: the command line options selected by the user
  @type args: list
  @param args: a list of length 2, [old_name, new_name]
  @rtype: int
  @return: the desired exit code

  """
  old_name, new_name = args
  op = opcodes.OpRenameGroup(old_name=old_name, new_name=new_name)
  SubmitOpCode(op, opts=opts)


138
commands = {
139
  "add": (
140
    AddGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT, NODE_PARAMS_OPT],
141
    "<group_name>", "Add a new node group to the cluster"),
142
143
144
  "list": (
    ListGroups, ARGS_MANY_GROUPS,
    [NOHDR_OPT, SEP_OPT, FIELDS_OPT, SYNC_OPT, ROMAN_OPT],
145
146
147
148
    "[<group_name>...]",
    "Lists the node groups in the cluster. The available fields are (see"
    " the man page for details): %s. The default list is (in order): %s." %
    (utils.CommaJoin(_LIST_HEADERS), utils.CommaJoin(_LIST_DEF_FIELDS))),
149
150
151
152
153
154
155
  "remove": (
    RemoveGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT],
    "[--dry-run] <group_name>",
    "Remove an (empty) node group from the cluster"),
  "rename": (
    RenameGroup, [ArgGroup(min=2, max=2)], [DRY_RUN_OPT],
    "[--dry-run] <old_name> <new_name>", "Rename a node group"),
156
157
158
159
160
}


def Main():
  return GenericMain(commands)