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
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():
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
122 OP_ID = "OP_ABSTRACT"
123 __slots__ = ["dry_run", "debug_level"]
125 def __getstate__(self):
126 """Specialized getstate for opcodes.
128 This method adds to the state dictionary the OP_ID of the class,
129 so that on unload we can identify the correct class for
130 instantiating the opcode.
133 @return: the state as a dictionary
136 data = BaseOpCode.__getstate__(self)
137 data["OP_ID"] = self.OP_ID
141 def LoadOpCode(cls, data):
142 """Generic load opcode method.
144 The method identifies the correct opcode class from the dict-form
145 by looking for a OP_ID key, if this is not found, or its value is
146 not available in this module as a child of this class, we fail.
149 @param data: the serialized opcode
152 if not isinstance(data, dict):
153 raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
154 if "OP_ID" not in data:
155 raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
156 op_id = data["OP_ID"]
158 if op_id in OP_MAPPING:
159 op_class = OP_MAPPING[op_id]
161 raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
164 new_data = data.copy()
165 del new_data["OP_ID"]
166 op.__setstate__(new_data)
170 """Generates a summary description of this opcode.
173 # all OP_ID start with OP_, we remove that
175 field_name = getattr(self, "OP_DSC_FIELD", None)
177 field_value = getattr(self, field_name, None)
178 txt = "%s(%s)" % (txt, field_value)
184 class OpPostInitCluster(OpCode):
185 """Post cluster initialization.
187 This opcode does not touch the cluster at all. Its purpose is to run hooks
188 after the cluster has been initialized.
191 OP_ID = "OP_CLUSTER_POST_INIT"
195 class OpDestroyCluster(OpCode):
196 """Destroy the cluster.
198 This opcode has no other parameters. All the state is irreversibly
199 lost after the execution of this opcode.
202 OP_ID = "OP_CLUSTER_DESTROY"
206 class OpQueryClusterInfo(OpCode):
207 """Query cluster information."""
208 OP_ID = "OP_CLUSTER_QUERY"
212 class OpVerifyCluster(OpCode):
213 """Verify the cluster state.
215 @type skip_checks: C{list}
216 @ivar skip_checks: steps to be skipped from the verify process; this
217 needs to be a subset of
218 L{constants.VERIFY_OPTIONAL_CHECKS}; currently
219 only L{constants.VERIFY_NPLUSONE_MEM} can be passed
222 OP_ID = "OP_CLUSTER_VERIFY"
223 __slots__ = ["skip_checks", "verbose", "error_codes",
224 "debug_simulate_errors"]
227 class OpVerifyDisks(OpCode):
228 """Verify the cluster disks.
232 Result: a tuple of four elements:
233 - list of node names with bad data returned (unreachable, etc.)
234 - dict of node names with broken volume groups (values: error msg)
235 - list of instances with degraded disks (that should be activated)
236 - dict of instances with missing logical volumes (values: (node, vol)
237 pairs with details about the missing volumes)
239 In normal operation, all lists should be empty. A non-empty instance
240 list (3rd element of the result) is still ok (errors were fixed) but
241 non-empty node list means some node is down, and probably there are
242 unfixable drbd errors.
244 Note that only instances that are drbd-based are taken into
245 consideration. This might need to be revisited in the future.
248 OP_ID = "OP_CLUSTER_VERIFY_DISKS"
252 class OpRepairDiskSizes(OpCode):
253 """Verify the disk sizes of the instances and fixes configuration
256 Parameters: optional instances list, in case we want to restrict the
257 checks to only a subset of the instances.
259 Result: a list of tuples, (instance, disk, new-size) for changed
262 In normal operation, the list should be empty.
264 @type instances: list
265 @ivar instances: the list of instances to check, or empty for all instances
268 OP_ID = "OP_CLUSTER_REPAIR_DISK_SIZES"
269 __slots__ = ["instances"]
272 class OpQueryConfigValues(OpCode):
273 """Query cluster configuration values."""
274 OP_ID = "OP_CLUSTER_CONFIG_QUERY"
275 __slots__ = ["output_fields"]
278 class OpRenameCluster(OpCode):
279 """Rename the cluster.
282 @ivar name: The new name of the cluster. The name and/or the master IP
283 address will be changed to match the new name and its IP
287 OP_ID = "OP_CLUSTER_RENAME"
288 OP_DSC_FIELD = "name"
292 class OpSetClusterParams(OpCode):
293 """Change the parameters of the cluster.
295 @type vg_name: C{str} or C{None}
296 @ivar vg_name: The new volume group name or None to disable LVM usage.
299 OP_ID = "OP_CLUSTER_SET_PARAMS"
302 "enabled_hypervisors",
307 "candidate_pool_size",
311 class OpRedistributeConfig(OpCode):
312 """Force a full push of the cluster configuration.
315 OP_ID = "OP_CLUSTER_REDIST_CONF"
320 class OpRemoveNode(OpCode):
323 @type node_name: C{str}
324 @ivar node_name: The name of the node to remove. If the node still has
325 instances on it, the operation will fail.
328 OP_ID = "OP_NODE_REMOVE"
329 OP_DSC_FIELD = "node_name"
330 __slots__ = ["node_name"]
333 class OpAddNode(OpCode):
334 """Add a node to the cluster.
336 @type node_name: C{str}
337 @ivar node_name: The name of the node to add. This can be a short name,
338 but it will be expanded to the FQDN.
339 @type primary_ip: IP address
340 @ivar primary_ip: The primary IP of the node. This will be ignored when the
341 opcode is submitted, but will be filled during the node
342 add (so it will be visible in the job query).
343 @type secondary_ip: IP address
344 @ivar secondary_ip: The secondary IP of the node. This needs to be passed
345 if the cluster has been initialized in 'dual-network'
346 mode, otherwise it must not be given.
348 @ivar readd: Whether to re-add an existing node to the cluster. If
349 this is not passed, then the operation will abort if the node
350 name is already in the cluster; use this parameter to 'repair'
351 a node that had its configuration broken, or was reinstalled
352 without removal from the cluster.
355 OP_ID = "OP_NODE_ADD"
356 OP_DSC_FIELD = "node_name"
357 __slots__ = ["node_name", "primary_ip", "secondary_ip", "readd"]
360 class OpQueryNodes(OpCode):
361 """Compute the list of nodes."""
362 OP_ID = "OP_NODE_QUERY"
363 __slots__ = ["output_fields", "names", "use_locking"]
366 class OpQueryNodeVolumes(OpCode):
367 """Get list of volumes on node."""
368 OP_ID = "OP_NODE_QUERYVOLS"
369 __slots__ = ["nodes", "output_fields"]
372 class OpQueryNodeStorage(OpCode):
373 """Get information on storage for node(s)."""
374 OP_ID = "OP_NODE_QUERY_STORAGE"
383 class OpModifyNodeStorage(OpCode):
384 """Modifies the properies of a storage unit"""
385 OP_ID = "OP_NODE_MODIFY_STORAGE"
394 class OpRepairNodeStorage(OpCode):
395 """Repairs the volume group on a node."""
396 OP_ID = "OP_REPAIR_NODE_STORAGE"
397 OP_DSC_FIELD = "node_name"
402 "ignore_consistency",
406 class OpSetNodeParams(OpCode):
407 """Change the parameters of a node."""
408 OP_ID = "OP_NODE_SET_PARAMS"
409 OP_DSC_FIELD = "node_name"
420 class OpPowercycleNode(OpCode):
421 """Tries to powercycle a node."""
422 OP_ID = "OP_NODE_POWERCYCLE"
423 OP_DSC_FIELD = "node_name"
430 class OpEvacuateNode(OpCode):
431 """Relocate secondary instances from a node."""
432 OP_ID = "OP_NODE_EVACUATE"
433 OP_DSC_FIELD = "node_name"
435 "node_name", "remote_node", "iallocator", "early_release",
439 class OpMigrateNode(OpCode):
440 """Migrate all instances from a node."""
441 OP_ID = "OP_NODE_MIGRATE"
442 OP_DSC_FIELD = "node_name"
449 class OpNodeEvacuationStrategy(OpCode):
450 """Compute the evacuation strategy for a list of nodes."""
451 OP_ID = "OP_NODE_EVAC_STRATEGY"
452 OP_DSC_FIELD = "nodes"
453 __slots__ = ["nodes", "iallocator", "remote_node"]
458 class OpCreateInstance(OpCode):
459 """Create an instance."""
460 OP_ID = "OP_INSTANCE_CREATE"
461 OP_DSC_FIELD = "instance_name"
463 "instance_name", "os_type", "force_variant",
464 "pnode", "disk_template", "snode", "mode",
466 "src_node", "src_path", "start",
467 "wait_for_sync", "ip_check", "name_check",
468 "file_storage_dir", "file_driver",
470 "hypervisor", "hvparams", "beparams",
475 class OpReinstallInstance(OpCode):
476 """Reinstall an instance's OS."""
477 OP_ID = "OP_INSTANCE_REINSTALL"
478 OP_DSC_FIELD = "instance_name"
479 __slots__ = ["instance_name", "os_type", "force_variant"]
482 class OpRemoveInstance(OpCode):
483 """Remove an instance."""
484 OP_ID = "OP_INSTANCE_REMOVE"
485 OP_DSC_FIELD = "instance_name"
493 class OpRenameInstance(OpCode):
494 """Rename an instance."""
495 OP_ID = "OP_INSTANCE_RENAME"
497 "instance_name", "ignore_ip", "new_name",
501 class OpStartupInstance(OpCode):
502 """Startup an instance."""
503 OP_ID = "OP_INSTANCE_STARTUP"
504 OP_DSC_FIELD = "instance_name"
506 "instance_name", "force", "hvparams", "beparams",
510 class OpShutdownInstance(OpCode):
511 """Shutdown an instance."""
512 OP_ID = "OP_INSTANCE_SHUTDOWN"
513 OP_DSC_FIELD = "instance_name"
514 __slots__ = ["instance_name", "timeout"]
517 class OpRebootInstance(OpCode):
518 """Reboot an instance."""
519 OP_ID = "OP_INSTANCE_REBOOT"
520 OP_DSC_FIELD = "instance_name"
522 "instance_name", "reboot_type", "ignore_secondaries", "shutdown_timeout",
526 class OpReplaceDisks(OpCode):
527 """Replace the disks of an instance."""
528 OP_ID = "OP_INSTANCE_REPLACE_DISKS"
529 OP_DSC_FIELD = "instance_name"
531 "instance_name", "remote_node", "mode", "disks", "iallocator",
536 class OpFailoverInstance(OpCode):
537 """Failover an instance."""
538 OP_ID = "OP_INSTANCE_FAILOVER"
539 OP_DSC_FIELD = "instance_name"
541 "instance_name", "ignore_consistency", "shutdown_timeout",
545 class OpMigrateInstance(OpCode):
546 """Migrate an instance.
548 This migrates (without shutting down an instance) to its secondary
551 @ivar instance_name: the name of the instance
554 OP_ID = "OP_INSTANCE_MIGRATE"
555 OP_DSC_FIELD = "instance_name"
556 __slots__ = ["instance_name", "live", "cleanup"]
559 class OpMoveInstance(OpCode):
562 This move (with shutting down an instance and data copying) to an
565 @ivar instance_name: the name of the instance
566 @ivar target_node: the destination node
569 OP_ID = "OP_INSTANCE_MOVE"
570 OP_DSC_FIELD = "instance_name"
572 "instance_name", "target_node", "shutdown_timeout",
576 class OpConnectConsole(OpCode):
577 """Connect to an instance's console."""
578 OP_ID = "OP_INSTANCE_CONSOLE"
579 OP_DSC_FIELD = "instance_name"
580 __slots__ = ["instance_name"]
583 class OpActivateInstanceDisks(OpCode):
584 """Activate an instance's disks."""
585 OP_ID = "OP_INSTANCE_ACTIVATE_DISKS"
586 OP_DSC_FIELD = "instance_name"
587 __slots__ = ["instance_name", "ignore_size"]
590 class OpDeactivateInstanceDisks(OpCode):
591 """Deactivate an instance's disks."""
592 OP_ID = "OP_INSTANCE_DEACTIVATE_DISKS"
593 OP_DSC_FIELD = "instance_name"
594 __slots__ = ["instance_name"]
597 class OpRecreateInstanceDisks(OpCode):
598 """Deactivate an instance's disks."""
599 OP_ID = "OP_INSTANCE_RECREATE_DISKS"
600 OP_DSC_FIELD = "instance_name"
601 __slots__ = ["instance_name", "disks"]
604 class OpQueryInstances(OpCode):
605 """Compute the list of instances."""
606 OP_ID = "OP_INSTANCE_QUERY"
607 __slots__ = ["output_fields", "names", "use_locking"]
610 class OpQueryInstanceData(OpCode):
611 """Compute the run-time status of instances."""
612 OP_ID = "OP_INSTANCE_QUERY_DATA"
613 __slots__ = ["instances", "static"]
616 class OpSetInstanceParams(OpCode):
617 """Change the parameters of an instance."""
618 OP_ID = "OP_INSTANCE_SET_PARAMS"
619 OP_DSC_FIELD = "instance_name"
622 "hvparams", "beparams", "force",
627 class OpGrowDisk(OpCode):
628 """Grow a disk of an instance."""
629 OP_ID = "OP_INSTANCE_GROW_DISK"
630 OP_DSC_FIELD = "instance_name"
632 "instance_name", "disk", "amount", "wait_for_sync",
637 class OpDiagnoseOS(OpCode):
638 """Compute the list of guest operating systems."""
639 OP_ID = "OP_OS_DIAGNOSE"
640 __slots__ = ["output_fields", "names"]
644 class OpQueryExports(OpCode):
645 """Compute the list of exported images."""
646 OP_ID = "OP_BACKUP_QUERY"
647 __slots__ = ["nodes", "use_locking"]
650 class OpExportInstance(OpCode):
651 """Export an instance."""
652 OP_ID = "OP_BACKUP_EXPORT"
653 OP_DSC_FIELD = "instance_name"
655 "instance_name", "target_node", "shutdown", "shutdown_timeout",
659 class OpRemoveExport(OpCode):
660 """Remove an instance's export."""
661 OP_ID = "OP_BACKUP_REMOVE"
662 OP_DSC_FIELD = "instance_name"
663 __slots__ = ["instance_name"]
667 class OpGetTags(OpCode):
668 """Returns the tags of the given object."""
669 OP_ID = "OP_TAGS_GET"
670 OP_DSC_FIELD = "name"
671 __slots__ = ["kind", "name"]
674 class OpSearchTags(OpCode):
675 """Searches the tags in the cluster for a given pattern."""
676 OP_ID = "OP_TAGS_SEARCH"
677 OP_DSC_FIELD = "pattern"
678 __slots__ = ["pattern"]
681 class OpAddTags(OpCode):
682 """Add a list of tags on a given object."""
683 OP_ID = "OP_TAGS_SET"
684 __slots__ = ["kind", "name", "tags"]
687 class OpDelTags(OpCode):
688 """Remove a list of tags from a given object."""
689 OP_ID = "OP_TAGS_DEL"
690 __slots__ = ["kind", "name", "tags"]
694 class OpTestDelay(OpCode):
695 """Sleeps for a configured amount of time.
697 This is used just for debugging and testing.
700 - duration: the time to sleep
701 - on_master: if true, sleep on the master
702 - on_nodes: list of nodes in which to sleep
704 If the on_master parameter is true, it will execute a sleep on the
705 master (before any node sleep).
707 If the on_nodes list is not empty, it will sleep on those nodes
708 (after the sleep on the master, if that is enabled).
710 As an additional feature, the case of duration < 0 will be reported
711 as an execution error, so this opcode can be used as a failure
712 generator. The case of duration == 0 will not be treated specially.
715 OP_ID = "OP_TEST_DELAY"
716 OP_DSC_FIELD = "duration"
717 __slots__ = ["duration", "on_master", "on_nodes"]
720 class OpTestAllocator(OpCode):
721 """Allocator framework testing.
723 This opcode has two modes:
724 - gather and return allocator input for a given mode (allocate new
725 or replace secondary) and a given instance definition (direction
727 - run a selected allocator for a given operation (as above) and
728 return the allocator output (direction 'out')
731 OP_ID = "OP_TEST_ALLOCATOR"
732 OP_DSC_FIELD = "allocator"
734 "direction", "mode", "allocator", "name",
735 "mem_size", "disks", "disk_template",
736 "os", "tags", "nics", "vcpus", "hypervisor",
741 OP_MAPPING = dict([(v.OP_ID, v) for v in globals().values()
742 if (isinstance(v, type) and issubclass(v, OpCode) and
743 hasattr(v, "OP_ID"))])