4 # Copyright (C) 2010 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"]
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.OpAddGroup(group_name=group_name, ndparams=opts.ndparams,
49 alloc_policy=opts.alloc_policy)
50 SubmitOpCode(op, opts=opts)
53 def ListGroups(opts, args):
54 """List node groups and their properties.
56 @param opts: the command line options selected by the user
58 @param args: groups to list, or empty for all
60 @return: the desired exit code
63 desired_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
64 fmtoverride = dict.fromkeys(["node_list", "pinst_list"], (",".join, False))
66 return GenericList(constants.QR_GROUP, desired_fields, args, None,
67 opts.separator, not opts.no_headers,
68 format_override=fmtoverride)
71 def ListGroupFields(opts, args):
74 @param opts: the command line options selected by the user
76 @param args: fields to list, or empty for all
78 @return: the desired exit code
81 return GenericListFields(constants.QR_GROUP, args, opts.separator,
85 def SetGroupParams(opts, args):
86 """Modifies a node group's parameters.
88 @param opts: the command line options seletect by the user
90 @param args: should contain only one element, the node group name
93 @return: the desired exit code
97 "ndparams": opts.ndparams,
98 "alloc_policy": opts.alloc_policy,
101 if all_changes.values().count(None) == len(all_changes):
102 ToStderr("Please give at least one of the parameters.")
105 op = opcodes.OpSetGroupParams(group_name=args[0], # pylint: disable-msg=W0142
107 result = SubmitOrSend(op, opts)
110 ToStdout("Modified node group %s", args[0])
111 for param, data in result:
112 ToStdout(" - %-5s -> %s", param, data)
117 def RemoveGroup(opts, args):
118 """Remove a node group from the cluster.
120 @param opts: the command line options selected by the user
122 @param args: a list of length 1 with the name of the group to remove
124 @return: the desired exit code
128 op = opcodes.OpRemoveGroup(group_name=group_name)
129 SubmitOpCode(op, opts=opts)
132 def RenameGroup(opts, args):
133 """Rename a node group.
135 @param opts: the command line options selected by the user
137 @param args: a list of length 2, [old_name, new_name]
139 @return: the desired exit code
142 old_name, new_name = args
143 op = opcodes.OpRenameGroup(old_name=old_name, new_name=new_name)
144 SubmitOpCode(op, opts=opts)
149 AddGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT],
150 "<group_name>", "Add a new node group to the cluster"),
152 ListGroups, ARGS_MANY_GROUPS,
153 [NOHDR_OPT, SEP_OPT, FIELDS_OPT],
155 "Lists the node groups in the cluster. The available fields can be shown"
156 " using the \"list-fields\" command (see the man page for details)."
157 " The default list is (in order): %s." % utils.CommaJoin(_LIST_DEF_FIELDS)),
159 ListGroupFields, [ArgUnknown()], [NOHDR_OPT, SEP_OPT], "[fields...]",
160 "Lists all available fields for node groups"),
162 SetGroupParams, ARGS_ONE_GROUP,
163 [DRY_RUN_OPT, SUBMIT_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT],
164 "<group_name>", "Alters the parameters of a node group"),
166 RemoveGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT],
167 "[--dry-run] <group_name>",
168 "Remove an (empty) node group from the cluster"),
170 RenameGroup, [ArgGroup(min=2, max=2)], [DRY_RUN_OPT],
171 "[--dry-run] <old_name> <new_name>", "Rename a node group"),
176 return GenericMain(commands)