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