4 # Copyright (C) 2006, 2007, 2010, 2011 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=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
43 from ganeti import compat
44 from ganeti import netutils
47 ON_OPT = cli_option("--on", default=False,
48 action="store_true", dest="on",
49 help="Recover from an EPO")
51 GROUPS_OPT = cli_option("--groups", default=False,
52 action="store_true", dest="groups",
53 help="Arguments are node groups instead of nodes")
55 _EPO_PING_INTERVAL = 30 # 30 seconds between pings
56 _EPO_PING_TIMEOUT = 1 # 1 second
57 _EPO_REACHABLE_TIMEOUT = 15 * 60 # 15 minutes
61 def InitCluster(opts, args):
62 """Initialize the cluster.
64 @param opts: the command line options selected by the user
66 @param args: should contain only one element, the desired
69 @return: the desired exit code
72 if not opts.lvm_storage and opts.vg_name:
73 ToStderr("Options --no-lvm-storage and --vg-name conflict.")
76 vg_name = opts.vg_name
77 if opts.lvm_storage and not opts.vg_name:
78 vg_name = constants.DEFAULT_VG
80 if not opts.drbd_storage and opts.drbd_helper:
81 ToStderr("Options --no-drbd-storage and --drbd-usermode-helper conflict.")
84 drbd_helper = opts.drbd_helper
85 if opts.drbd_storage and not opts.drbd_helper:
86 drbd_helper = constants.DEFAULT_DRBD_HELPER
88 master_netdev = opts.master_netdev
89 if master_netdev is None:
90 master_netdev = constants.DEFAULT_BRIDGE
92 hvlist = opts.enabled_hypervisors
94 hvlist = constants.DEFAULT_ENABLED_HYPERVISOR
95 hvlist = hvlist.split(",")
97 hvparams = dict(opts.hvparams)
98 beparams = opts.beparams
99 nicparams = opts.nicparams
101 # prepare beparams dict
102 beparams = objects.FillDict(constants.BEC_DEFAULTS, beparams)
103 utils.ForceDictType(beparams, constants.BES_PARAMETER_TYPES)
105 # prepare nicparams dict
106 nicparams = objects.FillDict(constants.NICC_DEFAULTS, nicparams)
107 utils.ForceDictType(nicparams, constants.NICS_PARAMETER_TYPES)
109 # prepare ndparams dict
110 if opts.ndparams is None:
111 ndparams = dict(constants.NDC_DEFAULTS)
113 ndparams = objects.FillDict(constants.NDC_DEFAULTS, opts.ndparams)
114 utils.ForceDictType(ndparams, constants.NDS_PARAMETER_TYPES)
116 # prepare hvparams dict
117 for hv in constants.HYPER_TYPES:
118 if hv not in hvparams:
120 hvparams[hv] = objects.FillDict(constants.HVC_DEFAULTS[hv], hvparams[hv])
121 utils.ForceDictType(hvparams[hv], constants.HVS_PARAMETER_TYPES)
123 if opts.candidate_pool_size is None:
124 opts.candidate_pool_size = constants.MASTER_POOL_SIZE_DEFAULT
126 if opts.mac_prefix is None:
127 opts.mac_prefix = constants.DEFAULT_MAC_PREFIX
129 uid_pool = opts.uid_pool
130 if uid_pool is not None:
131 uid_pool = uidpool.ParseUidPool(uid_pool)
133 if opts.prealloc_wipe_disks is None:
134 opts.prealloc_wipe_disks = False
137 primary_ip_version = int(opts.primary_ip_version)
138 except (ValueError, TypeError), err:
139 ToStderr("Invalid primary ip version value: %s" % str(err))
142 bootstrap.InitCluster(cluster_name=args[0],
143 secondary_ip=opts.secondary_ip,
145 mac_prefix=opts.mac_prefix,
146 master_netdev=master_netdev,
147 file_storage_dir=opts.file_storage_dir,
148 shared_file_storage_dir=opts.shared_file_storage_dir,
149 enabled_hypervisors=hvlist,
154 candidate_pool_size=opts.candidate_pool_size,
155 modify_etc_hosts=opts.modify_etc_hosts,
156 modify_ssh_setup=opts.modify_ssh_setup,
157 maintain_node_health=opts.maintain_node_health,
158 drbd_helper=drbd_helper,
160 default_iallocator=opts.default_iallocator,
161 primary_ip_version=primary_ip_version,
162 prealloc_wipe_disks=opts.prealloc_wipe_disks,
164 op = opcodes.OpClusterPostInit()
165 SubmitOpCode(op, opts=opts)
170 def DestroyCluster(opts, args):
171 """Destroy the cluster.
173 @param opts: the command line options selected by the user
175 @param args: should be an empty list
177 @return: the desired exit code
180 if not opts.yes_do_it:
181 ToStderr("Destroying a cluster is irreversible. If you really want"
182 " destroy this cluster, supply the --yes-do-it option.")
185 op = opcodes.OpClusterDestroy()
186 master = SubmitOpCode(op, opts=opts)
187 # if we reached this, the opcode didn't fail; we can proceed to
188 # shutdown all the daemons
189 bootstrap.FinalizeClusterDestroy(master)
193 def RenameCluster(opts, args):
194 """Rename the cluster.
196 @param opts: the command line options selected by the user
198 @param args: should contain only one element, the new cluster name
200 @return: the desired exit code
205 (cluster_name, ) = cl.QueryConfigValues(["cluster_name"])
209 usertext = ("This will rename the cluster from '%s' to '%s'. If you are"
210 " connected over the network to the cluster name, the"
211 " operation is very dangerous as the IP address will be"
212 " removed from the node and the change may not go through."
213 " Continue?") % (cluster_name, new_name)
214 if not AskUser(usertext):
217 op = opcodes.OpClusterRename(name=new_name)
218 result = SubmitOpCode(op, opts=opts, cl=cl)
221 ToStdout("Cluster renamed from '%s' to '%s'", cluster_name, result)
226 def RedistributeConfig(opts, args):
227 """Forces push of the cluster configuration.
229 @param opts: the command line options selected by the user
231 @param args: empty list
233 @return: the desired exit code
236 op = opcodes.OpClusterRedistConf()
237 SubmitOrSend(op, opts)
241 def ShowClusterVersion(opts, args):
242 """Write version of ganeti software to the standard output.
244 @param opts: the command line options selected by the user
246 @param args: should be an empty list
248 @return: the desired exit code
252 result = cl.QueryClusterInfo()
253 ToStdout("Software version: %s", result["software_version"])
254 ToStdout("Internode protocol: %s", result["protocol_version"])
255 ToStdout("Configuration format: %s", result["config_version"])
256 ToStdout("OS api version: %s", result["os_api_version"])
257 ToStdout("Export interface: %s", result["export_version"])
261 def ShowClusterMaster(opts, args):
262 """Write name of master node to the standard output.
264 @param opts: the command line options selected by the user
266 @param args: should be an empty list
268 @return: the desired exit code
271 master = bootstrap.GetMaster()
276 def _PrintGroupedParams(paramsdict, level=1, roman=False):
277 """Print Grouped parameters (be, nic, disk) by group.
279 @type paramsdict: dict of dicts
280 @param paramsdict: {group: {param: value, ...}, ...}
282 @param level: Level of indention
286 for item, val in sorted(paramsdict.items()):
287 if isinstance(val, dict):
288 ToStdout("%s- %s:", indent, item)
289 _PrintGroupedParams(val, level=level + 1, roman=roman)
290 elif roman and isinstance(val, int):
291 ToStdout("%s %s: %s", indent, item, compat.TryToRoman(val))
293 ToStdout("%s %s: %s", indent, item, val)
296 def ShowClusterConfig(opts, args):
297 """Shows cluster information.
299 @param opts: the command line options selected by the user
301 @param args: should be an empty list
303 @return: the desired exit code
307 result = cl.QueryClusterInfo()
309 ToStdout("Cluster name: %s", result["name"])
310 ToStdout("Cluster UUID: %s", result["uuid"])
312 ToStdout("Creation time: %s", utils.FormatTime(result["ctime"]))
313 ToStdout("Modification time: %s", utils.FormatTime(result["mtime"]))
315 ToStdout("Master node: %s", result["master"])
317 ToStdout("Architecture (this node): %s (%s)",
318 result["architecture"][0], result["architecture"][1])
321 tags = utils.CommaJoin(utils.NiceSort(result["tags"]))
325 ToStdout("Tags: %s", tags)
327 ToStdout("Default hypervisor: %s", result["default_hypervisor"])
328 ToStdout("Enabled hypervisors: %s",
329 utils.CommaJoin(result["enabled_hypervisors"]))
331 ToStdout("Hypervisor parameters:")
332 _PrintGroupedParams(result["hvparams"])
334 ToStdout("OS-specific hypervisor parameters:")
335 _PrintGroupedParams(result["os_hvp"])
337 ToStdout("OS parameters:")
338 _PrintGroupedParams(result["osparams"])
340 ToStdout("Hidden OSes: %s", utils.CommaJoin(result["hidden_os"]))
341 ToStdout("Blacklisted OSes: %s", utils.CommaJoin(result["blacklisted_os"]))
343 ToStdout("Cluster parameters:")
344 ToStdout(" - candidate pool size: %s",
345 compat.TryToRoman(result["candidate_pool_size"],
346 convert=opts.roman_integers))
347 ToStdout(" - master netdev: %s", result["master_netdev"])
348 ToStdout(" - lvm volume group: %s", result["volume_group_name"])
349 if result["reserved_lvs"]:
350 reserved_lvs = utils.CommaJoin(result["reserved_lvs"])
352 reserved_lvs = "(none)"
353 ToStdout(" - lvm reserved volumes: %s", reserved_lvs)
354 ToStdout(" - drbd usermode helper: %s", result["drbd_usermode_helper"])
355 ToStdout(" - file storage path: %s", result["file_storage_dir"])
356 ToStdout(" - shared file storage path: %s",
357 result["shared_file_storage_dir"])
358 ToStdout(" - maintenance of node health: %s",
359 result["maintain_node_health"])
360 ToStdout(" - uid pool: %s",
361 uidpool.FormatUidPool(result["uid_pool"],
362 roman=opts.roman_integers))
363 ToStdout(" - default instance allocator: %s", result["default_iallocator"])
364 ToStdout(" - primary ip version: %d", result["primary_ip_version"])
365 ToStdout(" - preallocation wipe disks: %s", result["prealloc_wipe_disks"])
366 ToStdout(" - OS search path: %s", utils.CommaJoin(constants.OS_SEARCH_PATH))
368 ToStdout("Default node parameters:")
369 _PrintGroupedParams(result["ndparams"], roman=opts.roman_integers)
371 ToStdout("Default instance parameters:")
372 _PrintGroupedParams(result["beparams"], roman=opts.roman_integers)
374 ToStdout("Default nic parameters:")
375 _PrintGroupedParams(result["nicparams"], roman=opts.roman_integers)
380 def ClusterCopyFile(opts, args):
381 """Copy a file from master to some nodes.
383 @param opts: the command line options selected by the user
385 @param args: should contain only one element, the path of
386 the file to be copied
388 @return: the desired exit code
392 if not os.path.exists(filename):
393 raise errors.OpPrereqError("No such filename '%s'" % filename,
398 cluster_name = cl.QueryConfigValues(["cluster_name"])[0]
400 results = GetOnlineNodes(nodes=opts.nodes, cl=cl, filter_master=True,
401 secondary_ips=opts.use_replication_network,
402 nodegroup=opts.nodegroup)
404 srun = ssh.SshRunner(cluster_name=cluster_name)
406 if not srun.CopyFileToNode(node, filename):
407 ToStderr("Copy of file %s to node %s failed", filename, node)
412 def RunClusterCommand(opts, args):
413 """Run a command on some nodes.
415 @param opts: the command line options selected by the user
417 @param args: should contain the command to be run and its arguments
419 @return: the desired exit code
424 command = " ".join(args)
426 nodes = GetOnlineNodes(nodes=opts.nodes, cl=cl, nodegroup=opts.nodegroup)
428 cluster_name, master_node = cl.QueryConfigValues(["cluster_name",
431 srun = ssh.SshRunner(cluster_name=cluster_name)
433 # Make sure master node is at list end
434 if master_node in nodes:
435 nodes.remove(master_node)
436 nodes.append(master_node)
439 result = srun.Run(name, "root", command)
440 ToStdout("------------------------------------------------")
441 ToStdout("node: %s", name)
442 ToStdout("%s", result.output)
443 ToStdout("return code = %s", result.exit_code)
448 def VerifyCluster(opts, args):
449 """Verify integrity of cluster, performing various test on nodes.
451 @param opts: the command line options selected by the user
453 @param args: should be an empty list
455 @return: the desired exit code
460 if opts.skip_nplusone_mem:
461 skip_checks.append(constants.VERIFY_NPLUSONE_MEM)
465 op = opcodes.OpClusterVerify(verbose=opts.verbose,
466 error_codes=opts.error_codes,
467 debug_simulate_errors=opts.simulate_errors,
468 skip_checks=skip_checks,
469 group_name=opts.nodegroup)
470 result = SubmitOpCode(op, cl=cl, opts=opts)
472 # Keep track of submitted jobs
473 jex = JobExecutor(cl=cl, opts=opts)
475 for (status, job_id) in result[constants.JOB_IDS_KEY]:
476 jex.AddJobId(None, status, job_id)
478 results = jex.GetResults()
480 (bad_jobs, bad_results) = \
482 # Convert iterators to lists
485 map(compat.partial(itertools.ifilterfalse, bool),
486 # Convert result to booleans in a tuple
487 zip(*((job_success, len(op_results) == 1 and op_results[0])
488 for (job_success, op_results) in results)))))
490 if bad_jobs == 0 and bad_results == 0:
491 rcode = constants.EXIT_SUCCESS
493 rcode = constants.EXIT_FAILURE
495 ToStdout("%s job(s) failed while verifying the cluster.", bad_jobs)
500 def VerifyDisks(opts, args):
501 """Verify integrity of cluster disks.
503 @param opts: the command line options selected by the user
505 @param args: should be an empty list
507 @return: the desired exit code
512 op = opcodes.OpClusterVerifyDisks()
514 result = SubmitOpCode(op, cl=cl, opts=opts)
516 # Keep track of submitted jobs
517 jex = JobExecutor(cl=cl, opts=opts)
519 for (status, job_id) in result[constants.JOB_IDS_KEY]:
520 jex.AddJobId(None, status, job_id)
522 retcode = constants.EXIT_SUCCESS
524 for (status, result) in jex.GetResults():
526 ToStdout("Job failed: %s", result)
529 ((bad_nodes, instances, missing), ) = result
531 for node, text in bad_nodes.items():
532 ToStdout("Error gathering data on node %s: %s",
533 node, utils.SafeEncode(text[-400:]))
534 retcode = constants.EXIT_FAILURE
535 ToStdout("You need to fix these nodes first before fixing instances")
537 for iname in instances:
540 op = opcodes.OpInstanceActivateDisks(instance_name=iname)
542 ToStdout("Activating disks for instance '%s'", iname)
543 SubmitOpCode(op, opts=opts, cl=cl)
544 except errors.GenericError, err:
545 nret, msg = FormatError(err)
547 ToStderr("Error activating disks for instance %s: %s", iname, msg)
550 for iname, ival in missing.iteritems():
551 all_missing = compat.all(x[0] in bad_nodes for x in ival)
553 ToStdout("Instance %s cannot be verified as it lives on"
554 " broken nodes", iname)
556 ToStdout("Instance %s has missing logical volumes:", iname)
558 for node, vol in ival:
559 if node in bad_nodes:
560 ToStdout("\tbroken node %s /dev/%s", node, vol)
562 ToStdout("\t%s /dev/%s", node, vol)
564 ToStdout("You need to replace or recreate disks for all the above"
565 " instances if this message persists after fixing broken nodes.")
566 retcode = constants.EXIT_FAILURE
571 def RepairDiskSizes(opts, args):
572 """Verify sizes of cluster disks.
574 @param opts: the command line options selected by the user
576 @param args: optional list of instances to restrict check to
578 @return: the desired exit code
581 op = opcodes.OpClusterRepairDiskSizes(instances=args)
582 SubmitOpCode(op, opts=opts)
586 def MasterFailover(opts, args):
587 """Failover the master node.
589 This command, when run on a non-master node, will cause the current
590 master to cease being master, and the non-master to become new
593 @param opts: the command line options selected by the user
595 @param args: should be an empty list
597 @return: the desired exit code
601 usertext = ("This will perform the failover even if most other nodes"
602 " are down, or if this node is outdated. This is dangerous"
603 " as it can lead to a non-consistent cluster. Check the"
604 " gnt-cluster(8) man page before proceeding. Continue?")
605 if not AskUser(usertext):
608 return bootstrap.MasterFailover(no_voting=opts.no_voting)
611 def MasterPing(opts, args):
612 """Checks if the master is alive.
614 @param opts: the command line options selected by the user
616 @param args: should be an empty list
618 @return: the desired exit code
623 cl.QueryClusterInfo()
625 except Exception: # pylint: disable=W0703
629 def SearchTags(opts, args):
630 """Searches the tags on all the cluster.
632 @param opts: the command line options selected by the user
634 @param args: should contain only one element, the tag pattern
636 @return: the desired exit code
639 op = opcodes.OpTagsSearch(pattern=args[0])
640 result = SubmitOpCode(op, opts=opts)
643 result = list(result)
645 for path, tag in result:
646 ToStdout("%s %s", path, tag)
649 def _RenewCrypto(new_cluster_cert, new_rapi_cert, rapi_cert_filename,
650 new_confd_hmac_key, new_cds, cds_filename,
652 """Renews cluster certificates, keys and secrets.
654 @type new_cluster_cert: bool
655 @param new_cluster_cert: Whether to generate a new cluster certificate
656 @type new_rapi_cert: bool
657 @param new_rapi_cert: Whether to generate a new RAPI certificate
658 @type rapi_cert_filename: string
659 @param rapi_cert_filename: Path to file containing new RAPI certificate
660 @type new_confd_hmac_key: bool
661 @param new_confd_hmac_key: Whether to generate a new HMAC key
663 @param new_cds: Whether to generate a new cluster domain secret
664 @type cds_filename: string
665 @param cds_filename: Path to file containing new cluster domain secret
667 @param force: Whether to ask user for confirmation
670 if new_rapi_cert and rapi_cert_filename:
671 ToStderr("Only one of the --new-rapi-certficate and --rapi-certificate"
672 " options can be specified at the same time.")
675 if new_cds and cds_filename:
676 ToStderr("Only one of the --new-cluster-domain-secret and"
677 " --cluster-domain-secret options can be specified at"
681 if rapi_cert_filename:
682 # Read and verify new certificate
684 rapi_cert_pem = utils.ReadFile(rapi_cert_filename)
686 OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
688 except Exception, err: # pylint: disable=W0703
689 ToStderr("Can't load new RAPI certificate from %s: %s" %
690 (rapi_cert_filename, str(err)))
694 OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM, rapi_cert_pem)
695 except Exception, err: # pylint: disable=W0703
696 ToStderr("Can't load new RAPI private key from %s: %s" %
697 (rapi_cert_filename, str(err)))
705 cds = utils.ReadFile(cds_filename)
706 except Exception, err: # pylint: disable=W0703
707 ToStderr("Can't load new cluster domain secret from %s: %s" %
708 (cds_filename, str(err)))
714 usertext = ("This requires all daemons on all nodes to be restarted and"
715 " may take some time. Continue?")
716 if not AskUser(usertext):
719 def _RenewCryptoInner(ctx):
720 ctx.feedback_fn("Updating certificates and keys")
721 bootstrap.GenerateClusterCrypto(new_cluster_cert, new_rapi_cert,
724 rapi_cert_pem=rapi_cert_pem,
730 files_to_copy.append(constants.NODED_CERT_FILE)
732 if new_rapi_cert or rapi_cert_pem:
733 files_to_copy.append(constants.RAPI_CERT_FILE)
735 if new_confd_hmac_key:
736 files_to_copy.append(constants.CONFD_HMAC_KEY)
739 files_to_copy.append(constants.CLUSTER_DOMAIN_SECRET_FILE)
742 for node_name in ctx.nonmaster_nodes:
743 ctx.feedback_fn("Copying %s to %s" %
744 (", ".join(files_to_copy), node_name))
745 for file_name in files_to_copy:
746 ctx.ssh.CopyFileToNode(node_name, file_name)
748 RunWhileClusterStopped(ToStdout, _RenewCryptoInner)
750 ToStdout("All requested certificates and keys have been replaced."
751 " Running \"gnt-cluster verify\" now is recommended.")
756 def RenewCrypto(opts, args):
757 """Renews cluster certificates, keys and secrets.
760 return _RenewCrypto(opts.new_cluster_cert,
763 opts.new_confd_hmac_key,
764 opts.new_cluster_domain_secret,
765 opts.cluster_domain_secret,
769 def SetClusterParams(opts, args):
770 """Modify the cluster.
772 @param opts: the command line options selected by the user
774 @param args: should be an empty list
776 @return: the desired exit code
779 if not (not opts.lvm_storage or opts.vg_name or
780 not opts.drbd_storage or opts.drbd_helper or
781 opts.enabled_hypervisors or opts.hvparams or
782 opts.beparams or opts.nicparams or opts.ndparams or
783 opts.candidate_pool_size is not None or
784 opts.uid_pool is not None or
785 opts.maintain_node_health is not None or
786 opts.add_uids is not None or
787 opts.remove_uids is not None or
788 opts.default_iallocator is not None or
789 opts.reserved_lvs is not None or
790 opts.master_netdev is not None or
791 opts.prealloc_wipe_disks is not None):
792 ToStderr("Please give at least one of the parameters.")
795 vg_name = opts.vg_name
796 if not opts.lvm_storage and opts.vg_name:
797 ToStderr("Options --no-lvm-storage and --vg-name conflict.")
800 if not opts.lvm_storage:
803 drbd_helper = opts.drbd_helper
804 if not opts.drbd_storage and opts.drbd_helper:
805 ToStderr("Options --no-drbd-storage and --drbd-usermode-helper conflict.")
808 if not opts.drbd_storage:
811 hvlist = opts.enabled_hypervisors
812 if hvlist is not None:
813 hvlist = hvlist.split(",")
815 # a list of (name, dict) we can pass directly to dict() (or [])
816 hvparams = dict(opts.hvparams)
817 for hv_params in hvparams.values():
818 utils.ForceDictType(hv_params, constants.HVS_PARAMETER_TYPES)
820 beparams = opts.beparams
821 utils.ForceDictType(beparams, constants.BES_PARAMETER_TYPES)
823 nicparams = opts.nicparams
824 utils.ForceDictType(nicparams, constants.NICS_PARAMETER_TYPES)
826 ndparams = opts.ndparams
827 if ndparams is not None:
828 utils.ForceDictType(ndparams, constants.NDS_PARAMETER_TYPES)
830 mnh = opts.maintain_node_health
832 uid_pool = opts.uid_pool
833 if uid_pool is not None:
834 uid_pool = uidpool.ParseUidPool(uid_pool)
836 add_uids = opts.add_uids
837 if add_uids is not None:
838 add_uids = uidpool.ParseUidPool(add_uids)
840 remove_uids = opts.remove_uids
841 if remove_uids is not None:
842 remove_uids = uidpool.ParseUidPool(remove_uids)
844 if opts.reserved_lvs is not None:
845 if opts.reserved_lvs == "":
846 opts.reserved_lvs = []
848 opts.reserved_lvs = utils.UnescapeAndSplit(opts.reserved_lvs, sep=",")
850 op = opcodes.OpClusterSetParams(vg_name=vg_name,
851 drbd_helper=drbd_helper,
852 enabled_hypervisors=hvlist,
858 candidate_pool_size=opts.candidate_pool_size,
859 maintain_node_health=mnh,
862 remove_uids=remove_uids,
863 default_iallocator=opts.default_iallocator,
864 prealloc_wipe_disks=opts.prealloc_wipe_disks,
865 master_netdev=opts.master_netdev,
866 reserved_lvs=opts.reserved_lvs)
867 SubmitOpCode(op, opts=opts)
871 def QueueOps(opts, args):
874 @param opts: the command line options selected by the user
876 @param args: should contain only one element, the subcommand
878 @return: the desired exit code
883 if command in ("drain", "undrain"):
884 drain_flag = command == "drain"
885 client.SetQueueDrainFlag(drain_flag)
886 elif command == "info":
887 result = client.QueryConfigValues(["drain_flag"])
892 ToStdout("The drain flag is %s" % val)
894 raise errors.OpPrereqError("Command '%s' is not valid." % command,
900 def _ShowWatcherPause(until):
901 if until is None or until < time.time():
902 ToStdout("The watcher is not paused.")
904 ToStdout("The watcher is paused until %s.", time.ctime(until))
907 def WatcherOps(opts, args):
908 """Watcher operations.
910 @param opts: the command line options selected by the user
912 @param args: should contain only one element, the subcommand
914 @return: the desired exit code
920 if command == "continue":
921 client.SetWatcherPause(None)
922 ToStdout("The watcher is no longer paused.")
924 elif command == "pause":
926 raise errors.OpPrereqError("Missing pause duration", errors.ECODE_INVAL)
928 result = client.SetWatcherPause(time.time() + ParseTimespec(args[1]))
929 _ShowWatcherPause(result)
931 elif command == "info":
932 result = client.QueryConfigValues(["watcher_pause"])
933 _ShowWatcherPause(result[0])
936 raise errors.OpPrereqError("Command '%s' is not valid." % command,
942 def _OobPower(opts, node_list, power):
943 """Puts the node in the list to desired power state.
945 @param opts: The command line options selected by the user
946 @param node_list: The list of nodes to operate on
947 @param power: True if they should be powered on, False otherwise
948 @return: The success of the operation (none failed)
952 command = constants.OOB_POWER_ON
954 command = constants.OOB_POWER_OFF
956 op = opcodes.OpOobCommand(node_names=node_list,
959 timeout=opts.oob_timeout,
960 power_delay=opts.power_delay)
961 result = SubmitOpCode(op, opts=opts)
963 for node_result in result:
964 (node_tuple, data_tuple) = node_result
965 (_, node_name) = node_tuple
966 (data_status, _) = data_tuple
967 if data_status != constants.RS_NORMAL:
968 assert data_status != constants.RS_UNAVAIL
970 ToStderr("There was a problem changing power for %s, please investigate",
979 def _InstanceStart(opts, inst_list, start):
980 """Puts the instances in the list to desired state.
982 @param opts: The command line options selected by the user
983 @param inst_list: The list of instances to operate on
984 @param start: True if they should be started, False for shutdown
985 @return: The success of the operation (none failed)
989 opcls = opcodes.OpInstanceStartup
990 text_submit, text_success, text_failed = ("startup", "started", "starting")
992 opcls = compat.partial(opcodes.OpInstanceShutdown,
993 timeout=opts.shutdown_timeout)
994 text_submit, text_success, text_failed = ("shutdown", "stopped", "stopping")
996 jex = JobExecutor(opts=opts)
998 for inst in inst_list:
999 ToStdout("Submit %s of instance %s", text_submit, inst)
1000 op = opcls(instance_name=inst)
1001 jex.QueueJob(inst, op)
1003 results = jex.GetResults()
1004 bad_cnt = len([1 for (success, _) in results if not success])
1007 ToStdout("All instances have been %s successfully", text_success)
1009 ToStderr("There were errors while %s instances:\n"
1010 "%d error(s) out of %d instance(s)", text_failed, bad_cnt,
1017 class _RunWhenNodesReachableHelper:
1018 """Helper class to make shared internal state sharing easier.
1020 @ivar success: Indicates if all action_cb calls were successful
1023 def __init__(self, node_list, action_cb, node2ip, port, feedback_fn,
1024 _ping_fn=netutils.TcpPing, _sleep_fn=time.sleep):
1027 @param node_list: The list of nodes to be reachable
1028 @param action_cb: Callback called when a new host is reachable
1030 @param node2ip: Node to ip mapping
1031 @param port: The port to use for the TCP ping
1032 @param feedback_fn: The function used for feedback
1033 @param _ping_fn: Function to check reachabilty (for unittest use only)
1034 @param _sleep_fn: Function to sleep (for unittest use only)
1037 self.down = set(node_list)
1039 self.node2ip = node2ip
1041 self.action_cb = action_cb
1043 self.feedback_fn = feedback_fn
1044 self._ping_fn = _ping_fn
1045 self._sleep_fn = _sleep_fn
1048 """When called we run action_cb.
1050 @raises utils.RetryAgain: When there are still down nodes
1053 if not self.action_cb(self.up):
1054 self.success = False
1057 raise utils.RetryAgain()
1061 def Wait(self, secs):
1062 """Checks if a host is up or waits remaining seconds.
1064 @param secs: The secs remaining
1068 for node in self.down:
1069 if self._ping_fn(self.node2ip[node], self.port, timeout=_EPO_PING_TIMEOUT,
1070 live_port_needed=True):
1071 self.feedback_fn("Node %s became available" % node)
1073 self.down -= self.up
1074 # If we have a node available there is the possibility to run the
1075 # action callback successfully, therefore we don't wait and return
1078 self._sleep_fn(max(0.0, start + secs - time.time()))
1081 def _RunWhenNodesReachable(node_list, action_cb, interval):
1082 """Run action_cb when nodes become reachable.
1084 @param node_list: The list of nodes to be reachable
1085 @param action_cb: Callback called when a new host is reachable
1086 @param interval: The earliest time to retry
1089 client = GetClient()
1090 cluster_info = client.QueryClusterInfo()
1091 if cluster_info["primary_ip_version"] == constants.IP4_VERSION:
1092 family = netutils.IPAddress.family
1094 family = netutils.IP6Address.family
1096 node2ip = dict((node, netutils.GetHostname(node, family=family).ip)
1097 for node in node_list)
1099 port = netutils.GetDaemonPort(constants.NODED)
1100 helper = _RunWhenNodesReachableHelper(node_list, action_cb, node2ip, port,
1104 return utils.Retry(helper, interval, _EPO_REACHABLE_TIMEOUT,
1105 wait_fn=helper.Wait)
1106 except utils.RetryTimeout:
1107 ToStderr("Time exceeded while waiting for nodes to become reachable"
1108 " again:\n - %s", " - ".join(helper.down))
1112 def _MaybeInstanceStartup(opts, inst_map, nodes_online,
1113 _instance_start_fn=_InstanceStart):
1114 """Start the instances conditional based on node_states.
1116 @param opts: The command line options selected by the user
1117 @param inst_map: A dict of inst -> nodes mapping
1118 @param nodes_online: A list of nodes online
1119 @param _instance_start_fn: Callback to start instances (unittest use only)
1120 @return: Success of the operation on all instances
1123 start_inst_list = []
1124 for (inst, nodes) in inst_map.items():
1125 if not (nodes - nodes_online):
1126 # All nodes the instance lives on are back online
1127 start_inst_list.append(inst)
1129 for inst in start_inst_list:
1133 return _instance_start_fn(opts, start_inst_list, True)
1138 def _EpoOn(opts, full_node_list, node_list, inst_map):
1139 """Does the actual power on.
1141 @param opts: The command line options selected by the user
1142 @param full_node_list: All nodes to operate on (includes nodes not supporting
1144 @param node_list: The list of nodes to operate on (all need to support OOB)
1145 @param inst_map: A dict of inst -> nodes mapping
1146 @return: The desired exit status
1149 if node_list and not _OobPower(opts, node_list, False):
1150 ToStderr("Not all nodes seem to get back up, investigate and start"
1151 " manually if needed")
1153 # Wait for the nodes to be back up
1154 action_cb = compat.partial(_MaybeInstanceStartup, opts, dict(inst_map))
1156 ToStdout("Waiting until all nodes are available again")
1157 if not _RunWhenNodesReachable(full_node_list, action_cb, _EPO_PING_INTERVAL):
1158 ToStderr("Please investigate and start stopped instances manually")
1159 return constants.EXIT_FAILURE
1161 return constants.EXIT_SUCCESS
1164 def _EpoOff(opts, node_list, inst_map):
1165 """Does the actual power off.
1167 @param opts: The command line options selected by the user
1168 @param node_list: The list of nodes to operate on (all need to support OOB)
1169 @param inst_map: A dict of inst -> nodes mapping
1170 @return: The desired exit status
1173 if not _InstanceStart(opts, inst_map.keys(), False):
1174 ToStderr("Please investigate and stop instances manually before continuing")
1175 return constants.EXIT_FAILURE
1178 return constants.EXIT_SUCCESS
1180 if _OobPower(opts, node_list, False):
1181 return constants.EXIT_SUCCESS
1183 return constants.EXIT_FAILURE
1186 def Epo(opts, args):
1189 @param opts: the command line options selected by the user
1191 @param args: should contain only one element, the subcommand
1193 @return: the desired exit code
1196 if opts.groups and opts.show_all:
1197 ToStderr("Only one of --groups or --all are allowed")
1198 return constants.EXIT_FAILURE
1199 elif args and opts.show_all:
1200 ToStderr("Arguments in combination with --all are not allowed")
1201 return constants.EXIT_FAILURE
1203 client = GetClient()
1206 node_query_list = itertools.chain(*client.QueryGroups(names=args,
1207 fields=["node_list"],
1210 node_query_list = args
1212 result = client.QueryNodes(names=node_query_list,
1213 fields=["name", "master", "pinst_list",
1214 "sinst_list", "powered", "offline"],
1218 for (idx, (node, master, pinsts, sinsts, powered,
1219 offline)) in enumerate(result):
1220 # Normalize the node_query_list as well
1221 if not opts.show_all:
1222 node_query_list[idx] = node
1224 for inst in (pinsts + sinsts):
1225 if inst in inst_map:
1227 inst_map[inst].add(node)
1229 inst_map[inst] = set()
1231 inst_map[inst] = set([node])
1233 if master and opts.on:
1234 # We ignore the master for turning on the machines, in fact we are
1235 # already operating on the master at this point :)
1237 elif master and not opts.show_all:
1238 ToStderr("%s is the master node, please do a master-failover to another"
1239 " node not affected by the EPO or use --all if you intend to"
1240 " shutdown the whole cluster", node)
1241 return constants.EXIT_FAILURE
1242 elif powered is None:
1243 ToStdout("Node %s does not support out-of-band handling, it can not be"
1244 " handled in a fully automated manner", node)
1245 elif powered == opts.on:
1246 ToStdout("Node %s is already in desired power state, skipping", node)
1247 elif not offline or (offline and powered):
1248 node_list.append(node)
1250 if not opts.force and not ConfirmOperation(node_query_list, "nodes", "epo"):
1251 return constants.EXIT_FAILURE
1254 return _EpoOn(opts, node_query_list, node_list, inst_map)
1256 return _EpoOff(opts, node_list, inst_map)
1261 InitCluster, [ArgHost(min=1, max=1)],
1262 [BACKEND_OPT, CP_SIZE_OPT, ENABLED_HV_OPT, GLOBAL_FILEDIR_OPT,
1263 HVLIST_OPT, MAC_PREFIX_OPT, MASTER_NETDEV_OPT, NIC_PARAMS_OPT,
1264 NOLVM_STORAGE_OPT, NOMODIFY_ETCHOSTS_OPT, NOMODIFY_SSH_SETUP_OPT,
1265 SECONDARY_IP_OPT, VG_NAME_OPT, MAINTAIN_NODE_HEALTH_OPT,
1266 UIDPOOL_OPT, DRBD_HELPER_OPT, NODRBD_STORAGE_OPT,
1267 DEFAULT_IALLOCATOR_OPT, PRIMARY_IP_VERSION_OPT, PREALLOC_WIPE_DISKS_OPT,
1268 NODE_PARAMS_OPT, GLOBAL_SHARED_FILEDIR_OPT],
1269 "[opts...] <cluster_name>", "Initialises a new cluster configuration"),
1271 DestroyCluster, ARGS_NONE, [YES_DOIT_OPT],
1272 "", "Destroy cluster"),
1274 RenameCluster, [ArgHost(min=1, max=1)],
1275 [FORCE_OPT, DRY_RUN_OPT],
1277 "Renames the cluster"),
1279 RedistributeConfig, ARGS_NONE, [SUBMIT_OPT, DRY_RUN_OPT, PRIORITY_OPT],
1280 "", "Forces a push of the configuration file and ssconf files"
1281 " to the nodes in the cluster"),
1283 VerifyCluster, ARGS_NONE,
1284 [VERBOSE_OPT, DEBUG_SIMERR_OPT, ERROR_CODES_OPT, NONPLUS1_OPT,
1285 DRY_RUN_OPT, PRIORITY_OPT, NODEGROUP_OPT],
1286 "", "Does a check on the cluster configuration"),
1288 VerifyDisks, ARGS_NONE, [PRIORITY_OPT],
1289 "", "Does a check on the cluster disk status"),
1290 "repair-disk-sizes": (
1291 RepairDiskSizes, ARGS_MANY_INSTANCES, [DRY_RUN_OPT, PRIORITY_OPT],
1292 "", "Updates mismatches in recorded disk sizes"),
1293 "master-failover": (
1294 MasterFailover, ARGS_NONE, [NOVOTING_OPT],
1295 "", "Makes the current node the master"),
1297 MasterPing, ARGS_NONE, [],
1298 "", "Checks if the master is alive"),
1300 ShowClusterVersion, ARGS_NONE, [],
1301 "", "Shows the cluster version"),
1303 ShowClusterMaster, ARGS_NONE, [],
1304 "", "Shows the cluster master"),
1306 ClusterCopyFile, [ArgFile(min=1, max=1)],
1307 [NODE_LIST_OPT, USE_REPL_NET_OPT, NODEGROUP_OPT],
1308 "[-n node...] <filename>", "Copies a file to all (or only some) nodes"),
1310 RunClusterCommand, [ArgCommand(min=1)],
1311 [NODE_LIST_OPT, NODEGROUP_OPT],
1312 "[-n node...] <command>", "Runs a command on all (or only some) nodes"),
1314 ShowClusterConfig, ARGS_NONE, [ROMAN_OPT],
1315 "[--roman]", "Show cluster configuration"),
1317 ListTags, ARGS_NONE, [], "", "List the tags of the cluster"),
1319 AddTags, [ArgUnknown()], [TAG_SRC_OPT, PRIORITY_OPT],
1320 "tag...", "Add tags to the cluster"),
1322 RemoveTags, [ArgUnknown()], [TAG_SRC_OPT, PRIORITY_OPT],
1323 "tag...", "Remove tags from the cluster"),
1325 SearchTags, [ArgUnknown(min=1, max=1)], [PRIORITY_OPT], "",
1326 "Searches the tags on all objects on"
1327 " the cluster for a given pattern (regex)"),
1330 [ArgChoice(min=1, max=1, choices=["drain", "undrain", "info"])],
1331 [], "drain|undrain|info", "Change queue properties"),
1334 [ArgChoice(min=1, max=1, choices=["pause", "continue", "info"]),
1335 ArgSuggest(min=0, max=1, choices=["30m", "1h", "4h"])],
1337 "{pause <timespec>|continue|info}", "Change watcher properties"),
1339 SetClusterParams, ARGS_NONE,
1340 [BACKEND_OPT, CP_SIZE_OPT, ENABLED_HV_OPT, HVLIST_OPT, MASTER_NETDEV_OPT,
1341 NIC_PARAMS_OPT, NOLVM_STORAGE_OPT, VG_NAME_OPT, MAINTAIN_NODE_HEALTH_OPT,
1342 UIDPOOL_OPT, ADD_UIDS_OPT, REMOVE_UIDS_OPT, DRBD_HELPER_OPT,
1343 NODRBD_STORAGE_OPT, DEFAULT_IALLOCATOR_OPT, RESERVED_LVS_OPT,
1344 DRY_RUN_OPT, PRIORITY_OPT, PREALLOC_WIPE_DISKS_OPT, NODE_PARAMS_OPT],
1346 "Alters the parameters of the cluster"),
1348 RenewCrypto, ARGS_NONE,
1349 [NEW_CLUSTER_CERT_OPT, NEW_RAPI_CERT_OPT, RAPI_CERT_OPT,
1350 NEW_CONFD_HMAC_KEY_OPT, FORCE_OPT,
1351 NEW_CLUSTER_DOMAIN_SECRET_OPT, CLUSTER_DOMAIN_SECRET_OPT],
1353 "Renews cluster certificates, keys and secrets"),
1355 Epo, [ArgUnknown()],
1356 [FORCE_OPT, ON_OPT, GROUPS_OPT, ALL_OPT, OOB_TIMEOUT_OPT,
1357 SHUTDOWN_TIMEOUT_OPT, POWER_DELAY_OPT],
1359 "Performs an emergency power-off on given args"),
1363 #: dictionary with aliases for commands
1365 "masterfailover": "master-failover",
1370 return GenericMain(commands, override={"tag_type": constants.TAG_CLUSTER},