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 ShowClusterVersion(opts, args):
181 """Write version of ganeti software to the standard output.
183 @param opts: the command line options selected by the user
185 @param args: should be an empty list
187 @return: the desired exit code
190 op = opcodes.OpQueryClusterInfo()
191 result = SubmitOpCode(op)
192 ToStdout("Software version: %s", result["software_version"])
193 ToStdout("Internode protocol: %s", result["protocol_version"])
194 ToStdout("Configuration format: %s", result["config_version"])
195 ToStdout("OS api version: %s", result["os_api_version"])
196 ToStdout("Export interface: %s", result["export_version"])
200 def ShowClusterMaster(opts, args):
201 """Write name of master node to the standard output.
203 @param opts: the command line options selected by the user
205 @param args: should be an empty list
207 @return: the desired exit code
210 ToStdout("%s", GetClient().QueryConfigValues(["master_node"])[0])
214 def ShowClusterConfig(opts, args):
215 """Shows cluster information.
217 @param opts: the command line options selected by the user
219 @param args: should be an empty list
221 @return: the desired exit code
224 op = opcodes.OpQueryClusterInfo()
225 result = SubmitOpCode(op)
227 ToStdout("Cluster name: %s", result["name"])
229 ToStdout("Master node: %s", result["master"])
231 ToStdout("Architecture (this node): %s (%s)",
232 result["architecture"][0], result["architecture"][1])
234 ToStdout("Default hypervisor: %s", result["default_hypervisor"])
235 ToStdout("Enabled hypervisors: %s", ", ".join(result["enabled_hypervisors"]))
237 ToStdout("Hypervisor parameters:")
238 for hv_name, hv_dict in result["hvparams"].items():
239 ToStdout(" - %s:", hv_name)
240 for item, val in hv_dict.iteritems():
241 ToStdout(" %s: %s", item, val)
243 ToStdout("Cluster parameters:")
244 ToStdout(" - candidate pool size: %s", result["candidate_pool_size"])
246 ToStdout("Default instance parameters:")
247 for gr_name, gr_dict in result["beparams"].items():
248 ToStdout(" - %s:", gr_name)
249 for item, val in gr_dict.iteritems():
250 ToStdout(" %s: %s", item, val)
255 def ClusterCopyFile(opts, args):
256 """Copy a file from master to some nodes.
258 @param opts: the command line options selected by the user
260 @param args: should contain only one element, the path of
261 the file to be copied
263 @return: the desired exit code
267 if not os.path.exists(filename):
268 raise errors.OpPrereqError("No such filename '%s'" % filename)
272 myname = utils.HostInfo().name
274 cluster_name = cl.QueryConfigValues(["cluster_name"])[0]
276 op = opcodes.OpQueryNodes(output_fields=["name"], names=opts.nodes)
277 results = [row[0] for row in SubmitOpCode(op, cl=cl) if row[0] != myname]
279 srun = ssh.SshRunner(cluster_name=cluster_name)
281 if not srun.CopyFileToNode(node, filename):
282 ToStderr("Copy of file %s to node %s failed", filename, node)
287 def RunClusterCommand(opts, args):
288 """Run a command on some nodes.
290 @param opts: the command line options selected by the user
292 @param args: should contain the command to be run and its arguments
294 @return: the desired exit code
299 command = " ".join(args)
300 op = opcodes.OpQueryNodes(output_fields=["name"], names=opts.nodes)
301 nodes = [row[0] for row in SubmitOpCode(op, cl=cl)]
303 cluster_name, master_node = cl.QueryConfigValues(["cluster_name",
306 srun = ssh.SshRunner(cluster_name=cluster_name)
308 # Make sure master node is at list end
309 if master_node in nodes:
310 nodes.remove(master_node)
311 nodes.append(master_node)
314 result = srun.Run(name, "root", command)
315 ToStdout("------------------------------------------------")
316 ToStdout("node: %s", name)
317 ToStdout("%s", result.output)
318 ToStdout("return code = %s", result.exit_code)
323 def VerifyCluster(opts, args):
324 """Verify integrity of cluster, performing various test on nodes.
326 @param opts: the command line options selected by the user
328 @param args: should be an empty list
330 @return: the desired exit code
334 if opts.skip_nplusone_mem:
335 skip_checks.append(constants.VERIFY_NPLUSONE_MEM)
336 op = opcodes.OpVerifyCluster(skip_checks=skip_checks)
343 def VerifyDisks(opts, args):
344 """Verify integrity of cluster disks.
346 @param opts: the command line options selected by the user
348 @param args: should be an empty list
350 @return: the desired exit code
353 op = opcodes.OpVerifyDisks()
354 result = SubmitOpCode(op)
355 if not isinstance(result, (list, tuple)) or len(result) != 4:
356 raise errors.ProgrammerError("Unknown result type for OpVerifyDisks")
358 nodes, nlvm, instances, missing = result
361 ToStdout("Nodes unreachable or with bad data:")
363 ToStdout("\t%s", name)
364 retcode = constants.EXIT_SUCCESS
367 for node, text in nlvm.iteritems():
368 ToStdout("Error on node %s: LVM error: %s",
369 node, text[-400:].encode('string_escape'))
371 ToStdout("You need to fix these nodes first before fixing instances")
374 for iname in instances:
377 op = opcodes.OpActivateInstanceDisks(instance_name=iname)
379 ToStdout("Activating disks for instance '%s'", iname)
381 except errors.GenericError, err:
382 nret, msg = FormatError(err)
384 ToStderr("Error activating disks for instance %s: %s", iname, msg)
387 for iname, ival in missing.iteritems():
388 all_missing = utils.all(ival, lambda x: x[0] in nlvm)
390 ToStdout("Instance %s cannot be verified as it lives on"
391 " broken nodes", iname)
393 ToStdout("Instance %s has missing logical volumes:", iname)
395 for node, vol in ival:
397 ToStdout("\tbroken node %s /dev/xenvg/%s", node, vol)
399 ToStdout("\t%s /dev/xenvg/%s", node, vol)
400 ToStdout("You need to run replace_disks for all the above"
401 " instances, if this message persist after fixing nodes.")
408 def MasterFailover(opts, args):
409 """Failover the master node.
411 This command, when run on a non-master node, will cause the current
412 master to cease being master, and the non-master to become new
415 @param opts: the command line options selected by the user
417 @param args: should be an empty list
419 @return: the desired exit code
422 return bootstrap.MasterFailover()
425 def SearchTags(opts, args):
426 """Searches the tags on all the cluster.
428 @param opts: the command line options selected by the user
430 @param args: should contain only one element, the tag pattern
432 @return: the desired exit code
435 op = opcodes.OpSearchTags(pattern=args[0])
436 result = SubmitOpCode(op)
439 result = list(result)
441 for path, tag in result:
442 ToStdout("%s %s", path, tag)
445 def SetClusterParams(opts, args):
446 """Modify the cluster.
448 @param opts: the command line options selected by the user
450 @param args: should be an empty list
452 @return: the desired exit code
455 if not (not opts.lvm_storage or opts.vg_name or
456 opts.enabled_hypervisors or opts.hvparams or
457 opts.beparams or opts.candidate_pool_size is not None):
458 ToStderr("Please give at least one of the parameters.")
461 vg_name = opts.vg_name
462 if not opts.lvm_storage and opts.vg_name:
463 ToStdout("Options --no-lvm-storage and --vg-name conflict.")
466 hvlist = opts.enabled_hypervisors
467 if hvlist is not None:
468 hvlist = hvlist.split(",")
470 hvparams = opts.hvparams
472 # a list of (name, dict) we can pass directly to dict()
473 hvparams = dict(opts.hvparams)
475 beparams = opts.beparams
477 op = opcodes.OpSetClusterParams(vg_name=opts.vg_name,
478 enabled_hypervisors=hvlist,
481 candidate_pool_size=opts.candidate_pool_size)
486 def QueueOps(opts, args):
489 @param opts: the command line options selected by the user
491 @param args: should contain only one element, the subcommand
493 @return: the desired exit code
498 if command in ("drain", "undrain"):
499 drain_flag = command == "drain"
500 client.SetQueueDrainFlag(drain_flag)
501 elif command == "info":
502 result = client.QueryConfigValues(["drain_flag"])
507 ToStdout("The drain flag is %s" % val)
510 # this is an option common to more than one command, so we declare
511 # it here and reuse it
512 node_option = make_option("-n", "--node", action="append", dest="nodes",
513 help="Node to copy to (if not given, all nodes),"
514 " can be given multiple times",
515 metavar="<node>", default=[])
518 'init': (InitCluster, ARGS_ONE,
520 make_option("-s", "--secondary-ip", dest="secondary_ip",
521 help="Specify the secondary ip for this node;"
522 " if given, the entire cluster must have secondary"
524 metavar="ADDRESS", default=None),
525 make_option("-m", "--mac-prefix", dest="mac_prefix",
526 help="Specify the mac prefix for the instance IP"
527 " addresses, in the format XX:XX:XX",
529 default="aa:00:00",),
530 make_option("-g", "--vg-name", dest="vg_name",
531 help="Specify the volume group name "
532 " (cluster-wide) for disk allocation [xenvg]",
535 make_option("-b", "--bridge", dest="def_bridge",
536 help="Specify the default bridge name (cluster-wide)"
537 " to connect the instances to [%s]" %
538 constants.DEFAULT_BRIDGE,
540 default=constants.DEFAULT_BRIDGE,),
541 make_option("--master-netdev", dest="master_netdev",
542 help="Specify the node interface (cluster-wide)"
543 " on which the master IP address will be added "
544 " [%s]" % constants.DEFAULT_BRIDGE,
546 default=constants.DEFAULT_BRIDGE,),
547 make_option("--file-storage-dir", dest="file_storage_dir",
548 help="Specify the default directory (cluster-wide)"
549 " for storing the file-based disks [%s]" %
550 constants.DEFAULT_FILE_STORAGE_DIR,
552 default=constants.DEFAULT_FILE_STORAGE_DIR,),
553 make_option("--no-lvm-storage", dest="lvm_storage",
554 help="No support for lvm based instances"
556 action="store_false", default=True,),
557 make_option("--enabled-hypervisors", dest="enabled_hypervisors",
558 help="Comma-separated list of hypervisors",
559 type="string", default=None),
560 make_option("-t", "--default-hypervisor",
561 dest="default_hypervisor",
562 help="Default hypervisor to use for instance creation",
563 choices=list(constants.HYPER_TYPES),
564 default=constants.DEFAULT_ENABLED_HYPERVISOR),
565 ikv_option("-H", "--hypervisor-parameters", dest="hvparams",
566 help="Hypervisor and hypervisor options, in the"
568 " hypervisor:option=value,option=value,...",
572 keyval_option("-B", "--backend-parameters", dest="beparams",
573 type="keyval", default={},
574 help="Backend parameters"),
575 make_option("-C", "--candidate-pool-size",
576 default=constants.MASTER_POOL_SIZE_DEFAULT,
577 help="Set the candidate pool size",
578 dest="candidate_pool_size", type="int"),
580 "[opts...] <cluster_name>",
581 "Initialises a new cluster configuration"),
582 'destroy': (DestroyCluster, ARGS_NONE,
584 make_option("--yes-do-it", dest="yes_do_it",
585 help="Destroy cluster",
586 action="store_true"),
588 "", "Destroy cluster"),
589 'rename': (RenameCluster, ARGS_ONE, [DEBUG_OPT, FORCE_OPT],
591 "Renames the cluster"),
592 'verify': (VerifyCluster, ARGS_NONE, [DEBUG_OPT,
593 make_option("--no-nplus1-mem", dest="skip_nplusone_mem",
594 help="Skip N+1 memory redundancy tests",
598 "", "Does a check on the cluster configuration"),
599 'verify-disks': (VerifyDisks, ARGS_NONE, [DEBUG_OPT],
600 "", "Does a check on the cluster disk status"),
601 'masterfailover': (MasterFailover, ARGS_NONE, [DEBUG_OPT],
602 "", "Makes the current node the master"),
603 'version': (ShowClusterVersion, ARGS_NONE, [DEBUG_OPT],
604 "", "Shows the cluster version"),
605 'getmaster': (ShowClusterMaster, ARGS_NONE, [DEBUG_OPT],
606 "", "Shows the cluster master"),
607 'copyfile': (ClusterCopyFile, ARGS_ONE, [DEBUG_OPT, node_option],
608 "[-n node...] <filename>",
609 "Copies a file to all (or only some) nodes"),
610 'command': (RunClusterCommand, ARGS_ATLEAST(1), [DEBUG_OPT, node_option],
611 "[-n node...] <command>",
612 "Runs a command on all (or only some) nodes"),
613 'info': (ShowClusterConfig, ARGS_NONE, [DEBUG_OPT],
614 "", "Show cluster configuration"),
615 'list-tags': (ListTags, ARGS_NONE,
616 [DEBUG_OPT], "", "List the tags of the cluster"),
617 'add-tags': (AddTags, ARGS_ANY, [DEBUG_OPT, TAG_SRC_OPT],
618 "tag...", "Add tags to the cluster"),
619 'remove-tags': (RemoveTags, ARGS_ANY, [DEBUG_OPT, TAG_SRC_OPT],
620 "tag...", "Remove tags from the cluster"),
621 'search-tags': (SearchTags, ARGS_ONE,
622 [DEBUG_OPT], "", "Searches the tags on all objects on"
623 " the cluster for a given pattern (regex)"),
624 'queue': (QueueOps, ARGS_ONE, [DEBUG_OPT],
625 "drain|undrain|info", "Change queue properties"),
626 'modify': (SetClusterParams, ARGS_NONE,
628 make_option("-g", "--vg-name", dest="vg_name",
629 help="Specify the volume group name "
630 " (cluster-wide) for disk allocation "
631 "and enable lvm based storage",
633 make_option("--no-lvm-storage", dest="lvm_storage",
634 help="Disable support for lvm based instances"
636 action="store_false", default=True,),
637 make_option("--enabled-hypervisors", dest="enabled_hypervisors",
638 help="Comma-separated list of hypervisors",
639 type="string", default=None),
640 ikv_option("-H", "--hypervisor-parameters", dest="hvparams",
641 help="Hypervisor and hypervisor options, in the"
643 " hypervisor:option=value,option=value,...",
647 keyval_option("-B", "--backend-parameters", dest="beparams",
648 type="keyval", default={},
649 help="Backend parameters"),
650 make_option("-C", "--candidate-pool-size", default=None,
651 help="Set the candidate pool size",
652 dest="candidate_pool_size", type="int"),
655 "Alters the parameters of the cluster"),
658 if __name__ == '__main__':
659 sys.exit(GenericMain(commands, override={"tag_type": constants.TAG_CLUSTER}))