Statistics
| Branch: | Tag: | Revision:

root / lib / client / gnt_group.py @ 57de31c0

History | View | Annotate | Download (7.9 kB)

1
#
2
#
3

    
4
# Copyright (C) 2010, 2011 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=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 constants
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", "alloc_policy", "ndparams"]
35

    
36

    
37
_ENV_OVERRIDE = frozenset(["list"])
38

    
39

    
40
def AddGroup(opts, args):
41
  """Add a node group to the cluster.
42

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

49
  """
50
  (group_name,) = args
51
  op = opcodes.OpGroupAdd(group_name=group_name, ndparams=opts.ndparams,
52
                          alloc_policy=opts.alloc_policy)
53
  SubmitOpCode(op, opts=opts)
54

    
55

    
56
def AssignNodes(opts, args):
57
  """Assign nodes to a group.
58

59
  @param opts: the command line options selected by the user
60
  @type args: list
61
  @param args: args[0]: group to assign nodes to; args[1:]: nodes to assign
62
  @rtype: int
63
  @return: the desired exit code
64

65
  """
66
  group_name = args[0]
67
  node_names = args[1:]
68

    
69
  op = opcodes.OpGroupAssignNodes(group_name=group_name, nodes=node_names,
70
                                  force=opts.force)
71
  SubmitOpCode(op, opts=opts)
72

    
73

    
74
def _FmtDict(data):
75
  """Format dict data into command-line format.
76

77
  @param data: The input dict to be formatted
78
  @return: The formatted dict
79

80
  """
81
  if not data:
82
    return "(empty)"
83

    
84
  return utils.CommaJoin(["%s=%s" % (key, value)
85
                          for key, value in data.items()])
86

    
87

    
88
def ListGroups(opts, args):
89
  """List node groups and their properties.
90

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

97
  """
98
  desired_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
99
  fmtoverride = {
100
    "node_list": (",".join, False),
101
    "pinst_list": (",".join, False),
102
    "ndparams": (_FmtDict, False),
103
    }
104

    
105
  return GenericList(constants.QR_GROUP, desired_fields, args, None,
106
                     opts.separator, not opts.no_headers,
107
                     format_override=fmtoverride, verbose=opts.verbose,
108
                     force_filter=opts.force_filter)
109

    
110

    
111
def ListGroupFields(opts, args):
112
  """List node fields.
113

114
  @param opts: the command line options selected by the user
115
  @type args: list
116
  @param args: fields to list, or empty for all
117
  @rtype: int
118
  @return: the desired exit code
119

120
  """
121
  return GenericListFields(constants.QR_GROUP, args, opts.separator,
122
                           not opts.no_headers)
123

    
124

    
125
def SetGroupParams(opts, args):
126
  """Modifies a node group's parameters.
127

128
  @param opts: the command line options selected by the user
129
  @type args: list
130
  @param args: should contain only one element, the node group name
131

132
  @rtype: int
133
  @return: the desired exit code
134

135
  """
136
  if opts.ndparams is None and opts.alloc_policy is None:
137
    ToStderr("Please give at least one of the parameters.")
138
    return 1
139

    
140
  op = opcodes.OpGroupSetParams(group_name=args[0],
141
                                ndparams=opts.ndparams,
142
                                alloc_policy=opts.alloc_policy)
143
  result = SubmitOrSend(op, opts)
144

    
145
  if result:
146
    ToStdout("Modified node group %s", args[0])
147
    for param, data in result:
148
      ToStdout(" - %-5s -> %s", param, data)
149

    
150
  return 0
151

    
152

    
153
def RemoveGroup(opts, args):
154
  """Remove a node group from the cluster.
155

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

162
  """
163
  (group_name,) = args
164
  op = opcodes.OpGroupRemove(group_name=group_name)
165
  SubmitOpCode(op, opts=opts)
166

    
167

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

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

177
  """
178
  group_name, new_name = args
179
  op = opcodes.OpGroupRename(group_name=group_name, new_name=new_name)
180
  SubmitOpCode(op, opts=opts)
181

    
182

    
183
def EvacuateGroup(opts, args):
184
  """Evacuate a node group.
185

186
  """
187
  (group_name, ) = args
188

    
189
  cl = GetClient()
190

    
191
  op = opcodes.OpGroupEvacuate(group_name=group_name,
192
                               iallocator=opts.iallocator,
193
                               target_groups=opts.to,
194
                               early_release=opts.early_release)
195
  result = SubmitOpCode(op, cl=cl, opts=opts)
196

    
197
  # Keep track of submitted jobs
198
  jex = JobExecutor(cl=cl, opts=opts)
199

    
200
  for (status, job_id) in result[constants.JOB_IDS_KEY]:
201
    jex.AddJobId(None, status, job_id)
202

    
203
  results = jex.GetResults()
204
  bad_cnt = len([row for row in results if not row[0]])
205
  if bad_cnt == 0:
206
    ToStdout("All instances evacuated successfully.")
207
    rcode = constants.EXIT_SUCCESS
208
  else:
209
    ToStdout("There were %s errors during the evacuation.", bad_cnt)
210
    rcode = constants.EXIT_FAILURE
211

    
212
  return rcode
213

    
214

    
215
commands = {
216
  "add": (
217
    AddGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT],
218
    "<group_name>", "Add a new node group to the cluster"),
219
  "assign-nodes": (
220
    AssignNodes, ARGS_ONE_GROUP + ARGS_MANY_NODES, [DRY_RUN_OPT, FORCE_OPT],
221
    "<group_name> <node>...", "Assign nodes to a group"),
222
  "list": (
223
    ListGroups, ARGS_MANY_GROUPS,
224
    [NOHDR_OPT, SEP_OPT, FIELDS_OPT, VERBOSE_OPT, FORCE_FILTER_OPT],
225
    "[<group_name>...]",
226
    "Lists the node groups in the cluster. The available fields can be shown"
227
    " using the \"list-fields\" command (see the man page for details)."
228
    " The default list is (in order): %s." % utils.CommaJoin(_LIST_DEF_FIELDS)),
229
  "list-fields": (
230
    ListGroupFields, [ArgUnknown()], [NOHDR_OPT, SEP_OPT], "[fields...]",
231
    "Lists all available fields for node groups"),
232
  "modify": (
233
    SetGroupParams, ARGS_ONE_GROUP,
234
    [DRY_RUN_OPT, SUBMIT_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT],
235
    "<group_name>", "Alters the parameters of a node group"),
236
  "remove": (
237
    RemoveGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT],
238
    "[--dry-run] <group-name>",
239
    "Remove an (empty) node group from the cluster"),
240
  "rename": (
241
    RenameGroup, [ArgGroup(min=2, max=2)], [DRY_RUN_OPT],
242
    "[--dry-run] <group-name> <new-name>", "Rename a node group"),
243
  "evacuate": (
244
    EvacuateGroup, [ArgGroup(min=1, max=1)],
245
    [TO_GROUP_OPT, IALLOCATOR_OPT, EARLY_RELEASE_OPT],
246
    "[-I <iallocator>] [--to <group>]",
247
    "Evacuate all instances within a group"),
248
  "list-tags": (
249
    ListTags, ARGS_ONE_GROUP, [PRIORITY_OPT],
250
    "<instance_name>", "List the tags of the given instance"),
251
  "add-tags": (
252
    AddTags, [ArgGroup(min=1, max=1), ArgUnknown()],
253
    [TAG_SRC_OPT, PRIORITY_OPT],
254
    "<instance_name> tag...", "Add tags to the given instance"),
255
  "remove-tags": (
256
    RemoveTags, [ArgGroup(min=1, max=1), ArgUnknown()],
257
    [TAG_SRC_OPT, PRIORITY_OPT],
258
    "<instance_name> tag...", "Remove tags from given instance"),
259
  }
260

    
261

    
262
def Main():
263
  return GenericMain(commands,
264
                     override={"tag_type": constants.TAG_NODEGROUP},
265
                     env_override=_ENV_OVERRIDE)