X-Git-Url: https://code.grnet.gr/git/ganeti-local/blobdiff_plain/a8173e827b17d6cdd699d99a7b5053524d84175a..9fa567b3b6b73d3728a29b0464328f93f016d991:/lib/client/gnt_group.py diff --git a/lib/client/gnt_group.py b/lib/client/gnt_group.py index 032e282..8cde11e 100644 --- a/lib/client/gnt_group.py +++ b/lib/client/gnt_group.py @@ -1,7 +1,7 @@ # # -# Copyright (C) 2010 Google Inc. +# Copyright (C) 2010, 2011 Google Inc. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -20,12 +20,13 @@ """Node group related commands""" -# pylint: disable-msg=W0401,W0614 +# pylint: disable=W0401,W0614 # W0401: Wildcard import ganeti.cli # W0614: Unused import %s from wildcard import (since we need cli) from ganeti.cli import * from ganeti import constants +from ganeti import objects from ganeti import opcodes from ganeti import utils @@ -34,6 +35,9 @@ from ganeti import utils _LIST_DEF_FIELDS = ["name", "node_cnt", "pinst_cnt", "alloc_policy", "ndparams"] +_ENV_OVERRIDE = frozenset(["list"]) + + def AddGroup(opts, args): """Add a node group to the cluster. @@ -44,9 +48,30 @@ def AddGroup(opts, args): @return: the desired exit code """ + ipolicy = \ + objects.CreateIPolicyFromOpts(ispecs_mem_size=opts.ispecs_mem_size, + ispecs_cpu_count=opts.ispecs_cpu_count, + ispecs_disk_count=opts.ispecs_disk_count, + ispecs_disk_size=opts.ispecs_disk_size, + ispecs_nic_count=opts.ispecs_nic_count, + group_ipolicy=True) + for key in ipolicy.keys(): + utils.ForceDictType(ipolicy[key], constants.ISPECS_PARAMETER_TYPES) + (group_name,) = args + diskparams = dict(opts.diskparams) + + if opts.disk_state: + disk_state = utils.FlatToDict(opts.disk_state) + else: + disk_state = {} + hv_state = dict(opts.hv_state) + op = opcodes.OpGroupAdd(group_name=group_name, ndparams=opts.ndparams, - alloc_policy=opts.alloc_policy) + alloc_policy=opts.alloc_policy, + diskparams=diskparams, ipolicy=ipolicy, + hv_state=hv_state, + disk_state=disk_state) SubmitOpCode(op, opts=opts) @@ -101,7 +126,8 @@ def ListGroups(opts, args): return GenericList(constants.QR_GROUP, desired_fields, args, None, opts.separator, not opts.no_headers, - format_override=fmtoverride) + format_override=fmtoverride, verbose=opts.verbose, + force_filter=opts.force_filter) def ListGroupFields(opts, args): @@ -121,7 +147,7 @@ def ListGroupFields(opts, args): def SetGroupParams(opts, args): """Modifies a node group's parameters. - @param opts: the command line options seletect by the user + @param opts: the command line options selected by the user @type args: list @param args: should contain only one element, the node group name @@ -129,17 +155,57 @@ def SetGroupParams(opts, args): @return: the desired exit code """ - all_changes = { - "ndparams": opts.ndparams, - "alloc_policy": opts.alloc_policy, - } - - if all_changes.values().count(None) == len(all_changes): + allmods = [opts.ndparams, opts.alloc_policy, opts.diskparams, opts.hv_state, + opts.disk_state, opts.ispecs_mem_size, opts.ispecs_cpu_count, + opts.ispecs_disk_count, opts.ispecs_disk_size, + opts.ispecs_nic_count, opts.diskparams] + if allmods.count(None) == len(allmods): ToStderr("Please give at least one of the parameters.") return 1 - op = opcodes.OpSetGroupParams(group_name=args[0], # pylint: disable-msg=W0142 - **all_changes) + if opts.disk_state: + disk_state = utils.FlatToDict(opts.disk_state) + else: + disk_state = {} + + hv_state = dict(opts.hv_state) + + diskparams = dict(opts.diskparams) + + # set the default values + to_ipolicy = [ + opts.ispecs_mem_size, + opts.ispecs_cpu_count, + opts.ispecs_disk_count, + opts.ispecs_disk_size, + opts.ispecs_nic_count, + ] + for ispec in to_ipolicy: + for param in ispec: + if isinstance(ispec[param], basestring): + if ispec[param].lower() == "default": + ispec[param] = constants.VALUE_DEFAULT + # create ipolicy object + ipolicy = objects.CreateIPolicyFromOpts(\ + ispecs_mem_size=opts.ispecs_mem_size, + ispecs_cpu_count=opts.ispecs_cpu_count, + ispecs_disk_count=opts.ispecs_disk_count, + ispecs_disk_size=opts.ispecs_disk_size, + ispecs_nic_count=opts.ispecs_nic_count, + group_ipolicy=True, + allowed_values=[constants.VALUE_DEFAULT]) + for key in ipolicy.keys(): + utils.ForceDictType(ipolicy[key], constants.ISPECS_PARAMETER_TYPES, + allowed_values=[constants.VALUE_DEFAULT]) + + op = opcodes.OpGroupSetParams(group_name=args[0], + ndparams=opts.ndparams, + alloc_policy=opts.alloc_policy, + hv_state=hv_state, + disk_state=disk_state, + diskparams=diskparams, + ipolicy=ipolicy) + result = SubmitOrSend(op, opts) if result: @@ -175,21 +241,62 @@ def RenameGroup(opts, args): @return: the desired exit code """ - old_name, new_name = args - op = opcodes.OpGroupRename(old_name=old_name, new_name=new_name) + group_name, new_name = args + op = opcodes.OpGroupRename(group_name=group_name, new_name=new_name) SubmitOpCode(op, opts=opts) +def EvacuateGroup(opts, args): + """Evacuate a node group. + + """ + (group_name, ) = args + + cl = GetClient() + + op = opcodes.OpGroupEvacuate(group_name=group_name, + iallocator=opts.iallocator, + target_groups=opts.to, + early_release=opts.early_release) + result = SubmitOpCode(op, cl=cl, opts=opts) + + # Keep track of submitted jobs + jex = JobExecutor(cl=cl, opts=opts) + + for (status, job_id) in result[constants.JOB_IDS_KEY]: + jex.AddJobId(None, status, job_id) + + results = jex.GetResults() + bad_cnt = len([row for row in results if not row[0]]) + if bad_cnt == 0: + ToStdout("All instances evacuated successfully.") + rcode = constants.EXIT_SUCCESS + else: + ToStdout("There were %s errors during the evacuation.", bad_cnt) + rcode = constants.EXIT_FAILURE + + return rcode + +INSTANCE_POLICY_OPTS = [ + SPECS_CPU_COUNT_OPT, + SPECS_DISK_COUNT_OPT, + SPECS_DISK_SIZE_OPT, + SPECS_MEM_SIZE_OPT, + SPECS_NIC_COUNT_OPT, + ] + commands = { "add": ( - AddGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT], + AddGroup, ARGS_ONE_GROUP, + [DRY_RUN_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT, DISK_PARAMS_OPT, + HV_STATE_OPT, DISK_STATE_OPT] + INSTANCE_POLICY_OPTS, "", "Add a new node group to the cluster"), "assign-nodes": ( AssignNodes, ARGS_ONE_GROUP + ARGS_MANY_NODES, [DRY_RUN_OPT, FORCE_OPT], " ...", "Assign nodes to a group"), "list": ( ListGroups, ARGS_MANY_GROUPS, - [NOHDR_OPT, SEP_OPT, FIELDS_OPT], + [NOHDR_OPT, SEP_OPT, FIELDS_OPT, VERBOSE_OPT, FORCE_FILTER_OPT], "[...]", "Lists the node groups in the cluster. The available fields can be shown" " using the \"list-fields\" command (see the man page for details)." @@ -199,17 +306,36 @@ commands = { "Lists all available fields for node groups"), "modify": ( SetGroupParams, ARGS_ONE_GROUP, - [DRY_RUN_OPT, SUBMIT_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT], + [DRY_RUN_OPT, SUBMIT_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT, HV_STATE_OPT, + DISK_STATE_OPT, DISK_PARAMS_OPT] + INSTANCE_POLICY_OPTS, "", "Alters the parameters of a node group"), "remove": ( RemoveGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT], - "[--dry-run] ", + "[--dry-run] ", "Remove an (empty) node group from the cluster"), "rename": ( RenameGroup, [ArgGroup(min=2, max=2)], [DRY_RUN_OPT], - "[--dry-run] ", "Rename a node group"), -} + "[--dry-run] ", "Rename a node group"), + "evacuate": ( + EvacuateGroup, [ArgGroup(min=1, max=1)], + [TO_GROUP_OPT, IALLOCATOR_OPT, EARLY_RELEASE_OPT], + "[-I ] [--to ]", + "Evacuate all instances within a group"), + "list-tags": ( + ListTags, ARGS_ONE_GROUP, [PRIORITY_OPT], + "", "List the tags of the given instance"), + "add-tags": ( + AddTags, [ArgGroup(min=1, max=1), ArgUnknown()], + [TAG_SRC_OPT, PRIORITY_OPT], + " tag...", "Add tags to the given instance"), + "remove-tags": ( + RemoveTags, [ArgGroup(min=1, max=1), ArgUnknown()], + [TAG_SRC_OPT, PRIORITY_OPT], + " tag...", "Remove tags from given instance"), + } def Main(): - return GenericMain(commands) + return GenericMain(commands, + override={"tag_type": constants.TAG_NODEGROUP}, + env_override=_ENV_OVERRIDE)