#
#
-# 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
"""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 compat
+from ganeti import constants
from ganeti import opcodes
from ganeti import utils
#: default list of fields for L{ListGroups}
-_LIST_DEF_FIELDS = ["name", "node_cnt", "pinst_cnt", "alloc_policy"]
-
-
-#: headers (and full field list) for L{ListGroups}
-_LIST_HEADERS = {
- "name": "Group", "uuid": "UUID", "alloc_policy": "AllocPolicy",
- "node_cnt": "Nodes", "node_list": "NodeList",
- "pinst_cnt": "Instances", "pinst_list": "InstanceList",
- "ctime": "CTime", "mtime": "MTime", "serial_no": "SerialNo",
-}
+_LIST_DEF_FIELDS = ["name", "node_cnt", "pinst_cnt", "alloc_policy", "ndparams"]
def AddGroup(opts, args):
"""
(group_name,) = args
- op = opcodes.OpAddGroup(group_name=group_name, ndparams=opts.ndparams,
+ op = opcodes.OpGroupAdd(group_name=group_name, ndparams=opts.ndparams,
alloc_policy=opts.alloc_policy)
SubmitOpCode(op, opts=opts)
-def ListGroups(opts, args):
- """List node groups and their properties.
+def AssignNodes(opts, args):
+ """Assign nodes to a group.
@param opts: the command line options selected by the user
@type args: list
- @param args: groups to list, or empty for all
+ @param args: args[0]: group to assign nodes to; args[1:]: nodes to assign
@rtype: int
@return: the desired exit code
"""
- desired_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
+ group_name = args[0]
+ node_names = args[1:]
- output = GetClient().QueryGroups(args, desired_fields, opts.do_locking)
+ op = opcodes.OpGroupAssignNodes(group_name=group_name, nodes=node_names,
+ force=opts.force)
+ SubmitOpCode(op, opts=opts)
- if opts.no_headers:
- headers = None
- else:
- headers = _LIST_HEADERS
- int_type_fields = frozenset(["node_cnt", "pinst_cnt", "serial_no"])
- list_type_fields = frozenset(["node_list", "pinst_list"])
- date_type_fields = frozenset(["mtime", "ctime"])
+def _FmtDict(data):
+ """Format dict data into command-line format.
- for row in output:
- for idx, field in enumerate(desired_fields):
- val = row[idx]
+ @param data: The input dict to be formatted
+ @return: The formatted dict
- if field in list_type_fields:
- val = ",".join(val)
- elif opts.roman_integers and field in int_type_fields:
- val = compat.TryToRoman(val)
- elif field in date_type_fields:
- val = utils.FormatTime(val)
- elif val is None:
- val = "?"
+ """
+ if not data:
+ return "(empty)"
- row[idx] = str(val)
+ return utils.CommaJoin(["%s=%s" % (key, value)
+ for key, value in data.items()])
- data = GenerateTable(separator=opts.separator, headers=headers,
- fields=desired_fields, data=output)
- for line in data:
- ToStdout(line)
+def ListGroups(opts, args):
+ """List node groups and their properties.
- return 0
+ @param opts: the command line options selected by the user
+ @type args: list
+ @param args: groups to list, or empty for all
+ @rtype: int
+ @return: the desired exit code
+
+ """
+ desired_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
+ fmtoverride = {
+ "node_list": (",".join, False),
+ "pinst_list": (",".join, False),
+ "ndparams": (_FmtDict, False),
+ }
+
+ return GenericList(constants.QR_GROUP, desired_fields, args, None,
+ opts.separator, not opts.no_headers,
+ format_override=fmtoverride, verbose=opts.verbose,
+ force_filter=opts.force_filter)
+
+
+def ListGroupFields(opts, args):
+ """List node fields.
+
+ @param opts: the command line options selected by the user
+ @type args: list
+ @param args: fields to list, or empty for all
+ @rtype: int
+ @return: the desired exit code
+
+ """
+ return GenericListFields(constants.QR_GROUP, args, opts.separator,
+ not opts.no_headers)
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
@return: the desired exit code
"""
- all_changes = {
- "ndparams": opts.ndparams,
- "alloc_policy": opts.alloc_policy,
- }
-
- if all_changes.values().count(None) == len(all_changes):
+ if opts.ndparams is None and opts.alloc_policy is None:
ToStderr("Please give at least one of the parameters.")
return 1
- op = opcodes.OpSetGroupParams(group_name=args[0], # pylint: disable-msg=W0142
- **all_changes)
+ op = opcodes.OpGroupSetParams(group_name=args[0],
+ ndparams=opts.ndparams,
+ alloc_policy=opts.alloc_policy)
result = SubmitOrSend(op, opts)
if result:
"""
(group_name,) = args
- op = opcodes.OpRemoveGroup(group_name=group_name)
+ op = opcodes.OpGroupRemove(group_name=group_name)
SubmitOpCode(op, opts=opts)
@return: the desired exit code
"""
- old_name, new_name = args
- op = opcodes.OpRenameGroup(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
+
+
commands = {
"add": (
AddGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT],
"<group_name>", "Add a new node group to the cluster"),
+ "assign-nodes": (
+ AssignNodes, ARGS_ONE_GROUP + ARGS_MANY_NODES, [DRY_RUN_OPT, FORCE_OPT],
+ "<group_name> <node>...", "Assign nodes to a group"),
"list": (
ListGroups, ARGS_MANY_GROUPS,
- [NOHDR_OPT, SEP_OPT, FIELDS_OPT, SYNC_OPT, ROMAN_OPT],
+ [NOHDR_OPT, SEP_OPT, FIELDS_OPT, VERBOSE_OPT, FORCE_FILTER_OPT],
"[<group_name>...]",
- "Lists the node groups in the cluster. The available fields are (see"
- " the man page for details): %s. The default list is (in order): %s." %
- (utils.CommaJoin(_LIST_HEADERS), utils.CommaJoin(_LIST_DEF_FIELDS))),
+ "Lists the node groups in the cluster. The available fields can be shown"
+ " using the \"list-fields\" command (see the man page for details)."
+ " The default list is (in order): %s." % utils.CommaJoin(_LIST_DEF_FIELDS)),
+ "list-fields": (
+ ListGroupFields, [ArgUnknown()], [NOHDR_OPT, SEP_OPT], "[fields...]",
+ "Lists all available fields for node groups"),
"modify": (
SetGroupParams, ARGS_ONE_GROUP,
[DRY_RUN_OPT, SUBMIT_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT],
"<group_name>", "Alters the parameters of a node group"),
"remove": (
RemoveGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT],
- "[--dry-run] <group_name>",
+ "[--dry-run] <group-name>",
"Remove an (empty) node group from the cluster"),
"rename": (
RenameGroup, [ArgGroup(min=2, max=2)], [DRY_RUN_OPT],
- "[--dry-run] <old_name> <new_name>", "Rename a node group"),
-}
+ "[--dry-run] <group-name> <new-name>", "Rename a node group"),
+ "evacuate": (
+ EvacuateGroup, [ArgGroup(min=1, max=1)],
+ [TO_GROUP_OPT, IALLOCATOR_OPT, EARLY_RELEASE_OPT],
+ "[-I <iallocator>] [--to <group>]",
+ "Evacuate all instances within a group"),
+ "list-tags": (
+ ListTags, ARGS_ONE_GROUP, [PRIORITY_OPT],
+ "<instance_name>", "List the tags of the given instance"),
+ "add-tags": (
+ AddTags, [ArgGroup(min=1, max=1), ArgUnknown()],
+ [TAG_SRC_OPT, PRIORITY_OPT],
+ "<instance_name> tag...", "Add tags to the given instance"),
+ "remove-tags": (
+ RemoveTags, [ArgGroup(min=1, max=1), ArgUnknown()],
+ [TAG_SRC_OPT, PRIORITY_OPT],
+ "<instance_name> tag...", "Remove tags from given instance"),
+ }
def Main():
- return GenericMain(commands)
+ return GenericMain(commands,
+ override={"tag_type": constants.TAG_NODEGROUP})