root / lib / client / gnt_cluster.py @ 4b97f902
History | View | Annotate | Download (41.8 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2006, 2007, 2010, 2011 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 os.path |
30 |
import time |
31 |
import OpenSSL |
32 |
import itertools |
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 |
from ganeti import uidpool |
43 |
from ganeti import compat |
44 |
from ganeti import netutils |
45 |
|
46 |
|
47 |
ON_OPT = cli_option("--on", default=False, |
48 |
action="store_true", dest="on", |
49 |
help="Recover from an EPO")
|
50 |
|
51 |
GROUPS_OPT = cli_option("--groups", default=False, |
52 |
action="store_true", dest="groups", |
53 |
help="Arguments are node groups instead of nodes")
|
54 |
|
55 |
_EPO_PING_INTERVAL = 30 # 30 seconds between pings |
56 |
_EPO_PING_TIMEOUT = 1 # 1 second |
57 |
_EPO_REACHABLE_TIMEOUT = 15 * 60 # 15 minutes |
58 |
|
59 |
|
60 |
@UsesRPC
|
61 |
def InitCluster(opts, args): |
62 |
"""Initialize the cluster.
|
63 |
|
64 |
@param opts: the command line options selected by the user
|
65 |
@type args: list
|
66 |
@param args: should contain only one element, the desired
|
67 |
cluster name
|
68 |
@rtype: int
|
69 |
@return: the desired exit code
|
70 |
|
71 |
"""
|
72 |
if not opts.lvm_storage and opts.vg_name: |
73 |
ToStderr("Options --no-lvm-storage and --vg-name conflict.")
|
74 |
return 1 |
75 |
|
76 |
vg_name = opts.vg_name |
77 |
if opts.lvm_storage and not opts.vg_name: |
78 |
vg_name = constants.DEFAULT_VG |
79 |
|
80 |
if not opts.drbd_storage and opts.drbd_helper: |
81 |
ToStderr("Options --no-drbd-storage and --drbd-usermode-helper conflict.")
|
82 |
return 1 |
83 |
|
84 |
drbd_helper = opts.drbd_helper |
85 |
if opts.drbd_storage and not opts.drbd_helper: |
86 |
drbd_helper = constants.DEFAULT_DRBD_HELPER |
87 |
|
88 |
master_netdev = opts.master_netdev |
89 |
if master_netdev is None: |
90 |
master_netdev = constants.DEFAULT_BRIDGE |
91 |
|
92 |
hvlist = opts.enabled_hypervisors |
93 |
if hvlist is None: |
94 |
hvlist = constants.DEFAULT_ENABLED_HYPERVISOR |
95 |
hvlist = hvlist.split(",")
|
96 |
|
97 |
hvparams = dict(opts.hvparams)
|
98 |
beparams = opts.beparams |
99 |
nicparams = opts.nicparams |
100 |
|
101 |
# prepare beparams dict
|
102 |
beparams = objects.FillDict(constants.BEC_DEFAULTS, beparams) |
103 |
utils.ForceDictType(beparams, constants.BES_PARAMETER_TYPES) |
104 |
|
105 |
# prepare nicparams dict
|
106 |
nicparams = objects.FillDict(constants.NICC_DEFAULTS, nicparams) |
107 |
utils.ForceDictType(nicparams, constants.NICS_PARAMETER_TYPES) |
108 |
|
109 |
# prepare ndparams dict
|
110 |
if opts.ndparams is None: |
111 |
ndparams = dict(constants.NDC_DEFAULTS)
|
112 |
else:
|
113 |
ndparams = objects.FillDict(constants.NDC_DEFAULTS, opts.ndparams) |
114 |
utils.ForceDictType(ndparams, constants.NDS_PARAMETER_TYPES) |
115 |
|
116 |
# prepare hvparams dict
|
117 |
for hv in constants.HYPER_TYPES: |
118 |
if hv not in hvparams: |
119 |
hvparams[hv] = {} |
120 |
hvparams[hv] = objects.FillDict(constants.HVC_DEFAULTS[hv], hvparams[hv]) |
121 |
utils.ForceDictType(hvparams[hv], constants.HVS_PARAMETER_TYPES) |
122 |
|
123 |
if opts.candidate_pool_size is None: |
124 |
opts.candidate_pool_size = constants.MASTER_POOL_SIZE_DEFAULT |
125 |
|
126 |
if opts.mac_prefix is None: |
127 |
opts.mac_prefix = constants.DEFAULT_MAC_PREFIX |
128 |
|
129 |
uid_pool = opts.uid_pool |
130 |
if uid_pool is not None: |
131 |
uid_pool = uidpool.ParseUidPool(uid_pool) |
132 |
|
133 |
if opts.prealloc_wipe_disks is None: |
134 |
opts.prealloc_wipe_disks = False
|
135 |
|
136 |
try:
|
137 |
primary_ip_version = int(opts.primary_ip_version)
|
138 |
except (ValueError, TypeError), err: |
139 |
ToStderr("Invalid primary ip version value: %s" % str(err)) |
140 |
return 1 |
141 |
|
142 |
bootstrap.InitCluster(cluster_name=args[0],
|
143 |
secondary_ip=opts.secondary_ip, |
144 |
vg_name=vg_name, |
145 |
mac_prefix=opts.mac_prefix, |
146 |
master_netdev=master_netdev, |
147 |
file_storage_dir=opts.file_storage_dir, |
148 |
enabled_hypervisors=hvlist, |
149 |
hvparams=hvparams, |
150 |
beparams=beparams, |
151 |
nicparams=nicparams, |
152 |
ndparams=ndparams, |
153 |
candidate_pool_size=opts.candidate_pool_size, |
154 |
modify_etc_hosts=opts.modify_etc_hosts, |
155 |
modify_ssh_setup=opts.modify_ssh_setup, |
156 |
maintain_node_health=opts.maintain_node_health, |
157 |
drbd_helper=drbd_helper, |
158 |
uid_pool=uid_pool, |
159 |
default_iallocator=opts.default_iallocator, |
160 |
primary_ip_version=primary_ip_version, |
161 |
prealloc_wipe_disks=opts.prealloc_wipe_disks, |
162 |
) |
163 |
op = opcodes.OpClusterPostInit() |
164 |
SubmitOpCode(op, opts=opts) |
165 |
return 0 |
166 |
|
167 |
|
168 |
@UsesRPC
|
169 |
def DestroyCluster(opts, args): |
170 |
"""Destroy the cluster.
|
171 |
|
172 |
@param opts: the command line options selected by the user
|
173 |
@type args: list
|
174 |
@param args: should be an empty list
|
175 |
@rtype: int
|
176 |
@return: the desired exit code
|
177 |
|
178 |
"""
|
179 |
if not opts.yes_do_it: |
180 |
ToStderr("Destroying a cluster is irreversible. If you really want"
|
181 |
" destroy this cluster, supply the --yes-do-it option.")
|
182 |
return 1 |
183 |
|
184 |
op = opcodes.OpClusterDestroy() |
185 |
master = SubmitOpCode(op, opts=opts) |
186 |
# if we reached this, the opcode didn't fail; we can proceed to
|
187 |
# shutdown all the daemons
|
188 |
bootstrap.FinalizeClusterDestroy(master) |
189 |
return 0 |
190 |
|
191 |
|
192 |
def RenameCluster(opts, args): |
193 |
"""Rename the cluster.
|
194 |
|
195 |
@param opts: the command line options selected by the user
|
196 |
@type args: list
|
197 |
@param args: should contain only one element, the new cluster name
|
198 |
@rtype: int
|
199 |
@return: the desired exit code
|
200 |
|
201 |
"""
|
202 |
cl = GetClient() |
203 |
|
204 |
(cluster_name, ) = cl.QueryConfigValues(["cluster_name"])
|
205 |
|
206 |
new_name = args[0]
|
207 |
if not opts.force: |
208 |
usertext = ("This will rename the cluster from '%s' to '%s'. If you are"
|
209 |
" connected over the network to the cluster name, the"
|
210 |
" operation is very dangerous as the IP address will be"
|
211 |
" removed from the node and the change may not go through."
|
212 |
" Continue?") % (cluster_name, new_name)
|
213 |
if not AskUser(usertext): |
214 |
return 1 |
215 |
|
216 |
op = opcodes.OpClusterRename(name=new_name) |
217 |
result = SubmitOpCode(op, opts=opts, cl=cl) |
218 |
|
219 |
if result:
|
220 |
ToStdout("Cluster renamed from '%s' to '%s'", cluster_name, result)
|
221 |
|
222 |
return 0 |
223 |
|
224 |
|
225 |
def RedistributeConfig(opts, args): |
226 |
"""Forces push of the cluster configuration.
|
227 |
|
228 |
@param opts: the command line options selected by the user
|
229 |
@type args: list
|
230 |
@param args: empty list
|
231 |
@rtype: int
|
232 |
@return: the desired exit code
|
233 |
|
234 |
"""
|
235 |
op = opcodes.OpClusterRedistConf() |
236 |
SubmitOrSend(op, opts) |
237 |
return 0 |
238 |
|
239 |
|
240 |
def ShowClusterVersion(opts, args): |
241 |
"""Write version of ganeti software to the standard output.
|
242 |
|
243 |
@param opts: the command line options selected by the user
|
244 |
@type args: list
|
245 |
@param args: should be an empty list
|
246 |
@rtype: int
|
247 |
@return: the desired exit code
|
248 |
|
249 |
"""
|
250 |
cl = GetClient() |
251 |
result = cl.QueryClusterInfo() |
252 |
ToStdout("Software version: %s", result["software_version"]) |
253 |
ToStdout("Internode protocol: %s", result["protocol_version"]) |
254 |
ToStdout("Configuration format: %s", result["config_version"]) |
255 |
ToStdout("OS api version: %s", result["os_api_version"]) |
256 |
ToStdout("Export interface: %s", result["export_version"]) |
257 |
return 0 |
258 |
|
259 |
|
260 |
def ShowClusterMaster(opts, args): |
261 |
"""Write name of master node to the standard output.
|
262 |
|
263 |
@param opts: the command line options selected by the user
|
264 |
@type args: list
|
265 |
@param args: should be an empty list
|
266 |
@rtype: int
|
267 |
@return: the desired exit code
|
268 |
|
269 |
"""
|
270 |
master = bootstrap.GetMaster() |
271 |
ToStdout(master) |
272 |
return 0 |
273 |
|
274 |
|
275 |
def _PrintGroupedParams(paramsdict, level=1, roman=False): |
276 |
"""Print Grouped parameters (be, nic, disk) by group.
|
277 |
|
278 |
@type paramsdict: dict of dicts
|
279 |
@param paramsdict: {group: {param: value, ...}, ...}
|
280 |
@type level: int
|
281 |
@param level: Level of indention
|
282 |
|
283 |
"""
|
284 |
indent = " " * level
|
285 |
for item, val in sorted(paramsdict.items()): |
286 |
if isinstance(val, dict): |
287 |
ToStdout("%s- %s:", indent, item)
|
288 |
_PrintGroupedParams(val, level=level + 1, roman=roman)
|
289 |
elif roman and isinstance(val, int): |
290 |
ToStdout("%s %s: %s", indent, item, compat.TryToRoman(val))
|
291 |
else:
|
292 |
ToStdout("%s %s: %s", indent, item, val)
|
293 |
|
294 |
|
295 |
def ShowClusterConfig(opts, args): |
296 |
"""Shows cluster information.
|
297 |
|
298 |
@param opts: the command line options selected by the user
|
299 |
@type args: list
|
300 |
@param args: should be an empty list
|
301 |
@rtype: int
|
302 |
@return: the desired exit code
|
303 |
|
304 |
"""
|
305 |
cl = GetClient() |
306 |
result = cl.QueryClusterInfo() |
307 |
|
308 |
ToStdout("Cluster name: %s", result["name"]) |
309 |
ToStdout("Cluster UUID: %s", result["uuid"]) |
310 |
|
311 |
ToStdout("Creation time: %s", utils.FormatTime(result["ctime"])) |
312 |
ToStdout("Modification time: %s", utils.FormatTime(result["mtime"])) |
313 |
|
314 |
ToStdout("Master node: %s", result["master"]) |
315 |
|
316 |
ToStdout("Architecture (this node): %s (%s)",
|
317 |
result["architecture"][0], result["architecture"][1]) |
318 |
|
319 |
if result["tags"]: |
320 |
tags = utils.CommaJoin(utils.NiceSort(result["tags"]))
|
321 |
else:
|
322 |
tags = "(none)"
|
323 |
|
324 |
ToStdout("Tags: %s", tags)
|
325 |
|
326 |
ToStdout("Default hypervisor: %s", result["default_hypervisor"]) |
327 |
ToStdout("Enabled hypervisors: %s",
|
328 |
utils.CommaJoin(result["enabled_hypervisors"]))
|
329 |
|
330 |
ToStdout("Hypervisor parameters:")
|
331 |
_PrintGroupedParams(result["hvparams"])
|
332 |
|
333 |
ToStdout("OS-specific hypervisor parameters:")
|
334 |
_PrintGroupedParams(result["os_hvp"])
|
335 |
|
336 |
ToStdout("OS parameters:")
|
337 |
_PrintGroupedParams(result["osparams"])
|
338 |
|
339 |
ToStdout("Hidden OSes: %s", utils.CommaJoin(result["hidden_os"])) |
340 |
ToStdout("Blacklisted OSes: %s", utils.CommaJoin(result["blacklisted_os"])) |
341 |
|
342 |
ToStdout("Cluster parameters:")
|
343 |
ToStdout(" - candidate pool size: %s",
|
344 |
compat.TryToRoman(result["candidate_pool_size"],
|
345 |
convert=opts.roman_integers)) |
346 |
ToStdout(" - master netdev: %s", result["master_netdev"]) |
347 |
ToStdout(" - lvm volume group: %s", result["volume_group_name"]) |
348 |
if result["reserved_lvs"]: |
349 |
reserved_lvs = utils.CommaJoin(result["reserved_lvs"])
|
350 |
else:
|
351 |
reserved_lvs = "(none)"
|
352 |
ToStdout(" - lvm reserved volumes: %s", reserved_lvs)
|
353 |
ToStdout(" - drbd usermode helper: %s", result["drbd_usermode_helper"]) |
354 |
ToStdout(" - file storage path: %s", result["file_storage_dir"]) |
355 |
ToStdout(" - shared file storage path: %s",
|
356 |
result["shared_file_storage_dir"])
|
357 |
ToStdout(" - maintenance of node health: %s",
|
358 |
result["maintain_node_health"])
|
359 |
ToStdout(" - uid pool: %s",
|
360 |
uidpool.FormatUidPool(result["uid_pool"],
|
361 |
roman=opts.roman_integers)) |
362 |
ToStdout(" - default instance allocator: %s", result["default_iallocator"]) |
363 |
ToStdout(" - primary ip version: %d", result["primary_ip_version"]) |
364 |
ToStdout(" - preallocation wipe disks: %s", result["prealloc_wipe_disks"]) |
365 |
|
366 |
ToStdout("Default node parameters:")
|
367 |
_PrintGroupedParams(result["ndparams"], roman=opts.roman_integers)
|
368 |
|
369 |
ToStdout("Default instance parameters:")
|
370 |
_PrintGroupedParams(result["beparams"], roman=opts.roman_integers)
|
371 |
|
372 |
ToStdout("Default nic parameters:")
|
373 |
_PrintGroupedParams(result["nicparams"], roman=opts.roman_integers)
|
374 |
|
375 |
return 0 |
376 |
|
377 |
|
378 |
def ClusterCopyFile(opts, args): |
379 |
"""Copy a file from master to some nodes.
|
380 |
|
381 |
@param opts: the command line options selected by the user
|
382 |
@type args: list
|
383 |
@param args: should contain only one element, the path of
|
384 |
the file to be copied
|
385 |
@rtype: int
|
386 |
@return: the desired exit code
|
387 |
|
388 |
"""
|
389 |
filename = args[0]
|
390 |
if not os.path.exists(filename): |
391 |
raise errors.OpPrereqError("No such filename '%s'" % filename, |
392 |
errors.ECODE_INVAL) |
393 |
|
394 |
cl = GetClient() |
395 |
|
396 |
cluster_name = cl.QueryConfigValues(["cluster_name"])[0] |
397 |
|
398 |
results = GetOnlineNodes(nodes=opts.nodes, cl=cl, filter_master=True,
|
399 |
secondary_ips=opts.use_replication_network) |
400 |
|
401 |
srun = ssh.SshRunner(cluster_name=cluster_name) |
402 |
for node in results: |
403 |
if not srun.CopyFileToNode(node, filename): |
404 |
ToStderr("Copy of file %s to node %s failed", filename, node)
|
405 |
|
406 |
return 0 |
407 |
|
408 |
|
409 |
def RunClusterCommand(opts, args): |
410 |
"""Run a command on some nodes.
|
411 |
|
412 |
@param opts: the command line options selected by the user
|
413 |
@type args: list
|
414 |
@param args: should contain the command to be run and its arguments
|
415 |
@rtype: int
|
416 |
@return: the desired exit code
|
417 |
|
418 |
"""
|
419 |
cl = GetClient() |
420 |
|
421 |
command = " ".join(args)
|
422 |
|
423 |
nodes = GetOnlineNodes(nodes=opts.nodes, cl=cl) |
424 |
|
425 |
cluster_name, master_node = cl.QueryConfigValues(["cluster_name",
|
426 |
"master_node"])
|
427 |
|
428 |
srun = ssh.SshRunner(cluster_name=cluster_name) |
429 |
|
430 |
# Make sure master node is at list end
|
431 |
if master_node in nodes: |
432 |
nodes.remove(master_node) |
433 |
nodes.append(master_node) |
434 |
|
435 |
for name in nodes: |
436 |
result = srun.Run(name, "root", command)
|
437 |
ToStdout("------------------------------------------------")
|
438 |
ToStdout("node: %s", name)
|
439 |
ToStdout("%s", result.output)
|
440 |
ToStdout("return code = %s", result.exit_code)
|
441 |
|
442 |
return 0 |
443 |
|
444 |
|
445 |
def VerifyCluster(opts, args): |
446 |
"""Verify integrity of cluster, performing various test on nodes.
|
447 |
|
448 |
@param opts: the command line options selected by the user
|
449 |
@type args: list
|
450 |
@param args: should be an empty list
|
451 |
@rtype: int
|
452 |
@return: the desired exit code
|
453 |
|
454 |
"""
|
455 |
skip_checks = [] |
456 |
if opts.skip_nplusone_mem:
|
457 |
skip_checks.append(constants.VERIFY_NPLUSONE_MEM) |
458 |
op = opcodes.OpClusterVerify(skip_checks=skip_checks, |
459 |
verbose=opts.verbose, |
460 |
error_codes=opts.error_codes, |
461 |
debug_simulate_errors=opts.simulate_errors) |
462 |
if SubmitOpCode(op, opts=opts):
|
463 |
return 0 |
464 |
else:
|
465 |
return 1 |
466 |
|
467 |
|
468 |
def VerifyDisks(opts, args): |
469 |
"""Verify integrity of cluster disks.
|
470 |
|
471 |
@param opts: the command line options selected by the user
|
472 |
@type args: list
|
473 |
@param args: should be an empty list
|
474 |
@rtype: int
|
475 |
@return: the desired exit code
|
476 |
|
477 |
"""
|
478 |
cl = GetClient() |
479 |
|
480 |
op = opcodes.OpClusterVerifyDisks() |
481 |
result = SubmitOpCode(op, opts=opts, cl=cl) |
482 |
if not isinstance(result, (list, tuple)) or len(result) != 3: |
483 |
raise errors.ProgrammerError("Unknown result type for OpClusterVerifyDisks") |
484 |
|
485 |
bad_nodes, instances, missing = result |
486 |
|
487 |
retcode = constants.EXIT_SUCCESS |
488 |
|
489 |
if bad_nodes:
|
490 |
for node, text in bad_nodes.items(): |
491 |
ToStdout("Error gathering data on node %s: %s",
|
492 |
node, utils.SafeEncode(text[-400:]))
|
493 |
retcode |= 1
|
494 |
ToStdout("You need to fix these nodes first before fixing instances")
|
495 |
|
496 |
if instances:
|
497 |
for iname in instances: |
498 |
if iname in missing: |
499 |
continue
|
500 |
op = opcodes.OpInstanceActivateDisks(instance_name=iname) |
501 |
try:
|
502 |
ToStdout("Activating disks for instance '%s'", iname)
|
503 |
SubmitOpCode(op, opts=opts, cl=cl) |
504 |
except errors.GenericError, err:
|
505 |
nret, msg = FormatError(err) |
506 |
retcode |= nret |
507 |
ToStderr("Error activating disks for instance %s: %s", iname, msg)
|
508 |
|
509 |
if missing:
|
510 |
for iname, ival in missing.iteritems(): |
511 |
all_missing = compat.all(x[0] in bad_nodes for x in ival) |
512 |
if all_missing:
|
513 |
ToStdout("Instance %s cannot be verified as it lives on"
|
514 |
" broken nodes", iname)
|
515 |
else:
|
516 |
ToStdout("Instance %s has missing logical volumes:", iname)
|
517 |
ival.sort() |
518 |
for node, vol in ival: |
519 |
if node in bad_nodes: |
520 |
ToStdout("\tbroken node %s /dev/%s", node, vol)
|
521 |
else:
|
522 |
ToStdout("\t%s /dev/%s", node, vol)
|
523 |
|
524 |
ToStdout("You need to run replace or recreate disks for all the above"
|
525 |
" instances, if this message persist after fixing nodes.")
|
526 |
retcode |= 1
|
527 |
|
528 |
return retcode
|
529 |
|
530 |
|
531 |
def RepairDiskSizes(opts, args): |
532 |
"""Verify sizes of cluster disks.
|
533 |
|
534 |
@param opts: the command line options selected by the user
|
535 |
@type args: list
|
536 |
@param args: optional list of instances to restrict check to
|
537 |
@rtype: int
|
538 |
@return: the desired exit code
|
539 |
|
540 |
"""
|
541 |
op = opcodes.OpClusterRepairDiskSizes(instances=args) |
542 |
SubmitOpCode(op, opts=opts) |
543 |
|
544 |
|
545 |
@UsesRPC
|
546 |
def MasterFailover(opts, args): |
547 |
"""Failover the master node.
|
548 |
|
549 |
This command, when run on a non-master node, will cause the current
|
550 |
master to cease being master, and the non-master to become new
|
551 |
master.
|
552 |
|
553 |
@param opts: the command line options selected by the user
|
554 |
@type args: list
|
555 |
@param args: should be an empty list
|
556 |
@rtype: int
|
557 |
@return: the desired exit code
|
558 |
|
559 |
"""
|
560 |
if opts.no_voting:
|
561 |
usertext = ("This will perform the failover even if most other nodes"
|
562 |
" are down, or if this node is outdated. This is dangerous"
|
563 |
" as it can lead to a non-consistent cluster. Check the"
|
564 |
" gnt-cluster(8) man page before proceeding. Continue?")
|
565 |
if not AskUser(usertext): |
566 |
return 1 |
567 |
|
568 |
return bootstrap.MasterFailover(no_voting=opts.no_voting)
|
569 |
|
570 |
|
571 |
def MasterPing(opts, args): |
572 |
"""Checks if the master is alive.
|
573 |
|
574 |
@param opts: the command line options selected by the user
|
575 |
@type args: list
|
576 |
@param args: should be an empty list
|
577 |
@rtype: int
|
578 |
@return: the desired exit code
|
579 |
|
580 |
"""
|
581 |
try:
|
582 |
cl = GetClient() |
583 |
cl.QueryClusterInfo() |
584 |
return 0 |
585 |
except Exception: # pylint: disable-msg=W0703 |
586 |
return 1 |
587 |
|
588 |
|
589 |
def SearchTags(opts, args): |
590 |
"""Searches the tags on all the cluster.
|
591 |
|
592 |
@param opts: the command line options selected by the user
|
593 |
@type args: list
|
594 |
@param args: should contain only one element, the tag pattern
|
595 |
@rtype: int
|
596 |
@return: the desired exit code
|
597 |
|
598 |
"""
|
599 |
op = opcodes.OpTagsSearch(pattern=args[0])
|
600 |
result = SubmitOpCode(op, opts=opts) |
601 |
if not result: |
602 |
return 1 |
603 |
result = list(result)
|
604 |
result.sort() |
605 |
for path, tag in result: |
606 |
ToStdout("%s %s", path, tag)
|
607 |
|
608 |
|
609 |
def _RenewCrypto(new_cluster_cert, new_rapi_cert, rapi_cert_filename, |
610 |
new_confd_hmac_key, new_cds, cds_filename, |
611 |
force): |
612 |
"""Renews cluster certificates, keys and secrets.
|
613 |
|
614 |
@type new_cluster_cert: bool
|
615 |
@param new_cluster_cert: Whether to generate a new cluster certificate
|
616 |
@type new_rapi_cert: bool
|
617 |
@param new_rapi_cert: Whether to generate a new RAPI certificate
|
618 |
@type rapi_cert_filename: string
|
619 |
@param rapi_cert_filename: Path to file containing new RAPI certificate
|
620 |
@type new_confd_hmac_key: bool
|
621 |
@param new_confd_hmac_key: Whether to generate a new HMAC key
|
622 |
@type new_cds: bool
|
623 |
@param new_cds: Whether to generate a new cluster domain secret
|
624 |
@type cds_filename: string
|
625 |
@param cds_filename: Path to file containing new cluster domain secret
|
626 |
@type force: bool
|
627 |
@param force: Whether to ask user for confirmation
|
628 |
|
629 |
"""
|
630 |
if new_rapi_cert and rapi_cert_filename: |
631 |
ToStderr("Only one of the --new-rapi-certficate and --rapi-certificate"
|
632 |
" options can be specified at the same time.")
|
633 |
return 1 |
634 |
|
635 |
if new_cds and cds_filename: |
636 |
ToStderr("Only one of the --new-cluster-domain-secret and"
|
637 |
" --cluster-domain-secret options can be specified at"
|
638 |
" the same time.")
|
639 |
return 1 |
640 |
|
641 |
if rapi_cert_filename:
|
642 |
# Read and verify new certificate
|
643 |
try:
|
644 |
rapi_cert_pem = utils.ReadFile(rapi_cert_filename) |
645 |
|
646 |
OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, |
647 |
rapi_cert_pem) |
648 |
except Exception, err: # pylint: disable-msg=W0703 |
649 |
ToStderr("Can't load new RAPI certificate from %s: %s" %
|
650 |
(rapi_cert_filename, str(err)))
|
651 |
return 1 |
652 |
|
653 |
try:
|
654 |
OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM, rapi_cert_pem) |
655 |
except Exception, err: # pylint: disable-msg=W0703 |
656 |
ToStderr("Can't load new RAPI private key from %s: %s" %
|
657 |
(rapi_cert_filename, str(err)))
|
658 |
return 1 |
659 |
|
660 |
else:
|
661 |
rapi_cert_pem = None
|
662 |
|
663 |
if cds_filename:
|
664 |
try:
|
665 |
cds = utils.ReadFile(cds_filename) |
666 |
except Exception, err: # pylint: disable-msg=W0703 |
667 |
ToStderr("Can't load new cluster domain secret from %s: %s" %
|
668 |
(cds_filename, str(err)))
|
669 |
return 1 |
670 |
else:
|
671 |
cds = None
|
672 |
|
673 |
if not force: |
674 |
usertext = ("This requires all daemons on all nodes to be restarted and"
|
675 |
" may take some time. Continue?")
|
676 |
if not AskUser(usertext): |
677 |
return 1 |
678 |
|
679 |
def _RenewCryptoInner(ctx): |
680 |
ctx.feedback_fn("Updating certificates and keys")
|
681 |
bootstrap.GenerateClusterCrypto(new_cluster_cert, new_rapi_cert, |
682 |
new_confd_hmac_key, |
683 |
new_cds, |
684 |
rapi_cert_pem=rapi_cert_pem, |
685 |
cds=cds) |
686 |
|
687 |
files_to_copy = [] |
688 |
|
689 |
if new_cluster_cert:
|
690 |
files_to_copy.append(constants.NODED_CERT_FILE) |
691 |
|
692 |
if new_rapi_cert or rapi_cert_pem: |
693 |
files_to_copy.append(constants.RAPI_CERT_FILE) |
694 |
|
695 |
if new_confd_hmac_key:
|
696 |
files_to_copy.append(constants.CONFD_HMAC_KEY) |
697 |
|
698 |
if new_cds or cds: |
699 |
files_to_copy.append(constants.CLUSTER_DOMAIN_SECRET_FILE) |
700 |
|
701 |
if files_to_copy:
|
702 |
for node_name in ctx.nonmaster_nodes: |
703 |
ctx.feedback_fn("Copying %s to %s" %
|
704 |
(", ".join(files_to_copy), node_name))
|
705 |
for file_name in files_to_copy: |
706 |
ctx.ssh.CopyFileToNode(node_name, file_name) |
707 |
|
708 |
RunWhileClusterStopped(ToStdout, _RenewCryptoInner) |
709 |
|
710 |
ToStdout("All requested certificates and keys have been replaced."
|
711 |
" Running \"gnt-cluster verify\" now is recommended.")
|
712 |
|
713 |
return 0 |
714 |
|
715 |
|
716 |
def RenewCrypto(opts, args): |
717 |
"""Renews cluster certificates, keys and secrets.
|
718 |
|
719 |
"""
|
720 |
return _RenewCrypto(opts.new_cluster_cert,
|
721 |
opts.new_rapi_cert, |
722 |
opts.rapi_cert, |
723 |
opts.new_confd_hmac_key, |
724 |
opts.new_cluster_domain_secret, |
725 |
opts.cluster_domain_secret, |
726 |
opts.force) |
727 |
|
728 |
|
729 |
def SetClusterParams(opts, args): |
730 |
"""Modify the cluster.
|
731 |
|
732 |
@param opts: the command line options selected by the user
|
733 |
@type args: list
|
734 |
@param args: should be an empty list
|
735 |
@rtype: int
|
736 |
@return: the desired exit code
|
737 |
|
738 |
"""
|
739 |
if not (not opts.lvm_storage or opts.vg_name or |
740 |
not opts.drbd_storage or opts.drbd_helper or |
741 |
opts.enabled_hypervisors or opts.hvparams or |
742 |
opts.beparams or opts.nicparams or opts.ndparams or |
743 |
opts.candidate_pool_size is not None or |
744 |
opts.uid_pool is not None or |
745 |
opts.maintain_node_health is not None or |
746 |
opts.add_uids is not None or |
747 |
opts.remove_uids is not None or |
748 |
opts.default_iallocator is not None or |
749 |
opts.reserved_lvs is not None or |
750 |
opts.master_netdev is not None or |
751 |
opts.prealloc_wipe_disks is not None): |
752 |
ToStderr("Please give at least one of the parameters.")
|
753 |
return 1 |
754 |
|
755 |
vg_name = opts.vg_name |
756 |
if not opts.lvm_storage and opts.vg_name: |
757 |
ToStderr("Options --no-lvm-storage and --vg-name conflict.")
|
758 |
return 1 |
759 |
|
760 |
if not opts.lvm_storage: |
761 |
vg_name = ""
|
762 |
|
763 |
drbd_helper = opts.drbd_helper |
764 |
if not opts.drbd_storage and opts.drbd_helper: |
765 |
ToStderr("Options --no-drbd-storage and --drbd-usermode-helper conflict.")
|
766 |
return 1 |
767 |
|
768 |
if not opts.drbd_storage: |
769 |
drbd_helper = ""
|
770 |
|
771 |
hvlist = opts.enabled_hypervisors |
772 |
if hvlist is not None: |
773 |
hvlist = hvlist.split(",")
|
774 |
|
775 |
# a list of (name, dict) we can pass directly to dict() (or [])
|
776 |
hvparams = dict(opts.hvparams)
|
777 |
for hv_params in hvparams.values(): |
778 |
utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES) |
779 |
|
780 |
beparams = opts.beparams |
781 |
utils.ForceDictType(beparams, constants.BES_PARAMETER_TYPES) |
782 |
|
783 |
nicparams = opts.nicparams |
784 |
utils.ForceDictType(nicparams, constants.NICS_PARAMETER_TYPES) |
785 |
|
786 |
ndparams = opts.ndparams |
787 |
if ndparams is not None: |
788 |
utils.ForceDictType(ndparams, constants.NDS_PARAMETER_TYPES) |
789 |
|
790 |
mnh = opts.maintain_node_health |
791 |
|
792 |
uid_pool = opts.uid_pool |
793 |
if uid_pool is not None: |
794 |
uid_pool = uidpool.ParseUidPool(uid_pool) |
795 |
|
796 |
add_uids = opts.add_uids |
797 |
if add_uids is not None: |
798 |
add_uids = uidpool.ParseUidPool(add_uids) |
799 |
|
800 |
remove_uids = opts.remove_uids |
801 |
if remove_uids is not None: |
802 |
remove_uids = uidpool.ParseUidPool(remove_uids) |
803 |
|
804 |
if opts.reserved_lvs is not None: |
805 |
if opts.reserved_lvs == "": |
806 |
opts.reserved_lvs = [] |
807 |
else:
|
808 |
opts.reserved_lvs = utils.UnescapeAndSplit(opts.reserved_lvs, sep=",")
|
809 |
|
810 |
op = opcodes.OpClusterSetParams(vg_name=vg_name, |
811 |
drbd_helper=drbd_helper, |
812 |
enabled_hypervisors=hvlist, |
813 |
hvparams=hvparams, |
814 |
os_hvp=None,
|
815 |
beparams=beparams, |
816 |
nicparams=nicparams, |
817 |
ndparams=ndparams, |
818 |
candidate_pool_size=opts.candidate_pool_size, |
819 |
maintain_node_health=mnh, |
820 |
uid_pool=uid_pool, |
821 |
add_uids=add_uids, |
822 |
remove_uids=remove_uids, |
823 |
default_iallocator=opts.default_iallocator, |
824 |
prealloc_wipe_disks=opts.prealloc_wipe_disks, |
825 |
master_netdev=opts.master_netdev, |
826 |
reserved_lvs=opts.reserved_lvs) |
827 |
SubmitOpCode(op, opts=opts) |
828 |
return 0 |
829 |
|
830 |
|
831 |
def QueueOps(opts, args): |
832 |
"""Queue operations.
|
833 |
|
834 |
@param opts: the command line options selected by the user
|
835 |
@type args: list
|
836 |
@param args: should contain only one element, the subcommand
|
837 |
@rtype: int
|
838 |
@return: the desired exit code
|
839 |
|
840 |
"""
|
841 |
command = args[0]
|
842 |
client = GetClient() |
843 |
if command in ("drain", "undrain"): |
844 |
drain_flag = command == "drain"
|
845 |
client.SetQueueDrainFlag(drain_flag) |
846 |
elif command == "info": |
847 |
result = client.QueryConfigValues(["drain_flag"])
|
848 |
if result[0]: |
849 |
val = "set"
|
850 |
else:
|
851 |
val = "unset"
|
852 |
ToStdout("The drain flag is %s" % val)
|
853 |
else:
|
854 |
raise errors.OpPrereqError("Command '%s' is not valid." % command, |
855 |
errors.ECODE_INVAL) |
856 |
|
857 |
return 0 |
858 |
|
859 |
|
860 |
def _ShowWatcherPause(until): |
861 |
if until is None or until < time.time(): |
862 |
ToStdout("The watcher is not paused.")
|
863 |
else:
|
864 |
ToStdout("The watcher is paused until %s.", time.ctime(until))
|
865 |
|
866 |
|
867 |
def WatcherOps(opts, args): |
868 |
"""Watcher operations.
|
869 |
|
870 |
@param opts: the command line options selected by the user
|
871 |
@type args: list
|
872 |
@param args: should contain only one element, the subcommand
|
873 |
@rtype: int
|
874 |
@return: the desired exit code
|
875 |
|
876 |
"""
|
877 |
command = args[0]
|
878 |
client = GetClient() |
879 |
|
880 |
if command == "continue": |
881 |
client.SetWatcherPause(None)
|
882 |
ToStdout("The watcher is no longer paused.")
|
883 |
|
884 |
elif command == "pause": |
885 |
if len(args) < 2: |
886 |
raise errors.OpPrereqError("Missing pause duration", errors.ECODE_INVAL) |
887 |
|
888 |
result = client.SetWatcherPause(time.time() + ParseTimespec(args[1]))
|
889 |
_ShowWatcherPause(result) |
890 |
|
891 |
elif command == "info": |
892 |
result = client.QueryConfigValues(["watcher_pause"])
|
893 |
_ShowWatcherPause(result[0])
|
894 |
|
895 |
else:
|
896 |
raise errors.OpPrereqError("Command '%s' is not valid." % command, |
897 |
errors.ECODE_INVAL) |
898 |
|
899 |
return 0 |
900 |
|
901 |
|
902 |
def _OobPower(opts, node_list, power): |
903 |
"""Puts the node in the list to desired power state.
|
904 |
|
905 |
@param opts: The command line options selected by the user
|
906 |
@param node_list: The list of nodes to operate on
|
907 |
@param power: True if they should be powered on, False otherwise
|
908 |
@return: The success of the operation (none failed)
|
909 |
|
910 |
"""
|
911 |
if power:
|
912 |
command = constants.OOB_POWER_ON |
913 |
else:
|
914 |
command = constants.OOB_POWER_OFF |
915 |
|
916 |
op = opcodes.OpOobCommand(node_names=node_list, |
917 |
command=command, |
918 |
ignore_status=True,
|
919 |
timeout=opts.oob_timeout) |
920 |
result = SubmitOpCode(op, opts=opts) |
921 |
errs = 0
|
922 |
for node_result in result: |
923 |
(node_tuple, data_tuple) = node_result |
924 |
(_, node_name) = node_tuple |
925 |
(data_status, _) = data_tuple |
926 |
if data_status != constants.RS_NORMAL:
|
927 |
assert data_status != constants.RS_UNAVAIL
|
928 |
errs += 1
|
929 |
ToStderr("There was a problem changing power for %s, please investigate",
|
930 |
node_name) |
931 |
|
932 |
if errs > 0: |
933 |
return False |
934 |
|
935 |
return True |
936 |
|
937 |
|
938 |
def _InstanceStart(opts, inst_list, start): |
939 |
"""Puts the instances in the list to desired state.
|
940 |
|
941 |
@param opts: The command line options selected by the user
|
942 |
@param inst_list: The list of instances to operate on
|
943 |
@param start: True if they should be started, False for shutdown
|
944 |
@return: The success of the operation (none failed)
|
945 |
|
946 |
"""
|
947 |
if start:
|
948 |
opcls = opcodes.OpInstanceStartup |
949 |
text_submit, text_success, text_failed = ("startup", "started", "starting") |
950 |
else:
|
951 |
opcls = opcodes.OpInstanceShutdown |
952 |
text_submit, text_success, text_failed = ("shutdown", "stopped", "stopping") |
953 |
|
954 |
jex = JobExecutor(opts=opts) |
955 |
|
956 |
for inst in inst_list: |
957 |
ToStdout("Submit %s of instance %s", text_submit, inst)
|
958 |
op = opcls(instance_name=inst) |
959 |
jex.QueueJob(inst, op) |
960 |
|
961 |
results = jex.GetResults() |
962 |
bad_cnt = len([1 for (success, _) in results if not success]) |
963 |
|
964 |
if bad_cnt == 0: |
965 |
ToStdout("All instances have been %s successfully", text_success)
|
966 |
else:
|
967 |
ToStderr("There were errors while %s instances:\n"
|
968 |
"%d error(s) out of %d instance(s)", text_failed, bad_cnt,
|
969 |
len(results))
|
970 |
return False |
971 |
|
972 |
return True |
973 |
|
974 |
|
975 |
class _RunWhenNodesReachableHelper: |
976 |
"""Helper class to make shared internal state sharing easier.
|
977 |
|
978 |
@ivar success: Indicates if all action_cb calls were successful
|
979 |
|
980 |
"""
|
981 |
def __init__(self, node_list, action_cb, node2ip, port, feedback_fn, |
982 |
_ping_fn=netutils.TcpPing, _sleep_fn=time.sleep): |
983 |
"""Init the object.
|
984 |
|
985 |
@param node_list: The list of nodes to be reachable
|
986 |
@param action_cb: Callback called when a new host is reachable
|
987 |
@type node2ip: dict
|
988 |
@param node2ip: Node to ip mapping
|
989 |
@param port: The port to use for the TCP ping
|
990 |
@param feedback_fn: The function used for feedback
|
991 |
@param _ping_fn: Function to check reachabilty (for unittest use only)
|
992 |
@param _sleep_fn: Function to sleep (for unittest use only)
|
993 |
|
994 |
"""
|
995 |
self.down = set(node_list) |
996 |
self.up = set() |
997 |
self.node2ip = node2ip
|
998 |
self.success = True |
999 |
self.action_cb = action_cb
|
1000 |
self.port = port
|
1001 |
self.feedback_fn = feedback_fn
|
1002 |
self._ping_fn = _ping_fn
|
1003 |
self._sleep_fn = _sleep_fn
|
1004 |
|
1005 |
def __call__(self): |
1006 |
"""When called we run action_cb.
|
1007 |
|
1008 |
@raises utils.RetryAgain: When there are still down nodes
|
1009 |
|
1010 |
"""
|
1011 |
if not self.action_cb(self.up): |
1012 |
self.success = False |
1013 |
|
1014 |
if self.down: |
1015 |
raise utils.RetryAgain()
|
1016 |
else:
|
1017 |
return self.success |
1018 |
|
1019 |
def Wait(self, secs): |
1020 |
"""Checks if a host is up or waits remaining seconds.
|
1021 |
|
1022 |
@param secs: The secs remaining
|
1023 |
|
1024 |
"""
|
1025 |
start = time.time() |
1026 |
for node in self.down: |
1027 |
if self._ping_fn(self.node2ip[node], self.port, timeout=_EPO_PING_TIMEOUT, |
1028 |
live_port_needed=True):
|
1029 |
self.feedback_fn("Node %s became available" % node) |
1030 |
self.up.add(node)
|
1031 |
self.down -= self.up |
1032 |
# If we have a node available there is the possibility to run the
|
1033 |
# action callback successfully, therefore we don't wait and return
|
1034 |
return
|
1035 |
|
1036 |
self._sleep_fn(max(0.0, start + secs - time.time())) |
1037 |
|
1038 |
|
1039 |
def _RunWhenNodesReachable(node_list, action_cb, interval): |
1040 |
"""Run action_cb when nodes become reachable.
|
1041 |
|
1042 |
@param node_list: The list of nodes to be reachable
|
1043 |
@param action_cb: Callback called when a new host is reachable
|
1044 |
@param interval: The earliest time to retry
|
1045 |
|
1046 |
"""
|
1047 |
client = GetClient() |
1048 |
cluster_info = client.QueryClusterInfo() |
1049 |
if cluster_info["primary_ip_version"] == constants.IP4_VERSION: |
1050 |
family = netutils.IPAddress.family |
1051 |
else:
|
1052 |
family = netutils.IP6Address.family |
1053 |
|
1054 |
node2ip = dict((node, netutils.GetHostname(node, family=family).ip)
|
1055 |
for node in node_list) |
1056 |
|
1057 |
port = netutils.GetDaemonPort(constants.NODED) |
1058 |
helper = _RunWhenNodesReachableHelper(node_list, action_cb, node2ip, port, |
1059 |
ToStdout) |
1060 |
|
1061 |
try:
|
1062 |
return utils.Retry(helper, interval, _EPO_REACHABLE_TIMEOUT,
|
1063 |
wait_fn=helper.Wait) |
1064 |
except utils.RetryTimeout:
|
1065 |
ToStderr("Time exceeded while waiting for nodes to become reachable"
|
1066 |
" again:\n - %s", " - ".join(helper.down)) |
1067 |
return False |
1068 |
|
1069 |
|
1070 |
def _MaybeInstanceStartup(opts, inst_map, nodes_online, |
1071 |
_instance_start_fn=_InstanceStart): |
1072 |
"""Start the instances conditional based on node_states.
|
1073 |
|
1074 |
@param opts: The command line options selected by the user
|
1075 |
@param inst_map: A dict of inst -> nodes mapping
|
1076 |
@param nodes_online: A list of nodes online
|
1077 |
@param _instance_start_fn: Callback to start instances (unittest use only)
|
1078 |
@return: Success of the operation on all instances
|
1079 |
|
1080 |
"""
|
1081 |
start_inst_list = [] |
1082 |
for (inst, nodes) in inst_map.items(): |
1083 |
if not (nodes - nodes_online): |
1084 |
# All nodes the instance lives on are back online
|
1085 |
start_inst_list.append(inst) |
1086 |
|
1087 |
for inst in start_inst_list: |
1088 |
del inst_map[inst]
|
1089 |
|
1090 |
if start_inst_list:
|
1091 |
return _instance_start_fn(opts, start_inst_list, True) |
1092 |
|
1093 |
return True |
1094 |
|
1095 |
|
1096 |
def _EpoOn(opts, full_node_list, node_list, inst_map): |
1097 |
"""Does the actual power on.
|
1098 |
|
1099 |
@param opts: The command line options selected by the user
|
1100 |
@param full_node_list: All nodes to operate on (includes nodes not supporting
|
1101 |
OOB)
|
1102 |
@param node_list: The list of nodes to operate on (all need to support OOB)
|
1103 |
@param inst_map: A dict of inst -> nodes mapping
|
1104 |
@return: The desired exit status
|
1105 |
|
1106 |
"""
|
1107 |
if node_list and not _OobPower(opts, node_list, False): |
1108 |
ToStderr("Not all nodes seem to get back up, investigate and start"
|
1109 |
" manually if needed")
|
1110 |
|
1111 |
# Wait for the nodes to be back up
|
1112 |
action_cb = compat.partial(_MaybeInstanceStartup, opts, dict(inst_map))
|
1113 |
|
1114 |
ToStdout("Waiting until all nodes are available again")
|
1115 |
if not _RunWhenNodesReachable(full_node_list, action_cb, _EPO_PING_INTERVAL): |
1116 |
ToStderr("Please investigate and start stopped instances manually")
|
1117 |
return constants.EXIT_FAILURE
|
1118 |
|
1119 |
return constants.EXIT_SUCCESS
|
1120 |
|
1121 |
|
1122 |
def _EpoOff(opts, node_list, inst_map): |
1123 |
"""Does the actual power off.
|
1124 |
|
1125 |
@param opts: The command line options selected by the user
|
1126 |
@param node_list: The list of nodes to operate on (all need to support OOB)
|
1127 |
@param inst_map: A dict of inst -> nodes mapping
|
1128 |
@return: The desired exit status
|
1129 |
|
1130 |
"""
|
1131 |
if not _InstanceStart(opts, inst_map.keys(), False): |
1132 |
ToStderr("Please investigate and stop instances manually before continuing")
|
1133 |
return constants.EXIT_FAILURE
|
1134 |
|
1135 |
if not node_list: |
1136 |
return constants.EXIT_SUCCESS
|
1137 |
|
1138 |
if _OobPower(opts, node_list, False): |
1139 |
return constants.EXIT_SUCCESS
|
1140 |
else:
|
1141 |
return constants.EXIT_FAILURE
|
1142 |
|
1143 |
|
1144 |
def Epo(opts, args): |
1145 |
"""EPO operations.
|
1146 |
|
1147 |
@param opts: the command line options selected by the user
|
1148 |
@type args: list
|
1149 |
@param args: should contain only one element, the subcommand
|
1150 |
@rtype: int
|
1151 |
@return: the desired exit code
|
1152 |
|
1153 |
"""
|
1154 |
if opts.groups and opts.show_all: |
1155 |
ToStderr("Only one of --groups or --all are allowed")
|
1156 |
return constants.EXIT_FAILURE
|
1157 |
elif args and opts.show_all: |
1158 |
ToStderr("Arguments in combination with --all are not allowed")
|
1159 |
return constants.EXIT_FAILURE
|
1160 |
|
1161 |
client = GetClient() |
1162 |
|
1163 |
if opts.groups:
|
1164 |
node_query_list = itertools.chain(*client.QueryGroups(names=args, |
1165 |
fields=["node_list"],
|
1166 |
use_locking=False))
|
1167 |
else:
|
1168 |
node_query_list = args |
1169 |
|
1170 |
result = client.QueryNodes(names=node_query_list, |
1171 |
fields=["name", "master", "pinst_list", |
1172 |
"sinst_list", "powered", "offline"], |
1173 |
use_locking=False)
|
1174 |
node_list = [] |
1175 |
inst_map = {} |
1176 |
for (idx, (node, master, pinsts, sinsts, powered,
|
1177 |
offline)) in enumerate(result): |
1178 |
# Normalize the node_query_list as well
|
1179 |
if not opts.show_all: |
1180 |
node_query_list[idx] = node |
1181 |
if not offline: |
1182 |
for inst in (pinsts + sinsts): |
1183 |
if inst in inst_map: |
1184 |
if not master: |
1185 |
inst_map[inst].add(node) |
1186 |
elif master:
|
1187 |
inst_map[inst] = set()
|
1188 |
else:
|
1189 |
inst_map[inst] = set([node])
|
1190 |
|
1191 |
if master and opts.on: |
1192 |
# We ignore the master for turning on the machines, in fact we are
|
1193 |
# already operating on the master at this point :)
|
1194 |
continue
|
1195 |
elif master and not opts.show_all: |
1196 |
ToStderr("%s is the master node, please do a master-failover to another"
|
1197 |
" node not affected by the EPO or use --all if you intend to"
|
1198 |
" shutdown the whole cluster", node)
|
1199 |
return constants.EXIT_FAILURE
|
1200 |
elif powered is None: |
1201 |
ToStdout("Node %s does not support out-of-band handling, it can not be"
|
1202 |
" handled in a fully automated manner", node)
|
1203 |
elif powered == opts.on:
|
1204 |
ToStdout("Node %s is already in desired power state, skipping", node)
|
1205 |
elif not offline or (offline and powered): |
1206 |
node_list.append(node) |
1207 |
|
1208 |
if not opts.force and not ConfirmOperation(node_query_list, "nodes", "epo"): |
1209 |
return constants.EXIT_FAILURE
|
1210 |
|
1211 |
if opts.on:
|
1212 |
return _EpoOn(opts, node_query_list, node_list, inst_map)
|
1213 |
else:
|
1214 |
return _EpoOff(opts, node_list, inst_map)
|
1215 |
|
1216 |
|
1217 |
commands = { |
1218 |
'init': (
|
1219 |
InitCluster, [ArgHost(min=1, max=1)], |
1220 |
[BACKEND_OPT, CP_SIZE_OPT, ENABLED_HV_OPT, GLOBAL_FILEDIR_OPT, |
1221 |
HVLIST_OPT, MAC_PREFIX_OPT, MASTER_NETDEV_OPT, NIC_PARAMS_OPT, |
1222 |
NOLVM_STORAGE_OPT, NOMODIFY_ETCHOSTS_OPT, NOMODIFY_SSH_SETUP_OPT, |
1223 |
SECONDARY_IP_OPT, VG_NAME_OPT, MAINTAIN_NODE_HEALTH_OPT, |
1224 |
UIDPOOL_OPT, DRBD_HELPER_OPT, NODRBD_STORAGE_OPT, |
1225 |
DEFAULT_IALLOCATOR_OPT, PRIMARY_IP_VERSION_OPT, PREALLOC_WIPE_DISKS_OPT, |
1226 |
NODE_PARAMS_OPT], |
1227 |
"[opts...] <cluster_name>", "Initialises a new cluster configuration"), |
1228 |
'destroy': (
|
1229 |
DestroyCluster, ARGS_NONE, [YES_DOIT_OPT], |
1230 |
"", "Destroy cluster"), |
1231 |
'rename': (
|
1232 |
RenameCluster, [ArgHost(min=1, max=1)], |
1233 |
[FORCE_OPT, DRY_RUN_OPT], |
1234 |
"<new_name>",
|
1235 |
"Renames the cluster"),
|
1236 |
'redist-conf': (
|
1237 |
RedistributeConfig, ARGS_NONE, [SUBMIT_OPT, DRY_RUN_OPT, PRIORITY_OPT], |
1238 |
"", "Forces a push of the configuration file and ssconf files" |
1239 |
" to the nodes in the cluster"),
|
1240 |
'verify': (
|
1241 |
VerifyCluster, ARGS_NONE, |
1242 |
[VERBOSE_OPT, DEBUG_SIMERR_OPT, ERROR_CODES_OPT, NONPLUS1_OPT, |
1243 |
DRY_RUN_OPT, PRIORITY_OPT], |
1244 |
"", "Does a check on the cluster configuration"), |
1245 |
'verify-disks': (
|
1246 |
VerifyDisks, ARGS_NONE, [PRIORITY_OPT], |
1247 |
"", "Does a check on the cluster disk status"), |
1248 |
'repair-disk-sizes': (
|
1249 |
RepairDiskSizes, ARGS_MANY_INSTANCES, [DRY_RUN_OPT, PRIORITY_OPT], |
1250 |
"", "Updates mismatches in recorded disk sizes"), |
1251 |
'master-failover': (
|
1252 |
MasterFailover, ARGS_NONE, [NOVOTING_OPT], |
1253 |
"", "Makes the current node the master"), |
1254 |
'master-ping': (
|
1255 |
MasterPing, ARGS_NONE, [], |
1256 |
"", "Checks if the master is alive"), |
1257 |
'version': (
|
1258 |
ShowClusterVersion, ARGS_NONE, [], |
1259 |
"", "Shows the cluster version"), |
1260 |
'getmaster': (
|
1261 |
ShowClusterMaster, ARGS_NONE, [], |
1262 |
"", "Shows the cluster master"), |
1263 |
'copyfile': (
|
1264 |
ClusterCopyFile, [ArgFile(min=1, max=1)], |
1265 |
[NODE_LIST_OPT, USE_REPL_NET_OPT], |
1266 |
"[-n node...] <filename>", "Copies a file to all (or only some) nodes"), |
1267 |
'command': (
|
1268 |
RunClusterCommand, [ArgCommand(min=1)],
|
1269 |
[NODE_LIST_OPT], |
1270 |
"[-n node...] <command>", "Runs a command on all (or only some) nodes"), |
1271 |
'info': (
|
1272 |
ShowClusterConfig, ARGS_NONE, [ROMAN_OPT], |
1273 |
"[--roman]", "Show cluster configuration"), |
1274 |
'list-tags': (
|
1275 |
ListTags, ARGS_NONE, [], "", "List the tags of the cluster"), |
1276 |
'add-tags': (
|
1277 |
AddTags, [ArgUnknown()], [TAG_SRC_OPT, PRIORITY_OPT], |
1278 |
"tag...", "Add tags to the cluster"), |
1279 |
'remove-tags': (
|
1280 |
RemoveTags, [ArgUnknown()], [TAG_SRC_OPT, PRIORITY_OPT], |
1281 |
"tag...", "Remove tags from the cluster"), |
1282 |
'search-tags': (
|
1283 |
SearchTags, [ArgUnknown(min=1, max=1)], [PRIORITY_OPT], "", |
1284 |
"Searches the tags on all objects on"
|
1285 |
" the cluster for a given pattern (regex)"),
|
1286 |
'queue': (
|
1287 |
QueueOps, |
1288 |
[ArgChoice(min=1, max=1, choices=["drain", "undrain", "info"])], |
1289 |
[], "drain|undrain|info", "Change queue properties"), |
1290 |
'watcher': (
|
1291 |
WatcherOps, |
1292 |
[ArgChoice(min=1, max=1, choices=["pause", "continue", "info"]), |
1293 |
ArgSuggest(min=0, max=1, choices=["30m", "1h", "4h"])], |
1294 |
[], |
1295 |
"{pause <timespec>|continue|info}", "Change watcher properties"), |
1296 |
'modify': (
|
1297 |
SetClusterParams, ARGS_NONE, |
1298 |
[BACKEND_OPT, CP_SIZE_OPT, ENABLED_HV_OPT, HVLIST_OPT, MASTER_NETDEV_OPT, |
1299 |
NIC_PARAMS_OPT, NOLVM_STORAGE_OPT, VG_NAME_OPT, MAINTAIN_NODE_HEALTH_OPT, |
1300 |
UIDPOOL_OPT, ADD_UIDS_OPT, REMOVE_UIDS_OPT, DRBD_HELPER_OPT, |
1301 |
NODRBD_STORAGE_OPT, DEFAULT_IALLOCATOR_OPT, RESERVED_LVS_OPT, |
1302 |
DRY_RUN_OPT, PRIORITY_OPT, PREALLOC_WIPE_DISKS_OPT, NODE_PARAMS_OPT], |
1303 |
"[opts...]",
|
1304 |
"Alters the parameters of the cluster"),
|
1305 |
"renew-crypto": (
|
1306 |
RenewCrypto, ARGS_NONE, |
1307 |
[NEW_CLUSTER_CERT_OPT, NEW_RAPI_CERT_OPT, RAPI_CERT_OPT, |
1308 |
NEW_CONFD_HMAC_KEY_OPT, FORCE_OPT, |
1309 |
NEW_CLUSTER_DOMAIN_SECRET_OPT, CLUSTER_DOMAIN_SECRET_OPT], |
1310 |
"[opts...]",
|
1311 |
"Renews cluster certificates, keys and secrets"),
|
1312 |
"epo": (
|
1313 |
Epo, [ArgUnknown()], |
1314 |
[FORCE_OPT, ON_OPT, GROUPS_OPT, ALL_OPT, OOB_TIMEOUT_OPT], |
1315 |
"[opts...] [args]",
|
1316 |
"Performs an emergency power-off on given args"),
|
1317 |
} |
1318 |
|
1319 |
|
1320 |
#: dictionary with aliases for commands
|
1321 |
aliases = { |
1322 |
'masterfailover': 'master-failover', |
1323 |
} |
1324 |
|
1325 |
|
1326 |
def Main(): |
1327 |
return GenericMain(commands, override={"tag_type": constants.TAG_CLUSTER}, |
1328 |
aliases=aliases) |