+# pylint: disable=R0903
+
+import logging
+import re
+
+from ganeti import constants
+from ganeti import errors
+from ganeti import ht
+from ganeti import objects
+from ganeti import query
+
+
+# Common opcode attributes
+
+#: output fields for a query operation
+_POutputFields = ("output_fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
+ "Selected output fields")
+
+#: the shutdown timeout
+_PShutdownTimeout = \
+ ("shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT, ht.TPositiveInt,
+ "How long to wait for instance to shut down")
+
+#: the force parameter
+_PForce = ("force", False, ht.TBool, "Whether to force the operation")
+
+#: a required instance name (for single-instance LUs)
+_PInstanceName = ("instance_name", ht.NoDefault, ht.TNonEmptyString,
+ "Instance name")
+
+#: Whether to ignore offline nodes
+_PIgnoreOfflineNodes = ("ignore_offline_nodes", False, ht.TBool,
+ "Whether to ignore offline nodes")
+
+#: a required node name (for single-node LUs)
+_PNodeName = ("node_name", ht.NoDefault, ht.TNonEmptyString, "Node name")
+
+#: a required node group name (for single-group LUs)
+_PGroupName = ("group_name", ht.NoDefault, ht.TNonEmptyString, "Group name")
+
+#: Migration type (live/non-live)
+_PMigrationMode = ("mode", None,
+ ht.TOr(ht.TNone, ht.TElemOf(constants.HT_MIGRATION_MODES)),
+ "Migration mode")
+
+#: Obsolete 'live' migration mode (boolean)
+_PMigrationLive = ("live", None, ht.TMaybeBool,
+ "Legacy setting for live migration, do not use")
+
+#: Tag type
+_PTagKind = ("kind", ht.NoDefault, ht.TElemOf(constants.VALID_TAG_TYPES), None)
+
+#: List of tag strings
+_PTags = ("tags", ht.NoDefault, ht.TListOf(ht.TNonEmptyString), None)
+
+_PForceVariant = ("force_variant", False, ht.TBool,
+ "Whether to force an unknown OS variant")
+
+_PWaitForSync = ("wait_for_sync", True, ht.TBool,
+ "Whether to wait for the disk to synchronize")
+
+_PIgnoreConsistency = ("ignore_consistency", False, ht.TBool,
+ "Whether to ignore disk consistency")
+
+_PStorageName = ("name", ht.NoDefault, ht.TMaybeString, "Storage name")
+
+_PUseLocking = ("use_locking", False, ht.TBool,
+ "Whether to use synchronization")
+
+_PNameCheck = ("name_check", True, ht.TBool, "Whether to check name")
+
+_PNodeGroupAllocPolicy = \
+ ("alloc_policy", None,
+ ht.TOr(ht.TNone, ht.TElemOf(constants.VALID_ALLOC_POLICIES)),
+ "Instance allocation policy")
+
+_PGroupNodeParams = ("ndparams", None, ht.TMaybeDict,
+ "Default node parameters for group")
+
+_PQueryWhat = ("what", ht.NoDefault, ht.TElemOf(constants.QR_VIA_OP),
+ "Resource(s) to query for")
+
+_PEarlyRelease = ("early_release", False, ht.TBool,
+ "Whether to release locks as soon as possible")
+
+_PIpCheckDoc = "Whether to ensure instance's IP address is inactive"
+
+#: Do not remember instance state changes
+_PNoRemember = ("no_remember", False, ht.TBool,
+ "Do not remember the state change")
+
+#: Target node for instance migration/failover
+_PMigrationTargetNode = ("target_node", None, ht.TMaybeString,
+ "Target node for shared-storage instances")
+
+_PStartupPaused = ("startup_paused", False, ht.TBool,
+ "Pause instance at startup")
+
+_PVerbose = ("verbose", False, ht.TBool, "Verbose mode")
+
+# Parameters for cluster verification
+_PDebugSimulateErrors = ("debug_simulate_errors", False, ht.TBool,
+ "Whether to simulate errors (useful for debugging)")
+_PErrorCodes = ("error_codes", False, ht.TBool, "Error codes")
+_PSkipChecks = ("skip_checks", ht.EmptyList,
+ ht.TListOf(ht.TElemOf(constants.VERIFY_OPTIONAL_CHECKS)),
+ "Which checks to skip")
+_PIgnoreErrors = ("ignore_errors", ht.EmptyList,
+ ht.TListOf(ht.TElemOf(constants.CV_ALL_ECODES_STRINGS)),
+ "List of error codes that should be treated as warnings")
+
+# Disk parameters
+_PDiskParams = ("diskparams", None,
+ ht.TOr(
+ ht.TDictOf(ht.TElemOf(constants.DISK_TEMPLATES), ht.TDict),
+ ht.TNone),
+ "Disk templates' parameter defaults")
+
+# Parameters for node resource model
+_PHvState = ("hv_state", None, ht.TMaybeDict, "Set hypervisor states")
+_PDiskState = ("disk_state", None, ht.TMaybeDict, "Set disk states")
+
+
+_PIgnoreIpolicy = ("ignore_ipolicy", False, ht.TBool,
+ "Whether to ignore ipolicy violations")
+
+# Allow runtime changes while migrating
+_PAllowRuntimeChgs = ("allow_runtime_changes", True, ht.TBool,
+ "Allow runtime changes (eg. memory ballooning)")
+
+
+#: OP_ID conversion regular expression
+_OPID_RE = re.compile("([a-z])([A-Z])")
+
+#: Utility function for L{OpClusterSetParams}
+_TestClusterOsListItem = \
+ ht.TAnd(ht.TIsLength(2), ht.TItems([
+ ht.TElemOf(constants.DDMS_VALUES),
+ ht.TNonEmptyString,
+ ]))
+
+_TestClusterOsList = ht.TMaybeListOf(_TestClusterOsListItem)
+
+# TODO: Generate check from constants.INIC_PARAMS_TYPES
+#: Utility function for testing NIC definitions
+_TestNicDef = \
+ ht.Comment("NIC parameters")(ht.TDictOf(ht.TElemOf(constants.INIC_PARAMS),
+ ht.TOr(ht.TNone, ht.TNonEmptyString)))
+
+_TSetParamsResultItemItems = [
+ ht.Comment("name of changed parameter")(ht.TNonEmptyString),
+ ht.Comment("new value")(ht.TAny),
+ ]
+
+_TSetParamsResult = \
+ ht.TListOf(ht.TAnd(ht.TIsLength(len(_TSetParamsResultItemItems)),
+ ht.TItems(_TSetParamsResultItemItems)))
+
+# TODO: Generate check from constants.IDISK_PARAMS_TYPES (however, not all users
+# of this check support all parameters)
+_TDiskParams = \
+ ht.Comment("Disk parameters")(ht.TDictOf(ht.TElemOf(constants.IDISK_PARAMS),
+ ht.TOr(ht.TNonEmptyString, ht.TInt)))
+
+_TQueryRow = \
+ ht.TListOf(ht.TAnd(ht.TIsLength(2),
+ ht.TItems([ht.TElemOf(constants.RS_ALL),
+ ht.TAny])))
+
+_TQueryResult = ht.TListOf(_TQueryRow)
+
+_TOldQueryRow = ht.TListOf(ht.TAny)
+
+_TOldQueryResult = ht.TListOf(_TOldQueryRow)
+
+
+_SUMMARY_PREFIX = {
+ "CLUSTER_": "C_",
+ "GROUP_": "G_",
+ "NODE_": "N_",
+ "INSTANCE_": "I_",
+ }
+
+#: Attribute name for dependencies
+DEPEND_ATTR = "depends"
+
+#: Attribute name for comment
+COMMENT_ATTR = "comment"
+
+
+def _NameToId(name):
+ """Convert an opcode class name to an OP_ID.
+
+ @type name: string
+ @param name: the class name, as OpXxxYyy
+ @rtype: string
+ @return: the name in the OP_XXXX_YYYY format
+
+ """
+ if not name.startswith("Op"):
+ return None
+ # Note: (?<=[a-z])(?=[A-Z]) would be ideal, since it wouldn't
+ # consume any input, and hence we would just have all the elements
+ # in the list, one by one; but it seems that split doesn't work on
+ # non-consuming input, hence we have to process the input string a
+ # bit
+ name = _OPID_RE.sub(r"\1,\2", name)
+ elems = name.split(",")
+ return "_".join(n.upper() for n in elems)
+
+
+def _GenerateObjectTypeCheck(obj, fields_types):
+ """Helper to generate type checks for objects.
+
+ @param obj: The object to generate type checks
+ @param fields_types: The fields and their types as a dict
+ @return: A ht type check function
+
+ """
+ assert set(obj.GetAllSlots()) == set(fields_types.keys()), \
+ "%s != %s" % (set(obj.GetAllSlots()), set(fields_types.keys()))
+ return ht.TStrictDict(True, True, fields_types)
+
+
+_TObjFdefs = \
+ _GenerateObjectTypeCheck(objects.QueryFieldDefinition, {
+ "name": ht.TRegex(query.FIELD_NAME_RE),
+ "title": ht.TRegex(query.TITLE_RE),
+ "kind": ht.TElemOf(constants.QFT_ALL),
+ "doc": ht.TRegex(query.DOC_RE),
+ })
+
+
+def RequireFileStorage():
+ """Checks that file storage is enabled.
+
+ While it doesn't really fit into this module, L{utils} was deemed too large
+ of a dependency to be imported for just one or two functions.
+
+ @raise errors.OpPrereqError: when file storage is disabled
+
+ """
+ if not constants.ENABLE_FILE_STORAGE:
+ raise errors.OpPrereqError("File storage disabled at configure time",
+ errors.ECODE_INVAL)
+
+
+def RequireSharedFileStorage():
+ """Checks that shared file storage is enabled.
+
+ While it doesn't really fit into this module, L{utils} was deemed too large
+ of a dependency to be imported for just one or two functions.
+
+ @raise errors.OpPrereqError: when shared file storage is disabled
+
+ """
+ if not constants.ENABLE_SHARED_FILE_STORAGE:
+ raise errors.OpPrereqError("Shared file storage disabled at"
+ " configure time", errors.ECODE_INVAL)
+
+
+@ht.WithDesc("CheckFileStorage")
+def _CheckFileStorage(value):
+ """Ensures file storage is enabled if used.
+
+ """
+ if value == constants.DT_FILE:
+ RequireFileStorage()
+ elif value == constants.DT_SHARED_FILE:
+ RequireSharedFileStorage()
+ return True
+
+
+def _BuildDiskTemplateCheck(accept_none):
+ """Builds check for disk template.
+
+ @type accept_none: bool
+ @param accept_none: whether to accept None as a correct value
+ @rtype: callable
+
+ """
+ template_check = ht.TElemOf(constants.DISK_TEMPLATES)
+
+ if accept_none:
+ template_check = ht.TOr(template_check, ht.TNone)
+
+ return ht.TAnd(template_check, _CheckFileStorage)
+
+
+def _CheckStorageType(storage_type):
+ """Ensure a given storage type is valid.
+
+ """
+ if storage_type not in constants.VALID_STORAGE_TYPES:
+ raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
+ errors.ECODE_INVAL)
+ if storage_type == constants.ST_FILE:
+ RequireFileStorage()
+ return True
+
+
+#: Storage type parameter
+_PStorageType = ("storage_type", ht.NoDefault, _CheckStorageType,
+ "Storage type")
+
+
+class _AutoOpParamSlots(type):
+ """Meta class for opcode definitions.
+
+ """
+ def __new__(mcs, name, bases, attrs):
+ """Called when a class should be created.
+
+ @param mcs: The meta class
+ @param name: Name of created class
+ @param bases: Base classes
+ @type attrs: dict
+ @param attrs: Class attributes
+
+ """
+ assert "__slots__" not in attrs, \
+ "Class '%s' defines __slots__ when it should use OP_PARAMS" % name
+ assert "OP_ID" not in attrs, "Class '%s' defining OP_ID" % name
+
+ attrs["OP_ID"] = _NameToId(name)
+
+ # Always set OP_PARAMS to avoid duplicates in BaseOpCode.GetAllParams
+ params = attrs.setdefault("OP_PARAMS", [])
+
+ # Use parameter names as slots
+ slots = [pname for (pname, _, _, _) in params]
+
+ assert "OP_DSC_FIELD" not in attrs or attrs["OP_DSC_FIELD"] in slots, \
+ "Class '%s' uses unknown field in OP_DSC_FIELD" % name
+
+ attrs["__slots__"] = slots
+
+ return type.__new__(mcs, name, bases, attrs)