Add `gnt-group assign-nodes` in the CLI
[ganeti-local] / lib / client / gnt_group.py
1 #
2 #
3
4 # Copyright (C) 2010 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-msg=W0401,W0614
24 # W0401: Wildcard import ganeti.cli
25 # W0614: Unused import %s from wildcard import (since we need cli)
26
27 from ganeti.cli import *
28 from ganeti import constants
29 from ganeti import opcodes
30 from ganeti import utils
31
32
33 #: default list of fields for L{ListGroups}
34 _LIST_DEF_FIELDS = ["name", "node_cnt", "pinst_cnt", "alloc_policy"]
35
36
37 def AddGroup(opts, args):
38   """Add a node group to the cluster.
39
40   @param opts: the command line options selected by the user
41   @type args: list
42   @param args: a list of length 1 with the name of the group to create
43   @rtype: int
44   @return: the desired exit code
45
46   """
47   (group_name,) = args
48   op = opcodes.OpAddGroup(group_name=group_name, ndparams=opts.ndparams,
49                           alloc_policy=opts.alloc_policy)
50   SubmitOpCode(op, opts=opts)
51
52
53 def AssignNodes(opts, args):
54   """Assign nodes to a group.
55
56   @param opts: the command line options selected by the user
57   @type args: list
58   @param args: args[0]: group to assign nodes to; args[1:]: nodes to assign
59   @rtype: int
60   @return: the desired exit code
61
62   """
63   group_name = args[0]
64   node_names = args[1:]
65
66   op = opcodes.OpAssignGroupNodes(group_name=group_name, nodes=node_names,
67                                   force=opts.force)
68   SubmitOpCode(op, opts=opts)
69
70
71 def ListGroups(opts, args):
72   """List node groups and their properties.
73
74   @param opts: the command line options selected by the user
75   @type args: list
76   @param args: groups to list, or empty for all
77   @rtype: int
78   @return: the desired exit code
79
80   """
81   desired_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
82   fmtoverride = dict.fromkeys(["node_list", "pinst_list"], (",".join, False))
83
84   return GenericList(constants.QR_GROUP, desired_fields, args, None,
85                      opts.separator, not opts.no_headers,
86                      format_override=fmtoverride)
87
88
89 def ListGroupFields(opts, args):
90   """List node fields.
91
92   @param opts: the command line options selected by the user
93   @type args: list
94   @param args: fields to list, or empty for all
95   @rtype: int
96   @return: the desired exit code
97
98   """
99   return GenericListFields(constants.QR_GROUP, args, opts.separator,
100                            not opts.no_headers)
101
102
103 def SetGroupParams(opts, args):
104   """Modifies a node group's parameters.
105
106   @param opts: the command line options seletect by the user
107   @type args: list
108   @param args: should contain only one element, the node group name
109
110   @rtype: int
111   @return: the desired exit code
112
113   """
114   all_changes = {
115     "ndparams": opts.ndparams,
116     "alloc_policy": opts.alloc_policy,
117   }
118
119   if all_changes.values().count(None) == len(all_changes):
120     ToStderr("Please give at least one of the parameters.")
121     return 1
122
123   op = opcodes.OpSetGroupParams(group_name=args[0], # pylint: disable-msg=W0142
124                                 **all_changes)
125   result = SubmitOrSend(op, opts)
126
127   if result:
128     ToStdout("Modified node group %s", args[0])
129     for param, data in result:
130       ToStdout(" - %-5s -> %s", param, data)
131
132   return 0
133
134
135 def RemoveGroup(opts, args):
136   """Remove a node group from the cluster.
137
138   @param opts: the command line options selected by the user
139   @type args: list
140   @param args: a list of length 1 with the name of the group to remove
141   @rtype: int
142   @return: the desired exit code
143
144   """
145   (group_name,) = args
146   op = opcodes.OpRemoveGroup(group_name=group_name)
147   SubmitOpCode(op, opts=opts)
148
149
150 def RenameGroup(opts, args):
151   """Rename a node group.
152
153   @param opts: the command line options selected by the user
154   @type args: list
155   @param args: a list of length 2, [old_name, new_name]
156   @rtype: int
157   @return: the desired exit code
158
159   """
160   old_name, new_name = args
161   op = opcodes.OpRenameGroup(old_name=old_name, new_name=new_name)
162   SubmitOpCode(op, opts=opts)
163
164
165 commands = {
166   "add": (
167     AddGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT],
168     "<group_name>", "Add a new node group to the cluster"),
169   "assign-nodes": (
170     AssignNodes, ARGS_ONE_GROUP + ARGS_MANY_NODES, [DRY_RUN_OPT, FORCE_OPT],
171     "<group_name> <node>...", "Assign nodes to a group"),
172   "list": (
173     ListGroups, ARGS_MANY_GROUPS,
174     [NOHDR_OPT, SEP_OPT, FIELDS_OPT],
175     "[<group_name>...]",
176     "Lists the node groups in the cluster. The available fields can be shown"
177     " using the \"list-fields\" command (see the man page for details)."
178     " The default list is (in order): %s." % utils.CommaJoin(_LIST_DEF_FIELDS)),
179   "list-fields": (
180     ListGroupFields, [ArgUnknown()], [NOHDR_OPT, SEP_OPT], "[fields...]",
181     "Lists all available fields for node groups"),
182   "modify": (
183     SetGroupParams, ARGS_ONE_GROUP,
184     [DRY_RUN_OPT, SUBMIT_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT],
185     "<group_name>", "Alters the parameters of a node group"),
186   "remove": (
187     RemoveGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT],
188     "[--dry-run] <group_name>",
189     "Remove an (empty) node group from the cluster"),
190   "rename": (
191     RenameGroup, [ArgGroup(min=2, max=2)], [DRY_RUN_OPT],
192     "[--dry-run] <old_name> <new_name>", "Rename a node group"),
193 }
194
195
196 def Main():
197   return GenericMain(commands)