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)
63 op = opcodes.OpGroupAdd(group_name=group_name, ndparams=opts.ndparams,
64 alloc_policy=opts.alloc_policy,
65 diskparams=diskparams, ipolicy=ipolicy)
66 SubmitOpCode(op, opts=opts)
69 def AssignNodes(opts, args):
70 """Assign nodes to a group.
72 @param opts: the command line options selected by the user
74 @param args: args[0]: group to assign nodes to; args[1:]: nodes to assign
76 @return: the desired exit code
82 op = opcodes.OpGroupAssignNodes(group_name=group_name, nodes=node_names,
84 SubmitOpCode(op, opts=opts)
88 """Format dict data into command-line format.
90 @param data: The input dict to be formatted
91 @return: The formatted dict
97 return utils.CommaJoin(["%s=%s" % (key, value)
98 for key, value in data.items()])
101 def ListGroups(opts, args):
102 """List node groups and their properties.
104 @param opts: the command line options selected by the user
106 @param args: groups to list, or empty for all
108 @return: the desired exit code
111 desired_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
113 "node_list": (",".join, False),
114 "pinst_list": (",".join, False),
115 "ndparams": (_FmtDict, False),
118 return GenericList(constants.QR_GROUP, desired_fields, args, None,
119 opts.separator, not opts.no_headers,
120 format_override=fmtoverride, verbose=opts.verbose,
121 force_filter=opts.force_filter)
124 def ListGroupFields(opts, args):
127 @param opts: the command line options selected by the user
129 @param args: fields to list, or empty for all
131 @return: the desired exit code
134 return GenericListFields(constants.QR_GROUP, args, opts.separator,
138 def SetGroupParams(opts, args):
139 """Modifies a node group's parameters.
141 @param opts: the command line options selected by the user
143 @param args: should contain only one element, the node group name
146 @return: the desired exit code
149 if (opts.ndparams is None and opts.alloc_policy is None
150 and not opts.diskparams and not opts.hv_state and not opts.disk_state):
151 ToStderr("Please give at least one of the parameters.")
155 disk_state = utils.FlatToDict(opts.disk_state)
159 hv_state = dict(opts.hv_state)
161 diskparams = dict(opts.diskparams)
162 op = opcodes.OpGroupSetParams(group_name=args[0],
163 ndparams=opts.ndparams,
164 alloc_policy=opts.alloc_policy,
166 disk_state=disk_state,
167 diskparams=diskparams)
168 result = SubmitOrSend(op, opts)
171 ToStdout("Modified node group %s", args[0])
172 for param, data in result:
173 ToStdout(" - %-5s -> %s", param, data)
178 def RemoveGroup(opts, args):
179 """Remove a node group from the cluster.
181 @param opts: the command line options selected by the user
183 @param args: a list of length 1 with the name of the group to remove
185 @return: the desired exit code
189 op = opcodes.OpGroupRemove(group_name=group_name)
190 SubmitOpCode(op, opts=opts)
193 def RenameGroup(opts, args):
194 """Rename a node group.
196 @param opts: the command line options selected by the user
198 @param args: a list of length 2, [old_name, new_name]
200 @return: the desired exit code
203 group_name, new_name = args
204 op = opcodes.OpGroupRename(group_name=group_name, new_name=new_name)
205 SubmitOpCode(op, opts=opts)
208 def EvacuateGroup(opts, args):
209 """Evacuate a node group.
212 (group_name, ) = args
216 op = opcodes.OpGroupEvacuate(group_name=group_name,
217 iallocator=opts.iallocator,
218 target_groups=opts.to,
219 early_release=opts.early_release)
220 result = SubmitOpCode(op, cl=cl, opts=opts)
222 # Keep track of submitted jobs
223 jex = JobExecutor(cl=cl, opts=opts)
225 for (status, job_id) in result[constants.JOB_IDS_KEY]:
226 jex.AddJobId(None, status, job_id)
228 results = jex.GetResults()
229 bad_cnt = len([row for row in results if not row[0]])
231 ToStdout("All instances evacuated successfully.")
232 rcode = constants.EXIT_SUCCESS
234 ToStdout("There were %s errors during the evacuation.", bad_cnt)
235 rcode = constants.EXIT_FAILURE
239 INSTANCE_POLICY_OPTS = [
241 SPECS_DISK_COUNT_OPT,
249 AddGroup, ARGS_ONE_GROUP,
250 [DRY_RUN_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT, DISK_PARAMS_OPT] +
251 INSTANCE_POLICY_OPTS,
252 "<group_name>", "Add a new node group to the cluster"),
254 AssignNodes, ARGS_ONE_GROUP + ARGS_MANY_NODES, [DRY_RUN_OPT, FORCE_OPT],
255 "<group_name> <node>...", "Assign nodes to a group"),
257 ListGroups, ARGS_MANY_GROUPS,
258 [NOHDR_OPT, SEP_OPT, FIELDS_OPT, VERBOSE_OPT, FORCE_FILTER_OPT],
260 "Lists the node groups in the cluster. The available fields can be shown"
261 " using the \"list-fields\" command (see the man page for details)."
262 " The default list is (in order): %s." % utils.CommaJoin(_LIST_DEF_FIELDS)),
264 ListGroupFields, [ArgUnknown()], [NOHDR_OPT, SEP_OPT], "[fields...]",
265 "Lists all available fields for node groups"),
267 SetGroupParams, ARGS_ONE_GROUP,
268 [DRY_RUN_OPT, SUBMIT_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT, HV_STATE_OPT,
269 DISK_STATE_OPT, DISK_PARAMS_OPT],
270 "<group_name>", "Alters the parameters of a node group"),
272 RemoveGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT],
273 "[--dry-run] <group-name>",
274 "Remove an (empty) node group from the cluster"),
276 RenameGroup, [ArgGroup(min=2, max=2)], [DRY_RUN_OPT],
277 "[--dry-run] <group-name> <new-name>", "Rename a node group"),
279 EvacuateGroup, [ArgGroup(min=1, max=1)],
280 [TO_GROUP_OPT, IALLOCATOR_OPT, EARLY_RELEASE_OPT],
281 "[-I <iallocator>] [--to <group>]",
282 "Evacuate all instances within a group"),
284 ListTags, ARGS_ONE_GROUP, [PRIORITY_OPT],
285 "<instance_name>", "List the tags of the given instance"),
287 AddTags, [ArgGroup(min=1, max=1), ArgUnknown()],
288 [TAG_SRC_OPT, PRIORITY_OPT],
289 "<instance_name> tag...", "Add tags to the given instance"),
291 RemoveTags, [ArgGroup(min=1, max=1), ArgUnknown()],
292 [TAG_SRC_OPT, PRIORITY_OPT],
293 "<instance_name> tag...", "Remove tags from given instance"),
298 return GenericMain(commands,
299 override={"tag_type": constants.TAG_NODEGROUP},
300 env_override=_ENV_OVERRIDE)