Package ganeti :: Package client :: Module gnt_group
[hide private]
[frames] | no frames]

Source Code for Module ganeti.client.gnt_group

  1  # 
  2  # 
  3   
  4  # Copyright (C) 2010, 2011, 2012, 2013 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 cStringIO import StringIO 
 28   
 29  from ganeti.cli import * 
 30  from ganeti import constants 
 31  from ganeti import opcodes 
 32  from ganeti import utils 
 33  from ganeti import compat 
 34   
 35   
 36  #: default list of fields for L{ListGroups} 
 37  _LIST_DEF_FIELDS = ["name", "node_cnt", "pinst_cnt", "alloc_policy", "ndparams"] 
 38   
 39  _ENV_OVERRIDE = compat.UniqueFrozenset(["list"]) 
 40   
 41   
42 -def AddGroup(opts, args):
43 """Add a node group to the cluster. 44 45 @param opts: the command line options selected by the user 46 @type args: list 47 @param args: a list of length 1 with the name of the group to create 48 @rtype: int 49 @return: the desired exit code 50 51 """ 52 ipolicy = CreateIPolicyFromOpts( 53 minmax_ispecs=opts.ipolicy_bounds_specs, 54 ipolicy_vcpu_ratio=opts.ipolicy_vcpu_ratio, 55 ipolicy_spindle_ratio=opts.ipolicy_spindle_ratio, 56 group_ipolicy=True) 57 58 (group_name,) = args 59 diskparams = dict(opts.diskparams) 60 61 if opts.disk_state: 62 disk_state = utils.FlatToDict(opts.disk_state) 63 else: 64 disk_state = {} 65 hv_state = dict(opts.hv_state) 66 67 op = opcodes.OpGroupAdd(group_name=group_name, ndparams=opts.ndparams, 68 alloc_policy=opts.alloc_policy, 69 diskparams=diskparams, ipolicy=ipolicy, 70 hv_state=hv_state, 71 disk_state=disk_state) 72 SubmitOrSend(op, opts)
73 74
75 -def AssignNodes(opts, args):
76 """Assign nodes to a group. 77 78 @param opts: the command line options selected by the user 79 @type args: list 80 @param args: args[0]: group to assign nodes to; args[1:]: nodes to assign 81 @rtype: int 82 @return: the desired exit code 83 84 """ 85 group_name = args[0] 86 node_names = args[1:] 87 88 op = opcodes.OpGroupAssignNodes(group_name=group_name, nodes=node_names, 89 force=opts.force) 90 SubmitOrSend(op, opts)
91 92
93 -def _FmtDict(data):
94 """Format dict data into command-line format. 95 96 @param data: The input dict to be formatted 97 @return: The formatted dict 98 99 """ 100 if not data: 101 return "(empty)" 102 103 return utils.CommaJoin(["%s=%s" % (key, value) 104 for key, value in data.items()])
105 106
107 -def ListGroups(opts, args):
108 """List node groups and their properties. 109 110 @param opts: the command line options selected by the user 111 @type args: list 112 @param args: groups to list, or empty for all 113 @rtype: int 114 @return: the desired exit code 115 116 """ 117 desired_fields = ParseFields(opts.output, _LIST_DEF_FIELDS) 118 fmtoverride = { 119 "node_list": (",".join, False), 120 "pinst_list": (",".join, False), 121 "ndparams": (_FmtDict, False), 122 } 123 124 cl = GetClient(query=True) 125 126 return GenericList(constants.QR_GROUP, desired_fields, args, None, 127 opts.separator, not opts.no_headers, 128 format_override=fmtoverride, verbose=opts.verbose, 129 force_filter=opts.force_filter, cl=cl)
130 131
132 -def ListGroupFields(opts, args):
133 """List node fields. 134 135 @param opts: the command line options selected by the user 136 @type args: list 137 @param args: fields to list, or empty for all 138 @rtype: int 139 @return: the desired exit code 140 141 """ 142 cl = GetClient(query=True) 143 144 return GenericListFields(constants.QR_GROUP, args, opts.separator, 145 not opts.no_headers, cl=cl)
146 147
148 -def SetGroupParams(opts, args):
149 """Modifies a node group's parameters. 150 151 @param opts: the command line options selected by the user 152 @type args: list 153 @param args: should contain only one element, the node group name 154 155 @rtype: int 156 @return: the desired exit code 157 158 """ 159 allmods = [opts.ndparams, opts.alloc_policy, opts.diskparams, opts.hv_state, 160 opts.disk_state, opts.ipolicy_bounds_specs, 161 opts.ipolicy_vcpu_ratio, opts.ipolicy_spindle_ratio, 162 opts.diskparams] 163 if allmods.count(None) == len(allmods): 164 ToStderr("Please give at least one of the parameters.") 165 return 1 166 167 if opts.disk_state: 168 disk_state = utils.FlatToDict(opts.disk_state) 169 else: 170 disk_state = {} 171 172 hv_state = dict(opts.hv_state) 173 174 diskparams = dict(opts.diskparams) 175 176 # create ipolicy object 177 ipolicy = CreateIPolicyFromOpts( 178 minmax_ispecs=opts.ipolicy_bounds_specs, 179 ipolicy_disk_templates=opts.ipolicy_disk_templates, 180 ipolicy_vcpu_ratio=opts.ipolicy_vcpu_ratio, 181 ipolicy_spindle_ratio=opts.ipolicy_spindle_ratio, 182 group_ipolicy=True, 183 allowed_values=[constants.VALUE_DEFAULT]) 184 185 op = opcodes.OpGroupSetParams(group_name=args[0], 186 ndparams=opts.ndparams, 187 alloc_policy=opts.alloc_policy, 188 hv_state=hv_state, 189 disk_state=disk_state, 190 diskparams=diskparams, 191 ipolicy=ipolicy) 192 193 result = SubmitOrSend(op, opts) 194 195 if result: 196 ToStdout("Modified node group %s", args[0]) 197 for param, data in result: 198 ToStdout(" - %-5s -> %s", param, data) 199 200 return 0
201 202
203 -def RemoveGroup(opts, args):
204 """Remove a node group from the cluster. 205 206 @param opts: the command line options selected by the user 207 @type args: list 208 @param args: a list of length 1 with the name of the group to remove 209 @rtype: int 210 @return: the desired exit code 211 212 """ 213 (group_name,) = args 214 op = opcodes.OpGroupRemove(group_name=group_name) 215 SubmitOrSend(op, opts)
216 217
218 -def RenameGroup(opts, args):
219 """Rename a node group. 220 221 @param opts: the command line options selected by the user 222 @type args: list 223 @param args: a list of length 2, [old_name, new_name] 224 @rtype: int 225 @return: the desired exit code 226 227 """ 228 group_name, new_name = args 229 op = opcodes.OpGroupRename(group_name=group_name, new_name=new_name) 230 SubmitOrSend(op, opts)
231 232
233 -def EvacuateGroup(opts, args):
234 """Evacuate a node group. 235 236 """ 237 (group_name, ) = args 238 239 cl = GetClient() 240 241 op = opcodes.OpGroupEvacuate(group_name=group_name, 242 iallocator=opts.iallocator, 243 target_groups=opts.to, 244 early_release=opts.early_release) 245 result = SubmitOrSend(op, opts, cl=cl) 246 247 # Keep track of submitted jobs 248 jex = JobExecutor(cl=cl, opts=opts) 249 250 for (status, job_id) in result[constants.JOB_IDS_KEY]: 251 jex.AddJobId(None, status, job_id) 252 253 results = jex.GetResults() 254 bad_cnt = len([row for row in results if not row[0]]) 255 if bad_cnt == 0: 256 ToStdout("All instances evacuated successfully.") 257 rcode = constants.EXIT_SUCCESS 258 else: 259 ToStdout("There were %s errors during the evacuation.", bad_cnt) 260 rcode = constants.EXIT_FAILURE 261 262 return rcode
263 264
265 -def _FormatGroupInfo(group):
266 (name, ndparams, custom_ndparams, diskparams, custom_diskparams, 267 ipolicy, custom_ipolicy) = group 268 return [ 269 ("Node group", name), 270 ("Node parameters", FormatParamsDictInfo(custom_ndparams, ndparams)), 271 ("Disk parameters", FormatParamsDictInfo(custom_diskparams, diskparams)), 272 ("Instance policy", FormatPolicyInfo(custom_ipolicy, ipolicy, False)), 273 ]
274 275
276 -def GroupInfo(_, args):
277 """Shows info about node group. 278 279 """ 280 cl = GetClient(query=True) 281 selected_fields = ["name", 282 "ndparams", "custom_ndparams", 283 "diskparams", "custom_diskparams", 284 "ipolicy", "custom_ipolicy"] 285 result = cl.QueryGroups(names=args, fields=selected_fields, 286 use_locking=False) 287 288 PrintGenericInfo([ 289 _FormatGroupInfo(group) for group in result 290 ])
291 292
293 -def _GetCreateCommand(group):
294 (name, ipolicy) = group 295 buf = StringIO() 296 buf.write("gnt-group add") 297 PrintIPolicyCommand(buf, ipolicy, True) 298 buf.write(" ") 299 buf.write(name) 300 return buf.getvalue()
301 302
303 -def ShowCreateCommand(opts, args):
304 """Shows the command that can be used to re-create a node group. 305 306 Currently it works only for ipolicy specs. 307 308 """ 309 cl = GetClient(query=True) 310 selected_fields = ["name"] 311 if opts.include_defaults: 312 selected_fields += ["ipolicy"] 313 else: 314 selected_fields += ["custom_ipolicy"] 315 result = cl.QueryGroups(names=args, fields=selected_fields, 316 use_locking=False) 317 318 for group in result: 319 ToStdout(_GetCreateCommand(group))
320 321 322 commands = { 323 "add": ( 324 AddGroup, ARGS_ONE_GROUP, 325 [DRY_RUN_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT, DISK_PARAMS_OPT, 326 HV_STATE_OPT, DISK_STATE_OPT, PRIORITY_OPT, 327 SUBMIT_OPT] + INSTANCE_POLICY_OPTS, 328 "<group_name>", "Add a new node group to the cluster"), 329 "assign-nodes": ( 330 AssignNodes, ARGS_ONE_GROUP + ARGS_MANY_NODES, 331 [DRY_RUN_OPT, FORCE_OPT, PRIORITY_OPT, SUBMIT_OPT], 332 "<group_name> <node>...", "Assign nodes to a group"), 333 "list": ( 334 ListGroups, ARGS_MANY_GROUPS, 335 [NOHDR_OPT, SEP_OPT, FIELDS_OPT, VERBOSE_OPT, FORCE_FILTER_OPT], 336 "[<group_name>...]", 337 "Lists the node groups in the cluster. The available fields can be shown" 338 " using the \"list-fields\" command (see the man page for details)." 339 " The default list is (in order): %s." % utils.CommaJoin(_LIST_DEF_FIELDS)), 340 "list-fields": ( 341 ListGroupFields, [ArgUnknown()], [NOHDR_OPT, SEP_OPT], "[fields...]", 342 "Lists all available fields for node groups"), 343 "modify": ( 344 SetGroupParams, ARGS_ONE_GROUP, 345 [DRY_RUN_OPT, SUBMIT_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT, HV_STATE_OPT, 346 DISK_STATE_OPT, DISK_PARAMS_OPT, PRIORITY_OPT] + INSTANCE_POLICY_OPTS, 347 "<group_name>", "Alters the parameters of a node group"), 348 "remove": ( 349 RemoveGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT, PRIORITY_OPT, SUBMIT_OPT], 350 "[--dry-run] <group-name>", 351 "Remove an (empty) node group from the cluster"), 352 "rename": ( 353 RenameGroup, [ArgGroup(min=2, max=2)], 354 [DRY_RUN_OPT, SUBMIT_OPT, PRIORITY_OPT], 355 "[--dry-run] <group-name> <new-name>", "Rename a node group"), 356 "evacuate": ( 357 EvacuateGroup, [ArgGroup(min=1, max=1)], 358 [TO_GROUP_OPT, IALLOCATOR_OPT, EARLY_RELEASE_OPT, SUBMIT_OPT, PRIORITY_OPT], 359 "[-I <iallocator>] [--to <group>]", 360 "Evacuate all instances within a group"), 361 "list-tags": ( 362 ListTags, ARGS_ONE_GROUP, [], 363 "<group_name>", "List the tags of the given group"), 364 "add-tags": ( 365 AddTags, [ArgGroup(min=1, max=1), ArgUnknown()], 366 [TAG_SRC_OPT, PRIORITY_OPT, SUBMIT_OPT], 367 "<group_name> tag...", "Add tags to the given group"), 368 "remove-tags": ( 369 RemoveTags, [ArgGroup(min=1, max=1), ArgUnknown()], 370 [TAG_SRC_OPT, PRIORITY_OPT, SUBMIT_OPT], 371 "<group_name> tag...", "Remove tags from the given group"), 372 "info": ( 373 GroupInfo, ARGS_MANY_GROUPS, [], "[<group_name>...]", 374 "Show group information"), 375 "show-ispecs-cmd": ( 376 ShowCreateCommand, ARGS_MANY_GROUPS, [INCLUDEDEFAULTS_OPT], 377 "[--include-defaults] [<group_name>...]", 378 "Show the command line to re-create a group"), 379 } 380 381
382 -def Main():
383 return GenericMain(commands, 384 override={"tag_type": constants.TAG_NODEGROUP}, 385 env_override=_ENV_OVERRIDE)
386