gnt-node modify: Adding --node-powered=yes|no
[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 compat
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 #: headers (and full field list) for L{ListGroups}
38 _LIST_HEADERS = {
39   "name": "Group", "uuid": "UUID", "alloc_policy": "AllocPolicy",
40   "node_cnt": "Nodes", "node_list": "NodeList",
41   "pinst_cnt": "Instances", "pinst_list": "InstanceList",
42   "ctime": "CTime", "mtime": "MTime", "serial_no": "SerialNo",
43 }
44
45
46 def AddGroup(opts, args):
47   """Add a node group to the cluster.
48
49   @param opts: the command line options selected by the user
50   @type args: list
51   @param args: a list of length 1 with the name of the group to create
52   @rtype: int
53   @return: the desired exit code
54
55   """
56   (group_name,) = args
57   op = opcodes.OpAddGroup(group_name=group_name, ndparams=opts.ndparams,
58                           alloc_policy=opts.alloc_policy)
59   SubmitOpCode(op, opts=opts)
60
61
62 def ListGroups(opts, args):
63   """List node groups and their properties.
64
65   @param opts: the command line options selected by the user
66   @type args: list
67   @param args: groups to list, or empty for all
68   @rtype: int
69   @return: the desired exit code
70
71   """
72   desired_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
73
74   output = GetClient().QueryGroups(args, desired_fields, opts.do_locking)
75
76   if opts.no_headers:
77     headers = None
78   else:
79     headers = _LIST_HEADERS
80
81   int_type_fields = frozenset(["node_cnt", "pinst_cnt", "serial_no"])
82   list_type_fields = frozenset(["node_list", "pinst_list"])
83   date_type_fields = frozenset(["mtime", "ctime"])
84
85   for row in output:
86     for idx, field in enumerate(desired_fields):
87       val = row[idx]
88
89       if field in list_type_fields:
90         val = ",".join(val)
91       elif opts.roman_integers and field in int_type_fields:
92         val = compat.TryToRoman(val)
93       elif field in date_type_fields:
94         val = utils.FormatTime(val)
95       elif val is None:
96         val = "?"
97
98       row[idx] = str(val)
99
100   data = GenerateTable(separator=opts.separator, headers=headers,
101                        fields=desired_fields, data=output)
102
103   for line in data:
104     ToStdout(line)
105
106   return 0
107
108
109 def SetGroupParams(opts, args):
110   """Modifies a node group's parameters.
111
112   @param opts: the command line options seletect by the user
113   @type args: list
114   @param args: should contain only one element, the node group name
115
116   @rtype: int
117   @return: the desired exit code
118
119   """
120   all_changes = {
121     "ndparams": opts.ndparams,
122     "alloc_policy": opts.alloc_policy,
123   }
124
125   if all_changes.values().count(None) == len(all_changes):
126     ToStderr("Please give at least one of the parameters.")
127     return 1
128
129   op = opcodes.OpSetGroupParams(group_name=args[0], # pylint: disable-msg=W0142
130                                 **all_changes)
131   result = SubmitOrSend(op, opts)
132
133   if result:
134     ToStdout("Modified node group %s", args[0])
135     for param, data in result:
136       ToStdout(" - %-5s -> %s", param, data)
137
138   return 0
139
140
141 def RemoveGroup(opts, args):
142   """Remove a node group from the cluster.
143
144   @param opts: the command line options selected by the user
145   @type args: list
146   @param args: a list of length 1 with the name of the group to remove
147   @rtype: int
148   @return: the desired exit code
149
150   """
151   (group_name,) = args
152   op = opcodes.OpRemoveGroup(group_name=group_name)
153   SubmitOpCode(op, opts=opts)
154
155
156 def RenameGroup(opts, args):
157   """Rename a node group.
158
159   @param opts: the command line options selected by the user
160   @type args: list
161   @param args: a list of length 2, [old_name, new_name]
162   @rtype: int
163   @return: the desired exit code
164
165   """
166   old_name, new_name = args
167   op = opcodes.OpRenameGroup(old_name=old_name, new_name=new_name)
168   SubmitOpCode(op, opts=opts)
169
170
171 commands = {
172   "add": (
173     AddGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT],
174     "<group_name>", "Add a new node group to the cluster"),
175   "list": (
176     ListGroups, ARGS_MANY_GROUPS,
177     [NOHDR_OPT, SEP_OPT, FIELDS_OPT, SYNC_OPT, ROMAN_OPT],
178     "[<group_name>...]",
179     "Lists the node groups in the cluster. The available fields are (see"
180     " the man page for details): %s. The default list is (in order): %s." %
181     (utils.CommaJoin(_LIST_HEADERS), utils.CommaJoin(_LIST_DEF_FIELDS))),
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)