4 # Copyright (C) 2006, 2007, 2008, 2009, 2010 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 """Instance related commands"""
23 # pylint: disable-msg=W0401,W0614,C0103
24 # W0401: Wildcard import ganeti.cli
25 # W0614: Unused import %s from wildcard import (since we need cli)
26 # C0103: Invalid name gnt-instance
32 from cStringIO import StringIO
34 from ganeti.cli import *
35 from ganeti import opcodes
36 from ganeti import constants
37 from ganeti import compat
38 from ganeti import utils
39 from ganeti import errors
40 from ganeti import netutils
43 _SHUTDOWN_CLUSTER = "cluster"
44 _SHUTDOWN_NODES_BOTH = "nodes"
45 _SHUTDOWN_NODES_PRI = "nodes-pri"
46 _SHUTDOWN_NODES_SEC = "nodes-sec"
47 _SHUTDOWN_NODES_BOTH_BY_TAGS = "nodes-by-tags"
48 _SHUTDOWN_NODES_PRI_BY_TAGS = "nodes-pri-by-tags"
49 _SHUTDOWN_NODES_SEC_BY_TAGS = "nodes-sec-by-tags"
50 _SHUTDOWN_INSTANCES = "instances"
51 _SHUTDOWN_INSTANCES_BY_TAGS = "instances-by-tags"
53 _SHUTDOWN_NODES_TAGS_MODES = (
54 _SHUTDOWN_NODES_BOTH_BY_TAGS,
55 _SHUTDOWN_NODES_PRI_BY_TAGS,
56 _SHUTDOWN_NODES_SEC_BY_TAGS)
61 #: default list of options for L{ListInstances}
63 "name", "hypervisor", "os", "pnode", "status", "oper_ram",
67 def _ExpandMultiNames(mode, names, client=None):
68 """Expand the given names using the passed mode.
70 For _SHUTDOWN_CLUSTER, all instances will be returned. For
71 _SHUTDOWN_NODES_PRI/SEC, all instances having those nodes as
72 primary/secondary will be returned. For _SHUTDOWN_NODES_BOTH, all
73 instances having those nodes as either primary or secondary will be
74 returned. For _SHUTDOWN_INSTANCES, the given instances will be
77 @param mode: one of L{_SHUTDOWN_CLUSTER}, L{_SHUTDOWN_NODES_BOTH},
78 L{_SHUTDOWN_NODES_PRI}, L{_SHUTDOWN_NODES_SEC} or
79 L{_SHUTDOWN_INSTANCES}
80 @param names: a list of names; for cluster, it must be empty,
81 and for node and instance it must be a list of valid item
82 names (short names are valid as usual, e.g. node1 instead of
85 @return: the list of names after the expansion
86 @raise errors.ProgrammerError: for unknown selection type
87 @raise errors.OpPrereqError: for invalid input parameters
90 # pylint: disable-msg=W0142
94 if mode == _SHUTDOWN_CLUSTER:
96 raise errors.OpPrereqError("Cluster filter mode takes no arguments",
98 idata = client.QueryInstances([], ["name"], False)
99 inames = [row[0] for row in idata]
101 elif mode in (_SHUTDOWN_NODES_BOTH,
103 _SHUTDOWN_NODES_SEC) + _SHUTDOWN_NODES_TAGS_MODES:
104 if mode in _SHUTDOWN_NODES_TAGS_MODES:
106 raise errors.OpPrereqError("No node tags passed", errors.ECODE_INVAL)
107 ndata = client.QueryNodes([], ["name", "pinst_list",
108 "sinst_list", "tags"], False)
109 ndata = [row for row in ndata if set(row[3]).intersection(names)]
112 raise errors.OpPrereqError("No node names passed", errors.ECODE_INVAL)
113 ndata = client.QueryNodes(names, ["name", "pinst_list", "sinst_list"],
116 ipri = [row[1] for row in ndata]
117 pri_names = list(itertools.chain(*ipri))
118 isec = [row[2] for row in ndata]
119 sec_names = list(itertools.chain(*isec))
120 if mode in (_SHUTDOWN_NODES_BOTH, _SHUTDOWN_NODES_BOTH_BY_TAGS):
121 inames = pri_names + sec_names
122 elif mode in (_SHUTDOWN_NODES_PRI, _SHUTDOWN_NODES_PRI_BY_TAGS):
124 elif mode in (_SHUTDOWN_NODES_SEC, _SHUTDOWN_NODES_SEC_BY_TAGS):
127 raise errors.ProgrammerError("Unhandled shutdown type")
128 elif mode == _SHUTDOWN_INSTANCES:
130 raise errors.OpPrereqError("No instance names passed",
132 idata = client.QueryInstances(names, ["name"], False)
133 inames = [row[0] for row in idata]
134 elif mode == _SHUTDOWN_INSTANCES_BY_TAGS:
136 raise errors.OpPrereqError("No instance tags passed",
138 idata = client.QueryInstances([], ["name", "tags"], False)
139 inames = [row[0] for row in idata if set(row[1]).intersection(names)]
141 raise errors.OpPrereqError("Unknown mode '%s'" % mode, errors.ECODE_INVAL)
146 def _ConfirmOperation(inames, text, extra=""):
147 """Ask the user to confirm an operation on a list of instances.
149 This function is used to request confirmation for doing an operation
150 on a given list of instances.
153 @param inames: the list of names that we display when
154 we ask for confirmation
156 @param text: the operation that the user should confirm
157 (e.g. I{shutdown} or I{startup})
159 @return: True or False depending on user's confirmation.
163 msg = ("The %s will operate on %d instances.\n%s"
164 "Do you want to continue?" % (text, count, extra))
165 affected = ("\nAffected instances:\n" +
166 "\n".join([" %s" % name for name in inames]))
168 choices = [('y', True, 'Yes, execute the %s' % text),
169 ('n', False, 'No, abort the %s' % text)]
172 choices.insert(1, ('v', 'v', 'View the list of affected instances'))
177 choice = AskUser(ask, choices)
180 choice = AskUser(msg + affected, choices)
184 def _EnsureInstancesExist(client, names):
185 """Check for and ensure the given instance names exist.
187 This function will raise an OpPrereqError in case they don't
188 exist. Otherwise it will exit cleanly.
190 @type client: L{ganeti.luxi.Client}
191 @param client: the client to use for the query
193 @param names: the list of instance names to query
194 @raise errors.OpPrereqError: in case any instance is missing
197 # TODO: change LUQueryInstances to that it actually returns None
198 # instead of raising an exception, or devise a better mechanism
199 result = client.QueryInstances(names, ["name"], False)
200 for orig_name, row in zip(names, result):
202 raise errors.OpPrereqError("Instance '%s' does not exist" % orig_name,
206 def GenericManyOps(operation, fn):
207 """Generic multi-instance operations.
209 The will return a wrapper that processes the options and arguments
210 given, and uses the passed function to build the opcode needed for
211 the specific operation. Thus all the generic loop/confirmation code
212 is abstracted into this function.
215 def realfn(opts, args):
216 if opts.multi_mode is None:
217 opts.multi_mode = _SHUTDOWN_INSTANCES
219 inames = _ExpandMultiNames(opts.multi_mode, args, client=cl)
221 raise errors.OpPrereqError("Selection filter does not match"
222 " any instances", errors.ECODE_INVAL)
223 multi_on = opts.multi_mode != _SHUTDOWN_INSTANCES or len(inames) > 1
224 if not (opts.force_multi or not multi_on
225 or _ConfirmOperation(inames, operation)):
227 jex = JobExecutor(verbose=multi_on, cl=cl, opts=opts)
230 jex.QueueJob(name, op)
231 results = jex.WaitOrShow(not opts.submit_only)
232 rcode = compat.all(row[0] for row in results)
233 return int(not rcode)
237 def ListInstances(opts, args):
238 """List instances and their properties.
240 @param opts: the command line options selected by the user
242 @param args: should be an empty list
244 @return: the desired exit code
247 if opts.output is None:
248 selected_fields = _LIST_DEF_FIELDS
249 elif opts.output.startswith("+"):
250 selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",")
252 selected_fields = opts.output.split(",")
254 output = GetClient().QueryInstances(args, selected_fields, opts.do_locking)
256 if not opts.no_headers:
258 "name": "Instance", "os": "OS", "pnode": "Primary_node",
259 "snodes": "Secondary_Nodes", "admin_state": "Autostart",
260 "oper_state": "Running",
261 "oper_ram": "Memory", "disk_template": "Disk_template",
262 "oper_vcpus": "VCPUs",
263 "ip": "IP_address", "mac": "MAC_address",
264 "nic_mode": "NIC_Mode", "nic_link": "NIC_Link",
266 "sda_size": "Disk/0", "sdb_size": "Disk/1",
267 "disk_usage": "DiskUsage",
268 "status": "Status", "tags": "Tags",
269 "network_port": "Network_port",
270 "hv/kernel_path": "Kernel_path",
271 "hv/initrd_path": "Initrd_path",
272 "hv/boot_order": "Boot_order",
275 "hv/cdrom_image_path": "CDROM_image_path",
276 "hv/nic_type": "NIC_type",
277 "hv/disk_type": "Disk_type",
278 "hv/vnc_bind_address": "VNC_bind_address",
279 "serial_no": "SerialNo", "hypervisor": "Hypervisor",
280 "hvparams": "Hypervisor_parameters",
281 "be/memory": "Configured_memory",
284 "be/auto_balance": "Auto_balance",
285 "disk.count": "Disks", "disk.sizes": "Disk_sizes",
286 "nic.count": "NICs", "nic.ips": "NIC_IPs",
287 "nic.modes": "NIC_modes", "nic.links": "NIC_links",
288 "nic.bridges": "NIC_bridges", "nic.macs": "NIC_MACs",
289 "ctime": "CTime", "mtime": "MTime", "uuid": "UUID",
294 unitfields = ["be/memory", "oper_ram", "sd(a|b)_size", "disk\.size/.*"]
295 numfields = ["be/memory", "oper_ram", "sd(a|b)_size", "be/vcpus",
296 "serial_no", "(disk|nic)\.count", "disk\.size/.*"]
298 list_type_fields = ("tags", "disk.sizes", "nic.macs", "nic.ips",
299 "nic.modes", "nic.links", "nic.bridges")
300 # change raw values to nicer strings
302 for idx, field in enumerate(selected_fields):
304 if field == "snodes":
305 val = ",".join(val) or "-"
306 elif field == "admin_state":
311 elif field == "oper_state":
318 elif field == "oper_ram":
321 elif field == "oper_vcpus":
324 elif field == "sda_size" or field == "sdb_size":
327 elif field == "ctime" or field == "mtime":
328 val = utils.FormatTime(val)
329 elif field in list_type_fields:
330 val = ",".join(str(item) for item in val)
333 if opts.roman_integers and isinstance(val, int):
334 val = compat.TryToRoman(val)
337 data = GenerateTable(separator=opts.separator, headers=headers,
338 fields=selected_fields, unitfields=unitfields,
339 numfields=numfields, data=output, units=opts.units)
347 def AddInstance(opts, args):
348 """Add an instance to the cluster.
350 This is just a wrapper over GenericInstanceCreate.
353 return GenericInstanceCreate(constants.INSTANCE_CREATE, opts, args)
356 def BatchCreate(opts, args):
357 """Create instances using a definition file.
359 This function reads a json file with instances defined
363 "disk_size": [20480],
369 "primary_node": "firstnode",
370 "secondary_node": "secondnode",
371 "iallocator": "dumb"}
374 Note that I{primary_node} and I{secondary_node} have precedence over
377 @param opts: the command line options selected by the user
379 @param args: should contain one element, the json filename
381 @return: the desired exit code
384 _DEFAULT_SPECS = {"disk_size": [20 * 1024],
387 "primary_node": None,
388 "secondary_node": None,
395 "file_storage_dir": None,
396 "force_variant": False,
397 "file_driver": 'loop'}
399 def _PopulateWithDefaults(spec):
400 """Returns a new hash combined with default values."""
401 mydict = _DEFAULT_SPECS.copy()
406 """Validate the instance specs."""
407 # Validate fields required under any circumstances
408 for required_field in ('os', 'template'):
409 if required_field not in spec:
410 raise errors.OpPrereqError('Required field "%s" is missing.' %
411 required_field, errors.ECODE_INVAL)
412 # Validate special fields
413 if spec['primary_node'] is not None:
414 if (spec['template'] in constants.DTS_NET_MIRROR and
415 spec['secondary_node'] is None):
416 raise errors.OpPrereqError('Template requires secondary node, but'
417 ' there was no secondary provided.',
419 elif spec['iallocator'] is None:
420 raise errors.OpPrereqError('You have to provide at least a primary_node'
421 ' or an iallocator.',
424 if (spec['hvparams'] and
425 not isinstance(spec['hvparams'], dict)):
426 raise errors.OpPrereqError('Hypervisor parameters must be a dict.',
429 json_filename = args[0]
431 instance_data = simplejson.loads(utils.ReadFile(json_filename))
432 except Exception, err: # pylint: disable-msg=W0703
433 ToStderr("Can't parse the instance definition file: %s" % str(err))
436 if not isinstance(instance_data, dict):
437 ToStderr("The instance definition file is not in dict format.")
440 jex = JobExecutor(opts=opts)
442 # Iterate over the instances and do:
443 # * Populate the specs with default value
444 # * Validate the instance specs
445 i_names = utils.NiceSort(instance_data.keys()) # pylint: disable-msg=E1103
447 specs = instance_data[name]
448 specs = _PopulateWithDefaults(specs)
451 hypervisor = specs['hypervisor']
452 hvparams = specs['hvparams']
455 for elem in specs['disk_size']:
457 size = utils.ParseUnit(elem)
458 except (TypeError, ValueError), err:
459 raise errors.OpPrereqError("Invalid disk size '%s' for"
461 (elem, name, err), errors.ECODE_INVAL)
462 disks.append({"size": size})
464 utils.ForceDictType(specs['backend'], constants.BES_PARAMETER_TYPES)
465 utils.ForceDictType(hvparams, constants.HVS_PARAMETER_TYPES)
468 for field in ('ip', 'mac', 'mode', 'link', 'bridge'):
472 tmp_nics[0][field] = specs[field]
474 if specs['nics'] is not None and tmp_nics:
475 raise errors.OpPrereqError("'nics' list incompatible with using"
476 " individual nic fields as well",
478 elif specs['nics'] is not None:
479 tmp_nics = specs['nics']
483 op = opcodes.OpCreateInstance(instance_name=name,
485 disk_template=specs['template'],
486 mode=constants.INSTANCE_CREATE,
488 force_variant=specs["force_variant"],
489 pnode=specs['primary_node'],
490 snode=specs['secondary_node'],
492 start=specs['start'],
493 ip_check=specs['ip_check'],
494 name_check=specs['name_check'],
496 iallocator=specs['iallocator'],
497 hypervisor=hypervisor,
499 beparams=specs['backend'],
500 file_storage_dir=specs['file_storage_dir'],
501 file_driver=specs['file_driver'])
503 jex.QueueJob(name, op)
504 # we never want to wait, just show the submitted job IDs
505 jex.WaitOrShow(False)
510 def ReinstallInstance(opts, args):
511 """Reinstall an instance.
513 @param opts: the command line options selected by the user
515 @param args: should contain only one element, the name of the
516 instance to be reinstalled
518 @return: the desired exit code
521 # first, compute the desired name list
522 if opts.multi_mode is None:
523 opts.multi_mode = _SHUTDOWN_INSTANCES
525 inames = _ExpandMultiNames(opts.multi_mode, args)
527 raise errors.OpPrereqError("Selection filter does not match any instances",
530 # second, if requested, ask for an OS
531 if opts.select_os is True:
532 op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "variants"],
534 result = SubmitOpCode(op, opts=opts)
537 ToStdout("Can't get the OS list")
540 ToStdout("Available OS templates:")
543 for (name, valid, variants) in result:
545 for entry in CalculateOSNames(name, variants):
546 ToStdout("%3s: %s", number, entry)
547 choices.append(("%s" % number, entry, entry))
550 choices.append(('x', 'exit', 'Exit gnt-instance reinstall'))
551 selected = AskUser("Enter OS template number (or x to abort):",
554 if selected == 'exit':
555 ToStderr("User aborted reinstall, exiting")
562 # third, get confirmation: multi-reinstall requires --force-multi,
563 # single-reinstall either --force or --force-multi (--force-multi is
564 # a stronger --force)
565 multi_on = opts.multi_mode != _SHUTDOWN_INSTANCES or len(inames) > 1
567 warn_msg = "Note: this will remove *all* data for the below instances!\n"
568 if not (opts.force_multi or
569 _ConfirmOperation(inames, "reinstall", extra=warn_msg)):
572 if not (opts.force or opts.force_multi):
573 usertext = ("This will reinstall the instance %s and remove"
574 " all data. Continue?") % inames[0]
575 if not AskUser(usertext):
578 jex = JobExecutor(verbose=multi_on, opts=opts)
579 for instance_name in inames:
580 op = opcodes.OpReinstallInstance(instance_name=instance_name,
582 force_variant=opts.force_variant)
583 jex.QueueJob(instance_name, op)
585 jex.WaitOrShow(not opts.submit_only)
589 def RemoveInstance(opts, args):
590 """Remove an instance.
592 @param opts: the command line options selected by the user
594 @param args: should contain only one element, the name of
595 the instance to be removed
597 @return: the desired exit code
600 instance_name = args[0]
605 _EnsureInstancesExist(cl, [instance_name])
607 usertext = ("This will remove the volumes of the instance %s"
608 " (including mirrors), thus removing all the data"
609 " of the instance. Continue?") % instance_name
610 if not AskUser(usertext):
613 op = opcodes.OpRemoveInstance(instance_name=instance_name,
614 ignore_failures=opts.ignore_failures,
615 shutdown_timeout=opts.shutdown_timeout)
616 SubmitOrSend(op, opts, cl=cl)
620 def RenameInstance(opts, args):
621 """Rename an instance.
623 @param opts: the command line options selected by the user
625 @param args: should contain two elements, the old and the
628 @return: the desired exit code
631 if not opts.name_check:
632 if not AskUser("As you disabled the check of the DNS entry, please verify"
633 " that '%s' is a FQDN. Continue?" % args[1]):
636 op = opcodes.OpRenameInstance(instance_name=args[0],
638 ip_check=opts.ip_check,
639 name_check=opts.name_check)
640 result = SubmitOrSend(op, opts)
642 ToStdout("Instance '%s' renamed to '%s'", args[0], result)
647 def ActivateDisks(opts, args):
648 """Activate an instance's disks.
650 This serves two purposes:
651 - it allows (as long as the instance is not running)
652 mounting the disks and modifying them from the node
653 - it repairs inactive secondary drbds
655 @param opts: the command line options selected by the user
657 @param args: should contain only one element, the instance name
659 @return: the desired exit code
662 instance_name = args[0]
663 op = opcodes.OpActivateInstanceDisks(instance_name=instance_name,
664 ignore_size=opts.ignore_size)
665 disks_info = SubmitOrSend(op, opts)
666 for host, iname, nname in disks_info:
667 ToStdout("%s:%s:%s", host, iname, nname)
671 def DeactivateDisks(opts, args):
672 """Deactivate an instance's disks.
674 This function takes the instance name, looks for its primary node
675 and the tries to shutdown its block devices on that node.
677 @param opts: the command line options selected by the user
679 @param args: should contain only one element, the instance name
681 @return: the desired exit code
684 instance_name = args[0]
685 op = opcodes.OpDeactivateInstanceDisks(instance_name=instance_name)
686 SubmitOrSend(op, opts)
690 def RecreateDisks(opts, args):
691 """Recreate an instance's disks.
693 @param opts: the command line options selected by the user
695 @param args: should contain only one element, the instance name
697 @return: the desired exit code
700 instance_name = args[0]
703 opts.disks = [int(v) for v in opts.disks.split(",")]
704 except (ValueError, TypeError), err:
705 ToStderr("Invalid disks value: %s" % str(err))
710 op = opcodes.OpRecreateInstanceDisks(instance_name=instance_name,
712 SubmitOrSend(op, opts)
716 def GrowDisk(opts, args):
717 """Grow an instance's disks.
719 @param opts: the command line options selected by the user
721 @param args: should contain two elements, the instance name
722 whose disks we grow and the disk name, e.g. I{sda}
724 @return: the desired exit code
731 except (TypeError, ValueError), err:
732 raise errors.OpPrereqError("Invalid disk index: %s" % str(err),
734 amount = utils.ParseUnit(args[2])
735 op = opcodes.OpGrowDisk(instance_name=instance, disk=disk, amount=amount,
736 wait_for_sync=opts.wait_for_sync)
737 SubmitOrSend(op, opts)
741 def _StartupInstance(name, opts):
742 """Startup instances.
744 This returns the opcode to start an instance, and its decorator will
745 wrap this into a loop starting all desired instances.
747 @param name: the name of the instance to act on
748 @param opts: the command line options selected by the user
749 @return: the opcode needed for the operation
752 op = opcodes.OpStartupInstance(instance_name=name,
754 # do not add these parameters to the opcode unless they're defined
756 op.hvparams = opts.hvparams
758 op.beparams = opts.beparams
762 def _RebootInstance(name, opts):
763 """Reboot instance(s).
765 This returns the opcode to reboot an instance, and its decorator
766 will wrap this into a loop rebooting all desired instances.
768 @param name: the name of the instance to act on
769 @param opts: the command line options selected by the user
770 @return: the opcode needed for the operation
773 return opcodes.OpRebootInstance(instance_name=name,
774 reboot_type=opts.reboot_type,
775 ignore_secondaries=opts.ignore_secondaries,
776 shutdown_timeout=opts.shutdown_timeout)
779 def _ShutdownInstance(name, opts):
780 """Shutdown an instance.
782 This returns the opcode to shutdown an instance, and its decorator
783 will wrap this into a loop shutting down all desired instances.
785 @param name: the name of the instance to act on
786 @param opts: the command line options selected by the user
787 @return: the opcode needed for the operation
790 return opcodes.OpShutdownInstance(instance_name=name,
791 timeout=opts.timeout)
794 def ReplaceDisks(opts, args):
795 """Replace the disks of an instance
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
804 new_2ndary = opts.dst_node
805 iallocator = opts.iallocator
806 if opts.disks is None:
810 disks = [int(i) for i in opts.disks.split(",")]
811 except (TypeError, ValueError), err:
812 raise errors.OpPrereqError("Invalid disk index passed: %s" % str(err),
814 cnt = [opts.on_primary, opts.on_secondary, opts.auto,
815 new_2ndary is not None, iallocator is not None].count(True)
817 raise errors.OpPrereqError("One and only one of the -p, -s, -a, -n and -i"
818 " options must be passed", errors.ECODE_INVAL)
819 elif opts.on_primary:
820 mode = constants.REPLACE_DISK_PRI
821 elif opts.on_secondary:
822 mode = constants.REPLACE_DISK_SEC
824 mode = constants.REPLACE_DISK_AUTO
826 raise errors.OpPrereqError("Cannot specify disks when using automatic"
827 " mode", errors.ECODE_INVAL)
828 elif new_2ndary is not None or iallocator is not None:
830 mode = constants.REPLACE_DISK_CHG
832 op = opcodes.OpReplaceDisks(instance_name=args[0], disks=disks,
833 remote_node=new_2ndary, mode=mode,
834 iallocator=iallocator,
835 early_release=opts.early_release)
836 SubmitOrSend(op, opts)
840 def FailoverInstance(opts, args):
841 """Failover an instance.
843 The failover is done by shutting it down on its present node and
844 starting it on the secondary.
846 @param opts: the command line options selected by the user
848 @param args: should contain only one element, the instance name
850 @return: the desired exit code
854 instance_name = args[0]
858 _EnsureInstancesExist(cl, [instance_name])
860 usertext = ("Failover will happen to image %s."
861 " This requires a shutdown of the instance. Continue?" %
863 if not AskUser(usertext):
866 op = opcodes.OpFailoverInstance(instance_name=instance_name,
867 ignore_consistency=opts.ignore_consistency,
868 shutdown_timeout=opts.shutdown_timeout)
869 SubmitOrSend(op, opts, cl=cl)
873 def MigrateInstance(opts, args):
874 """Migrate an instance.
876 The migrate is done without shutdown.
878 @param opts: the command line options selected by the user
880 @param args: should contain only one element, the instance name
882 @return: the desired exit code
886 instance_name = args[0]
890 _EnsureInstancesExist(cl, [instance_name])
893 usertext = ("Instance %s will be recovered from a failed migration."
894 " Note that the migration procedure (including cleanup)" %
897 usertext = ("Instance %s will be migrated. Note that migration" %
899 usertext += (" might impact the instance if anything goes wrong"
900 " (e.g. due to bugs in the hypervisor). Continue?")
901 if not AskUser(usertext):
904 # this should be removed once --non-live is deprecated
905 if not opts.live and opts.migration_mode is not None:
906 raise errors.OpPrereqError("Only one of the --non-live and "
907 "--migration-mode options can be passed",
909 if not opts.live: # --non-live passed
910 mode = constants.HT_MIGRATION_NONLIVE
912 mode = opts.migration_mode
914 op = opcodes.OpMigrateInstance(instance_name=instance_name, mode=mode,
915 cleanup=opts.cleanup)
916 SubmitOpCode(op, cl=cl, opts=opts)
920 def MoveInstance(opts, args):
923 @param opts: the command line options selected by the user
925 @param args: should contain only one element, the instance name
927 @return: the desired exit code
931 instance_name = args[0]
935 usertext = ("Instance %s will be moved."
936 " This requires a shutdown of the instance. Continue?" %
938 if not AskUser(usertext):
941 op = opcodes.OpMoveInstance(instance_name=instance_name,
942 target_node=opts.node,
943 shutdown_timeout=opts.shutdown_timeout)
944 SubmitOrSend(op, opts, cl=cl)
948 def ConnectToInstanceConsole(opts, args):
949 """Connect to the console of an instance.
951 @param opts: the command line options selected by the user
953 @param args: should contain only one element, the instance name
955 @return: the desired exit code
958 instance_name = args[0]
960 op = opcodes.OpConnectConsole(instance_name=instance_name)
961 cmd = SubmitOpCode(op, opts=opts)
963 if opts.show_command:
964 ToStdout("%s", utils.ShellQuoteArgs(cmd))
967 os.execvp(cmd[0], cmd)
969 ToStderr("Can't run console command %s with arguments:\n'%s'",
970 cmd[0], " ".join(cmd))
971 os._exit(1) # pylint: disable-msg=W0212
974 def _FormatLogicalID(dev_type, logical_id, roman):
975 """Formats the logical_id of a disk.
978 if dev_type == constants.LD_DRBD8:
979 node_a, node_b, port, minor_a, minor_b, key = logical_id
981 ("nodeA", "%s, minor=%s" % (node_a, compat.TryToRoman(minor_a,
983 ("nodeB", "%s, minor=%s" % (node_b, compat.TryToRoman(minor_b,
985 ("port", compat.TryToRoman(port, convert=roman)),
988 elif dev_type == constants.LD_LV:
989 vg_name, lv_name = logical_id
990 data = ["%s/%s" % (vg_name, lv_name)]
992 data = [str(logical_id)]
997 def _FormatBlockDevInfo(idx, top_level, dev, static, roman):
998 """Show block device information.
1000 This is only used by L{ShowInstanceConfig}, but it's too big to be
1001 left for an inline definition.
1004 @param idx: the index of the current disk
1005 @type top_level: boolean
1006 @param top_level: if this a top-level disk?
1008 @param dev: dictionary with disk information
1009 @type static: boolean
1010 @param static: wheter the device information doesn't contain
1011 runtime information but only static data
1012 @type roman: boolean
1013 @param roman: whether to try to use roman integers
1014 @return: a list of either strings, tuples or lists
1015 (which should be formatted at a higher indent level)
1018 def helper(dtype, status):
1019 """Format one line for physical device status.
1022 @param dtype: a constant from the L{constants.LDS_BLOCK} set
1024 @param status: a tuple as returned from L{backend.FindBlockDevice}
1025 @return: the string representing the status
1031 (path, major, minor, syncp, estt, degr, ldisk_status) = status
1033 major_string = "N/A"
1035 major_string = str(compat.TryToRoman(major, convert=roman))
1038 minor_string = "N/A"
1040 minor_string = str(compat.TryToRoman(minor, convert=roman))
1042 txt += ("%s (%s:%s)" % (path, major_string, minor_string))
1043 if dtype in (constants.LD_DRBD8, ):
1044 if syncp is not None:
1045 sync_text = "*RECOVERING* %5.2f%%," % syncp
1047 sync_text += " ETA %ss" % compat.TryToRoman(estt, convert=roman)
1049 sync_text += " ETA unknown"
1051 sync_text = "in sync"
1053 degr_text = "*DEGRADED*"
1056 if ldisk_status == constants.LDS_FAULTY:
1057 ldisk_text = " *MISSING DISK*"
1058 elif ldisk_status == constants.LDS_UNKNOWN:
1059 ldisk_text = " *UNCERTAIN STATE*"
1062 txt += (" %s, status %s%s" % (sync_text, degr_text, ldisk_text))
1063 elif dtype == constants.LD_LV:
1064 if ldisk_status == constants.LDS_FAULTY:
1065 ldisk_text = " *FAILED* (failed drive?)"
1073 if dev["iv_name"] is not None:
1074 txt = dev["iv_name"]
1076 txt = "disk %s" % compat.TryToRoman(idx, convert=roman)
1078 txt = "child %s" % compat.TryToRoman(idx, convert=roman)
1079 if isinstance(dev["size"], int):
1080 nice_size = utils.FormatUnit(dev["size"], "h")
1082 nice_size = dev["size"]
1083 d1 = ["- %s: %s, size %s" % (txt, dev["dev_type"], nice_size)]
1086 data.append(("access mode", dev["mode"]))
1087 if dev["logical_id"] is not None:
1089 l_id = _FormatLogicalID(dev["dev_type"], dev["logical_id"], roman)
1091 l_id = [str(dev["logical_id"])]
1093 data.append(("logical_id", l_id[0]))
1096 elif dev["physical_id"] is not None:
1097 data.append("physical_id:")
1098 data.append([dev["physical_id"]])
1100 data.append(("on primary", helper(dev["dev_type"], dev["pstatus"])))
1101 if dev["sstatus"] and not static:
1102 data.append(("on secondary", helper(dev["dev_type"], dev["sstatus"])))
1105 data.append("child devices:")
1106 for c_idx, child in enumerate(dev["children"]):
1107 data.append(_FormatBlockDevInfo(c_idx, False, child, static, roman))
1112 def _FormatList(buf, data, indent_level):
1113 """Formats a list of data at a given indent level.
1115 If the element of the list is:
1116 - a string, it is simply formatted as is
1117 - a tuple, it will be split into key, value and the all the
1118 values in a list will be aligned all at the same start column
1119 - a list, will be recursively formatted
1122 @param buf: the buffer into which we write the output
1123 @param data: the list to format
1124 @type indent_level: int
1125 @param indent_level: the indent level to format at
1128 max_tlen = max([len(elem[0]) for elem in data
1129 if isinstance(elem, tuple)] or [0])
1131 if isinstance(elem, basestring):
1132 buf.write("%*s%s\n" % (2*indent_level, "", elem))
1133 elif isinstance(elem, tuple):
1135 spacer = "%*s" % (max_tlen - len(key), "")
1136 buf.write("%*s%s:%s %s\n" % (2*indent_level, "", key, spacer, value))
1137 elif isinstance(elem, list):
1138 _FormatList(buf, elem, indent_level+1)
1141 def _FormatParameterDict(buf, per_inst, actual):
1142 """Formats a parameter dictionary.
1144 @type buf: L{StringIO}
1145 @param buf: the buffer into which to write
1146 @type per_inst: dict
1147 @param per_inst: the instance's own parameters
1149 @param actual: the current parameter set (including defaults)
1152 for key in sorted(actual):
1153 val = per_inst.get(key, "default (%s)" % actual[key])
1154 buf.write(" - %s: %s\n" % (key, val))
1156 def ShowInstanceConfig(opts, args):
1157 """Compute instance run-time status.
1159 @param opts: the command line options selected by the user
1161 @param args: either an empty list, and then we query all
1162 instances, or should contain a list of instance names
1164 @return: the desired exit code
1167 if not args and not opts.show_all:
1168 ToStderr("No instance selected."
1169 " Please pass in --all if you want to query all instances.\n"
1170 "Note that this can take a long time on a big cluster.")
1172 elif args and opts.show_all:
1173 ToStderr("Cannot use --all if you specify instance names.")
1177 op = opcodes.OpQueryInstanceData(instances=args, static=opts.static)
1178 result = SubmitOpCode(op, opts=opts)
1180 ToStdout("No instances.")
1185 for instance_name in result:
1186 instance = result[instance_name]
1187 buf.write("Instance name: %s\n" % instance["name"])
1188 buf.write("UUID: %s\n" % instance["uuid"])
1189 buf.write("Serial number: %s\n" %
1190 compat.TryToRoman(instance["serial_no"],
1191 convert=opts.roman_integers))
1192 buf.write("Creation time: %s\n" % utils.FormatTime(instance["ctime"]))
1193 buf.write("Modification time: %s\n" % utils.FormatTime(instance["mtime"]))
1194 buf.write("State: configured to be %s" % instance["config_state"])
1196 buf.write(", actual state is %s" % instance["run_state"])
1198 ##buf.write("Considered for memory checks in cluster verify: %s\n" %
1199 ## instance["auto_balance"])
1200 buf.write(" Nodes:\n")
1201 buf.write(" - primary: %s\n" % instance["pnode"])
1202 buf.write(" - secondaries: %s\n" % utils.CommaJoin(instance["snodes"]))
1203 buf.write(" Operating system: %s\n" % instance["os"])
1204 _FormatParameterDict(buf, instance["os_instance"], instance["os_actual"])
1205 if instance.has_key("network_port"):
1206 buf.write(" Allocated network port: %s\n" %
1207 compat.TryToRoman(instance["network_port"],
1208 convert=opts.roman_integers))
1209 buf.write(" Hypervisor: %s\n" % instance["hypervisor"])
1211 # custom VNC console information
1212 vnc_bind_address = instance["hv_actual"].get(constants.HV_VNC_BIND_ADDRESS,
1214 if vnc_bind_address:
1215 port = instance["network_port"]
1216 display = int(port) - constants.VNC_BASE_PORT
1217 if display > 0 and vnc_bind_address == constants.IP4_ADDRESS_ANY:
1218 vnc_console_port = "%s:%s (display %s)" % (instance["pnode"],
1221 elif display > 0 and netutils.IsValidIP4(vnc_bind_address):
1222 vnc_console_port = ("%s:%s (node %s) (display %s)" %
1223 (vnc_bind_address, port,
1224 instance["pnode"], display))
1226 # vnc bind address is a file
1227 vnc_console_port = "%s:%s" % (instance["pnode"],
1229 buf.write(" - console connection: vnc to %s\n" % vnc_console_port)
1231 _FormatParameterDict(buf, instance["hv_instance"], instance["hv_actual"])
1232 buf.write(" Hardware:\n")
1233 buf.write(" - VCPUs: %s\n" %
1234 compat.TryToRoman(instance["be_actual"][constants.BE_VCPUS],
1235 convert=opts.roman_integers))
1236 buf.write(" - memory: %sMiB\n" %
1237 compat.TryToRoman(instance["be_actual"][constants.BE_MEMORY],
1238 convert=opts.roman_integers))
1239 buf.write(" - NICs:\n")
1240 for idx, (ip, mac, mode, link) in enumerate(instance["nics"]):
1241 buf.write(" - nic/%d: MAC: %s, IP: %s, mode: %s, link: %s\n" %
1242 (idx, mac, ip, mode, link))
1243 buf.write(" Disks:\n")
1245 for idx, device in enumerate(instance["disks"]):
1246 _FormatList(buf, _FormatBlockDevInfo(idx, True, device, opts.static,
1247 opts.roman_integers), 2)
1249 ToStdout(buf.getvalue().rstrip('\n'))
1253 def SetInstanceParams(opts, args):
1254 """Modifies an instance.
1256 All parameters take effect only at the next restart of the instance.
1258 @param opts: the command line options selected by the user
1260 @param args: should contain only one element, the instance name
1262 @return: the desired exit code
1265 if not (opts.nics or opts.disks or opts.disk_template or
1266 opts.hvparams or opts.beparams or opts.os or opts.osparams):
1267 ToStderr("Please give at least one of the parameters.")
1270 for param in opts.beparams:
1271 if isinstance(opts.beparams[param], basestring):
1272 if opts.beparams[param].lower() == "default":
1273 opts.beparams[param] = constants.VALUE_DEFAULT
1275 utils.ForceDictType(opts.beparams, constants.BES_PARAMETER_TYPES,
1276 allowed_values=[constants.VALUE_DEFAULT])
1278 for param in opts.hvparams:
1279 if isinstance(opts.hvparams[param], basestring):
1280 if opts.hvparams[param].lower() == "default":
1281 opts.hvparams[param] = constants.VALUE_DEFAULT
1283 utils.ForceDictType(opts.hvparams, constants.HVS_PARAMETER_TYPES,
1284 allowed_values=[constants.VALUE_DEFAULT])
1286 for idx, (nic_op, nic_dict) in enumerate(opts.nics):
1288 nic_op = int(nic_op)
1289 opts.nics[idx] = (nic_op, nic_dict)
1290 except (TypeError, ValueError):
1293 for idx, (disk_op, disk_dict) in enumerate(opts.disks):
1295 disk_op = int(disk_op)
1296 opts.disks[idx] = (disk_op, disk_dict)
1297 except (TypeError, ValueError):
1299 if disk_op == constants.DDM_ADD:
1300 if 'size' not in disk_dict:
1301 raise errors.OpPrereqError("Missing required parameter 'size'",
1303 disk_dict['size'] = utils.ParseUnit(disk_dict['size'])
1305 if (opts.disk_template and
1306 opts.disk_template in constants.DTS_NET_MIRROR and
1308 ToStderr("Changing the disk template to a mirrored one requires"
1309 " specifying a secondary node")
1312 op = opcodes.OpSetInstanceParams(instance_name=args[0],
1315 disk_template=opts.disk_template,
1316 remote_node=opts.node,
1317 hvparams=opts.hvparams,
1318 beparams=opts.beparams,
1320 osparams=opts.osparams,
1321 force_variant=opts.force_variant,
1324 # even if here we process the result, we allow submit only
1325 result = SubmitOrSend(op, opts)
1328 ToStdout("Modified instance %s", args[0])
1329 for param, data in result:
1330 ToStdout(" - %-5s -> %s", param, data)
1331 ToStdout("Please don't forget that most parameters take effect"
1332 " only at the next start of the instance.")
1336 # multi-instance selection options
1337 m_force_multi = cli_option("--force-multiple", dest="force_multi",
1338 help="Do not ask for confirmation when more than"
1339 " one instance is affected",
1340 action="store_true", default=False)
1342 m_pri_node_opt = cli_option("--primary", dest="multi_mode",
1343 help="Filter by nodes (primary only)",
1344 const=_SHUTDOWN_NODES_PRI, action="store_const")
1346 m_sec_node_opt = cli_option("--secondary", dest="multi_mode",
1347 help="Filter by nodes (secondary only)",
1348 const=_SHUTDOWN_NODES_SEC, action="store_const")
1350 m_node_opt = cli_option("--node", dest="multi_mode",
1351 help="Filter by nodes (primary and secondary)",
1352 const=_SHUTDOWN_NODES_BOTH, action="store_const")
1354 m_clust_opt = cli_option("--all", dest="multi_mode",
1355 help="Select all instances in the cluster",
1356 const=_SHUTDOWN_CLUSTER, action="store_const")
1358 m_inst_opt = cli_option("--instance", dest="multi_mode",
1359 help="Filter by instance name [default]",
1360 const=_SHUTDOWN_INSTANCES, action="store_const")
1362 m_node_tags_opt = cli_option("--node-tags", dest="multi_mode",
1363 help="Filter by node tag",
1364 const=_SHUTDOWN_NODES_BOTH_BY_TAGS,
1365 action="store_const")
1367 m_pri_node_tags_opt = cli_option("--pri-node-tags", dest="multi_mode",
1368 help="Filter by primary node tag",
1369 const=_SHUTDOWN_NODES_PRI_BY_TAGS,
1370 action="store_const")
1372 m_sec_node_tags_opt = cli_option("--sec-node-tags", dest="multi_mode",
1373 help="Filter by secondary node tag",
1374 const=_SHUTDOWN_NODES_SEC_BY_TAGS,
1375 action="store_const")
1377 m_inst_tags_opt = cli_option("--tags", dest="multi_mode",
1378 help="Filter by instance tag",
1379 const=_SHUTDOWN_INSTANCES_BY_TAGS,
1380 action="store_const")
1382 # this is defined separately due to readability only
1388 FILESTORE_DRIVER_OPT,
1408 AddInstance, [ArgHost(min=1, max=1)], add_opts,
1409 "[...] -t disk-type -n node[:secondary-node] -o os-type <name>",
1410 "Creates and adds a new instance to the cluster"),
1412 BatchCreate, [ArgFile(min=1, max=1)], [],
1414 "Create a bunch of instances based on specs in the file."),
1416 ConnectToInstanceConsole, ARGS_ONE_INSTANCE,
1418 "[--show-cmd] <instance>", "Opens a console on the specified instance"),
1420 FailoverInstance, ARGS_ONE_INSTANCE,
1421 [FORCE_OPT, IGNORE_CONSIST_OPT, SUBMIT_OPT, SHUTDOWN_TIMEOUT_OPT],
1422 "[-f] <instance>", "Stops the instance and starts it on the backup node,"
1423 " using the remote mirror (only for instances of type drbd)"),
1425 MigrateInstance, ARGS_ONE_INSTANCE,
1426 [FORCE_OPT, NONLIVE_OPT, MIGRATION_MODE_OPT, CLEANUP_OPT],
1427 "[-f] <instance>", "Migrate instance to its secondary node"
1428 " (only for instances of type drbd)"),
1430 MoveInstance, ARGS_ONE_INSTANCE,
1431 [FORCE_OPT, SUBMIT_OPT, SINGLE_NODE_OPT, SHUTDOWN_TIMEOUT_OPT],
1432 "[-f] <instance>", "Move instance to an arbitrary node"
1433 " (only for instances of type file and lv)"),
1435 ShowInstanceConfig, ARGS_MANY_INSTANCES,
1436 [STATIC_OPT, ALL_OPT, ROMAN_OPT],
1437 "[-s] {--all | <instance>...}",
1438 "Show information on the specified instance(s)"),
1440 ListInstances, ARGS_MANY_INSTANCES,
1441 [NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT, SYNC_OPT, ROMAN_OPT],
1443 "Lists the instances and their status. The available fields are"
1444 " (see the man page for details): status, oper_state, oper_ram,"
1445 " oper_vcpus, name, os, pnode, snodes, admin_state, admin_ram,"
1446 " disk_template, ip, mac, nic_mode, nic_link, sda_size, sdb_size,"
1447 " vcpus, serial_no,"
1448 " nic.count, nic.mac/N, nic.ip/N, nic.mode/N, nic.link/N,"
1449 " nic.macs, nic.ips, nic.modes, nic.links,"
1450 " disk.count, disk.size/N, disk.sizes,"
1451 " hv/NAME, be/memory, be/vcpus, be/auto_balance,"
1453 " The default field"
1454 " list is (in order): %s." % utils.CommaJoin(_LIST_DEF_FIELDS),
1457 ReinstallInstance, [ArgInstance()],
1458 [FORCE_OPT, OS_OPT, FORCE_VARIANT_OPT, m_force_multi, m_node_opt,
1459 m_pri_node_opt, m_sec_node_opt, m_clust_opt, m_inst_opt, m_node_tags_opt,
1460 m_pri_node_tags_opt, m_sec_node_tags_opt, m_inst_tags_opt, SELECT_OS_OPT,
1462 "[-f] <instance>", "Reinstall a stopped instance"),
1464 RemoveInstance, ARGS_ONE_INSTANCE,
1465 [FORCE_OPT, SHUTDOWN_TIMEOUT_OPT, IGNORE_FAILURES_OPT, SUBMIT_OPT],
1466 "[-f] <instance>", "Shuts down the instance and removes it"),
1469 [ArgInstance(min=1, max=1), ArgHost(min=1, max=1)],
1470 [NOIPCHECK_OPT, NONAMECHECK_OPT, SUBMIT_OPT],
1471 "<instance> <new_name>", "Rename the instance"),
1473 ReplaceDisks, ARGS_ONE_INSTANCE,
1474 [AUTO_REPLACE_OPT, DISKIDX_OPT, IALLOCATOR_OPT, EARLY_RELEASE_OPT,
1475 NEW_SECONDARY_OPT, ON_PRIMARY_OPT, ON_SECONDARY_OPT, SUBMIT_OPT],
1476 "[-s|-p|-n NODE|-I NAME] <instance>",
1477 "Replaces all disks for the instance"),
1479 SetInstanceParams, ARGS_ONE_INSTANCE,
1480 [BACKEND_OPT, DISK_OPT, FORCE_OPT, HVOPTS_OPT, NET_OPT, SUBMIT_OPT,
1481 DISK_TEMPLATE_OPT, SINGLE_NODE_OPT, OS_OPT, FORCE_VARIANT_OPT,
1483 "<instance>", "Alters the parameters of an instance"),
1485 GenericManyOps("shutdown", _ShutdownInstance), [ArgInstance()],
1486 [m_node_opt, m_pri_node_opt, m_sec_node_opt, m_clust_opt,
1487 m_node_tags_opt, m_pri_node_tags_opt, m_sec_node_tags_opt,
1488 m_inst_tags_opt, m_inst_opt, m_force_multi, TIMEOUT_OPT, SUBMIT_OPT],
1489 "<instance>", "Stops an instance"),
1491 GenericManyOps("startup", _StartupInstance), [ArgInstance()],
1492 [FORCE_OPT, m_force_multi, m_node_opt, m_pri_node_opt, m_sec_node_opt,
1493 m_node_tags_opt, m_pri_node_tags_opt, m_sec_node_tags_opt,
1494 m_inst_tags_opt, m_clust_opt, m_inst_opt, SUBMIT_OPT, HVOPTS_OPT,
1496 "<instance>", "Starts an instance"),
1498 GenericManyOps("reboot", _RebootInstance), [ArgInstance()],
1499 [m_force_multi, REBOOT_TYPE_OPT, IGNORE_SECONDARIES_OPT, m_node_opt,
1500 m_pri_node_opt, m_sec_node_opt, m_clust_opt, m_inst_opt, SUBMIT_OPT,
1501 m_node_tags_opt, m_pri_node_tags_opt, m_sec_node_tags_opt,
1502 m_inst_tags_opt, SHUTDOWN_TIMEOUT_OPT],
1503 "<instance>", "Reboots an instance"),
1505 ActivateDisks, ARGS_ONE_INSTANCE, [SUBMIT_OPT, IGNORE_SIZE_OPT],
1506 "<instance>", "Activate an instance's disks"),
1507 'deactivate-disks': (
1508 DeactivateDisks, ARGS_ONE_INSTANCE, [SUBMIT_OPT],
1509 "<instance>", "Deactivate an instance's disks"),
1511 RecreateDisks, ARGS_ONE_INSTANCE, [SUBMIT_OPT, DISKIDX_OPT],
1512 "<instance>", "Recreate an instance's disks"),
1515 [ArgInstance(min=1, max=1), ArgUnknown(min=1, max=1),
1516 ArgUnknown(min=1, max=1)],
1517 [SUBMIT_OPT, NWSYNC_OPT],
1518 "<instance> <disk> <size>", "Grow an instance's disk"),
1520 ListTags, ARGS_ONE_INSTANCE, [],
1521 "<instance_name>", "List the tags of the given instance"),
1523 AddTags, [ArgInstance(min=1, max=1), ArgUnknown()],
1525 "<instance_name> tag...", "Add tags to the given instance"),
1527 RemoveTags, [ArgInstance(min=1, max=1), ArgUnknown()],
1529 "<instance_name> tag...", "Remove tags from given instance"),
1532 #: dictionary with aliases for commands
1534 'activate_block_devs': 'activate-disks',
1535 'replace_disks': 'replace-disks',
1541 if __name__ == '__main__':
1542 sys.exit(GenericMain(commands, aliases=aliases,
1543 override={"tag_type": constants.TAG_INSTANCE}))