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,
131 def DestroyCluster(opts, args):
132 """Destroy the cluster.
134 @param opts: the command line options selected by the user
136 @param args: should be an empty list
138 @return: the desired exit code
141 if not opts.yes_do_it:
142 ToStderr("Destroying a cluster is irreversible. If you really want"
143 " destroy this cluster, supply the --yes-do-it option.")
146 op = opcodes.OpDestroyCluster()
147 master = SubmitOpCode(op)
148 # if we reached this, the opcode didn't fail; we can proceed to
149 # shutdown all the daemons
150 bootstrap.FinalizeClusterDestroy(master)
154 def RenameCluster(opts, args):
155 """Rename the cluster.
157 @param opts: the command line options selected by the user
159 @param args: should contain only one element, the new cluster name
161 @return: the desired exit code
166 usertext = ("This will rename the cluster to '%s'. If you are connected"
167 " over the network to the cluster name, the operation is very"
168 " dangerous as the IP address will be removed from the node"
169 " and the change may not go through. Continue?") % name
170 if not AskUser(usertext):
173 op = opcodes.OpRenameCluster(name=name)
178 def ShowClusterVersion(opts, args):
179 """Write version of ganeti software to the standard output.
181 @param opts: the command line options selected by the user
183 @param args: should be an empty list
185 @return: the desired exit code
188 op = opcodes.OpQueryClusterInfo()
189 result = SubmitOpCode(op)
190 ToStdout("Software version: %s", result["software_version"])
191 ToStdout("Internode protocol: %s", result["protocol_version"])
192 ToStdout("Configuration format: %s", result["config_version"])
193 ToStdout("OS api version: %s", result["os_api_version"])
194 ToStdout("Export interface: %s", result["export_version"])
198 def ShowClusterMaster(opts, args):
199 """Write name of master node to the standard output.
201 @param opts: the command line options selected by the user
203 @param args: should be an empty list
205 @return: the desired exit code
208 ToStdout("%s", GetClient().QueryConfigValues(["master_node"])[0])
212 def ShowClusterConfig(opts, args):
213 """Shows cluster information.
215 @param opts: the command line options selected by the user
217 @param args: should be an empty list
219 @return: the desired exit code
222 op = opcodes.OpQueryClusterInfo()
223 result = SubmitOpCode(op)
225 ToStdout("Cluster name: %s", result["name"])
227 ToStdout("Master node: %s", result["master"])
229 ToStdout("Architecture (this node): %s (%s)",
230 result["architecture"][0], result["architecture"][1])
232 ToStdout("Default hypervisor: %s", result["default_hypervisor"])
233 ToStdout("Enabled hypervisors: %s", ", ".join(result["enabled_hypervisors"]))
235 ToStdout("Hypervisor parameters:")
236 for hv_name, hv_dict in result["hvparams"].items():
237 ToStdout(" - %s:", hv_name)
238 for item, val in hv_dict.iteritems():
239 ToStdout(" %s: %s", item, val)
241 ToStdout("Cluster parameters:")
242 ToStdout(" - candidate pool size: %s", result["candidate_pool_size"])
244 ToStdout("Default instance parameters:")
245 for gr_name, gr_dict in result["beparams"].items():
246 ToStdout(" - %s:", gr_name)
247 for item, val in gr_dict.iteritems():
248 ToStdout(" %s: %s", item, val)
253 def ClusterCopyFile(opts, args):
254 """Copy a file from master to some nodes.
256 @param opts: the command line options selected by the user
258 @param args: should contain only one element, the path of
259 the file to be copied
261 @return: the desired exit code
265 if not os.path.exists(filename):
266 raise errors.OpPrereqError("No such filename '%s'" % filename)
270 myname = utils.HostInfo().name
272 cluster_name = cl.QueryConfigValues(["cluster_name"])[0]
274 op = opcodes.OpQueryNodes(output_fields=["name"], names=opts.nodes)
275 results = [row[0] for row in SubmitOpCode(op, cl=cl) if row[0] != myname]
277 srun = ssh.SshRunner(cluster_name=cluster_name)
279 if not srun.CopyFileToNode(node, filename):
280 ToStderr("Copy of file %s to node %s failed", filename, node)
285 def RunClusterCommand(opts, args):
286 """Run a command on some nodes.
288 @param opts: the command line options selected by the user
290 @param args: should contain the command to be run and its arguments
292 @return: the desired exit code
297 command = " ".join(args)
298 op = opcodes.OpQueryNodes(output_fields=["name"], names=opts.nodes)
299 nodes = [row[0] for row in SubmitOpCode(op, cl=cl)]
301 cluster_name, master_node = cl.QueryConfigValues(["cluster_name",
304 srun = ssh.SshRunner(cluster_name=cluster_name)
306 # Make sure master node is at list end
307 if master_node in nodes:
308 nodes.remove(master_node)
309 nodes.append(master_node)
312 result = srun.Run(name, "root", command)
313 ToStdout("------------------------------------------------")
314 ToStdout("node: %s", name)
315 ToStdout("%s", result.output)
316 ToStdout("return code = %s", result.exit_code)
321 def VerifyCluster(opts, args):
322 """Verify integrity of cluster, performing various test on nodes.
324 @param opts: the command line options selected by the user
326 @param args: should be an empty list
328 @return: the desired exit code
332 if opts.skip_nplusone_mem:
333 skip_checks.append(constants.VERIFY_NPLUSONE_MEM)
334 op = opcodes.OpVerifyCluster(skip_checks=skip_checks)
341 def VerifyDisks(opts, args):
342 """Verify integrity of cluster disks.
344 @param opts: the command line options selected by the user
346 @param args: should be an empty list
348 @return: the desired exit code
351 op = opcodes.OpVerifyDisks()
352 result = SubmitOpCode(op)
353 if not isinstance(result, (list, tuple)) or len(result) != 4:
354 raise errors.ProgrammerError("Unknown result type for OpVerifyDisks")
356 nodes, nlvm, instances, missing = result
359 ToStdout("Nodes unreachable or with bad data:")
361 ToStdout("\t%s", name)
362 retcode = constants.EXIT_SUCCESS
365 for node, text in nlvm.iteritems():
366 ToStdout("Error on node %s: LVM error: %s",
367 node, text[-400:].encode('string_escape'))
369 ToStdout("You need to fix these nodes first before fixing instances")
372 for iname in instances:
375 op = opcodes.OpActivateInstanceDisks(instance_name=iname)
377 ToStdout("Activating disks for instance '%s'", iname)
379 except errors.GenericError, err:
380 nret, msg = FormatError(err)
382 ToStderr("Error activating disks for instance %s: %s", iname, msg)
385 for iname, ival in missing.iteritems():
386 all_missing = utils.all(ival, lambda x: x[0] in nlvm)
388 ToStdout("Instance %s cannot be verified as it lives on"
389 " broken nodes", iname)
391 ToStdout("Instance %s has missing logical volumes:", iname)
393 for node, vol in ival:
395 ToStdout("\tbroken node %s /dev/xenvg/%s", node, vol)
397 ToStdout("\t%s /dev/xenvg/%s", node, vol)
398 ToStdout("You need to run replace_disks for all the above"
399 " instances, if this message persist after fixing nodes.")
406 def MasterFailover(opts, args):
407 """Failover the master node.
409 This command, when run on a non-master node, will cause the current
410 master to cease being master, and the non-master to become new
413 @param opts: the command line options selected by the user
415 @param args: should be an empty list
417 @return: the desired exit code
420 return bootstrap.MasterFailover()
423 def SearchTags(opts, args):
424 """Searches the tags on all the cluster.
426 @param opts: the command line options selected by the user
428 @param args: should contain only one element, the tag pattern
430 @return: the desired exit code
433 op = opcodes.OpSearchTags(pattern=args[0])
434 result = SubmitOpCode(op)
437 result = list(result)
439 for path, tag in result:
440 ToStdout("%s %s", path, tag)
443 def SetClusterParams(opts, args):
444 """Modify the cluster.
446 @param opts: the command line options selected by the user
448 @param args: should be an empty list
450 @return: the desired exit code
453 if not (not opts.lvm_storage or opts.vg_name or
454 opts.enabled_hypervisors or opts.hvparams or
455 opts.beparams or opts.candidate_pool_size is not None):
456 ToStderr("Please give at least one of the parameters.")
459 vg_name = opts.vg_name
460 if not opts.lvm_storage and opts.vg_name:
461 ToStdout("Options --no-lvm-storage and --vg-name conflict.")
464 hvlist = opts.enabled_hypervisors
465 if hvlist is not None:
466 hvlist = hvlist.split(",")
468 hvparams = opts.hvparams
470 # a list of (name, dict) we can pass directly to dict()
471 hvparams = dict(opts.hvparams)
473 beparams = opts.beparams
475 op = opcodes.OpSetClusterParams(vg_name=opts.vg_name,
476 enabled_hypervisors=hvlist,
479 candidate_pool_size=opts.candidate_pool_size)
484 def QueueOps(opts, args):
487 @param opts: the command line options selected by the user
489 @param args: should contain only one element, the subcommand
491 @return: the desired exit code
496 if command in ("drain", "undrain"):
497 drain_flag = command == "drain"
498 client.SetQueueDrainFlag(drain_flag)
499 elif command == "info":
500 result = client.QueryConfigValues(["drain_flag"])
505 ToStdout("The drain flag is %s" % val)
508 # this is an option common to more than one command, so we declare
509 # it here and reuse it
510 node_option = make_option("-n", "--node", action="append", dest="nodes",
511 help="Node to copy to (if not given, all nodes),"
512 " can be given multiple times",
513 metavar="<node>", default=[])
516 'init': (InitCluster, ARGS_ONE,
518 make_option("-s", "--secondary-ip", dest="secondary_ip",
519 help="Specify the secondary ip for this node;"
520 " if given, the entire cluster must have secondary"
522 metavar="ADDRESS", default=None),
523 make_option("-m", "--mac-prefix", dest="mac_prefix",
524 help="Specify the mac prefix for the instance IP"
525 " addresses, in the format XX:XX:XX",
527 default="aa:00:00",),
528 make_option("-g", "--vg-name", dest="vg_name",
529 help="Specify the volume group name "
530 " (cluster-wide) for disk allocation [xenvg]",
533 make_option("-b", "--bridge", dest="def_bridge",
534 help="Specify the default bridge name (cluster-wide)"
535 " to connect the instances to [%s]" %
536 constants.DEFAULT_BRIDGE,
538 default=constants.DEFAULT_BRIDGE,),
539 make_option("--master-netdev", dest="master_netdev",
540 help="Specify the node interface (cluster-wide)"
541 " on which the master IP address will be added "
542 " [%s]" % constants.DEFAULT_BRIDGE,
544 default=constants.DEFAULT_BRIDGE,),
545 make_option("--file-storage-dir", dest="file_storage_dir",
546 help="Specify the default directory (cluster-wide)"
547 " for storing the file-based disks [%s]" %
548 constants.DEFAULT_FILE_STORAGE_DIR,
550 default=constants.DEFAULT_FILE_STORAGE_DIR,),
551 make_option("--no-lvm-storage", dest="lvm_storage",
552 help="No support for lvm based instances"
554 action="store_false", default=True,),
555 make_option("--enabled-hypervisors", dest="enabled_hypervisors",
556 help="Comma-separated list of hypervisors",
557 type="string", default=None),
558 make_option("-t", "--default-hypervisor",
559 dest="default_hypervisor",
560 help="Default hypervisor to use for instance creation",
561 choices=list(constants.HYPER_TYPES),
562 default=constants.DEFAULT_ENABLED_HYPERVISOR),
563 ikv_option("-H", "--hypervisor-parameters", dest="hvparams",
564 help="Hypervisor and hypervisor options, in the"
566 " hypervisor:option=value,option=value,...",
570 keyval_option("-B", "--backend-parameters", dest="beparams",
571 type="keyval", default={},
572 help="Backend parameters"),
574 "[opts...] <cluster_name>",
575 "Initialises a new cluster configuration"),
576 'destroy': (DestroyCluster, ARGS_NONE,
578 make_option("--yes-do-it", dest="yes_do_it",
579 help="Destroy cluster",
580 action="store_true"),
582 "", "Destroy cluster"),
583 'rename': (RenameCluster, ARGS_ONE, [DEBUG_OPT, FORCE_OPT],
585 "Renames the cluster"),
586 'verify': (VerifyCluster, ARGS_NONE, [DEBUG_OPT,
587 make_option("--no-nplus1-mem", dest="skip_nplusone_mem",
588 help="Skip N+1 memory redundancy tests",
592 "", "Does a check on the cluster configuration"),
593 'verify-disks': (VerifyDisks, ARGS_NONE, [DEBUG_OPT],
594 "", "Does a check on the cluster disk status"),
595 'masterfailover': (MasterFailover, ARGS_NONE, [DEBUG_OPT],
596 "", "Makes the current node the master"),
597 'version': (ShowClusterVersion, ARGS_NONE, [DEBUG_OPT],
598 "", "Shows the cluster version"),
599 'getmaster': (ShowClusterMaster, ARGS_NONE, [DEBUG_OPT],
600 "", "Shows the cluster master"),
601 'copyfile': (ClusterCopyFile, ARGS_ONE, [DEBUG_OPT, node_option],
602 "[-n node...] <filename>",
603 "Copies a file to all (or only some) nodes"),
604 'command': (RunClusterCommand, ARGS_ATLEAST(1), [DEBUG_OPT, node_option],
605 "[-n node...] <command>",
606 "Runs a command on all (or only some) nodes"),
607 'info': (ShowClusterConfig, ARGS_NONE, [DEBUG_OPT],
608 "", "Show cluster configuration"),
609 'list-tags': (ListTags, ARGS_NONE,
610 [DEBUG_OPT], "", "List the tags of the cluster"),
611 'add-tags': (AddTags, ARGS_ANY, [DEBUG_OPT, TAG_SRC_OPT],
612 "tag...", "Add tags to the cluster"),
613 'remove-tags': (RemoveTags, ARGS_ANY, [DEBUG_OPT, TAG_SRC_OPT],
614 "tag...", "Remove tags from the cluster"),
615 'search-tags': (SearchTags, ARGS_ONE,
616 [DEBUG_OPT], "", "Searches the tags on all objects on"
617 " the cluster for a given pattern (regex)"),
618 'queue': (QueueOps, ARGS_ONE, [DEBUG_OPT],
619 "drain|undrain|info", "Change queue properties"),
620 'modify': (SetClusterParams, ARGS_NONE,
622 make_option("-g", "--vg-name", dest="vg_name",
623 help="Specify the volume group name "
624 " (cluster-wide) for disk allocation "
625 "and enable lvm based storage",
627 make_option("--no-lvm-storage", dest="lvm_storage",
628 help="Disable support for lvm based instances"
630 action="store_false", default=True,),
631 make_option("--enabled-hypervisors", dest="enabled_hypervisors",
632 help="Comma-separated list of hypervisors",
633 type="string", default=None),
634 ikv_option("-H", "--hypervisor-parameters", dest="hvparams",
635 help="Hypervisor and hypervisor options, in the"
637 " hypervisor:option=value,option=value,...",
641 keyval_option("-B", "--backend-parameters", dest="beparams",
642 type="keyval", default={},
643 help="Backend parameters"),
644 make_option("-C", "--candidate-pool-size", default=None,
645 help="Set the candidate pool size",
646 dest="candidate_pool_size", type="int"),
649 "Alters the parameters of the cluster"),
652 if __name__ == '__main__':
653 sys.exit(GenericMain(commands, override={"tag_type": constants.TAG_CLUSTER}))