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)
31 from cStringIO import StringIO
33 from ganeti.cli import *
34 from ganeti import opcodes
35 from ganeti import constants
36 from ganeti import utils
37 from ganeti import errors
40 _SHUTDOWN_CLUSTER = "cluster"
41 _SHUTDOWN_NODES_BOTH = "nodes"
42 _SHUTDOWN_NODES_PRI = "nodes-pri"
43 _SHUTDOWN_NODES_SEC = "nodes-sec"
44 _SHUTDOWN_INSTANCES = "instances"
49 #: default list of options for L{ListInstances}
51 "name", "hypervisor", "os", "pnode", "status", "oper_ram",
55 def _ExpandMultiNames(mode, names, client=None):
56 """Expand the given names using the passed mode.
58 For _SHUTDOWN_CLUSTER, all instances will be returned. For
59 _SHUTDOWN_NODES_PRI/SEC, all instances having those nodes as
60 primary/secondary will be returned. For _SHUTDOWN_NODES_BOTH, all
61 instances having those nodes as either primary or secondary will be
62 returned. For _SHUTDOWN_INSTANCES, the given instances will be
65 @param mode: one of L{_SHUTDOWN_CLUSTER}, L{_SHUTDOWN_NODES_BOTH},
66 L{_SHUTDOWN_NODES_PRI}, L{_SHUTDOWN_NODES_SEC} or
67 L{_SHUTDOWN_INSTANCES}
68 @param names: a list of names; for cluster, it must be empty,
69 and for node and instance it must be a list of valid item
70 names (short names are valid as usual, e.g. node1 instead of
73 @return: the list of names after the expansion
74 @raise errors.ProgrammerError: for unknown selection type
75 @raise errors.OpPrereqError: for invalid input parameters
80 if mode == _SHUTDOWN_CLUSTER:
82 raise errors.OpPrereqError("Cluster filter mode takes no arguments",
84 idata = client.QueryInstances([], ["name"], False)
85 inames = [row[0] for row in idata]
87 elif mode in (_SHUTDOWN_NODES_BOTH,
91 raise errors.OpPrereqError("No node names passed", errors.ECODE_INVAL)
92 ndata = client.QueryNodes(names, ["name", "pinst_list", "sinst_list"],
94 ipri = [row[1] for row in ndata]
95 pri_names = list(itertools.chain(*ipri))
96 isec = [row[2] for row in ndata]
97 sec_names = list(itertools.chain(*isec))
98 if mode == _SHUTDOWN_NODES_BOTH:
99 inames = pri_names + sec_names
100 elif mode == _SHUTDOWN_NODES_PRI:
102 elif mode == _SHUTDOWN_NODES_SEC:
105 raise errors.ProgrammerError("Unhandled shutdown type")
107 elif mode == _SHUTDOWN_INSTANCES:
109 raise errors.OpPrereqError("No instance names passed",
111 idata = client.QueryInstances(names, ["name"], False)
112 inames = [row[0] for row in idata]
115 raise errors.OpPrereqError("Unknown mode '%s'" % mode, errors.ECODE_INVAL)
120 def _ConfirmOperation(inames, text, extra=""):
121 """Ask the user to confirm an operation on a list of instances.
123 This function is used to request confirmation for doing an operation
124 on a given list of instances.
127 @param inames: the list of names that we display when
128 we ask for confirmation
130 @param text: the operation that the user should confirm
131 (e.g. I{shutdown} or I{startup})
133 @return: True or False depending on user's confirmation.
137 msg = ("The %s will operate on %d instances.\n%s"
138 "Do you want to continue?" % (text, count, extra))
139 affected = ("\nAffected instances:\n" +
140 "\n".join([" %s" % name for name in inames]))
142 choices = [('y', True, 'Yes, execute the %s' % text),
143 ('n', False, 'No, abort the %s' % text)]
146 choices.insert(1, ('v', 'v', 'View the list of affected instances'))
151 choice = AskUser(ask, choices)
154 choice = AskUser(msg + affected, choices)
158 def _EnsureInstancesExist(client, names):
159 """Check for and ensure the given instance names exist.
161 This function will raise an OpPrereqError in case they don't
162 exist. Otherwise it will exit cleanly.
164 @type client: L{ganeti.luxi.Client}
165 @param client: the client to use for the query
167 @param names: the list of instance names to query
168 @raise errors.OpPrereqError: in case any instance is missing
171 # TODO: change LUQueryInstances to that it actually returns None
172 # instead of raising an exception, or devise a better mechanism
173 result = client.QueryInstances(names, ["name"], False)
174 for orig_name, row in zip(names, result):
176 raise errors.OpPrereqError("Instance '%s' does not exist" % orig_name,
180 def GenericManyOps(operation, fn):
181 """Generic multi-instance operations.
183 The will return a wrapper that processes the options and arguments
184 given, and uses the passed function to build the opcode needed for
185 the specific operation. Thus all the generic loop/confirmation code
186 is abstracted into this function.
189 def realfn(opts, args):
190 if opts.multi_mode is None:
191 opts.multi_mode = _SHUTDOWN_INSTANCES
193 inames = _ExpandMultiNames(opts.multi_mode, args, client=cl)
195 raise errors.OpPrereqError("Selection filter does not match"
196 " any instances", errors.ECODE_INVAL)
197 multi_on = opts.multi_mode != _SHUTDOWN_INSTANCES or len(inames) > 1
198 if not (opts.force_multi or not multi_on
199 or _ConfirmOperation(inames, operation)):
201 jex = JobExecutor(verbose=multi_on, cl=cl)
204 jex.QueueJob(name, op)
205 jex.WaitOrShow(not opts.submit_only)
210 def ListInstances(opts, args):
211 """List instances and their properties.
213 @param opts: the command line options selected by the user
215 @param args: should be an empty list
217 @return: the desired exit code
220 if opts.output is None:
221 selected_fields = _LIST_DEF_FIELDS
222 elif opts.output.startswith("+"):
223 selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",")
225 selected_fields = opts.output.split(",")
227 output = GetClient().QueryInstances(args, selected_fields, opts.do_locking)
229 if not opts.no_headers:
231 "name": "Instance", "os": "OS", "pnode": "Primary_node",
232 "snodes": "Secondary_Nodes", "admin_state": "Autostart",
233 "oper_state": "Running",
234 "oper_ram": "Memory", "disk_template": "Disk_template",
235 "ip": "IP_address", "mac": "MAC_address",
236 "nic_mode": "NIC_Mode", "nic_link": "NIC_Link",
238 "sda_size": "Disk/0", "sdb_size": "Disk/1",
239 "disk_usage": "DiskUsage",
240 "status": "Status", "tags": "Tags",
241 "network_port": "Network_port",
242 "hv/kernel_path": "Kernel_path",
243 "hv/initrd_path": "Initrd_path",
244 "hv/boot_order": "Boot_order",
247 "hv/cdrom_image_path": "CDROM_image_path",
248 "hv/nic_type": "NIC_type",
249 "hv/disk_type": "Disk_type",
250 "hv/vnc_bind_address": "VNC_bind_address",
251 "serial_no": "SerialNo", "hypervisor": "Hypervisor",
252 "hvparams": "Hypervisor_parameters",
253 "be/memory": "Configured_memory",
256 "be/auto_balance": "Auto_balance",
257 "disk.count": "Disks", "disk.sizes": "Disk_sizes",
258 "nic.count": "NICs", "nic.ips": "NIC_IPs",
259 "nic.modes": "NIC_modes", "nic.links": "NIC_links",
260 "nic.bridges": "NIC_bridges", "nic.macs": "NIC_MACs",
261 "ctime": "CTime", "mtime": "MTime", "uuid": "UUID",
266 unitfields = ["be/memory", "oper_ram", "sd(a|b)_size", "disk\.size/.*"]
267 numfields = ["be/memory", "oper_ram", "sd(a|b)_size", "be/vcpus",
268 "serial_no", "(disk|nic)\.count", "disk\.size/.*"]
270 list_type_fields = ("tags", "disk.sizes", "nic.macs", "nic.ips",
271 "nic.modes", "nic.links", "nic.bridges")
272 # change raw values to nicer strings
274 for idx, field in enumerate(selected_fields):
276 if field == "snodes":
277 val = ",".join(val) or "-"
278 elif field == "admin_state":
283 elif field == "oper_state":
290 elif field == "oper_ram":
293 elif field == "sda_size" or field == "sdb_size":
296 elif field == "ctime" or field == "mtime":
297 val = utils.FormatTime(val)
298 elif field in list_type_fields:
299 val = ",".join(str(item) for item in val)
304 data = GenerateTable(separator=opts.separator, headers=headers,
305 fields=selected_fields, unitfields=unitfields,
306 numfields=numfields, data=output, units=opts.units)
314 def AddInstance(opts, args):
315 """Add an instance to the cluster.
317 This is just a wrapper over GenericInstanceCreate.
320 return GenericInstanceCreate(constants.INSTANCE_CREATE, opts, args)
324 def BatchCreate(opts, args):
325 """Create instances using a definition file.
327 This function reads a json file with instances defined
331 "disk_size": [20480],
337 "primary_node": "firstnode",
338 "secondary_node": "secondnode",
339 "iallocator": "dumb"}
342 Note that I{primary_node} and I{secondary_node} have precedence over
345 @param opts: the command line options selected by the user
347 @param args: should contain one element, the json filename
349 @return: the desired exit code
352 _DEFAULT_SPECS = {"disk_size": [20 * 1024],
355 "primary_node": None,
356 "secondary_node": None,
362 "file_storage_dir": None,
363 "file_driver": 'loop'}
365 def _PopulateWithDefaults(spec):
366 """Returns a new hash combined with default values."""
367 mydict = _DEFAULT_SPECS.copy()
372 """Validate the instance specs."""
373 # Validate fields required under any circumstances
374 for required_field in ('os', 'template'):
375 if required_field not in spec:
376 raise errors.OpPrereqError('Required field "%s" is missing.' %
377 required_field, errors.ECODE_INVAL)
378 # Validate special fields
379 if spec['primary_node'] is not None:
380 if (spec['template'] in constants.DTS_NET_MIRROR and
381 spec['secondary_node'] is None):
382 raise errors.OpPrereqError('Template requires secondary node, but'
383 ' there was no secondary provided.',
385 elif spec['iallocator'] is None:
386 raise errors.OpPrereqError('You have to provide at least a primary_node'
387 ' or an iallocator.',
390 if (spec['hvparams'] and
391 not isinstance(spec['hvparams'], dict)):
392 raise errors.OpPrereqError('Hypervisor parameters must be a dict.',
395 json_filename = args[0]
397 instance_data = simplejson.loads(utils.ReadFile(json_filename))
398 except Exception, err:
399 ToStderr("Can't parse the instance definition file: %s" % str(err))
404 # Iterate over the instances and do:
405 # * Populate the specs with default value
406 # * Validate the instance specs
407 i_names = utils.NiceSort(instance_data.keys())
409 specs = instance_data[name]
410 specs = _PopulateWithDefaults(specs)
413 hypervisor = specs['hypervisor']
414 hvparams = specs['hvparams']
417 for elem in specs['disk_size']:
419 size = utils.ParseUnit(elem)
420 except ValueError, err:
421 raise errors.OpPrereqError("Invalid disk size '%s' for"
423 (elem, name, err), errors.ECODE_INVAL)
424 disks.append({"size": size})
426 utils.ForceDictType(specs['backend'], constants.BES_PARAMETER_TYPES)
427 utils.ForceDictType(hvparams, constants.HVS_PARAMETER_TYPES)
430 for field in ('ip', 'mac', 'mode', 'link', 'bridge'):
434 tmp_nics[0][field] = specs[field]
436 if specs['nics'] is not None and tmp_nics:
437 raise errors.OpPrereqError("'nics' list incompatible with using"
438 " individual nic fields as well",
440 elif specs['nics'] is not None:
441 tmp_nics = specs['nics']
445 op = opcodes.OpCreateInstance(instance_name=name,
447 disk_template=specs['template'],
448 mode=constants.INSTANCE_CREATE,
450 force_variant=opts.force_variant,
451 pnode=specs['primary_node'],
452 snode=specs['secondary_node'],
454 start=specs['start'],
455 ip_check=specs['ip_check'],
457 iallocator=specs['iallocator'],
458 hypervisor=hypervisor,
460 beparams=specs['backend'],
461 file_storage_dir=specs['file_storage_dir'],
462 file_driver=specs['file_driver'])
464 jex.QueueJob(name, op)
465 # we never want to wait, just show the submitted job IDs
466 jex.WaitOrShow(False)
471 def ReinstallInstance(opts, args):
472 """Reinstall an instance.
474 @param opts: the command line options selected by the user
476 @param args: should contain only one element, the name of the
477 instance to be reinstalled
479 @return: the desired exit code
482 # first, compute the desired name list
483 if opts.multi_mode is None:
484 opts.multi_mode = _SHUTDOWN_INSTANCES
486 inames = _ExpandMultiNames(opts.multi_mode, args)
488 raise errors.OpPrereqError("Selection filter does not match any instances",
491 # second, if requested, ask for an OS
492 if opts.select_os is True:
493 op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "variants"],
495 result = SubmitOpCode(op)
498 ToStdout("Can't get the OS list")
501 ToStdout("Available OS templates:")
504 for (name, valid, variants) in result:
506 for entry in CalculateOSNames(name, variants):
507 ToStdout("%3s: %s", number, entry)
508 choices.append(("%s" % number, entry, entry))
511 choices.append(('x', 'exit', 'Exit gnt-instance reinstall'))
512 selected = AskUser("Enter OS template number (or x to abort):",
515 if selected == 'exit':
516 ToStderr("User aborted reinstall, exiting")
523 # third, get confirmation: multi-reinstall requires --force-multi
524 # *and* --force, single-reinstall just --force
525 multi_on = opts.multi_mode != _SHUTDOWN_INSTANCES or len(inames) > 1
527 warn_msg = "Note: this will remove *all* data for the below instances!\n"
528 if not ((opts.force_multi and opts.force) or
529 _ConfirmOperation(inames, "reinstall", extra=warn_msg)):
533 usertext = ("This will reinstall the instance %s and remove"
534 " all data. Continue?") % inames[0]
535 if not AskUser(usertext):
538 jex = JobExecutor(verbose=multi_on)
539 for instance_name in inames:
540 op = opcodes.OpReinstallInstance(instance_name=instance_name,
542 force_variant=opts.force_variant)
543 jex.QueueJob(instance_name, op)
545 jex.WaitOrShow(not opts.submit_only)
549 def RemoveInstance(opts, args):
550 """Remove an instance.
552 @param opts: the command line options selected by the user
554 @param args: should contain only one element, the name of
555 the instance to be removed
557 @return: the desired exit code
560 instance_name = args[0]
565 _EnsureInstancesExist(cl, [instance_name])
567 usertext = ("This will remove the volumes of the instance %s"
568 " (including mirrors), thus removing all the data"
569 " of the instance. Continue?") % instance_name
570 if not AskUser(usertext):
573 op = opcodes.OpRemoveInstance(instance_name=instance_name,
574 ignore_failures=opts.ignore_failures,
575 shutdown_timeout=opts.shutdown_timeout)
576 SubmitOrSend(op, opts, cl=cl)
580 def RenameInstance(opts, args):
581 """Rename an instance.
583 @param opts: the command line options selected by the user
585 @param args: should contain two elements, the old and the
588 @return: the desired exit code
591 op = opcodes.OpRenameInstance(instance_name=args[0],
593 ignore_ip=opts.ignore_ip)
594 SubmitOrSend(op, opts)
598 def ActivateDisks(opts, args):
599 """Activate an instance's disks.
601 This serves two purposes:
602 - it allows (as long as the instance is not running)
603 mounting the disks and modifying them from the node
604 - it repairs inactive secondary drbds
606 @param opts: the command line options selected by the user
608 @param args: should contain only one element, the instance name
610 @return: the desired exit code
613 instance_name = args[0]
614 op = opcodes.OpActivateInstanceDisks(instance_name=instance_name,
615 ignore_size=opts.ignore_size)
616 disks_info = SubmitOrSend(op, opts)
617 for host, iname, nname in disks_info:
618 ToStdout("%s:%s:%s", host, iname, nname)
622 def DeactivateDisks(opts, args):
623 """Deactivate an instance's disks.
625 This function takes the instance name, looks for its primary node
626 and the tries to shutdown its block devices on that node.
628 @param opts: the command line options selected by the user
630 @param args: should contain only one element, the instance name
632 @return: the desired exit code
635 instance_name = args[0]
636 op = opcodes.OpDeactivateInstanceDisks(instance_name=instance_name)
637 SubmitOrSend(op, opts)
641 def RecreateDisks(opts, args):
642 """Recreate an instance's disks.
644 @param opts: the command line options selected by the user
646 @param args: should contain only one element, the instance name
648 @return: the desired exit code
651 instance_name = args[0]
654 opts.disks = [int(v) for v in opts.disks.split(",")]
655 except (ValueError, TypeError), err:
656 ToStderr("Invalid disks value: %s" % str(err))
661 op = opcodes.OpRecreateInstanceDisks(instance_name=instance_name,
663 SubmitOrSend(op, opts)
667 def GrowDisk(opts, args):
668 """Grow an instance's disks.
670 @param opts: the command line options selected by the user
672 @param args: should contain two elements, the instance name
673 whose disks we grow and the disk name, e.g. I{sda}
675 @return: the desired exit code
682 except ValueError, err:
683 raise errors.OpPrereqError("Invalid disk index: %s" % str(err),
685 amount = utils.ParseUnit(args[2])
686 op = opcodes.OpGrowDisk(instance_name=instance, disk=disk, amount=amount,
687 wait_for_sync=opts.wait_for_sync)
688 SubmitOrSend(op, opts)
692 def _StartupInstance(name, opts):
693 """Startup instances.
695 This returns the opcode to start an instance, and its decorator will
696 wrap this into a loop starting all desired instances.
698 @param name: the name of the instance to act on
699 @param opts: the command line options selected by the user
700 @return: the opcode needed for the operation
703 op = opcodes.OpStartupInstance(instance_name=name,
705 # do not add these parameters to the opcode unless they're defined
707 op.hvparams = opts.hvparams
709 op.beparams = opts.beparams
713 def _RebootInstance(name, opts):
714 """Reboot instance(s).
716 This returns the opcode to reboot an instance, and its decorator
717 will wrap this into a loop rebooting all desired instances.
719 @param name: the name of the instance to act on
720 @param opts: the command line options selected by the user
721 @return: the opcode needed for the operation
724 return opcodes.OpRebootInstance(instance_name=name,
725 reboot_type=opts.reboot_type,
726 ignore_secondaries=opts.ignore_secondaries,
727 shutdown_timeout=opts.shutdown_timeout)
730 def _ShutdownInstance(name, opts):
731 """Shutdown an instance.
733 This returns the opcode to shutdown an instance, and its decorator
734 will wrap this into a loop shutting down all desired instances.
736 @param name: the name of the instance to act on
737 @param opts: the command line options selected by the user
738 @return: the opcode needed for the operation
741 return opcodes.OpShutdownInstance(instance_name=name,
742 timeout=opts.timeout)
745 def ReplaceDisks(opts, args):
746 """Replace the disks of an instance
748 @param opts: the command line options selected by the user
750 @param args: should contain only one element, the instance name
752 @return: the desired exit code
755 instance_name = args[0]
756 new_2ndary = opts.dst_node
757 iallocator = opts.iallocator
758 if opts.disks is None:
762 disks = [int(i) for i in opts.disks.split(",")]
763 except ValueError, err:
764 raise errors.OpPrereqError("Invalid disk index passed: %s" % str(err),
766 cnt = [opts.on_primary, opts.on_secondary, opts.auto,
767 new_2ndary is not None, iallocator is not None].count(True)
769 raise errors.OpPrereqError("One and only one of the -p, -s, -a, -n and -i"
770 " options must be passed", errors.ECODE_INVAL)
771 elif opts.on_primary:
772 mode = constants.REPLACE_DISK_PRI
773 elif opts.on_secondary:
774 mode = constants.REPLACE_DISK_SEC
776 mode = constants.REPLACE_DISK_AUTO
778 raise errors.OpPrereqError("Cannot specify disks when using automatic"
779 " mode", errors.ECODE_INVAL)
780 elif new_2ndary is not None or iallocator is not None:
782 mode = constants.REPLACE_DISK_CHG
784 op = opcodes.OpReplaceDisks(instance_name=args[0], disks=disks,
785 remote_node=new_2ndary, mode=mode,
786 iallocator=iallocator)
787 SubmitOrSend(op, opts)
791 def FailoverInstance(opts, args):
792 """Failover an instance.
794 The failover is done by shutting it down on its present node and
795 starting it on the secondary.
797 @param opts: the command line options selected by the user
799 @param args: should contain only one element, the instance name
801 @return: the desired exit code
805 instance_name = args[0]
809 _EnsureInstancesExist(cl, [instance_name])
811 usertext = ("Failover will happen to image %s."
812 " This requires a shutdown of the instance. Continue?" %
814 if not AskUser(usertext):
817 op = opcodes.OpFailoverInstance(instance_name=instance_name,
818 ignore_consistency=opts.ignore_consistency,
819 shutdown_timeout=opts.shutdown_timeout)
820 SubmitOrSend(op, opts, cl=cl)
824 def MigrateInstance(opts, args):
825 """Migrate an instance.
827 The migrate is done without shutdown.
829 @param opts: the command line options selected by the user
831 @param args: should contain only one element, the instance name
833 @return: the desired exit code
837 instance_name = args[0]
841 _EnsureInstancesExist(cl, [instance_name])
844 usertext = ("Instance %s will be recovered from a failed migration."
845 " Note that the migration procedure (including cleanup)" %
848 usertext = ("Instance %s will be migrated. Note that migration" %
850 usertext += (" is **experimental** in this version."
851 " This might impact the instance if anything goes wrong."
853 if not AskUser(usertext):
856 op = opcodes.OpMigrateInstance(instance_name=instance_name, live=opts.live,
857 cleanup=opts.cleanup)
858 SubmitOpCode(op, cl=cl)
862 def MoveInstance(opts, args):
865 @param opts: the command line options selected by the user
867 @param args: should contain only one element, the instance name
869 @return: the desired exit code
873 instance_name = args[0]
877 usertext = ("Instance %s will be moved."
878 " This requires a shutdown of the instance. Continue?" %
880 if not AskUser(usertext):
883 op = opcodes.OpMoveInstance(instance_name=instance_name,
884 target_node=opts.node,
885 shutdown_timeout=opts.shutdown_timeout)
886 SubmitOrSend(op, opts, cl=cl)
890 def ConnectToInstanceConsole(opts, args):
891 """Connect to the console of an instance.
893 @param opts: the command line options selected by the user
895 @param args: should contain only one element, the instance name
897 @return: the desired exit code
900 instance_name = args[0]
902 op = opcodes.OpConnectConsole(instance_name=instance_name)
903 cmd = SubmitOpCode(op)
905 if opts.show_command:
906 ToStdout("%s", utils.ShellQuoteArgs(cmd))
909 os.execvp(cmd[0], cmd)
911 ToStderr("Can't run console command %s with arguments:\n'%s'",
912 cmd[0], " ".join(cmd))
916 def _FormatLogicalID(dev_type, logical_id):
917 """Formats the logical_id of a disk.
920 if dev_type == constants.LD_DRBD8:
921 node_a, node_b, port, minor_a, minor_b, key = logical_id
923 ("nodeA", "%s, minor=%s" % (node_a, minor_a)),
924 ("nodeB", "%s, minor=%s" % (node_b, minor_b)),
928 elif dev_type == constants.LD_LV:
929 vg_name, lv_name = logical_id
930 data = ["%s/%s" % (vg_name, lv_name)]
932 data = [str(logical_id)]
937 def _FormatBlockDevInfo(idx, top_level, dev, static):
938 """Show block device information.
940 This is only used by L{ShowInstanceConfig}, but it's too big to be
941 left for an inline definition.
944 @param idx: the index of the current disk
945 @type top_level: boolean
946 @param top_level: if this a top-level disk?
948 @param dev: dictionary with disk information
949 @type static: boolean
950 @param static: wheter the device information doesn't contain
951 runtime information but only static data
952 @return: a list of either strings, tuples or lists
953 (which should be formatted at a higher indent level)
956 def helper(dtype, status):
957 """Format one line for physical device status.
960 @param dtype: a constant from the L{constants.LDS_BLOCK} set
962 @param status: a tuple as returned from L{backend.FindBlockDevice}
963 @return: the string representing the status
969 (path, major, minor, syncp, estt, degr, ldisk_status) = status
973 major_string = str(major)
978 minor_string = str(minor)
980 txt += ("%s (%s:%s)" % (path, major_string, minor_string))
981 if dtype in (constants.LD_DRBD8, ):
982 if syncp is not None:
983 sync_text = "*RECOVERING* %5.2f%%," % syncp
985 sync_text += " ETA %ds" % estt
987 sync_text += " ETA unknown"
989 sync_text = "in sync"
991 degr_text = "*DEGRADED*"
994 if ldisk_status == constants.LDS_FAULTY:
995 ldisk_text = " *MISSING DISK*"
996 elif ldisk_status == constants.LDS_UNKNOWN:
997 ldisk_text = " *UNCERTAIN STATE*"
1000 txt += (" %s, status %s%s" % (sync_text, degr_text, ldisk_text))
1001 elif dtype == constants.LD_LV:
1002 if ldisk_status == constants.LDS_FAULTY:
1003 ldisk_text = " *FAILED* (failed drive?)"
1011 if dev["iv_name"] is not None:
1012 txt = dev["iv_name"]
1014 txt = "disk %d" % idx
1016 txt = "child %d" % idx
1017 if isinstance(dev["size"], int):
1018 nice_size = utils.FormatUnit(dev["size"], "h")
1020 nice_size = dev["size"]
1021 d1 = ["- %s: %s, size %s" % (txt, dev["dev_type"], nice_size)]
1024 data.append(("access mode", dev["mode"]))
1025 if dev["logical_id"] is not None:
1027 l_id = _FormatLogicalID(dev["dev_type"], dev["logical_id"])
1029 l_id = [str(dev["logical_id"])]
1031 data.append(("logical_id", l_id[0]))
1034 elif dev["physical_id"] is not None:
1035 data.append("physical_id:")
1036 data.append([dev["physical_id"]])
1038 data.append(("on primary", helper(dev["dev_type"], dev["pstatus"])))
1039 if dev["sstatus"] and not static:
1040 data.append(("on secondary", helper(dev["dev_type"], dev["sstatus"])))
1043 data.append("child devices:")
1044 for c_idx, child in enumerate(dev["children"]):
1045 data.append(_FormatBlockDevInfo(c_idx, False, child, static))
1050 def _FormatList(buf, data, indent_level):
1051 """Formats a list of data at a given indent level.
1053 If the element of the list is:
1054 - a string, it is simply formatted as is
1055 - a tuple, it will be split into key, value and the all the
1056 values in a list will be aligned all at the same start column
1057 - a list, will be recursively formatted
1060 @param buf: the buffer into which we write the output
1061 @param data: the list to format
1062 @type indent_level: int
1063 @param indent_level: the indent level to format at
1066 max_tlen = max([len(elem[0]) for elem in data
1067 if isinstance(elem, tuple)] or [0])
1069 if isinstance(elem, basestring):
1070 buf.write("%*s%s\n" % (2*indent_level, "", elem))
1071 elif isinstance(elem, tuple):
1073 spacer = "%*s" % (max_tlen - len(key), "")
1074 buf.write("%*s%s:%s %s\n" % (2*indent_level, "", key, spacer, value))
1075 elif isinstance(elem, list):
1076 _FormatList(buf, elem, indent_level+1)
1079 def ShowInstanceConfig(opts, args):
1080 """Compute instance run-time status.
1082 @param opts: the command line options selected by the user
1084 @param args: either an empty list, and then we query all
1085 instances, or should contain a list of instance names
1087 @return: the desired exit code
1090 if not args and not opts.show_all:
1091 ToStderr("No instance selected."
1092 " Please pass in --all if you want to query all instances.\n"
1093 "Note that this can take a long time on a big cluster.")
1095 elif args and opts.show_all:
1096 ToStderr("Cannot use --all if you specify instance names.")
1100 op = opcodes.OpQueryInstanceData(instances=args, static=opts.static)
1101 result = SubmitOpCode(op)
1103 ToStdout("No instances.")
1108 for instance_name in result:
1109 instance = result[instance_name]
1110 buf.write("Instance name: %s\n" % instance["name"])
1111 buf.write("UUID: %s\n" % instance["uuid"])
1112 buf.write("Serial number: %s\n" % instance["serial_no"])
1113 buf.write("Creation time: %s\n" % utils.FormatTime(instance["ctime"]))
1114 buf.write("Modification time: %s\n" % utils.FormatTime(instance["mtime"]))
1115 buf.write("State: configured to be %s" % instance["config_state"])
1117 buf.write(", actual state is %s" % instance["run_state"])
1119 ##buf.write("Considered for memory checks in cluster verify: %s\n" %
1120 ## instance["auto_balance"])
1121 buf.write(" Nodes:\n")
1122 buf.write(" - primary: %s\n" % instance["pnode"])
1123 buf.write(" - secondaries: %s\n" % ", ".join(instance["snodes"]))
1124 buf.write(" Operating system: %s\n" % instance["os"])
1125 if instance.has_key("network_port"):
1126 buf.write(" Allocated network port: %s\n" % instance["network_port"])
1127 buf.write(" Hypervisor: %s\n" % instance["hypervisor"])
1129 # custom VNC console information
1130 vnc_bind_address = instance["hv_actual"].get(constants.HV_VNC_BIND_ADDRESS,
1132 if vnc_bind_address:
1133 port = instance["network_port"]
1134 display = int(port) - constants.VNC_BASE_PORT
1135 if display > 0 and vnc_bind_address == constants.BIND_ADDRESS_GLOBAL:
1136 vnc_console_port = "%s:%s (display %s)" % (instance["pnode"],
1139 elif display > 0 and utils.IsValidIP(vnc_bind_address):
1140 vnc_console_port = ("%s:%s (node %s) (display %s)" %
1141 (vnc_bind_address, port,
1142 instance["pnode"], display))
1144 # vnc bind address is a file
1145 vnc_console_port = "%s:%s" % (instance["pnode"],
1147 buf.write(" - console connection: vnc to %s\n" % vnc_console_port)
1149 for key in instance["hv_actual"]:
1150 if key in instance["hv_instance"]:
1151 val = instance["hv_instance"][key]
1153 val = "default (%s)" % instance["hv_actual"][key]
1154 buf.write(" - %s: %s\n" % (key, val))
1155 buf.write(" Hardware:\n")
1156 buf.write(" - VCPUs: %d\n" %
1157 instance["be_actual"][constants.BE_VCPUS])
1158 buf.write(" - memory: %dMiB\n" %
1159 instance["be_actual"][constants.BE_MEMORY])
1160 buf.write(" - NICs:\n")
1161 for idx, (ip, mac, mode, link) in enumerate(instance["nics"]):
1162 buf.write(" - nic/%d: MAC: %s, IP: %s, mode: %s, link: %s\n" %
1163 (idx, mac, ip, mode, link))
1164 buf.write(" Disks:\n")
1166 for idx, device in enumerate(instance["disks"]):
1167 _FormatList(buf, _FormatBlockDevInfo(idx, True, device, opts.static), 2)
1169 ToStdout(buf.getvalue().rstrip('\n'))
1173 def SetInstanceParams(opts, args):
1174 """Modifies an instance.
1176 All parameters take effect only at the next restart of the instance.
1178 @param opts: the command line options selected by the user
1180 @param args: should contain only one element, the instance name
1182 @return: the desired exit code
1185 if not (opts.nics or opts.disks or
1186 opts.hvparams or opts.beparams):
1187 ToStderr("Please give at least one of the parameters.")
1190 for param in opts.beparams:
1191 if isinstance(opts.beparams[param], basestring):
1192 if opts.beparams[param].lower() == "default":
1193 opts.beparams[param] = constants.VALUE_DEFAULT
1195 utils.ForceDictType(opts.beparams, constants.BES_PARAMETER_TYPES,
1196 allowed_values=[constants.VALUE_DEFAULT])
1198 for param in opts.hvparams:
1199 if isinstance(opts.hvparams[param], basestring):
1200 if opts.hvparams[param].lower() == "default":
1201 opts.hvparams[param] = constants.VALUE_DEFAULT
1203 utils.ForceDictType(opts.hvparams, constants.HVS_PARAMETER_TYPES,
1204 allowed_values=[constants.VALUE_DEFAULT])
1206 for idx, (nic_op, nic_dict) in enumerate(opts.nics):
1208 nic_op = int(nic_op)
1209 opts.nics[idx] = (nic_op, nic_dict)
1213 for idx, (disk_op, disk_dict) in enumerate(opts.disks):
1215 disk_op = int(disk_op)
1216 opts.disks[idx] = (disk_op, disk_dict)
1219 if disk_op == constants.DDM_ADD:
1220 if 'size' not in disk_dict:
1221 raise errors.OpPrereqError("Missing required parameter 'size'",
1223 disk_dict['size'] = utils.ParseUnit(disk_dict['size'])
1225 op = opcodes.OpSetInstanceParams(instance_name=args[0],
1228 hvparams=opts.hvparams,
1229 beparams=opts.beparams,
1232 # even if here we process the result, we allow submit only
1233 result = SubmitOrSend(op, opts)
1236 ToStdout("Modified instance %s", args[0])
1237 for param, data in result:
1238 ToStdout(" - %-5s -> %s", param, data)
1239 ToStdout("Please don't forget that these parameters take effect"
1240 " only at the next start of the instance.")
1244 # multi-instance selection options
1245 m_force_multi = cli_option("--force-multiple", dest="force_multi",
1246 help="Do not ask for confirmation when more than"
1247 " one instance is affected",
1248 action="store_true", default=False)
1250 m_pri_node_opt = cli_option("--primary", dest="multi_mode",
1251 help="Filter by nodes (primary only)",
1252 const=_SHUTDOWN_NODES_PRI, action="store_const")
1254 m_sec_node_opt = cli_option("--secondary", dest="multi_mode",
1255 help="Filter by nodes (secondary only)",
1256 const=_SHUTDOWN_NODES_SEC, action="store_const")
1258 m_node_opt = cli_option("--node", dest="multi_mode",
1259 help="Filter by nodes (primary and secondary)",
1260 const=_SHUTDOWN_NODES_BOTH, action="store_const")
1262 m_clust_opt = cli_option("--all", dest="multi_mode",
1263 help="Select all instances in the cluster",
1264 const=_SHUTDOWN_CLUSTER, action="store_const")
1266 m_inst_opt = cli_option("--instance", dest="multi_mode",
1267 help="Filter by instance name [default]",
1268 const=_SHUTDOWN_INSTANCES, action="store_const")
1271 # this is defined separately due to readability only
1277 FILESTORE_DRIVER_OPT,
1294 AddInstance, [ArgHost(min=1, max=1)], add_opts,
1295 "[...] -t disk-type -n node[:secondary-node] -o os-type <name>",
1296 "Creates and adds a new instance to the cluster"),
1298 BatchCreate, [ArgFile(min=1, max=1)], [],
1300 "Create a bunch of instances based on specs in the file."),
1302 ConnectToInstanceConsole, ARGS_ONE_INSTANCE,
1304 "[--show-cmd] <instance>", "Opens a console on the specified instance"),
1306 FailoverInstance, ARGS_ONE_INSTANCE,
1307 [FORCE_OPT, IGNORE_CONSIST_OPT, SUBMIT_OPT, SHUTDOWN_TIMEOUT_OPT],
1308 "[-f] <instance>", "Stops the instance and starts it on the backup node,"
1309 " using the remote mirror (only for instances of type drbd)"),
1311 MigrateInstance, ARGS_ONE_INSTANCE,
1312 [FORCE_OPT, NONLIVE_OPT, CLEANUP_OPT],
1313 "[-f] <instance>", "Migrate instance to its secondary node"
1314 " (only for instances of type drbd)"),
1316 MoveInstance, ARGS_ONE_INSTANCE,
1317 [FORCE_OPT, SUBMIT_OPT, SINGLE_NODE_OPT, SHUTDOWN_TIMEOUT_OPT],
1318 "[-f] <instance>", "Move instance to an arbitrary node"
1319 " (only for instances of type file and lv)"),
1321 ShowInstanceConfig, ARGS_MANY_INSTANCES,
1322 [STATIC_OPT, ALL_OPT],
1323 "[-s] {--all | <instance>...}",
1324 "Show information on the specified instance(s)"),
1326 ListInstances, ARGS_MANY_INSTANCES,
1327 [NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT, SYNC_OPT],
1329 "Lists the instances and their status. The available fields are"
1330 " (see the man page for details): status, oper_state, oper_ram,"
1331 " name, os, pnode, snodes, admin_state, admin_ram, disk_template,"
1332 " ip, mac, nic_mode, nic_link, sda_size, sdb_size, vcpus, serial_no,"
1333 " nic.count, nic.mac/N, nic.ip/N, nic.mode/N, nic.link/N,"
1334 " nic.macs, nic.ips, nic.modes, nic.links,"
1335 " disk.count, disk.size/N, disk.sizes,"
1336 " hv/NAME, be/memory, be/vcpus, be/auto_balance,"
1338 " The default field"
1339 " list is (in order): %s." % ", ".join(_LIST_DEF_FIELDS),
1342 ReinstallInstance, [ArgInstance()],
1343 [FORCE_OPT, OS_OPT, FORCE_VARIANT_OPT, m_force_multi, m_node_opt,
1344 m_pri_node_opt, m_sec_node_opt, m_clust_opt, m_inst_opt, SELECT_OS_OPT,
1346 "[-f] <instance>", "Reinstall a stopped instance"),
1348 RemoveInstance, ARGS_ONE_INSTANCE,
1349 [FORCE_OPT, SHUTDOWN_TIMEOUT_OPT, IGNORE_FAILURES_OPT, SUBMIT_OPT],
1350 "[-f] <instance>", "Shuts down the instance and removes it"),
1353 [ArgInstance(min=1, max=1), ArgHost(min=1, max=1)],
1354 [NOIPCHECK_OPT, SUBMIT_OPT],
1355 "<instance> <new_name>", "Rename the instance"),
1357 ReplaceDisks, ARGS_ONE_INSTANCE,
1358 [AUTO_REPLACE_OPT, DISKIDX_OPT, IALLOCATOR_OPT,
1359 NEW_SECONDARY_OPT, ON_PRIMARY_OPT, ON_SECONDARY_OPT, SUBMIT_OPT],
1360 "[-s|-p|-n NODE|-I NAME] <instance>",
1361 "Replaces all disks for the instance"),
1363 SetInstanceParams, ARGS_ONE_INSTANCE,
1364 [BACKEND_OPT, DISK_OPT, FORCE_OPT, HVOPTS_OPT, NET_OPT, SUBMIT_OPT],
1365 "<instance>", "Alters the parameters of an instance"),
1367 GenericManyOps("shutdown", _ShutdownInstance), [ArgInstance()],
1368 [m_node_opt, m_pri_node_opt, m_sec_node_opt, m_clust_opt,
1369 m_inst_opt, m_force_multi, TIMEOUT_OPT, SUBMIT_OPT],
1370 "<instance>", "Stops an instance"),
1372 GenericManyOps("startup", _StartupInstance), [ArgInstance()],
1373 [FORCE_OPT, m_force_multi, m_node_opt, m_pri_node_opt,
1374 m_sec_node_opt, m_clust_opt, m_inst_opt, SUBMIT_OPT, HVOPTS_OPT,
1376 "<instance>", "Starts an instance"),
1378 GenericManyOps("reboot", _RebootInstance), [ArgInstance()],
1379 [m_force_multi, REBOOT_TYPE_OPT, IGNORE_SECONDARIES_OPT, m_node_opt,
1380 m_pri_node_opt, m_sec_node_opt, m_clust_opt, m_inst_opt, SUBMIT_OPT,
1381 SHUTDOWN_TIMEOUT_OPT],
1382 "<instance>", "Reboots an instance"),
1384 ActivateDisks, ARGS_ONE_INSTANCE, [SUBMIT_OPT, IGNORE_SIZE_OPT],
1385 "<instance>", "Activate an instance's disks"),
1386 'deactivate-disks': (
1387 DeactivateDisks, ARGS_ONE_INSTANCE, [SUBMIT_OPT],
1388 "<instance>", "Deactivate an instance's disks"),
1390 RecreateDisks, ARGS_ONE_INSTANCE, [SUBMIT_OPT, DISKIDX_OPT],
1391 "<instance>", "Recreate an instance's disks"),
1394 [ArgInstance(min=1, max=1), ArgUnknown(min=1, max=1),
1395 ArgUnknown(min=1, max=1)],
1396 [SUBMIT_OPT, NWSYNC_OPT],
1397 "<instance> <disk> <size>", "Grow an instance's disk"),
1399 ListTags, ARGS_ONE_INSTANCE, [],
1400 "<instance_name>", "List the tags of the given instance"),
1402 AddTags, [ArgInstance(min=1, max=1), ArgUnknown()],
1404 "<instance_name> tag...", "Add tags to the given instance"),
1406 RemoveTags, [ArgInstance(min=1, max=1), ArgUnknown()],
1408 "<instance_name> tag...", "Remove tags from given instance"),
1411 #: dictionary with aliases for commands
1413 'activate_block_devs': 'activate-disks',
1414 'replace_disks': 'replace-disks',
1420 if __name__ == '__main__':
1421 sys.exit(GenericMain(commands, aliases=aliases,
1422 override={"tag_type": constants.TAG_INSTANCE}))