4 # Copyright (C) 2006, 2007 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 """Cluster 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-cluster
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
46 def InitCluster(opts, args):
47 """Initialize the cluster.
49 @param opts: the command line options selected by the user
51 @param args: should contain only one element, the desired
54 @return: the desired exit code
57 if not opts.lvm_storage and opts.vg_name:
58 ToStderr("Options --no-lvm-storage and --vg-name conflict.")
61 vg_name = opts.vg_name
62 if opts.lvm_storage and not opts.vg_name:
63 vg_name = constants.DEFAULT_VG
65 hvlist = opts.enabled_hypervisors
67 hvlist = constants.DEFAULT_ENABLED_HYPERVISOR
68 hvlist = hvlist.split(",")
70 hvparams = dict(opts.hvparams)
71 beparams = opts.beparams
72 nicparams = opts.nicparams
74 # prepare beparams dict
75 beparams = objects.FillDict(constants.BEC_DEFAULTS, beparams)
76 utils.ForceDictType(beparams, constants.BES_PARAMETER_TYPES)
78 # prepare nicparams dict
79 nicparams = objects.FillDict(constants.NICC_DEFAULTS, nicparams)
80 utils.ForceDictType(nicparams, constants.NICS_PARAMETER_TYPES)
82 # prepare hvparams dict
83 for hv in constants.HYPER_TYPES:
84 if hv not in hvparams:
86 hvparams[hv] = objects.FillDict(constants.HVC_DEFAULTS[hv], hvparams[hv])
87 utils.ForceDictType(hvparams[hv], constants.HVS_PARAMETER_TYPES)
89 if opts.candidate_pool_size is None:
90 opts.candidate_pool_size = constants.MASTER_POOL_SIZE_DEFAULT
92 if opts.mac_prefix is None:
93 opts.mac_prefix = constants.DEFAULT_MAC_PREFIX
95 uid_pool = opts.uid_pool
96 if uid_pool is not None:
97 uid_pool = uidpool.ParseUidPool(uid_pool)
99 bootstrap.InitCluster(cluster_name=args[0],
100 secondary_ip=opts.secondary_ip,
102 mac_prefix=opts.mac_prefix,
103 master_netdev=opts.master_netdev,
104 file_storage_dir=opts.file_storage_dir,
105 enabled_hypervisors=hvlist,
109 candidate_pool_size=opts.candidate_pool_size,
110 modify_etc_hosts=opts.modify_etc_hosts,
111 modify_ssh_setup=opts.modify_ssh_setup,
112 maintain_node_health=opts.maintain_node_health,
115 op = opcodes.OpPostInitCluster()
116 SubmitOpCode(op, opts=opts)
121 def DestroyCluster(opts, args):
122 """Destroy the cluster.
124 @param opts: the command line options selected by the user
126 @param args: should be an empty list
128 @return: the desired exit code
131 if not opts.yes_do_it:
132 ToStderr("Destroying a cluster is irreversible. If you really want"
133 " destroy this cluster, supply the --yes-do-it option.")
136 op = opcodes.OpDestroyCluster()
137 master = SubmitOpCode(op, opts=opts)
138 # if we reached this, the opcode didn't fail; we can proceed to
139 # shutdown all the daemons
140 bootstrap.FinalizeClusterDestroy(master)
144 def RenameCluster(opts, args):
145 """Rename the cluster.
147 @param opts: the command line options selected by the user
149 @param args: should contain only one element, the new cluster name
151 @return: the desired exit code
156 usertext = ("This will rename the cluster to '%s'. If you are connected"
157 " over the network to the cluster name, the operation is very"
158 " dangerous as the IP address will be removed from the node"
159 " and the change may not go through. Continue?") % name
160 if not AskUser(usertext):
163 op = opcodes.OpRenameCluster(name=name)
164 SubmitOpCode(op, opts=opts)
168 def RedistributeConfig(opts, args):
169 """Forces push of the cluster configuration.
171 @param opts: the command line options selected by the user
173 @param args: empty list
175 @return: the desired exit code
178 op = opcodes.OpRedistributeConfig()
179 SubmitOrSend(op, opts)
183 def ShowClusterVersion(opts, args):
184 """Write version of ganeti software to the standard output.
186 @param opts: the command line options selected by the user
188 @param args: should be an empty list
190 @return: the desired exit code
194 result = cl.QueryClusterInfo()
195 ToStdout("Software version: %s", result["software_version"])
196 ToStdout("Internode protocol: %s", result["protocol_version"])
197 ToStdout("Configuration format: %s", result["config_version"])
198 ToStdout("OS api version: %s", result["os_api_version"])
199 ToStdout("Export interface: %s", result["export_version"])
203 def ShowClusterMaster(opts, args):
204 """Write name of master node to the standard output.
206 @param opts: the command line options selected by the user
208 @param args: should be an empty list
210 @return: the desired exit code
213 master = bootstrap.GetMaster()
218 def _PrintGroupedParams(paramsdict, level=1):
219 """Print Grouped parameters (be, nic, disk) by group.
221 @type paramsdict: dict of dicts
222 @param paramsdict: {group: {param: value, ...}, ...}
224 @param level: Level of indention
228 for item, val in paramsdict.items():
229 if isinstance(val, dict):
230 ToStdout("%s- %s:", indent, item)
231 _PrintGroupedParams(val, level=level + 1)
233 ToStdout("%s %s: %s", indent, item, val)
236 def ShowClusterConfig(opts, args):
237 """Shows cluster information.
239 @param opts: the command line options selected by the user
241 @param args: should be an empty list
243 @return: the desired exit code
247 result = cl.QueryClusterInfo()
249 ToStdout("Cluster name: %s", result["name"])
250 ToStdout("Cluster UUID: %s", result["uuid"])
252 ToStdout("Creation time: %s", utils.FormatTime(result["ctime"]))
253 ToStdout("Modification time: %s", utils.FormatTime(result["mtime"]))
255 ToStdout("Master node: %s", result["master"])
257 ToStdout("Architecture (this node): %s (%s)",
258 result["architecture"][0], result["architecture"][1])
261 tags = utils.CommaJoin(utils.NiceSort(result["tags"]))
265 ToStdout("Tags: %s", tags)
267 ToStdout("Default hypervisor: %s", result["default_hypervisor"])
268 ToStdout("Enabled hypervisors: %s",
269 utils.CommaJoin(result["enabled_hypervisors"]))
271 ToStdout("Hypervisor parameters:")
272 _PrintGroupedParams(result["hvparams"])
274 ToStdout("OS specific hypervisor parameters:")
275 _PrintGroupedParams(result["os_hvp"])
277 ToStdout("Cluster parameters:")
278 ToStdout(" - candidate pool size: %s", result["candidate_pool_size"])
279 ToStdout(" - master netdev: %s", result["master_netdev"])
280 ToStdout(" - lvm volume group: %s", result["volume_group_name"])
281 ToStdout(" - file storage path: %s", result["file_storage_dir"])
282 ToStdout(" - maintenance of node health: %s",
283 result["maintain_node_health"])
284 ToStdout(" - uid pool: %s", uidpool.FormatUidPool(result["uid_pool"]))
286 ToStdout("Default instance parameters:")
287 _PrintGroupedParams(result["beparams"])
289 ToStdout("Default nic parameters:")
290 _PrintGroupedParams(result["nicparams"])
295 def ClusterCopyFile(opts, args):
296 """Copy a file from master to some nodes.
298 @param opts: the command line options selected by the user
300 @param args: should contain only one element, the path of
301 the file to be copied
303 @return: the desired exit code
307 if not os.path.exists(filename):
308 raise errors.OpPrereqError("No such filename '%s'" % filename,
313 cluster_name = cl.QueryConfigValues(["cluster_name"])[0]
315 results = GetOnlineNodes(nodes=opts.nodes, cl=cl, filter_master=True,
316 secondary_ips=opts.use_replication_network)
318 srun = ssh.SshRunner(cluster_name=cluster_name)
320 if not srun.CopyFileToNode(node, filename):
321 ToStderr("Copy of file %s to node %s failed", filename, node)
326 def RunClusterCommand(opts, args):
327 """Run a command on some nodes.
329 @param opts: the command line options selected by the user
331 @param args: should contain the command to be run and its arguments
333 @return: the desired exit code
338 command = " ".join(args)
340 nodes = GetOnlineNodes(nodes=opts.nodes, cl=cl)
342 cluster_name, master_node = cl.QueryConfigValues(["cluster_name",
345 srun = ssh.SshRunner(cluster_name=cluster_name)
347 # Make sure master node is at list end
348 if master_node in nodes:
349 nodes.remove(master_node)
350 nodes.append(master_node)
353 result = srun.Run(name, "root", command)
354 ToStdout("------------------------------------------------")
355 ToStdout("node: %s", name)
356 ToStdout("%s", result.output)
357 ToStdout("return code = %s", result.exit_code)
362 def VerifyCluster(opts, args):
363 """Verify integrity of cluster, performing various test on nodes.
365 @param opts: the command line options selected by the user
367 @param args: should be an empty list
369 @return: the desired exit code
373 if opts.skip_nplusone_mem:
374 skip_checks.append(constants.VERIFY_NPLUSONE_MEM)
375 op = opcodes.OpVerifyCluster(skip_checks=skip_checks,
376 verbose=opts.verbose,
377 error_codes=opts.error_codes,
378 debug_simulate_errors=opts.simulate_errors)
379 if SubmitOpCode(op, opts=opts):
385 def VerifyDisks(opts, args):
386 """Verify integrity of cluster disks.
388 @param opts: the command line options selected by the user
390 @param args: should be an empty list
392 @return: the desired exit code
395 op = opcodes.OpVerifyDisks()
396 result = SubmitOpCode(op, opts=opts)
397 if not isinstance(result, (list, tuple)) or len(result) != 3:
398 raise errors.ProgrammerError("Unknown result type for OpVerifyDisks")
400 bad_nodes, instances, missing = result
402 retcode = constants.EXIT_SUCCESS
405 for node, text in bad_nodes.items():
406 ToStdout("Error gathering data on node %s: %s",
407 node, utils.SafeEncode(text[-400:]))
409 ToStdout("You need to fix these nodes first before fixing instances")
412 for iname in instances:
415 op = opcodes.OpActivateInstanceDisks(instance_name=iname)
417 ToStdout("Activating disks for instance '%s'", iname)
418 SubmitOpCode(op, opts=opts)
419 except errors.GenericError, err:
420 nret, msg = FormatError(err)
422 ToStderr("Error activating disks for instance %s: %s", iname, msg)
425 for iname, ival in missing.iteritems():
426 all_missing = utils.all(ival, lambda x: x[0] in bad_nodes)
428 ToStdout("Instance %s cannot be verified as it lives on"
429 " broken nodes", iname)
431 ToStdout("Instance %s has missing logical volumes:", iname)
433 for node, vol in ival:
434 if node in bad_nodes:
435 ToStdout("\tbroken node %s /dev/xenvg/%s", node, vol)
437 ToStdout("\t%s /dev/xenvg/%s", node, vol)
438 ToStdout("You need to run replace_disks for all the above"
439 " instances, if this message persist after fixing nodes.")
445 def RepairDiskSizes(opts, args):
446 """Verify sizes of cluster disks.
448 @param opts: the command line options selected by the user
450 @param args: optional list of instances to restrict check to
452 @return: the desired exit code
455 op = opcodes.OpRepairDiskSizes(instances=args)
456 SubmitOpCode(op, opts=opts)
460 def MasterFailover(opts, args):
461 """Failover the master node.
463 This command, when run on a non-master node, will cause the current
464 master to cease being master, and the non-master to become new
467 @param opts: the command line options selected by the user
469 @param args: should be an empty list
471 @return: the desired exit code
475 usertext = ("This will perform the failover even if most other nodes"
476 " are down, or if this node is outdated. This is dangerous"
477 " as it can lead to a non-consistent cluster. Check the"
478 " gnt-cluster(8) man page before proceeding. Continue?")
479 if not AskUser(usertext):
482 return bootstrap.MasterFailover(no_voting=opts.no_voting)
485 def SearchTags(opts, args):
486 """Searches the tags on all the cluster.
488 @param opts: the command line options selected by the user
490 @param args: should contain only one element, the tag pattern
492 @return: the desired exit code
495 op = opcodes.OpSearchTags(pattern=args[0])
496 result = SubmitOpCode(op, opts=opts)
499 result = list(result)
501 for path, tag in result:
502 ToStdout("%s %s", path, tag)
505 def _RenewCrypto(new_cluster_cert, new_rapi_cert, rapi_cert_filename,
506 new_confd_hmac_key, force):
507 """Renews cluster certificates, keys and secrets.
509 @type new_cluster_cert: bool
510 @param new_cluster_cert: Whether to generate a new cluster certificate
511 @type new_rapi_cert: bool
512 @param new_rapi_cert: Whether to generate a new RAPI certificate
513 @type rapi_cert_filename: string
514 @param rapi_cert_filename: Path to file containing new RAPI certificate
515 @type new_confd_hmac_key: bool
516 @param new_confd_hmac_key: Whether to generate a new HMAC key
518 @param force: Whether to ask user for confirmation
521 if new_rapi_cert and rapi_cert_filename:
522 ToStderr("Only one of the --new-rapi-certficate and --rapi-certificate"
523 " options can be specified at the same time.")
526 if rapi_cert_filename:
527 # Read and verify new certificate
529 rapi_cert_pem = utils.ReadFile(rapi_cert_filename)
531 OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
533 except Exception, err: # pylint: disable-msg=W0703
534 ToStderr("Can't load new RAPI certificate from %s: %s" %
535 (rapi_cert_filename, str(err)))
539 OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM, rapi_cert_pem)
540 except Exception, err: # pylint: disable-msg=W0703
541 ToStderr("Can't load new RAPI private key from %s: %s" %
542 (rapi_cert_filename, str(err)))
549 usertext = ("This requires all daemons on all nodes to be restarted and"
550 " may take some time. Continue?")
551 if not AskUser(usertext):
554 def _RenewCryptoInner(ctx):
555 ctx.feedback_fn("Updating certificates and keys")
556 bootstrap.GenerateClusterCrypto(new_cluster_cert, new_rapi_cert,
558 rapi_cert_pem=rapi_cert_pem)
563 files_to_copy.append(constants.NODED_CERT_FILE)
565 if new_rapi_cert or rapi_cert_pem:
566 files_to_copy.append(constants.RAPI_CERT_FILE)
568 if new_confd_hmac_key:
569 files_to_copy.append(constants.CONFD_HMAC_KEY)
572 for node_name in ctx.nonmaster_nodes:
573 ctx.feedback_fn("Copying %s to %s" %
574 (", ".join(files_to_copy), node_name))
575 for file_name in files_to_copy:
576 ctx.ssh.CopyFileToNode(node_name, file_name)
578 RunWhileClusterStopped(ToStdout, _RenewCryptoInner)
580 ToStdout("All requested certificates and keys have been replaced."
581 " Running \"gnt-cluster verify\" now is recommended.")
586 def RenewCrypto(opts, args):
587 """Renews cluster certificates, keys and secrets.
590 return _RenewCrypto(opts.new_cluster_cert,
593 opts.new_confd_hmac_key,
597 def SetClusterParams(opts, args):
598 """Modify the cluster.
600 @param opts: the command line options selected by the user
602 @param args: should be an empty list
604 @return: the desired exit code
607 if not (not opts.lvm_storage or opts.vg_name or
608 opts.enabled_hypervisors or opts.hvparams or
609 opts.beparams or opts.nicparams or
610 opts.candidate_pool_size is not None or
611 opts.uid_pool is not None or
612 opts.maintain_node_health is not None or
613 opts.add_uids is not None or
614 opts.remove_uids is not None):
615 ToStderr("Please give at least one of the parameters.")
618 vg_name = opts.vg_name
619 if not opts.lvm_storage and opts.vg_name:
620 ToStderr("Options --no-lvm-storage and --vg-name conflict.")
623 if not opts.lvm_storage:
626 hvlist = opts.enabled_hypervisors
627 if hvlist is not None:
628 hvlist = hvlist.split(",")
630 # a list of (name, dict) we can pass directly to dict() (or [])
631 hvparams = dict(opts.hvparams)
632 for hv_params in hvparams.values():
633 utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
635 beparams = opts.beparams
636 utils.ForceDictType(beparams, constants.BES_PARAMETER_TYPES)
638 nicparams = opts.nicparams
639 utils.ForceDictType(nicparams, constants.NICS_PARAMETER_TYPES)
642 mnh = opts.maintain_node_health
644 uid_pool = opts.uid_pool
645 if uid_pool is not None:
646 uid_pool = uidpool.ParseUidPool(uid_pool)
648 add_uids = opts.add_uids
649 if add_uids is not None:
650 add_uids = uidpool.ParseUidPool(add_uids)
652 remove_uids = opts.remove_uids
653 if remove_uids is not None:
654 remove_uids = uidpool.ParseUidPool(remove_uids)
656 op = opcodes.OpSetClusterParams(vg_name=vg_name,
657 enabled_hypervisors=hvlist,
662 candidate_pool_size=opts.candidate_pool_size,
663 maintain_node_health=mnh,
666 remove_uids=remove_uids)
667 SubmitOpCode(op, opts=opts)
671 def QueueOps(opts, args):
674 @param opts: the command line options selected by the user
676 @param args: should contain only one element, the subcommand
678 @return: the desired exit code
683 if command in ("drain", "undrain"):
684 drain_flag = command == "drain"
685 client.SetQueueDrainFlag(drain_flag)
686 elif command == "info":
687 result = client.QueryConfigValues(["drain_flag"])
692 ToStdout("The drain flag is %s" % val)
694 raise errors.OpPrereqError("Command '%s' is not valid." % command,
700 def _ShowWatcherPause(until):
701 if until is None or until < time.time():
702 ToStdout("The watcher is not paused.")
704 ToStdout("The watcher is paused until %s.", time.ctime(until))
707 def WatcherOps(opts, args):
708 """Watcher operations.
710 @param opts: the command line options selected by the user
712 @param args: should contain only one element, the subcommand
714 @return: the desired exit code
720 if command == "continue":
721 client.SetWatcherPause(None)
722 ToStdout("The watcher is no longer paused.")
724 elif command == "pause":
726 raise errors.OpPrereqError("Missing pause duration", errors.ECODE_INVAL)
728 result = client.SetWatcherPause(time.time() + ParseTimespec(args[1]))
729 _ShowWatcherPause(result)
731 elif command == "info":
732 result = client.QueryConfigValues(["watcher_pause"])
733 _ShowWatcherPause(result[0])
736 raise errors.OpPrereqError("Command '%s' is not valid." % command,
744 InitCluster, [ArgHost(min=1, max=1)],
745 [BACKEND_OPT, CP_SIZE_OPT, ENABLED_HV_OPT, GLOBAL_FILEDIR_OPT,
746 HVLIST_OPT, MAC_PREFIX_OPT, MASTER_NETDEV_OPT, NIC_PARAMS_OPT,
747 NOLVM_STORAGE_OPT, NOMODIFY_ETCHOSTS_OPT, NOMODIFY_SSH_SETUP_OPT,
748 SECONDARY_IP_OPT, VG_NAME_OPT, MAINTAIN_NODE_HEALTH_OPT,
750 "[opts...] <cluster_name>", "Initialises a new cluster configuration"),
752 DestroyCluster, ARGS_NONE, [YES_DOIT_OPT],
753 "", "Destroy cluster"),
755 RenameCluster, [ArgHost(min=1, max=1)],
758 "Renames the cluster"),
760 RedistributeConfig, ARGS_NONE, [SUBMIT_OPT],
761 "", "Forces a push of the configuration file and ssconf files"
762 " to the nodes in the cluster"),
764 VerifyCluster, ARGS_NONE,
765 [VERBOSE_OPT, DEBUG_SIMERR_OPT, ERROR_CODES_OPT, NONPLUS1_OPT],
766 "", "Does a check on the cluster configuration"),
768 VerifyDisks, ARGS_NONE, [],
769 "", "Does a check on the cluster disk status"),
770 'repair-disk-sizes': (
771 RepairDiskSizes, ARGS_MANY_INSTANCES, [],
772 "", "Updates mismatches in recorded disk sizes"),
774 MasterFailover, ARGS_NONE, [NOVOTING_OPT],
775 "", "Makes the current node the master"),
777 ShowClusterVersion, ARGS_NONE, [],
778 "", "Shows the cluster version"),
780 ShowClusterMaster, ARGS_NONE, [],
781 "", "Shows the cluster master"),
783 ClusterCopyFile, [ArgFile(min=1, max=1)],
784 [NODE_LIST_OPT, USE_REPL_NET_OPT],
785 "[-n node...] <filename>", "Copies a file to all (or only some) nodes"),
787 RunClusterCommand, [ArgCommand(min=1)],
789 "[-n node...] <command>", "Runs a command on all (or only some) nodes"),
791 ShowClusterConfig, ARGS_NONE, [],
792 "", "Show cluster configuration"),
794 ListTags, ARGS_NONE, [], "", "List the tags of the cluster"),
796 AddTags, [ArgUnknown()], [TAG_SRC_OPT],
797 "tag...", "Add tags to the cluster"),
799 RemoveTags, [ArgUnknown()], [TAG_SRC_OPT],
800 "tag...", "Remove tags from the cluster"),
802 SearchTags, [ArgUnknown(min=1, max=1)],
803 [], "", "Searches the tags on all objects on"
804 " the cluster for a given pattern (regex)"),
807 [ArgChoice(min=1, max=1, choices=["drain", "undrain", "info"])],
808 [], "drain|undrain|info", "Change queue properties"),
811 [ArgChoice(min=1, max=1, choices=["pause", "continue", "info"]),
812 ArgSuggest(min=0, max=1, choices=["30m", "1h", "4h"])],
814 "{pause <timespec>|continue|info}", "Change watcher properties"),
816 SetClusterParams, ARGS_NONE,
817 [BACKEND_OPT, CP_SIZE_OPT, ENABLED_HV_OPT, HVLIST_OPT,
818 NIC_PARAMS_OPT, NOLVM_STORAGE_OPT, VG_NAME_OPT, MAINTAIN_NODE_HEALTH_OPT,
819 UIDPOOL_OPT, ADD_UIDS_OPT, REMOVE_UIDS_OPT],
821 "Alters the parameters of the cluster"),
823 RenewCrypto, ARGS_NONE,
824 [NEW_CLUSTER_CERT_OPT, NEW_RAPI_CERT_OPT, RAPI_CERT_OPT,
825 NEW_CONFD_HMAC_KEY_OPT, FORCE_OPT],
827 "Renews cluster certificates, keys and secrets"),
831 if __name__ == '__main__':
832 sys.exit(GenericMain(commands, override={"tag_type": constants.TAG_CLUSTER}))