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-msg=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 opcodes
30 from ganeti import utils
33 #: default list of fields for L{ListGroups}
34 _LIST_DEF_FIELDS = ["name", "node_cnt", "pinst_cnt", "alloc_policy", "ndparams"]
37 def AddGroup(opts, args):
38 """Add a node group to the cluster.
40 @param opts: the command line options selected by the user
42 @param args: a list of length 1 with the name of the group to create
44 @return: the desired exit code
48 op = opcodes.OpGroupAdd(group_name=group_name, ndparams=opts.ndparams,
49 alloc_policy=opts.alloc_policy)
50 SubmitOpCode(op, opts=opts)
53 def AssignNodes(opts, args):
54 """Assign nodes to a group.
56 @param opts: the command line options selected by the user
58 @param args: args[0]: group to assign nodes to; args[1:]: nodes to assign
60 @return: the desired exit code
66 op = opcodes.OpGroupAssignNodes(group_name=group_name, nodes=node_names,
68 SubmitOpCode(op, opts=opts)
72 """Format dict data into command-line format.
74 @param data: The input dict to be formatted
75 @return: The formatted dict
81 return utils.CommaJoin(["%s=%s" % (key, value)
82 for key, value in data.items()])
85 def ListGroups(opts, args):
86 """List node groups and their properties.
88 @param opts: the command line options selected by the user
90 @param args: groups to list, or empty for all
92 @return: the desired exit code
95 desired_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
97 "node_list": (",".join, False),
98 "pinst_list": (",".join, False),
99 "ndparams": (_FmtDict, False),
102 return GenericList(constants.QR_GROUP, desired_fields, args, None,
103 opts.separator, not opts.no_headers,
104 format_override=fmtoverride, verbose=opts.verbose,
105 force_filter=opts.force_filter)
108 def ListGroupFields(opts, args):
111 @param opts: the command line options selected by the user
113 @param args: fields to list, or empty for all
115 @return: the desired exit code
118 return GenericListFields(constants.QR_GROUP, args, opts.separator,
122 def SetGroupParams(opts, args):
123 """Modifies a node group's parameters.
125 @param opts: the command line options selected by the user
127 @param args: should contain only one element, the node group name
130 @return: the desired exit code
134 "ndparams": opts.ndparams,
135 "alloc_policy": opts.alloc_policy,
138 if all_changes.values().count(None) == len(all_changes):
139 ToStderr("Please give at least one of the parameters.")
142 op = opcodes.OpGroupSetParams(group_name=args[0], # pylint: disable-msg=W0142
144 result = SubmitOrSend(op, opts)
147 ToStdout("Modified node group %s", args[0])
148 for param, data in result:
149 ToStdout(" - %-5s -> %s", param, data)
154 def RemoveGroup(opts, args):
155 """Remove a node group from the cluster.
157 @param opts: the command line options selected by the user
159 @param args: a list of length 1 with the name of the group to remove
161 @return: the desired exit code
165 op = opcodes.OpGroupRemove(group_name=group_name)
166 SubmitOpCode(op, opts=opts)
169 def RenameGroup(opts, args):
170 """Rename a node group.
172 @param opts: the command line options selected by the user
174 @param args: a list of length 2, [old_name, new_name]
176 @return: the desired exit code
179 group_name, new_name = args
180 op = opcodes.OpGroupRename(group_name=group_name, new_name=new_name)
181 SubmitOpCode(op, opts=opts)
184 def EvacuateGroup(opts, args):
185 """Evacuate a node group.
188 (group_name, ) = args
192 op = opcodes.OpGroupEvacuate(group_name=group_name,
193 iallocator=opts.iallocator,
194 target_groups=opts.to,
195 early_release=opts.early_release)
196 result = SubmitOpCode(op, cl=cl, opts=opts)
198 # Keep track of submitted jobs
199 jex = JobExecutor(cl=cl, opts=opts)
201 for (status, job_id) in result[constants.JOB_IDS_KEY]:
202 jex.AddJobId(None, status, job_id)
204 results = jex.GetResults()
205 bad_cnt = len([row for row in results if not row[0]])
207 ToStdout("All instances evacuated successfully.")
208 rcode = constants.EXIT_SUCCESS
210 ToStdout("There were %s errors during the evacuation.", bad_cnt)
211 rcode = constants.EXIT_FAILURE
218 AddGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT],
219 "<group_name>", "Add a new node group to the cluster"),
221 AssignNodes, ARGS_ONE_GROUP + ARGS_MANY_NODES, [DRY_RUN_OPT, FORCE_OPT],
222 "<group_name> <node>...", "Assign nodes to a group"),
224 ListGroups, ARGS_MANY_GROUPS,
225 [NOHDR_OPT, SEP_OPT, FIELDS_OPT, VERBOSE_OPT, FORCE_FILTER_OPT],
227 "Lists the node groups in the cluster. The available fields can be shown"
228 " using the \"list-fields\" command (see the man page for details)."
229 " The default list is (in order): %s." % utils.CommaJoin(_LIST_DEF_FIELDS)),
231 ListGroupFields, [ArgUnknown()], [NOHDR_OPT, SEP_OPT], "[fields...]",
232 "Lists all available fields for node groups"),
234 SetGroupParams, ARGS_ONE_GROUP,
235 [DRY_RUN_OPT, SUBMIT_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT],
236 "<group_name>", "Alters the parameters of a node group"),
238 RemoveGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT],
239 "[--dry-run] <group-name>",
240 "Remove an (empty) node group from the cluster"),
242 RenameGroup, [ArgGroup(min=2, max=2)], [DRY_RUN_OPT],
243 "[--dry-run] <group-name> <new-name>", "Rename a node group"),
245 EvacuateGroup, [ArgGroup(min=1, max=1)],
246 [TO_GROUP_OPT, IALLOCATOR_OPT, EARLY_RELEASE_OPT],
247 "[-I <iallocator>] [--to <group>]",
248 "Evacuate all instances within a group"),
250 ListTags, ARGS_ONE_GROUP, [PRIORITY_OPT],
251 "<instance_name>", "List the tags of the given instance"),
253 AddTags, [ArgGroup(min=1, max=1), ArgUnknown()],
254 [TAG_SRC_OPT, PRIORITY_OPT],
255 "<instance_name> tag...", "Add tags to the given instance"),
257 RemoveTags, [ArgGroup(min=1, max=1), ArgUnknown()],
258 [TAG_SRC_OPT, PRIORITY_OPT],
259 "<instance_name> tag...", "Remove tags from given instance"),
264 return GenericMain(commands,
265 override={"tag_type": constants.TAG_NODEGROUP})