root / scripts / gnt-cluster @ 6d4a1656
History | View | Annotate | Download (23.9 kB)
1 |
#!/usr/bin/python |
---|---|
2 |
# |
3 |
|
4 |
# Copyright (C) 2006, 2007 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 |
"""Cluster related commands""" |
22 |
|
23 |
# pylint: disable-msg=W0401,W0613,W0614,C0103 |
24 |
# W0401: Wildcard import ganeti.cli |
25 |
# W0613: Unused argument, since all functions follow the same API |
26 |
# W0614: Unused import %s from wildcard import (since we need cli) |
27 |
# C0103: Invalid name gnt-cluster |
28 |
|
29 |
import sys |
30 |
import os.path |
31 |
import time |
32 |
import OpenSSL |
33 |
|
34 |
from ganeti.cli import * |
35 |
from ganeti import opcodes |
36 |
from ganeti import constants |
37 |
from ganeti import errors |
38 |
from ganeti import utils |
39 |
from ganeti import bootstrap |
40 |
from ganeti import ssh |
41 |
from ganeti import objects |
42 |
|
43 |
|
44 |
@UsesRPC |
45 |
def InitCluster(opts, args): |
46 |
"""Initialize the cluster. |
47 |
|
48 |
@param opts: the command line options selected by the user |
49 |
@type args: list |
50 |
@param args: should contain only one element, the desired |
51 |
cluster name |
52 |
@rtype: int |
53 |
@return: the desired exit code |
54 |
|
55 |
""" |
56 |
if not opts.lvm_storage and opts.vg_name: |
57 |
ToStderr("Options --no-lvm-storage and --vg-name conflict.") |
58 |
return 1 |
59 |
|
60 |
vg_name = opts.vg_name |
61 |
if opts.lvm_storage and not opts.vg_name: |
62 |
vg_name = constants.DEFAULT_VG |
63 |
|
64 |
hvlist = opts.enabled_hypervisors |
65 |
if hvlist is None: |
66 |
hvlist = constants.DEFAULT_ENABLED_HYPERVISOR |
67 |
hvlist = hvlist.split(",") |
68 |
|
69 |
hvparams = dict(opts.hvparams) |
70 |
beparams = opts.beparams |
71 |
nicparams = opts.nicparams |
72 |
|
73 |
# prepare beparams dict |
74 |
beparams = objects.FillDict(constants.BEC_DEFAULTS, beparams) |
75 |
utils.ForceDictType(beparams, constants.BES_PARAMETER_TYPES) |
76 |
|
77 |
# prepare nicparams dict |
78 |
nicparams = objects.FillDict(constants.NICC_DEFAULTS, nicparams) |
79 |
utils.ForceDictType(nicparams, constants.NICS_PARAMETER_TYPES) |
80 |
|
81 |
# prepare hvparams dict |
82 |
for hv in constants.HYPER_TYPES: |
83 |
if hv not in hvparams: |
84 |
hvparams[hv] = {} |
85 |
hvparams[hv] = objects.FillDict(constants.HVC_DEFAULTS[hv], hvparams[hv]) |
86 |
utils.ForceDictType(hvparams[hv], constants.HVS_PARAMETER_TYPES) |
87 |
|
88 |
if opts.candidate_pool_size is None: |
89 |
opts.candidate_pool_size = constants.MASTER_POOL_SIZE_DEFAULT |
90 |
|
91 |
if opts.mac_prefix is None: |
92 |
opts.mac_prefix = constants.DEFAULT_MAC_PREFIX |
93 |
|
94 |
bootstrap.InitCluster(cluster_name=args[0], |
95 |
secondary_ip=opts.secondary_ip, |
96 |
vg_name=vg_name, |
97 |
mac_prefix=opts.mac_prefix, |
98 |
master_netdev=opts.master_netdev, |
99 |
file_storage_dir=opts.file_storage_dir, |
100 |
enabled_hypervisors=hvlist, |
101 |
hvparams=hvparams, |
102 |
beparams=beparams, |
103 |
nicparams=nicparams, |
104 |
candidate_pool_size=opts.candidate_pool_size, |
105 |
modify_etc_hosts=opts.modify_etc_hosts, |
106 |
modify_ssh_setup=opts.modify_ssh_setup, |
107 |
) |
108 |
op = opcodes.OpPostInitCluster() |
109 |
SubmitOpCode(op, opts=opts) |
110 |
return 0 |
111 |
|
112 |
|
113 |
@UsesRPC |
114 |
def DestroyCluster(opts, args): |
115 |
"""Destroy the cluster. |
116 |
|
117 |
@param opts: the command line options selected by the user |
118 |
@type args: list |
119 |
@param args: should be an empty list |
120 |
@rtype: int |
121 |
@return: the desired exit code |
122 |
|
123 |
""" |
124 |
if not opts.yes_do_it: |
125 |
ToStderr("Destroying a cluster is irreversible. If you really want" |
126 |
" destroy this cluster, supply the --yes-do-it option.") |
127 |
return 1 |
128 |
|
129 |
op = opcodes.OpDestroyCluster() |
130 |
master = SubmitOpCode(op, opts=opts) |
131 |
# if we reached this, the opcode didn't fail; we can proceed to |
132 |
# shutdown all the daemons |
133 |
bootstrap.FinalizeClusterDestroy(master) |
134 |
return 0 |
135 |
|
136 |
|
137 |
def RenameCluster(opts, args): |
138 |
"""Rename the cluster. |
139 |
|
140 |
@param opts: the command line options selected by the user |
141 |
@type args: list |
142 |
@param args: should contain only one element, the new cluster name |
143 |
@rtype: int |
144 |
@return: the desired exit code |
145 |
|
146 |
""" |
147 |
name = args[0] |
148 |
if not opts.force: |
149 |
usertext = ("This will rename the cluster to '%s'. If you are connected" |
150 |
" over the network to the cluster name, the operation is very" |
151 |
" dangerous as the IP address will be removed from the node" |
152 |
" and the change may not go through. Continue?") % name |
153 |
if not AskUser(usertext): |
154 |
return 1 |
155 |
|
156 |
op = opcodes.OpRenameCluster(name=name) |
157 |
SubmitOpCode(op, opts=opts) |
158 |
return 0 |
159 |
|
160 |
|
161 |
def RedistributeConfig(opts, args): |
162 |
"""Forces push of the cluster configuration. |
163 |
|
164 |
@param opts: the command line options selected by the user |
165 |
@type args: list |
166 |
@param args: empty list |
167 |
@rtype: int |
168 |
@return: the desired exit code |
169 |
|
170 |
""" |
171 |
op = opcodes.OpRedistributeConfig() |
172 |
SubmitOrSend(op, opts) |
173 |
return 0 |
174 |
|
175 |
|
176 |
def ShowClusterVersion(opts, args): |
177 |
"""Write version of ganeti software to the standard output. |
178 |
|
179 |
@param opts: the command line options selected by the user |
180 |
@type args: list |
181 |
@param args: should be an empty list |
182 |
@rtype: int |
183 |
@return: the desired exit code |
184 |
|
185 |
""" |
186 |
cl = GetClient() |
187 |
result = cl.QueryClusterInfo() |
188 |
ToStdout("Software version: %s", result["software_version"]) |
189 |
ToStdout("Internode protocol: %s", result["protocol_version"]) |
190 |
ToStdout("Configuration format: %s", result["config_version"]) |
191 |
ToStdout("OS api version: %s", result["os_api_version"]) |
192 |
ToStdout("Export interface: %s", result["export_version"]) |
193 |
return 0 |
194 |
|
195 |
|
196 |
def ShowClusterMaster(opts, args): |
197 |
"""Write name of master node to the standard output. |
198 |
|
199 |
@param opts: the command line options selected by the user |
200 |
@type args: list |
201 |
@param args: should be an empty list |
202 |
@rtype: int |
203 |
@return: the desired exit code |
204 |
|
205 |
""" |
206 |
master = bootstrap.GetMaster() |
207 |
ToStdout(master) |
208 |
return 0 |
209 |
|
210 |
|
211 |
def _PrintGroupedParams(paramsdict, level=1): |
212 |
"""Print Grouped parameters (be, nic, disk) by group. |
213 |
|
214 |
@type paramsdict: dict of dicts |
215 |
@param paramsdict: {group: {param: value, ...}, ...} |
216 |
@type level: int |
217 |
@param level: Level of indention |
218 |
|
219 |
""" |
220 |
indent = " " * level |
221 |
for item, val in paramsdict.items(): |
222 |
if isinstance(val, dict): |
223 |
ToStdout("%s- %s:", indent, item) |
224 |
_PrintGroupedParams(val, level=level + 1) |
225 |
else: |
226 |
ToStdout("%s %s: %s", indent, item, val) |
227 |
|
228 |
|
229 |
def ShowClusterConfig(opts, args): |
230 |
"""Shows cluster information. |
231 |
|
232 |
@param opts: the command line options selected by the user |
233 |
@type args: list |
234 |
@param args: should be an empty list |
235 |
@rtype: int |
236 |
@return: the desired exit code |
237 |
|
238 |
""" |
239 |
cl = GetClient() |
240 |
result = cl.QueryClusterInfo() |
241 |
|
242 |
ToStdout("Cluster name: %s", result["name"]) |
243 |
ToStdout("Cluster UUID: %s", result["uuid"]) |
244 |
|
245 |
ToStdout("Creation time: %s", utils.FormatTime(result["ctime"])) |
246 |
ToStdout("Modification time: %s", utils.FormatTime(result["mtime"])) |
247 |
|
248 |
ToStdout("Master node: %s", result["master"]) |
249 |
|
250 |
ToStdout("Architecture (this node): %s (%s)", |
251 |
result["architecture"][0], result["architecture"][1]) |
252 |
|
253 |
if result["tags"]: |
254 |
tags = utils.CommaJoin(utils.NiceSort(result["tags"])) |
255 |
else: |
256 |
tags = "(none)" |
257 |
|
258 |
ToStdout("Tags: %s", tags) |
259 |
|
260 |
ToStdout("Default hypervisor: %s", result["default_hypervisor"]) |
261 |
ToStdout("Enabled hypervisors: %s", |
262 |
utils.CommaJoin(result["enabled_hypervisors"])) |
263 |
|
264 |
ToStdout("Hypervisor parameters:") |
265 |
_PrintGroupedParams(result["hvparams"]) |
266 |
|
267 |
ToStdout("OS specific hypervisor parameters:") |
268 |
_PrintGroupedParams(result["os_hvp"]) |
269 |
|
270 |
ToStdout("Cluster parameters:") |
271 |
ToStdout(" - candidate pool size: %s", result["candidate_pool_size"]) |
272 |
ToStdout(" - master netdev: %s", result["master_netdev"]) |
273 |
ToStdout(" - lvm volume group: %s", result["volume_group_name"]) |
274 |
ToStdout(" - file storage path: %s", result["file_storage_dir"]) |
275 |
|
276 |
ToStdout("Default instance parameters:") |
277 |
_PrintGroupedParams(result["beparams"]) |
278 |
|
279 |
ToStdout("Default nic parameters:") |
280 |
_PrintGroupedParams(result["nicparams"]) |
281 |
|
282 |
return 0 |
283 |
|
284 |
|
285 |
def ClusterCopyFile(opts, args): |
286 |
"""Copy a file from master to some nodes. |
287 |
|
288 |
@param opts: the command line options selected by the user |
289 |
@type args: list |
290 |
@param args: should contain only one element, the path of |
291 |
the file to be copied |
292 |
@rtype: int |
293 |
@return: the desired exit code |
294 |
|
295 |
""" |
296 |
filename = args[0] |
297 |
if not os.path.exists(filename): |
298 |
raise errors.OpPrereqError("No such filename '%s'" % filename, |
299 |
errors.ECODE_INVAL) |
300 |
|
301 |
cl = GetClient() |
302 |
|
303 |
myname = utils.GetHostInfo().name |
304 |
|
305 |
cluster_name = cl.QueryConfigValues(["cluster_name"])[0] |
306 |
|
307 |
results = GetOnlineNodes(nodes=opts.nodes, cl=cl) |
308 |
results = [name for name in results if name != myname] |
309 |
|
310 |
srun = ssh.SshRunner(cluster_name=cluster_name) |
311 |
for node in results: |
312 |
if not srun.CopyFileToNode(node, filename): |
313 |
ToStderr("Copy of file %s to node %s failed", filename, node) |
314 |
|
315 |
return 0 |
316 |
|
317 |
|
318 |
def RunClusterCommand(opts, args): |
319 |
"""Run a command on some nodes. |
320 |
|
321 |
@param opts: the command line options selected by the user |
322 |
@type args: list |
323 |
@param args: should contain the command to be run and its arguments |
324 |
@rtype: int |
325 |
@return: the desired exit code |
326 |
|
327 |
""" |
328 |
cl = GetClient() |
329 |
|
330 |
command = " ".join(args) |
331 |
|
332 |
nodes = GetOnlineNodes(nodes=opts.nodes, cl=cl) |
333 |
|
334 |
cluster_name, master_node = cl.QueryConfigValues(["cluster_name", |
335 |
"master_node"]) |
336 |
|
337 |
srun = ssh.SshRunner(cluster_name=cluster_name) |
338 |
|
339 |
# Make sure master node is at list end |
340 |
if master_node in nodes: |
341 |
nodes.remove(master_node) |
342 |
nodes.append(master_node) |
343 |
|
344 |
for name in nodes: |
345 |
result = srun.Run(name, "root", command) |
346 |
ToStdout("------------------------------------------------") |
347 |
ToStdout("node: %s", name) |
348 |
ToStdout("%s", result.output) |
349 |
ToStdout("return code = %s", result.exit_code) |
350 |
|
351 |
return 0 |
352 |
|
353 |
|
354 |
def VerifyCluster(opts, args): |
355 |
"""Verify integrity of cluster, performing various test on nodes. |
356 |
|
357 |
@param opts: the command line options selected by the user |
358 |
@type args: list |
359 |
@param args: should be an empty list |
360 |
@rtype: int |
361 |
@return: the desired exit code |
362 |
|
363 |
""" |
364 |
skip_checks = [] |
365 |
if opts.skip_nplusone_mem: |
366 |
skip_checks.append(constants.VERIFY_NPLUSONE_MEM) |
367 |
op = opcodes.OpVerifyCluster(skip_checks=skip_checks, |
368 |
verbose=opts.verbose, |
369 |
error_codes=opts.error_codes, |
370 |
debug_simulate_errors=opts.simulate_errors) |
371 |
if SubmitOpCode(op, opts=opts): |
372 |
return 0 |
373 |
else: |
374 |
return 1 |
375 |
|
376 |
|
377 |
def VerifyDisks(opts, args): |
378 |
"""Verify integrity of cluster disks. |
379 |
|
380 |
@param opts: the command line options selected by the user |
381 |
@type args: list |
382 |
@param args: should be an empty list |
383 |
@rtype: int |
384 |
@return: the desired exit code |
385 |
|
386 |
""" |
387 |
op = opcodes.OpVerifyDisks() |
388 |
result = SubmitOpCode(op, opts=opts) |
389 |
if not isinstance(result, (list, tuple)) or len(result) != 3: |
390 |
raise errors.ProgrammerError("Unknown result type for OpVerifyDisks") |
391 |
|
392 |
bad_nodes, instances, missing = result |
393 |
|
394 |
retcode = constants.EXIT_SUCCESS |
395 |
|
396 |
if bad_nodes: |
397 |
for node, text in bad_nodes.items(): |
398 |
ToStdout("Error gathering data on node %s: %s", |
399 |
node, utils.SafeEncode(text[-400:])) |
400 |
retcode |= 1 |
401 |
ToStdout("You need to fix these nodes first before fixing instances") |
402 |
|
403 |
if instances: |
404 |
for iname in instances: |
405 |
if iname in missing: |
406 |
continue |
407 |
op = opcodes.OpActivateInstanceDisks(instance_name=iname) |
408 |
try: |
409 |
ToStdout("Activating disks for instance '%s'", iname) |
410 |
SubmitOpCode(op, opts=opts) |
411 |
except errors.GenericError, err: |
412 |
nret, msg = FormatError(err) |
413 |
retcode |= nret |
414 |
ToStderr("Error activating disks for instance %s: %s", iname, msg) |
415 |
|
416 |
if missing: |
417 |
for iname, ival in missing.iteritems(): |
418 |
all_missing = utils.all(ival, lambda x: x[0] in bad_nodes) |
419 |
if all_missing: |
420 |
ToStdout("Instance %s cannot be verified as it lives on" |
421 |
" broken nodes", iname) |
422 |
else: |
423 |
ToStdout("Instance %s has missing logical volumes:", iname) |
424 |
ival.sort() |
425 |
for node, vol in ival: |
426 |
if node in bad_nodes: |
427 |
ToStdout("\tbroken node %s /dev/xenvg/%s", node, vol) |
428 |
else: |
429 |
ToStdout("\t%s /dev/xenvg/%s", node, vol) |
430 |
ToStdout("You need to run replace_disks for all the above" |
431 |
" instances, if this message persist after fixing nodes.") |
432 |
retcode |= 1 |
433 |
|
434 |
return retcode |
435 |
|
436 |
|
437 |
def RepairDiskSizes(opts, args): |
438 |
"""Verify sizes of cluster disks. |
439 |
|
440 |
@param opts: the command line options selected by the user |
441 |
@type args: list |
442 |
@param args: optional list of instances to restrict check to |
443 |
@rtype: int |
444 |
@return: the desired exit code |
445 |
|
446 |
""" |
447 |
op = opcodes.OpRepairDiskSizes(instances=args) |
448 |
SubmitOpCode(op, opts=opts) |
449 |
|
450 |
|
451 |
@UsesRPC |
452 |
def MasterFailover(opts, args): |
453 |
"""Failover the master node. |
454 |
|
455 |
This command, when run on a non-master node, will cause the current |
456 |
master to cease being master, and the non-master to become new |
457 |
master. |
458 |
|
459 |
@param opts: the command line options selected by the user |
460 |
@type args: list |
461 |
@param args: should be an empty list |
462 |
@rtype: int |
463 |
@return: the desired exit code |
464 |
|
465 |
""" |
466 |
if opts.no_voting: |
467 |
usertext = ("This will perform the failover even if most other nodes" |
468 |
" are down, or if this node is outdated. This is dangerous" |
469 |
" as it can lead to a non-consistent cluster. Check the" |
470 |
" gnt-cluster(8) man page before proceeding. Continue?") |
471 |
if not AskUser(usertext): |
472 |
return 1 |
473 |
|
474 |
return bootstrap.MasterFailover(no_voting=opts.no_voting) |
475 |
|
476 |
|
477 |
def SearchTags(opts, args): |
478 |
"""Searches the tags on all the cluster. |
479 |
|
480 |
@param opts: the command line options selected by the user |
481 |
@type args: list |
482 |
@param args: should contain only one element, the tag pattern |
483 |
@rtype: int |
484 |
@return: the desired exit code |
485 |
|
486 |
""" |
487 |
op = opcodes.OpSearchTags(pattern=args[0]) |
488 |
result = SubmitOpCode(op, opts=opts) |
489 |
if not result: |
490 |
return 1 |
491 |
result = list(result) |
492 |
result.sort() |
493 |
for path, tag in result: |
494 |
ToStdout("%s %s", path, tag) |
495 |
|
496 |
|
497 |
def _RenewCrypto(new_cluster_cert, new_rapi_cert, rapi_cert_filename, |
498 |
new_hmac_key, force): |
499 |
"""Renews cluster certificates, keys and secrets. |
500 |
|
501 |
@type new_cluster_cert: bool |
502 |
@param new_cluster_cert: Whether to generate a new cluster certificate |
503 |
@type new_rapi_cert: bool |
504 |
@param new_rapi_cert: Whether to generate a new RAPI certificate |
505 |
@type rapi_cert_filename: string |
506 |
@param rapi_cert_filename: Path to file containing new RAPI certificate |
507 |
@type new_hmac_key: bool |
508 |
@param new_hmac_key: Whether to generate a new HMAC key |
509 |
@type force: bool |
510 |
@param force: Whether to ask user for confirmation |
511 |
|
512 |
""" |
513 |
assert new_cluster_cert or new_rapi_cert or rapi_cert_filename or new_hmac_key |
514 |
|
515 |
if new_rapi_cert and rapi_cert_filename: |
516 |
ToStderr("Only one of the --new-rapi-certficate and --rapi-certificate" |
517 |
" options can be specified at the same time.") |
518 |
return 1 |
519 |
|
520 |
if rapi_cert_filename: |
521 |
# Read and verify new certificate |
522 |
try: |
523 |
rapi_cert_pem = utils.ReadFile(rapi_cert_filename) |
524 |
|
525 |
OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, |
526 |
rapi_cert_pem) |
527 |
except Exception, err: # pylint: disable-msg=W0703 |
528 |
ToStderr("Can't load new RAPI certificate from %s: %s" % |
529 |
(rapi_cert_filename, str(err))) |
530 |
return 1 |
531 |
|
532 |
try: |
533 |
OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM, rapi_cert_pem) |
534 |
except Exception, err: # pylint: disable-msg=W0703 |
535 |
ToStderr("Can't load new RAPI private key from %s: %s" % |
536 |
(rapi_cert_filename, str(err))) |
537 |
return 1 |
538 |
|
539 |
else: |
540 |
rapi_cert_pem = None |
541 |
|
542 |
if not force: |
543 |
usertext = ("This requires all daemons on all nodes to be restarted and" |
544 |
" may take some time. Continue?") |
545 |
if not AskUser(usertext): |
546 |
return 1 |
547 |
|
548 |
def _RenewCryptoInner(ctx): |
549 |
ctx.feedback_fn("Updating certificates and keys") |
550 |
bootstrap.GenerateClusterCrypto(new_cluster_cert, new_rapi_cert, |
551 |
new_hmac_key, |
552 |
rapi_cert_pem=rapi_cert_pem) |
553 |
|
554 |
files_to_copy = [] |
555 |
|
556 |
if new_cluster_cert: |
557 |
files_to_copy.append(constants.SSL_CERT_FILE) |
558 |
|
559 |
if new_rapi_cert or rapi_cert_pem: |
560 |
files_to_copy.append(constants.RAPI_CERT_FILE) |
561 |
|
562 |
if new_hmac_key: |
563 |
files_to_copy.append(constants.HMAC_CLUSTER_KEY) |
564 |
|
565 |
if files_to_copy: |
566 |
for node_name in ctx.nonmaster_nodes: |
567 |
ctx.feedback_fn("Copying %s to %s" % |
568 |
(", ".join(files_to_copy), node_name)) |
569 |
for file_name in files_to_copy: |
570 |
ctx.ssh.CopyFileToNode(node_name, file_name) |
571 |
|
572 |
RunWhileClusterStopped(ToStdout, _RenewCryptoInner) |
573 |
|
574 |
ToStdout("All requested certificates and keys have been replaced." |
575 |
" Running \"gnt-cluster verify\" now is recommended.") |
576 |
|
577 |
return 0 |
578 |
|
579 |
|
580 |
def RenewCrypto(opts, args): |
581 |
"""Renews cluster certificates, keys and secrets. |
582 |
|
583 |
""" |
584 |
return _RenewCrypto(opts.new_cluster_cert, |
585 |
opts.new_rapi_cert, |
586 |
opts.rapi_cert, |
587 |
opts.new_hmac_key, |
588 |
opts.force) |
589 |
|
590 |
|
591 |
def SetClusterParams(opts, args): |
592 |
"""Modify the cluster. |
593 |
|
594 |
@param opts: the command line options selected by the user |
595 |
@type args: list |
596 |
@param args: should be an empty list |
597 |
@rtype: int |
598 |
@return: the desired exit code |
599 |
|
600 |
""" |
601 |
if not (not opts.lvm_storage or opts.vg_name or |
602 |
opts.enabled_hypervisors or opts.hvparams or |
603 |
opts.beparams or opts.nicparams or |
604 |
opts.candidate_pool_size is not None): |
605 |
ToStderr("Please give at least one of the parameters.") |
606 |
return 1 |
607 |
|
608 |
vg_name = opts.vg_name |
609 |
if not opts.lvm_storage and opts.vg_name: |
610 |
ToStderr("Options --no-lvm-storage and --vg-name conflict.") |
611 |
return 1 |
612 |
|
613 |
if not opts.lvm_storage: |
614 |
vg_name = "" |
615 |
|
616 |
hvlist = opts.enabled_hypervisors |
617 |
if hvlist is not None: |
618 |
hvlist = hvlist.split(",") |
619 |
|
620 |
# a list of (name, dict) we can pass directly to dict() (or []) |
621 |
hvparams = dict(opts.hvparams) |
622 |
for hv_params in hvparams.values(): |
623 |
utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES) |
624 |
|
625 |
beparams = opts.beparams |
626 |
utils.ForceDictType(beparams, constants.BES_PARAMETER_TYPES) |
627 |
|
628 |
nicparams = opts.nicparams |
629 |
utils.ForceDictType(nicparams, constants.NICS_PARAMETER_TYPES) |
630 |
|
631 |
op = opcodes.OpSetClusterParams(vg_name=vg_name, |
632 |
enabled_hypervisors=hvlist, |
633 |
hvparams=hvparams, |
634 |
os_hvp=None, |
635 |
beparams=beparams, |
636 |
nicparams=nicparams, |
637 |
candidate_pool_size=opts.candidate_pool_size) |
638 |
SubmitOpCode(op, opts=opts) |
639 |
return 0 |
640 |
|
641 |
|
642 |
def QueueOps(opts, args): |
643 |
"""Queue operations. |
644 |
|
645 |
@param opts: the command line options selected by the user |
646 |
@type args: list |
647 |
@param args: should contain only one element, the subcommand |
648 |
@rtype: int |
649 |
@return: the desired exit code |
650 |
|
651 |
""" |
652 |
command = args[0] |
653 |
client = GetClient() |
654 |
if command in ("drain", "undrain"): |
655 |
drain_flag = command == "drain" |
656 |
client.SetQueueDrainFlag(drain_flag) |
657 |
elif command == "info": |
658 |
result = client.QueryConfigValues(["drain_flag"]) |
659 |
if result[0]: |
660 |
val = "set" |
661 |
else: |
662 |
val = "unset" |
663 |
ToStdout("The drain flag is %s" % val) |
664 |
else: |
665 |
raise errors.OpPrereqError("Command '%s' is not valid." % command, |
666 |
errors.ECODE_INVAL) |
667 |
|
668 |
return 0 |
669 |
|
670 |
|
671 |
def _ShowWatcherPause(until): |
672 |
if until is None or until < time.time(): |
673 |
ToStdout("The watcher is not paused.") |
674 |
else: |
675 |
ToStdout("The watcher is paused until %s.", time.ctime(until)) |
676 |
|
677 |
|
678 |
def WatcherOps(opts, args): |
679 |
"""Watcher operations. |
680 |
|
681 |
@param opts: the command line options selected by the user |
682 |
@type args: list |
683 |
@param args: should contain only one element, the subcommand |
684 |
@rtype: int |
685 |
@return: the desired exit code |
686 |
|
687 |
""" |
688 |
command = args[0] |
689 |
client = GetClient() |
690 |
|
691 |
if command == "continue": |
692 |
client.SetWatcherPause(None) |
693 |
ToStdout("The watcher is no longer paused.") |
694 |
|
695 |
elif command == "pause": |
696 |
if len(args) < 2: |
697 |
raise errors.OpPrereqError("Missing pause duration", errors.ECODE_INVAL) |
698 |
|
699 |
result = client.SetWatcherPause(time.time() + ParseTimespec(args[1])) |
700 |
_ShowWatcherPause(result) |
701 |
|
702 |
elif command == "info": |
703 |
result = client.QueryConfigValues(["watcher_pause"]) |
704 |
_ShowWatcherPause(result[0]) |
705 |
|
706 |
else: |
707 |
raise errors.OpPrereqError("Command '%s' is not valid." % command, |
708 |
errors.ECODE_INVAL) |
709 |
|
710 |
return 0 |
711 |
|
712 |
|
713 |
commands = { |
714 |
'init': ( |
715 |
InitCluster, [ArgHost(min=1, max=1)], |
716 |
[BACKEND_OPT, CP_SIZE_OPT, ENABLED_HV_OPT, GLOBAL_FILEDIR_OPT, |
717 |
HVLIST_OPT, MAC_PREFIX_OPT, MASTER_NETDEV_OPT, NIC_PARAMS_OPT, |
718 |
NOLVM_STORAGE_OPT, NOMODIFY_ETCHOSTS_OPT, NOMODIFY_SSH_SETUP_OPT, |
719 |
SECONDARY_IP_OPT, VG_NAME_OPT], |
720 |
"[opts...] <cluster_name>", "Initialises a new cluster configuration"), |
721 |
'destroy': ( |
722 |
DestroyCluster, ARGS_NONE, [YES_DOIT_OPT], |
723 |
"", "Destroy cluster"), |
724 |
'rename': ( |
725 |
RenameCluster, [ArgHost(min=1, max=1)], |
726 |
[FORCE_OPT], |
727 |
"<new_name>", |
728 |
"Renames the cluster"), |
729 |
'redist-conf': ( |
730 |
RedistributeConfig, ARGS_NONE, [SUBMIT_OPT], |
731 |
"", "Forces a push of the configuration file and ssconf files" |
732 |
" to the nodes in the cluster"), |
733 |
'verify': ( |
734 |
VerifyCluster, ARGS_NONE, |
735 |
[VERBOSE_OPT, DEBUG_SIMERR_OPT, ERROR_CODES_OPT, NONPLUS1_OPT], |
736 |
"", "Does a check on the cluster configuration"), |
737 |
'verify-disks': ( |
738 |
VerifyDisks, ARGS_NONE, [], |
739 |
"", "Does a check on the cluster disk status"), |
740 |
'repair-disk-sizes': ( |
741 |
RepairDiskSizes, ARGS_MANY_INSTANCES, [], |
742 |
"", "Updates mismatches in recorded disk sizes"), |
743 |
'masterfailover': ( |
744 |
MasterFailover, ARGS_NONE, [NOVOTING_OPT], |
745 |
"", "Makes the current node the master"), |
746 |
'version': ( |
747 |
ShowClusterVersion, ARGS_NONE, [], |
748 |
"", "Shows the cluster version"), |
749 |
'getmaster': ( |
750 |
ShowClusterMaster, ARGS_NONE, [], |
751 |
"", "Shows the cluster master"), |
752 |
'copyfile': ( |
753 |
ClusterCopyFile, [ArgFile(min=1, max=1)], |
754 |
[NODE_LIST_OPT], |
755 |
"[-n node...] <filename>", "Copies a file to all (or only some) nodes"), |
756 |
'command': ( |
757 |
RunClusterCommand, [ArgCommand(min=1)], |
758 |
[NODE_LIST_OPT], |
759 |
"[-n node...] <command>", "Runs a command on all (or only some) nodes"), |
760 |
'info': ( |
761 |
ShowClusterConfig, ARGS_NONE, [], |
762 |
"", "Show cluster configuration"), |
763 |
'list-tags': ( |
764 |
ListTags, ARGS_NONE, [], "", "List the tags of the cluster"), |
765 |
'add-tags': ( |
766 |
AddTags, [ArgUnknown()], [TAG_SRC_OPT], |
767 |
"tag...", "Add tags to the cluster"), |
768 |
'remove-tags': ( |
769 |
RemoveTags, [ArgUnknown()], [TAG_SRC_OPT], |
770 |
"tag...", "Remove tags from the cluster"), |
771 |
'search-tags': ( |
772 |
SearchTags, [ArgUnknown(min=1, max=1)], |
773 |
[], "", "Searches the tags on all objects on" |
774 |
" the cluster for a given pattern (regex)"), |
775 |
'queue': ( |
776 |
QueueOps, |
777 |
[ArgChoice(min=1, max=1, choices=["drain", "undrain", "info"])], |
778 |
[], "drain|undrain|info", "Change queue properties"), |
779 |
'watcher': ( |
780 |
WatcherOps, |
781 |
[ArgChoice(min=1, max=1, choices=["pause", "continue", "info"]), |
782 |
ArgSuggest(min=0, max=1, choices=["30m", "1h", "4h"])], |
783 |
[], |
784 |
"{pause <timespec>|continue|info}", "Change watcher properties"), |
785 |
'modify': ( |
786 |
SetClusterParams, ARGS_NONE, |
787 |
[BACKEND_OPT, CP_SIZE_OPT, ENABLED_HV_OPT, HVLIST_OPT, |
788 |
NIC_PARAMS_OPT, NOLVM_STORAGE_OPT, VG_NAME_OPT], |
789 |
"[opts...]", |
790 |
"Alters the parameters of the cluster"), |
791 |
"renew-crypto": ( |
792 |
RenewCrypto, ARGS_NONE, |
793 |
[NEW_CLUSTER_CERT_OPT, NEW_RAPI_CERT_OPT, RAPI_CERT_OPT, NEW_HMAC_KEY_OPT, |
794 |
FORCE_OPT], |
795 |
"[opts...]", |
796 |
"Renews cluster certificates, keys and secrets"), |
797 |
} |
798 |
|
799 |
|
800 |
if __name__ == '__main__': |
801 |
sys.exit(GenericMain(commands, override={"tag_type": constants.TAG_CLUSTER})) |