root / lib / client / gnt_node.py @ 65a77fab
History | View | Annotate | Download (29.2 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2006, 2007, 2008, 2009, 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 |
"""Node 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-node
|
28 |
|
29 |
from ganeti.cli import * |
30 |
from ganeti import cli |
31 |
from ganeti import bootstrap |
32 |
from ganeti import opcodes |
33 |
from ganeti import utils |
34 |
from ganeti import constants |
35 |
from ganeti import errors |
36 |
from ganeti import netutils |
37 |
from cStringIO import StringIO |
38 |
|
39 |
|
40 |
#: default list of field for L{ListNodes}
|
41 |
_LIST_DEF_FIELDS = [ |
42 |
"name", "dtotal", "dfree", |
43 |
"mtotal", "mnode", "mfree", |
44 |
"pinst_cnt", "sinst_cnt", |
45 |
] |
46 |
|
47 |
|
48 |
#: Default field list for L{ListVolumes}
|
49 |
_LIST_VOL_DEF_FIELDS = ["node", "phys", "vg", "name", "size", "instance"] |
50 |
|
51 |
|
52 |
#: default list of field for L{ListStorage}
|
53 |
_LIST_STOR_DEF_FIELDS = [ |
54 |
constants.SF_NODE, |
55 |
constants.SF_TYPE, |
56 |
constants.SF_NAME, |
57 |
constants.SF_SIZE, |
58 |
constants.SF_USED, |
59 |
constants.SF_FREE, |
60 |
constants.SF_ALLOCATABLE, |
61 |
] |
62 |
|
63 |
|
64 |
#: default list of power commands
|
65 |
_LIST_POWER_COMMANDS = ["on", "off", "cycle", "status"] |
66 |
|
67 |
|
68 |
#: headers (and full field list) for L{ListStorage}
|
69 |
_LIST_STOR_HEADERS = { |
70 |
constants.SF_NODE: "Node",
|
71 |
constants.SF_TYPE: "Type",
|
72 |
constants.SF_NAME: "Name",
|
73 |
constants.SF_SIZE: "Size",
|
74 |
constants.SF_USED: "Used",
|
75 |
constants.SF_FREE: "Free",
|
76 |
constants.SF_ALLOCATABLE: "Allocatable",
|
77 |
} |
78 |
|
79 |
|
80 |
#: User-facing storage unit types
|
81 |
_USER_STORAGE_TYPE = { |
82 |
constants.ST_FILE: "file",
|
83 |
constants.ST_LVM_PV: "lvm-pv",
|
84 |
constants.ST_LVM_VG: "lvm-vg",
|
85 |
} |
86 |
|
87 |
_STORAGE_TYPE_OPT = \ |
88 |
cli_option("-t", "--storage-type", |
89 |
dest="user_storage_type",
|
90 |
choices=_USER_STORAGE_TYPE.keys(), |
91 |
default=None,
|
92 |
metavar="STORAGE_TYPE",
|
93 |
help=("Storage type (%s)" %
|
94 |
utils.CommaJoin(_USER_STORAGE_TYPE.keys()))) |
95 |
|
96 |
_REPAIRABLE_STORAGE_TYPES = \ |
97 |
[st for st, so in constants.VALID_STORAGE_OPERATIONS.iteritems() |
98 |
if constants.SO_FIX_CONSISTENCY in so] |
99 |
|
100 |
_MODIFIABLE_STORAGE_TYPES = constants.MODIFIABLE_STORAGE_FIELDS.keys() |
101 |
|
102 |
|
103 |
_OOB_COMMAND_ASK = frozenset([constants.OOB_POWER_OFF,
|
104 |
constants.OOB_POWER_CYCLE]) |
105 |
|
106 |
|
107 |
NONODE_SETUP_OPT = cli_option("--no-node-setup", default=True, |
108 |
action="store_false", dest="node_setup", |
109 |
help=("Do not make initial SSH setup on remote"
|
110 |
" node (needs to be done manually)"))
|
111 |
|
112 |
IGNORE_STATUS_OPT = cli_option("--ignore-status", default=False, |
113 |
action="store_true", dest="ignore_status", |
114 |
help=("Ignore the Node(s) offline status"
|
115 |
" (potentially DANGEROUS)"))
|
116 |
|
117 |
FORCE_MASTER_OPT = cli_option("--force-master", default=False, |
118 |
action="store_true", dest="force_master", |
119 |
help=("Operate on the master node too"
|
120 |
" (potentially DANGEROUS)"))
|
121 |
|
122 |
OOB_TIMEOUT_OPT = cli_option("--oob-timeout", dest="oob_timeout", type="int", |
123 |
default=constants.OOB_TIMEOUT, |
124 |
help="Maximum time to wait for out-of-band helper")
|
125 |
|
126 |
|
127 |
def ConvertStorageType(user_storage_type): |
128 |
"""Converts a user storage type to its internal name.
|
129 |
|
130 |
"""
|
131 |
try:
|
132 |
return _USER_STORAGE_TYPE[user_storage_type]
|
133 |
except KeyError: |
134 |
raise errors.OpPrereqError("Unknown storage type: %s" % user_storage_type, |
135 |
errors.ECODE_INVAL) |
136 |
|
137 |
|
138 |
def _RunSetupSSH(options, nodes): |
139 |
"""Wrapper around utils.RunCmd to call setup-ssh
|
140 |
|
141 |
@param options: The command line options
|
142 |
@param nodes: The nodes to setup
|
143 |
|
144 |
"""
|
145 |
cmd = [constants.SETUP_SSH] |
146 |
|
147 |
# Pass --debug|--verbose to the external script if set on our invocation
|
148 |
# --debug overrides --verbose
|
149 |
if options.debug:
|
150 |
cmd.append("--debug")
|
151 |
elif options.verbose:
|
152 |
cmd.append("--verbose")
|
153 |
if not options.ssh_key_check: |
154 |
cmd.append("--no-ssh-key-check")
|
155 |
|
156 |
cmd.extend(nodes) |
157 |
|
158 |
result = utils.RunCmd(cmd, interactive=True)
|
159 |
|
160 |
if result.failed:
|
161 |
errmsg = ("Command '%s' failed with exit code %s; output %r" %
|
162 |
(result.cmd, result.exit_code, result.output)) |
163 |
raise errors.OpExecError(errmsg)
|
164 |
|
165 |
|
166 |
@UsesRPC
|
167 |
def AddNode(opts, args): |
168 |
"""Add a node to the cluster.
|
169 |
|
170 |
@param opts: the command line options selected by the user
|
171 |
@type args: list
|
172 |
@param args: should contain only one element, the new node name
|
173 |
@rtype: int
|
174 |
@return: the desired exit code
|
175 |
|
176 |
"""
|
177 |
cl = GetClient() |
178 |
node = netutils.GetHostname(name=args[0]).name
|
179 |
readd = opts.readd |
180 |
|
181 |
try:
|
182 |
output = cl.QueryNodes(names=[node], fields=['name', 'sip'], |
183 |
use_locking=False)
|
184 |
node_exists, sip = output[0]
|
185 |
except (errors.OpPrereqError, errors.OpExecError):
|
186 |
node_exists = ""
|
187 |
sip = None
|
188 |
|
189 |
if readd:
|
190 |
if not node_exists: |
191 |
ToStderr("Node %s not in the cluster"
|
192 |
" - please retry without '--readd'", node)
|
193 |
return 1 |
194 |
else:
|
195 |
if node_exists:
|
196 |
ToStderr("Node %s already in the cluster (as %s)"
|
197 |
" - please retry with '--readd'", node, node_exists)
|
198 |
return 1 |
199 |
sip = opts.secondary_ip |
200 |
|
201 |
# read the cluster name from the master
|
202 |
output = cl.QueryConfigValues(['cluster_name'])
|
203 |
cluster_name = output[0]
|
204 |
|
205 |
if not readd and opts.node_setup: |
206 |
ToStderr("-- WARNING -- \n"
|
207 |
"Performing this operation is going to replace the ssh daemon"
|
208 |
" keypair\n"
|
209 |
"on the target machine (%s) with the ones of the"
|
210 |
" current one\n"
|
211 |
"and grant full intra-cluster ssh root access to/from it\n", node)
|
212 |
|
213 |
if opts.node_setup:
|
214 |
_RunSetupSSH(opts, [node]) |
215 |
|
216 |
bootstrap.SetupNodeDaemon(cluster_name, node, opts.ssh_key_check) |
217 |
|
218 |
op = opcodes.OpNodeAdd(node_name=args[0], secondary_ip=sip,
|
219 |
readd=opts.readd, group=opts.nodegroup, |
220 |
vm_capable=opts.vm_capable, ndparams=opts.ndparams, |
221 |
master_capable=opts.master_capable) |
222 |
SubmitOpCode(op, opts=opts) |
223 |
|
224 |
|
225 |
def ListNodes(opts, args): |
226 |
"""List nodes and their properties.
|
227 |
|
228 |
@param opts: the command line options selected by the user
|
229 |
@type args: list
|
230 |
@param args: nodes to list, or empty for all
|
231 |
@rtype: int
|
232 |
@return: the desired exit code
|
233 |
|
234 |
"""
|
235 |
selected_fields = ParseFields(opts.output, _LIST_DEF_FIELDS) |
236 |
|
237 |
fmtoverride = dict.fromkeys(["pinst_list", "sinst_list", "tags"], |
238 |
(",".join, False)) |
239 |
|
240 |
return GenericList(constants.QR_NODE, selected_fields, args, opts.units,
|
241 |
opts.separator, not opts.no_headers,
|
242 |
format_override=fmtoverride) |
243 |
|
244 |
|
245 |
def ListNodeFields(opts, args): |
246 |
"""List node fields.
|
247 |
|
248 |
@param opts: the command line options selected by the user
|
249 |
@type args: list
|
250 |
@param args: fields to list, or empty for all
|
251 |
@rtype: int
|
252 |
@return: the desired exit code
|
253 |
|
254 |
"""
|
255 |
return GenericListFields(constants.QR_NODE, args, opts.separator,
|
256 |
not opts.no_headers)
|
257 |
|
258 |
|
259 |
def EvacuateNode(opts, args): |
260 |
"""Relocate all secondary instance from a node.
|
261 |
|
262 |
@param opts: the command line options selected by the user
|
263 |
@type args: list
|
264 |
@param args: should be an empty list
|
265 |
@rtype: int
|
266 |
@return: the desired exit code
|
267 |
|
268 |
"""
|
269 |
cl = GetClient() |
270 |
force = opts.force |
271 |
|
272 |
dst_node = opts.dst_node |
273 |
iallocator = opts.iallocator |
274 |
|
275 |
op = opcodes.OpNodeEvacStrategy(nodes=args, |
276 |
iallocator=iallocator, |
277 |
remote_node=dst_node) |
278 |
|
279 |
result = SubmitOpCode(op, cl=cl, opts=opts) |
280 |
if not result: |
281 |
# no instances to migrate
|
282 |
ToStderr("No secondary instances on node(s) %s, exiting.",
|
283 |
utils.CommaJoin(args)) |
284 |
return constants.EXIT_SUCCESS
|
285 |
|
286 |
if not force and not AskUser("Relocate instance(s) %s from node(s) %s?" % |
287 |
(",".join("'%s'" % name[0] for name in result), |
288 |
utils.CommaJoin(args))): |
289 |
return constants.EXIT_CONFIRMATION
|
290 |
|
291 |
jex = JobExecutor(cl=cl, opts=opts) |
292 |
for row in result: |
293 |
iname = row[0]
|
294 |
node = row[1]
|
295 |
ToStdout("Will relocate instance %s to node %s", iname, node)
|
296 |
op = opcodes.OpInstanceReplaceDisks(instance_name=iname, |
297 |
remote_node=node, disks=[], |
298 |
mode=constants.REPLACE_DISK_CHG, |
299 |
early_release=opts.early_release) |
300 |
jex.QueueJob(iname, op) |
301 |
results = jex.GetResults() |
302 |
bad_cnt = len([row for row in results if not row[0]]) |
303 |
if bad_cnt == 0: |
304 |
ToStdout("All %d instance(s) failed over successfully.", len(results)) |
305 |
rcode = constants.EXIT_SUCCESS |
306 |
else:
|
307 |
ToStdout("There were errors during the failover:\n"
|
308 |
"%d error(s) out of %d instance(s).", bad_cnt, len(results)) |
309 |
rcode = constants.EXIT_FAILURE |
310 |
return rcode
|
311 |
|
312 |
|
313 |
def FailoverNode(opts, args): |
314 |
"""Failover all primary instance on a node.
|
315 |
|
316 |
@param opts: the command line options selected by the user
|
317 |
@type args: list
|
318 |
@param args: should be an empty list
|
319 |
@rtype: int
|
320 |
@return: the desired exit code
|
321 |
|
322 |
"""
|
323 |
cl = GetClient() |
324 |
force = opts.force |
325 |
selected_fields = ["name", "pinst_list"] |
326 |
|
327 |
# these fields are static data anyway, so it doesn't matter, but
|
328 |
# locking=True should be safer
|
329 |
result = cl.QueryNodes(names=args, fields=selected_fields, |
330 |
use_locking=False)
|
331 |
node, pinst = result[0]
|
332 |
|
333 |
if not pinst: |
334 |
ToStderr("No primary instances on node %s, exiting.", node)
|
335 |
return 0 |
336 |
|
337 |
pinst = utils.NiceSort(pinst) |
338 |
|
339 |
retcode = 0
|
340 |
|
341 |
if not force and not AskUser("Fail over instance(s) %s?" % |
342 |
(",".join("'%s'" % name for name in pinst))): |
343 |
return 2 |
344 |
|
345 |
jex = JobExecutor(cl=cl, opts=opts) |
346 |
for iname in pinst: |
347 |
op = opcodes.OpInstanceFailover(instance_name=iname, |
348 |
ignore_consistency=opts.ignore_consistency) |
349 |
jex.QueueJob(iname, op) |
350 |
results = jex.GetResults() |
351 |
bad_cnt = len([row for row in results if not row[0]]) |
352 |
if bad_cnt == 0: |
353 |
ToStdout("All %d instance(s) failed over successfully.", len(results)) |
354 |
else:
|
355 |
ToStdout("There were errors during the failover:\n"
|
356 |
"%d error(s) out of %d instance(s).", bad_cnt, len(results)) |
357 |
return retcode
|
358 |
|
359 |
|
360 |
def MigrateNode(opts, args): |
361 |
"""Migrate all primary instance on a node.
|
362 |
|
363 |
"""
|
364 |
cl = GetClient() |
365 |
force = opts.force |
366 |
selected_fields = ["name", "pinst_list"] |
367 |
|
368 |
result = cl.QueryNodes(names=args, fields=selected_fields, use_locking=False)
|
369 |
node, pinst = result[0]
|
370 |
|
371 |
if not pinst: |
372 |
ToStdout("No primary instances on node %s, exiting." % node)
|
373 |
return 0 |
374 |
|
375 |
pinst = utils.NiceSort(pinst) |
376 |
|
377 |
if not force and not AskUser("Migrate instance(s) %s?" % |
378 |
(",".join("'%s'" % name for name in pinst))): |
379 |
return 2 |
380 |
|
381 |
# this should be removed once --non-live is deprecated
|
382 |
if not opts.live and opts.migration_mode is not None: |
383 |
raise errors.OpPrereqError("Only one of the --non-live and " |
384 |
"--migration-mode options can be passed",
|
385 |
errors.ECODE_INVAL) |
386 |
if not opts.live: # --non-live passed |
387 |
mode = constants.HT_MIGRATION_NONLIVE |
388 |
else:
|
389 |
mode = opts.migration_mode |
390 |
op = opcodes.OpNodeMigrate(node_name=args[0], mode=mode)
|
391 |
SubmitOpCode(op, cl=cl, opts=opts) |
392 |
|
393 |
|
394 |
def ShowNodeConfig(opts, args): |
395 |
"""Show node information.
|
396 |
|
397 |
@param opts: the command line options selected by the user
|
398 |
@type args: list
|
399 |
@param args: should either be an empty list, in which case
|
400 |
we show information about all nodes, or should contain
|
401 |
a list of nodes to be queried for information
|
402 |
@rtype: int
|
403 |
@return: the desired exit code
|
404 |
|
405 |
"""
|
406 |
cl = GetClient() |
407 |
result = cl.QueryNodes(fields=["name", "pip", "sip", |
408 |
"pinst_list", "sinst_list", |
409 |
"master_candidate", "drained", "offline", |
410 |
"master_capable", "vm_capable", "powered", |
411 |
"ndparams", "custom_ndparams"], |
412 |
names=args, use_locking=False)
|
413 |
|
414 |
for (name, primary_ip, secondary_ip, pinst, sinst, is_mc, drained, offline,
|
415 |
master_capable, vm_capable, powered, ndparams, |
416 |
ndparams_custom) in result:
|
417 |
ToStdout("Node name: %s", name)
|
418 |
ToStdout(" primary ip: %s", primary_ip)
|
419 |
ToStdout(" secondary ip: %s", secondary_ip)
|
420 |
ToStdout(" master candidate: %s", is_mc)
|
421 |
ToStdout(" drained: %s", drained)
|
422 |
ToStdout(" offline: %s", offline)
|
423 |
if powered is not None: |
424 |
ToStdout(" powered: %s", powered)
|
425 |
ToStdout(" master_capable: %s", master_capable)
|
426 |
ToStdout(" vm_capable: %s", vm_capable)
|
427 |
if vm_capable:
|
428 |
if pinst:
|
429 |
ToStdout(" primary for instances:")
|
430 |
for iname in utils.NiceSort(pinst): |
431 |
ToStdout(" - %s", iname)
|
432 |
else:
|
433 |
ToStdout(" primary for no instances")
|
434 |
if sinst:
|
435 |
ToStdout(" secondary for instances:")
|
436 |
for iname in utils.NiceSort(sinst): |
437 |
ToStdout(" - %s", iname)
|
438 |
else:
|
439 |
ToStdout(" secondary for no instances")
|
440 |
ToStdout(" node parameters:")
|
441 |
buf = StringIO() |
442 |
FormatParameterDict(buf, ndparams_custom, ndparams, level=2)
|
443 |
ToStdout(buf.getvalue().rstrip("\n"))
|
444 |
|
445 |
return 0 |
446 |
|
447 |
|
448 |
def RemoveNode(opts, args): |
449 |
"""Remove a node from the cluster.
|
450 |
|
451 |
@param opts: the command line options selected by the user
|
452 |
@type args: list
|
453 |
@param args: should contain only one element, the name of
|
454 |
the node to be removed
|
455 |
@rtype: int
|
456 |
@return: the desired exit code
|
457 |
|
458 |
"""
|
459 |
op = opcodes.OpNodeRemove(node_name=args[0])
|
460 |
SubmitOpCode(op, opts=opts) |
461 |
return 0 |
462 |
|
463 |
|
464 |
def PowercycleNode(opts, args): |
465 |
"""Remove a node from the cluster.
|
466 |
|
467 |
@param opts: the command line options selected by the user
|
468 |
@type args: list
|
469 |
@param args: should contain only one element, the name of
|
470 |
the node to be removed
|
471 |
@rtype: int
|
472 |
@return: the desired exit code
|
473 |
|
474 |
"""
|
475 |
node = args[0]
|
476 |
if (not opts.confirm and |
477 |
not AskUser("Are you sure you want to hard powercycle node %s?" % node)): |
478 |
return 2 |
479 |
|
480 |
op = opcodes.OpNodePowercycle(node_name=node, force=opts.force) |
481 |
result = SubmitOpCode(op, opts=opts) |
482 |
if result:
|
483 |
ToStderr(result) |
484 |
return 0 |
485 |
|
486 |
|
487 |
def PowerNode(opts, args): |
488 |
"""Change/ask power state of a node.
|
489 |
|
490 |
@param opts: the command line options selected by the user
|
491 |
@type args: list
|
492 |
@param args: should contain only one element, the name of
|
493 |
the node to be removed
|
494 |
@rtype: int
|
495 |
@return: the desired exit code
|
496 |
|
497 |
"""
|
498 |
client = GetClient() |
499 |
command = args.pop(0)
|
500 |
|
501 |
if opts.no_headers:
|
502 |
headers = None
|
503 |
else:
|
504 |
headers = {"node": "Node", "status": "Status"} |
505 |
|
506 |
if command not in _LIST_POWER_COMMANDS: |
507 |
ToStderr("power subcommand %s not supported." % command)
|
508 |
return constants.EXIT_FAILURE
|
509 |
|
510 |
nodes = [node for (node, ) in client.QueryNodes(args, ["name"], False)] |
511 |
oob_command = "power-%s" % command
|
512 |
|
513 |
if oob_command in _OOB_COMMAND_ASK: |
514 |
if not args and not opts.show_all: |
515 |
ToStderr("Please provide at least one node or use --all for this command"
|
516 |
" as this is a potentially harmful command")
|
517 |
return constants.EXIT_FAILURE
|
518 |
elif args and opts.show_all: |
519 |
ToStderr("Please provide either nodes or use --all, can not use both at"
|
520 |
" the same time")
|
521 |
return constants.EXIT_FAILURE
|
522 |
elif not opts.force and not ConfirmOperation(nodes, "nodes", |
523 |
"power %s" % command):
|
524 |
return constants.EXIT_FAILURE
|
525 |
|
526 |
opcodelist = [] |
527 |
if not opts.ignore_status and oob_command == constants.OOB_POWER_OFF: |
528 |
# TODO: This is a little ugly as we can't catch and revert
|
529 |
for node in nodes: |
530 |
opcodelist.append(opcodes.OpNodeSetParams(node_name=node, offline=True,
|
531 |
auto_promote=opts.auto_promote)) |
532 |
|
533 |
opcodelist.append(opcodes.OpOobCommand(node_names=nodes, |
534 |
command=oob_command, |
535 |
ignore_status=opts.ignore_status, |
536 |
force_master=opts.force_master, |
537 |
timeout=opts.oob_timeout)) |
538 |
|
539 |
cli.SetGenericOpcodeOpts(opcodelist, opts) |
540 |
|
541 |
job_id = cli.SendJob(opcodelist) |
542 |
|
543 |
# We just want the OOB Opcode status
|
544 |
# If it fails PollJob gives us the error message in it
|
545 |
result = cli.PollJob(job_id)[-1]
|
546 |
|
547 |
errs = 0
|
548 |
data = [] |
549 |
for node_result in result: |
550 |
(node_tuple, data_tuple) = node_result |
551 |
(_, node_name) = node_tuple |
552 |
(data_status, data_node) = data_tuple |
553 |
if data_status == constants.RS_NORMAL:
|
554 |
if oob_command == constants.OOB_POWER_STATUS:
|
555 |
if data_node[constants.OOB_POWER_STATUS_POWERED]:
|
556 |
text = "powered"
|
557 |
else:
|
558 |
text = "unpowered"
|
559 |
data.append([node_name, text]) |
560 |
else:
|
561 |
# We don't expect data here, so we just say, it was successfully invoked
|
562 |
data.append([node_name, "invoked"])
|
563 |
else:
|
564 |
errs += 1
|
565 |
data.append([node_name, cli.FormatResultError(data_status)]) |
566 |
|
567 |
data = GenerateTable(separator=opts.separator, headers=headers, |
568 |
fields=["node", "status"], data=data) |
569 |
|
570 |
for line in data: |
571 |
ToStdout(line) |
572 |
|
573 |
if errs:
|
574 |
return constants.EXIT_FAILURE
|
575 |
else:
|
576 |
return constants.EXIT_SUCCESS
|
577 |
|
578 |
|
579 |
def Health(opts, args): |
580 |
"""Show health of a node using OOB.
|
581 |
|
582 |
@param opts: the command line options selected by the user
|
583 |
@type args: list
|
584 |
@param args: should contain only one element, the name of
|
585 |
the node to be removed
|
586 |
@rtype: int
|
587 |
@return: the desired exit code
|
588 |
|
589 |
"""
|
590 |
op = opcodes.OpOobCommand(node_names=args, command=constants.OOB_HEALTH, |
591 |
timeout=opts.oob_timeout) |
592 |
result = SubmitOpCode(op, opts=opts) |
593 |
|
594 |
if opts.no_headers:
|
595 |
headers = None
|
596 |
else:
|
597 |
headers = {"node": "Node", "status": "Status"} |
598 |
|
599 |
errs = 0
|
600 |
data = [] |
601 |
for node_result in result: |
602 |
(node_tuple, data_tuple) = node_result |
603 |
(_, node_name) = node_tuple |
604 |
(data_status, data_node) = data_tuple |
605 |
if data_status == constants.RS_NORMAL:
|
606 |
data.append([node_name, "%s=%s" % tuple(data_node[0])]) |
607 |
for item, status in data_node[1:]: |
608 |
data.append(["", "%s=%s" % (item, status)]) |
609 |
else:
|
610 |
errs += 1
|
611 |
data.append([node_name, cli.FormatResultError(data_status)]) |
612 |
|
613 |
data = GenerateTable(separator=opts.separator, headers=headers, |
614 |
fields=["node", "status"], data=data) |
615 |
|
616 |
for line in data: |
617 |
ToStdout(line) |
618 |
|
619 |
if errs:
|
620 |
return constants.EXIT_FAILURE
|
621 |
else:
|
622 |
return constants.EXIT_SUCCESS
|
623 |
|
624 |
|
625 |
def ListVolumes(opts, args): |
626 |
"""List logical volumes on node(s).
|
627 |
|
628 |
@param opts: the command line options selected by the user
|
629 |
@type args: list
|
630 |
@param args: should either be an empty list, in which case
|
631 |
we list data for all nodes, or contain a list of nodes
|
632 |
to display data only for those
|
633 |
@rtype: int
|
634 |
@return: the desired exit code
|
635 |
|
636 |
"""
|
637 |
selected_fields = ParseFields(opts.output, _LIST_VOL_DEF_FIELDS) |
638 |
|
639 |
op = opcodes.OpNodeQueryvols(nodes=args, output_fields=selected_fields) |
640 |
output = SubmitOpCode(op, opts=opts) |
641 |
|
642 |
if not opts.no_headers: |
643 |
headers = {"node": "Node", "phys": "PhysDev", |
644 |
"vg": "VG", "name": "Name", |
645 |
"size": "Size", "instance": "Instance"} |
646 |
else:
|
647 |
headers = None
|
648 |
|
649 |
unitfields = ["size"]
|
650 |
|
651 |
numfields = ["size"]
|
652 |
|
653 |
data = GenerateTable(separator=opts.separator, headers=headers, |
654 |
fields=selected_fields, unitfields=unitfields, |
655 |
numfields=numfields, data=output, units=opts.units) |
656 |
|
657 |
for line in data: |
658 |
ToStdout(line) |
659 |
|
660 |
return 0 |
661 |
|
662 |
|
663 |
def ListStorage(opts, args): |
664 |
"""List physical volumes on node(s).
|
665 |
|
666 |
@param opts: the command line options selected by the user
|
667 |
@type args: list
|
668 |
@param args: should either be an empty list, in which case
|
669 |
we list data for all nodes, or contain a list of nodes
|
670 |
to display data only for those
|
671 |
@rtype: int
|
672 |
@return: the desired exit code
|
673 |
|
674 |
"""
|
675 |
# TODO: Default to ST_FILE if LVM is disabled on the cluster
|
676 |
if opts.user_storage_type is None: |
677 |
opts.user_storage_type = constants.ST_LVM_PV |
678 |
|
679 |
storage_type = ConvertStorageType(opts.user_storage_type) |
680 |
|
681 |
selected_fields = ParseFields(opts.output, _LIST_STOR_DEF_FIELDS) |
682 |
|
683 |
op = opcodes.OpNodeQueryStorage(nodes=args, |
684 |
storage_type=storage_type, |
685 |
output_fields=selected_fields) |
686 |
output = SubmitOpCode(op, opts=opts) |
687 |
|
688 |
if not opts.no_headers: |
689 |
headers = { |
690 |
constants.SF_NODE: "Node",
|
691 |
constants.SF_TYPE: "Type",
|
692 |
constants.SF_NAME: "Name",
|
693 |
constants.SF_SIZE: "Size",
|
694 |
constants.SF_USED: "Used",
|
695 |
constants.SF_FREE: "Free",
|
696 |
constants.SF_ALLOCATABLE: "Allocatable",
|
697 |
} |
698 |
else:
|
699 |
headers = None
|
700 |
|
701 |
unitfields = [constants.SF_SIZE, constants.SF_USED, constants.SF_FREE] |
702 |
numfields = [constants.SF_SIZE, constants.SF_USED, constants.SF_FREE] |
703 |
|
704 |
# change raw values to nicer strings
|
705 |
for row in output: |
706 |
for idx, field in enumerate(selected_fields): |
707 |
val = row[idx] |
708 |
if field == constants.SF_ALLOCATABLE:
|
709 |
if val:
|
710 |
val = "Y"
|
711 |
else:
|
712 |
val = "N"
|
713 |
row[idx] = str(val)
|
714 |
|
715 |
data = GenerateTable(separator=opts.separator, headers=headers, |
716 |
fields=selected_fields, unitfields=unitfields, |
717 |
numfields=numfields, data=output, units=opts.units) |
718 |
|
719 |
for line in data: |
720 |
ToStdout(line) |
721 |
|
722 |
return 0 |
723 |
|
724 |
|
725 |
def ModifyStorage(opts, args): |
726 |
"""Modify storage volume on a node.
|
727 |
|
728 |
@param opts: the command line options selected by the user
|
729 |
@type args: list
|
730 |
@param args: should contain 3 items: node name, storage type and volume name
|
731 |
@rtype: int
|
732 |
@return: the desired exit code
|
733 |
|
734 |
"""
|
735 |
(node_name, user_storage_type, volume_name) = args |
736 |
|
737 |
storage_type = ConvertStorageType(user_storage_type) |
738 |
|
739 |
changes = {} |
740 |
|
741 |
if opts.allocatable is not None: |
742 |
changes[constants.SF_ALLOCATABLE] = opts.allocatable |
743 |
|
744 |
if changes:
|
745 |
op = opcodes.OpNodeModifyStorage(node_name=node_name, |
746 |
storage_type=storage_type, |
747 |
name=volume_name, |
748 |
changes=changes) |
749 |
SubmitOpCode(op, opts=opts) |
750 |
else:
|
751 |
ToStderr("No changes to perform, exiting.")
|
752 |
|
753 |
|
754 |
def RepairStorage(opts, args): |
755 |
"""Repairs a storage volume on a node.
|
756 |
|
757 |
@param opts: the command line options selected by the user
|
758 |
@type args: list
|
759 |
@param args: should contain 3 items: node name, storage type and volume name
|
760 |
@rtype: int
|
761 |
@return: the desired exit code
|
762 |
|
763 |
"""
|
764 |
(node_name, user_storage_type, volume_name) = args |
765 |
|
766 |
storage_type = ConvertStorageType(user_storage_type) |
767 |
|
768 |
op = opcodes.OpRepairNodeStorage(node_name=node_name, |
769 |
storage_type=storage_type, |
770 |
name=volume_name, |
771 |
ignore_consistency=opts.ignore_consistency) |
772 |
SubmitOpCode(op, opts=opts) |
773 |
|
774 |
|
775 |
def SetNodeParams(opts, args): |
776 |
"""Modifies a node.
|
777 |
|
778 |
@param opts: the command line options selected by the user
|
779 |
@type args: list
|
780 |
@param args: should contain only one element, the node name
|
781 |
@rtype: int
|
782 |
@return: the desired exit code
|
783 |
|
784 |
"""
|
785 |
all_changes = [opts.master_candidate, opts.drained, opts.offline, |
786 |
opts.master_capable, opts.vm_capable, opts.secondary_ip, |
787 |
opts.ndparams] |
788 |
if all_changes.count(None) == len(all_changes): |
789 |
ToStderr("Please give at least one of the parameters.")
|
790 |
return 1 |
791 |
|
792 |
op = opcodes.OpNodeSetParams(node_name=args[0],
|
793 |
master_candidate=opts.master_candidate, |
794 |
offline=opts.offline, |
795 |
drained=opts.drained, |
796 |
master_capable=opts.master_capable, |
797 |
vm_capable=opts.vm_capable, |
798 |
secondary_ip=opts.secondary_ip, |
799 |
force=opts.force, |
800 |
ndparams=opts.ndparams, |
801 |
auto_promote=opts.auto_promote, |
802 |
powered=opts.node_powered) |
803 |
|
804 |
# even if here we process the result, we allow submit only
|
805 |
result = SubmitOrSend(op, opts) |
806 |
|
807 |
if result:
|
808 |
ToStdout("Modified node %s", args[0]) |
809 |
for param, data in result: |
810 |
ToStdout(" - %-5s -> %s", param, data)
|
811 |
return 0 |
812 |
|
813 |
|
814 |
commands = { |
815 |
'add': (
|
816 |
AddNode, [ArgHost(min=1, max=1)], |
817 |
[SECONDARY_IP_OPT, READD_OPT, NOSSH_KEYCHECK_OPT, NONODE_SETUP_OPT, |
818 |
VERBOSE_OPT, NODEGROUP_OPT, PRIORITY_OPT, CAPAB_MASTER_OPT, |
819 |
CAPAB_VM_OPT, NODE_PARAMS_OPT], |
820 |
"[-s ip] [--readd] [--no-ssh-key-check] [--no-node-setup] [--verbose] "
|
821 |
" <node_name>",
|
822 |
"Add a node to the cluster"),
|
823 |
'evacuate': (
|
824 |
EvacuateNode, [ArgNode(min=1)],
|
825 |
[FORCE_OPT, IALLOCATOR_OPT, NEW_SECONDARY_OPT, EARLY_RELEASE_OPT, |
826 |
PRIORITY_OPT], |
827 |
"[-f] {-I <iallocator> | -n <dst>} <node>",
|
828 |
"Relocate the secondary instances from a node"
|
829 |
" to other nodes (only for instances with drbd disk template)"),
|
830 |
'failover': (
|
831 |
FailoverNode, ARGS_ONE_NODE, [FORCE_OPT, IGNORE_CONSIST_OPT, PRIORITY_OPT], |
832 |
"[-f] <node>",
|
833 |
"Stops the primary instances on a node and start them on their"
|
834 |
" secondary node (only for instances with drbd disk template)"),
|
835 |
'migrate': (
|
836 |
MigrateNode, ARGS_ONE_NODE, |
837 |
[FORCE_OPT, NONLIVE_OPT, MIGRATION_MODE_OPT, PRIORITY_OPT], |
838 |
"[-f] <node>",
|
839 |
"Migrate all the primary instance on a node away from it"
|
840 |
" (only for instances of type drbd)"),
|
841 |
'info': (
|
842 |
ShowNodeConfig, ARGS_MANY_NODES, [], |
843 |
"[<node_name>...]", "Show information about the node(s)"), |
844 |
'list': (
|
845 |
ListNodes, ARGS_MANY_NODES, |
846 |
[NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT], |
847 |
"[nodes...]",
|
848 |
"Lists the nodes in the cluster. The available fields can be shown using"
|
849 |
" the \"list-fields\" command (see the man page for details)."
|
850 |
" The default field list is (in order): %s." %
|
851 |
utils.CommaJoin(_LIST_DEF_FIELDS)), |
852 |
"list-fields": (
|
853 |
ListNodeFields, [ArgUnknown()], |
854 |
[NOHDR_OPT, SEP_OPT], |
855 |
"[fields...]",
|
856 |
"Lists all available fields for nodes"),
|
857 |
'modify': (
|
858 |
SetNodeParams, ARGS_ONE_NODE, |
859 |
[FORCE_OPT, SUBMIT_OPT, MC_OPT, DRAINED_OPT, OFFLINE_OPT, |
860 |
CAPAB_MASTER_OPT, CAPAB_VM_OPT, SECONDARY_IP_OPT, |
861 |
AUTO_PROMOTE_OPT, DRY_RUN_OPT, PRIORITY_OPT, NODE_PARAMS_OPT, |
862 |
NODE_POWERED_OPT], |
863 |
"<node_name>", "Alters the parameters of a node"), |
864 |
'powercycle': (
|
865 |
PowercycleNode, ARGS_ONE_NODE, |
866 |
[FORCE_OPT, CONFIRM_OPT, DRY_RUN_OPT, PRIORITY_OPT], |
867 |
"<node_name>", "Tries to forcefully powercycle a node"), |
868 |
'power': (
|
869 |
PowerNode, |
870 |
[ArgChoice(min=1, max=1, choices=_LIST_POWER_COMMANDS), |
871 |
ArgNode()], |
872 |
[SUBMIT_OPT, AUTO_PROMOTE_OPT, PRIORITY_OPT, IGNORE_STATUS_OPT, |
873 |
FORCE_MASTER_OPT, FORCE_OPT, NOHDR_OPT, SEP_OPT, ALL_OPT, |
874 |
OOB_TIMEOUT_OPT], |
875 |
"on|off|cycle|status [nodes...]",
|
876 |
"Change power state of node by calling out-of-band helper."),
|
877 |
'remove': (
|
878 |
RemoveNode, ARGS_ONE_NODE, [DRY_RUN_OPT, PRIORITY_OPT], |
879 |
"<node_name>", "Removes a node from the cluster"), |
880 |
'volumes': (
|
881 |
ListVolumes, [ArgNode()], |
882 |
[NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT, PRIORITY_OPT], |
883 |
"[<node_name>...]", "List logical volumes on node(s)"), |
884 |
'list-storage': (
|
885 |
ListStorage, ARGS_MANY_NODES, |
886 |
[NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT, _STORAGE_TYPE_OPT, |
887 |
PRIORITY_OPT], |
888 |
"[<node_name>...]", "List physical volumes on node(s). The available" |
889 |
" fields are (see the man page for details): %s." %
|
890 |
(utils.CommaJoin(_LIST_STOR_HEADERS))), |
891 |
'modify-storage': (
|
892 |
ModifyStorage, |
893 |
[ArgNode(min=1, max=1), |
894 |
ArgChoice(min=1, max=1, choices=_MODIFIABLE_STORAGE_TYPES), |
895 |
ArgFile(min=1, max=1)], |
896 |
[ALLOCATABLE_OPT, DRY_RUN_OPT, PRIORITY_OPT], |
897 |
"<node_name> <storage_type> <name>", "Modify storage volume on a node"), |
898 |
'repair-storage': (
|
899 |
RepairStorage, |
900 |
[ArgNode(min=1, max=1), |
901 |
ArgChoice(min=1, max=1, choices=_REPAIRABLE_STORAGE_TYPES), |
902 |
ArgFile(min=1, max=1)], |
903 |
[IGNORE_CONSIST_OPT, DRY_RUN_OPT, PRIORITY_OPT], |
904 |
"<node_name> <storage_type> <name>",
|
905 |
"Repairs a storage volume on a node"),
|
906 |
'list-tags': (
|
907 |
ListTags, ARGS_ONE_NODE, [], |
908 |
"<node_name>", "List the tags of the given node"), |
909 |
'add-tags': (
|
910 |
AddTags, [ArgNode(min=1, max=1), ArgUnknown()], [TAG_SRC_OPT, PRIORITY_OPT], |
911 |
"<node_name> tag...", "Add tags to the given node"), |
912 |
'remove-tags': (
|
913 |
RemoveTags, [ArgNode(min=1, max=1), ArgUnknown()], |
914 |
[TAG_SRC_OPT, PRIORITY_OPT], |
915 |
"<node_name> tag...", "Remove tags from the given node"), |
916 |
"health": (
|
917 |
Health, ARGS_MANY_NODES, |
918 |
[NOHDR_OPT, SEP_OPT, SUBMIT_OPT, PRIORITY_OPT, OOB_TIMEOUT_OPT], |
919 |
"[<node_name>...]", "List health of node(s) using out-of-band"), |
920 |
} |
921 |
|
922 |
|
923 |
def Main(): |
924 |
return GenericMain(commands, override={"tag_type": constants.TAG_NODE}) |