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 #: OP_ID conversion regular expression
118 _OPID_RE = re.compile("([a-z])([A-Z])")
120 #: Utility function for L{OpClusterSetParams}
121 _TestClusterOsList = ht.TOr(ht.TNone,
122 ht.TListOf(ht.TAnd(ht.TList, ht.TIsLength(2),
123 ht.TMap(ht.WithDesc("GetFirstItem")(operator.itemgetter(0)),
124 ht.TElemOf(constants.DDMS_VALUES)))))
127 # TODO: Generate check from constants.INIC_PARAMS_TYPES
128 #: Utility function for testing NIC definitions
129 _TestNicDef = ht.TDictOf(ht.TElemOf(constants.INIC_PARAMS),
130 ht.TOr(ht.TNone, ht.TNonEmptyString))
134 """Convert an opcode class name to an OP_ID.
137 @param name: the class name, as OpXxxYyy
139 @return: the name in the OP_XXXX_YYYY format
142 if not name.startswith("Op"):
144 # Note: (?<=[a-z])(?=[A-Z]) would be ideal, since it wouldn't
145 # consume any input, and hence we would just have all the elements
146 # in the list, one by one; but it seems that split doesn't work on
147 # non-consuming input, hence we have to process the input string a
149 name = _OPID_RE.sub(r"\1,\2", name)
150 elems = name.split(",")
151 return "_".join(n.upper() for n in elems)
154 def RequireFileStorage():
155 """Checks that file storage is enabled.
157 While it doesn't really fit into this module, L{utils} was deemed too large
158 of a dependency to be imported for just one or two functions.
160 @raise errors.OpPrereqError: when file storage is disabled
163 if not constants.ENABLE_FILE_STORAGE:
164 raise errors.OpPrereqError("File storage disabled at configure time",
168 def RequireSharedFileStorage():
169 """Checks that shared file storage is enabled.
171 While it doesn't really fit into this module, L{utils} was deemed too large
172 of a dependency to be imported for just one or two functions.
174 @raise errors.OpPrereqError: when shared file storage is disabled
177 if not constants.ENABLE_SHARED_FILE_STORAGE:
178 raise errors.OpPrereqError("Shared file storage disabled at"
179 " configure time", errors.ECODE_INVAL)
182 @ht.WithDesc("CheckFileStorage")
183 def _CheckFileStorage(value):
184 """Ensures file storage is enabled if used.
187 if value == constants.DT_FILE:
189 elif value == constants.DT_SHARED_FILE:
190 RequireSharedFileStorage()
194 _CheckDiskTemplate = ht.TAnd(ht.TElemOf(constants.DISK_TEMPLATES),
198 def _CheckStorageType(storage_type):
199 """Ensure a given storage type is valid.
202 if storage_type not in constants.VALID_STORAGE_TYPES:
203 raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
205 if storage_type == constants.ST_FILE:
210 #: Storage type parameter
211 _PStorageType = ("storage_type", ht.NoDefault, _CheckStorageType,
215 class _AutoOpParamSlots(type):
216 """Meta class for opcode definitions.
219 def __new__(mcs, name, bases, attrs):
220 """Called when a class should be created.
222 @param mcs: The meta class
223 @param name: Name of created class
224 @param bases: Base classes
226 @param attrs: Class attributes
229 assert "__slots__" not in attrs, \
230 "Class '%s' defines __slots__ when it should use OP_PARAMS" % name
231 assert "OP_ID" not in attrs, "Class '%s' defining OP_ID" % name
233 attrs["OP_ID"] = _NameToId(name)
235 # Always set OP_PARAMS to avoid duplicates in BaseOpCode.GetAllParams
236 params = attrs.setdefault("OP_PARAMS", [])
238 # Use parameter names as slots
239 slots = [pname for (pname, _, _, _) in params]
241 assert "OP_DSC_FIELD" not in attrs or attrs["OP_DSC_FIELD"] in slots, \
242 "Class '%s' uses unknown field in OP_DSC_FIELD" % name
244 attrs["__slots__"] = slots
246 return type.__new__(mcs, name, bases, attrs)
249 class BaseOpCode(object):
250 """A simple serializable object.
252 This object serves as a parent class for OpCode without any custom
256 # pylint: disable-msg=E1101
257 # as OP_ID is dynamically defined
258 __metaclass__ = _AutoOpParamSlots
260 def __init__(self, **kwargs):
261 """Constructor for BaseOpCode.
263 The constructor takes only keyword arguments and will set
264 attributes on this object based on the passed arguments. As such,
265 it means that you should not pass arguments which are not in the
266 __slots__ attribute for this class.
269 slots = self._all_slots()
272 raise TypeError("Object %s doesn't support the parameter '%s'" %
273 (self.__class__.__name__, key))
274 setattr(self, key, kwargs[key])
276 def __getstate__(self):
277 """Generic serializer.
279 This method just returns the contents of the instance as a
283 @return: the instance attributes and their values
287 for name in self._all_slots():
288 if hasattr(self, name):
289 state[name] = getattr(self, name)
292 def __setstate__(self, state):
293 """Generic unserializer.
295 This method just restores from the serialized state the attributes
296 of the current instance.
298 @param state: the serialized opcode data
302 if not isinstance(state, dict):
303 raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
306 for name in self._all_slots():
307 if name not in state and hasattr(self, name):
311 setattr(self, name, state[name])
315 """Compute the list of all declared slots for a class.
319 for parent in cls.__mro__:
320 slots.extend(getattr(parent, "__slots__", []))
324 def GetAllParams(cls):
325 """Compute list of all parameters for an opcode.
329 for parent in cls.__mro__:
330 slots.extend(getattr(parent, "OP_PARAMS", []))
333 def Validate(self, set_defaults):
334 """Validate opcode parameters, optionally setting default values.
336 @type set_defaults: bool
337 @param set_defaults: Whether to set default values
338 @raise errors.OpPrereqError: When a parameter value doesn't match
342 for (attr_name, default, test, _) in self.GetAllParams():
343 assert test == ht.NoType or callable(test)
345 if not hasattr(self, attr_name):
346 if default == ht.NoDefault:
347 raise errors.OpPrereqError("Required parameter '%s.%s' missing" %
348 (self.OP_ID, attr_name),
351 if callable(default):
355 setattr(self, attr_name, dval)
357 if test == ht.NoType:
361 if set_defaults or hasattr(self, attr_name):
362 attr_val = getattr(self, attr_name)
363 if not test(attr_val):
364 logging.error("OpCode %s, parameter %s, has invalid type %s/value %s",
365 self.OP_ID, attr_name, type(attr_val), attr_val)
366 raise errors.OpPrereqError("Parameter '%s.%s' fails validation" %
367 (self.OP_ID, attr_name),
371 class OpCode(BaseOpCode):
374 This is the root of the actual OpCode hierarchy. All clases derived
375 from this class should override OP_ID.
377 @cvar OP_ID: The ID of this opcode. This should be unique amongst all
378 children of this class.
379 @cvar OP_DSC_FIELD: The name of a field whose value will be included in the
380 string returned by Summary(); see the docstring of that
382 @cvar OP_PARAMS: List of opcode attributes, the default values they should
383 get if not already defined, and types they must match.
384 @cvar WITH_LU: Boolean that specifies whether this should be included in
385 mcpu's dispatch table
386 @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
388 @ivar priority: Opcode priority for queue
391 # pylint: disable-msg=E1101
392 # as OP_ID is dynamically defined
395 ("dry_run", None, ht.TMaybeBool, "Run checks only, don't execute"),
396 ("debug_level", None, ht.TOr(ht.TNone, ht.TPositiveInt), "Debug level"),
397 ("priority", constants.OP_PRIO_DEFAULT,
398 ht.TElemOf(constants.OP_PRIO_SUBMIT_VALID), "Opcode priority"),
401 def __getstate__(self):
402 """Specialized getstate for opcodes.
404 This method adds to the state dictionary the OP_ID of the class,
405 so that on unload we can identify the correct class for
406 instantiating the opcode.
409 @return: the state as a dictionary
412 data = BaseOpCode.__getstate__(self)
413 data["OP_ID"] = self.OP_ID
417 def LoadOpCode(cls, data):
418 """Generic load opcode method.
420 The method identifies the correct opcode class from the dict-form
421 by looking for a OP_ID key, if this is not found, or its value is
422 not available in this module as a child of this class, we fail.
425 @param data: the serialized opcode
428 if not isinstance(data, dict):
429 raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
430 if "OP_ID" not in data:
431 raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
432 op_id = data["OP_ID"]
434 if op_id in OP_MAPPING:
435 op_class = OP_MAPPING[op_id]
437 raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
440 new_data = data.copy()
441 del new_data["OP_ID"]
442 op.__setstate__(new_data)
446 """Generates a summary description of this opcode.
448 The summary is the value of the OP_ID attribute (without the "OP_"
449 prefix), plus the value of the OP_DSC_FIELD attribute, if one was
450 defined; this field should allow to easily identify the operation
451 (for an instance creation job, e.g., it would be the instance
455 assert self.OP_ID is not None and len(self.OP_ID) > 3
456 # all OP_ID start with OP_, we remove that
458 field_name = getattr(self, "OP_DSC_FIELD", None)
460 field_value = getattr(self, field_name, None)
461 if isinstance(field_value, (list, tuple)):
462 field_value = ",".join(str(i) for i in field_value)
463 txt = "%s(%s)" % (txt, field_value)
469 class OpClusterPostInit(OpCode):
470 """Post cluster initialization.
472 This opcode does not touch the cluster at all. Its purpose is to run hooks
473 after the cluster has been initialized.
478 class OpClusterDestroy(OpCode):
479 """Destroy the cluster.
481 This opcode has no other parameters. All the state is irreversibly
482 lost after the execution of this opcode.
487 class OpClusterQuery(OpCode):
488 """Query cluster information."""
491 class OpClusterVerify(OpCode):
492 """Verify the cluster state.
494 @type skip_checks: C{list}
495 @ivar skip_checks: steps to be skipped from the verify process; this
496 needs to be a subset of
497 L{constants.VERIFY_OPTIONAL_CHECKS}; currently
498 only L{constants.VERIFY_NPLUSONE_MEM} can be passed
502 ("skip_checks", ht.EmptyList,
503 ht.TListOf(ht.TElemOf(constants.VERIFY_OPTIONAL_CHECKS)), None),
504 ("verbose", False, ht.TBool, None),
505 ("error_codes", False, ht.TBool, None),
506 ("debug_simulate_errors", False, ht.TBool, None),
510 class OpClusterVerifyDisks(OpCode):
511 """Verify the cluster disks.
515 Result: a tuple of four elements:
516 - list of node names with bad data returned (unreachable, etc.)
517 - dict of node names with broken volume groups (values: error msg)
518 - list of instances with degraded disks (that should be activated)
519 - dict of instances with missing logical volumes (values: (node, vol)
520 pairs with details about the missing volumes)
522 In normal operation, all lists should be empty. A non-empty instance
523 list (3rd element of the result) is still ok (errors were fixed) but
524 non-empty node list means some node is down, and probably there are
525 unfixable drbd errors.
527 Note that only instances that are drbd-based are taken into
528 consideration. This might need to be revisited in the future.
533 class OpClusterRepairDiskSizes(OpCode):
534 """Verify the disk sizes of the instances and fixes configuration
537 Parameters: optional instances list, in case we want to restrict the
538 checks to only a subset of the instances.
540 Result: a list of tuples, (instance, disk, new-size) for changed
543 In normal operation, the list should be empty.
545 @type instances: list
546 @ivar instances: the list of instances to check, or empty for all instances
550 ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
554 class OpClusterConfigQuery(OpCode):
555 """Query cluster configuration values."""
561 class OpClusterRename(OpCode):
562 """Rename the cluster.
565 @ivar name: The new name of the cluster. The name and/or the master IP
566 address will be changed to match the new name and its IP
570 OP_DSC_FIELD = "name"
572 ("name", ht.NoDefault, ht.TNonEmptyString, None),
576 class OpClusterSetParams(OpCode):
577 """Change the parameters of the cluster.
579 @type vg_name: C{str} or C{None}
580 @ivar vg_name: The new volume group name or None to disable LVM usage.
584 ("vg_name", None, ht.TMaybeString, "Volume group name"),
585 ("enabled_hypervisors", None,
586 ht.TOr(ht.TAnd(ht.TListOf(ht.TElemOf(constants.HYPER_TYPES)), ht.TTrue),
588 "List of enabled hypervisors"),
589 ("hvparams", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
591 "Cluster-wide hypervisor parameter defaults, hypervisor-dependent"),
592 ("beparams", None, ht.TOr(ht.TDict, ht.TNone),
593 "Cluster-wide backend parameter defaults"),
594 ("os_hvp", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
596 "Cluster-wide per-OS hypervisor parameter defaults"),
597 ("osparams", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
599 "Cluster-wide OS parameter defaults"),
600 ("candidate_pool_size", None, ht.TOr(ht.TStrictPositiveInt, ht.TNone),
601 "Master candidate pool size"),
602 ("uid_pool", None, ht.NoType,
603 "Set UID pool, must be list of lists describing UID ranges (two items,"
604 " start and end inclusive)"),
605 ("add_uids", None, ht.NoType,
606 "Extend UID pool, must be list of lists describing UID ranges (two"
607 " items, start and end inclusive) to be added"),
608 ("remove_uids", None, ht.NoType,
609 "Shrink UID pool, must be list of lists describing UID ranges (two"
610 " items, start and end inclusive) to be removed"),
611 ("maintain_node_health", None, ht.TMaybeBool,
612 "Whether to automatically maintain node health"),
613 ("prealloc_wipe_disks", None, ht.TMaybeBool,
614 "Whether to wipe disks before allocating them to instances"),
615 ("nicparams", None, ht.TMaybeDict, "Cluster-wide NIC parameter defaults"),
616 ("ndparams", None, ht.TMaybeDict, "Cluster-wide node parameter defaults"),
617 ("drbd_helper", None, ht.TOr(ht.TString, ht.TNone), "DRBD helper program"),
618 ("default_iallocator", None, ht.TOr(ht.TString, ht.TNone),
619 "Default iallocator for cluster"),
620 ("master_netdev", None, ht.TOr(ht.TString, ht.TNone),
621 "Master network device"),
622 ("reserved_lvs", None, ht.TOr(ht.TListOf(ht.TNonEmptyString), ht.TNone),
623 "List of reserved LVs"),
624 ("hidden_os", None, _TestClusterOsList,
625 "Modify list of hidden operating systems. Each modification must have"
626 " two items, the operation and the OS name. The operation can be"
627 " ``%s`` or ``%s``." % (constants.DDM_ADD, constants.DDM_REMOVE)),
628 ("blacklisted_os", None, _TestClusterOsList,
629 "Modify list of blacklisted operating systems. Each modification must have"
630 " two items, the operation and the OS name. The operation can be"
631 " ``%s`` or ``%s``." % (constants.DDM_ADD, constants.DDM_REMOVE)),
635 class OpClusterRedistConf(OpCode):
636 """Force a full push of the cluster configuration.
641 class OpQuery(OpCode):
642 """Query for resources/items.
644 @ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
645 @ivar fields: List of fields to retrieve
646 @ivar filter: Query filter
651 ("fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
653 ("filter", None, ht.TOr(ht.TNone,
654 ht.TListOf(ht.TOr(ht.TNonEmptyString, ht.TList))),
659 class OpQueryFields(OpCode):
660 """Query for available resource/item fields.
662 @ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
663 @ivar fields: List of fields to retrieve
668 ("fields", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
669 "Requested fields; if not given, all are returned"),
673 class OpOobCommand(OpCode):
674 """Interact with OOB."""
676 ("node_names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
677 "List of nodes to run the OOB command against"),
678 ("command", None, ht.TElemOf(constants.OOB_COMMANDS),
679 "OOB command to be run"),
680 ("timeout", constants.OOB_TIMEOUT, ht.TInt,
681 "Timeout before the OOB helper will be terminated"),
682 ("ignore_status", False, ht.TBool,
683 "Ignores the node offline status for power off"),
684 ("power_delay", constants.OOB_POWER_DELAY, ht.TPositiveFloat,
685 "Time in seconds to wait between powering on nodes"),
691 class OpNodeRemove(OpCode):
694 @type node_name: C{str}
695 @ivar node_name: The name of the node to remove. If the node still has
696 instances on it, the operation will fail.
699 OP_DSC_FIELD = "node_name"
705 class OpNodeAdd(OpCode):
706 """Add a node to the cluster.
708 @type node_name: C{str}
709 @ivar node_name: The name of the node to add. This can be a short name,
710 but it will be expanded to the FQDN.
711 @type primary_ip: IP address
712 @ivar primary_ip: The primary IP of the node. This will be ignored when the
713 opcode is submitted, but will be filled during the node
714 add (so it will be visible in the job query).
715 @type secondary_ip: IP address
716 @ivar secondary_ip: The secondary IP of the node. This needs to be passed
717 if the cluster has been initialized in 'dual-network'
718 mode, otherwise it must not be given.
720 @ivar readd: Whether to re-add an existing node to the cluster. If
721 this is not passed, then the operation will abort if the node
722 name is already in the cluster; use this parameter to 'repair'
723 a node that had its configuration broken, or was reinstalled
724 without removal from the cluster.
726 @ivar group: The node group to which this node will belong.
727 @type vm_capable: C{bool}
728 @ivar vm_capable: The vm_capable node attribute
729 @type master_capable: C{bool}
730 @ivar master_capable: The master_capable node attribute
733 OP_DSC_FIELD = "node_name"
736 ("primary_ip", None, ht.NoType, "Primary IP address"),
737 ("secondary_ip", None, ht.TMaybeString, "Secondary IP address"),
738 ("readd", False, ht.TBool, "Whether node is re-added to cluster"),
739 ("group", None, ht.TMaybeString, "Initial node group"),
740 ("master_capable", None, ht.TMaybeBool,
741 "Whether node can become master or master candidate"),
742 ("vm_capable", None, ht.TMaybeBool,
743 "Whether node can host instances"),
744 ("ndparams", None, ht.TMaybeDict, "Node parameters"),
748 class OpNodeQuery(OpCode):
749 """Compute the list of nodes."""
753 ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
754 "Empty list to query all nodes, node names otherwise"),
758 class OpNodeQueryvols(OpCode):
759 """Get list of volumes on node."""
762 ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
763 "Empty list to query all nodes, node names otherwise"),
767 class OpNodeQueryStorage(OpCode):
768 """Get information on storage for node(s)."""
772 ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "List of nodes"),
773 ("name", None, ht.TMaybeString, "Storage name"),
777 class OpNodeModifyStorage(OpCode):
778 """Modifies the properies of a storage unit"""
783 ("changes", ht.NoDefault, ht.TDict, "Requested changes"),
787 class OpRepairNodeStorage(OpCode):
788 """Repairs the volume group on a node."""
789 OP_DSC_FIELD = "node_name"
798 class OpNodeSetParams(OpCode):
799 """Change the parameters of a node."""
800 OP_DSC_FIELD = "node_name"
804 ("master_candidate", None, ht.TMaybeBool,
805 "Whether the node should become a master candidate"),
806 ("offline", None, ht.TMaybeBool,
807 "Whether the node should be marked as offline"),
808 ("drained", None, ht.TMaybeBool,
809 "Whether the node should be marked as drained"),
810 ("auto_promote", False, ht.TBool,
811 "Whether node(s) should be promoted to master candidate if necessary"),
812 ("master_capable", None, ht.TMaybeBool,
813 "Denote whether node can become master or master candidate"),
814 ("vm_capable", None, ht.TMaybeBool,
815 "Denote whether node can host instances"),
816 ("secondary_ip", None, ht.TMaybeString,
817 "Change node's secondary IP address"),
818 ("ndparams", None, ht.TMaybeDict, "Set node parameters"),
819 ("powered", None, ht.TMaybeBool,
820 "Whether the node should be marked as powered"),
824 class OpNodePowercycle(OpCode):
825 """Tries to powercycle a node."""
826 OP_DSC_FIELD = "node_name"
833 class OpNodeMigrate(OpCode):
834 """Migrate all instances from a node."""
835 OP_DSC_FIELD = "node_name"
840 ("iallocator", None, ht.TMaybeString,
841 "Iallocator for deciding the target node for shared-storage instances"),
845 class OpNodeEvacStrategy(OpCode):
846 """Compute the evacuation strategy for a list of nodes."""
847 OP_DSC_FIELD = "nodes"
849 ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString), None),
850 ("remote_node", None, ht.TMaybeString, None),
851 ("iallocator", None, ht.TMaybeString, None),
857 class OpInstanceCreate(OpCode):
858 """Create an instance.
860 @ivar instance_name: Instance name
861 @ivar mode: Instance creation mode (one of L{constants.INSTANCE_CREATE_MODES})
862 @ivar source_handshake: Signed handshake from source (remote import only)
863 @ivar source_x509_ca: Source X509 CA in PEM format (remote import only)
864 @ivar source_instance_name: Previous name of instance (remote import only)
865 @ivar source_shutdown_timeout: Shutdown timeout used for source instance
869 OP_DSC_FIELD = "instance_name"
875 ("beparams", ht.EmptyDict, ht.TDict, "Backend parameters for instance"),
876 ("disks", ht.NoDefault,
877 # TODO: Generate check from constants.IDISK_PARAMS_TYPES
878 ht.TListOf(ht.TDictOf(ht.TElemOf(constants.IDISK_PARAMS),
879 ht.TOr(ht.TNonEmptyString, ht.TInt))),
880 "Disk descriptions, for example ``[{\"%s\": 100}, {\"%s\": 5}]``;"
881 " each disk definition must contain a ``%s`` value and"
882 " can contain an optional ``%s`` value denoting the disk access mode"
884 (constants.IDISK_SIZE, constants.IDISK_SIZE, constants.IDISK_SIZE,
885 constants.IDISK_MODE,
886 " or ".join("``%s``" % i for i in sorted(constants.DISK_ACCESS_SET)))),
887 ("disk_template", ht.NoDefault, _CheckDiskTemplate, "Disk template"),
888 ("file_driver", None, ht.TOr(ht.TNone, ht.TElemOf(constants.FILE_DRIVER)),
889 "Driver for file-backed disks"),
890 ("file_storage_dir", None, ht.TMaybeString,
891 "Directory for storing file-backed disks"),
892 ("hvparams", ht.EmptyDict, ht.TDict,
893 "Hypervisor parameters for instance, hypervisor-dependent"),
894 ("hypervisor", None, ht.TMaybeString, "Hypervisor"),
895 ("iallocator", None, ht.TMaybeString,
896 "Iallocator for deciding which node(s) to use"),
897 ("identify_defaults", False, ht.TBool,
898 "Reset instance parameters to default if equal"),
899 ("ip_check", True, ht.TBool, _PIpCheckDoc),
900 ("mode", ht.NoDefault, ht.TElemOf(constants.INSTANCE_CREATE_MODES),
901 "Instance creation mode"),
902 ("nics", ht.NoDefault, ht.TListOf(_TestNicDef),
903 "List of NIC (network interface) definitions, for example"
904 " ``[{}, {}, {\"%s\": \"198.51.100.4\"}]``; each NIC definition can"
905 " contain the optional values %s" %
907 ", ".join("``%s``" % i for i in sorted(constants.INIC_PARAMS)))),
908 ("no_install", None, ht.TMaybeBool,
909 "Do not install the OS (will disable automatic start)"),
910 ("osparams", ht.EmptyDict, ht.TDict, "OS parameters for instance"),
911 ("os_type", None, ht.TMaybeString, "Operating system"),
912 ("pnode", None, ht.TMaybeString, "Primary node"),
913 ("snode", None, ht.TMaybeString, "Secondary node"),
914 ("source_handshake", None, ht.TOr(ht.TList, ht.TNone),
915 "Signed handshake from source (remote import only)"),
916 ("source_instance_name", None, ht.TMaybeString,
917 "Source instance name (remote import only)"),
918 ("source_shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT,
920 "How long source instance was given to shut down (remote import only)"),
921 ("source_x509_ca", None, ht.TMaybeString,
922 "Source X509 CA in PEM format (remote import only)"),
923 ("src_node", None, ht.TMaybeString, "Source node for import"),
924 ("src_path", None, ht.TMaybeString, "Source directory for import"),
925 ("start", True, ht.TBool, "Whether to start instance after creation"),
929 class OpInstanceReinstall(OpCode):
930 """Reinstall an instance's OS."""
931 OP_DSC_FIELD = "instance_name"
935 ("os_type", None, ht.TMaybeString, "Instance operating system"),
936 ("osparams", None, ht.TMaybeDict, "Temporary OS parameters"),
940 class OpInstanceRemove(OpCode):
941 """Remove an instance."""
942 OP_DSC_FIELD = "instance_name"
946 ("ignore_failures", False, ht.TBool,
947 "Whether to ignore failures during removal"),
951 class OpInstanceRename(OpCode):
952 """Rename an instance."""
956 ("new_name", ht.NoDefault, ht.TNonEmptyString, "New instance name"),
957 ("ip_check", False, ht.TBool, _PIpCheckDoc),
961 class OpInstanceStartup(OpCode):
962 """Startup an instance."""
963 OP_DSC_FIELD = "instance_name"
967 _PIgnoreOfflineNodes,
968 ("hvparams", ht.EmptyDict, ht.TDict,
969 "Temporary hypervisor parameters, hypervisor-dependent"),
970 ("beparams", ht.EmptyDict, ht.TDict, "Temporary backend parameters"),
974 class OpInstanceShutdown(OpCode):
975 """Shutdown an instance."""
976 OP_DSC_FIELD = "instance_name"
979 _PIgnoreOfflineNodes,
980 ("timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT, ht.TPositiveInt,
981 "How long to wait for instance to shut down"),
985 class OpInstanceReboot(OpCode):
986 """Reboot an instance."""
987 OP_DSC_FIELD = "instance_name"
991 ("ignore_secondaries", False, ht.TBool,
992 "Whether to start the instance even if secondary disks are failing"),
993 ("reboot_type", ht.NoDefault, ht.TElemOf(constants.REBOOT_TYPES),
994 "How to reboot instance"),
998 class OpInstanceReplaceDisks(OpCode):
999 """Replace the disks of an instance."""
1000 OP_DSC_FIELD = "instance_name"
1003 ("mode", ht.NoDefault, ht.TElemOf(constants.REPLACE_MODES),
1004 "Replacement mode"),
1005 ("disks", ht.EmptyList, ht.TListOf(ht.TPositiveInt),
1007 ("remote_node", None, ht.TMaybeString, "New secondary node"),
1008 ("iallocator", None, ht.TMaybeString,
1009 "Iallocator for deciding new secondary node"),
1010 ("early_release", False, ht.TBool,
1011 "Whether to release locks as soon as possible"),
1015 class OpInstanceFailover(OpCode):
1016 """Failover an instance."""
1017 OP_DSC_FIELD = "instance_name"
1021 _PIgnoreConsistency,
1022 ("iallocator", None, ht.TMaybeString,
1023 "Iallocator for deciding the target node for shared-storage instances"),
1024 ("target_node", None, ht.TMaybeString,
1025 "Target node for shared-storage instances"),
1029 class OpInstanceMigrate(OpCode):
1030 """Migrate an instance.
1032 This migrates (without shutting down an instance) to its secondary
1035 @ivar instance_name: the name of the instance
1036 @ivar mode: the migration mode (live, non-live or None for auto)
1039 OP_DSC_FIELD = "instance_name"
1044 ("cleanup", False, ht.TBool,
1045 "Whether a previously failed migration should be cleaned up"),
1046 ("iallocator", None, ht.TMaybeString,
1047 "Iallocator for deciding the target node for shared-storage instances"),
1048 ("target_node", None, ht.TMaybeString,
1049 "Target node for shared-storage instances"),
1050 ("allow_failover", False, ht.TBool,
1051 "Whether we can fallback to failover if migration is not possible"),
1055 class OpInstanceMove(OpCode):
1056 """Move an instance.
1058 This move (with shutting down an instance and data copying) to an
1061 @ivar instance_name: the name of the instance
1062 @ivar target_node: the destination node
1065 OP_DSC_FIELD = "instance_name"
1069 ("target_node", ht.NoDefault, ht.TNonEmptyString, "Target node"),
1073 class OpInstanceConsole(OpCode):
1074 """Connect to an instance's console."""
1075 OP_DSC_FIELD = "instance_name"
1081 class OpInstanceActivateDisks(OpCode):
1082 """Activate an instance's disks."""
1083 OP_DSC_FIELD = "instance_name"
1086 ("ignore_size", False, ht.TBool, "Whether to ignore recorded size"),
1090 class OpInstanceDeactivateDisks(OpCode):
1091 """Deactivate an instance's disks."""
1092 OP_DSC_FIELD = "instance_name"
1099 class OpInstanceRecreateDisks(OpCode):
1100 """Deactivate an instance's disks."""
1101 OP_DSC_FIELD = "instance_name"
1104 ("disks", ht.EmptyList, ht.TListOf(ht.TPositiveInt),
1105 "List of disk indexes"),
1109 class OpInstanceQuery(OpCode):
1110 """Compute the list of instances."""
1114 ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1115 "Empty list to query all instances, instance names otherwise"),
1119 class OpInstanceQueryData(OpCode):
1120 """Compute the run-time status of instances."""
1122 ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1123 ("static", False, ht.TBool, None),
1127 class OpInstanceSetParams(OpCode):
1128 """Change the parameters of an instance."""
1129 OP_DSC_FIELD = "instance_name"
1134 # TODO: Use _TestNicDef
1135 ("nics", ht.EmptyList, ht.TList,
1136 "List of NIC changes. Each item is of the form ``(op, settings)``."
1137 " ``op`` can be ``%s`` to add a new NIC with the specified settings,"
1138 " ``%s`` to remove the last NIC or a number to modify the settings"
1139 " of the NIC with that index." %
1140 (constants.DDM_ADD, constants.DDM_REMOVE)),
1141 ("disks", ht.EmptyList, ht.TList, "List of disk changes. See ``nics``."),
1142 ("beparams", ht.EmptyDict, ht.TDict, "Per-instance backend parameters"),
1143 ("hvparams", ht.EmptyDict, ht.TDict,
1144 "Per-instance hypervisor parameters, hypervisor-dependent"),
1145 ("disk_template", None, ht.TOr(ht.TNone, _CheckDiskTemplate),
1146 "Disk template for instance"),
1147 ("remote_node", None, ht.TMaybeString,
1148 "Secondary node (used when changing disk template)"),
1149 ("os_name", None, ht.TMaybeString,
1150 "Change instance's OS name. Does not reinstall the instance."),
1151 ("osparams", None, ht.TMaybeDict, "Per-instance OS parameters"),
1155 class OpInstanceGrowDisk(OpCode):
1156 """Grow a disk of an instance."""
1157 OP_DSC_FIELD = "instance_name"
1161 ("disk", ht.NoDefault, ht.TInt, "Disk index"),
1162 ("amount", ht.NoDefault, ht.TInt,
1163 "Amount of disk space to add (megabytes)"),
1167 # Node group opcodes
1169 class OpGroupAdd(OpCode):
1170 """Add a node group to the cluster."""
1171 OP_DSC_FIELD = "group_name"
1174 _PNodeGroupAllocPolicy,
1179 class OpGroupAssignNodes(OpCode):
1180 """Assign nodes to a node group."""
1181 OP_DSC_FIELD = "group_name"
1185 ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
1186 "List of nodes to assign"),
1190 class OpGroupQuery(OpCode):
1191 """Compute the list of node groups."""
1194 ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1195 "Empty list to query all groups, group names otherwise"),
1199 class OpGroupSetParams(OpCode):
1200 """Change the parameters of a node group."""
1201 OP_DSC_FIELD = "group_name"
1204 _PNodeGroupAllocPolicy,
1209 class OpGroupRemove(OpCode):
1210 """Remove a node group from the cluster."""
1211 OP_DSC_FIELD = "group_name"
1217 class OpGroupRename(OpCode):
1218 """Rename a node group in the cluster."""
1221 ("new_name", ht.NoDefault, ht.TNonEmptyString, "New group name"),
1226 class OpOsDiagnose(OpCode):
1227 """Compute the list of guest operating systems."""
1230 ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1231 "Which operating systems to diagnose"),
1236 class OpBackupQuery(OpCode):
1237 """Compute the list of exported images."""
1240 ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1241 "Empty list to query all nodes, node names otherwise"),
1245 class OpBackupPrepare(OpCode):
1246 """Prepares an instance export.
1248 @ivar instance_name: Instance name
1249 @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1252 OP_DSC_FIELD = "instance_name"
1255 ("mode", ht.NoDefault, ht.TElemOf(constants.EXPORT_MODES),
1260 class OpBackupExport(OpCode):
1261 """Export an instance.
1263 For local exports, the export destination is the node name. For remote
1264 exports, the export destination is a list of tuples, each consisting of
1265 hostname/IP address, port, HMAC and HMAC salt. The HMAC is calculated using
1266 the cluster domain secret over the value "${index}:${hostname}:${port}". The
1267 destination X509 CA must be a signed certificate.
1269 @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1270 @ivar target_node: Export destination
1271 @ivar x509_key_name: X509 key to use (remote export only)
1272 @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
1276 OP_DSC_FIELD = "instance_name"
1280 # TODO: Rename target_node as it changes meaning for different export modes
1281 # (e.g. "destination")
1282 ("target_node", ht.NoDefault, ht.TOr(ht.TNonEmptyString, ht.TList),
1283 "Destination information, depends on export mode"),
1284 ("shutdown", True, ht.TBool, "Whether to shutdown instance before export"),
1285 ("remove_instance", False, ht.TBool,
1286 "Whether to remove instance after export"),
1287 ("ignore_remove_failures", False, ht.TBool,
1288 "Whether to ignore failures while removing instances"),
1289 ("mode", constants.EXPORT_MODE_LOCAL, ht.TElemOf(constants.EXPORT_MODES),
1291 ("x509_key_name", None, ht.TOr(ht.TList, ht.TNone),
1292 "Name of X509 key (remote export only)"),
1293 ("destination_x509_ca", None, ht.TMaybeString,
1294 "Destination X509 CA (remote export only)"),
1298 class OpBackupRemove(OpCode):
1299 """Remove an instance's export."""
1300 OP_DSC_FIELD = "instance_name"
1307 class OpTagsGet(OpCode):
1308 """Returns the tags of the given object."""
1309 OP_DSC_FIELD = "name"
1312 # Name is only meaningful for nodes and instances
1313 ("name", ht.NoDefault, ht.TMaybeString, None),
1317 class OpTagsSearch(OpCode):
1318 """Searches the tags in the cluster for a given pattern."""
1319 OP_DSC_FIELD = "pattern"
1321 ("pattern", ht.NoDefault, ht.TNonEmptyString, None),
1325 class OpTagsSet(OpCode):
1326 """Add a list of tags on a given object."""
1330 # Name is only meaningful for nodes and instances
1331 ("name", ht.NoDefault, ht.TMaybeString, None),
1335 class OpTagsDel(OpCode):
1336 """Remove a list of tags from a given object."""
1340 # Name is only meaningful for nodes and instances
1341 ("name", ht.NoDefault, ht.TMaybeString, None),
1345 class OpTestDelay(OpCode):
1346 """Sleeps for a configured amount of time.
1348 This is used just for debugging and testing.
1351 - duration: the time to sleep
1352 - on_master: if true, sleep on the master
1353 - on_nodes: list of nodes in which to sleep
1355 If the on_master parameter is true, it will execute a sleep on the
1356 master (before any node sleep).
1358 If the on_nodes list is not empty, it will sleep on those nodes
1359 (after the sleep on the master, if that is enabled).
1361 As an additional feature, the case of duration < 0 will be reported
1362 as an execution error, so this opcode can be used as a failure
1363 generator. The case of duration == 0 will not be treated specially.
1366 OP_DSC_FIELD = "duration"
1368 ("duration", ht.NoDefault, ht.TFloat, None),
1369 ("on_master", True, ht.TBool, None),
1370 ("on_nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1371 ("repeat", 0, ht.TPositiveInt, None),
1375 class OpTestAllocator(OpCode):
1376 """Allocator framework testing.
1378 This opcode has two modes:
1379 - gather and return allocator input for a given mode (allocate new
1380 or replace secondary) and a given instance definition (direction
1382 - run a selected allocator for a given operation (as above) and
1383 return the allocator output (direction 'out')
1386 OP_DSC_FIELD = "allocator"
1388 ("direction", ht.NoDefault,
1389 ht.TElemOf(constants.VALID_IALLOCATOR_DIRECTIONS), None),
1390 ("mode", ht.NoDefault, ht.TElemOf(constants.VALID_IALLOCATOR_MODES), None),
1391 ("name", ht.NoDefault, ht.TNonEmptyString, None),
1392 ("nics", ht.NoDefault, ht.TOr(ht.TNone, ht.TListOf(
1393 ht.TDictOf(ht.TElemOf(["mac", "ip", "bridge"]),
1394 ht.TOr(ht.TNone, ht.TNonEmptyString)))), None),
1395 ("disks", ht.NoDefault, ht.TOr(ht.TNone, ht.TList), None),
1396 ("hypervisor", None, ht.TMaybeString, None),
1397 ("allocator", None, ht.TMaybeString, None),
1398 ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1399 ("mem_size", None, ht.TOr(ht.TNone, ht.TPositiveInt), None),
1400 ("vcpus", None, ht.TOr(ht.TNone, ht.TPositiveInt), None),
1401 ("os", None, ht.TMaybeString, None),
1402 ("disk_template", None, ht.TMaybeString, None),
1403 ("evac_nodes", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
1408 class OpTestJqueue(OpCode):
1409 """Utility opcode to test some aspects of the job queue.
1413 ("notify_waitlock", False, ht.TBool, None),
1414 ("notify_exec", False, ht.TBool, None),
1415 ("log_messages", ht.EmptyList, ht.TListOf(ht.TString), None),
1416 ("fail", False, ht.TBool, None),
1420 class OpTestDummy(OpCode):
1421 """Utility opcode used by unittests.
1425 ("result", ht.NoDefault, ht.NoType, None),
1426 ("messages", ht.NoDefault, ht.NoType, None),
1427 ("fail", ht.NoDefault, ht.NoType, None),
1428 ("submit_jobs", None, ht.NoType, None),
1434 """Returns list of all defined opcodes.
1436 Does not eliminate duplicates by C{OP_ID}.
1439 return [v for v in globals().values()
1440 if (isinstance(v, type) and issubclass(v, OpCode) and
1441 hasattr(v, "OP_ID") and v is not OpCode)]
1444 OP_MAPPING = dict((v.OP_ID, v) for v in _GetOpList())