4 # Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012 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=R0903
40 from ganeti import constants
41 from ganeti import errors
43 from ganeti import objects
44 from ganeti import objectutils
47 # Common opcode attributes
49 #: output fields for a query operation
50 _POutputFields = ("output_fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
51 "Selected output fields")
53 #: the shutdown timeout
55 ("shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT, ht.TNonNegativeInt,
56 "How long to wait for instance to shut down")
58 #: the force parameter
59 _PForce = ("force", False, ht.TBool, "Whether to force the operation")
61 #: a required instance name (for single-instance LUs)
62 _PInstanceName = ("instance_name", ht.NoDefault, ht.TNonEmptyString,
65 #: Whether to ignore offline nodes
66 _PIgnoreOfflineNodes = ("ignore_offline_nodes", False, ht.TBool,
67 "Whether to ignore offline nodes")
69 #: a required node name (for single-node LUs)
70 _PNodeName = ("node_name", ht.NoDefault, ht.TNonEmptyString, "Node name")
72 #: a required node group name (for single-group LUs)
73 _PGroupName = ("group_name", ht.NoDefault, ht.TNonEmptyString, "Group name")
75 #: Migration type (live/non-live)
76 _PMigrationMode = ("mode", None,
77 ht.TOr(ht.TNone, ht.TElemOf(constants.HT_MIGRATION_MODES)),
80 #: Obsolete 'live' migration mode (boolean)
81 _PMigrationLive = ("live", None, ht.TMaybeBool,
82 "Legacy setting for live migration, do not use")
85 _PTagKind = ("kind", ht.NoDefault, ht.TElemOf(constants.VALID_TAG_TYPES),
88 #: List of tag strings
89 _PTags = ("tags", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
92 _PForceVariant = ("force_variant", False, ht.TBool,
93 "Whether to force an unknown OS variant")
95 _PWaitForSync = ("wait_for_sync", True, ht.TBool,
96 "Whether to wait for the disk to synchronize")
98 _PWaitForSyncFalse = ("wait_for_sync", False, ht.TBool,
99 "Whether to wait for the disk to synchronize"
100 " (defaults to false)")
102 _PIgnoreConsistency = ("ignore_consistency", False, ht.TBool,
103 "Whether to ignore disk consistency")
105 _PStorageName = ("name", ht.NoDefault, ht.TMaybeString, "Storage name")
107 _PUseLocking = ("use_locking", False, ht.TBool,
108 "Whether to use synchronization")
110 _PNameCheck = ("name_check", True, ht.TBool, "Whether to check name")
112 _PNodeGroupAllocPolicy = \
113 ("alloc_policy", None,
114 ht.TOr(ht.TNone, ht.TElemOf(constants.VALID_ALLOC_POLICIES)),
115 "Instance allocation policy")
117 _PGroupNodeParams = ("ndparams", None, ht.TMaybeDict,
118 "Default node parameters for group")
120 _PQueryWhat = ("what", ht.NoDefault, ht.TElemOf(constants.QR_VIA_OP),
121 "Resource(s) to query for")
123 _PEarlyRelease = ("early_release", False, ht.TBool,
124 "Whether to release locks as soon as possible")
126 _PIpCheckDoc = "Whether to ensure instance's IP address is inactive"
128 #: Do not remember instance state changes
129 _PNoRemember = ("no_remember", False, ht.TBool,
130 "Do not remember the state change")
132 #: Target node for instance migration/failover
133 _PMigrationTargetNode = ("target_node", None, ht.TMaybeString,
134 "Target node for shared-storage instances")
136 _PStartupPaused = ("startup_paused", False, ht.TBool,
137 "Pause instance at startup")
139 _PVerbose = ("verbose", False, ht.TBool, "Verbose mode")
141 # Parameters for cluster verification
142 _PDebugSimulateErrors = ("debug_simulate_errors", False, ht.TBool,
143 "Whether to simulate errors (useful for debugging)")
144 _PErrorCodes = ("error_codes", False, ht.TBool, "Error codes")
145 _PSkipChecks = ("skip_checks", ht.EmptyList,
146 ht.TListOf(ht.TElemOf(constants.VERIFY_OPTIONAL_CHECKS)),
147 "Which checks to skip")
148 _PIgnoreErrors = ("ignore_errors", ht.EmptyList,
149 ht.TListOf(ht.TElemOf(constants.CV_ALL_ECODES_STRINGS)),
150 "List of error codes that should be treated as warnings")
153 _PDiskParams = ("diskparams", None,
155 ht.TDictOf(ht.TElemOf(constants.DISK_TEMPLATES), ht.TDict),
157 "Disk templates' parameter defaults")
159 # Parameters for node resource model
160 _PHvState = ("hv_state", None, ht.TMaybeDict, "Set hypervisor states")
161 _PDiskState = ("disk_state", None, ht.TMaybeDict, "Set disk states")
164 _PIgnoreIpolicy = ("ignore_ipolicy", False, ht.TBool,
165 "Whether to ignore ipolicy violations")
167 # Allow runtime changes while migrating
168 _PAllowRuntimeChgs = ("allow_runtime_changes", True, ht.TBool,
169 "Allow runtime changes (eg. memory ballooning)")
171 #: a required network name
172 _PNetworkName = ("network_name", ht.NoDefault, ht.TNonEmptyString,
175 #: OP_ID conversion regular expression
176 _OPID_RE = re.compile("([a-z])([A-Z])")
178 #: Utility function for L{OpClusterSetParams}
179 _TestClusterOsListItem = \
180 ht.TAnd(ht.TIsLength(2), ht.TItems([
181 ht.TElemOf(constants.DDMS_VALUES),
185 _TestClusterOsList = ht.TMaybeListOf(_TestClusterOsListItem)
187 # TODO: Generate check from constants.INIC_PARAMS_TYPES
188 #: Utility function for testing NIC definitions
190 ht.Comment("NIC parameters")(ht.TDictOf(ht.TElemOf(constants.INIC_PARAMS),
191 ht.TOr(ht.TNone, ht.TNonEmptyString)))
193 _TSetParamsResultItemItems = [
194 ht.Comment("name of changed parameter")(ht.TNonEmptyString),
195 ht.Comment("new value")(ht.TAny),
198 _TSetParamsResult = \
199 ht.TListOf(ht.TAnd(ht.TIsLength(len(_TSetParamsResultItemItems)),
200 ht.TItems(_TSetParamsResultItemItems)))
202 # TODO: Generate check from constants.IDISK_PARAMS_TYPES (however, not all users
203 # of this check support all parameters)
205 ht.Comment("Disk parameters")(ht.TDictOf(ht.TElemOf(constants.IDISK_PARAMS),
206 ht.TOr(ht.TNonEmptyString, ht.TInt)))
209 ht.TListOf(ht.TAnd(ht.TIsLength(2),
210 ht.TItems([ht.TElemOf(constants.RS_ALL),
213 _TQueryResult = ht.TListOf(_TQueryRow)
215 _TOldQueryRow = ht.TListOf(ht.TAny)
217 _TOldQueryResult = ht.TListOf(_TOldQueryRow)
227 #: Attribute name for dependencies
228 DEPEND_ATTR = "depends"
230 #: Attribute name for comment
231 COMMENT_ATTR = "comment"
235 """Convert an opcode class name to an OP_ID.
238 @param name: the class name, as OpXxxYyy
240 @return: the name in the OP_XXXX_YYYY format
243 if not name.startswith("Op"):
245 # Note: (?<=[a-z])(?=[A-Z]) would be ideal, since it wouldn't
246 # consume any input, and hence we would just have all the elements
247 # in the list, one by one; but it seems that split doesn't work on
248 # non-consuming input, hence we have to process the input string a
250 name = _OPID_RE.sub(r"\1,\2", name)
251 elems = name.split(",")
252 return "_".join(n.upper() for n in elems)
255 def _GenerateObjectTypeCheck(obj, fields_types):
256 """Helper to generate type checks for objects.
258 @param obj: The object to generate type checks
259 @param fields_types: The fields and their types as a dict
260 @return: A ht type check function
263 assert set(obj.GetAllSlots()) == set(fields_types.keys()), \
264 "%s != %s" % (set(obj.GetAllSlots()), set(fields_types.keys()))
265 return ht.TStrictDict(True, True, fields_types)
269 _GenerateObjectTypeCheck(objects.QueryFieldDefinition, {
270 "name": ht.TNonEmptyString,
271 "title": ht.TNonEmptyString,
272 "kind": ht.TElemOf(constants.QFT_ALL),
273 "doc": ht.TNonEmptyString,
277 def RequireFileStorage():
278 """Checks that file storage is enabled.
280 While it doesn't really fit into this module, L{utils} was deemed too large
281 of a dependency to be imported for just one or two functions.
283 @raise errors.OpPrereqError: when file storage is disabled
286 if not constants.ENABLE_FILE_STORAGE:
287 raise errors.OpPrereqError("File storage disabled at configure time",
291 def RequireSharedFileStorage():
292 """Checks that shared file storage is enabled.
294 While it doesn't really fit into this module, L{utils} was deemed too large
295 of a dependency to be imported for just one or two functions.
297 @raise errors.OpPrereqError: when shared file storage is disabled
300 if not constants.ENABLE_SHARED_FILE_STORAGE:
301 raise errors.OpPrereqError("Shared file storage disabled at"
302 " configure time", errors.ECODE_INVAL)
305 @ht.WithDesc("CheckFileStorage")
306 def _CheckFileStorage(value):
307 """Ensures file storage is enabled if used.
310 if value == constants.DT_FILE:
312 elif value == constants.DT_SHARED_FILE:
313 RequireSharedFileStorage()
317 def _BuildDiskTemplateCheck(accept_none):
318 """Builds check for disk template.
320 @type accept_none: bool
321 @param accept_none: whether to accept None as a correct value
325 template_check = ht.TElemOf(constants.DISK_TEMPLATES)
328 template_check = ht.TOr(template_check, ht.TNone)
330 return ht.TAnd(template_check, _CheckFileStorage)
333 def _CheckStorageType(storage_type):
334 """Ensure a given storage type is valid.
337 if storage_type not in constants.VALID_STORAGE_TYPES:
338 raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
340 if storage_type == constants.ST_FILE:
341 # TODO: What about shared file storage?
346 #: Storage type parameter
347 _PStorageType = ("storage_type", ht.NoDefault, _CheckStorageType,
350 _CheckNetworkType = ht.TElemOf(constants.NETWORK_VALID_TYPES)
352 #: Network type parameter
353 _PNetworkType = ("network_type", None, ht.TOr(ht.TNone, _CheckNetworkType),
356 def _CheckCIDRNetNotation(value):
357 """Ensure a given cidr notation type is valid.
361 ipaddr.IPv4Network(value)
362 except ipaddr.AddressValueError:
366 def _CheckCIDRAddrNotation(value):
367 """Ensure a given cidr notation type is valid.
371 ipaddr.IPv4Address(value)
372 except ipaddr.AddressValueError:
376 def _CheckCIDR6AddrNotation(value):
377 """Ensure a given cidr notation type is valid.
381 ipaddr.IPv6Address(value)
382 except ipaddr.AddressValueError:
386 def _CheckCIDR6NetNotation(value):
387 """Ensure a given cidr notation type is valid.
391 ipaddr.IPv6Network(value)
392 except ipaddr.AddressValueError:
396 class _AutoOpParamSlots(objectutils.AutoSlots):
397 """Meta class for opcode definitions.
400 def __new__(mcs, name, bases, attrs):
401 """Called when a class should be created.
403 @param mcs: The meta class
404 @param name: Name of created class
405 @param bases: Base classes
407 @param attrs: Class attributes
410 assert "OP_ID" not in attrs, "Class '%s' defining OP_ID" % name
412 slots = mcs._GetSlots(attrs)
413 assert "OP_DSC_FIELD" not in attrs or attrs["OP_DSC_FIELD"] in slots, \
414 "Class '%s' uses unknown field in OP_DSC_FIELD" % name
416 attrs["OP_ID"] = _NameToId(name)
418 return objectutils.AutoSlots.__new__(mcs, name, bases, attrs)
421 def _GetSlots(mcs, attrs):
422 """Build the slots out of OP_PARAMS.
425 # Always set OP_PARAMS to avoid duplicates in BaseOpCode.GetAllParams
426 params = attrs.setdefault("OP_PARAMS", [])
428 # Use parameter names as slots
429 return [pname for (pname, _, _, _) in params]
432 class BaseOpCode(objectutils.ValidatedSlots):
433 """A simple serializable object.
435 This object serves as a parent class for OpCode without any custom
439 # pylint: disable=E1101
440 # as OP_ID is dynamically defined
441 __metaclass__ = _AutoOpParamSlots
443 def __getstate__(self):
444 """Generic serializer.
446 This method just returns the contents of the instance as a
450 @return: the instance attributes and their values
454 for name in self.GetAllSlots():
455 if hasattr(self, name):
456 state[name] = getattr(self, name)
459 def __setstate__(self, state):
460 """Generic unserializer.
462 This method just restores from the serialized state the attributes
463 of the current instance.
465 @param state: the serialized opcode data
469 if not isinstance(state, dict):
470 raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
473 for name in self.GetAllSlots():
474 if name not in state and hasattr(self, name):
478 setattr(self, name, state[name])
481 def GetAllParams(cls):
482 """Compute list of all parameters for an opcode.
486 for parent in cls.__mro__:
487 slots.extend(getattr(parent, "OP_PARAMS", []))
490 def Validate(self, set_defaults): # pylint: disable=W0221
491 """Validate opcode parameters, optionally setting default values.
493 @type set_defaults: bool
494 @param set_defaults: Whether to set default values
495 @raise errors.OpPrereqError: When a parameter value doesn't match
499 for (attr_name, default, test, _) in self.GetAllParams():
500 assert test == ht.NoType or callable(test)
502 if not hasattr(self, attr_name):
503 if default == ht.NoDefault:
504 raise errors.OpPrereqError("Required parameter '%s.%s' missing" %
505 (self.OP_ID, attr_name),
508 if callable(default):
512 setattr(self, attr_name, dval)
514 if test == ht.NoType:
518 if set_defaults or hasattr(self, attr_name):
519 attr_val = getattr(self, attr_name)
520 if not test(attr_val):
521 logging.error("OpCode %s, parameter %s, has invalid type %s/value"
522 " '%s' expecting type %s",
523 self.OP_ID, attr_name, type(attr_val), attr_val, test)
524 raise errors.OpPrereqError("Parameter '%s.%s' fails validation" %
525 (self.OP_ID, attr_name),
529 def _BuildJobDepCheck(relative):
530 """Builds check for job dependencies (L{DEPEND_ATTR}).
533 @param relative: Whether to accept relative job IDs (negative)
538 job_id = ht.TOr(ht.TJobId, ht.TRelativeJobId)
543 ht.TAnd(ht.TIsLength(2),
545 ht.TListOf(ht.TElemOf(constants.JOBS_FINALIZED))]))
547 return ht.TMaybeListOf(job_dep)
550 TNoRelativeJobDependencies = _BuildJobDepCheck(False)
552 #: List of submission status and job ID as returned by C{SubmitManyJobs}
554 ht.TAnd(ht.TIsLength(2),
555 ht.TItems([ht.Comment("success")(ht.TBool),
556 ht.Comment("Job ID if successful, error message"
557 " otherwise")(ht.TOr(ht.TString,
559 TJobIdList = ht.TListOf(_TJobIdListItem)
561 #: Result containing only list of submitted jobs
562 TJobIdListOnly = ht.TStrictDict(True, True, {
563 constants.JOB_IDS_KEY: ht.Comment("List of submitted jobs")(TJobIdList),
567 class OpCode(BaseOpCode):
570 This is the root of the actual OpCode hierarchy. All clases derived
571 from this class should override OP_ID.
573 @cvar OP_ID: The ID of this opcode. This should be unique amongst all
574 children of this class.
575 @cvar OP_DSC_FIELD: The name of a field whose value will be included in the
576 string returned by Summary(); see the docstring of that
578 @cvar OP_PARAMS: List of opcode attributes, the default values they should
579 get if not already defined, and types they must match.
580 @cvar OP_RESULT: Callable to verify opcode result
581 @cvar WITH_LU: Boolean that specifies whether this should be included in
582 mcpu's dispatch table
583 @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
585 @ivar priority: Opcode priority for queue
588 # pylint: disable=E1101
589 # as OP_ID is dynamically defined
592 ("dry_run", None, ht.TMaybeBool, "Run checks only, don't execute"),
593 ("debug_level", None, ht.TOr(ht.TNone, ht.TNonNegativeInt), "Debug level"),
594 ("priority", constants.OP_PRIO_DEFAULT,
595 ht.TElemOf(constants.OP_PRIO_SUBMIT_VALID), "Opcode priority"),
596 (DEPEND_ATTR, None, _BuildJobDepCheck(True),
597 "Job dependencies; if used through ``SubmitManyJobs`` relative (negative)"
598 " job IDs can be used; see :doc:`design document <design-chained-jobs>`"
600 (COMMENT_ATTR, None, ht.TMaybeString,
601 "Comment describing the purpose of the opcode"),
605 def __getstate__(self):
606 """Specialized getstate for opcodes.
608 This method adds to the state dictionary the OP_ID of the class,
609 so that on unload we can identify the correct class for
610 instantiating the opcode.
613 @return: the state as a dictionary
616 data = BaseOpCode.__getstate__(self)
617 data["OP_ID"] = self.OP_ID
621 def LoadOpCode(cls, data):
622 """Generic load opcode method.
624 The method identifies the correct opcode class from the dict-form
625 by looking for a OP_ID key, if this is not found, or its value is
626 not available in this module as a child of this class, we fail.
629 @param data: the serialized opcode
632 if not isinstance(data, dict):
633 raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
634 if "OP_ID" not in data:
635 raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
636 op_id = data["OP_ID"]
638 if op_id in OP_MAPPING:
639 op_class = OP_MAPPING[op_id]
641 raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
644 new_data = data.copy()
645 del new_data["OP_ID"]
646 op.__setstate__(new_data)
650 """Generates a summary description of this opcode.
652 The summary is the value of the OP_ID attribute (without the "OP_"
653 prefix), plus the value of the OP_DSC_FIELD attribute, if one was
654 defined; this field should allow to easily identify the operation
655 (for an instance creation job, e.g., it would be the instance
659 assert self.OP_ID is not None and len(self.OP_ID) > 3
660 # all OP_ID start with OP_, we remove that
662 field_name = getattr(self, "OP_DSC_FIELD", None)
664 field_value = getattr(self, field_name, None)
665 if isinstance(field_value, (list, tuple)):
666 field_value = ",".join(str(i) for i in field_value)
667 txt = "%s(%s)" % (txt, field_value)
670 def TinySummary(self):
671 """Generates a compact summary description of the opcode.
674 assert self.OP_ID.startswith("OP_")
676 text = self.OP_ID[3:]
678 for (prefix, supplement) in _SUMMARY_PREFIX.items():
679 if text.startswith(prefix):
680 return supplement + text[len(prefix):]
687 class OpClusterPostInit(OpCode):
688 """Post cluster initialization.
690 This opcode does not touch the cluster at all. Its purpose is to run hooks
691 after the cluster has been initialized.
697 class OpClusterDestroy(OpCode):
698 """Destroy the cluster.
700 This opcode has no other parameters. All the state is irreversibly
701 lost after the execution of this opcode.
704 OP_RESULT = ht.TNonEmptyString
707 class OpClusterQuery(OpCode):
708 """Query cluster information."""
709 OP_RESULT = ht.TDictOf(ht.TNonEmptyString, ht.TAny)
712 class OpClusterVerify(OpCode):
713 """Submits all jobs necessary to verify the cluster.
717 _PDebugSimulateErrors,
722 ("group_name", None, ht.TMaybeString, "Group to verify")
724 OP_RESULT = TJobIdListOnly
727 class OpClusterVerifyConfig(OpCode):
728 """Verify the cluster config.
732 _PDebugSimulateErrors,
740 class OpClusterVerifyGroup(OpCode):
741 """Run verify on a node group from the cluster.
743 @type skip_checks: C{list}
744 @ivar skip_checks: steps to be skipped from the verify process; this
745 needs to be a subset of
746 L{constants.VERIFY_OPTIONAL_CHECKS}; currently
747 only L{constants.VERIFY_NPLUSONE_MEM} can be passed
750 OP_DSC_FIELD = "group_name"
753 _PDebugSimulateErrors,
762 class OpClusterVerifyDisks(OpCode):
763 """Verify the cluster disks.
766 OP_RESULT = TJobIdListOnly
769 class OpGroupVerifyDisks(OpCode):
770 """Verifies the status of all disks in a node group.
772 Result: a tuple of three elements:
773 - dict of node names with issues (values: error msg)
774 - list of instances with degraded disks (that should be activated)
775 - dict of instances with missing logical volumes (values: (node, vol)
776 pairs with details about the missing volumes)
778 In normal operation, all lists should be empty. A non-empty instance
779 list (3rd element of the result) is still ok (errors were fixed) but
780 non-empty node list means some node is down, and probably there are
781 unfixable drbd errors.
783 Note that only instances that are drbd-based are taken into
784 consideration. This might need to be revisited in the future.
787 OP_DSC_FIELD = "group_name"
792 ht.TAnd(ht.TIsLength(3),
793 ht.TItems([ht.TDictOf(ht.TString, ht.TString),
794 ht.TListOf(ht.TString),
795 ht.TDictOf(ht.TString,
796 ht.TListOf(ht.TListOf(ht.TString)))]))
799 class OpClusterRepairDiskSizes(OpCode):
800 """Verify the disk sizes of the instances and fixes configuration
803 Parameters: optional instances list, in case we want to restrict the
804 checks to only a subset of the instances.
806 Result: a list of tuples, (instance, disk, new-size) for changed
809 In normal operation, the list should be empty.
811 @type instances: list
812 @ivar instances: the list of instances to check, or empty for all instances
816 ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
818 OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(3),
819 ht.TItems([ht.TNonEmptyString,
821 ht.TNonNegativeInt])))
824 class OpClusterConfigQuery(OpCode):
825 """Query cluster configuration values."""
829 OP_RESULT = ht.TListOf(ht.TAny)
832 class OpClusterRename(OpCode):
833 """Rename the cluster.
836 @ivar name: The new name of the cluster. The name and/or the master IP
837 address will be changed to match the new name and its IP
841 OP_DSC_FIELD = "name"
843 ("name", ht.NoDefault, ht.TNonEmptyString, None),
845 OP_RESULT = ht.TNonEmptyString
848 class OpClusterSetParams(OpCode):
849 """Change the parameters of the cluster.
851 @type vg_name: C{str} or C{None}
852 @ivar vg_name: The new volume group name or None to disable LVM usage.
858 ("vg_name", None, ht.TOr(ht.TString, ht.TNone), "Volume group name"),
859 ("enabled_hypervisors", None,
860 ht.TOr(ht.TAnd(ht.TListOf(ht.TElemOf(constants.HYPER_TYPES)), ht.TTrue),
862 "List of enabled hypervisors"),
863 ("hvparams", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
865 "Cluster-wide hypervisor parameter defaults, hypervisor-dependent"),
866 ("beparams", None, ht.TOr(ht.TDict, ht.TNone),
867 "Cluster-wide backend parameter defaults"),
868 ("os_hvp", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
870 "Cluster-wide per-OS hypervisor parameter defaults"),
871 ("osparams", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
873 "Cluster-wide OS parameter defaults"),
875 ("candidate_pool_size", None, ht.TOr(ht.TPositiveInt, ht.TNone),
876 "Master candidate pool size"),
877 ("uid_pool", None, ht.NoType,
878 "Set UID pool, must be list of lists describing UID ranges (two items,"
879 " start and end inclusive)"),
880 ("add_uids", None, ht.NoType,
881 "Extend UID pool, must be list of lists describing UID ranges (two"
882 " items, start and end inclusive) to be added"),
883 ("remove_uids", None, ht.NoType,
884 "Shrink UID pool, must be list of lists describing UID ranges (two"
885 " items, start and end inclusive) to be removed"),
886 ("maintain_node_health", None, ht.TMaybeBool,
887 "Whether to automatically maintain node health"),
888 ("prealloc_wipe_disks", None, ht.TMaybeBool,
889 "Whether to wipe disks before allocating them to instances"),
890 ("nicparams", None, ht.TMaybeDict, "Cluster-wide NIC parameter defaults"),
891 ("ndparams", None, ht.TMaybeDict, "Cluster-wide node parameter defaults"),
892 ("ipolicy", None, ht.TMaybeDict,
893 "Cluster-wide :ref:`instance policy <rapi-ipolicy>` specs"),
894 ("drbd_helper", None, ht.TOr(ht.TString, ht.TNone), "DRBD helper program"),
895 ("default_iallocator", None, ht.TOr(ht.TString, ht.TNone),
896 "Default iallocator for cluster"),
897 ("master_netdev", None, ht.TOr(ht.TString, ht.TNone),
898 "Master network device"),
899 ("master_netmask", None, ht.TOr(ht.TInt, ht.TNone),
900 "Netmask of the master IP"),
901 ("reserved_lvs", None, ht.TMaybeListOf(ht.TNonEmptyString),
902 "List of reserved LVs"),
903 ("hidden_os", None, _TestClusterOsList,
904 "Modify list of hidden operating systems: each modification must have"
905 " two items, the operation and the OS name; the operation can be"
906 " ``%s`` or ``%s``" % (constants.DDM_ADD, constants.DDM_REMOVE)),
907 ("blacklisted_os", None, _TestClusterOsList,
908 "Modify list of blacklisted operating systems: each modification must"
909 " have two items, the operation and the OS name; the operation can be"
910 " ``%s`` or ``%s``" % (constants.DDM_ADD, constants.DDM_REMOVE)),
911 ("use_external_mip_script", None, ht.TMaybeBool,
912 "Whether to use an external master IP address setup script"),
917 class OpClusterRedistConf(OpCode):
918 """Force a full push of the cluster configuration.
924 class OpClusterActivateMasterIp(OpCode):
925 """Activate the master IP on the master node.
931 class OpClusterDeactivateMasterIp(OpCode):
932 """Deactivate the master IP on the master node.
938 class OpQuery(OpCode):
939 """Query for resources/items.
941 @ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
942 @ivar fields: List of fields to retrieve
943 @ivar qfilter: Query filter
946 OP_DSC_FIELD = "what"
950 ("fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
952 ("qfilter", None, ht.TOr(ht.TNone, ht.TList),
956 _GenerateObjectTypeCheck(objects.QueryResponse, {
957 "fields": ht.TListOf(_TQueryFieldDef),
958 "data": _TQueryResult,
962 class OpQueryFields(OpCode):
963 """Query for available resource/item fields.
965 @ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
966 @ivar fields: List of fields to retrieve
969 OP_DSC_FIELD = "what"
972 ("fields", None, ht.TMaybeListOf(ht.TNonEmptyString),
973 "Requested fields; if not given, all are returned"),
976 _GenerateObjectTypeCheck(objects.QueryFieldsResponse, {
977 "fields": ht.TListOf(_TQueryFieldDef),
981 class OpOobCommand(OpCode):
982 """Interact with OOB."""
984 ("node_names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
985 "List of nodes to run the OOB command against"),
986 ("command", None, ht.TElemOf(constants.OOB_COMMANDS),
987 "OOB command to be run"),
988 ("timeout", constants.OOB_TIMEOUT, ht.TInt,
989 "Timeout before the OOB helper will be terminated"),
990 ("ignore_status", False, ht.TBool,
991 "Ignores the node offline status for power off"),
992 ("power_delay", constants.OOB_POWER_DELAY, ht.TNonNegativeFloat,
993 "Time in seconds to wait between powering on nodes"),
995 # Fixme: Make it more specific with all the special cases in LUOobCommand
996 OP_RESULT = _TQueryResult
999 class OpRestrictedCommand(OpCode):
1000 """Runs a restricted command on node(s).
1005 ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
1006 "Nodes on which the command should be run (at least one)"),
1007 ("command", ht.NoDefault, ht.TNonEmptyString,
1008 "Command name (no parameters)"),
1012 ht.Comment("success")(ht.TBool),
1013 ht.Comment("output or error message")(ht.TString),
1017 ht.TListOf(ht.TAnd(ht.TIsLength(len(_RESULT_ITEMS)),
1018 ht.TItems(_RESULT_ITEMS)))
1023 class OpNodeRemove(OpCode):
1026 @type node_name: C{str}
1027 @ivar node_name: The name of the node to remove. If the node still has
1028 instances on it, the operation will fail.
1031 OP_DSC_FIELD = "node_name"
1035 OP_RESULT = ht.TNone
1038 class OpNodeAdd(OpCode):
1039 """Add a node to the cluster.
1041 @type node_name: C{str}
1042 @ivar node_name: The name of the node to add. This can be a short name,
1043 but it will be expanded to the FQDN.
1044 @type primary_ip: IP address
1045 @ivar primary_ip: The primary IP of the node. This will be ignored when the
1046 opcode is submitted, but will be filled during the node
1047 add (so it will be visible in the job query).
1048 @type secondary_ip: IP address
1049 @ivar secondary_ip: The secondary IP of the node. This needs to be passed
1050 if the cluster has been initialized in 'dual-network'
1051 mode, otherwise it must not be given.
1052 @type readd: C{bool}
1053 @ivar readd: Whether to re-add an existing node to the cluster. If
1054 this is not passed, then the operation will abort if the node
1055 name is already in the cluster; use this parameter to 'repair'
1056 a node that had its configuration broken, or was reinstalled
1057 without removal from the cluster.
1059 @ivar group: The node group to which this node will belong.
1060 @type vm_capable: C{bool}
1061 @ivar vm_capable: The vm_capable node attribute
1062 @type master_capable: C{bool}
1063 @ivar master_capable: The master_capable node attribute
1066 OP_DSC_FIELD = "node_name"
1071 ("primary_ip", None, ht.NoType, "Primary IP address"),
1072 ("secondary_ip", None, ht.TMaybeString, "Secondary IP address"),
1073 ("readd", False, ht.TBool, "Whether node is re-added to cluster"),
1074 ("group", None, ht.TMaybeString, "Initial node group"),
1075 ("master_capable", None, ht.TMaybeBool,
1076 "Whether node can become master or master candidate"),
1077 ("vm_capable", None, ht.TMaybeBool,
1078 "Whether node can host instances"),
1079 ("ndparams", None, ht.TMaybeDict, "Node parameters"),
1081 OP_RESULT = ht.TNone
1084 class OpNodeQuery(OpCode):
1085 """Compute the list of nodes."""
1089 ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1090 "Empty list to query all nodes, node names otherwise"),
1092 OP_RESULT = _TOldQueryResult
1095 class OpNodeQueryvols(OpCode):
1096 """Get list of volumes on node."""
1099 ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1100 "Empty list to query all nodes, node names otherwise"),
1102 OP_RESULT = ht.TListOf(ht.TAny)
1105 class OpNodeQueryStorage(OpCode):
1106 """Get information on storage for node(s)."""
1110 ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "List of nodes"),
1111 ("name", None, ht.TMaybeString, "Storage name"),
1113 OP_RESULT = _TOldQueryResult
1116 class OpNodeModifyStorage(OpCode):
1117 """Modifies the properies of a storage unit"""
1118 OP_DSC_FIELD = "node_name"
1123 ("changes", ht.NoDefault, ht.TDict, "Requested changes"),
1125 OP_RESULT = ht.TNone
1128 class OpRepairNodeStorage(OpCode):
1129 """Repairs the volume group on a node."""
1130 OP_DSC_FIELD = "node_name"
1135 _PIgnoreConsistency,
1137 OP_RESULT = ht.TNone
1140 class OpNodeSetParams(OpCode):
1141 """Change the parameters of a node."""
1142 OP_DSC_FIELD = "node_name"
1148 ("master_candidate", None, ht.TMaybeBool,
1149 "Whether the node should become a master candidate"),
1150 ("offline", None, ht.TMaybeBool,
1151 "Whether the node should be marked as offline"),
1152 ("drained", None, ht.TMaybeBool,
1153 "Whether the node should be marked as drained"),
1154 ("auto_promote", False, ht.TBool,
1155 "Whether node(s) should be promoted to master candidate if necessary"),
1156 ("master_capable", None, ht.TMaybeBool,
1157 "Denote whether node can become master or master candidate"),
1158 ("vm_capable", None, ht.TMaybeBool,
1159 "Denote whether node can host instances"),
1160 ("secondary_ip", None, ht.TMaybeString,
1161 "Change node's secondary IP address"),
1162 ("ndparams", None, ht.TMaybeDict, "Set node parameters"),
1163 ("powered", None, ht.TMaybeBool,
1164 "Whether the node should be marked as powered"),
1166 OP_RESULT = _TSetParamsResult
1169 class OpNodePowercycle(OpCode):
1170 """Tries to powercycle a node."""
1171 OP_DSC_FIELD = "node_name"
1176 OP_RESULT = ht.TMaybeString
1179 class OpNodeMigrate(OpCode):
1180 """Migrate all instances from a node."""
1181 OP_DSC_FIELD = "node_name"
1186 _PMigrationTargetNode,
1189 ("iallocator", None, ht.TMaybeString,
1190 "Iallocator for deciding the target node for shared-storage instances"),
1192 OP_RESULT = TJobIdListOnly
1195 class OpNodeEvacuate(OpCode):
1196 """Evacuate instances off a number of nodes."""
1197 OP_DSC_FIELD = "node_name"
1201 ("remote_node", None, ht.TMaybeString, "New secondary node"),
1202 ("iallocator", None, ht.TMaybeString, "Iallocator for computing solution"),
1203 ("mode", ht.NoDefault, ht.TElemOf(constants.NODE_EVAC_MODES),
1204 "Node evacuation mode"),
1206 OP_RESULT = TJobIdListOnly
1211 class OpInstanceCreate(OpCode):
1212 """Create an instance.
1214 @ivar instance_name: Instance name
1215 @ivar mode: Instance creation mode (one of L{constants.INSTANCE_CREATE_MODES})
1216 @ivar source_handshake: Signed handshake from source (remote import only)
1217 @ivar source_x509_ca: Source X509 CA in PEM format (remote import only)
1218 @ivar source_instance_name: Previous name of instance (remote import only)
1219 @ivar source_shutdown_timeout: Shutdown timeout used for source instance
1220 (remote import only)
1223 OP_DSC_FIELD = "instance_name"
1230 ("beparams", ht.EmptyDict, ht.TDict, "Backend parameters for instance"),
1231 ("disks", ht.NoDefault, ht.TListOf(_TDiskParams),
1232 "Disk descriptions, for example ``[{\"%s\": 100}, {\"%s\": 5}]``;"
1233 " each disk definition must contain a ``%s`` value and"
1234 " can contain an optional ``%s`` value denoting the disk access mode"
1236 (constants.IDISK_SIZE, constants.IDISK_SIZE, constants.IDISK_SIZE,
1237 constants.IDISK_MODE,
1238 " or ".join("``%s``" % i for i in sorted(constants.DISK_ACCESS_SET)))),
1239 ("disk_template", ht.NoDefault, _BuildDiskTemplateCheck(True),
1241 ("file_driver", None, ht.TOr(ht.TNone, ht.TElemOf(constants.FILE_DRIVER)),
1242 "Driver for file-backed disks"),
1243 ("file_storage_dir", None, ht.TMaybeString,
1244 "Directory for storing file-backed disks"),
1245 ("hvparams", ht.EmptyDict, ht.TDict,
1246 "Hypervisor parameters for instance, hypervisor-dependent"),
1247 ("hypervisor", None, ht.TMaybeString, "Hypervisor"),
1248 ("iallocator", None, ht.TMaybeString,
1249 "Iallocator for deciding which node(s) to use"),
1250 ("identify_defaults", False, ht.TBool,
1251 "Reset instance parameters to default if equal"),
1252 ("ip_check", True, ht.TBool, _PIpCheckDoc),
1253 ("conflicts_check", True, ht.TBool, "Check for conflicting IPs"),
1254 ("mode", ht.NoDefault, ht.TElemOf(constants.INSTANCE_CREATE_MODES),
1255 "Instance creation mode"),
1256 ("nics", ht.NoDefault, ht.TListOf(_TestNicDef),
1257 "List of NIC (network interface) definitions, for example"
1258 " ``[{}, {}, {\"%s\": \"198.51.100.4\"}]``; each NIC definition can"
1259 " contain the optional values %s" %
1261 ", ".join("``%s``" % i for i in sorted(constants.INIC_PARAMS)))),
1262 ("no_install", None, ht.TMaybeBool,
1263 "Do not install the OS (will disable automatic start)"),
1264 ("osparams", ht.EmptyDict, ht.TDict, "OS parameters for instance"),
1265 ("os_type", None, ht.TMaybeString, "Operating system"),
1266 ("pnode", None, ht.TMaybeString, "Primary node"),
1267 ("snode", None, ht.TMaybeString, "Secondary node"),
1268 ("source_handshake", None, ht.TOr(ht.TList, ht.TNone),
1269 "Signed handshake from source (remote import only)"),
1270 ("source_instance_name", None, ht.TMaybeString,
1271 "Source instance name (remote import only)"),
1272 ("source_shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT,
1274 "How long source instance was given to shut down (remote import only)"),
1275 ("source_x509_ca", None, ht.TMaybeString,
1276 "Source X509 CA in PEM format (remote import only)"),
1277 ("src_node", None, ht.TMaybeString, "Source node for import"),
1278 ("src_path", None, ht.TMaybeString, "Source directory for import"),
1279 ("start", True, ht.TBool, "Whether to start instance after creation"),
1280 ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "Instance tags"),
1282 OP_RESULT = ht.Comment("instance nodes")(ht.TListOf(ht.TNonEmptyString))
1285 class OpInstanceMultiAlloc(OpCode):
1286 """Allocates multiple instances.
1290 ("iallocator", None, ht.TMaybeString,
1291 "Iallocator used to allocate all the instances"),
1292 ("instances", [], ht.TListOf(ht.TInstanceOf(OpInstanceCreate)),
1293 "List of instance create opcodes describing the instances to allocate"),
1295 _JOB_LIST = ht.Comment("List of submitted jobs")(TJobIdList)
1296 ALLOCATABLE_KEY = "allocatable"
1297 FAILED_KEY = "allocatable"
1298 OP_RESULT = ht.TStrictDict(True, True, {
1299 constants.JOB_IDS_KEY: _JOB_LIST,
1300 ALLOCATABLE_KEY: ht.TListOf(ht.TNonEmptyString),
1301 FAILED_KEY: ht.TListOf(ht.TNonEmptyString)
1304 def __getstate__(self):
1305 """Generic serializer.
1308 state = OpCode.__getstate__(self)
1309 if hasattr(self, "instances"):
1310 # pylint: disable=E1101
1311 state["instances"] = [inst.__getstate__() for inst in self.instances]
1314 def __setstate__(self, state):
1315 """Generic unserializer.
1317 This method just restores from the serialized state the attributes
1318 of the current instance.
1320 @param state: the serialized opcode data
1321 @type state: C{dict}
1324 if not isinstance(state, dict):
1325 raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
1328 if "instances" in state:
1329 insts = [OpCode.LoadOpCode(inst) for inst in state["instances"]]
1330 state["instances"] = insts
1331 return OpCode.__setstate__(self, state)
1333 def Validate(self, set_defaults):
1334 """Validates this opcode.
1336 We do this recursively.
1339 OpCode.Validate(self, set_defaults)
1341 for inst in self.instances: # pylint: disable=E1101
1342 inst.Validate(set_defaults)
1345 class OpInstanceReinstall(OpCode):
1346 """Reinstall an instance's OS."""
1347 OP_DSC_FIELD = "instance_name"
1351 ("os_type", None, ht.TMaybeString, "Instance operating system"),
1352 ("osparams", None, ht.TMaybeDict, "Temporary OS parameters"),
1354 OP_RESULT = ht.TNone
1357 class OpInstanceRemove(OpCode):
1358 """Remove an instance."""
1359 OP_DSC_FIELD = "instance_name"
1363 ("ignore_failures", False, ht.TBool,
1364 "Whether to ignore failures during removal"),
1366 OP_RESULT = ht.TNone
1369 class OpInstanceRename(OpCode):
1370 """Rename an instance."""
1374 ("new_name", ht.NoDefault, ht.TNonEmptyString, "New instance name"),
1375 ("ip_check", False, ht.TBool, _PIpCheckDoc),
1377 OP_RESULT = ht.Comment("New instance name")(ht.TNonEmptyString)
1380 class OpInstanceStartup(OpCode):
1381 """Startup an instance."""
1382 OP_DSC_FIELD = "instance_name"
1386 _PIgnoreOfflineNodes,
1387 ("hvparams", ht.EmptyDict, ht.TDict,
1388 "Temporary hypervisor parameters, hypervisor-dependent"),
1389 ("beparams", ht.EmptyDict, ht.TDict, "Temporary backend parameters"),
1393 OP_RESULT = ht.TNone
1396 class OpInstanceShutdown(OpCode):
1397 """Shutdown an instance."""
1398 OP_DSC_FIELD = "instance_name"
1401 _PIgnoreOfflineNodes,
1402 ("timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT, ht.TNonNegativeInt,
1403 "How long to wait for instance to shut down"),
1406 OP_RESULT = ht.TNone
1409 class OpInstanceReboot(OpCode):
1410 """Reboot an instance."""
1411 OP_DSC_FIELD = "instance_name"
1415 ("ignore_secondaries", False, ht.TBool,
1416 "Whether to start the instance even if secondary disks are failing"),
1417 ("reboot_type", ht.NoDefault, ht.TElemOf(constants.REBOOT_TYPES),
1418 "How to reboot instance"),
1420 OP_RESULT = ht.TNone
1423 class OpInstanceReplaceDisks(OpCode):
1424 """Replace the disks of an instance."""
1425 OP_DSC_FIELD = "instance_name"
1430 ("mode", ht.NoDefault, ht.TElemOf(constants.REPLACE_MODES),
1431 "Replacement mode"),
1432 ("disks", ht.EmptyList, ht.TListOf(ht.TNonNegativeInt),
1434 ("remote_node", None, ht.TMaybeString, "New secondary node"),
1435 ("iallocator", None, ht.TMaybeString,
1436 "Iallocator for deciding new secondary node"),
1438 OP_RESULT = ht.TNone
1441 class OpInstanceFailover(OpCode):
1442 """Failover an instance."""
1443 OP_DSC_FIELD = "instance_name"
1447 _PIgnoreConsistency,
1448 _PMigrationTargetNode,
1450 ("iallocator", None, ht.TMaybeString,
1451 "Iallocator for deciding the target node for shared-storage instances"),
1453 OP_RESULT = ht.TNone
1456 class OpInstanceMigrate(OpCode):
1457 """Migrate an instance.
1459 This migrates (without shutting down an instance) to its secondary
1462 @ivar instance_name: the name of the instance
1463 @ivar mode: the migration mode (live, non-live or None for auto)
1466 OP_DSC_FIELD = "instance_name"
1471 _PMigrationTargetNode,
1474 ("cleanup", False, ht.TBool,
1475 "Whether a previously failed migration should be cleaned up"),
1476 ("iallocator", None, ht.TMaybeString,
1477 "Iallocator for deciding the target node for shared-storage instances"),
1478 ("allow_failover", False, ht.TBool,
1479 "Whether we can fallback to failover if migration is not possible"),
1481 OP_RESULT = ht.TNone
1484 class OpInstanceMove(OpCode):
1485 """Move an instance.
1487 This move (with shutting down an instance and data copying) to an
1490 @ivar instance_name: the name of the instance
1491 @ivar target_node: the destination node
1494 OP_DSC_FIELD = "instance_name"
1499 ("target_node", ht.NoDefault, ht.TNonEmptyString, "Target node"),
1500 _PIgnoreConsistency,
1502 OP_RESULT = ht.TNone
1505 class OpInstanceConsole(OpCode):
1506 """Connect to an instance's console."""
1507 OP_DSC_FIELD = "instance_name"
1511 OP_RESULT = ht.TDict
1514 class OpInstanceActivateDisks(OpCode):
1515 """Activate an instance's disks."""
1516 OP_DSC_FIELD = "instance_name"
1519 ("ignore_size", False, ht.TBool, "Whether to ignore recorded size"),
1522 OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(3),
1523 ht.TItems([ht.TNonEmptyString,
1525 ht.TNonEmptyString])))
1528 class OpInstanceDeactivateDisks(OpCode):
1529 """Deactivate an instance's disks."""
1530 OP_DSC_FIELD = "instance_name"
1535 OP_RESULT = ht.TNone
1538 class OpInstanceRecreateDisks(OpCode):
1539 """Recreate an instance's disks."""
1541 ht.TAnd(ht.TIsLength(2),
1542 ht.TItems([ht.Comment("Disk index")(ht.TNonNegativeInt),
1543 ht.Comment("Parameters")(_TDiskParams)]))
1545 OP_DSC_FIELD = "instance_name"
1548 ("disks", ht.EmptyList,
1549 ht.TOr(ht.TListOf(ht.TNonNegativeInt), ht.TListOf(_TDiskChanges)),
1550 "List of disk indexes (deprecated) or a list of tuples containing a disk"
1551 " index and a possibly empty dictionary with disk parameter changes"),
1552 ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1553 "New instance nodes, if relocation is desired"),
1554 ("iallocator", None, ht.TMaybeString,
1555 "Iallocator for deciding new nodes"),
1557 OP_RESULT = ht.TNone
1560 class OpInstanceQuery(OpCode):
1561 """Compute the list of instances."""
1565 ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1566 "Empty list to query all instances, instance names otherwise"),
1568 OP_RESULT = _TOldQueryResult
1571 class OpInstanceQueryData(OpCode):
1572 """Compute the run-time status of instances."""
1575 ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1577 ("static", False, ht.TBool,
1578 "Whether to only return configuration data without querying"
1581 OP_RESULT = ht.TDictOf(ht.TNonEmptyString, ht.TDict)
1584 def _TestInstSetParamsModList(fn):
1585 """Generates a check for modification lists.
1589 # TODO: Remove in version 2.8 including support in LUInstanceSetParams
1591 ht.TAnd(ht.TIsLength(2), ht.TItems([
1592 ht.TOr(ht.TElemOf(constants.DDMS_VALUES), ht.TNonNegativeInt),
1596 # New format, supporting adding/removing disks/NICs at arbitrary indices
1598 ht.TAnd(ht.TIsLength(3), ht.TItems([
1599 ht.TElemOf(constants.DDMS_VALUES_WITH_MODIFY),
1600 ht.Comment("Disk index, can be negative, e.g. -1 for last disk")(ht.TInt),
1604 return ht.TOr(ht.Comment("Recommended")(ht.TListOf(mod_item_fn)),
1605 ht.Comment("Deprecated")(ht.TListOf(old_mod_item_fn)))
1608 class OpInstanceSetParams(OpCode):
1609 """Change the parameters of an instance.
1612 TestNicModifications = _TestInstSetParamsModList(_TestNicDef)
1613 TestDiskModifications = _TestInstSetParamsModList(_TDiskParams)
1615 OP_DSC_FIELD = "instance_name"
1621 ("nics", ht.EmptyList, TestNicModifications,
1622 "List of NIC changes: each item is of the form ``(op, index, settings)``,"
1623 " ``op`` is one of ``%s``, ``%s`` or ``%s``, ``index`` can be either -1"
1624 " to refer to the last position, or a zero-based index number; a"
1625 " deprecated version of this parameter used the form ``(op, settings)``,"
1626 " where ``op`` can be ``%s`` to add a new NIC with the specified"
1627 " settings, ``%s`` to remove the last NIC or a number to modify the"
1628 " settings of the NIC with that index" %
1629 (constants.DDM_ADD, constants.DDM_MODIFY, constants.DDM_REMOVE,
1630 constants.DDM_ADD, constants.DDM_REMOVE)),
1631 ("disks", ht.EmptyList, TestDiskModifications,
1632 "List of disk changes; see ``nics``"),
1633 ("beparams", ht.EmptyDict, ht.TDict, "Per-instance backend parameters"),
1634 ("runtime_mem", None, ht.TMaybePositiveInt, "New runtime memory"),
1635 ("hvparams", ht.EmptyDict, ht.TDict,
1636 "Per-instance hypervisor parameters, hypervisor-dependent"),
1637 ("disk_template", None, ht.TOr(ht.TNone, _BuildDiskTemplateCheck(False)),
1638 "Disk template for instance"),
1639 ("remote_node", None, ht.TMaybeString,
1640 "Secondary node (used when changing disk template)"),
1641 ("os_name", None, ht.TMaybeString,
1642 "Change the instance's OS without reinstalling the instance"),
1643 ("osparams", None, ht.TMaybeDict, "Per-instance OS parameters"),
1644 ("wait_for_sync", True, ht.TBool,
1645 "Whether to wait for the disk to synchronize, when changing template"),
1646 ("offline", None, ht.TMaybeBool, "Whether to mark instance as offline"),
1647 ("conflicts_check", True, ht.TBool, "Check for conflicting IPs"),
1649 OP_RESULT = _TSetParamsResult
1652 class OpInstanceGrowDisk(OpCode):
1653 """Grow a disk of an instance."""
1654 OP_DSC_FIELD = "instance_name"
1658 ("disk", ht.NoDefault, ht.TInt, "Disk index"),
1659 ("amount", ht.NoDefault, ht.TNonNegativeInt,
1660 "Amount of disk space to add (megabytes)"),
1661 ("absolute", False, ht.TBool,
1662 "Whether the amount parameter is an absolute target or a relative one"),
1664 OP_RESULT = ht.TNone
1667 class OpInstanceChangeGroup(OpCode):
1668 """Moves an instance to another node group."""
1669 OP_DSC_FIELD = "instance_name"
1673 ("iallocator", None, ht.TMaybeString, "Iallocator for computing solution"),
1674 ("target_groups", None, ht.TMaybeListOf(ht.TNonEmptyString),
1675 "Destination group names or UUIDs (defaults to \"all but current group\""),
1677 OP_RESULT = TJobIdListOnly
1680 # Node group opcodes
1682 class OpGroupAdd(OpCode):
1683 """Add a node group to the cluster."""
1684 OP_DSC_FIELD = "group_name"
1687 _PNodeGroupAllocPolicy,
1692 ("ipolicy", None, ht.TMaybeDict,
1693 "Group-wide :ref:`instance policy <rapi-ipolicy>` specs"),
1695 OP_RESULT = ht.TNone
1698 class OpGroupAssignNodes(OpCode):
1699 """Assign nodes to a node group."""
1700 OP_DSC_FIELD = "group_name"
1704 ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
1705 "List of nodes to assign"),
1707 OP_RESULT = ht.TNone
1710 class OpGroupQuery(OpCode):
1711 """Compute the list of node groups."""
1714 ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1715 "Empty list to query all groups, group names otherwise"),
1717 OP_RESULT = _TOldQueryResult
1720 class OpGroupSetParams(OpCode):
1721 """Change the parameters of a node group."""
1722 OP_DSC_FIELD = "group_name"
1725 _PNodeGroupAllocPolicy,
1730 ("ipolicy", None, ht.TMaybeDict, "Group-wide instance policy specs"),
1732 OP_RESULT = _TSetParamsResult
1735 class OpGroupRemove(OpCode):
1736 """Remove a node group from the cluster."""
1737 OP_DSC_FIELD = "group_name"
1741 OP_RESULT = ht.TNone
1744 class OpGroupRename(OpCode):
1745 """Rename a node group in the cluster."""
1748 ("new_name", ht.NoDefault, ht.TNonEmptyString, "New group name"),
1750 OP_RESULT = ht.Comment("New group name")(ht.TNonEmptyString)
1753 class OpGroupEvacuate(OpCode):
1754 """Evacuate a node group in the cluster."""
1755 OP_DSC_FIELD = "group_name"
1759 ("iallocator", None, ht.TMaybeString, "Iallocator for computing solution"),
1760 ("target_groups", None, ht.TMaybeListOf(ht.TNonEmptyString),
1761 "Destination group names or UUIDs"),
1763 OP_RESULT = TJobIdListOnly
1767 class OpOsDiagnose(OpCode):
1768 """Compute the list of guest operating systems."""
1771 ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1772 "Which operating systems to diagnose"),
1774 OP_RESULT = _TOldQueryResult
1778 class OpBackupQuery(OpCode):
1779 """Compute the list of exported images."""
1782 ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1783 "Empty list to query all nodes, node names otherwise"),
1785 OP_RESULT = ht.TDictOf(ht.TNonEmptyString,
1786 ht.TOr(ht.Comment("False on error")(ht.TBool),
1787 ht.TListOf(ht.TNonEmptyString)))
1790 class OpBackupPrepare(OpCode):
1791 """Prepares an instance export.
1793 @ivar instance_name: Instance name
1794 @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1797 OP_DSC_FIELD = "instance_name"
1800 ("mode", ht.NoDefault, ht.TElemOf(constants.EXPORT_MODES),
1803 OP_RESULT = ht.TOr(ht.TNone, ht.TDict)
1806 class OpBackupExport(OpCode):
1807 """Export an instance.
1809 For local exports, the export destination is the node name. For remote
1810 exports, the export destination is a list of tuples, each consisting of
1811 hostname/IP address, port, HMAC and HMAC salt. The HMAC is calculated using
1812 the cluster domain secret over the value "${index}:${hostname}:${port}". The
1813 destination X509 CA must be a signed certificate.
1815 @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1816 @ivar target_node: Export destination
1817 @ivar x509_key_name: X509 key to use (remote export only)
1818 @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
1822 OP_DSC_FIELD = "instance_name"
1826 # TODO: Rename target_node as it changes meaning for different export modes
1827 # (e.g. "destination")
1828 ("target_node", ht.NoDefault, ht.TOr(ht.TNonEmptyString, ht.TList),
1829 "Destination information, depends on export mode"),
1830 ("shutdown", True, ht.TBool, "Whether to shutdown instance before export"),
1831 ("remove_instance", False, ht.TBool,
1832 "Whether to remove instance after export"),
1833 ("ignore_remove_failures", False, ht.TBool,
1834 "Whether to ignore failures while removing instances"),
1835 ("mode", constants.EXPORT_MODE_LOCAL, ht.TElemOf(constants.EXPORT_MODES),
1837 ("x509_key_name", None, ht.TOr(ht.TList, ht.TNone),
1838 "Name of X509 key (remote export only)"),
1839 ("destination_x509_ca", None, ht.TMaybeString,
1840 "Destination X509 CA (remote export only)"),
1843 ht.TAnd(ht.TIsLength(2), ht.TItems([
1844 ht.Comment("Finalizing status")(ht.TBool),
1845 ht.Comment("Status for every exported disk")(ht.TListOf(ht.TBool)),
1849 class OpBackupRemove(OpCode):
1850 """Remove an instance's export."""
1851 OP_DSC_FIELD = "instance_name"
1855 OP_RESULT = ht.TNone
1859 class OpTagsGet(OpCode):
1860 """Returns the tags of the given object."""
1861 OP_DSC_FIELD = "name"
1864 # Not using _PUseLocking as the default is different for historical reasons
1865 ("use_locking", True, ht.TBool, "Whether to use synchronization"),
1866 # Name is only meaningful for nodes and instances
1867 ("name", ht.NoDefault, ht.TMaybeString,
1868 "Name of object to retrieve tags from"),
1870 OP_RESULT = ht.TListOf(ht.TNonEmptyString)
1873 class OpTagsSearch(OpCode):
1874 """Searches the tags in the cluster for a given pattern."""
1875 OP_DSC_FIELD = "pattern"
1877 ("pattern", ht.NoDefault, ht.TNonEmptyString,
1878 "Search pattern (regular expression)"),
1880 OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(2), ht.TItems([
1886 class OpTagsSet(OpCode):
1887 """Add a list of tags on a given object."""
1891 # Name is only meaningful for groups, nodes and instances
1892 ("name", ht.NoDefault, ht.TMaybeString,
1893 "Name of object where tag(s) should be added"),
1895 OP_RESULT = ht.TNone
1898 class OpTagsDel(OpCode):
1899 """Remove a list of tags from a given object."""
1903 # Name is only meaningful for groups, nodes and instances
1904 ("name", ht.NoDefault, ht.TMaybeString,
1905 "Name of object where tag(s) should be deleted"),
1907 OP_RESULT = ht.TNone
1911 class OpTestDelay(OpCode):
1912 """Sleeps for a configured amount of time.
1914 This is used just for debugging and testing.
1917 - duration: the time to sleep
1918 - on_master: if true, sleep on the master
1919 - on_nodes: list of nodes in which to sleep
1921 If the on_master parameter is true, it will execute a sleep on the
1922 master (before any node sleep).
1924 If the on_nodes list is not empty, it will sleep on those nodes
1925 (after the sleep on the master, if that is enabled).
1927 As an additional feature, the case of duration < 0 will be reported
1928 as an execution error, so this opcode can be used as a failure
1929 generator. The case of duration == 0 will not be treated specially.
1932 OP_DSC_FIELD = "duration"
1934 ("duration", ht.NoDefault, ht.TNumber, None),
1935 ("on_master", True, ht.TBool, None),
1936 ("on_nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1937 ("repeat", 0, ht.TNonNegativeInt, None),
1941 class OpTestAllocator(OpCode):
1942 """Allocator framework testing.
1944 This opcode has two modes:
1945 - gather and return allocator input for a given mode (allocate new
1946 or replace secondary) and a given instance definition (direction
1948 - run a selected allocator for a given operation (as above) and
1949 return the allocator output (direction 'out')
1952 OP_DSC_FIELD = "allocator"
1954 ("direction", ht.NoDefault,
1955 ht.TElemOf(constants.VALID_IALLOCATOR_DIRECTIONS), None),
1956 ("mode", ht.NoDefault, ht.TElemOf(constants.VALID_IALLOCATOR_MODES), None),
1957 ("name", ht.NoDefault, ht.TNonEmptyString, None),
1958 ("nics", ht.NoDefault,
1959 ht.TMaybeListOf(ht.TDictOf(ht.TElemOf([constants.INIC_MAC,
1962 ht.TOr(ht.TNone, ht.TNonEmptyString))),
1964 ("disks", ht.NoDefault, ht.TOr(ht.TNone, ht.TList), None),
1965 ("hypervisor", None, ht.TMaybeString, None),
1966 ("allocator", None, ht.TMaybeString, None),
1967 ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1968 ("memory", None, ht.TOr(ht.TNone, ht.TNonNegativeInt), None),
1969 ("vcpus", None, ht.TOr(ht.TNone, ht.TNonNegativeInt), None),
1970 ("os", None, ht.TMaybeString, None),
1971 ("disk_template", None, ht.TMaybeString, None),
1972 ("instances", None, ht.TMaybeListOf(ht.TNonEmptyString), None),
1974 ht.TOr(ht.TNone, ht.TElemOf(constants.IALLOCATOR_NEVAC_MODES)), None),
1975 ("target_groups", None, ht.TMaybeListOf(ht.TNonEmptyString), None),
1976 ("spindle_use", 1, ht.TNonNegativeInt, None),
1977 ("count", 1, ht.TNonNegativeInt, None),
1981 class OpTestJqueue(OpCode):
1982 """Utility opcode to test some aspects of the job queue.
1986 ("notify_waitlock", False, ht.TBool, None),
1987 ("notify_exec", False, ht.TBool, None),
1988 ("log_messages", ht.EmptyList, ht.TListOf(ht.TString), None),
1989 ("fail", False, ht.TBool, None),
1993 class OpTestDummy(OpCode):
1994 """Utility opcode used by unittests.
1998 ("result", ht.NoDefault, ht.NoType, None),
1999 ("messages", ht.NoDefault, ht.NoType, None),
2000 ("fail", ht.NoDefault, ht.NoType, None),
2001 ("submit_jobs", None, ht.NoType, None),
2007 # Add a new network in the cluster
2008 class OpNetworkAdd(OpCode):
2009 """Add an IP network to the cluster."""
2010 OP_DSC_FIELD = "network_name"
2014 ("network", None, ht.TAnd(ht.TString ,_CheckCIDRNetNotation), None),
2015 ("gateway", None, ht.TOr(ht.TNone, _CheckCIDRAddrNotation), None),
2016 ("network6", None, ht.TOr(ht.TNone, _CheckCIDR6NetNotation), None),
2017 ("gateway6", None, ht.TOr(ht.TNone, _CheckCIDR6AddrNotation), None),
2018 ("mac_prefix", None, ht.TMaybeString, None),
2019 ("add_reserved_ips", None,
2020 ht.TOr(ht.TNone, ht.TListOf(_CheckCIDRAddrNotation)), None),
2021 ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "Network tags"),
2024 class OpNetworkRemove(OpCode):
2025 """Remove an existing network from the cluster.
2026 Must not be connected to any nodegroup.
2029 OP_DSC_FIELD = "network_name"
2035 class OpNetworkSetParams(OpCode):
2036 """Modify Network's parameters except for IPv4 subnet"""
2037 OP_DSC_FIELD = "network_name"
2041 ("gateway", None, ht.TOr(ht.TNone, _CheckCIDRAddrNotation), None),
2042 ("network6", None, ht.TOr(ht.TNone, _CheckCIDR6NetNotation), None),
2043 ("gateway6", None, ht.TOr(ht.TNone, _CheckCIDR6AddrNotation), None),
2044 ("mac_prefix", None, ht.TMaybeString, None),
2045 ("add_reserved_ips", None,
2046 ht.TOr(ht.TNone, ht.TListOf(_CheckCIDRAddrNotation)), None),
2047 ("remove_reserved_ips", None,
2048 ht.TOr(ht.TNone, ht.TListOf(_CheckCIDRAddrNotation)), None),
2051 class OpNetworkConnect(OpCode):
2052 """Connect a Network to a specific Nodegroup with the defined netparams
2053 (mode, link). Nics in this Network will inherit those params.
2054 Produce errors if a NIC (that its not already assigned to a network)
2055 has an IP that is contained in the Network this will produce error unless
2056 --no-conflicts-check is passed.
2059 OP_DSC_FIELD = "network_name"
2063 ("network_mode", None, ht.TString, None),
2064 ("network_link", None, ht.TString, None),
2065 ("conflicts_check", True, ht.TBool, "Check for conflicting IPs"),
2068 class OpNetworkDisconnect(OpCode):
2069 """Disconnect a Network from a Nodegroup. Produce errors if NICs are
2070 present in the Network unless --no-conficts-check option is passed.
2073 OP_DSC_FIELD = "network_name"
2077 ("conflicts_check", True, ht.TBool, "Check for conflicting IPs"),
2080 class OpNetworkQuery(OpCode):
2081 """Compute the list of networks."""
2084 ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
2085 "Empty list to query all groups, group names otherwise"),
2090 """Returns list of all defined opcodes.
2092 Does not eliminate duplicates by C{OP_ID}.
2095 return [v for v in globals().values()
2096 if (isinstance(v, type) and issubclass(v, OpCode) and
2097 hasattr(v, "OP_ID") and v is not OpCode)]
2100 OP_MAPPING = dict((v.OP_ID, v) for v in _GetOpList())