4 # Copyright (C) 2010, 2011 Google Inc.
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.
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.
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
21 """Node group related commands"""
23 # pylint: disable=W0401,W0614
24 # W0401: Wildcard import ganeti.cli
25 # W0614: Unused import %s from wildcard import (since we need cli)
27 from ganeti.cli import *
28 from ganeti import constants
29 from ganeti import objects
30 from ganeti import opcodes
31 from ganeti import utils
34 #: default list of fields for L{ListGroups}
35 _LIST_DEF_FIELDS = ["name", "node_cnt", "pinst_cnt", "alloc_policy", "ndparams"]
38 _ENV_OVERRIDE = frozenset(["list"])
41 def AddGroup(opts, args):
42 """Add a node group to the cluster.
44 @param opts: the command line options selected by the user
46 @param args: a list of length 1 with the name of the group to create
48 @return: the desired exit code
52 objects.CreateIPolicyFromOpts(ispecs_mem_size=opts.ispecs_mem_size,
53 ispecs_cpu_count=opts.ispecs_cpu_count,
54 ispecs_disk_count=opts.ispecs_disk_count,
55 ispecs_disk_size=opts.ispecs_disk_size,
56 ispecs_nic_count=opts.ispecs_nic_count,
58 for key in ipolicy.keys():
59 utils.ForceDictType(ipolicy[key], constants.ISPECS_PARAMETER_TYPES)
62 diskparams = dict(opts.diskparams)
65 disk_state = utils.FlatToDict(opts.disk_state)
68 hv_state = dict(opts.hv_state)
70 op = opcodes.OpGroupAdd(group_name=group_name, ndparams=opts.ndparams,
71 alloc_policy=opts.alloc_policy,
72 diskparams=diskparams, ipolicy=ipolicy,
74 disk_state=disk_state)
75 SubmitOpCode(op, opts=opts)
78 def AssignNodes(opts, args):
79 """Assign nodes to a group.
81 @param opts: the command line options selected by the user
83 @param args: args[0]: group to assign nodes to; args[1:]: nodes to assign
85 @return: the desired exit code
91 op = opcodes.OpGroupAssignNodes(group_name=group_name, nodes=node_names,
93 SubmitOpCode(op, opts=opts)
97 """Format dict data into command-line format.
99 @param data: The input dict to be formatted
100 @return: The formatted dict
106 return utils.CommaJoin(["%s=%s" % (key, value)
107 for key, value in data.items()])
110 def ListGroups(opts, args):
111 """List node groups and their properties.
113 @param opts: the command line options selected by the user
115 @param args: groups to list, or empty for all
117 @return: the desired exit code
120 desired_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
122 "node_list": (",".join, False),
123 "pinst_list": (",".join, False),
124 "ndparams": (_FmtDict, False),
127 return GenericList(constants.QR_GROUP, desired_fields, args, None,
128 opts.separator, not opts.no_headers,
129 format_override=fmtoverride, verbose=opts.verbose,
130 force_filter=opts.force_filter)
133 def ListGroupFields(opts, args):
136 @param opts: the command line options selected by the user
138 @param args: fields to list, or empty for all
140 @return: the desired exit code
143 return GenericListFields(constants.QR_GROUP, args, opts.separator,
147 def SetGroupParams(opts, args):
148 """Modifies a node group's parameters.
150 @param opts: the command line options selected by the user
152 @param args: should contain only one element, the node group name
155 @return: the desired exit code
158 allmods = [opts.ndparams, opts.alloc_policy, opts.diskparams, opts.hv_state,
159 opts.disk_state, opts.ispecs_mem_size, opts.ispecs_cpu_count,
160 opts.ispecs_disk_count, opts.ispecs_disk_size,
161 opts.ispecs_nic_count, opts.diskparams]
162 if allmods.count(None) == len(allmods):
163 ToStderr("Please give at least one of the parameters.")
167 disk_state = utils.FlatToDict(opts.disk_state)
171 hv_state = dict(opts.hv_state)
173 diskparams = dict(opts.diskparams)
175 # set the default values
177 opts.ispecs_mem_size,
178 opts.ispecs_cpu_count,
179 opts.ispecs_disk_count,
180 opts.ispecs_disk_size,
181 opts.ispecs_nic_count,
183 for ispec in to_ipolicy:
185 if isinstance(ispec[param], basestring):
186 if ispec[param].lower() == "default":
187 ispec[param] = constants.VALUE_DEFAULT
188 # create ipolicy object
189 ipolicy = objects.CreateIPolicyFromOpts(\
190 ispecs_mem_size=opts.ispecs_mem_size,
191 ispecs_cpu_count=opts.ispecs_cpu_count,
192 ispecs_disk_count=opts.ispecs_disk_count,
193 ispecs_disk_size=opts.ispecs_disk_size,
194 ispecs_nic_count=opts.ispecs_nic_count,
196 allowed_values=[constants.VALUE_DEFAULT])
197 for key in ipolicy.keys():
198 utils.ForceDictType(ipolicy[key], constants.ISPECS_PARAMETER_TYPES,
199 allowed_values=[constants.VALUE_DEFAULT])
201 op = opcodes.OpGroupSetParams(group_name=args[0],
202 ndparams=opts.ndparams,
203 alloc_policy=opts.alloc_policy,
205 disk_state=disk_state,
206 diskparams=diskparams,
209 result = SubmitOrSend(op, opts)
212 ToStdout("Modified node group %s", args[0])
213 for param, data in result:
214 ToStdout(" - %-5s -> %s", param, data)
219 def RemoveGroup(opts, args):
220 """Remove a node group from the cluster.
222 @param opts: the command line options selected by the user
224 @param args: a list of length 1 with the name of the group to remove
226 @return: the desired exit code
230 op = opcodes.OpGroupRemove(group_name=group_name)
231 SubmitOpCode(op, opts=opts)
234 def RenameGroup(opts, args):
235 """Rename a node group.
237 @param opts: the command line options selected by the user
239 @param args: a list of length 2, [old_name, new_name]
241 @return: the desired exit code
244 group_name, new_name = args
245 op = opcodes.OpGroupRename(group_name=group_name, new_name=new_name)
246 SubmitOpCode(op, opts=opts)
249 def EvacuateGroup(opts, args):
250 """Evacuate a node group.
253 (group_name, ) = args
257 op = opcodes.OpGroupEvacuate(group_name=group_name,
258 iallocator=opts.iallocator,
259 target_groups=opts.to,
260 early_release=opts.early_release)
261 result = SubmitOpCode(op, cl=cl, opts=opts)
263 # Keep track of submitted jobs
264 jex = JobExecutor(cl=cl, opts=opts)
266 for (status, job_id) in result[constants.JOB_IDS_KEY]:
267 jex.AddJobId(None, status, job_id)
269 results = jex.GetResults()
270 bad_cnt = len([row for row in results if not row[0]])
272 ToStdout("All instances evacuated successfully.")
273 rcode = constants.EXIT_SUCCESS
275 ToStdout("There were %s errors during the evacuation.", bad_cnt)
276 rcode = constants.EXIT_FAILURE
280 INSTANCE_POLICY_OPTS = [
282 SPECS_DISK_COUNT_OPT,
290 AddGroup, ARGS_ONE_GROUP,
291 [DRY_RUN_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT, DISK_PARAMS_OPT,
292 HV_STATE_OPT, DISK_STATE_OPT] + INSTANCE_POLICY_OPTS,
293 "<group_name>", "Add a new node group to the cluster"),
295 AssignNodes, ARGS_ONE_GROUP + ARGS_MANY_NODES, [DRY_RUN_OPT, FORCE_OPT],
296 "<group_name> <node>...", "Assign nodes to a group"),
298 ListGroups, ARGS_MANY_GROUPS,
299 [NOHDR_OPT, SEP_OPT, FIELDS_OPT, VERBOSE_OPT, FORCE_FILTER_OPT],
301 "Lists the node groups in the cluster. The available fields can be shown"
302 " using the \"list-fields\" command (see the man page for details)."
303 " The default list is (in order): %s." % utils.CommaJoin(_LIST_DEF_FIELDS)),
305 ListGroupFields, [ArgUnknown()], [NOHDR_OPT, SEP_OPT], "[fields...]",
306 "Lists all available fields for node groups"),
308 SetGroupParams, ARGS_ONE_GROUP,
309 [DRY_RUN_OPT, SUBMIT_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT, HV_STATE_OPT,
310 DISK_STATE_OPT, DISK_PARAMS_OPT] + INSTANCE_POLICY_OPTS,
311 "<group_name>", "Alters the parameters of a node group"),
313 RemoveGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT],
314 "[--dry-run] <group-name>",
315 "Remove an (empty) node group from the cluster"),
317 RenameGroup, [ArgGroup(min=2, max=2)], [DRY_RUN_OPT],
318 "[--dry-run] <group-name> <new-name>", "Rename a node group"),
320 EvacuateGroup, [ArgGroup(min=1, max=1)],
321 [TO_GROUP_OPT, IALLOCATOR_OPT, EARLY_RELEASE_OPT],
322 "[-I <iallocator>] [--to <group>]",
323 "Evacuate all instances within a group"),
325 ListTags, ARGS_ONE_GROUP, [PRIORITY_OPT],
326 "<instance_name>", "List the tags of the given instance"),
328 AddTags, [ArgGroup(min=1, max=1), ArgUnknown()],
329 [TAG_SRC_OPT, PRIORITY_OPT],
330 "<instance_name> tag...", "Add tags to the given instance"),
332 RemoveTags, [ArgGroup(min=1, max=1), ArgUnknown()],
333 [TAG_SRC_OPT, PRIORITY_OPT],
334 "<instance_name> tag...", "Remove tags from given instance"),
339 return GenericMain(commands,
340 override={"tag_type": constants.TAG_NODEGROUP},
341 env_override=_ENV_OVERRIDE)