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.
56 if key not in self.__slots__:
57 raise TypeError("Object %s doesn't support the parameter '%s'" %
58 (self.__class__.__name__, key))
59 setattr(self, key, kwargs[key])
61 def __getstate__(self):
62 """Generic serializer.
64 This method just returns the contents of the instance as a
68 @return: the instance attributes and their values
72 for name in self.__slots__:
73 if hasattr(self, name):
74 state[name] = getattr(self, name)
77 def __setstate__(self, state):
78 """Generic unserializer.
80 This method just restores from the serialized state the attributes
81 of the current instance.
83 @param state: the serialized opcode data
87 if not isinstance(state, dict):
88 raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
91 for name in self.__slots__:
96 setattr(self, name, state[name])
99 class OpCode(BaseOpCode):
102 This is the root of the actual OpCode hierarchy. All clases derived
103 from this class should override OP_ID.
105 @cvar OP_ID: The ID of this opcode. This should be unique amongst all
106 children of this class.
107 @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
111 OP_ID = "OP_ABSTRACT"
112 __slots__ = BaseOpCode.__slots__ + ["dry_run"]
114 def __getstate__(self):
115 """Specialized getstate for opcodes.
117 This method adds to the state dictionary the OP_ID of the class,
118 so that on unload we can identify the correct class for
119 instantiating the opcode.
122 @return: the state as a dictionary
125 data = BaseOpCode.__getstate__(self)
126 data["OP_ID"] = self.OP_ID
130 def LoadOpCode(cls, data):
131 """Generic load opcode method.
133 The method identifies the correct opcode class from the dict-form
134 by looking for a OP_ID key, if this is not found, or its value is
135 not available in this module as a child of this class, we fail.
138 @param data: the serialized opcode
141 if not isinstance(data, dict):
142 raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
143 if "OP_ID" not in data:
144 raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
145 op_id = data["OP_ID"]
147 if op_id in OP_MAPPING:
148 op_class = OP_MAPPING[op_id]
150 raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
153 new_data = data.copy()
154 del new_data["OP_ID"]
155 op.__setstate__(new_data)
159 """Generates a summary description of this opcode.
162 # all OP_ID start with OP_, we remove that
164 field_name = getattr(self, "OP_DSC_FIELD", None)
166 field_value = getattr(self, field_name, None)
167 txt = "%s(%s)" % (txt, field_value)
173 class OpPostInitCluster(OpCode):
174 """Post cluster initialization.
176 This opcode does not touch the cluster at all. Its purpose is to run hooks
177 after the cluster has been initialized.
180 OP_ID = "OP_CLUSTER_POST_INIT"
181 __slots__ = OpCode.__slots__ + []
184 class OpDestroyCluster(OpCode):
185 """Destroy the cluster.
187 This opcode has no other parameters. All the state is irreversibly
188 lost after the execution of this opcode.
191 OP_ID = "OP_CLUSTER_DESTROY"
192 __slots__ = OpCode.__slots__ + []
195 class OpQueryClusterInfo(OpCode):
196 """Query cluster information."""
197 OP_ID = "OP_CLUSTER_QUERY"
198 __slots__ = OpCode.__slots__ + []
201 class OpVerifyCluster(OpCode):
202 """Verify the cluster state.
204 @type skip_checks: C{list}
205 @ivar skip_checks: steps to be skipped from the verify process; this
206 needs to be a subset of
207 L{constants.VERIFY_OPTIONAL_CHECKS}; currently
208 only L{constants.VERIFY_NPLUSONE_MEM} can be passed
211 OP_ID = "OP_CLUSTER_VERIFY"
212 __slots__ = OpCode.__slots__ + ["skip_checks", "verbose", "error_codes",
213 "debug_simulate_errors"]
216 class OpVerifyDisks(OpCode):
217 """Verify the cluster disks.
221 Result: a tuple of four elements:
222 - list of node names with bad data returned (unreachable, etc.)
223 - dict of node names with broken volume groups (values: error msg)
224 - list of instances with degraded disks (that should be activated)
225 - dict of instances with missing logical volumes (values: (node, vol)
226 pairs with details about the missing volumes)
228 In normal operation, all lists should be empty. A non-empty instance
229 list (3rd element of the result) is still ok (errors were fixed) but
230 non-empty node list means some node is down, and probably there are
231 unfixable drbd errors.
233 Note that only instances that are drbd-based are taken into
234 consideration. This might need to be revisited in the future.
237 OP_ID = "OP_CLUSTER_VERIFY_DISKS"
238 __slots__ = OpCode.__slots__ + []
241 class OpRepairDiskSizes(OpCode):
242 """Verify the disk sizes of the instances and fixes configuration
245 Parameters: optional instances list, in case we want to restrict the
246 checks to only a subset of the instances.
248 Result: a list of tuples, (instance, disk, new-size) for changed
251 In normal operation, the list should be empty.
253 @type instances: list
254 @ivar instances: the list of instances to check, or empty for all instances
257 OP_ID = "OP_CLUSTER_REPAIR_DISK_SIZES"
258 __slots__ = ["instances"]
261 class OpQueryConfigValues(OpCode):
262 """Query cluster configuration values."""
263 OP_ID = "OP_CLUSTER_CONFIG_QUERY"
264 __slots__ = OpCode.__slots__ + ["output_fields"]
267 class OpRenameCluster(OpCode):
268 """Rename the cluster.
271 @ivar name: The new name of the cluster. The name and/or the master IP
272 address will be changed to match the new name and its IP
276 OP_ID = "OP_CLUSTER_RENAME"
277 OP_DSC_FIELD = "name"
278 __slots__ = OpCode.__slots__ + ["name"]
281 class OpSetClusterParams(OpCode):
282 """Change the parameters of the cluster.
284 @type vg_name: C{str} or C{None}
285 @ivar vg_name: The new volume group name or None to disable LVM usage.
288 OP_ID = "OP_CLUSTER_SET_PARAMS"
289 __slots__ = OpCode.__slots__ + [
291 "enabled_hypervisors",
295 "candidate_pool_size",
299 class OpRedistributeConfig(OpCode):
300 """Force a full push of the cluster configuration.
303 OP_ID = "OP_CLUSTER_REDIST_CONF"
304 __slots__ = OpCode.__slots__ + [
309 class OpRemoveNode(OpCode):
312 @type node_name: C{str}
313 @ivar node_name: The name of the node to remove. If the node still has
314 instances on it, the operation will fail.
317 OP_ID = "OP_NODE_REMOVE"
318 OP_DSC_FIELD = "node_name"
319 __slots__ = OpCode.__slots__ + ["node_name"]
322 class OpAddNode(OpCode):
323 """Add a node to the cluster.
325 @type node_name: C{str}
326 @ivar node_name: The name of the node to add. This can be a short name,
327 but it will be expanded to the FQDN.
328 @type primary_ip: IP address
329 @ivar primary_ip: The primary IP of the node. This will be ignored when the
330 opcode is submitted, but will be filled during the node
331 add (so it will be visible in the job query).
332 @type secondary_ip: IP address
333 @ivar secondary_ip: The secondary IP of the node. This needs to be passed
334 if the cluster has been initialized in 'dual-network'
335 mode, otherwise it must not be given.
337 @ivar readd: Whether to re-add an existing node to the cluster. If
338 this is not passed, then the operation will abort if the node
339 name is already in the cluster; use this parameter to 'repair'
340 a node that had its configuration broken, or was reinstalled
341 without removal from the cluster.
344 OP_ID = "OP_NODE_ADD"
345 OP_DSC_FIELD = "node_name"
346 __slots__ = OpCode.__slots__ + [
347 "node_name", "primary_ip", "secondary_ip", "readd",
351 class OpQueryNodes(OpCode):
352 """Compute the list of nodes."""
353 OP_ID = "OP_NODE_QUERY"
354 __slots__ = OpCode.__slots__ + ["output_fields", "names", "use_locking"]
357 class OpQueryNodeVolumes(OpCode):
358 """Get list of volumes on node."""
359 OP_ID = "OP_NODE_QUERYVOLS"
360 __slots__ = OpCode.__slots__ + ["nodes", "output_fields"]
363 class OpQueryNodeStorage(OpCode):
364 """Get information on storage for node(s)."""
365 OP_ID = "OP_NODE_QUERY_STORAGE"
366 __slots__ = OpCode.__slots__ + [
374 class OpModifyNodeStorage(OpCode):
375 """Modifies the properies of a storage unit"""
376 OP_ID = "OP_NODE_MODIFY_STORAGE"
377 __slots__ = OpCode.__slots__ + [
385 class OpRepairNodeStorage(OpCode):
386 """Repairs the volume group on a node."""
387 OP_ID = "OP_REPAIR_NODE_STORAGE"
388 OP_DSC_FIELD = "node_name"
389 __slots__ = OpCode.__slots__ + [
393 "ignore_consistency",
397 class OpSetNodeParams(OpCode):
398 """Change the parameters of a node."""
399 OP_ID = "OP_NODE_SET_PARAMS"
400 OP_DSC_FIELD = "node_name"
401 __slots__ = OpCode.__slots__ + [
410 class OpPowercycleNode(OpCode):
411 """Tries to powercycle a node."""
412 OP_ID = "OP_NODE_POWERCYCLE"
413 OP_DSC_FIELD = "node_name"
414 __slots__ = OpCode.__slots__ + [
420 class OpEvacuateNode(OpCode):
421 """Relocate secondary instances from a node."""
422 OP_ID = "OP_NODE_EVACUATE"
423 OP_DSC_FIELD = "node_name"
424 __slots__ = OpCode.__slots__ + [
425 "node_name", "remote_node", "iallocator",
429 class OpMigrateNode(OpCode):
430 """Migrate all instances from a node."""
431 OP_ID = "OP_NODE_MIGRATE"
432 OP_DSC_FIELD = "node_name"
433 __slots__ = OpCode.__slots__ + [
441 class OpCreateInstance(OpCode):
442 """Create an instance."""
443 OP_ID = "OP_INSTANCE_CREATE"
444 OP_DSC_FIELD = "instance_name"
445 __slots__ = OpCode.__slots__ + [
446 "instance_name", "os_type", "force_variant",
447 "pnode", "disk_template", "snode", "mode",
449 "src_node", "src_path", "start",
450 "wait_for_sync", "ip_check",
451 "file_storage_dir", "file_driver",
453 "hypervisor", "hvparams", "beparams",
458 class OpReinstallInstance(OpCode):
459 """Reinstall an instance's OS."""
460 OP_ID = "OP_INSTANCE_REINSTALL"
461 OP_DSC_FIELD = "instance_name"
462 __slots__ = OpCode.__slots__ + ["instance_name", "os_type", "force_variant"]
465 class OpRemoveInstance(OpCode):
466 """Remove an instance."""
467 OP_ID = "OP_INSTANCE_REMOVE"
468 OP_DSC_FIELD = "instance_name"
469 __slots__ = OpCode.__slots__ + [
476 class OpRenameInstance(OpCode):
477 """Rename an instance."""
478 OP_ID = "OP_INSTANCE_RENAME"
479 __slots__ = OpCode.__slots__ + [
480 "instance_name", "ignore_ip", "new_name",
484 class OpStartupInstance(OpCode):
485 """Startup an instance."""
486 OP_ID = "OP_INSTANCE_STARTUP"
487 OP_DSC_FIELD = "instance_name"
488 __slots__ = OpCode.__slots__ + [
489 "instance_name", "force", "hvparams", "beparams",
493 class OpShutdownInstance(OpCode):
494 """Shutdown an instance."""
495 OP_ID = "OP_INSTANCE_SHUTDOWN"
496 OP_DSC_FIELD = "instance_name"
497 __slots__ = OpCode.__slots__ + ["instance_name", "timeout"]
500 class OpRebootInstance(OpCode):
501 """Reboot an instance."""
502 OP_ID = "OP_INSTANCE_REBOOT"
503 OP_DSC_FIELD = "instance_name"
504 __slots__ = OpCode.__slots__ + [
505 "instance_name", "reboot_type", "ignore_secondaries", "shutdown_timeout",
509 class OpReplaceDisks(OpCode):
510 """Replace the disks of an instance."""
511 OP_ID = "OP_INSTANCE_REPLACE_DISKS"
512 OP_DSC_FIELD = "instance_name"
513 __slots__ = OpCode.__slots__ + [
514 "instance_name", "remote_node", "mode", "disks", "iallocator",
518 class OpFailoverInstance(OpCode):
519 """Failover an instance."""
520 OP_ID = "OP_INSTANCE_FAILOVER"
521 OP_DSC_FIELD = "instance_name"
522 __slots__ = OpCode.__slots__ + [
523 "instance_name", "ignore_consistency", "shutdown_timeout",
527 class OpMigrateInstance(OpCode):
528 """Migrate an instance.
530 This migrates (without shutting down an instance) to its secondary
533 @ivar instance_name: the name of the instance
536 OP_ID = "OP_INSTANCE_MIGRATE"
537 OP_DSC_FIELD = "instance_name"
538 __slots__ = OpCode.__slots__ + ["instance_name", "live", "cleanup"]
541 class OpMoveInstance(OpCode):
544 This move (with shutting down an instance and data copying) to an
547 @ivar instance_name: the name of the instance
548 @ivar target_node: the destination node
551 OP_ID = "OP_INSTANCE_MOVE"
552 OP_DSC_FIELD = "instance_name"
553 __slots__ = OpCode.__slots__ + [
554 "instance_name", "target_node", "shutdown_timeout",
558 class OpConnectConsole(OpCode):
559 """Connect to an instance's console."""
560 OP_ID = "OP_INSTANCE_CONSOLE"
561 OP_DSC_FIELD = "instance_name"
562 __slots__ = OpCode.__slots__ + ["instance_name"]
565 class OpActivateInstanceDisks(OpCode):
566 """Activate an instance's disks."""
567 OP_ID = "OP_INSTANCE_ACTIVATE_DISKS"
568 OP_DSC_FIELD = "instance_name"
569 __slots__ = OpCode.__slots__ + ["instance_name", "ignore_size"]
572 class OpDeactivateInstanceDisks(OpCode):
573 """Deactivate an instance's disks."""
574 OP_ID = "OP_INSTANCE_DEACTIVATE_DISKS"
575 OP_DSC_FIELD = "instance_name"
576 __slots__ = OpCode.__slots__ + ["instance_name"]
579 class OpRecreateInstanceDisks(OpCode):
580 """Deactivate an instance's disks."""
581 OP_ID = "OP_INSTANCE_RECREATE_DISKS"
582 OP_DSC_FIELD = "instance_name"
583 __slots__ = OpCode.__slots__ + ["instance_name", "disks"]
586 class OpQueryInstances(OpCode):
587 """Compute the list of instances."""
588 OP_ID = "OP_INSTANCE_QUERY"
589 __slots__ = OpCode.__slots__ + ["output_fields", "names", "use_locking"]
592 class OpQueryInstanceData(OpCode):
593 """Compute the run-time status of instances."""
594 OP_ID = "OP_INSTANCE_QUERY_DATA"
595 __slots__ = OpCode.__slots__ + ["instances", "static"]
598 class OpSetInstanceParams(OpCode):
599 """Change the parameters of an instance."""
600 OP_ID = "OP_INSTANCE_SET_PARAMS"
601 OP_DSC_FIELD = "instance_name"
602 __slots__ = OpCode.__slots__ + [
604 "hvparams", "beparams", "force",
609 class OpGrowDisk(OpCode):
610 """Grow a disk of an instance."""
611 OP_ID = "OP_INSTANCE_GROW_DISK"
612 OP_DSC_FIELD = "instance_name"
613 __slots__ = OpCode.__slots__ + [
614 "instance_name", "disk", "amount", "wait_for_sync",
619 class OpDiagnoseOS(OpCode):
620 """Compute the list of guest operating systems."""
621 OP_ID = "OP_OS_DIAGNOSE"
622 __slots__ = OpCode.__slots__ + ["output_fields", "names"]
626 class OpQueryExports(OpCode):
627 """Compute the list of exported images."""
628 OP_ID = "OP_BACKUP_QUERY"
629 __slots__ = OpCode.__slots__ + ["nodes", "use_locking"]
632 class OpExportInstance(OpCode):
633 """Export an instance."""
634 OP_ID = "OP_BACKUP_EXPORT"
635 OP_DSC_FIELD = "instance_name"
636 __slots__ = OpCode.__slots__ + [
637 "instance_name", "target_node", "shutdown", "shutdown_timeout",
641 class OpRemoveExport(OpCode):
642 """Remove an instance's export."""
643 OP_ID = "OP_BACKUP_REMOVE"
644 OP_DSC_FIELD = "instance_name"
645 __slots__ = OpCode.__slots__ + ["instance_name"]
649 class OpGetTags(OpCode):
650 """Returns the tags of the given object."""
651 OP_ID = "OP_TAGS_GET"
652 OP_DSC_FIELD = "name"
653 __slots__ = OpCode.__slots__ + ["kind", "name"]
656 class OpSearchTags(OpCode):
657 """Searches the tags in the cluster for a given pattern."""
658 OP_ID = "OP_TAGS_SEARCH"
659 OP_DSC_FIELD = "pattern"
660 __slots__ = OpCode.__slots__ + ["pattern"]
663 class OpAddTags(OpCode):
664 """Add a list of tags on a given object."""
665 OP_ID = "OP_TAGS_SET"
666 __slots__ = OpCode.__slots__ + ["kind", "name", "tags"]
669 class OpDelTags(OpCode):
670 """Remove a list of tags from a given object."""
671 OP_ID = "OP_TAGS_DEL"
672 __slots__ = OpCode.__slots__ + ["kind", "name", "tags"]
676 class OpTestDelay(OpCode):
677 """Sleeps for a configured amount of time.
679 This is used just for debugging and testing.
682 - duration: the time to sleep
683 - on_master: if true, sleep on the master
684 - on_nodes: list of nodes in which to sleep
686 If the on_master parameter is true, it will execute a sleep on the
687 master (before any node sleep).
689 If the on_nodes list is not empty, it will sleep on those nodes
690 (after the sleep on the master, if that is enabled).
692 As an additional feature, the case of duration < 0 will be reported
693 as an execution error, so this opcode can be used as a failure
694 generator. The case of duration == 0 will not be treated specially.
697 OP_ID = "OP_TEST_DELAY"
698 OP_DSC_FIELD = "duration"
699 __slots__ = OpCode.__slots__ + ["duration", "on_master", "on_nodes"]
702 class OpTestAllocator(OpCode):
703 """Allocator framework testing.
705 This opcode has two modes:
706 - gather and return allocator input for a given mode (allocate new
707 or replace secondary) and a given instance definition (direction
709 - run a selected allocator for a given operation (as above) and
710 return the allocator output (direction 'out')
713 OP_ID = "OP_TEST_ALLOCATOR"
714 OP_DSC_FIELD = "allocator"
715 __slots__ = OpCode.__slots__ + [
716 "direction", "mode", "allocator", "name",
717 "mem_size", "disks", "disk_template",
718 "os", "tags", "nics", "vcpus", "hypervisor",
722 OP_MAPPING = dict([(v.OP_ID, v) for v in globals().values()
723 if (isinstance(v, type) and issubclass(v, OpCode) and
724 hasattr(v, "OP_ID"))])