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"]
215 class OpVerifyDisks(OpCode):
216 """Verify the cluster disks.
220 Result: a tuple of four elements:
221 - list of node names with bad data returned (unreachable, etc.)
222 - dict of node names with broken volume groups (values: error msg)
223 - list of instances with degraded disks (that should be activated)
224 - dict of instances with missing logical volumes (values: (node, vol)
225 pairs with details about the missing volumes)
227 In normal operation, all lists should be empty. A non-empty instance
228 list (3rd element of the result) is still ok (errors were fixed) but
229 non-empty node list means some node is down, and probably there are
230 unfixable drbd errors.
232 Note that only instances that are drbd-based are taken into
233 consideration. This might need to be revisited in the future.
236 OP_ID = "OP_CLUSTER_VERIFY_DISKS"
237 __slots__ = OpCode.__slots__ + []
240 class OpRepairDiskSizes(OpCode):
241 """Verify the disk sizes of the instances and fixes configuration
244 Parameters: optional instances list, in case we want to restrict the
245 checks to only a subset of the instances.
247 Result: a list of tuples, (instance, disk, new-size) for changed
250 In normal operation, the list should be empty.
252 @type instances: list
253 @ivar instances: the list of instances to check, or empty for all instances
256 OP_ID = "OP_CLUSTER_REPAIR_DISK_SIZES"
257 __slots__ = ["instances"]
260 class OpQueryConfigValues(OpCode):
261 """Query cluster configuration values."""
262 OP_ID = "OP_CLUSTER_CONFIG_QUERY"
263 __slots__ = OpCode.__slots__ + ["output_fields"]
266 class OpRenameCluster(OpCode):
267 """Rename the cluster.
270 @ivar name: The new name of the cluster. The name and/or the master IP
271 address will be changed to match the new name and its IP
275 OP_ID = "OP_CLUSTER_RENAME"
276 OP_DSC_FIELD = "name"
277 __slots__ = OpCode.__slots__ + ["name"]
280 class OpSetClusterParams(OpCode):
281 """Change the parameters of the cluster.
283 @type vg_name: C{str} or C{None}
284 @ivar vg_name: The new volume group name or None to disable LVM usage.
287 OP_ID = "OP_CLUSTER_SET_PARAMS"
288 __slots__ = OpCode.__slots__ + [
290 "enabled_hypervisors",
294 "candidate_pool_size",
298 class OpRedistributeConfig(OpCode):
299 """Force a full push of the cluster configuration.
302 OP_ID = "OP_CLUSTER_REDIST_CONF"
303 __slots__ = OpCode.__slots__ + [
308 class OpRemoveNode(OpCode):
311 @type node_name: C{str}
312 @ivar node_name: The name of the node to remove. If the node still has
313 instances on it, the operation will fail.
316 OP_ID = "OP_NODE_REMOVE"
317 OP_DSC_FIELD = "node_name"
318 __slots__ = OpCode.__slots__ + ["node_name"]
321 class OpAddNode(OpCode):
322 """Add a node to the cluster.
324 @type node_name: C{str}
325 @ivar node_name: The name of the node to add. This can be a short name,
326 but it will be expanded to the FQDN.
327 @type primary_ip: IP address
328 @ivar primary_ip: The primary IP of the node. This will be ignored when the
329 opcode is submitted, but will be filled during the node
330 add (so it will be visible in the job query).
331 @type secondary_ip: IP address
332 @ivar secondary_ip: The secondary IP of the node. This needs to be passed
333 if the cluster has been initialized in 'dual-network'
334 mode, otherwise it must not be given.
336 @ivar readd: Whether to re-add an existing node to the cluster. If
337 this is not passed, then the operation will abort if the node
338 name is already in the cluster; use this parameter to 'repair'
339 a node that had its configuration broken, or was reinstalled
340 without removal from the cluster.
343 OP_ID = "OP_NODE_ADD"
344 OP_DSC_FIELD = "node_name"
345 __slots__ = OpCode.__slots__ + [
346 "node_name", "primary_ip", "secondary_ip", "readd",
350 class OpQueryNodes(OpCode):
351 """Compute the list of nodes."""
352 OP_ID = "OP_NODE_QUERY"
353 __slots__ = OpCode.__slots__ + ["output_fields", "names", "use_locking"]
356 class OpQueryNodeVolumes(OpCode):
357 """Get list of volumes on node."""
358 OP_ID = "OP_NODE_QUERYVOLS"
359 __slots__ = OpCode.__slots__ + ["nodes", "output_fields"]
362 class OpQueryNodeStorage(OpCode):
363 """Get information on storage for node(s)."""
364 OP_ID = "OP_NODE_QUERY_STORAGE"
365 __slots__ = OpCode.__slots__ + [
373 class OpModifyNodeStorage(OpCode):
375 OP_ID = "OP_NODE_MODIFY_STORAGE"
376 __slots__ = OpCode.__slots__ + [
384 class OpRepairNodeStorage(OpCode):
385 """Repairs the volume group on a node."""
386 OP_ID = "OP_REPAIR_NODE_STORAGE"
387 OP_DSC_FIELD = "node_name"
388 __slots__ = OpCode.__slots__ + [
395 class OpSetNodeParams(OpCode):
396 """Change the parameters of a node."""
397 OP_ID = "OP_NODE_SET_PARAMS"
398 OP_DSC_FIELD = "node_name"
399 __slots__ = OpCode.__slots__ + [
408 class OpPowercycleNode(OpCode):
409 """Tries to powercycle a node."""
410 OP_ID = "OP_NODE_POWERCYCLE"
411 OP_DSC_FIELD = "node_name"
412 __slots__ = OpCode.__slots__ + [
418 class OpEvacuateNode(OpCode):
419 """Relocate secondary instances from a node."""
420 OP_ID = "OP_NODE_EVACUATE"
421 OP_DSC_FIELD = "node_name"
422 __slots__ = OpCode.__slots__ + [
423 "node_name", "remote_node", "iallocator",
427 class OpMigrateNode(OpCode):
428 """Migrate all instances from a node."""
429 OP_ID = "OP_NODE_MIGRATE"
430 OP_DSC_FIELD = "node_name"
431 __slots__ = OpCode.__slots__ + [
439 class OpCreateInstance(OpCode):
440 """Create an instance."""
441 OP_ID = "OP_INSTANCE_CREATE"
442 OP_DSC_FIELD = "instance_name"
443 __slots__ = OpCode.__slots__ + [
444 "instance_name", "os_type", "pnode",
445 "disk_template", "snode", "mode",
447 "src_node", "src_path", "start",
448 "wait_for_sync", "ip_check",
449 "file_storage_dir", "file_driver",
451 "hypervisor", "hvparams", "beparams",
456 class OpReinstallInstance(OpCode):
457 """Reinstall an instance's OS."""
458 OP_ID = "OP_INSTANCE_REINSTALL"
459 OP_DSC_FIELD = "instance_name"
460 __slots__ = OpCode.__slots__ + ["instance_name", "os_type"]
463 class OpRemoveInstance(OpCode):
464 """Remove an instance."""
465 OP_ID = "OP_INSTANCE_REMOVE"
466 OP_DSC_FIELD = "instance_name"
467 __slots__ = OpCode.__slots__ + ["instance_name", "ignore_failures"]
470 class OpRenameInstance(OpCode):
471 """Rename an instance."""
472 OP_ID = "OP_INSTANCE_RENAME"
473 __slots__ = OpCode.__slots__ + [
474 "instance_name", "ignore_ip", "new_name",
478 class OpStartupInstance(OpCode):
479 """Startup an instance."""
480 OP_ID = "OP_INSTANCE_STARTUP"
481 OP_DSC_FIELD = "instance_name"
482 __slots__ = OpCode.__slots__ + [
483 "instance_name", "force", "hvparams", "beparams",
487 class OpShutdownInstance(OpCode):
488 """Shutdown an instance."""
489 OP_ID = "OP_INSTANCE_SHUTDOWN"
490 OP_DSC_FIELD = "instance_name"
491 __slots__ = OpCode.__slots__ + ["instance_name"]
494 class OpRebootInstance(OpCode):
495 """Reboot an instance."""
496 OP_ID = "OP_INSTANCE_REBOOT"
497 OP_DSC_FIELD = "instance_name"
498 __slots__ = OpCode.__slots__ + [
499 "instance_name", "reboot_type", "ignore_secondaries",
503 class OpReplaceDisks(OpCode):
504 """Replace the disks of an instance."""
505 OP_ID = "OP_INSTANCE_REPLACE_DISKS"
506 OP_DSC_FIELD = "instance_name"
507 __slots__ = OpCode.__slots__ + [
508 "instance_name", "remote_node", "mode", "disks", "iallocator",
512 class OpFailoverInstance(OpCode):
513 """Failover an instance."""
514 OP_ID = "OP_INSTANCE_FAILOVER"
515 OP_DSC_FIELD = "instance_name"
516 __slots__ = OpCode.__slots__ + ["instance_name", "ignore_consistency"]
519 class OpMigrateInstance(OpCode):
520 """Migrate an instance.
522 This migrates (without shutting down an instance) to its secondary
525 @ivar instance_name: the name of the instance
528 OP_ID = "OP_INSTANCE_MIGRATE"
529 OP_DSC_FIELD = "instance_name"
530 __slots__ = OpCode.__slots__ + ["instance_name", "live", "cleanup"]
533 class OpConnectConsole(OpCode):
534 """Connect to an instance's console."""
535 OP_ID = "OP_INSTANCE_CONSOLE"
536 OP_DSC_FIELD = "instance_name"
537 __slots__ = OpCode.__slots__ + ["instance_name"]
540 class OpActivateInstanceDisks(OpCode):
541 """Activate an instance's disks."""
542 OP_ID = "OP_INSTANCE_ACTIVATE_DISKS"
543 OP_DSC_FIELD = "instance_name"
544 __slots__ = OpCode.__slots__ + ["instance_name", "ignore_size"]
547 class OpDeactivateInstanceDisks(OpCode):
548 """Deactivate an instance's disks."""
549 OP_ID = "OP_INSTANCE_DEACTIVATE_DISKS"
550 OP_DSC_FIELD = "instance_name"
551 __slots__ = OpCode.__slots__ + ["instance_name"]
554 class OpRecreateInstanceDisks(OpCode):
555 """Deactivate an instance's disks."""
556 OP_ID = "OP_INSTANCE_RECREATE_DISKS"
557 OP_DSC_FIELD = "instance_name"
558 __slots__ = OpCode.__slots__ + ["instance_name", "disks"]
561 class OpQueryInstances(OpCode):
562 """Compute the list of instances."""
563 OP_ID = "OP_INSTANCE_QUERY"
564 __slots__ = OpCode.__slots__ + ["output_fields", "names", "use_locking"]
567 class OpQueryInstanceData(OpCode):
568 """Compute the run-time status of instances."""
569 OP_ID = "OP_INSTANCE_QUERY_DATA"
570 __slots__ = OpCode.__slots__ + ["instances", "static"]
573 class OpSetInstanceParams(OpCode):
574 """Change the parameters of an instance."""
575 OP_ID = "OP_INSTANCE_SET_PARAMS"
576 OP_DSC_FIELD = "instance_name"
577 __slots__ = OpCode.__slots__ + [
579 "hvparams", "beparams", "force",
584 class OpGrowDisk(OpCode):
585 """Grow a disk of an instance."""
586 OP_ID = "OP_INSTANCE_GROW_DISK"
587 OP_DSC_FIELD = "instance_name"
588 __slots__ = OpCode.__slots__ + [
589 "instance_name", "disk", "amount", "wait_for_sync",
594 class OpDiagnoseOS(OpCode):
595 """Compute the list of guest operating systems."""
596 OP_ID = "OP_OS_DIAGNOSE"
597 __slots__ = OpCode.__slots__ + ["output_fields", "names"]
601 class OpQueryExports(OpCode):
602 """Compute the list of exported images."""
603 OP_ID = "OP_BACKUP_QUERY"
604 __slots__ = OpCode.__slots__ + ["nodes", "use_locking"]
607 class OpExportInstance(OpCode):
608 """Export an instance."""
609 OP_ID = "OP_BACKUP_EXPORT"
610 OP_DSC_FIELD = "instance_name"
611 __slots__ = OpCode.__slots__ + ["instance_name", "target_node", "shutdown"]
614 class OpRemoveExport(OpCode):
615 """Remove an instance's export."""
616 OP_ID = "OP_BACKUP_REMOVE"
617 OP_DSC_FIELD = "instance_name"
618 __slots__ = OpCode.__slots__ + ["instance_name"]
622 class OpGetTags(OpCode):
623 """Returns the tags of the given object."""
624 OP_ID = "OP_TAGS_GET"
625 OP_DSC_FIELD = "name"
626 __slots__ = OpCode.__slots__ + ["kind", "name"]
629 class OpSearchTags(OpCode):
630 """Searches the tags in the cluster for a given pattern."""
631 OP_ID = "OP_TAGS_SEARCH"
632 OP_DSC_FIELD = "pattern"
633 __slots__ = OpCode.__slots__ + ["pattern"]
636 class OpAddTags(OpCode):
637 """Add a list of tags on a given object."""
638 OP_ID = "OP_TAGS_SET"
639 __slots__ = OpCode.__slots__ + ["kind", "name", "tags"]
642 class OpDelTags(OpCode):
643 """Remove a list of tags from a given object."""
644 OP_ID = "OP_TAGS_DEL"
645 __slots__ = OpCode.__slots__ + ["kind", "name", "tags"]
649 class OpTestDelay(OpCode):
650 """Sleeps for a configured amount of time.
652 This is used just for debugging and testing.
655 - duration: the time to sleep
656 - on_master: if true, sleep on the master
657 - on_nodes: list of nodes in which to sleep
659 If the on_master parameter is true, it will execute a sleep on the
660 master (before any node sleep).
662 If the on_nodes list is not empty, it will sleep on those nodes
663 (after the sleep on the master, if that is enabled).
665 As an additional feature, the case of duration < 0 will be reported
666 as an execution error, so this opcode can be used as a failure
667 generator. The case of duration == 0 will not be treated specially.
670 OP_ID = "OP_TEST_DELAY"
671 OP_DSC_FIELD = "duration"
672 __slots__ = OpCode.__slots__ + ["duration", "on_master", "on_nodes"]
675 class OpTestAllocator(OpCode):
676 """Allocator framework testing.
678 This opcode has two modes:
679 - gather and return allocator input for a given mode (allocate new
680 or replace secondary) and a given instance definition (direction
682 - run a selected allocator for a given operation (as above) and
683 return the allocator output (direction 'out')
686 OP_ID = "OP_TEST_ALLOCATOR"
687 OP_DSC_FIELD = "allocator"
688 __slots__ = OpCode.__slots__ + [
689 "direction", "mode", "allocator", "name",
690 "mem_size", "disks", "disk_template",
691 "os", "tags", "nics", "vcpus", "hypervisor",
695 OP_MAPPING = dict([(v.OP_ID, v) for v in globals().values()
696 if (isinstance(v, type) and issubclass(v, OpCode) and
697 hasattr(v, "OP_ID"))])