root / lib / client / gnt_group.py @ d2d3935a
History | View | Annotate | Download (11.6 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2010, 2011, 2012, 2013 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=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 |
from ganeti import compat |
32 |
|
33 |
|
34 |
#: default list of fields for L{ListGroups}
|
35 |
_LIST_DEF_FIELDS = ["name", "node_cnt", "pinst_cnt", "alloc_policy", "ndparams"] |
36 |
|
37 |
_ENV_OVERRIDE = compat.UniqueFrozenset(["list"])
|
38 |
|
39 |
|
40 |
def AddGroup(opts, args): |
41 |
"""Add a node group to the cluster.
|
42 |
|
43 |
@param opts: the command line options selected by the user
|
44 |
@type args: list
|
45 |
@param args: a list of length 1 with the name of the group to create
|
46 |
@rtype: int
|
47 |
@return: the desired exit code
|
48 |
|
49 |
"""
|
50 |
ipolicy = CreateIPolicyFromOpts( |
51 |
ispecs_mem_size=opts.ispecs_mem_size, |
52 |
ispecs_cpu_count=opts.ispecs_cpu_count, |
53 |
ispecs_disk_count=opts.ispecs_disk_count, |
54 |
ispecs_disk_size=opts.ispecs_disk_size, |
55 |
ispecs_nic_count=opts.ispecs_nic_count, |
56 |
minmax_ispecs=opts.ipolicy_bounds_specs, |
57 |
ipolicy_vcpu_ratio=opts.ipolicy_vcpu_ratio, |
58 |
ipolicy_spindle_ratio=opts.ipolicy_spindle_ratio, |
59 |
group_ipolicy=True)
|
60 |
|
61 |
(group_name,) = args |
62 |
diskparams = dict(opts.diskparams)
|
63 |
|
64 |
if opts.disk_state:
|
65 |
disk_state = utils.FlatToDict(opts.disk_state) |
66 |
else:
|
67 |
disk_state = {} |
68 |
hv_state = dict(opts.hv_state)
|
69 |
|
70 |
op = opcodes.OpGroupAdd(group_name=group_name, ndparams=opts.ndparams, |
71 |
alloc_policy=opts.alloc_policy, |
72 |
diskparams=diskparams, ipolicy=ipolicy, |
73 |
hv_state=hv_state, |
74 |
disk_state=disk_state) |
75 |
SubmitOrSend(op, opts) |
76 |
|
77 |
|
78 |
def AssignNodes(opts, args): |
79 |
"""Assign nodes to a group.
|
80 |
|
81 |
@param opts: the command line options selected by the user
|
82 |
@type args: list
|
83 |
@param args: args[0]: group to assign nodes to; args[1:]: nodes to assign
|
84 |
@rtype: int
|
85 |
@return: the desired exit code
|
86 |
|
87 |
"""
|
88 |
group_name = args[0]
|
89 |
node_names = args[1:]
|
90 |
|
91 |
op = opcodes.OpGroupAssignNodes(group_name=group_name, nodes=node_names, |
92 |
force=opts.force) |
93 |
SubmitOrSend(op, opts) |
94 |
|
95 |
|
96 |
def _FmtDict(data): |
97 |
"""Format dict data into command-line format.
|
98 |
|
99 |
@param data: The input dict to be formatted
|
100 |
@return: The formatted dict
|
101 |
|
102 |
"""
|
103 |
if not data: |
104 |
return "(empty)" |
105 |
|
106 |
return utils.CommaJoin(["%s=%s" % (key, value) |
107 |
for key, value in data.items()]) |
108 |
|
109 |
|
110 |
def ListGroups(opts, args): |
111 |
"""List node groups and their properties.
|
112 |
|
113 |
@param opts: the command line options selected by the user
|
114 |
@type args: list
|
115 |
@param args: groups to list, or empty for all
|
116 |
@rtype: int
|
117 |
@return: the desired exit code
|
118 |
|
119 |
"""
|
120 |
desired_fields = ParseFields(opts.output, _LIST_DEF_FIELDS) |
121 |
fmtoverride = { |
122 |
"node_list": (",".join, False), |
123 |
"pinst_list": (",".join, False), |
124 |
"ndparams": (_FmtDict, False), |
125 |
} |
126 |
|
127 |
cl = GetClient(query=True)
|
128 |
|
129 |
return GenericList(constants.QR_GROUP, desired_fields, args, None, |
130 |
opts.separator, not opts.no_headers,
|
131 |
format_override=fmtoverride, verbose=opts.verbose, |
132 |
force_filter=opts.force_filter, cl=cl) |
133 |
|
134 |
|
135 |
def ListGroupFields(opts, args): |
136 |
"""List node fields.
|
137 |
|
138 |
@param opts: the command line options selected by the user
|
139 |
@type args: list
|
140 |
@param args: fields to list, or empty for all
|
141 |
@rtype: int
|
142 |
@return: the desired exit code
|
143 |
|
144 |
"""
|
145 |
cl = GetClient(query=True)
|
146 |
|
147 |
return GenericListFields(constants.QR_GROUP, args, opts.separator,
|
148 |
not opts.no_headers, cl=cl)
|
149 |
|
150 |
|
151 |
def SetGroupParams(opts, args): |
152 |
"""Modifies a node group's parameters.
|
153 |
|
154 |
@param opts: the command line options selected by the user
|
155 |
@type args: list
|
156 |
@param args: should contain only one element, the node group name
|
157 |
|
158 |
@rtype: int
|
159 |
@return: the desired exit code
|
160 |
|
161 |
"""
|
162 |
allmods = [opts.ndparams, opts.alloc_policy, opts.diskparams, opts.hv_state, |
163 |
opts.disk_state, opts.ispecs_mem_size, opts.ispecs_cpu_count, |
164 |
opts.ispecs_disk_count, opts.ispecs_disk_size, |
165 |
opts.ispecs_nic_count, opts.ipolicy_bounds_specs, |
166 |
opts.ipolicy_vcpu_ratio, opts.ipolicy_spindle_ratio, |
167 |
opts.diskparams] |
168 |
if allmods.count(None) == len(allmods): |
169 |
ToStderr("Please give at least one of the parameters.")
|
170 |
return 1 |
171 |
|
172 |
if opts.disk_state:
|
173 |
disk_state = utils.FlatToDict(opts.disk_state) |
174 |
else:
|
175 |
disk_state = {} |
176 |
|
177 |
hv_state = dict(opts.hv_state)
|
178 |
|
179 |
diskparams = dict(opts.diskparams)
|
180 |
|
181 |
# set the default values
|
182 |
to_ipolicy = [ |
183 |
opts.ispecs_mem_size, |
184 |
opts.ispecs_cpu_count, |
185 |
opts.ispecs_disk_count, |
186 |
opts.ispecs_disk_size, |
187 |
opts.ispecs_nic_count, |
188 |
] |
189 |
for ispec in to_ipolicy: |
190 |
for param in ispec: |
191 |
if isinstance(ispec[param], basestring): |
192 |
if ispec[param].lower() == "default": |
193 |
ispec[param] = constants.VALUE_DEFAULT |
194 |
# create ipolicy object
|
195 |
ipolicy = CreateIPolicyFromOpts( |
196 |
ispecs_mem_size=opts.ispecs_mem_size, |
197 |
ispecs_cpu_count=opts.ispecs_cpu_count, |
198 |
ispecs_disk_count=opts.ispecs_disk_count, |
199 |
ispecs_disk_size=opts.ispecs_disk_size, |
200 |
ispecs_nic_count=opts.ispecs_nic_count, |
201 |
minmax_ispecs=opts.ipolicy_bounds_specs, |
202 |
ipolicy_disk_templates=opts.ipolicy_disk_templates, |
203 |
ipolicy_vcpu_ratio=opts.ipolicy_vcpu_ratio, |
204 |
ipolicy_spindle_ratio=opts.ipolicy_spindle_ratio, |
205 |
group_ipolicy=True,
|
206 |
allowed_values=[constants.VALUE_DEFAULT]) |
207 |
|
208 |
op = opcodes.OpGroupSetParams(group_name=args[0],
|
209 |
ndparams=opts.ndparams, |
210 |
alloc_policy=opts.alloc_policy, |
211 |
hv_state=hv_state, |
212 |
disk_state=disk_state, |
213 |
diskparams=diskparams, |
214 |
ipolicy=ipolicy) |
215 |
|
216 |
result = SubmitOrSend(op, opts) |
217 |
|
218 |
if result:
|
219 |
ToStdout("Modified node group %s", args[0]) |
220 |
for param, data in result: |
221 |
ToStdout(" - %-5s -> %s", param, data)
|
222 |
|
223 |
return 0 |
224 |
|
225 |
|
226 |
def RemoveGroup(opts, args): |
227 |
"""Remove a node group from the cluster.
|
228 |
|
229 |
@param opts: the command line options selected by the user
|
230 |
@type args: list
|
231 |
@param args: a list of length 1 with the name of the group to remove
|
232 |
@rtype: int
|
233 |
@return: the desired exit code
|
234 |
|
235 |
"""
|
236 |
(group_name,) = args |
237 |
op = opcodes.OpGroupRemove(group_name=group_name) |
238 |
SubmitOrSend(op, opts) |
239 |
|
240 |
|
241 |
def RenameGroup(opts, args): |
242 |
"""Rename a node group.
|
243 |
|
244 |
@param opts: the command line options selected by the user
|
245 |
@type args: list
|
246 |
@param args: a list of length 2, [old_name, new_name]
|
247 |
@rtype: int
|
248 |
@return: the desired exit code
|
249 |
|
250 |
"""
|
251 |
group_name, new_name = args |
252 |
op = opcodes.OpGroupRename(group_name=group_name, new_name=new_name) |
253 |
SubmitOrSend(op, opts) |
254 |
|
255 |
|
256 |
def EvacuateGroup(opts, args): |
257 |
"""Evacuate a node group.
|
258 |
|
259 |
"""
|
260 |
(group_name, ) = args |
261 |
|
262 |
cl = GetClient() |
263 |
|
264 |
op = opcodes.OpGroupEvacuate(group_name=group_name, |
265 |
iallocator=opts.iallocator, |
266 |
target_groups=opts.to, |
267 |
early_release=opts.early_release) |
268 |
result = SubmitOrSend(op, opts, cl=cl) |
269 |
|
270 |
# Keep track of submitted jobs
|
271 |
jex = JobExecutor(cl=cl, opts=opts) |
272 |
|
273 |
for (status, job_id) in result[constants.JOB_IDS_KEY]: |
274 |
jex.AddJobId(None, status, job_id)
|
275 |
|
276 |
results = jex.GetResults() |
277 |
bad_cnt = len([row for row in results if not row[0]]) |
278 |
if bad_cnt == 0: |
279 |
ToStdout("All instances evacuated successfully.")
|
280 |
rcode = constants.EXIT_SUCCESS |
281 |
else:
|
282 |
ToStdout("There were %s errors during the evacuation.", bad_cnt)
|
283 |
rcode = constants.EXIT_FAILURE |
284 |
|
285 |
return rcode
|
286 |
|
287 |
|
288 |
def _FormatGroupInfo(group): |
289 |
(name, ndparams, custom_ndparams, diskparams, custom_diskparams, |
290 |
ipolicy, custom_ipolicy) = group |
291 |
return [
|
292 |
("Node group", name),
|
293 |
("Node parameters", FormatParamsDictInfo(custom_ndparams, ndparams)),
|
294 |
("Disk parameters", FormatParamsDictInfo(custom_diskparams, diskparams)),
|
295 |
("Instance policy", FormatPolicyInfo(custom_ipolicy, ipolicy, False)), |
296 |
] |
297 |
|
298 |
|
299 |
def GroupInfo(_, args): |
300 |
"""Shows info about node group.
|
301 |
|
302 |
"""
|
303 |
cl = GetClient(query=True)
|
304 |
selected_fields = ["name",
|
305 |
"ndparams", "custom_ndparams", |
306 |
"diskparams", "custom_diskparams", |
307 |
"ipolicy", "custom_ipolicy"] |
308 |
result = cl.QueryGroups(names=args, fields=selected_fields, |
309 |
use_locking=False)
|
310 |
|
311 |
PrintGenericInfo([ |
312 |
_FormatGroupInfo(group) for group in result |
313 |
]) |
314 |
|
315 |
|
316 |
commands = { |
317 |
"add": (
|
318 |
AddGroup, ARGS_ONE_GROUP, |
319 |
[DRY_RUN_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT, DISK_PARAMS_OPT, |
320 |
HV_STATE_OPT, DISK_STATE_OPT, PRIORITY_OPT, |
321 |
SUBMIT_OPT] + INSTANCE_POLICY_OPTS, |
322 |
"<group_name>", "Add a new node group to the cluster"), |
323 |
"assign-nodes": (
|
324 |
AssignNodes, ARGS_ONE_GROUP + ARGS_MANY_NODES, |
325 |
[DRY_RUN_OPT, FORCE_OPT, PRIORITY_OPT, SUBMIT_OPT], |
326 |
"<group_name> <node>...", "Assign nodes to a group"), |
327 |
"list": (
|
328 |
ListGroups, ARGS_MANY_GROUPS, |
329 |
[NOHDR_OPT, SEP_OPT, FIELDS_OPT, VERBOSE_OPT, FORCE_FILTER_OPT], |
330 |
"[<group_name>...]",
|
331 |
"Lists the node groups in the cluster. The available fields can be shown"
|
332 |
" using the \"list-fields\" command (see the man page for details)."
|
333 |
" The default list is (in order): %s." % utils.CommaJoin(_LIST_DEF_FIELDS)),
|
334 |
"list-fields": (
|
335 |
ListGroupFields, [ArgUnknown()], [NOHDR_OPT, SEP_OPT], "[fields...]",
|
336 |
"Lists all available fields for node groups"),
|
337 |
"modify": (
|
338 |
SetGroupParams, ARGS_ONE_GROUP, |
339 |
[DRY_RUN_OPT, SUBMIT_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT, HV_STATE_OPT, |
340 |
DISK_STATE_OPT, DISK_PARAMS_OPT, PRIORITY_OPT] + INSTANCE_POLICY_OPTS, |
341 |
"<group_name>", "Alters the parameters of a node group"), |
342 |
"remove": (
|
343 |
RemoveGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT, PRIORITY_OPT, SUBMIT_OPT], |
344 |
"[--dry-run] <group-name>",
|
345 |
"Remove an (empty) node group from the cluster"),
|
346 |
"rename": (
|
347 |
RenameGroup, [ArgGroup(min=2, max=2)], |
348 |
[DRY_RUN_OPT, SUBMIT_OPT, PRIORITY_OPT], |
349 |
"[--dry-run] <group-name> <new-name>", "Rename a node group"), |
350 |
"evacuate": (
|
351 |
EvacuateGroup, [ArgGroup(min=1, max=1)], |
352 |
[TO_GROUP_OPT, IALLOCATOR_OPT, EARLY_RELEASE_OPT, SUBMIT_OPT, PRIORITY_OPT], |
353 |
"[-I <iallocator>] [--to <group>]",
|
354 |
"Evacuate all instances within a group"),
|
355 |
"list-tags": (
|
356 |
ListTags, ARGS_ONE_GROUP, [], |
357 |
"<group_name>", "List the tags of the given group"), |
358 |
"add-tags": (
|
359 |
AddTags, [ArgGroup(min=1, max=1), ArgUnknown()], |
360 |
[TAG_SRC_OPT, PRIORITY_OPT, SUBMIT_OPT], |
361 |
"<group_name> tag...", "Add tags to the given group"), |
362 |
"remove-tags": (
|
363 |
RemoveTags, [ArgGroup(min=1, max=1), ArgUnknown()], |
364 |
[TAG_SRC_OPT, PRIORITY_OPT, SUBMIT_OPT], |
365 |
"<group_name> tag...", "Remove tags from the given group"), |
366 |
"info": (
|
367 |
GroupInfo, ARGS_MANY_GROUPS, [], "[<group_name>...]",
|
368 |
"Show group information"),
|
369 |
} |
370 |
|
371 |
|
372 |
def Main(): |
373 |
return GenericMain(commands,
|
374 |
override={"tag_type": constants.TAG_NODEGROUP},
|
375 |
env_override=_ENV_OVERRIDE) |