4 # Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 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
40 from ganeti import constants
41 from ganeti import errors
45 # Common opcode attributes
47 #: output fields for a query operation
48 _POutputFields = ("output_fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
49 "Selected output fields")
51 #: the shutdown timeout
53 ("shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT, ht.TPositiveInt,
54 "How long to wait for instance to shut down")
56 #: the force parameter
57 _PForce = ("force", False, ht.TBool, "Whether to force the operation")
59 #: a required instance name (for single-instance LUs)
60 _PInstanceName = ("instance_name", ht.NoDefault, ht.TNonEmptyString,
63 #: Whether to ignore offline nodes
64 _PIgnoreOfflineNodes = ("ignore_offline_nodes", False, ht.TBool,
65 "Whether to ignore offline nodes")
67 #: a required node name (for single-node LUs)
68 _PNodeName = ("node_name", ht.NoDefault, ht.TNonEmptyString, "Node name")
70 #: a required node group name (for single-group LUs)
71 _PGroupName = ("group_name", ht.NoDefault, ht.TNonEmptyString, "Group name")
73 #: Migration type (live/non-live)
74 _PMigrationMode = ("mode", None,
75 ht.TOr(ht.TNone, ht.TElemOf(constants.HT_MIGRATION_MODES)),
78 #: Obsolete 'live' migration mode (boolean)
79 _PMigrationLive = ("live", None, ht.TMaybeBool,
80 "Legacy setting for live migration, do not use")
83 _PTagKind = ("kind", ht.NoDefault, ht.TElemOf(constants.VALID_TAG_TYPES), None)
85 #: List of tag strings
86 _PTags = ("tags", ht.NoDefault, ht.TListOf(ht.TNonEmptyString), None)
88 _PForceVariant = ("force_variant", False, ht.TBool,
89 "Whether to force an unknown OS variant")
91 _PWaitForSync = ("wait_for_sync", True, ht.TBool,
92 "Whether to wait for the disk to synchronize")
94 _PIgnoreConsistency = ("ignore_consistency", False, ht.TBool,
95 "Whether to ignore disk consistency")
97 _PStorageName = ("name", ht.NoDefault, ht.TMaybeString, "Storage name")
99 _PUseLocking = ("use_locking", False, ht.TBool,
100 "Whether to use synchronization")
102 _PNameCheck = ("name_check", True, ht.TBool, "Whether to check name")
104 _PNodeGroupAllocPolicy = \
105 ("alloc_policy", None,
106 ht.TOr(ht.TNone, ht.TElemOf(constants.VALID_ALLOC_POLICIES)),
107 "Instance allocation policy")
109 _PGroupNodeParams = ("ndparams", None, ht.TMaybeDict,
110 "Default node parameters for group")
112 _PQueryWhat = ("what", ht.NoDefault, ht.TElemOf(constants.QR_VIA_OP),
113 "Resource(s) to query for")
115 _PIpCheckDoc = "Whether to ensure instance's IP address is inactive"
117 #: Do not remember instance state changes
118 _PNoRemember = ("no_remember", False, ht.TBool,
119 "Do not remember the state change")
121 #: Target node for instance migration/failover
122 _PMigrationTargetNode = ("target_node", None, ht.TMaybeString,
123 "Target node for shared-storage instances")
125 #: OP_ID conversion regular expression
126 _OPID_RE = re.compile("([a-z])([A-Z])")
128 #: Utility function for L{OpClusterSetParams}
129 _TestClusterOsList = ht.TOr(ht.TNone,
130 ht.TListOf(ht.TAnd(ht.TList, ht.TIsLength(2),
131 ht.TMap(ht.WithDesc("GetFirstItem")(operator.itemgetter(0)),
132 ht.TElemOf(constants.DDMS_VALUES)))))
135 # TODO: Generate check from constants.INIC_PARAMS_TYPES
136 #: Utility function for testing NIC definitions
137 _TestNicDef = ht.TDictOf(ht.TElemOf(constants.INIC_PARAMS),
138 ht.TOr(ht.TNone, ht.TNonEmptyString))
149 """Convert an opcode class name to an OP_ID.
152 @param name: the class name, as OpXxxYyy
154 @return: the name in the OP_XXXX_YYYY format
157 if not name.startswith("Op"):
159 # Note: (?<=[a-z])(?=[A-Z]) would be ideal, since it wouldn't
160 # consume any input, and hence we would just have all the elements
161 # in the list, one by one; but it seems that split doesn't work on
162 # non-consuming input, hence we have to process the input string a
164 name = _OPID_RE.sub(r"\1,\2", name)
165 elems = name.split(",")
166 return "_".join(n.upper() for n in elems)
169 def RequireFileStorage():
170 """Checks that file storage is enabled.
172 While it doesn't really fit into this module, L{utils} was deemed too large
173 of a dependency to be imported for just one or two functions.
175 @raise errors.OpPrereqError: when file storage is disabled
178 if not constants.ENABLE_FILE_STORAGE:
179 raise errors.OpPrereqError("File storage disabled at configure time",
183 def RequireSharedFileStorage():
184 """Checks that shared file storage is enabled.
186 While it doesn't really fit into this module, L{utils} was deemed too large
187 of a dependency to be imported for just one or two functions.
189 @raise errors.OpPrereqError: when shared file storage is disabled
192 if not constants.ENABLE_SHARED_FILE_STORAGE:
193 raise errors.OpPrereqError("Shared file storage disabled at"
194 " configure time", errors.ECODE_INVAL)
197 @ht.WithDesc("CheckFileStorage")
198 def _CheckFileStorage(value):
199 """Ensures file storage is enabled if used.
202 if value == constants.DT_FILE:
204 elif value == constants.DT_SHARED_FILE:
205 RequireSharedFileStorage()
209 _CheckDiskTemplate = ht.TAnd(ht.TElemOf(constants.DISK_TEMPLATES),
213 def _CheckStorageType(storage_type):
214 """Ensure a given storage type is valid.
217 if storage_type not in constants.VALID_STORAGE_TYPES:
218 raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
220 if storage_type == constants.ST_FILE:
225 #: Storage type parameter
226 _PStorageType = ("storage_type", ht.NoDefault, _CheckStorageType,
230 class _AutoOpParamSlots(type):
231 """Meta class for opcode definitions.
234 def __new__(mcs, name, bases, attrs):
235 """Called when a class should be created.
237 @param mcs: The meta class
238 @param name: Name of created class
239 @param bases: Base classes
241 @param attrs: Class attributes
244 assert "__slots__" not in attrs, \
245 "Class '%s' defines __slots__ when it should use OP_PARAMS" % name
246 assert "OP_ID" not in attrs, "Class '%s' defining OP_ID" % name
248 attrs["OP_ID"] = _NameToId(name)
250 # Always set OP_PARAMS to avoid duplicates in BaseOpCode.GetAllParams
251 params = attrs.setdefault("OP_PARAMS", [])
253 # Use parameter names as slots
254 slots = [pname for (pname, _, _, _) in params]
256 assert "OP_DSC_FIELD" not in attrs or attrs["OP_DSC_FIELD"] in slots, \
257 "Class '%s' uses unknown field in OP_DSC_FIELD" % name
259 attrs["__slots__"] = slots
261 return type.__new__(mcs, name, bases, attrs)
264 class BaseOpCode(object):
265 """A simple serializable object.
267 This object serves as a parent class for OpCode without any custom
271 # pylint: disable-msg=E1101
272 # as OP_ID is dynamically defined
273 __metaclass__ = _AutoOpParamSlots
275 def __init__(self, **kwargs):
276 """Constructor for BaseOpCode.
278 The constructor takes only keyword arguments and will set
279 attributes on this object based on the passed arguments. As such,
280 it means that you should not pass arguments which are not in the
281 __slots__ attribute for this class.
284 slots = self._all_slots()
287 raise TypeError("Object %s doesn't support the parameter '%s'" %
288 (self.__class__.__name__, key))
289 setattr(self, key, kwargs[key])
291 def __getstate__(self):
292 """Generic serializer.
294 This method just returns the contents of the instance as a
298 @return: the instance attributes and their values
302 for name in self._all_slots():
303 if hasattr(self, name):
304 state[name] = getattr(self, name)
307 def __setstate__(self, state):
308 """Generic unserializer.
310 This method just restores from the serialized state the attributes
311 of the current instance.
313 @param state: the serialized opcode data
317 if not isinstance(state, dict):
318 raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
321 for name in self._all_slots():
322 if name not in state and hasattr(self, name):
326 setattr(self, name, state[name])
330 """Compute the list of all declared slots for a class.
334 for parent in cls.__mro__:
335 slots.extend(getattr(parent, "__slots__", []))
339 def GetAllParams(cls):
340 """Compute list of all parameters for an opcode.
344 for parent in cls.__mro__:
345 slots.extend(getattr(parent, "OP_PARAMS", []))
348 def Validate(self, set_defaults):
349 """Validate opcode parameters, optionally setting default values.
351 @type set_defaults: bool
352 @param set_defaults: Whether to set default values
353 @raise errors.OpPrereqError: When a parameter value doesn't match
357 for (attr_name, default, test, _) in self.GetAllParams():
358 assert test == ht.NoType or callable(test)
360 if not hasattr(self, attr_name):
361 if default == ht.NoDefault:
362 raise errors.OpPrereqError("Required parameter '%s.%s' missing" %
363 (self.OP_ID, attr_name),
366 if callable(default):
370 setattr(self, attr_name, dval)
372 if test == ht.NoType:
376 if set_defaults or hasattr(self, attr_name):
377 attr_val = getattr(self, attr_name)
378 if not test(attr_val):
379 logging.error("OpCode %s, parameter %s, has invalid type %s/value %s",
380 self.OP_ID, attr_name, type(attr_val), attr_val)
381 raise errors.OpPrereqError("Parameter '%s.%s' fails validation" %
382 (self.OP_ID, attr_name),
386 class OpCode(BaseOpCode):
389 This is the root of the actual OpCode hierarchy. All clases derived
390 from this class should override OP_ID.
392 @cvar OP_ID: The ID of this opcode. This should be unique amongst all
393 children of this class.
394 @cvar OP_DSC_FIELD: The name of a field whose value will be included in the
395 string returned by Summary(); see the docstring of that
397 @cvar OP_PARAMS: List of opcode attributes, the default values they should
398 get if not already defined, and types they must match.
399 @cvar WITH_LU: Boolean that specifies whether this should be included in
400 mcpu's dispatch table
401 @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
403 @ivar priority: Opcode priority for queue
406 # pylint: disable-msg=E1101
407 # as OP_ID is dynamically defined
410 ("dry_run", None, ht.TMaybeBool, "Run checks only, don't execute"),
411 ("debug_level", None, ht.TOr(ht.TNone, ht.TPositiveInt), "Debug level"),
412 ("priority", constants.OP_PRIO_DEFAULT,
413 ht.TElemOf(constants.OP_PRIO_SUBMIT_VALID), "Opcode priority"),
416 def __getstate__(self):
417 """Specialized getstate for opcodes.
419 This method adds to the state dictionary the OP_ID of the class,
420 so that on unload we can identify the correct class for
421 instantiating the opcode.
424 @return: the state as a dictionary
427 data = BaseOpCode.__getstate__(self)
428 data["OP_ID"] = self.OP_ID
432 def LoadOpCode(cls, data):
433 """Generic load opcode method.
435 The method identifies the correct opcode class from the dict-form
436 by looking for a OP_ID key, if this is not found, or its value is
437 not available in this module as a child of this class, we fail.
440 @param data: the serialized opcode
443 if not isinstance(data, dict):
444 raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
445 if "OP_ID" not in data:
446 raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
447 op_id = data["OP_ID"]
449 if op_id in OP_MAPPING:
450 op_class = OP_MAPPING[op_id]
452 raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
455 new_data = data.copy()
456 del new_data["OP_ID"]
457 op.__setstate__(new_data)
461 """Generates a summary description of this opcode.
463 The summary is the value of the OP_ID attribute (without the "OP_"
464 prefix), plus the value of the OP_DSC_FIELD attribute, if one was
465 defined; this field should allow to easily identify the operation
466 (for an instance creation job, e.g., it would be the instance
470 assert self.OP_ID is not None and len(self.OP_ID) > 3
471 # all OP_ID start with OP_, we remove that
473 field_name = getattr(self, "OP_DSC_FIELD", None)
475 field_value = getattr(self, field_name, None)
476 if isinstance(field_value, (list, tuple)):
477 field_value = ",".join(str(i) for i in field_value)
478 txt = "%s(%s)" % (txt, field_value)
481 def TinySummary(self):
482 """Generates a compact summary description of the opcode.
485 assert self.OP_ID.startswith("OP_")
487 text = self.OP_ID[3:]
489 for (prefix, supplement) in _SUMMARY_PREFIX.items():
490 if text.startswith(prefix):
491 return supplement + text[len(prefix):]
498 class OpClusterPostInit(OpCode):
499 """Post cluster initialization.
501 This opcode does not touch the cluster at all. Its purpose is to run hooks
502 after the cluster has been initialized.
507 class OpClusterDestroy(OpCode):
508 """Destroy the cluster.
510 This opcode has no other parameters. All the state is irreversibly
511 lost after the execution of this opcode.
516 class OpClusterQuery(OpCode):
517 """Query cluster information."""
520 class OpClusterVerifyConfig(OpCode):
521 """Verify the cluster config.
525 ("verbose", False, ht.TBool, None),
526 ("error_codes", False, ht.TBool, None),
527 ("debug_simulate_errors", False, ht.TBool, None),
531 class OpClusterVerifyGroup(OpCode):
532 """Run verify on a node group from the cluster.
534 @type skip_checks: C{list}
535 @ivar skip_checks: steps to be skipped from the verify process; this
536 needs to be a subset of
537 L{constants.VERIFY_OPTIONAL_CHECKS}; currently
538 only L{constants.VERIFY_NPLUSONE_MEM} can be passed
541 OP_DSC_FIELD = "group_name"
543 ("group_name", ht.NoDefault, ht.TNonEmptyString, None),
544 ("skip_checks", ht.EmptyList,
545 ht.TListOf(ht.TElemOf(constants.VERIFY_OPTIONAL_CHECKS)), None),
546 ("verbose", False, ht.TBool, None),
547 ("error_codes", False, ht.TBool, None),
548 ("debug_simulate_errors", False, ht.TBool, None),
552 class OpClusterVerifyDisks(OpCode):
553 """Verify the cluster disks.
557 Result: a tuple of four elements:
558 - list of node names with bad data returned (unreachable, etc.)
559 - dict of node names with broken volume groups (values: error msg)
560 - list of instances with degraded disks (that should be activated)
561 - dict of instances with missing logical volumes (values: (node, vol)
562 pairs with details about the missing volumes)
564 In normal operation, all lists should be empty. A non-empty instance
565 list (3rd element of the result) is still ok (errors were fixed) but
566 non-empty node list means some node is down, and probably there are
567 unfixable drbd errors.
569 Note that only instances that are drbd-based are taken into
570 consideration. This might need to be revisited in the future.
575 class OpClusterRepairDiskSizes(OpCode):
576 """Verify the disk sizes of the instances and fixes configuration
579 Parameters: optional instances list, in case we want to restrict the
580 checks to only a subset of the instances.
582 Result: a list of tuples, (instance, disk, new-size) for changed
585 In normal operation, the list should be empty.
587 @type instances: list
588 @ivar instances: the list of instances to check, or empty for all instances
592 ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
596 class OpClusterConfigQuery(OpCode):
597 """Query cluster configuration values."""
603 class OpClusterRename(OpCode):
604 """Rename the cluster.
607 @ivar name: The new name of the cluster. The name and/or the master IP
608 address will be changed to match the new name and its IP
612 OP_DSC_FIELD = "name"
614 ("name", ht.NoDefault, ht.TNonEmptyString, None),
618 class OpClusterSetParams(OpCode):
619 """Change the parameters of the cluster.
621 @type vg_name: C{str} or C{None}
622 @ivar vg_name: The new volume group name or None to disable LVM usage.
626 ("vg_name", None, ht.TMaybeString, "Volume group name"),
627 ("enabled_hypervisors", None,
628 ht.TOr(ht.TAnd(ht.TListOf(ht.TElemOf(constants.HYPER_TYPES)), ht.TTrue),
630 "List of enabled hypervisors"),
631 ("hvparams", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
633 "Cluster-wide hypervisor parameter defaults, hypervisor-dependent"),
634 ("beparams", None, ht.TOr(ht.TDict, ht.TNone),
635 "Cluster-wide backend parameter defaults"),
636 ("os_hvp", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
638 "Cluster-wide per-OS hypervisor parameter defaults"),
639 ("osparams", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
641 "Cluster-wide OS parameter defaults"),
642 ("candidate_pool_size", None, ht.TOr(ht.TStrictPositiveInt, ht.TNone),
643 "Master candidate pool size"),
644 ("uid_pool", None, ht.NoType,
645 "Set UID pool, must be list of lists describing UID ranges (two items,"
646 " start and end inclusive)"),
647 ("add_uids", None, ht.NoType,
648 "Extend UID pool, must be list of lists describing UID ranges (two"
649 " items, start and end inclusive) to be added"),
650 ("remove_uids", None, ht.NoType,
651 "Shrink UID pool, must be list of lists describing UID ranges (two"
652 " items, start and end inclusive) to be removed"),
653 ("maintain_node_health", None, ht.TMaybeBool,
654 "Whether to automatically maintain node health"),
655 ("prealloc_wipe_disks", None, ht.TMaybeBool,
656 "Whether to wipe disks before allocating them to instances"),
657 ("nicparams", None, ht.TMaybeDict, "Cluster-wide NIC parameter defaults"),
658 ("ndparams", None, ht.TMaybeDict, "Cluster-wide node parameter defaults"),
659 ("drbd_helper", None, ht.TOr(ht.TString, ht.TNone), "DRBD helper program"),
660 ("default_iallocator", None, ht.TOr(ht.TString, ht.TNone),
661 "Default iallocator for cluster"),
662 ("master_netdev", None, ht.TOr(ht.TString, ht.TNone),
663 "Master network device"),
664 ("reserved_lvs", None, ht.TOr(ht.TListOf(ht.TNonEmptyString), ht.TNone),
665 "List of reserved LVs"),
666 ("hidden_os", None, _TestClusterOsList,
667 "Modify list of hidden operating systems. Each modification must have"
668 " two items, the operation and the OS name. The operation can be"
669 " ``%s`` or ``%s``." % (constants.DDM_ADD, constants.DDM_REMOVE)),
670 ("blacklisted_os", None, _TestClusterOsList,
671 "Modify list of blacklisted operating systems. Each modification must have"
672 " two items, the operation and the OS name. The operation can be"
673 " ``%s`` or ``%s``." % (constants.DDM_ADD, constants.DDM_REMOVE)),
677 class OpClusterRedistConf(OpCode):
678 """Force a full push of the cluster configuration.
683 class OpQuery(OpCode):
684 """Query for resources/items.
686 @ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
687 @ivar fields: List of fields to retrieve
688 @ivar filter: Query filter
693 ("fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
695 ("filter", None, ht.TOr(ht.TNone, ht.TListOf),
700 class OpQueryFields(OpCode):
701 """Query for available resource/item fields.
703 @ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
704 @ivar fields: List of fields to retrieve
709 ("fields", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
710 "Requested fields; if not given, all are returned"),
714 class OpOobCommand(OpCode):
715 """Interact with OOB."""
717 ("node_names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
718 "List of nodes to run the OOB command against"),
719 ("command", None, ht.TElemOf(constants.OOB_COMMANDS),
720 "OOB command to be run"),
721 ("timeout", constants.OOB_TIMEOUT, ht.TInt,
722 "Timeout before the OOB helper will be terminated"),
723 ("ignore_status", False, ht.TBool,
724 "Ignores the node offline status for power off"),
725 ("power_delay", constants.OOB_POWER_DELAY, ht.TPositiveFloat,
726 "Time in seconds to wait between powering on nodes"),
732 class OpNodeRemove(OpCode):
735 @type node_name: C{str}
736 @ivar node_name: The name of the node to remove. If the node still has
737 instances on it, the operation will fail.
740 OP_DSC_FIELD = "node_name"
746 class OpNodeAdd(OpCode):
747 """Add a node to the cluster.
749 @type node_name: C{str}
750 @ivar node_name: The name of the node to add. This can be a short name,
751 but it will be expanded to the FQDN.
752 @type primary_ip: IP address
753 @ivar primary_ip: The primary IP of the node. This will be ignored when the
754 opcode is submitted, but will be filled during the node
755 add (so it will be visible in the job query).
756 @type secondary_ip: IP address
757 @ivar secondary_ip: The secondary IP of the node. This needs to be passed
758 if the cluster has been initialized in 'dual-network'
759 mode, otherwise it must not be given.
761 @ivar readd: Whether to re-add an existing node to the cluster. If
762 this is not passed, then the operation will abort if the node
763 name is already in the cluster; use this parameter to 'repair'
764 a node that had its configuration broken, or was reinstalled
765 without removal from the cluster.
767 @ivar group: The node group to which this node will belong.
768 @type vm_capable: C{bool}
769 @ivar vm_capable: The vm_capable node attribute
770 @type master_capable: C{bool}
771 @ivar master_capable: The master_capable node attribute
774 OP_DSC_FIELD = "node_name"
777 ("primary_ip", None, ht.NoType, "Primary IP address"),
778 ("secondary_ip", None, ht.TMaybeString, "Secondary IP address"),
779 ("readd", False, ht.TBool, "Whether node is re-added to cluster"),
780 ("group", None, ht.TMaybeString, "Initial node group"),
781 ("master_capable", None, ht.TMaybeBool,
782 "Whether node can become master or master candidate"),
783 ("vm_capable", None, ht.TMaybeBool,
784 "Whether node can host instances"),
785 ("ndparams", None, ht.TMaybeDict, "Node parameters"),
789 class OpNodeQuery(OpCode):
790 """Compute the list of nodes."""
794 ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
795 "Empty list to query all nodes, node names otherwise"),
799 class OpNodeQueryvols(OpCode):
800 """Get list of volumes on node."""
803 ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
804 "Empty list to query all nodes, node names otherwise"),
808 class OpNodeQueryStorage(OpCode):
809 """Get information on storage for node(s)."""
813 ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "List of nodes"),
814 ("name", None, ht.TMaybeString, "Storage name"),
818 class OpNodeModifyStorage(OpCode):
819 """Modifies the properies of a storage unit"""
824 ("changes", ht.NoDefault, ht.TDict, "Requested changes"),
828 class OpRepairNodeStorage(OpCode):
829 """Repairs the volume group on a node."""
830 OP_DSC_FIELD = "node_name"
839 class OpNodeSetParams(OpCode):
840 """Change the parameters of a node."""
841 OP_DSC_FIELD = "node_name"
845 ("master_candidate", None, ht.TMaybeBool,
846 "Whether the node should become a master candidate"),
847 ("offline", None, ht.TMaybeBool,
848 "Whether the node should be marked as offline"),
849 ("drained", None, ht.TMaybeBool,
850 "Whether the node should be marked as drained"),
851 ("auto_promote", False, ht.TBool,
852 "Whether node(s) should be promoted to master candidate if necessary"),
853 ("master_capable", None, ht.TMaybeBool,
854 "Denote whether node can become master or master candidate"),
855 ("vm_capable", None, ht.TMaybeBool,
856 "Denote whether node can host instances"),
857 ("secondary_ip", None, ht.TMaybeString,
858 "Change node's secondary IP address"),
859 ("ndparams", None, ht.TMaybeDict, "Set node parameters"),
860 ("powered", None, ht.TMaybeBool,
861 "Whether the node should be marked as powered"),
865 class OpNodePowercycle(OpCode):
866 """Tries to powercycle a node."""
867 OP_DSC_FIELD = "node_name"
874 class OpNodeMigrate(OpCode):
875 """Migrate all instances from a node."""
876 OP_DSC_FIELD = "node_name"
881 _PMigrationTargetNode,
882 ("iallocator", None, ht.TMaybeString,
883 "Iallocator for deciding the target node for shared-storage instances"),
887 class OpNodeEvacStrategy(OpCode):
888 """Compute the evacuation strategy for a list of nodes."""
889 OP_DSC_FIELD = "nodes"
891 ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString), None),
892 ("remote_node", None, ht.TMaybeString, None),
893 ("iallocator", None, ht.TMaybeString, None),
899 class OpInstanceCreate(OpCode):
900 """Create an instance.
902 @ivar instance_name: Instance name
903 @ivar mode: Instance creation mode (one of L{constants.INSTANCE_CREATE_MODES})
904 @ivar source_handshake: Signed handshake from source (remote import only)
905 @ivar source_x509_ca: Source X509 CA in PEM format (remote import only)
906 @ivar source_instance_name: Previous name of instance (remote import only)
907 @ivar source_shutdown_timeout: Shutdown timeout used for source instance
911 OP_DSC_FIELD = "instance_name"
917 ("beparams", ht.EmptyDict, ht.TDict, "Backend parameters for instance"),
918 ("disks", ht.NoDefault,
919 # TODO: Generate check from constants.IDISK_PARAMS_TYPES
920 ht.TListOf(ht.TDictOf(ht.TElemOf(constants.IDISK_PARAMS),
921 ht.TOr(ht.TNonEmptyString, ht.TInt))),
922 "Disk descriptions, for example ``[{\"%s\": 100}, {\"%s\": 5}]``;"
923 " each disk definition must contain a ``%s`` value and"
924 " can contain an optional ``%s`` value denoting the disk access mode"
926 (constants.IDISK_SIZE, constants.IDISK_SIZE, constants.IDISK_SIZE,
927 constants.IDISK_MODE,
928 " or ".join("``%s``" % i for i in sorted(constants.DISK_ACCESS_SET)))),
929 ("disk_template", ht.NoDefault, _CheckDiskTemplate, "Disk template"),
930 ("file_driver", None, ht.TOr(ht.TNone, ht.TElemOf(constants.FILE_DRIVER)),
931 "Driver for file-backed disks"),
932 ("file_storage_dir", None, ht.TMaybeString,
933 "Directory for storing file-backed disks"),
934 ("hvparams", ht.EmptyDict, ht.TDict,
935 "Hypervisor parameters for instance, hypervisor-dependent"),
936 ("hypervisor", None, ht.TMaybeString, "Hypervisor"),
937 ("iallocator", None, ht.TMaybeString,
938 "Iallocator for deciding which node(s) to use"),
939 ("identify_defaults", False, ht.TBool,
940 "Reset instance parameters to default if equal"),
941 ("ip_check", True, ht.TBool, _PIpCheckDoc),
942 ("mode", ht.NoDefault, ht.TElemOf(constants.INSTANCE_CREATE_MODES),
943 "Instance creation mode"),
944 ("nics", ht.NoDefault, ht.TListOf(_TestNicDef),
945 "List of NIC (network interface) definitions, for example"
946 " ``[{}, {}, {\"%s\": \"198.51.100.4\"}]``; each NIC definition can"
947 " contain the optional values %s" %
949 ", ".join("``%s``" % i for i in sorted(constants.INIC_PARAMS)))),
950 ("no_install", None, ht.TMaybeBool,
951 "Do not install the OS (will disable automatic start)"),
952 ("osparams", ht.EmptyDict, ht.TDict, "OS parameters for instance"),
953 ("os_type", None, ht.TMaybeString, "Operating system"),
954 ("pnode", None, ht.TMaybeString, "Primary node"),
955 ("snode", None, ht.TMaybeString, "Secondary node"),
956 ("source_handshake", None, ht.TOr(ht.TList, ht.TNone),
957 "Signed handshake from source (remote import only)"),
958 ("source_instance_name", None, ht.TMaybeString,
959 "Source instance name (remote import only)"),
960 ("source_shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT,
962 "How long source instance was given to shut down (remote import only)"),
963 ("source_x509_ca", None, ht.TMaybeString,
964 "Source X509 CA in PEM format (remote import only)"),
965 ("src_node", None, ht.TMaybeString, "Source node for import"),
966 ("src_path", None, ht.TMaybeString, "Source directory for import"),
967 ("start", True, ht.TBool, "Whether to start instance after creation"),
968 ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "Instance tags"),
972 class OpInstanceReinstall(OpCode):
973 """Reinstall an instance's OS."""
974 OP_DSC_FIELD = "instance_name"
978 ("os_type", None, ht.TMaybeString, "Instance operating system"),
979 ("osparams", None, ht.TMaybeDict, "Temporary OS parameters"),
983 class OpInstanceRemove(OpCode):
984 """Remove an instance."""
985 OP_DSC_FIELD = "instance_name"
989 ("ignore_failures", False, ht.TBool,
990 "Whether to ignore failures during removal"),
994 class OpInstanceRename(OpCode):
995 """Rename an instance."""
999 ("new_name", ht.NoDefault, ht.TNonEmptyString, "New instance name"),
1000 ("ip_check", False, ht.TBool, _PIpCheckDoc),
1004 class OpInstanceStartup(OpCode):
1005 """Startup an instance."""
1006 OP_DSC_FIELD = "instance_name"
1010 _PIgnoreOfflineNodes,
1011 ("hvparams", ht.EmptyDict, ht.TDict,
1012 "Temporary hypervisor parameters, hypervisor-dependent"),
1013 ("beparams", ht.EmptyDict, ht.TDict, "Temporary backend parameters"),
1018 class OpInstanceShutdown(OpCode):
1019 """Shutdown an instance."""
1020 OP_DSC_FIELD = "instance_name"
1023 _PIgnoreOfflineNodes,
1024 ("timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT, ht.TPositiveInt,
1025 "How long to wait for instance to shut down"),
1030 class OpInstanceReboot(OpCode):
1031 """Reboot an instance."""
1032 OP_DSC_FIELD = "instance_name"
1036 ("ignore_secondaries", False, ht.TBool,
1037 "Whether to start the instance even if secondary disks are failing"),
1038 ("reboot_type", ht.NoDefault, ht.TElemOf(constants.REBOOT_TYPES),
1039 "How to reboot instance"),
1043 class OpInstanceReplaceDisks(OpCode):
1044 """Replace the disks of an instance."""
1045 OP_DSC_FIELD = "instance_name"
1048 ("mode", ht.NoDefault, ht.TElemOf(constants.REPLACE_MODES),
1049 "Replacement mode"),
1050 ("disks", ht.EmptyList, ht.TListOf(ht.TPositiveInt),
1052 ("remote_node", None, ht.TMaybeString, "New secondary node"),
1053 ("iallocator", None, ht.TMaybeString,
1054 "Iallocator for deciding new secondary node"),
1055 ("early_release", False, ht.TBool,
1056 "Whether to release locks as soon as possible"),
1060 class OpInstanceFailover(OpCode):
1061 """Failover an instance."""
1062 OP_DSC_FIELD = "instance_name"
1066 _PIgnoreConsistency,
1067 _PMigrationTargetNode,
1068 ("iallocator", None, ht.TMaybeString,
1069 "Iallocator for deciding the target node for shared-storage instances"),
1073 class OpInstanceMigrate(OpCode):
1074 """Migrate an instance.
1076 This migrates (without shutting down an instance) to its secondary
1079 @ivar instance_name: the name of the instance
1080 @ivar mode: the migration mode (live, non-live or None for auto)
1083 OP_DSC_FIELD = "instance_name"
1088 _PMigrationTargetNode,
1089 ("cleanup", False, ht.TBool,
1090 "Whether a previously failed migration should be cleaned up"),
1091 ("iallocator", None, ht.TMaybeString,
1092 "Iallocator for deciding the target node for shared-storage instances"),
1093 ("allow_failover", False, ht.TBool,
1094 "Whether we can fallback to failover if migration is not possible"),
1098 class OpInstanceMove(OpCode):
1099 """Move an instance.
1101 This move (with shutting down an instance and data copying) to an
1104 @ivar instance_name: the name of the instance
1105 @ivar target_node: the destination node
1108 OP_DSC_FIELD = "instance_name"
1112 ("target_node", ht.NoDefault, ht.TNonEmptyString, "Target node"),
1113 _PIgnoreConsistency,
1117 class OpInstanceConsole(OpCode):
1118 """Connect to an instance's console."""
1119 OP_DSC_FIELD = "instance_name"
1125 class OpInstanceActivateDisks(OpCode):
1126 """Activate an instance's disks."""
1127 OP_DSC_FIELD = "instance_name"
1130 ("ignore_size", False, ht.TBool, "Whether to ignore recorded size"),
1134 class OpInstanceDeactivateDisks(OpCode):
1135 """Deactivate an instance's disks."""
1136 OP_DSC_FIELD = "instance_name"
1143 class OpInstanceRecreateDisks(OpCode):
1144 """Deactivate an instance's disks."""
1145 OP_DSC_FIELD = "instance_name"
1148 ("disks", ht.EmptyList, ht.TListOf(ht.TPositiveInt),
1149 "List of disk indexes"),
1150 ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1151 "New instance nodes, if relocation is desired"),
1155 class OpInstanceQuery(OpCode):
1156 """Compute the list of instances."""
1160 ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1161 "Empty list to query all instances, instance names otherwise"),
1165 class OpInstanceQueryData(OpCode):
1166 """Compute the run-time status of instances."""
1169 ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1171 ("static", False, ht.TBool,
1172 "Whether to only return configuration data without querying"
1177 class OpInstanceSetParams(OpCode):
1178 """Change the parameters of an instance."""
1179 OP_DSC_FIELD = "instance_name"
1184 # TODO: Use _TestNicDef
1185 ("nics", ht.EmptyList, ht.TList,
1186 "List of NIC changes. Each item is of the form ``(op, settings)``."
1187 " ``op`` can be ``%s`` to add a new NIC with the specified settings,"
1188 " ``%s`` to remove the last NIC or a number to modify the settings"
1189 " of the NIC with that index." %
1190 (constants.DDM_ADD, constants.DDM_REMOVE)),
1191 ("disks", ht.EmptyList, ht.TList, "List of disk changes. See ``nics``."),
1192 ("beparams", ht.EmptyDict, ht.TDict, "Per-instance backend parameters"),
1193 ("hvparams", ht.EmptyDict, ht.TDict,
1194 "Per-instance hypervisor parameters, hypervisor-dependent"),
1195 ("disk_template", None, ht.TOr(ht.TNone, _CheckDiskTemplate),
1196 "Disk template for instance"),
1197 ("remote_node", None, ht.TMaybeString,
1198 "Secondary node (used when changing disk template)"),
1199 ("os_name", None, ht.TMaybeString,
1200 "Change instance's OS name. Does not reinstall the instance."),
1201 ("osparams", None, ht.TMaybeDict, "Per-instance OS parameters"),
1202 ("wait_for_sync", True, ht.TBool,
1203 "Whether to wait for the disk to synchronize, when changing template"),
1207 class OpInstanceGrowDisk(OpCode):
1208 """Grow a disk of an instance."""
1209 OP_DSC_FIELD = "instance_name"
1213 ("disk", ht.NoDefault, ht.TInt, "Disk index"),
1214 ("amount", ht.NoDefault, ht.TInt,
1215 "Amount of disk space to add (megabytes)"),
1219 # Node group opcodes
1221 class OpGroupAdd(OpCode):
1222 """Add a node group to the cluster."""
1223 OP_DSC_FIELD = "group_name"
1226 _PNodeGroupAllocPolicy,
1231 class OpGroupAssignNodes(OpCode):
1232 """Assign nodes to a node group."""
1233 OP_DSC_FIELD = "group_name"
1237 ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
1238 "List of nodes to assign"),
1242 class OpGroupQuery(OpCode):
1243 """Compute the list of node groups."""
1246 ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1247 "Empty list to query all groups, group names otherwise"),
1251 class OpGroupSetParams(OpCode):
1252 """Change the parameters of a node group."""
1253 OP_DSC_FIELD = "group_name"
1256 _PNodeGroupAllocPolicy,
1261 class OpGroupRemove(OpCode):
1262 """Remove a node group from the cluster."""
1263 OP_DSC_FIELD = "group_name"
1269 class OpGroupRename(OpCode):
1270 """Rename a node group in the cluster."""
1273 ("new_name", ht.NoDefault, ht.TNonEmptyString, "New group name"),
1278 class OpOsDiagnose(OpCode):
1279 """Compute the list of guest operating systems."""
1282 ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1283 "Which operating systems to diagnose"),
1288 class OpBackupQuery(OpCode):
1289 """Compute the list of exported images."""
1292 ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1293 "Empty list to query all nodes, node names otherwise"),
1297 class OpBackupPrepare(OpCode):
1298 """Prepares an instance export.
1300 @ivar instance_name: Instance name
1301 @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1304 OP_DSC_FIELD = "instance_name"
1307 ("mode", ht.NoDefault, ht.TElemOf(constants.EXPORT_MODES),
1312 class OpBackupExport(OpCode):
1313 """Export an instance.
1315 For local exports, the export destination is the node name. For remote
1316 exports, the export destination is a list of tuples, each consisting of
1317 hostname/IP address, port, HMAC and HMAC salt. The HMAC is calculated using
1318 the cluster domain secret over the value "${index}:${hostname}:${port}". The
1319 destination X509 CA must be a signed certificate.
1321 @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1322 @ivar target_node: Export destination
1323 @ivar x509_key_name: X509 key to use (remote export only)
1324 @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
1328 OP_DSC_FIELD = "instance_name"
1332 # TODO: Rename target_node as it changes meaning for different export modes
1333 # (e.g. "destination")
1334 ("target_node", ht.NoDefault, ht.TOr(ht.TNonEmptyString, ht.TList),
1335 "Destination information, depends on export mode"),
1336 ("shutdown", True, ht.TBool, "Whether to shutdown instance before export"),
1337 ("remove_instance", False, ht.TBool,
1338 "Whether to remove instance after export"),
1339 ("ignore_remove_failures", False, ht.TBool,
1340 "Whether to ignore failures while removing instances"),
1341 ("mode", constants.EXPORT_MODE_LOCAL, ht.TElemOf(constants.EXPORT_MODES),
1343 ("x509_key_name", None, ht.TOr(ht.TList, ht.TNone),
1344 "Name of X509 key (remote export only)"),
1345 ("destination_x509_ca", None, ht.TMaybeString,
1346 "Destination X509 CA (remote export only)"),
1350 class OpBackupRemove(OpCode):
1351 """Remove an instance's export."""
1352 OP_DSC_FIELD = "instance_name"
1359 class OpTagsGet(OpCode):
1360 """Returns the tags of the given object."""
1361 OP_DSC_FIELD = "name"
1364 # Name is only meaningful for nodes and instances
1365 ("name", ht.NoDefault, ht.TMaybeString, None),
1369 class OpTagsSearch(OpCode):
1370 """Searches the tags in the cluster for a given pattern."""
1371 OP_DSC_FIELD = "pattern"
1373 ("pattern", ht.NoDefault, ht.TNonEmptyString, None),
1377 class OpTagsSet(OpCode):
1378 """Add a list of tags on a given object."""
1382 # Name is only meaningful for nodes and instances
1383 ("name", ht.NoDefault, ht.TMaybeString, None),
1387 class OpTagsDel(OpCode):
1388 """Remove a list of tags from a given object."""
1392 # Name is only meaningful for nodes and instances
1393 ("name", ht.NoDefault, ht.TMaybeString, None),
1397 class OpTestDelay(OpCode):
1398 """Sleeps for a configured amount of time.
1400 This is used just for debugging and testing.
1403 - duration: the time to sleep
1404 - on_master: if true, sleep on the master
1405 - on_nodes: list of nodes in which to sleep
1407 If the on_master parameter is true, it will execute a sleep on the
1408 master (before any node sleep).
1410 If the on_nodes list is not empty, it will sleep on those nodes
1411 (after the sleep on the master, if that is enabled).
1413 As an additional feature, the case of duration < 0 will be reported
1414 as an execution error, so this opcode can be used as a failure
1415 generator. The case of duration == 0 will not be treated specially.
1418 OP_DSC_FIELD = "duration"
1420 ("duration", ht.NoDefault, ht.TFloat, None),
1421 ("on_master", True, ht.TBool, None),
1422 ("on_nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1423 ("repeat", 0, ht.TPositiveInt, None),
1427 class OpTestAllocator(OpCode):
1428 """Allocator framework testing.
1430 This opcode has two modes:
1431 - gather and return allocator input for a given mode (allocate new
1432 or replace secondary) and a given instance definition (direction
1434 - run a selected allocator for a given operation (as above) and
1435 return the allocator output (direction 'out')
1438 OP_DSC_FIELD = "allocator"
1440 ("direction", ht.NoDefault,
1441 ht.TElemOf(constants.VALID_IALLOCATOR_DIRECTIONS), None),
1442 ("mode", ht.NoDefault, ht.TElemOf(constants.VALID_IALLOCATOR_MODES), None),
1443 ("name", ht.NoDefault, ht.TNonEmptyString, None),
1444 ("nics", ht.NoDefault, ht.TOr(ht.TNone, ht.TListOf(
1445 ht.TDictOf(ht.TElemOf([constants.INIC_MAC, constants.INIC_IP, "bridge"]),
1446 ht.TOr(ht.TNone, ht.TNonEmptyString)))), None),
1447 ("disks", ht.NoDefault, ht.TOr(ht.TNone, ht.TList), None),
1448 ("hypervisor", None, ht.TMaybeString, None),
1449 ("allocator", None, ht.TMaybeString, None),
1450 ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1451 ("memory", None, ht.TOr(ht.TNone, ht.TPositiveInt), None),
1452 ("vcpus", None, ht.TOr(ht.TNone, ht.TPositiveInt), None),
1453 ("os", None, ht.TMaybeString, None),
1454 ("disk_template", None, ht.TMaybeString, None),
1455 ("evac_nodes", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
1457 ("instances", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
1460 ht.TOr(ht.TNone, ht.TElemOf(constants.IALLOCATOR_NEVAC_MODES)), None),
1461 ("target_groups", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
1466 class OpTestJqueue(OpCode):
1467 """Utility opcode to test some aspects of the job queue.
1471 ("notify_waitlock", False, ht.TBool, None),
1472 ("notify_exec", False, ht.TBool, None),
1473 ("log_messages", ht.EmptyList, ht.TListOf(ht.TString), None),
1474 ("fail", False, ht.TBool, None),
1478 class OpTestDummy(OpCode):
1479 """Utility opcode used by unittests.
1483 ("result", ht.NoDefault, ht.NoType, None),
1484 ("messages", ht.NoDefault, ht.NoType, None),
1485 ("fail", ht.NoDefault, ht.NoType, None),
1486 ("submit_jobs", None, ht.NoType, None),
1492 """Returns list of all defined opcodes.
1494 Does not eliminate duplicates by C{OP_ID}.
1497 return [v for v in globals().values()
1498 if (isinstance(v, type) and issubclass(v, OpCode) and
1499 hasattr(v, "OP_ID") and v is not OpCode)]
1502 OP_MAPPING = dict((v.OP_ID, v) for v in _GetOpList())