Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.4 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 opcodes
30
from ganeti import utils
31

    
32

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

    
36

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

    
45

    
46
def AddGroup(opts, args):
47
  """Add a node group to the cluster.
48

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

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

    
60

    
61
def ListGroups(opts, args):
62
  """List node groups and their properties.
63

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

70
  """
71
  desired_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
72

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

    
75
  if opts.no_headers:
76
    headers = None
77
  else:
78
    headers = _LIST_HEADERS
79

    
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
  for row in output:
85
    for idx, field in enumerate(desired_fields):
86
      val = row[idx]
87

    
88
      if field in list_type_fields:
89
        val = ",".join(val)
90
      elif opts.roman_integers and field in int_type_fields:
91
        val = compat.TryToRoman(val)
92
      elif field in date_type_fields:
93
        val = utils.FormatTime(val)
94
      elif val is None:
95
        val = "?"
96

    
97
      row[idx] = str(val)
98

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

    
102
  for line in data:
103
    ToStdout(line)
104

    
105
  return 0
106

    
107

    
108
def SetGroupParams(opts, args):
109
  """Modifies a node group's parameters.
110

111
  @param opts: the command line options seletect by the user
112
  @type args: list
113
  @param args: should contain only one element, the node group name
114

115
  @rtype: int
116
  @return: the desired exit code
117

118
  """
119
  all_changes = {
120
    "ndparams": opts.ndparams,
121
  }
122

    
123
  if all_changes.values().count(None) == len(all_changes):
124
    ToStderr("Please give at least one of the parameters.")
125
    return 1
126

    
127
  op = opcodes.OpSetGroupParams(group_name=args[0], **all_changes)
128
  result = SubmitOrSend(op, opts)
129

    
130
  if result:
131
    ToStdout("Modified node group %s", args[0])
132
    for param, data in result:
133
      ToStdout(" - %-5s -> %s", param, data)
134

    
135
  return 0
136

    
137

    
138
def RemoveGroup(opts, args):
139
  """Remove a node group from the cluster.
140

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

147
  """
148
  (group_name,) = args
149
  op = opcodes.OpRemoveGroup(group_name=group_name)
150
  SubmitOpCode(op, opts=opts)
151

    
152

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

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

162
  """
163
  old_name, new_name = args
164
  op = opcodes.OpRenameGroup(old_name=old_name, new_name=new_name)
165
  SubmitOpCode(op, opts=opts)
166

    
167

    
168
commands = {
169
  "add": (
170
    AddGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT, NODE_PARAMS_OPT],
171
    "<group_name>", "Add a new node group to the cluster"),
172
  "list": (
173
    ListGroups, ARGS_MANY_GROUPS,
174
    [NOHDR_OPT, SEP_OPT, FIELDS_OPT, SYNC_OPT, ROMAN_OPT],
175
    "[<group_name>...]",
176
    "Lists the node groups in the cluster. The available fields are (see"
177
    " the man page for details): %s. The default list is (in order): %s." %
178
    (utils.CommaJoin(_LIST_HEADERS), utils.CommaJoin(_LIST_DEF_FIELDS))),
179
  "modify": (
180
    SetGroupParams, ARGS_ONE_GROUP,
181
    [DRY_RUN_OPT, SUBMIT_OPT, NODE_PARAMS_OPT],
182
    "<group_name>", "Alters the parameters of a node group"),
183
  "remove": (
184
    RemoveGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT],
185
    "[--dry-run] <group_name>",
186
    "Remove an (empty) node group from the cluster"),
187
  "rename": (
188
    RenameGroup, [ArgGroup(min=2, max=2)], [DRY_RUN_OPT],
189
    "[--dry-run] <old_name> <new_name>", "Rename a node group"),
190
}
191

    
192

    
193
def Main():
194
  return GenericMain(commands)