1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """OpCodes module
23
24 This module implements the data structures which define the cluster
25 operations - the so-called opcodes.
26
27 Every operation which modifies the cluster state is expressed via
28 opcodes.
29
30 """
31
32
33
34
35
36 import logging
37 import re
38
39 from ganeti import compat
40 from ganeti import constants
41 from ganeti import errors
42 from ganeti import ht
43
44
45
46
47
48 _POutputFields = ("output_fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
49 "Selected output fields")
50
51
52 _PShutdownTimeout = \
53 ("shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT, ht.TPositiveInt,
54 "How long to wait for instance to shut down")
55
56
57 _PForce = ("force", False, ht.TBool, "Whether to force the operation")
58
59
60 _PInstanceName = ("instance_name", ht.NoDefault, ht.TNonEmptyString,
61 "Instance name")
62
63
64 _PIgnoreOfflineNodes = ("ignore_offline_nodes", False, ht.TBool,
65 "Whether to ignore offline nodes")
66
67
68 _PNodeName = ("node_name", ht.NoDefault, ht.TNonEmptyString, "Node name")
69
70
71 _PGroupName = ("group_name", ht.NoDefault, ht.TNonEmptyString, "Group name")
72
73
74 _PMigrationMode = ("mode", None,
75 ht.TOr(ht.TNone, ht.TElemOf(constants.HT_MIGRATION_MODES)),
76 "Migration mode")
77
78
79 _PMigrationLive = ("live", None, ht.TMaybeBool,
80 "Legacy setting for live migration, do not use")
81
82
83 _PTagKind = ("kind", ht.NoDefault, ht.TElemOf(constants.VALID_TAG_TYPES), None)
84
85
86 _PTags = ("tags", ht.NoDefault, ht.TListOf(ht.TNonEmptyString), None)
87
88 _PForceVariant = ("force_variant", False, ht.TBool,
89 "Whether to force an unknown OS variant")
90
91 _PWaitForSync = ("wait_for_sync", True, ht.TBool,
92 "Whether to wait for the disk to synchronize")
93
94 _PIgnoreConsistency = ("ignore_consistency", False, ht.TBool,
95 "Whether to ignore disk consistency")
96
97 _PStorageName = ("name", ht.NoDefault, ht.TMaybeString, "Storage name")
98
99 _PUseLocking = ("use_locking", False, ht.TBool,
100 "Whether to use synchronization")
101
102 _PNameCheck = ("name_check", True, ht.TBool, "Whether to check name")
103
104 _PNodeGroupAllocPolicy = \
105 ("alloc_policy", None,
106 ht.TOr(ht.TNone, ht.TElemOf(constants.VALID_ALLOC_POLICIES)),
107 "Instance allocation policy")
108
109 _PGroupNodeParams = ("ndparams", None, ht.TMaybeDict,
110 "Default node parameters for group")
111
112 _PQueryWhat = ("what", ht.NoDefault, ht.TElemOf(constants.QR_VIA_OP),
113 "Resource(s) to query for")
114
115 _PEarlyRelease = ("early_release", False, ht.TBool,
116 "Whether to release locks as soon as possible")
117
118 _PIpCheckDoc = "Whether to ensure instance's IP address is inactive"
119
120
121 _PNoRemember = ("no_remember", False, ht.TBool,
122 "Do not remember the state change")
123
124
125 _PMigrationTargetNode = ("target_node", None, ht.TMaybeString,
126 "Target node for shared-storage instances")
127
128 _PStartupPaused = ("startup_paused", False, ht.TBool,
129 "Pause instance at startup")
130
131 _PVerbose = ("verbose", False, ht.TBool, "Verbose mode")
132
133
134 _PDebugSimulateErrors = ("debug_simulate_errors", False, ht.TBool,
135 "Whether to simulate errors (useful for debugging)")
136 _PErrorCodes = ("error_codes", False, ht.TBool, "Error codes")
137 _PSkipChecks = ("skip_checks", ht.EmptyList,
138 ht.TListOf(ht.TElemOf(constants.VERIFY_OPTIONAL_CHECKS)),
139 "Which checks to skip")
140
141
142 _OPID_RE = re.compile("([a-z])([A-Z])")
143
144
145 _TestClusterOsList = ht.TOr(ht.TNone,
146 ht.TListOf(ht.TAnd(ht.TList, ht.TIsLength(2),
147 ht.TMap(ht.WithDesc("GetFirstItem")(compat.fst),
148 ht.TElemOf(constants.DDMS_VALUES)))))
149
150
151
152
153 _TestNicDef = ht.TDictOf(ht.TElemOf(constants.INIC_PARAMS),
154 ht.TOr(ht.TNone, ht.TNonEmptyString))
155
156 _TSetParamsResultItemItems = [
157 ht.Comment("name of changed parameter")(ht.TNonEmptyString),
158 ht.TAny,
159 ]
160
161 _TSetParamsResult = \
162 ht.TListOf(ht.TAnd(ht.TIsLength(len(_TSetParamsResultItemItems)),
163 ht.TItems(_TSetParamsResultItemItems)))
164
165 _SUMMARY_PREFIX = {
166 "CLUSTER_": "C_",
167 "GROUP_": "G_",
168 "NODE_": "N_",
169 "INSTANCE_": "I_",
170 }
171
172
173 DEPEND_ATTR = "depends"
174
175
176 COMMENT_ATTR = "comment"
180 """Convert an opcode class name to an OP_ID.
181
182 @type name: string
183 @param name: the class name, as OpXxxYyy
184 @rtype: string
185 @return: the name in the OP_XXXX_YYYY format
186
187 """
188 if not name.startswith("Op"):
189 return None
190
191
192
193
194
195 name = _OPID_RE.sub(r"\1,\2", name)
196 elems = name.split(",")
197 return "_".join(n.upper() for n in elems)
198
201 """Checks that file storage is enabled.
202
203 While it doesn't really fit into this module, L{utils} was deemed too large
204 of a dependency to be imported for just one or two functions.
205
206 @raise errors.OpPrereqError: when file storage is disabled
207
208 """
209 if not constants.ENABLE_FILE_STORAGE:
210 raise errors.OpPrereqError("File storage disabled at configure time",
211 errors.ECODE_INVAL)
212
215 """Checks that shared file storage is enabled.
216
217 While it doesn't really fit into this module, L{utils} was deemed too large
218 of a dependency to be imported for just one or two functions.
219
220 @raise errors.OpPrereqError: when shared file storage is disabled
221
222 """
223 if not constants.ENABLE_SHARED_FILE_STORAGE:
224 raise errors.OpPrereqError("Shared file storage disabled at"
225 " configure time", errors.ECODE_INVAL)
226
238
239
240 _CheckDiskTemplate = ht.TAnd(ht.TElemOf(constants.DISK_TEMPLATES),
241 _CheckFileStorage)
254
255
256
257 _PStorageType = ("storage_type", ht.NoDefault, _CheckStorageType,
258 "Storage type")
262 """Meta class for opcode definitions.
263
264 """
265 - def __new__(mcs, name, bases, attrs):
266 """Called when a class should be created.
267
268 @param mcs: The meta class
269 @param name: Name of created class
270 @param bases: Base classes
271 @type attrs: dict
272 @param attrs: Class attributes
273
274 """
275 assert "__slots__" not in attrs, \
276 "Class '%s' defines __slots__ when it should use OP_PARAMS" % name
277 assert "OP_ID" not in attrs, "Class '%s' defining OP_ID" % name
278
279 attrs["OP_ID"] = _NameToId(name)
280
281
282 params = attrs.setdefault("OP_PARAMS", [])
283
284
285 slots = [pname for (pname, _, _, _) in params]
286
287 assert "OP_DSC_FIELD" not in attrs or attrs["OP_DSC_FIELD"] in slots, \
288 "Class '%s' uses unknown field in OP_DSC_FIELD" % name
289
290 attrs["__slots__"] = slots
291
292 return type.__new__(mcs, name, bases, attrs)
293
296 """A simple serializable object.
297
298 This object serves as a parent class for OpCode without any custom
299 field handling.
300
301 """
302
303
304 __metaclass__ = _AutoOpParamSlots
305
307 """Constructor for BaseOpCode.
308
309 The constructor takes only keyword arguments and will set
310 attributes on this object based on the passed arguments. As such,
311 it means that you should not pass arguments which are not in the
312 __slots__ attribute for this class.
313
314 """
315 slots = self._all_slots()
316 for key in kwargs:
317 if key not in slots:
318 raise TypeError("Object %s doesn't support the parameter '%s'" %
319 (self.__class__.__name__, key))
320 setattr(self, key, kwargs[key])
321
323 """Generic serializer.
324
325 This method just returns the contents of the instance as a
326 dictionary.
327
328 @rtype: C{dict}
329 @return: the instance attributes and their values
330
331 """
332 state = {}
333 for name in self._all_slots():
334 if hasattr(self, name):
335 state[name] = getattr(self, name)
336 return state
337
339 """Generic unserializer.
340
341 This method just restores from the serialized state the attributes
342 of the current instance.
343
344 @param state: the serialized opcode data
345 @type state: C{dict}
346
347 """
348 if not isinstance(state, dict):
349 raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
350 type(state))
351
352 for name in self._all_slots():
353 if name not in state and hasattr(self, name):
354 delattr(self, name)
355
356 for name in state:
357 setattr(self, name, state[name])
358
359 @classmethod
361 """Compute the list of all declared slots for a class.
362
363 """
364 slots = []
365 for parent in cls.__mro__:
366 slots.extend(getattr(parent, "__slots__", []))
367 return slots
368
369 @classmethod
371 """Compute list of all parameters for an opcode.
372
373 """
374 slots = []
375 for parent in cls.__mro__:
376 slots.extend(getattr(parent, "OP_PARAMS", []))
377 return slots
378
380 """Validate opcode parameters, optionally setting default values.
381
382 @type set_defaults: bool
383 @param set_defaults: Whether to set default values
384 @raise errors.OpPrereqError: When a parameter value doesn't match
385 requirements
386
387 """
388 for (attr_name, default, test, _) in self.GetAllParams():
389 assert test == ht.NoType or callable(test)
390
391 if not hasattr(self, attr_name):
392 if default == ht.NoDefault:
393 raise errors.OpPrereqError("Required parameter '%s.%s' missing" %
394 (self.OP_ID, attr_name),
395 errors.ECODE_INVAL)
396 elif set_defaults:
397 if callable(default):
398 dval = default()
399 else:
400 dval = default
401 setattr(self, attr_name, dval)
402
403 if test == ht.NoType:
404
405 continue
406
407 if set_defaults or hasattr(self, attr_name):
408 attr_val = getattr(self, attr_name)
409 if not test(attr_val):
410 logging.error("OpCode %s, parameter %s, has invalid type %s/value %s",
411 self.OP_ID, attr_name, type(attr_val), attr_val)
412 raise errors.OpPrereqError("Parameter '%s.%s' fails validation" %
413 (self.OP_ID, attr_name),
414 errors.ECODE_INVAL)
415
436
437
438 TNoRelativeJobDependencies = _BuildJobDepCheck(False)
439
440
441 _TJobIdListItem = \
442 ht.TAnd(ht.TIsLength(2),
443 ht.TItems([ht.Comment("success")(ht.TBool),
444 ht.Comment("Job ID if successful, error message"
445 " otherwise")(ht.TOr(ht.TString,
446 ht.TJobId))]))
447 TJobIdList = ht.TListOf(_TJobIdListItem)
448
449
450 TJobIdListOnly = ht.TStrictDict(True, True, {
451 constants.JOB_IDS_KEY: ht.Comment("List of submitted jobs")(TJobIdList),
452 })
453
454
455 -class OpCode(BaseOpCode):
456 """Abstract OpCode.
457
458 This is the root of the actual OpCode hierarchy. All clases derived
459 from this class should override OP_ID.
460
461 @cvar OP_ID: The ID of this opcode. This should be unique amongst all
462 children of this class.
463 @cvar OP_DSC_FIELD: The name of a field whose value will be included in the
464 string returned by Summary(); see the docstring of that
465 method for details).
466 @cvar OP_PARAMS: List of opcode attributes, the default values they should
467 get if not already defined, and types they must match.
468 @cvar OP_RESULT: Callable to verify opcode result
469 @cvar WITH_LU: Boolean that specifies whether this should be included in
470 mcpu's dispatch table
471 @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
472 the check steps
473 @ivar priority: Opcode priority for queue
474
475 """
476
477
478 WITH_LU = True
479 OP_PARAMS = [
480 ("dry_run", None, ht.TMaybeBool, "Run checks only, don't execute"),
481 ("debug_level", None, ht.TOr(ht.TNone, ht.TPositiveInt), "Debug level"),
482 ("priority", constants.OP_PRIO_DEFAULT,
483 ht.TElemOf(constants.OP_PRIO_SUBMIT_VALID), "Opcode priority"),
484 (DEPEND_ATTR, None, _BuildJobDepCheck(True),
485 "Job dependencies; if used through ``SubmitManyJobs`` relative (negative)"
486 " job IDs can be used"),
487 (COMMENT_ATTR, None, ht.TMaybeString,
488 "Comment describing the purpose of the opcode"),
489 ]
490 OP_RESULT = None
491
493 """Specialized getstate for opcodes.
494
495 This method adds to the state dictionary the OP_ID of the class,
496 so that on unload we can identify the correct class for
497 instantiating the opcode.
498
499 @rtype: C{dict}
500 @return: the state as a dictionary
501
502 """
503 data = BaseOpCode.__getstate__(self)
504 data["OP_ID"] = self.OP_ID
505 return data
506
507 @classmethod
509 """Generic load opcode method.
510
511 The method identifies the correct opcode class from the dict-form
512 by looking for a OP_ID key, if this is not found, or its value is
513 not available in this module as a child of this class, we fail.
514
515 @type data: C{dict}
516 @param data: the serialized opcode
517
518 """
519 if not isinstance(data, dict):
520 raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
521 if "OP_ID" not in data:
522 raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
523 op_id = data["OP_ID"]
524 op_class = None
525 if op_id in OP_MAPPING:
526 op_class = OP_MAPPING[op_id]
527 else:
528 raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
529 op_id)
530 op = op_class()
531 new_data = data.copy()
532 del new_data["OP_ID"]
533 op.__setstate__(new_data)
534 return op
535
537 """Generates a summary description of this opcode.
538
539 The summary is the value of the OP_ID attribute (without the "OP_"
540 prefix), plus the value of the OP_DSC_FIELD attribute, if one was
541 defined; this field should allow to easily identify the operation
542 (for an instance creation job, e.g., it would be the instance
543 name).
544
545 """
546 assert self.OP_ID is not None and len(self.OP_ID) > 3
547
548 txt = self.OP_ID[3:]
549 field_name = getattr(self, "OP_DSC_FIELD", None)
550 if field_name:
551 field_value = getattr(self, field_name, None)
552 if isinstance(field_value, (list, tuple)):
553 field_value = ",".join(str(i) for i in field_value)
554 txt = "%s(%s)" % (txt, field_value)
555 return txt
556
558 """Generates a compact summary description of the opcode.
559
560 """
561 assert self.OP_ID.startswith("OP_")
562
563 text = self.OP_ID[3:]
564
565 for (prefix, supplement) in _SUMMARY_PREFIX.items():
566 if text.startswith(prefix):
567 return supplement + text[len(prefix):]
568
569 return text
570
571
572
573
574 -class OpClusterPostInit(OpCode):
575 """Post cluster initialization.
576
577 This opcode does not touch the cluster at all. Its purpose is to run hooks
578 after the cluster has been initialized.
579
580 """
581
584 """Destroy the cluster.
585
586 This opcode has no other parameters. All the state is irreversibly
587 lost after the execution of this opcode.
588
589 """
590
593 """Query cluster information."""
594
608
620
641
648
651 """Verifies the status of all disks in a node group.
652
653 Result: a tuple of three elements:
654 - dict of node names with issues (values: error msg)
655 - list of instances with degraded disks (that should be activated)
656 - dict of instances with missing logical volumes (values: (node, vol)
657 pairs with details about the missing volumes)
658
659 In normal operation, all lists should be empty. A non-empty instance
660 list (3rd element of the result) is still ok (errors were fixed) but
661 non-empty node list means some node is down, and probably there are
662 unfixable drbd errors.
663
664 Note that only instances that are drbd-based are taken into
665 consideration. This might need to be revisited in the future.
666
667 """
668 OP_DSC_FIELD = "group_name"
669 OP_PARAMS = [
670 _PGroupName,
671 ]
672 OP_RESULT = \
673 ht.TAnd(ht.TIsLength(3),
674 ht.TItems([ht.TDictOf(ht.TString, ht.TString),
675 ht.TListOf(ht.TString),
676 ht.TDictOf(ht.TString,
677 ht.TListOf(ht.TListOf(ht.TString)))]))
678
681 """Verify the disk sizes of the instances and fixes configuration
682 mimatches.
683
684 Parameters: optional instances list, in case we want to restrict the
685 checks to only a subset of the instances.
686
687 Result: a list of tuples, (instance, disk, new-size) for changed
688 configurations.
689
690 In normal operation, the list should be empty.
691
692 @type instances: list
693 @ivar instances: the list of instances to check, or empty for all instances
694
695 """
696 OP_PARAMS = [
697 ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
698 ]
699
706
709 """Rename the cluster.
710
711 @type name: C{str}
712 @ivar name: The new name of the cluster. The name and/or the master IP
713 address will be changed to match the new name and its IP
714 address.
715
716 """
717 OP_DSC_FIELD = "name"
718 OP_PARAMS = [
719 ("name", ht.NoDefault, ht.TNonEmptyString, None),
720 ]
721
724 """Change the parameters of the cluster.
725
726 @type vg_name: C{str} or C{None}
727 @ivar vg_name: The new volume group name or None to disable LVM usage.
728
729 """
730 OP_PARAMS = [
731 ("vg_name", None, ht.TMaybeString, "Volume group name"),
732 ("enabled_hypervisors", None,
733 ht.TOr(ht.TAnd(ht.TListOf(ht.TElemOf(constants.HYPER_TYPES)), ht.TTrue),
734 ht.TNone),
735 "List of enabled hypervisors"),
736 ("hvparams", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
737 ht.TNone),
738 "Cluster-wide hypervisor parameter defaults, hypervisor-dependent"),
739 ("beparams", None, ht.TOr(ht.TDict, ht.TNone),
740 "Cluster-wide backend parameter defaults"),
741 ("os_hvp", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
742 ht.TNone),
743 "Cluster-wide per-OS hypervisor parameter defaults"),
744 ("osparams", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
745 ht.TNone),
746 "Cluster-wide OS parameter defaults"),
747 ("candidate_pool_size", None, ht.TOr(ht.TStrictPositiveInt, ht.TNone),
748 "Master candidate pool size"),
749 ("uid_pool", None, ht.NoType,
750 "Set UID pool, must be list of lists describing UID ranges (two items,"
751 " start and end inclusive)"),
752 ("add_uids", None, ht.NoType,
753 "Extend UID pool, must be list of lists describing UID ranges (two"
754 " items, start and end inclusive) to be added"),
755 ("remove_uids", None, ht.NoType,
756 "Shrink UID pool, must be list of lists describing UID ranges (two"
757 " items, start and end inclusive) to be removed"),
758 ("maintain_node_health", None, ht.TMaybeBool,
759 "Whether to automatically maintain node health"),
760 ("prealloc_wipe_disks", None, ht.TMaybeBool,
761 "Whether to wipe disks before allocating them to instances"),
762 ("nicparams", None, ht.TMaybeDict, "Cluster-wide NIC parameter defaults"),
763 ("ndparams", None, ht.TMaybeDict, "Cluster-wide node parameter defaults"),
764 ("drbd_helper", None, ht.TOr(ht.TString, ht.TNone), "DRBD helper program"),
765 ("default_iallocator", None, ht.TOr(ht.TString, ht.TNone),
766 "Default iallocator for cluster"),
767 ("master_netdev", None, ht.TOr(ht.TString, ht.TNone),
768 "Master network device"),
769 ("reserved_lvs", None, ht.TOr(ht.TListOf(ht.TNonEmptyString), ht.TNone),
770 "List of reserved LVs"),
771 ("hidden_os", None, _TestClusterOsList,
772 "Modify list of hidden operating systems. Each modification must have"
773 " two items, the operation and the OS name. The operation can be"
774 " ``%s`` or ``%s``." % (constants.DDM_ADD, constants.DDM_REMOVE)),
775 ("blacklisted_os", None, _TestClusterOsList,
776 "Modify list of blacklisted operating systems. Each modification must have"
777 " two items, the operation and the OS name. The operation can be"
778 " ``%s`` or ``%s``." % (constants.DDM_ADD, constants.DDM_REMOVE)),
779 ]
780
783 """Force a full push of the cluster configuration.
784
785 """
786
789 """Query for resources/items.
790
791 @ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
792 @ivar fields: List of fields to retrieve
793 @ivar filter: Query filter
794
795 """
796 OP_DSC_FIELD = "what"
797 OP_PARAMS = [
798 _PQueryWhat,
799 _PUseLocking,
800 ("fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
801 "Requested fields"),
802 ("filter", None, ht.TOr(ht.TNone, ht.TList),
803 "Query filter"),
804 ]
805
808 """Query for available resource/item fields.
809
810 @ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
811 @ivar fields: List of fields to retrieve
812
813 """
814 OP_DSC_FIELD = "what"
815 OP_PARAMS = [
816 _PQueryWhat,
817 ("fields", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
818 "Requested fields; if not given, all are returned"),
819 ]
820
823 """Interact with OOB."""
824 OP_PARAMS = [
825 ("node_names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
826 "List of nodes to run the OOB command against"),
827 ("command", None, ht.TElemOf(constants.OOB_COMMANDS),
828 "OOB command to be run"),
829 ("timeout", constants.OOB_TIMEOUT, ht.TInt,
830 "Timeout before the OOB helper will be terminated"),
831 ("ignore_status", False, ht.TBool,
832 "Ignores the node offline status for power off"),
833 ("power_delay", constants.OOB_POWER_DELAY, ht.TPositiveFloat,
834 "Time in seconds to wait between powering on nodes"),
835 ]
836
841 """Remove a node.
842
843 @type node_name: C{str}
844 @ivar node_name: The name of the node to remove. If the node still has
845 instances on it, the operation will fail.
846
847 """
848 OP_DSC_FIELD = "node_name"
849 OP_PARAMS = [
850 _PNodeName,
851 ]
852
855 """Add a node to the cluster.
856
857 @type node_name: C{str}
858 @ivar node_name: The name of the node to add. This can be a short name,
859 but it will be expanded to the FQDN.
860 @type primary_ip: IP address
861 @ivar primary_ip: The primary IP of the node. This will be ignored when the
862 opcode is submitted, but will be filled during the node
863 add (so it will be visible in the job query).
864 @type secondary_ip: IP address
865 @ivar secondary_ip: The secondary IP of the node. This needs to be passed
866 if the cluster has been initialized in 'dual-network'
867 mode, otherwise it must not be given.
868 @type readd: C{bool}
869 @ivar readd: Whether to re-add an existing node to the cluster. If
870 this is not passed, then the operation will abort if the node
871 name is already in the cluster; use this parameter to 'repair'
872 a node that had its configuration broken, or was reinstalled
873 without removal from the cluster.
874 @type group: C{str}
875 @ivar group: The node group to which this node will belong.
876 @type vm_capable: C{bool}
877 @ivar vm_capable: The vm_capable node attribute
878 @type master_capable: C{bool}
879 @ivar master_capable: The master_capable node attribute
880
881 """
882 OP_DSC_FIELD = "node_name"
883 OP_PARAMS = [
884 _PNodeName,
885 ("primary_ip", None, ht.NoType, "Primary IP address"),
886 ("secondary_ip", None, ht.TMaybeString, "Secondary IP address"),
887 ("readd", False, ht.TBool, "Whether node is re-added to cluster"),
888 ("group", None, ht.TMaybeString, "Initial node group"),
889 ("master_capable", None, ht.TMaybeBool,
890 "Whether node can become master or master candidate"),
891 ("vm_capable", None, ht.TMaybeBool,
892 "Whether node can host instances"),
893 ("ndparams", None, ht.TMaybeDict, "Node parameters"),
894 ]
895
905
914
917 """Get information on storage for node(s)."""
918 OP_PARAMS = [
919 _POutputFields,
920 _PStorageType,
921 ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "List of nodes"),
922 ("name", None, ht.TMaybeString, "Storage name"),
923 ]
924
934
945
948 """Change the parameters of a node."""
949 OP_DSC_FIELD = "node_name"
950 OP_PARAMS = [
951 _PNodeName,
952 _PForce,
953 ("master_candidate", None, ht.TMaybeBool,
954 "Whether the node should become a master candidate"),
955 ("offline", None, ht.TMaybeBool,
956 "Whether the node should be marked as offline"),
957 ("drained", None, ht.TMaybeBool,
958 "Whether the node should be marked as drained"),
959 ("auto_promote", False, ht.TBool,
960 "Whether node(s) should be promoted to master candidate if necessary"),
961 ("master_capable", None, ht.TMaybeBool,
962 "Denote whether node can become master or master candidate"),
963 ("vm_capable", None, ht.TMaybeBool,
964 "Denote whether node can host instances"),
965 ("secondary_ip", None, ht.TMaybeString,
966 "Change node's secondary IP address"),
967 ("ndparams", None, ht.TMaybeDict, "Set node parameters"),
968 ("powered", None, ht.TMaybeBool,
969 "Whether the node should be marked as powered"),
970 ]
971 OP_RESULT = _TSetParamsResult
972
981
995
998 """Evacuate instances off a number of nodes."""
999 OP_DSC_FIELD = "node_name"
1000 OP_PARAMS = [
1001 _PEarlyRelease,
1002 _PNodeName,
1003 ("remote_node", None, ht.TMaybeString, "New secondary node"),
1004 ("iallocator", None, ht.TMaybeString, "Iallocator for computing solution"),
1005 ("mode", ht.NoDefault, ht.TElemOf(constants.IALLOCATOR_NEVAC_MODES),
1006 "Node evacuation mode"),
1007 ]
1008 OP_RESULT = TJobIdListOnly
1009
1014 """Create an instance.
1015
1016 @ivar instance_name: Instance name
1017 @ivar mode: Instance creation mode (one of L{constants.INSTANCE_CREATE_MODES})
1018 @ivar source_handshake: Signed handshake from source (remote import only)
1019 @ivar source_x509_ca: Source X509 CA in PEM format (remote import only)
1020 @ivar source_instance_name: Previous name of instance (remote import only)
1021 @ivar source_shutdown_timeout: Shutdown timeout used for source instance
1022 (remote import only)
1023
1024 """
1025 OP_DSC_FIELD = "instance_name"
1026 OP_PARAMS = [
1027 _PInstanceName,
1028 _PForceVariant,
1029 _PWaitForSync,
1030 _PNameCheck,
1031 ("beparams", ht.EmptyDict, ht.TDict, "Backend parameters for instance"),
1032 ("disks", ht.NoDefault,
1033
1034 ht.TListOf(ht.TDictOf(ht.TElemOf(constants.IDISK_PARAMS),
1035 ht.TOr(ht.TNonEmptyString, ht.TInt))),
1036 "Disk descriptions, for example ``[{\"%s\": 100}, {\"%s\": 5}]``;"
1037 " each disk definition must contain a ``%s`` value and"
1038 " can contain an optional ``%s`` value denoting the disk access mode"
1039 " (%s)" %
1040 (constants.IDISK_SIZE, constants.IDISK_SIZE, constants.IDISK_SIZE,
1041 constants.IDISK_MODE,
1042 " or ".join("``%s``" % i for i in sorted(constants.DISK_ACCESS_SET)))),
1043 ("disk_template", ht.NoDefault, _CheckDiskTemplate, "Disk template"),
1044 ("file_driver", None, ht.TOr(ht.TNone, ht.TElemOf(constants.FILE_DRIVER)),
1045 "Driver for file-backed disks"),
1046 ("file_storage_dir", None, ht.TMaybeString,
1047 "Directory for storing file-backed disks"),
1048 ("hvparams", ht.EmptyDict, ht.TDict,
1049 "Hypervisor parameters for instance, hypervisor-dependent"),
1050 ("hypervisor", None, ht.TMaybeString, "Hypervisor"),
1051 ("iallocator", None, ht.TMaybeString,
1052 "Iallocator for deciding which node(s) to use"),
1053 ("identify_defaults", False, ht.TBool,
1054 "Reset instance parameters to default if equal"),
1055 ("ip_check", True, ht.TBool, _PIpCheckDoc),
1056 ("mode", ht.NoDefault, ht.TElemOf(constants.INSTANCE_CREATE_MODES),
1057 "Instance creation mode"),
1058 ("nics", ht.NoDefault, ht.TListOf(_TestNicDef),
1059 "List of NIC (network interface) definitions, for example"
1060 " ``[{}, {}, {\"%s\": \"198.51.100.4\"}]``; each NIC definition can"
1061 " contain the optional values %s" %
1062 (constants.INIC_IP,
1063 ", ".join("``%s``" % i for i in sorted(constants.INIC_PARAMS)))),
1064 ("no_install", None, ht.TMaybeBool,
1065 "Do not install the OS (will disable automatic start)"),
1066 ("osparams", ht.EmptyDict, ht.TDict, "OS parameters for instance"),
1067 ("os_type", None, ht.TMaybeString, "Operating system"),
1068 ("pnode", None, ht.TMaybeString, "Primary node"),
1069 ("snode", None, ht.TMaybeString, "Secondary node"),
1070 ("source_handshake", None, ht.TOr(ht.TList, ht.TNone),
1071 "Signed handshake from source (remote import only)"),
1072 ("source_instance_name", None, ht.TMaybeString,
1073 "Source instance name (remote import only)"),
1074 ("source_shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT,
1075 ht.TPositiveInt,
1076 "How long source instance was given to shut down (remote import only)"),
1077 ("source_x509_ca", None, ht.TMaybeString,
1078 "Source X509 CA in PEM format (remote import only)"),
1079 ("src_node", None, ht.TMaybeString, "Source node for import"),
1080 ("src_path", None, ht.TMaybeString, "Source directory for import"),
1081 ("start", True, ht.TBool, "Whether to start instance after creation"),
1082 ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "Instance tags"),
1083 ]
1084 OP_RESULT = ht.Comment("instance nodes")(ht.TListOf(ht.TNonEmptyString))
1085
1088 """Reinstall an instance's OS."""
1089 OP_DSC_FIELD = "instance_name"
1090 OP_PARAMS = [
1091 _PInstanceName,
1092 _PForceVariant,
1093 ("os_type", None, ht.TMaybeString, "Instance operating system"),
1094 ("osparams", None, ht.TMaybeDict, "Temporary OS parameters"),
1095 ]
1096
1107
1110 """Rename an instance."""
1111 OP_PARAMS = [
1112 _PInstanceName,
1113 _PNameCheck,
1114 ("new_name", ht.NoDefault, ht.TNonEmptyString, "New instance name"),
1115 ("ip_check", False, ht.TBool, _PIpCheckDoc),
1116 ]
1117 OP_RESULT = ht.Comment("New instance name")(ht.TNonEmptyString)
1118
1121 """Startup an instance."""
1122 OP_DSC_FIELD = "instance_name"
1123 OP_PARAMS = [
1124 _PInstanceName,
1125 _PForce,
1126 _PIgnoreOfflineNodes,
1127 ("hvparams", ht.EmptyDict, ht.TDict,
1128 "Temporary hypervisor parameters, hypervisor-dependent"),
1129 ("beparams", ht.EmptyDict, ht.TDict, "Temporary backend parameters"),
1130 _PNoRemember,
1131 _PStartupPaused,
1132 ]
1133
1145
1148 """Reboot an instance."""
1149 OP_DSC_FIELD = "instance_name"
1150 OP_PARAMS = [
1151 _PInstanceName,
1152 _PShutdownTimeout,
1153 ("ignore_secondaries", False, ht.TBool,
1154 "Whether to start the instance even if secondary disks are failing"),
1155 ("reboot_type", ht.NoDefault, ht.TElemOf(constants.REBOOT_TYPES),
1156 "How to reboot instance"),
1157 ]
1158
1161 """Replace the disks of an instance."""
1162 OP_DSC_FIELD = "instance_name"
1163 OP_PARAMS = [
1164 _PInstanceName,
1165 _PEarlyRelease,
1166 ("mode", ht.NoDefault, ht.TElemOf(constants.REPLACE_MODES),
1167 "Replacement mode"),
1168 ("disks", ht.EmptyList, ht.TListOf(ht.TPositiveInt),
1169 "Disk indexes"),
1170 ("remote_node", None, ht.TMaybeString, "New secondary node"),
1171 ("iallocator", None, ht.TMaybeString,
1172 "Iallocator for deciding new secondary node"),
1173 ]
1174
1187
1190 """Migrate an instance.
1191
1192 This migrates (without shutting down an instance) to its secondary
1193 node.
1194
1195 @ivar instance_name: the name of the instance
1196 @ivar mode: the migration mode (live, non-live or None for auto)
1197
1198 """
1199 OP_DSC_FIELD = "instance_name"
1200 OP_PARAMS = [
1201 _PInstanceName,
1202 _PMigrationMode,
1203 _PMigrationLive,
1204 _PMigrationTargetNode,
1205 ("cleanup", False, ht.TBool,
1206 "Whether a previously failed migration should be cleaned up"),
1207 ("iallocator", None, ht.TMaybeString,
1208 "Iallocator for deciding the target node for shared-storage instances"),
1209 ("allow_failover", False, ht.TBool,
1210 "Whether we can fallback to failover if migration is not possible"),
1211 ]
1212
1231
1239
1248
1257
1260 """Deactivate an instance's disks."""
1261 OP_DSC_FIELD = "instance_name"
1262 OP_PARAMS = [
1263 _PInstanceName,
1264 ("disks", ht.EmptyList, ht.TListOf(ht.TPositiveInt),
1265 "List of disk indexes"),
1266 ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1267 "New instance nodes, if relocation is desired"),
1268 ]
1269
1279
1291
1294 """Change the parameters of an instance."""
1295 OP_DSC_FIELD = "instance_name"
1296 OP_PARAMS = [
1297 _PInstanceName,
1298 _PForce,
1299 _PForceVariant,
1300
1301 ("nics", ht.EmptyList, ht.TList,
1302 "List of NIC changes. Each item is of the form ``(op, settings)``."
1303 " ``op`` can be ``%s`` to add a new NIC with the specified settings,"
1304 " ``%s`` to remove the last NIC or a number to modify the settings"
1305 " of the NIC with that index." %
1306 (constants.DDM_ADD, constants.DDM_REMOVE)),
1307 ("disks", ht.EmptyList, ht.TList, "List of disk changes. See ``nics``."),
1308 ("beparams", ht.EmptyDict, ht.TDict, "Per-instance backend parameters"),
1309 ("hvparams", ht.EmptyDict, ht.TDict,
1310 "Per-instance hypervisor parameters, hypervisor-dependent"),
1311 ("disk_template", None, ht.TOr(ht.TNone, _CheckDiskTemplate),
1312 "Disk template for instance"),
1313 ("remote_node", None, ht.TMaybeString,
1314 "Secondary node (used when changing disk template)"),
1315 ("os_name", None, ht.TMaybeString,
1316 "Change instance's OS name. Does not reinstall the instance."),
1317 ("osparams", None, ht.TMaybeDict, "Per-instance OS parameters"),
1318 ("wait_for_sync", True, ht.TBool,
1319 "Whether to wait for the disk to synchronize, when changing template"),
1320 ]
1321 OP_RESULT = _TSetParamsResult
1322
1325 """Grow a disk of an instance."""
1326 OP_DSC_FIELD = "instance_name"
1327 OP_PARAMS = [
1328 _PInstanceName,
1329 _PWaitForSync,
1330 ("disk", ht.NoDefault, ht.TInt, "Disk index"),
1331 ("amount", ht.NoDefault, ht.TInt,
1332 "Amount of disk space to add (megabytes)"),
1333 ]
1334
1337 """Moves an instance to another node group."""
1338 OP_DSC_FIELD = "instance_name"
1339 OP_PARAMS = [
1340 _PInstanceName,
1341 _PEarlyRelease,
1342 ("iallocator", None, ht.TMaybeString, "Iallocator for computing solution"),
1343 ("target_groups", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
1344 "Destination group names or UUIDs (defaults to \"all but current group\""),
1345 ]
1346 OP_RESULT = TJobIdListOnly
1347
1359
1370
1379
1390
1398
1407
1410 """Evacuate a node group in the cluster."""
1411 OP_DSC_FIELD = "group_name"
1412 OP_PARAMS = [
1413 _PGroupName,
1414 _PEarlyRelease,
1415 ("iallocator", None, ht.TMaybeString, "Iallocator for computing solution"),
1416 ("target_groups", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
1417 "Destination group names or UUIDs"),
1418 ]
1419 OP_RESULT = TJobIdListOnly
1420
1430
1440
1455
1458 """Export an instance.
1459
1460 For local exports, the export destination is the node name. For remote
1461 exports, the export destination is a list of tuples, each consisting of
1462 hostname/IP address, port, HMAC and HMAC salt. The HMAC is calculated using
1463 the cluster domain secret over the value "${index}:${hostname}:${port}". The
1464 destination X509 CA must be a signed certificate.
1465
1466 @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1467 @ivar target_node: Export destination
1468 @ivar x509_key_name: X509 key to use (remote export only)
1469 @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
1470 only)
1471
1472 """
1473 OP_DSC_FIELD = "instance_name"
1474 OP_PARAMS = [
1475 _PInstanceName,
1476 _PShutdownTimeout,
1477
1478
1479 ("target_node", ht.NoDefault, ht.TOr(ht.TNonEmptyString, ht.TList),
1480 "Destination information, depends on export mode"),
1481 ("shutdown", True, ht.TBool, "Whether to shutdown instance before export"),
1482 ("remove_instance", False, ht.TBool,
1483 "Whether to remove instance after export"),
1484 ("ignore_remove_failures", False, ht.TBool,
1485 "Whether to ignore failures while removing instances"),
1486 ("mode", constants.EXPORT_MODE_LOCAL, ht.TElemOf(constants.EXPORT_MODES),
1487 "Export mode"),
1488 ("x509_key_name", None, ht.TOr(ht.TList, ht.TNone),
1489 "Name of X509 key (remote export only)"),
1490 ("destination_x509_ca", None, ht.TMaybeString,
1491 "Destination X509 CA (remote export only)"),
1492 ]
1493
1501
1512
1520
1530
1540
1544 """Sleeps for a configured amount of time.
1545
1546 This is used just for debugging and testing.
1547
1548 Parameters:
1549 - duration: the time to sleep
1550 - on_master: if true, sleep on the master
1551 - on_nodes: list of nodes in which to sleep
1552
1553 If the on_master parameter is true, it will execute a sleep on the
1554 master (before any node sleep).
1555
1556 If the on_nodes list is not empty, it will sleep on those nodes
1557 (after the sleep on the master, if that is enabled).
1558
1559 As an additional feature, the case of duration < 0 will be reported
1560 as an execution error, so this opcode can be used as a failure
1561 generator. The case of duration == 0 will not be treated specially.
1562
1563 """
1564 OP_DSC_FIELD = "duration"
1565 OP_PARAMS = [
1566 ("duration", ht.NoDefault, ht.TNumber, None),
1567 ("on_master", True, ht.TBool, None),
1568 ("on_nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1569 ("repeat", 0, ht.TPositiveInt, None),
1570 ]
1571
1574 """Allocator framework testing.
1575
1576 This opcode has two modes:
1577 - gather and return allocator input for a given mode (allocate new
1578 or replace secondary) and a given instance definition (direction
1579 'in')
1580 - run a selected allocator for a given operation (as above) and
1581 return the allocator output (direction 'out')
1582
1583 """
1584 OP_DSC_FIELD = "allocator"
1585 OP_PARAMS = [
1586 ("direction", ht.NoDefault,
1587 ht.TElemOf(constants.VALID_IALLOCATOR_DIRECTIONS), None),
1588 ("mode", ht.NoDefault, ht.TElemOf(constants.VALID_IALLOCATOR_MODES), None),
1589 ("name", ht.NoDefault, ht.TNonEmptyString, None),
1590 ("nics", ht.NoDefault, ht.TOr(ht.TNone, ht.TListOf(
1591 ht.TDictOf(ht.TElemOf([constants.INIC_MAC, constants.INIC_IP, "bridge"]),
1592 ht.TOr(ht.TNone, ht.TNonEmptyString)))), None),
1593 ("disks", ht.NoDefault, ht.TOr(ht.TNone, ht.TList), None),
1594 ("hypervisor", None, ht.TMaybeString, None),
1595 ("allocator", None, ht.TMaybeString, None),
1596 ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1597 ("memory", None, ht.TOr(ht.TNone, ht.TPositiveInt), None),
1598 ("vcpus", None, ht.TOr(ht.TNone, ht.TPositiveInt), None),
1599 ("os", None, ht.TMaybeString, None),
1600 ("disk_template", None, ht.TMaybeString, None),
1601 ("instances", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
1602 None),
1603 ("evac_mode", None,
1604 ht.TOr(ht.TNone, ht.TElemOf(constants.IALLOCATOR_NEVAC_MODES)), None),
1605 ("target_groups", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
1606 None),
1607 ]
1608
1611 """Utility opcode to test some aspects of the job queue.
1612
1613 """
1614 OP_PARAMS = [
1615 ("notify_waitlock", False, ht.TBool, None),
1616 ("notify_exec", False, ht.TBool, None),
1617 ("log_messages", ht.EmptyList, ht.TListOf(ht.TString), None),
1618 ("fail", False, ht.TBool, None),
1619 ]
1620
1623 """Utility opcode used by unittests.
1624
1625 """
1626 OP_PARAMS = [
1627 ("result", ht.NoDefault, ht.NoType, None),
1628 ("messages", ht.NoDefault, ht.NoType, None),
1629 ("fail", ht.NoDefault, ht.NoType, None),
1630 ("submit_jobs", None, ht.NoType, None),
1631 ]
1632 WITH_LU = False
1633
1636 """Returns list of all defined opcodes.
1637
1638 Does not eliminate duplicates by C{OP_ID}.
1639
1640 """
1641 return [v for v in globals().values()
1642 if (isinstance(v, type) and issubclass(v, OpCode) and
1643 hasattr(v, "OP_ID") and v is not OpCode)]
1644
1645
1646 OP_MAPPING = dict((v.OP_ID, v) for v in _GetOpList())
1647