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 @cvar OP_DSC_FIELD: The name of a field whose value will be included in the
119 string returned by Summary(); see the docstring of that
121 @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
123 @ivar priority: Opcode priority for queue
126 OP_ID = "OP_ABSTRACT"
127 __slots__ = ["dry_run", "debug_level", "priority"]
129 def __getstate__(self):
130 """Specialized getstate for opcodes.
132 This method adds to the state dictionary the OP_ID of the class,
133 so that on unload we can identify the correct class for
134 instantiating the opcode.
137 @return: the state as a dictionary
140 data = BaseOpCode.__getstate__(self)
141 data["OP_ID"] = self.OP_ID
145 def LoadOpCode(cls, data):
146 """Generic load opcode method.
148 The method identifies the correct opcode class from the dict-form
149 by looking for a OP_ID key, if this is not found, or its value is
150 not available in this module as a child of this class, we fail.
153 @param data: the serialized opcode
156 if not isinstance(data, dict):
157 raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
158 if "OP_ID" not in data:
159 raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
160 op_id = data["OP_ID"]
162 if op_id in OP_MAPPING:
163 op_class = OP_MAPPING[op_id]
165 raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
168 new_data = data.copy()
169 del new_data["OP_ID"]
170 op.__setstate__(new_data)
174 """Generates a summary description of this opcode.
176 The summary is the value of the OP_ID attribute (without the "OP_" prefix),
177 plus the value of the OP_DSC_FIELD attribute, if one was defined; this field
178 should allow to easily identify the operation (for an instance creation job,
179 e.g., it would be the instance name).
182 # all OP_ID start with OP_, we remove that
184 field_name = getattr(self, "OP_DSC_FIELD", None)
186 field_value = getattr(self, field_name, None)
187 if isinstance(field_value, (list, tuple)):
188 field_value = ",".join(str(i) for i in field_value)
189 txt = "%s(%s)" % (txt, field_value)
195 class OpPostInitCluster(OpCode):
196 """Post cluster initialization.
198 This opcode does not touch the cluster at all. Its purpose is to run hooks
199 after the cluster has been initialized.
202 OP_ID = "OP_CLUSTER_POST_INIT"
206 class OpDestroyCluster(OpCode):
207 """Destroy the cluster.
209 This opcode has no other parameters. All the state is irreversibly
210 lost after the execution of this opcode.
213 OP_ID = "OP_CLUSTER_DESTROY"
217 class OpQueryClusterInfo(OpCode):
218 """Query cluster information."""
219 OP_ID = "OP_CLUSTER_QUERY"
223 class OpVerifyCluster(OpCode):
224 """Verify the cluster state.
226 @type skip_checks: C{list}
227 @ivar skip_checks: steps to be skipped from the verify process; this
228 needs to be a subset of
229 L{constants.VERIFY_OPTIONAL_CHECKS}; currently
230 only L{constants.VERIFY_NPLUSONE_MEM} can be passed
233 OP_ID = "OP_CLUSTER_VERIFY"
234 __slots__ = ["skip_checks", "verbose", "error_codes",
235 "debug_simulate_errors"]
238 class OpVerifyDisks(OpCode):
239 """Verify the cluster disks.
243 Result: a tuple of four elements:
244 - list of node names with bad data returned (unreachable, etc.)
245 - dict of node names with broken volume groups (values: error msg)
246 - list of instances with degraded disks (that should be activated)
247 - dict of instances with missing logical volumes (values: (node, vol)
248 pairs with details about the missing volumes)
250 In normal operation, all lists should be empty. A non-empty instance
251 list (3rd element of the result) is still ok (errors were fixed) but
252 non-empty node list means some node is down, and probably there are
253 unfixable drbd errors.
255 Note that only instances that are drbd-based are taken into
256 consideration. This might need to be revisited in the future.
259 OP_ID = "OP_CLUSTER_VERIFY_DISKS"
263 class OpRepairDiskSizes(OpCode):
264 """Verify the disk sizes of the instances and fixes configuration
267 Parameters: optional instances list, in case we want to restrict the
268 checks to only a subset of the instances.
270 Result: a list of tuples, (instance, disk, new-size) for changed
273 In normal operation, the list should be empty.
275 @type instances: list
276 @ivar instances: the list of instances to check, or empty for all instances
279 OP_ID = "OP_CLUSTER_REPAIR_DISK_SIZES"
280 __slots__ = ["instances"]
283 class OpQueryConfigValues(OpCode):
284 """Query cluster configuration values."""
285 OP_ID = "OP_CLUSTER_CONFIG_QUERY"
286 __slots__ = ["output_fields"]
289 class OpRenameCluster(OpCode):
290 """Rename the cluster.
293 @ivar name: The new name of the cluster. The name and/or the master IP
294 address will be changed to match the new name and its IP
298 OP_ID = "OP_CLUSTER_RENAME"
299 OP_DSC_FIELD = "name"
303 class OpSetClusterParams(OpCode):
304 """Change the parameters of the cluster.
306 @type vg_name: C{str} or C{None}
307 @ivar vg_name: The new volume group name or None to disable LVM usage.
310 OP_ID = "OP_CLUSTER_SET_PARAMS"
314 "enabled_hypervisors",
321 "candidate_pool_size",
322 "maintain_node_health",
326 "default_iallocator",
330 "prealloc_wipe_disks",
334 class OpRedistributeConfig(OpCode):
335 """Force a full push of the cluster configuration.
338 OP_ID = "OP_CLUSTER_REDIST_CONF"
342 class OpQuery(OpCode):
343 """Query for resources/items.
345 @ivar what: Resources to query for, must be one of L{constants.QR_OP_QUERY}
346 @ivar fields: List of fields to retrieve
347 @ivar filter: Query filter
358 class OpQueryFields(OpCode):
359 """Query for available resource/item fields.
361 @ivar what: Resources to query for, must be one of L{constants.QR_OP_QUERY}
362 @ivar fields: List of fields to retrieve
365 OP_ID = "OP_QUERY_FIELDS"
374 class OpRemoveNode(OpCode):
377 @type node_name: C{str}
378 @ivar node_name: The name of the node to remove. If the node still has
379 instances on it, the operation will fail.
382 OP_ID = "OP_NODE_REMOVE"
383 OP_DSC_FIELD = "node_name"
384 __slots__ = ["node_name"]
387 class OpAddNode(OpCode):
388 """Add a node to the cluster.
390 @type node_name: C{str}
391 @ivar node_name: The name of the node to add. This can be a short name,
392 but it will be expanded to the FQDN.
393 @type primary_ip: IP address
394 @ivar primary_ip: The primary IP of the node. This will be ignored when the
395 opcode is submitted, but will be filled during the node
396 add (so it will be visible in the job query).
397 @type secondary_ip: IP address
398 @ivar secondary_ip: The secondary IP of the node. This needs to be passed
399 if the cluster has been initialized in 'dual-network'
400 mode, otherwise it must not be given.
402 @ivar readd: Whether to re-add an existing node to the cluster. If
403 this is not passed, then the operation will abort if the node
404 name is already in the cluster; use this parameter to 'repair'
405 a node that had its configuration broken, or was reinstalled
406 without removal from the cluster.
408 @ivar group: The node group to which this node will belong.
409 @type vm_capable: C{bool}
410 @ivar vm_capable: The vm_capable node attribute
411 @type master_capable: C{bool}
412 @ivar master_capable: The master_capable node attribute
415 OP_ID = "OP_NODE_ADD"
416 OP_DSC_FIELD = "node_name"
417 __slots__ = ["node_name", "primary_ip", "secondary_ip", "readd", "group",
418 "vm_capable", "master_capable", "ndparams"]
421 class OpQueryNodes(OpCode):
422 """Compute the list of nodes."""
423 OP_ID = "OP_NODE_QUERY"
424 __slots__ = ["output_fields", "names", "use_locking"]
427 class OpQueryNodeVolumes(OpCode):
428 """Get list of volumes on node."""
429 OP_ID = "OP_NODE_QUERYVOLS"
430 __slots__ = ["nodes", "output_fields"]
433 class OpQueryNodeStorage(OpCode):
434 """Get information on storage for node(s)."""
435 OP_ID = "OP_NODE_QUERY_STORAGE"
444 class OpModifyNodeStorage(OpCode):
445 """Modifies the properies of a storage unit"""
446 OP_ID = "OP_NODE_MODIFY_STORAGE"
455 class OpRepairNodeStorage(OpCode):
456 """Repairs the volume group on a node."""
457 OP_ID = "OP_REPAIR_NODE_STORAGE"
458 OP_DSC_FIELD = "node_name"
463 "ignore_consistency",
467 class OpSetNodeParams(OpCode):
468 """Change the parameters of a node."""
469 OP_ID = "OP_NODE_SET_PARAMS"
470 OP_DSC_FIELD = "node_name"
485 class OpPowercycleNode(OpCode):
486 """Tries to powercycle a node."""
487 OP_ID = "OP_NODE_POWERCYCLE"
488 OP_DSC_FIELD = "node_name"
495 class OpMigrateNode(OpCode):
496 """Migrate all instances from a node."""
497 OP_ID = "OP_NODE_MIGRATE"
498 OP_DSC_FIELD = "node_name"
506 class OpNodeEvacuationStrategy(OpCode):
507 """Compute the evacuation strategy for a list of nodes."""
508 OP_ID = "OP_NODE_EVAC_STRATEGY"
509 OP_DSC_FIELD = "nodes"
510 __slots__ = ["nodes", "iallocator", "remote_node"]
515 class OpCreateInstance(OpCode):
516 """Create an instance.
518 @ivar instance_name: Instance name
519 @ivar mode: Instance creation mode (one of L{constants.INSTANCE_CREATE_MODES})
520 @ivar source_handshake: Signed handshake from source (remote import only)
521 @ivar source_x509_ca: Source X509 CA in PEM format (remote import only)
522 @ivar source_instance_name: Previous name of instance (remote import only)
523 @ivar source_shutdown_timeout: Shutdown timeout used for source instance
527 OP_ID = "OP_INSTANCE_CREATE"
528 OP_DSC_FIELD = "instance_name"
531 "os_type", "force_variant", "no_install",
532 "pnode", "disk_template", "snode", "mode",
534 "src_node", "src_path", "start", "identify_defaults",
535 "wait_for_sync", "ip_check", "name_check",
536 "file_storage_dir", "file_driver",
538 "hypervisor", "hvparams", "beparams", "osparams",
541 "source_instance_name",
542 "source_shutdown_timeout",
546 class OpReinstallInstance(OpCode):
547 """Reinstall an instance's OS."""
548 OP_ID = "OP_INSTANCE_REINSTALL"
549 OP_DSC_FIELD = "instance_name"
550 __slots__ = ["instance_name", "os_type", "force_variant", "osparams"]
553 class OpRemoveInstance(OpCode):
554 """Remove an instance."""
555 OP_ID = "OP_INSTANCE_REMOVE"
556 OP_DSC_FIELD = "instance_name"
564 class OpRenameInstance(OpCode):
565 """Rename an instance."""
566 OP_ID = "OP_INSTANCE_RENAME"
568 "instance_name", "ip_check", "new_name", "name_check",
572 class OpStartupInstance(OpCode):
573 """Startup an instance."""
574 OP_ID = "OP_INSTANCE_STARTUP"
575 OP_DSC_FIELD = "instance_name"
577 "instance_name", "force", "hvparams", "beparams", "ignore_offline_nodes",
581 class OpShutdownInstance(OpCode):
582 """Shutdown an instance."""
583 OP_ID = "OP_INSTANCE_SHUTDOWN"
584 OP_DSC_FIELD = "instance_name"
586 "instance_name", "timeout", "ignore_offline_nodes",
590 class OpRebootInstance(OpCode):
591 """Reboot an instance."""
592 OP_ID = "OP_INSTANCE_REBOOT"
593 OP_DSC_FIELD = "instance_name"
595 "instance_name", "reboot_type", "ignore_secondaries", "shutdown_timeout",
599 class OpReplaceDisks(OpCode):
600 """Replace the disks of an instance."""
601 OP_ID = "OP_INSTANCE_REPLACE_DISKS"
602 OP_DSC_FIELD = "instance_name"
604 "instance_name", "remote_node", "mode", "disks", "iallocator",
609 class OpFailoverInstance(OpCode):
610 """Failover an instance."""
611 OP_ID = "OP_INSTANCE_FAILOVER"
612 OP_DSC_FIELD = "instance_name"
614 "instance_name", "ignore_consistency", "shutdown_timeout",
618 class OpMigrateInstance(OpCode):
619 """Migrate an instance.
621 This migrates (without shutting down an instance) to its secondary
624 @ivar instance_name: the name of the instance
625 @ivar mode: the migration mode (live, non-live or None for auto)
628 OP_ID = "OP_INSTANCE_MIGRATE"
629 OP_DSC_FIELD = "instance_name"
630 __slots__ = ["instance_name", "mode", "cleanup", "live"]
633 class OpMoveInstance(OpCode):
636 This move (with shutting down an instance and data copying) to an
639 @ivar instance_name: the name of the instance
640 @ivar target_node: the destination node
643 OP_ID = "OP_INSTANCE_MOVE"
644 OP_DSC_FIELD = "instance_name"
646 "instance_name", "target_node", "shutdown_timeout",
650 class OpConnectConsole(OpCode):
651 """Connect to an instance's console."""
652 OP_ID = "OP_INSTANCE_CONSOLE"
653 OP_DSC_FIELD = "instance_name"
654 __slots__ = ["instance_name"]
657 class OpActivateInstanceDisks(OpCode):
658 """Activate an instance's disks."""
659 OP_ID = "OP_INSTANCE_ACTIVATE_DISKS"
660 OP_DSC_FIELD = "instance_name"
661 __slots__ = ["instance_name", "ignore_size"]
664 class OpDeactivateInstanceDisks(OpCode):
665 """Deactivate an instance's disks."""
666 OP_ID = "OP_INSTANCE_DEACTIVATE_DISKS"
667 OP_DSC_FIELD = "instance_name"
668 __slots__ = ["instance_name"]
671 class OpRecreateInstanceDisks(OpCode):
672 """Deactivate an instance's disks."""
673 OP_ID = "OP_INSTANCE_RECREATE_DISKS"
674 OP_DSC_FIELD = "instance_name"
675 __slots__ = ["instance_name", "disks"]
678 class OpQueryInstances(OpCode):
679 """Compute the list of instances."""
680 OP_ID = "OP_INSTANCE_QUERY"
681 __slots__ = ["output_fields", "names", "use_locking"]
684 class OpQueryInstanceData(OpCode):
685 """Compute the run-time status of instances."""
686 OP_ID = "OP_INSTANCE_QUERY_DATA"
687 __slots__ = ["instances", "static"]
690 class OpSetInstanceParams(OpCode):
691 """Change the parameters of an instance."""
692 OP_ID = "OP_INSTANCE_SET_PARAMS"
693 OP_DSC_FIELD = "instance_name"
696 "hvparams", "beparams", "osparams", "force",
697 "nics", "disks", "disk_template",
698 "remote_node", "os_name", "force_variant",
702 class OpGrowDisk(OpCode):
703 """Grow a disk of an instance."""
704 OP_ID = "OP_INSTANCE_GROW_DISK"
705 OP_DSC_FIELD = "instance_name"
707 "instance_name", "disk", "amount", "wait_for_sync",
713 class OpQueryGroups(OpCode):
714 """Compute the list of node groups."""
715 OP_ID = "OP_GROUP_QUERY"
716 __slots__ = ["output_fields", "names"]
720 class OpDiagnoseOS(OpCode):
721 """Compute the list of guest operating systems."""
722 OP_ID = "OP_OS_DIAGNOSE"
723 __slots__ = ["output_fields", "names"]
727 class OpQueryExports(OpCode):
728 """Compute the list of exported images."""
729 OP_ID = "OP_BACKUP_QUERY"
730 __slots__ = ["nodes", "use_locking"]
733 class OpPrepareExport(OpCode):
734 """Prepares an instance export.
736 @ivar instance_name: Instance name
737 @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
740 OP_ID = "OP_BACKUP_PREPARE"
741 OP_DSC_FIELD = "instance_name"
743 "instance_name", "mode",
747 class OpExportInstance(OpCode):
748 """Export an instance.
750 For local exports, the export destination is the node name. For remote
751 exports, the export destination is a list of tuples, each consisting of
752 hostname/IP address, port, HMAC and HMAC salt. The HMAC is calculated using
753 the cluster domain secret over the value "${index}:${hostname}:${port}". The
754 destination X509 CA must be a signed certificate.
756 @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
757 @ivar target_node: Export destination
758 @ivar x509_key_name: X509 key to use (remote export only)
759 @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
763 OP_ID = "OP_BACKUP_EXPORT"
764 OP_DSC_FIELD = "instance_name"
766 # TODO: Rename target_node as it changes meaning for different export modes
767 # (e.g. "destination")
768 "instance_name", "target_node", "shutdown", "shutdown_timeout",
770 "ignore_remove_failures",
773 "destination_x509_ca",
777 class OpRemoveExport(OpCode):
778 """Remove an instance's export."""
779 OP_ID = "OP_BACKUP_REMOVE"
780 OP_DSC_FIELD = "instance_name"
781 __slots__ = ["instance_name"]
785 class OpGetTags(OpCode):
786 """Returns the tags of the given object."""
787 OP_ID = "OP_TAGS_GET"
788 OP_DSC_FIELD = "name"
789 __slots__ = ["kind", "name"]
792 class OpSearchTags(OpCode):
793 """Searches the tags in the cluster for a given pattern."""
794 OP_ID = "OP_TAGS_SEARCH"
795 OP_DSC_FIELD = "pattern"
796 __slots__ = ["pattern"]
799 class OpAddTags(OpCode):
800 """Add a list of tags on a given object."""
801 OP_ID = "OP_TAGS_SET"
802 __slots__ = ["kind", "name", "tags"]
805 class OpDelTags(OpCode):
806 """Remove a list of tags from a given object."""
807 OP_ID = "OP_TAGS_DEL"
808 __slots__ = ["kind", "name", "tags"]
812 class OpTestDelay(OpCode):
813 """Sleeps for a configured amount of time.
815 This is used just for debugging and testing.
818 - duration: the time to sleep
819 - on_master: if true, sleep on the master
820 - on_nodes: list of nodes in which to sleep
822 If the on_master parameter is true, it will execute a sleep on the
823 master (before any node sleep).
825 If the on_nodes list is not empty, it will sleep on those nodes
826 (after the sleep on the master, if that is enabled).
828 As an additional feature, the case of duration < 0 will be reported
829 as an execution error, so this opcode can be used as a failure
830 generator. The case of duration == 0 will not be treated specially.
833 OP_ID = "OP_TEST_DELAY"
834 OP_DSC_FIELD = "duration"
835 __slots__ = ["duration", "on_master", "on_nodes", "repeat"]
838 class OpTestAllocator(OpCode):
839 """Allocator framework testing.
841 This opcode has two modes:
842 - gather and return allocator input for a given mode (allocate new
843 or replace secondary) and a given instance definition (direction
845 - run a selected allocator for a given operation (as above) and
846 return the allocator output (direction 'out')
849 OP_ID = "OP_TEST_ALLOCATOR"
850 OP_DSC_FIELD = "allocator"
852 "direction", "mode", "allocator", "name",
853 "mem_size", "disks", "disk_template",
854 "os", "tags", "nics", "vcpus", "hypervisor",
859 class OpTestJobqueue(OpCode):
860 """Utility opcode to test some aspects of the job queue.
863 OP_ID = "OP_TEST_JQUEUE"
872 class OpTestDummy(OpCode):
873 """Utility opcode used by unittests.
876 OP_ID = "OP_TEST_DUMMY"
884 OP_MAPPING = dict([(v.OP_ID, v) for v in globals().values()
885 if (isinstance(v, type) and issubclass(v, OpCode) and
886 hasattr(v, "OP_ID"))])