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
24 This module implements the data structures which define the cluster
25 operations - the so-called opcodes.
27 Every operation which modifies the cluster state is expressed via
32 # this are practically structures, so disable the message about too
34 # pylint: disable-msg=R0903
37 class BaseOpCode(object):
38 """A simple serializable object.
40 This object serves as a parent class for OpCode without any custom
46 def __init__(self, **kwargs):
47 """Constructor for BaseOpCode.
49 The constructor takes only keyword arguments and will set
50 attributes on this object based on the passed arguments. As such,
51 it means that you should not pass arguments which are not in the
52 __slots__ attribute for this class.
55 slots = self._all_slots()
58 raise TypeError("Object %s doesn't support the parameter '%s'" %
59 (self.__class__.__name__, key))
60 setattr(self, key, kwargs[key])
62 def __getstate__(self):
63 """Generic serializer.
65 This method just returns the contents of the instance as a
69 @return: the instance attributes and their values
73 for name in self._all_slots():
74 if hasattr(self, name):
75 state[name] = getattr(self, name)
78 def __setstate__(self, state):
79 """Generic unserializer.
81 This method just restores from the serialized state the attributes
82 of the current instance.
84 @param state: the serialized opcode data
88 if not isinstance(state, dict):
89 raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
92 for name in self._all_slots():
93 if name not in state and hasattr(self, name):
97 setattr(self, name, state[name])
101 """Compute the list of all declared slots for a class.
105 for parent in cls.__mro__:
106 slots.extend(getattr(parent, "__slots__", []))
110 class OpCode(BaseOpCode):
113 This is the root of the actual OpCode hierarchy. All clases derived
114 from this class should override OP_ID.
116 @cvar OP_ID: The ID of this opcode. This should be unique amongst all
117 children of this class.
118 @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
120 @ivar priority: Opcode priority for queue
123 OP_ID = "OP_ABSTRACT"
124 __slots__ = ["dry_run", "debug_level", "priority"]
126 def __getstate__(self):
127 """Specialized getstate for opcodes.
129 This method adds to the state dictionary the OP_ID of the class,
130 so that on unload we can identify the correct class for
131 instantiating the opcode.
134 @return: the state as a dictionary
137 data = BaseOpCode.__getstate__(self)
138 data["OP_ID"] = self.OP_ID
142 def LoadOpCode(cls, data):
143 """Generic load opcode method.
145 The method identifies the correct opcode class from the dict-form
146 by looking for a OP_ID key, if this is not found, or its value is
147 not available in this module as a child of this class, we fail.
150 @param data: the serialized opcode
153 if not isinstance(data, dict):
154 raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
155 if "OP_ID" not in data:
156 raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
157 op_id = data["OP_ID"]
159 if op_id in OP_MAPPING:
160 op_class = OP_MAPPING[op_id]
162 raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
165 new_data = data.copy()
166 del new_data["OP_ID"]
167 op.__setstate__(new_data)
171 """Generates a summary description of this opcode.
174 # all OP_ID start with OP_, we remove that
176 field_name = getattr(self, "OP_DSC_FIELD", None)
178 field_value = getattr(self, field_name, None)
179 if isinstance(field_value, (list, tuple)):
180 field_value = ",".join(str(i) for i in field_value)
181 txt = "%s(%s)" % (txt, field_value)
187 class OpPostInitCluster(OpCode):
188 """Post cluster initialization.
190 This opcode does not touch the cluster at all. Its purpose is to run hooks
191 after the cluster has been initialized.
194 OP_ID = "OP_CLUSTER_POST_INIT"
198 class OpDestroyCluster(OpCode):
199 """Destroy the cluster.
201 This opcode has no other parameters. All the state is irreversibly
202 lost after the execution of this opcode.
205 OP_ID = "OP_CLUSTER_DESTROY"
209 class OpQueryClusterInfo(OpCode):
210 """Query cluster information."""
211 OP_ID = "OP_CLUSTER_QUERY"
215 class OpVerifyCluster(OpCode):
216 """Verify the cluster state.
218 @type skip_checks: C{list}
219 @ivar skip_checks: steps to be skipped from the verify process; this
220 needs to be a subset of
221 L{constants.VERIFY_OPTIONAL_CHECKS}; currently
222 only L{constants.VERIFY_NPLUSONE_MEM} can be passed
225 OP_ID = "OP_CLUSTER_VERIFY"
226 __slots__ = ["skip_checks", "verbose", "error_codes",
227 "debug_simulate_errors"]
230 class OpVerifyDisks(OpCode):
231 """Verify the cluster disks.
235 Result: a tuple of four elements:
236 - list of node names with bad data returned (unreachable, etc.)
237 - dict of node names with broken volume groups (values: error msg)
238 - list of instances with degraded disks (that should be activated)
239 - dict of instances with missing logical volumes (values: (node, vol)
240 pairs with details about the missing volumes)
242 In normal operation, all lists should be empty. A non-empty instance
243 list (3rd element of the result) is still ok (errors were fixed) but
244 non-empty node list means some node is down, and probably there are
245 unfixable drbd errors.
247 Note that only instances that are drbd-based are taken into
248 consideration. This might need to be revisited in the future.
251 OP_ID = "OP_CLUSTER_VERIFY_DISKS"
255 class OpRepairDiskSizes(OpCode):
256 """Verify the disk sizes of the instances and fixes configuration
259 Parameters: optional instances list, in case we want to restrict the
260 checks to only a subset of the instances.
262 Result: a list of tuples, (instance, disk, new-size) for changed
265 In normal operation, the list should be empty.
267 @type instances: list
268 @ivar instances: the list of instances to check, or empty for all instances
271 OP_ID = "OP_CLUSTER_REPAIR_DISK_SIZES"
272 __slots__ = ["instances"]
275 class OpQueryConfigValues(OpCode):
276 """Query cluster configuration values."""
277 OP_ID = "OP_CLUSTER_CONFIG_QUERY"
278 __slots__ = ["output_fields"]
281 class OpRenameCluster(OpCode):
282 """Rename the cluster.
285 @ivar name: The new name of the cluster. The name and/or the master IP
286 address will be changed to match the new name and its IP
290 OP_ID = "OP_CLUSTER_RENAME"
291 OP_DSC_FIELD = "name"
295 class OpSetClusterParams(OpCode):
296 """Change the parameters of the cluster.
298 @type vg_name: C{str} or C{None}
299 @ivar vg_name: The new volume group name or None to disable LVM usage.
302 OP_ID = "OP_CLUSTER_SET_PARAMS"
306 "enabled_hypervisors",
313 "candidate_pool_size",
314 "maintain_node_health",
318 "default_iallocator",
322 "prealloc_wipe_disks",
326 class OpRedistributeConfig(OpCode):
327 """Force a full push of the cluster configuration.
330 OP_ID = "OP_CLUSTER_REDIST_CONF"
334 class OpQuery(OpCode):
335 """Query for resources/items.
337 @ivar what: Resources to query for, must be one of L{constants.QR_OP_QUERY}
338 @ivar fields: List of fields to retrieve
339 @ivar filter: Query filter
350 class OpQueryFields(OpCode):
351 """Query for available resource/item fields.
353 @ivar what: Resources to query for, must be one of L{constants.QR_OP_QUERY}
354 @ivar fields: List of fields to retrieve
357 OP_ID = "OP_QUERY_FIELDS"
366 class OpRemoveNode(OpCode):
369 @type node_name: C{str}
370 @ivar node_name: The name of the node to remove. If the node still has
371 instances on it, the operation will fail.
374 OP_ID = "OP_NODE_REMOVE"
375 OP_DSC_FIELD = "node_name"
376 __slots__ = ["node_name"]
379 class OpAddNode(OpCode):
380 """Add a node to the cluster.
382 @type node_name: C{str}
383 @ivar node_name: The name of the node to add. This can be a short name,
384 but it will be expanded to the FQDN.
385 @type primary_ip: IP address
386 @ivar primary_ip: The primary IP of the node. This will be ignored when the
387 opcode is submitted, but will be filled during the node
388 add (so it will be visible in the job query).
389 @type secondary_ip: IP address
390 @ivar secondary_ip: The secondary IP of the node. This needs to be passed
391 if the cluster has been initialized in 'dual-network'
392 mode, otherwise it must not be given.
394 @ivar readd: Whether to re-add an existing node to the cluster. If
395 this is not passed, then the operation will abort if the node
396 name is already in the cluster; use this parameter to 'repair'
397 a node that had its configuration broken, or was reinstalled
398 without removal from the cluster.
400 @ivar group: The node group to which this node will belong.
401 @type vm_capable: C{bool}
402 @ivar vm_capable: The vm_capable node attribute
403 @type master_capable: C{bool}
404 @ivar master_capable: The master_capable node attribute
407 OP_ID = "OP_NODE_ADD"
408 OP_DSC_FIELD = "node_name"
409 __slots__ = ["node_name", "primary_ip", "secondary_ip", "readd", "group",
410 "vm_capable", "master_capable", "ndparams"]
413 class OpQueryNodes(OpCode):
414 """Compute the list of nodes."""
415 OP_ID = "OP_NODE_QUERY"
416 __slots__ = ["output_fields", "names", "use_locking"]
419 class OpQueryNodeVolumes(OpCode):
420 """Get list of volumes on node."""
421 OP_ID = "OP_NODE_QUERYVOLS"
422 __slots__ = ["nodes", "output_fields"]
425 class OpQueryNodeStorage(OpCode):
426 """Get information on storage for node(s)."""
427 OP_ID = "OP_NODE_QUERY_STORAGE"
436 class OpModifyNodeStorage(OpCode):
437 """Modifies the properies of a storage unit"""
438 OP_ID = "OP_NODE_MODIFY_STORAGE"
447 class OpRepairNodeStorage(OpCode):
448 """Repairs the volume group on a node."""
449 OP_ID = "OP_REPAIR_NODE_STORAGE"
450 OP_DSC_FIELD = "node_name"
455 "ignore_consistency",
459 class OpSetNodeParams(OpCode):
460 """Change the parameters of a node."""
461 OP_ID = "OP_NODE_SET_PARAMS"
462 OP_DSC_FIELD = "node_name"
477 class OpPowercycleNode(OpCode):
478 """Tries to powercycle a node."""
479 OP_ID = "OP_NODE_POWERCYCLE"
480 OP_DSC_FIELD = "node_name"
487 class OpMigrateNode(OpCode):
488 """Migrate all instances from a node."""
489 OP_ID = "OP_NODE_MIGRATE"
490 OP_DSC_FIELD = "node_name"
498 class OpNodeEvacuationStrategy(OpCode):
499 """Compute the evacuation strategy for a list of nodes."""
500 OP_ID = "OP_NODE_EVAC_STRATEGY"
501 OP_DSC_FIELD = "nodes"
502 __slots__ = ["nodes", "iallocator", "remote_node"]
507 class OpCreateInstance(OpCode):
508 """Create an instance.
510 @ivar instance_name: Instance name
511 @ivar mode: Instance creation mode (one of L{constants.INSTANCE_CREATE_MODES})
512 @ivar source_handshake: Signed handshake from source (remote import only)
513 @ivar source_x509_ca: Source X509 CA in PEM format (remote import only)
514 @ivar source_instance_name: Previous name of instance (remote import only)
515 @ivar source_shutdown_timeout: Shutdown timeout used for source instance
519 OP_ID = "OP_INSTANCE_CREATE"
520 OP_DSC_FIELD = "instance_name"
523 "os_type", "force_variant", "no_install",
524 "pnode", "disk_template", "snode", "mode",
526 "src_node", "src_path", "start", "identify_defaults",
527 "wait_for_sync", "ip_check", "name_check",
528 "file_storage_dir", "file_driver",
530 "hypervisor", "hvparams", "beparams", "osparams",
533 "source_instance_name",
534 "source_shutdown_timeout",
538 class OpReinstallInstance(OpCode):
539 """Reinstall an instance's OS."""
540 OP_ID = "OP_INSTANCE_REINSTALL"
541 OP_DSC_FIELD = "instance_name"
542 __slots__ = ["instance_name", "os_type", "force_variant", "osparams"]
545 class OpRemoveInstance(OpCode):
546 """Remove an instance."""
547 OP_ID = "OP_INSTANCE_REMOVE"
548 OP_DSC_FIELD = "instance_name"
556 class OpRenameInstance(OpCode):
557 """Rename an instance."""
558 OP_ID = "OP_INSTANCE_RENAME"
560 "instance_name", "ip_check", "new_name", "name_check",
564 class OpStartupInstance(OpCode):
565 """Startup an instance."""
566 OP_ID = "OP_INSTANCE_STARTUP"
567 OP_DSC_FIELD = "instance_name"
569 "instance_name", "force", "hvparams", "beparams", "ignore_offline_nodes",
573 class OpShutdownInstance(OpCode):
574 """Shutdown an instance."""
575 OP_ID = "OP_INSTANCE_SHUTDOWN"
576 OP_DSC_FIELD = "instance_name"
578 "instance_name", "timeout", "ignore_offline_nodes",
582 class OpRebootInstance(OpCode):
583 """Reboot an instance."""
584 OP_ID = "OP_INSTANCE_REBOOT"
585 OP_DSC_FIELD = "instance_name"
587 "instance_name", "reboot_type", "ignore_secondaries", "shutdown_timeout",
591 class OpReplaceDisks(OpCode):
592 """Replace the disks of an instance."""
593 OP_ID = "OP_INSTANCE_REPLACE_DISKS"
594 OP_DSC_FIELD = "instance_name"
596 "instance_name", "remote_node", "mode", "disks", "iallocator",
601 class OpFailoverInstance(OpCode):
602 """Failover an instance."""
603 OP_ID = "OP_INSTANCE_FAILOVER"
604 OP_DSC_FIELD = "instance_name"
606 "instance_name", "ignore_consistency", "shutdown_timeout",
610 class OpMigrateInstance(OpCode):
611 """Migrate an instance.
613 This migrates (without shutting down an instance) to its secondary
616 @ivar instance_name: the name of the instance
617 @ivar mode: the migration mode (live, non-live or None for auto)
620 OP_ID = "OP_INSTANCE_MIGRATE"
621 OP_DSC_FIELD = "instance_name"
622 __slots__ = ["instance_name", "mode", "cleanup", "live"]
625 class OpMoveInstance(OpCode):
628 This move (with shutting down an instance and data copying) to an
631 @ivar instance_name: the name of the instance
632 @ivar target_node: the destination node
635 OP_ID = "OP_INSTANCE_MOVE"
636 OP_DSC_FIELD = "instance_name"
638 "instance_name", "target_node", "shutdown_timeout",
642 class OpConnectConsole(OpCode):
643 """Connect to an instance's console."""
644 OP_ID = "OP_INSTANCE_CONSOLE"
645 OP_DSC_FIELD = "instance_name"
646 __slots__ = ["instance_name"]
649 class OpActivateInstanceDisks(OpCode):
650 """Activate an instance's disks."""
651 OP_ID = "OP_INSTANCE_ACTIVATE_DISKS"
652 OP_DSC_FIELD = "instance_name"
653 __slots__ = ["instance_name", "ignore_size"]
656 class OpDeactivateInstanceDisks(OpCode):
657 """Deactivate an instance's disks."""
658 OP_ID = "OP_INSTANCE_DEACTIVATE_DISKS"
659 OP_DSC_FIELD = "instance_name"
660 __slots__ = ["instance_name"]
663 class OpRecreateInstanceDisks(OpCode):
664 """Deactivate an instance's disks."""
665 OP_ID = "OP_INSTANCE_RECREATE_DISKS"
666 OP_DSC_FIELD = "instance_name"
667 __slots__ = ["instance_name", "disks"]
670 class OpQueryInstances(OpCode):
671 """Compute the list of instances."""
672 OP_ID = "OP_INSTANCE_QUERY"
673 __slots__ = ["output_fields", "names", "use_locking"]
676 class OpQueryInstanceData(OpCode):
677 """Compute the run-time status of instances."""
678 OP_ID = "OP_INSTANCE_QUERY_DATA"
679 __slots__ = ["instances", "static"]
682 class OpSetInstanceParams(OpCode):
683 """Change the parameters of an instance."""
684 OP_ID = "OP_INSTANCE_SET_PARAMS"
685 OP_DSC_FIELD = "instance_name"
688 "hvparams", "beparams", "osparams", "force",
689 "nics", "disks", "disk_template",
690 "remote_node", "os_name", "force_variant",
694 class OpGrowDisk(OpCode):
695 """Grow a disk of an instance."""
696 OP_ID = "OP_INSTANCE_GROW_DISK"
697 OP_DSC_FIELD = "instance_name"
699 "instance_name", "disk", "amount", "wait_for_sync",
705 class OpQueryGroups(OpCode):
706 """Compute the list of node groups."""
707 OP_ID = "OP_GROUP_QUERY"
708 __slots__ = ["output_fields", "names"]
712 class OpDiagnoseOS(OpCode):
713 """Compute the list of guest operating systems."""
714 OP_ID = "OP_OS_DIAGNOSE"
715 __slots__ = ["output_fields", "names"]
719 class OpQueryExports(OpCode):
720 """Compute the list of exported images."""
721 OP_ID = "OP_BACKUP_QUERY"
722 __slots__ = ["nodes", "use_locking"]
725 class OpPrepareExport(OpCode):
726 """Prepares an instance export.
728 @ivar instance_name: Instance name
729 @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
732 OP_ID = "OP_BACKUP_PREPARE"
733 OP_DSC_FIELD = "instance_name"
735 "instance_name", "mode",
739 class OpExportInstance(OpCode):
740 """Export an instance.
742 For local exports, the export destination is the node name. For remote
743 exports, the export destination is a list of tuples, each consisting of
744 hostname/IP address, port, HMAC and HMAC salt. The HMAC is calculated using
745 the cluster domain secret over the value "${index}:${hostname}:${port}". The
746 destination X509 CA must be a signed certificate.
748 @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
749 @ivar target_node: Export destination
750 @ivar x509_key_name: X509 key to use (remote export only)
751 @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
755 OP_ID = "OP_BACKUP_EXPORT"
756 OP_DSC_FIELD = "instance_name"
758 # TODO: Rename target_node as it changes meaning for different export modes
759 # (e.g. "destination")
760 "instance_name", "target_node", "shutdown", "shutdown_timeout",
762 "ignore_remove_failures",
765 "destination_x509_ca",
769 class OpRemoveExport(OpCode):
770 """Remove an instance's export."""
771 OP_ID = "OP_BACKUP_REMOVE"
772 OP_DSC_FIELD = "instance_name"
773 __slots__ = ["instance_name"]
777 class OpGetTags(OpCode):
778 """Returns the tags of the given object."""
779 OP_ID = "OP_TAGS_GET"
780 OP_DSC_FIELD = "name"
781 __slots__ = ["kind", "name"]
784 class OpSearchTags(OpCode):
785 """Searches the tags in the cluster for a given pattern."""
786 OP_ID = "OP_TAGS_SEARCH"
787 OP_DSC_FIELD = "pattern"
788 __slots__ = ["pattern"]
791 class OpAddTags(OpCode):
792 """Add a list of tags on a given object."""
793 OP_ID = "OP_TAGS_SET"
794 __slots__ = ["kind", "name", "tags"]
797 class OpDelTags(OpCode):
798 """Remove a list of tags from a given object."""
799 OP_ID = "OP_TAGS_DEL"
800 __slots__ = ["kind", "name", "tags"]
804 class OpTestDelay(OpCode):
805 """Sleeps for a configured amount of time.
807 This is used just for debugging and testing.
810 - duration: the time to sleep
811 - on_master: if true, sleep on the master
812 - on_nodes: list of nodes in which to sleep
814 If the on_master parameter is true, it will execute a sleep on the
815 master (before any node sleep).
817 If the on_nodes list is not empty, it will sleep on those nodes
818 (after the sleep on the master, if that is enabled).
820 As an additional feature, the case of duration < 0 will be reported
821 as an execution error, so this opcode can be used as a failure
822 generator. The case of duration == 0 will not be treated specially.
825 OP_ID = "OP_TEST_DELAY"
826 OP_DSC_FIELD = "duration"
827 __slots__ = ["duration", "on_master", "on_nodes", "repeat"]
830 class OpTestAllocator(OpCode):
831 """Allocator framework testing.
833 This opcode has two modes:
834 - gather and return allocator input for a given mode (allocate new
835 or replace secondary) and a given instance definition (direction
837 - run a selected allocator for a given operation (as above) and
838 return the allocator output (direction 'out')
841 OP_ID = "OP_TEST_ALLOCATOR"
842 OP_DSC_FIELD = "allocator"
844 "direction", "mode", "allocator", "name",
845 "mem_size", "disks", "disk_template",
846 "os", "tags", "nics", "vcpus", "hypervisor",
851 class OpTestJobqueue(OpCode):
852 """Utility opcode to test some aspects of the job queue.
855 OP_ID = "OP_TEST_JQUEUE"
864 class OpTestDummy(OpCode):
865 """Utility opcode used by unittests.
868 OP_ID = "OP_TEST_DUMMY"
876 OP_MAPPING = dict([(v.OP_ID, v) for v in globals().values()
877 if (isinstance(v, type) and issubclass(v, OpCode) and
878 hasattr(v, "OP_ID"))])