4 # Copyright (C) 2006, 2007, 2008, 2009, 2010 Google Inc.
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.
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.
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
21 """Node related commands"""
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
29 from ganeti.cli import *
30 from ganeti import bootstrap
31 from ganeti import opcodes
32 from ganeti import utils
33 from ganeti import constants
34 from ganeti import errors
35 from ganeti import netutils
38 #: default list of field for L{ListNodes}
40 "name", "dtotal", "dfree",
41 "mtotal", "mnode", "mfree",
42 "pinst_cnt", "sinst_cnt",
46 #: Default field list for L{ListVolumes}
47 _LIST_VOL_DEF_FIELDS = ["node", "phys", "vg", "name", "size", "instance"]
50 #: default list of field for L{ListStorage}
51 _LIST_STOR_DEF_FIELDS = [
58 constants.SF_ALLOCATABLE,
62 #: headers (and full field list) for L{ListStorage}
63 _LIST_STOR_HEADERS = {
64 constants.SF_NODE: "Node",
65 constants.SF_TYPE: "Type",
66 constants.SF_NAME: "Name",
67 constants.SF_SIZE: "Size",
68 constants.SF_USED: "Used",
69 constants.SF_FREE: "Free",
70 constants.SF_ALLOCATABLE: "Allocatable",
74 #: User-facing storage unit types
75 _USER_STORAGE_TYPE = {
76 constants.ST_FILE: "file",
77 constants.ST_LVM_PV: "lvm-pv",
78 constants.ST_LVM_VG: "lvm-vg",
82 cli_option("-t", "--storage-type",
83 dest="user_storage_type",
84 choices=_USER_STORAGE_TYPE.keys(),
86 metavar="STORAGE_TYPE",
87 help=("Storage type (%s)" %
88 utils.CommaJoin(_USER_STORAGE_TYPE.keys())))
90 _REPAIRABLE_STORAGE_TYPES = \
91 [st for st, so in constants.VALID_STORAGE_OPERATIONS.iteritems()
92 if constants.SO_FIX_CONSISTENCY in so]
94 _MODIFIABLE_STORAGE_TYPES = constants.MODIFIABLE_STORAGE_FIELDS.keys()
97 NONODE_SETUP_OPT = cli_option("--no-node-setup", default=True,
98 action="store_false", dest="node_setup",
99 help=("Do not make initial SSH setup on remote"
100 " node (needs to be done manually)"))
103 def ConvertStorageType(user_storage_type):
104 """Converts a user storage type to its internal name.
108 return _USER_STORAGE_TYPE[user_storage_type]
110 raise errors.OpPrereqError("Unknown storage type: %s" % user_storage_type,
114 def _RunSetupSSH(options, nodes):
115 """Wrapper around utils.RunCmd to call setup-ssh
117 @param options: The command line options
118 @param nodes: The nodes to setup
121 cmd = [constants.SETUP_SSH]
123 # Pass --debug|--verbose to the external script if set on our invocation
124 # --debug overrides --verbose
126 cmd.append("--debug")
127 elif options.verbose:
128 cmd.append("--verbose")
129 if not options.ssh_key_check:
130 cmd.append("--no-ssh-key-check")
134 result = utils.RunCmd(cmd, interactive=True)
137 errmsg = ("Command '%s' failed with exit code %s; output %r" %
138 (result.cmd, result.exit_code, result.output))
139 raise errors.OpExecError(errmsg)
143 def AddNode(opts, args):
144 """Add a node to the cluster.
146 @param opts: the command line options selected by the user
148 @param args: should contain only one element, the new node name
150 @return: the desired exit code
154 node = netutils.GetHostname(name=args[0]).name
158 output = cl.QueryNodes(names=[node], fields=['name', 'sip'],
160 node_exists, sip = output[0]
161 except (errors.OpPrereqError, errors.OpExecError):
167 ToStderr("Node %s not in the cluster"
168 " - please retry without '--readd'", node)
172 ToStderr("Node %s already in the cluster (as %s)"
173 " - please retry with '--readd'", node, node_exists)
175 sip = opts.secondary_ip
177 # read the cluster name from the master
178 output = cl.QueryConfigValues(['cluster_name'])
179 cluster_name = output[0]
181 if not readd and opts.node_setup:
182 ToStderr("-- WARNING -- \n"
183 "Performing this operation is going to replace the ssh daemon"
185 "on the target machine (%s) with the ones of the"
187 "and grant full intra-cluster ssh root access to/from it\n", node)
190 _RunSetupSSH(opts, [node])
192 bootstrap.SetupNodeDaemon(cluster_name, node, opts.ssh_key_check)
194 op = opcodes.OpAddNode(node_name=args[0], secondary_ip=sip,
195 readd=opts.readd, group=opts.nodegroup,
196 vm_capable=opts.vm_capable, ndparams=opts.ndparams,
197 master_capable=opts.master_capable)
198 SubmitOpCode(op, opts=opts)
201 def ListNodes(opts, args):
202 """List nodes and their properties.
204 @param opts: the command line options selected by the user
206 @param args: nodes to list, or empty for all
208 @return: the desired exit code
211 selected_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
213 fmtoverride = dict.fromkeys(["pinst_list", "sinst_list", "tags"],
216 return GenericList(constants.QR_NODE, selected_fields, args, opts.units,
217 opts.separator, not opts.no_headers,
218 format_override=fmtoverride)
221 def ListNodeFields(opts, args):
224 @param opts: the command line options selected by the user
226 @param args: fields to list, or empty for all
228 @return: the desired exit code
231 return GenericListFields(constants.QR_NODE, args, opts.separator,
235 def EvacuateNode(opts, args):
236 """Relocate all secondary instance from a node.
238 @param opts: the command line options selected by the user
240 @param args: should be an empty list
242 @return: the desired exit code
248 dst_node = opts.dst_node
249 iallocator = opts.iallocator
251 op = opcodes.OpNodeEvacuationStrategy(nodes=args,
252 iallocator=iallocator,
253 remote_node=dst_node)
255 result = SubmitOpCode(op, cl=cl, opts=opts)
257 # no instances to migrate
258 ToStderr("No secondary instances on node(s) %s, exiting.",
259 utils.CommaJoin(args))
260 return constants.EXIT_SUCCESS
262 if not force and not AskUser("Relocate instance(s) %s from node(s) %s?" %
263 (",".join("'%s'" % name[0] for name in result),
264 utils.CommaJoin(args))):
265 return constants.EXIT_CONFIRMATION
267 jex = JobExecutor(cl=cl, opts=opts)
271 ToStdout("Will relocate instance %s to node %s", iname, node)
272 op = opcodes.OpReplaceDisks(instance_name=iname,
273 remote_node=node, disks=[],
274 mode=constants.REPLACE_DISK_CHG,
275 early_release=opts.early_release)
276 jex.QueueJob(iname, op)
277 results = jex.GetResults()
278 bad_cnt = len([row for row in results if not row[0]])
280 ToStdout("All %d instance(s) failed over successfully.", len(results))
281 rcode = constants.EXIT_SUCCESS
283 ToStdout("There were errors during the failover:\n"
284 "%d error(s) out of %d instance(s).", bad_cnt, len(results))
285 rcode = constants.EXIT_FAILURE
289 def FailoverNode(opts, args):
290 """Failover all primary instance on a node.
292 @param opts: the command line options selected by the user
294 @param args: should be an empty list
296 @return: the desired exit code
301 selected_fields = ["name", "pinst_list"]
303 # these fields are static data anyway, so it doesn't matter, but
304 # locking=True should be safer
305 result = cl.QueryNodes(names=args, fields=selected_fields,
307 node, pinst = result[0]
310 ToStderr("No primary instances on node %s, exiting.", node)
313 pinst = utils.NiceSort(pinst)
317 if not force and not AskUser("Fail over instance(s) %s?" %
318 (",".join("'%s'" % name for name in pinst))):
321 jex = JobExecutor(cl=cl, opts=opts)
323 op = opcodes.OpFailoverInstance(instance_name=iname,
324 ignore_consistency=opts.ignore_consistency)
325 jex.QueueJob(iname, op)
326 results = jex.GetResults()
327 bad_cnt = len([row for row in results if not row[0]])
329 ToStdout("All %d instance(s) failed over successfully.", len(results))
331 ToStdout("There were errors during the failover:\n"
332 "%d error(s) out of %d instance(s).", bad_cnt, len(results))
336 def MigrateNode(opts, args):
337 """Migrate all primary instance on a node.
342 selected_fields = ["name", "pinst_list"]
344 result = cl.QueryNodes(names=args, fields=selected_fields, use_locking=False)
345 node, pinst = result[0]
348 ToStdout("No primary instances on node %s, exiting." % node)
351 pinst = utils.NiceSort(pinst)
353 if not force and not AskUser("Migrate instance(s) %s?" %
354 (",".join("'%s'" % name for name in pinst))):
357 # this should be removed once --non-live is deprecated
358 if not opts.live and opts.migration_mode is not None:
359 raise errors.OpPrereqError("Only one of the --non-live and "
360 "--migration-mode options can be passed",
362 if not opts.live: # --non-live passed
363 mode = constants.HT_MIGRATION_NONLIVE
365 mode = opts.migration_mode
366 op = opcodes.OpMigrateNode(node_name=args[0], mode=mode)
367 SubmitOpCode(op, cl=cl, opts=opts)
370 def ShowNodeConfig(opts, args):
371 """Show node information.
373 @param opts: the command line options selected by the user
375 @param args: should either be an empty list, in which case
376 we show information about all nodes, or should contain
377 a list of nodes to be queried for information
379 @return: the desired exit code
383 result = cl.QueryNodes(fields=["name", "pip", "sip",
384 "pinst_list", "sinst_list",
385 "master_candidate", "drained", "offline",
386 "master_capable", "vm_capable"],
387 names=args, use_locking=False)
389 for (name, primary_ip, secondary_ip, pinst, sinst,
390 is_mc, drained, offline, master_capable, vm_capable) in result:
391 ToStdout("Node name: %s", name)
392 ToStdout(" primary ip: %s", primary_ip)
393 ToStdout(" secondary ip: %s", secondary_ip)
394 ToStdout(" master candidate: %s", is_mc)
395 ToStdout(" drained: %s", drained)
396 ToStdout(" offline: %s", offline)
397 ToStdout(" master_capable: %s", master_capable)
398 ToStdout(" vm_capable: %s", vm_capable)
401 ToStdout(" primary for instances:")
402 for iname in utils.NiceSort(pinst):
403 ToStdout(" - %s", iname)
405 ToStdout(" primary for no instances")
407 ToStdout(" secondary for instances:")
408 for iname in utils.NiceSort(sinst):
409 ToStdout(" - %s", iname)
411 ToStdout(" secondary for no instances")
416 def RemoveNode(opts, args):
417 """Remove a node from the cluster.
419 @param opts: the command line options selected by the user
421 @param args: should contain only one element, the name of
422 the node to be removed
424 @return: the desired exit code
427 op = opcodes.OpRemoveNode(node_name=args[0])
428 SubmitOpCode(op, opts=opts)
432 def PowercycleNode(opts, args):
433 """Remove a node from the cluster.
435 @param opts: the command line options selected by the user
437 @param args: should contain only one element, the name of
438 the node to be removed
440 @return: the desired exit code
444 if (not opts.confirm and
445 not AskUser("Are you sure you want to hard powercycle node %s?" % node)):
448 op = opcodes.OpPowercycleNode(node_name=node, force=opts.force)
449 result = SubmitOpCode(op, opts=opts)
455 def ListVolumes(opts, args):
456 """List logical volumes on node(s).
458 @param opts: the command line options selected by the user
460 @param args: should either be an empty list, in which case
461 we list data for all nodes, or contain a list of nodes
462 to display data only for those
464 @return: the desired exit code
467 selected_fields = ParseFields(opts.output, _LIST_VOL_DEF_FIELDS)
469 op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields)
470 output = SubmitOpCode(op, opts=opts)
472 if not opts.no_headers:
473 headers = {"node": "Node", "phys": "PhysDev",
474 "vg": "VG", "name": "Name",
475 "size": "Size", "instance": "Instance"}
479 unitfields = ["size"]
483 data = GenerateTable(separator=opts.separator, headers=headers,
484 fields=selected_fields, unitfields=unitfields,
485 numfields=numfields, data=output, units=opts.units)
493 def ListStorage(opts, args):
494 """List physical volumes on node(s).
496 @param opts: the command line options selected by the user
498 @param args: should either be an empty list, in which case
499 we list data for all nodes, or contain a list of nodes
500 to display data only for those
502 @return: the desired exit code
505 # TODO: Default to ST_FILE if LVM is disabled on the cluster
506 if opts.user_storage_type is None:
507 opts.user_storage_type = constants.ST_LVM_PV
509 storage_type = ConvertStorageType(opts.user_storage_type)
511 selected_fields = ParseFields(opts.output, _LIST_STOR_DEF_FIELDS)
513 op = opcodes.OpQueryNodeStorage(nodes=args,
514 storage_type=storage_type,
515 output_fields=selected_fields)
516 output = SubmitOpCode(op, opts=opts)
518 if not opts.no_headers:
520 constants.SF_NODE: "Node",
521 constants.SF_TYPE: "Type",
522 constants.SF_NAME: "Name",
523 constants.SF_SIZE: "Size",
524 constants.SF_USED: "Used",
525 constants.SF_FREE: "Free",
526 constants.SF_ALLOCATABLE: "Allocatable",
531 unitfields = [constants.SF_SIZE, constants.SF_USED, constants.SF_FREE]
532 numfields = [constants.SF_SIZE, constants.SF_USED, constants.SF_FREE]
534 # change raw values to nicer strings
536 for idx, field in enumerate(selected_fields):
538 if field == constants.SF_ALLOCATABLE:
545 data = GenerateTable(separator=opts.separator, headers=headers,
546 fields=selected_fields, unitfields=unitfields,
547 numfields=numfields, data=output, units=opts.units)
555 def ModifyStorage(opts, args):
556 """Modify storage volume on a node.
558 @param opts: the command line options selected by the user
560 @param args: should contain 3 items: node name, storage type and volume name
562 @return: the desired exit code
565 (node_name, user_storage_type, volume_name) = args
567 storage_type = ConvertStorageType(user_storage_type)
571 if opts.allocatable is not None:
572 changes[constants.SF_ALLOCATABLE] = opts.allocatable
575 op = opcodes.OpModifyNodeStorage(node_name=node_name,
576 storage_type=storage_type,
579 SubmitOpCode(op, opts=opts)
581 ToStderr("No changes to perform, exiting.")
584 def RepairStorage(opts, args):
585 """Repairs a storage volume on a node.
587 @param opts: the command line options selected by the user
589 @param args: should contain 3 items: node name, storage type and volume name
591 @return: the desired exit code
594 (node_name, user_storage_type, volume_name) = args
596 storage_type = ConvertStorageType(user_storage_type)
598 op = opcodes.OpRepairNodeStorage(node_name=node_name,
599 storage_type=storage_type,
601 ignore_consistency=opts.ignore_consistency)
602 SubmitOpCode(op, opts=opts)
605 def SetNodeParams(opts, args):
608 @param opts: the command line options selected by the user
610 @param args: should contain only one element, the node name
612 @return: the desired exit code
615 all_changes = [opts.master_candidate, opts.drained, opts.offline,
616 opts.master_capable, opts.vm_capable, opts.secondary_ip,
618 if all_changes.count(None) == len(all_changes):
619 ToStderr("Please give at least one of the parameters.")
622 op = opcodes.OpSetNodeParams(node_name=args[0],
623 master_candidate=opts.master_candidate,
624 offline=opts.offline,
625 drained=opts.drained,
626 master_capable=opts.master_capable,
627 vm_capable=opts.vm_capable,
628 secondary_ip=opts.secondary_ip,
630 ndparams=opts.ndparams,
631 auto_promote=opts.auto_promote)
633 # even if here we process the result, we allow submit only
634 result = SubmitOrSend(op, opts)
637 ToStdout("Modified node %s", args[0])
638 for param, data in result:
639 ToStdout(" - %-5s -> %s", param, data)
645 AddNode, [ArgHost(min=1, max=1)],
646 [SECONDARY_IP_OPT, READD_OPT, NOSSH_KEYCHECK_OPT, NONODE_SETUP_OPT,
647 VERBOSE_OPT, NODEGROUP_OPT, PRIORITY_OPT, CAPAB_MASTER_OPT,
648 CAPAB_VM_OPT, NODE_PARAMS_OPT],
649 "[-s ip] [--readd] [--no-ssh-key-check] [--no-node-setup] [--verbose] "
651 "Add a node to the cluster"),
653 EvacuateNode, [ArgNode(min=1)],
654 [FORCE_OPT, IALLOCATOR_OPT, NEW_SECONDARY_OPT, EARLY_RELEASE_OPT,
656 "[-f] {-I <iallocator> | -n <dst>} <node>",
657 "Relocate the secondary instances from a node"
658 " to other nodes (only for instances with drbd disk template)"),
660 FailoverNode, ARGS_ONE_NODE, [FORCE_OPT, IGNORE_CONSIST_OPT, PRIORITY_OPT],
662 "Stops the primary instances on a node and start them on their"
663 " secondary node (only for instances with drbd disk template)"),
665 MigrateNode, ARGS_ONE_NODE,
666 [FORCE_OPT, NONLIVE_OPT, MIGRATION_MODE_OPT, PRIORITY_OPT],
668 "Migrate all the primary instance on a node away from it"
669 " (only for instances of type drbd)"),
671 ShowNodeConfig, ARGS_MANY_NODES, [],
672 "[<node_name>...]", "Show information about the node(s)"),
674 ListNodes, ARGS_MANY_NODES,
675 [NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
677 "Lists the nodes in the cluster. The available fields can be shown using"
678 " the \"list-fields\" command (see the man page for details)."
679 " The default field list is (in order): %s." %
680 utils.CommaJoin(_LIST_DEF_FIELDS)),
682 ListNodeFields, [ArgUnknown()],
683 [NOHDR_OPT, SEP_OPT],
685 "Lists all available fields for nodes"),
687 SetNodeParams, ARGS_ONE_NODE,
688 [FORCE_OPT, SUBMIT_OPT, MC_OPT, DRAINED_OPT, OFFLINE_OPT,
689 CAPAB_MASTER_OPT, CAPAB_VM_OPT, SECONDARY_IP_OPT,
690 AUTO_PROMOTE_OPT, DRY_RUN_OPT, PRIORITY_OPT, NODE_PARAMS_OPT],
691 "<node_name>", "Alters the parameters of a node"),
693 PowercycleNode, ARGS_ONE_NODE,
694 [FORCE_OPT, CONFIRM_OPT, DRY_RUN_OPT, PRIORITY_OPT],
695 "<node_name>", "Tries to forcefully powercycle a node"),
697 RemoveNode, ARGS_ONE_NODE, [DRY_RUN_OPT, PRIORITY_OPT],
698 "<node_name>", "Removes a node from the cluster"),
700 ListVolumes, [ArgNode()],
701 [NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT, PRIORITY_OPT],
702 "[<node_name>...]", "List logical volumes on node(s)"),
704 ListStorage, ARGS_MANY_NODES,
705 [NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT, _STORAGE_TYPE_OPT,
707 "[<node_name>...]", "List physical volumes on node(s). The available"
708 " fields are (see the man page for details): %s." %
709 (utils.CommaJoin(_LIST_STOR_HEADERS))),
712 [ArgNode(min=1, max=1),
713 ArgChoice(min=1, max=1, choices=_MODIFIABLE_STORAGE_TYPES),
714 ArgFile(min=1, max=1)],
715 [ALLOCATABLE_OPT, DRY_RUN_OPT, PRIORITY_OPT],
716 "<node_name> <storage_type> <name>", "Modify storage volume on a node"),
719 [ArgNode(min=1, max=1),
720 ArgChoice(min=1, max=1, choices=_REPAIRABLE_STORAGE_TYPES),
721 ArgFile(min=1, max=1)],
722 [IGNORE_CONSIST_OPT, DRY_RUN_OPT, PRIORITY_OPT],
723 "<node_name> <storage_type> <name>",
724 "Repairs a storage volume on a node"),
726 ListTags, ARGS_ONE_NODE, [],
727 "<node_name>", "List the tags of the given node"),
729 AddTags, [ArgNode(min=1, max=1), ArgUnknown()], [TAG_SRC_OPT, PRIORITY_OPT],
730 "<node_name> tag...", "Add tags to the given node"),
732 RemoveTags, [ArgNode(min=1, max=1), ArgUnknown()],
733 [TAG_SRC_OPT, PRIORITY_OPT],
734 "<node_name> tag...", "Remove tags from the given node"),
739 return GenericMain(commands, override={"tag_type": constants.TAG_NODE})