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
22 # pylint: disable-msg=W0401,W0614
23 # W0401: Wildcard import ganeti.cli
24 # W0614: Unused import %s from wildcard import (since we need cli)
27 from optparse import make_option
30 from ganeti.cli import *
31 from ganeti import opcodes
32 from ganeti import constants
33 from ganeti import errors
34 from ganeti import utils
35 from ganeti import bootstrap
36 from ganeti import ssh
40 def InitCluster(opts, args):
41 """Initialize the cluster.
43 @param opts: the command line options selected by the user
45 @param args: should contain only one element, the desired
48 @return: the desired exit code
51 if not opts.lvm_storage and opts.vg_name:
52 ToStderr("Options --no-lvm-storage and --vg-name conflict.")
55 vg_name = opts.vg_name
56 if opts.lvm_storage and not opts.vg_name:
57 vg_name = constants.DEFAULT_VG
59 hvlist = opts.enabled_hypervisors
60 if hvlist is not None:
61 hvlist = hvlist.split(",")
63 hvlist = [opts.default_hypervisor]
65 # avoid an impossible situation
66 if opts.default_hypervisor not in hvlist:
67 ToStderr("The default hypervisor requested (%s) is not"
68 " within the enabled hypervisor list (%s)" %
69 (opts.default_hypervisor, hvlist))
72 hvparams = opts.hvparams
74 # a list of (name, dict) we can pass directly to dict()
75 hvparams = dict(opts.hvparams)
77 # otherwise init as empty dict
80 beparams = opts.beparams
81 # check for invalid parameters
82 for parameter in beparams:
83 if parameter not in constants.BES_PARAMETERS:
84 ToStderr("Invalid backend parameter: %s", parameter)
87 # prepare beparams dict
88 for parameter in constants.BES_PARAMETERS:
89 if parameter not in beparams:
90 beparams[parameter] = constants.BEC_DEFAULTS[parameter]
94 beparams[constants.BE_VCPUS] = int(beparams[constants.BE_VCPUS])
96 ToStderr("%s must be an integer", constants.BE_VCPUS)
99 if not isinstance(beparams[constants.BE_MEMORY], int):
100 beparams[constants.BE_MEMORY] = utils.ParseUnit(
101 beparams[constants.BE_MEMORY])
103 # prepare hvparams dict
104 for hv in constants.HYPER_TYPES:
105 if hv not in hvparams:
107 for parameter in constants.HVC_DEFAULTS[hv]:
108 if parameter not in hvparams[hv]:
109 hvparams[hv][parameter] = constants.HVC_DEFAULTS[hv][parameter]
112 if hv not in constants.HYPER_TYPES:
113 ToStderr("invalid hypervisor: %s", hv)
116 bootstrap.InitCluster(cluster_name=args[0],
117 secondary_ip=opts.secondary_ip,
119 mac_prefix=opts.mac_prefix,
120 def_bridge=opts.def_bridge,
121 master_netdev=opts.master_netdev,
122 file_storage_dir=opts.file_storage_dir,
123 enabled_hypervisors=hvlist,
124 default_hypervisor=opts.default_hypervisor,
127 candidate_pool_size=opts.candidate_pool_size,
133 def DestroyCluster(opts, args):
134 """Destroy the cluster.
136 @param opts: the command line options selected by the user
138 @param args: should be an empty list
140 @return: the desired exit code
143 if not opts.yes_do_it:
144 ToStderr("Destroying a cluster is irreversible. If you really want"
145 " destroy this cluster, supply the --yes-do-it option.")
148 op = opcodes.OpDestroyCluster()
149 master = SubmitOpCode(op)
150 # if we reached this, the opcode didn't fail; we can proceed to
151 # shutdown all the daemons
152 bootstrap.FinalizeClusterDestroy(master)
156 def RenameCluster(opts, args):
157 """Rename the cluster.
159 @param opts: the command line options selected by the user
161 @param args: should contain only one element, the new cluster name
163 @return: the desired exit code
168 usertext = ("This will rename the cluster to '%s'. If you are connected"
169 " over the network to the cluster name, the operation is very"
170 " dangerous as the IP address will be removed from the node"
171 " and the change may not go through. Continue?") % name
172 if not AskUser(usertext):
175 op = opcodes.OpRenameCluster(name=name)
180 def RedistributeConfig(opts, args):
181 """Forces push of the cluster configuration.
183 @param opts: the command line options selected by the user
185 @param args: empty list
187 @return: the desired exit code
190 op = opcodes.OpRedistributeConfig()
191 SubmitOrSend(op, opts)
195 def ShowClusterVersion(opts, args):
196 """Write version of ganeti software to the standard output.
198 @param opts: the command line options selected by the user
200 @param args: should be an empty list
202 @return: the desired exit code
206 result = cl.QueryClusterInfo()
207 ToStdout("Software version: %s", result["software_version"])
208 ToStdout("Internode protocol: %s", result["protocol_version"])
209 ToStdout("Configuration format: %s", result["config_version"])
210 ToStdout("OS api version: %s", result["os_api_version"])
211 ToStdout("Export interface: %s", result["export_version"])
215 def ShowClusterMaster(opts, args):
216 """Write name of master node to the standard output.
218 @param opts: the command line options selected by the user
220 @param args: should be an empty list
222 @return: the desired exit code
225 ToStdout("%s", GetClient().QueryConfigValues(["master_node"])[0])
229 def ShowClusterConfig(opts, args):
230 """Shows cluster information.
232 @param opts: the command line options selected by the user
234 @param args: should be an empty list
236 @return: the desired exit code
240 result = cl.QueryClusterInfo()
242 ToStdout("Cluster name: %s", result["name"])
244 ToStdout("Master node: %s", result["master"])
246 ToStdout("Architecture (this node): %s (%s)",
247 result["architecture"][0], result["architecture"][1])
249 ToStdout("Default hypervisor: %s", result["default_hypervisor"])
250 ToStdout("Enabled hypervisors: %s", ", ".join(result["enabled_hypervisors"]))
252 ToStdout("Hypervisor parameters:")
253 for hv_name, hv_dict in result["hvparams"].items():
254 ToStdout(" - %s:", hv_name)
255 for item, val in hv_dict.iteritems():
256 ToStdout(" %s: %s", item, val)
258 ToStdout("Cluster parameters:")
259 ToStdout(" - candidate pool size: %s", result["candidate_pool_size"])
261 ToStdout("Default instance parameters:")
262 for gr_name, gr_dict in result["beparams"].items():
263 ToStdout(" - %s:", gr_name)
264 for item, val in gr_dict.iteritems():
265 ToStdout(" %s: %s", item, val)
270 def ClusterCopyFile(opts, args):
271 """Copy a file from master to some nodes.
273 @param opts: the command line options selected by the user
275 @param args: should contain only one element, the path of
276 the file to be copied
278 @return: the desired exit code
282 if not os.path.exists(filename):
283 raise errors.OpPrereqError("No such filename '%s'" % filename)
287 myname = utils.HostInfo().name
289 cluster_name = cl.QueryConfigValues(["cluster_name"])[0]
291 results = GetOnlineNodes(nodes=opts.nodes, cl=cl)
292 results = [name for name in results if name != myname]
294 srun = ssh.SshRunner(cluster_name=cluster_name)
296 if not srun.CopyFileToNode(node, filename):
297 ToStderr("Copy of file %s to node %s failed", filename, node)
302 def RunClusterCommand(opts, args):
303 """Run a command on some nodes.
305 @param opts: the command line options selected by the user
307 @param args: should contain the command to be run and its arguments
309 @return: the desired exit code
314 command = " ".join(args)
316 nodes = GetOnlineNodes(nodes=opts.nodes, cl=cl)
318 cluster_name, master_node = cl.QueryConfigValues(["cluster_name",
321 srun = ssh.SshRunner(cluster_name=cluster_name)
323 # Make sure master node is at list end
324 if master_node in nodes:
325 nodes.remove(master_node)
326 nodes.append(master_node)
329 result = srun.Run(name, "root", command)
330 ToStdout("------------------------------------------------")
331 ToStdout("node: %s", name)
332 ToStdout("%s", result.output)
333 ToStdout("return code = %s", result.exit_code)
338 def VerifyCluster(opts, args):
339 """Verify integrity of cluster, performing various test on nodes.
341 @param opts: the command line options selected by the user
343 @param args: should be an empty list
345 @return: the desired exit code
349 if opts.skip_nplusone_mem:
350 skip_checks.append(constants.VERIFY_NPLUSONE_MEM)
351 op = opcodes.OpVerifyCluster(skip_checks=skip_checks)
358 def VerifyDisks(opts, args):
359 """Verify integrity of cluster disks.
361 @param opts: the command line options selected by the user
363 @param args: should be an empty list
365 @return: the desired exit code
368 op = opcodes.OpVerifyDisks()
369 result = SubmitOpCode(op)
370 if not isinstance(result, (list, tuple)) or len(result) != 4:
371 raise errors.ProgrammerError("Unknown result type for OpVerifyDisks")
373 nodes, nlvm, instances, missing = result
376 ToStdout("Nodes unreachable or with bad data:")
378 ToStdout("\t%s", name)
379 retcode = constants.EXIT_SUCCESS
382 for node, text in nlvm.iteritems():
383 ToStdout("Error on node %s: LVM error: %s",
384 node, utils.SafeEncode(text[-400:]))
386 ToStdout("You need to fix these nodes first before fixing instances")
389 for iname in instances:
392 op = opcodes.OpActivateInstanceDisks(instance_name=iname)
394 ToStdout("Activating disks for instance '%s'", iname)
396 except errors.GenericError, err:
397 nret, msg = FormatError(err)
399 ToStderr("Error activating disks for instance %s: %s", iname, msg)
402 for iname, ival in missing.iteritems():
403 all_missing = utils.all(ival, lambda x: x[0] in nlvm)
405 ToStdout("Instance %s cannot be verified as it lives on"
406 " broken nodes", iname)
408 ToStdout("Instance %s has missing logical volumes:", iname)
410 for node, vol in ival:
412 ToStdout("\tbroken node %s /dev/xenvg/%s", node, vol)
414 ToStdout("\t%s /dev/xenvg/%s", node, vol)
415 ToStdout("You need to run replace_disks for all the above"
416 " instances, if this message persist after fixing nodes.")
423 def MasterFailover(opts, args):
424 """Failover the master node.
426 This command, when run on a non-master node, will cause the current
427 master to cease being master, and the non-master to become new
430 @param opts: the command line options selected by the user
432 @param args: should be an empty list
434 @return: the desired exit code
437 return bootstrap.MasterFailover()
440 def SearchTags(opts, args):
441 """Searches the tags on all the cluster.
443 @param opts: the command line options selected by the user
445 @param args: should contain only one element, the tag pattern
447 @return: the desired exit code
450 op = opcodes.OpSearchTags(pattern=args[0])
451 result = SubmitOpCode(op)
454 result = list(result)
456 for path, tag in result:
457 ToStdout("%s %s", path, tag)
460 def SetClusterParams(opts, args):
461 """Modify the cluster.
463 @param opts: the command line options selected by the user
465 @param args: should be an empty list
467 @return: the desired exit code
470 if not (not opts.lvm_storage or opts.vg_name or
471 opts.enabled_hypervisors or opts.hvparams or
472 opts.beparams or opts.candidate_pool_size is not None):
473 ToStderr("Please give at least one of the parameters.")
476 vg_name = opts.vg_name
477 if not opts.lvm_storage and opts.vg_name:
478 ToStdout("Options --no-lvm-storage and --vg-name conflict.")
481 hvlist = opts.enabled_hypervisors
482 if hvlist is not None:
483 hvlist = hvlist.split(",")
485 hvparams = opts.hvparams
487 # a list of (name, dict) we can pass directly to dict()
488 hvparams = dict(opts.hvparams)
490 beparams = opts.beparams
492 op = opcodes.OpSetClusterParams(vg_name=opts.vg_name,
493 enabled_hypervisors=hvlist,
496 candidate_pool_size=opts.candidate_pool_size)
501 def QueueOps(opts, args):
504 @param opts: the command line options selected by the user
506 @param args: should contain only one element, the subcommand
508 @return: the desired exit code
513 if command in ("drain", "undrain"):
514 drain_flag = command == "drain"
515 client.SetQueueDrainFlag(drain_flag)
516 elif command == "info":
517 result = client.QueryConfigValues(["drain_flag"])
522 ToStdout("The drain flag is %s" % val)
525 # this is an option common to more than one command, so we declare
526 # it here and reuse it
527 node_option = make_option("-n", "--node", action="append", dest="nodes",
528 help="Node to copy to (if not given, all nodes),"
529 " can be given multiple times",
530 metavar="<node>", default=[])
533 'init': (InitCluster, ARGS_ONE,
535 make_option("-s", "--secondary-ip", dest="secondary_ip",
536 help="Specify the secondary ip for this node;"
537 " if given, the entire cluster must have secondary"
539 metavar="ADDRESS", default=None),
540 make_option("-m", "--mac-prefix", dest="mac_prefix",
541 help="Specify the mac prefix for the instance IP"
542 " addresses, in the format XX:XX:XX",
544 default=constants.DEFAULT_MAC_PREFIX,),
545 make_option("-g", "--vg-name", dest="vg_name",
546 help="Specify the volume group name "
547 " (cluster-wide) for disk allocation [xenvg]",
550 make_option("-b", "--bridge", dest="def_bridge",
551 help="Specify the default bridge name (cluster-wide)"
552 " to connect the instances to [%s]" %
553 constants.DEFAULT_BRIDGE,
555 default=constants.DEFAULT_BRIDGE,),
556 make_option("--master-netdev", dest="master_netdev",
557 help="Specify the node interface (cluster-wide)"
558 " on which the master IP address will be added "
559 " [%s]" % constants.DEFAULT_BRIDGE,
561 default=constants.DEFAULT_BRIDGE,),
562 make_option("--file-storage-dir", dest="file_storage_dir",
563 help="Specify the default directory (cluster-wide)"
564 " for storing the file-based disks [%s]" %
565 constants.DEFAULT_FILE_STORAGE_DIR,
567 default=constants.DEFAULT_FILE_STORAGE_DIR,),
568 make_option("--no-lvm-storage", dest="lvm_storage",
569 help="No support for lvm based instances"
571 action="store_false", default=True,),
572 make_option("--enabled-hypervisors", dest="enabled_hypervisors",
573 help="Comma-separated list of hypervisors",
574 type="string", default=None),
575 make_option("-t", "--default-hypervisor",
576 dest="default_hypervisor",
577 help="Default hypervisor to use for instance creation",
578 choices=list(constants.HYPER_TYPES),
579 default=constants.DEFAULT_ENABLED_HYPERVISOR),
580 ikv_option("-H", "--hypervisor-parameters", dest="hvparams",
581 help="Hypervisor and hypervisor options, in the"
583 " hypervisor:option=value,option=value,...",
587 keyval_option("-B", "--backend-parameters", dest="beparams",
588 type="keyval", default={},
589 help="Backend parameters"),
590 make_option("-C", "--candidate-pool-size",
591 default=constants.MASTER_POOL_SIZE_DEFAULT,
592 help="Set the candidate pool size",
593 dest="candidate_pool_size", type="int"),
595 "[opts...] <cluster_name>",
596 "Initialises a new cluster configuration"),
597 'destroy': (DestroyCluster, ARGS_NONE,
599 make_option("--yes-do-it", dest="yes_do_it",
600 help="Destroy cluster",
601 action="store_true"),
603 "", "Destroy cluster"),
604 'rename': (RenameCluster, ARGS_ONE, [DEBUG_OPT, FORCE_OPT],
606 "Renames the cluster"),
607 'redist-conf': (RedistributeConfig, ARGS_NONE, [DEBUG_OPT, SUBMIT_OPT],
609 "Forces a push of the configuration file and ssconf files"
610 " to the nodes in the cluster"),
611 'verify': (VerifyCluster, ARGS_NONE, [DEBUG_OPT,
612 make_option("--no-nplus1-mem", dest="skip_nplusone_mem",
613 help="Skip N+1 memory redundancy tests",
617 "", "Does a check on the cluster configuration"),
618 'verify-disks': (VerifyDisks, ARGS_NONE, [DEBUG_OPT],
619 "", "Does a check on the cluster disk status"),
620 'masterfailover': (MasterFailover, ARGS_NONE, [DEBUG_OPT],
621 "", "Makes the current node the master"),
622 'version': (ShowClusterVersion, ARGS_NONE, [DEBUG_OPT],
623 "", "Shows the cluster version"),
624 'getmaster': (ShowClusterMaster, ARGS_NONE, [DEBUG_OPT],
625 "", "Shows the cluster master"),
626 'copyfile': (ClusterCopyFile, ARGS_ONE, [DEBUG_OPT, node_option],
627 "[-n node...] <filename>",
628 "Copies a file to all (or only some) nodes"),
629 'command': (RunClusterCommand, ARGS_ATLEAST(1), [DEBUG_OPT, node_option],
630 "[-n node...] <command>",
631 "Runs a command on all (or only some) nodes"),
632 'info': (ShowClusterConfig, ARGS_NONE, [DEBUG_OPT],
633 "", "Show cluster configuration"),
634 'list-tags': (ListTags, ARGS_NONE,
635 [DEBUG_OPT], "", "List the tags of the cluster"),
636 'add-tags': (AddTags, ARGS_ANY, [DEBUG_OPT, TAG_SRC_OPT],
637 "tag...", "Add tags to the cluster"),
638 'remove-tags': (RemoveTags, ARGS_ANY, [DEBUG_OPT, TAG_SRC_OPT],
639 "tag...", "Remove tags from the cluster"),
640 'search-tags': (SearchTags, ARGS_ONE,
641 [DEBUG_OPT], "", "Searches the tags on all objects on"
642 " the cluster for a given pattern (regex)"),
643 'queue': (QueueOps, ARGS_ONE, [DEBUG_OPT],
644 "drain|undrain|info", "Change queue properties"),
645 'modify': (SetClusterParams, ARGS_NONE,
647 make_option("-g", "--vg-name", dest="vg_name",
648 help="Specify the volume group name "
649 " (cluster-wide) for disk allocation "
650 "and enable lvm based storage",
652 make_option("--no-lvm-storage", dest="lvm_storage",
653 help="Disable support for lvm based instances"
655 action="store_false", default=True,),
656 make_option("--enabled-hypervisors", dest="enabled_hypervisors",
657 help="Comma-separated list of hypervisors",
658 type="string", default=None),
659 ikv_option("-H", "--hypervisor-parameters", dest="hvparams",
660 help="Hypervisor and hypervisor options, in the"
662 " hypervisor:option=value,option=value,...",
666 keyval_option("-B", "--backend-parameters", dest="beparams",
667 type="keyval", default={},
668 help="Backend parameters"),
669 make_option("-C", "--candidate-pool-size", default=None,
670 help="Set the candidate pool size",
671 dest="candidate_pool_size", type="int"),
674 "Alters the parameters of the cluster"),
677 if __name__ == '__main__':
678 sys.exit(GenericMain(commands, override={"tag_type": constants.TAG_CLUSTER}))