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 """
38 """A simple serializable object.
39
40 This object serves as a parent class for OpCode without any custom
41 field handling.
42
43 """
44 __slots__ = []
45
47 """Constructor for BaseOpCode.
48
49 The constructor takes only keyword arguments and will set
50 attributes on this object based on the passed arguments. As such,
51 it means that you should not pass arguments which are not in the
52 __slots__ attribute for this class.
53
54 """
55 slots = self._all_slots()
56 for key in kwargs:
57 if key not in slots:
58 raise TypeError("Object %s doesn't support the parameter '%s'" %
59 (self.__class__.__name__, key))
60 setattr(self, key, kwargs[key])
61
63 """Generic serializer.
64
65 This method just returns the contents of the instance as a
66 dictionary.
67
68 @rtype: C{dict}
69 @return: the instance attributes and their values
70
71 """
72 state = {}
73 for name in self._all_slots():
74 if hasattr(self, name):
75 state[name] = getattr(self, name)
76 return state
77
79 """Generic unserializer.
80
81 This method just restores from the serialized state the attributes
82 of the current instance.
83
84 @param state: the serialized opcode data
85 @type state: C{dict}
86
87 """
88 if not isinstance(state, dict):
89 raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
90 type(state))
91
92 for name in self._all_slots():
93 if name not in state and hasattr(self, name):
94 delattr(self, name)
95
96 for name in state:
97 setattr(self, name, state[name])
98
99 @classmethod
101 """Compute the list of all declared slots for a class.
102
103 """
104 slots = []
105 for parent in cls.__mro__:
106 slots.extend(getattr(parent, "__slots__", []))
107 return slots
108
111 """Abstract OpCode.
112
113 This is the root of the actual OpCode hierarchy. All clases derived
114 from this class should override OP_ID.
115
116 @cvar OP_ID: The ID of this opcode. This should be unique amongst all
117 children of this class.
118 @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
119 the check steps
120 @ivar priority: Opcode priority for queue
121
122 """
123 OP_ID = "OP_ABSTRACT"
124 __slots__ = ["dry_run", "debug_level", "priority"]
125
127 """Specialized getstate for opcodes.
128
129 This method adds to the state dictionary the OP_ID of the class,
130 so that on unload we can identify the correct class for
131 instantiating the opcode.
132
133 @rtype: C{dict}
134 @return: the state as a dictionary
135
136 """
137 data = BaseOpCode.__getstate__(self)
138 data["OP_ID"] = self.OP_ID
139 return data
140
141 @classmethod
143 """Generic load opcode method.
144
145 The method identifies the correct opcode class from the dict-form
146 by looking for a OP_ID key, if this is not found, or its value is
147 not available in this module as a child of this class, we fail.
148
149 @type data: C{dict}
150 @param data: the serialized opcode
151
152 """
153 if not isinstance(data, dict):
154 raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
155 if "OP_ID" not in data:
156 raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
157 op_id = data["OP_ID"]
158 op_class = None
159 if op_id in OP_MAPPING:
160 op_class = OP_MAPPING[op_id]
161 else:
162 raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
163 op_id)
164 op = op_class()
165 new_data = data.copy()
166 del new_data["OP_ID"]
167 op.__setstate__(new_data)
168 return op
169
171 """Generates a summary description of this opcode.
172
173 """
174
175 txt = self.OP_ID[3:]
176 field_name = getattr(self, "OP_DSC_FIELD", None)
177 if field_name:
178 field_value = getattr(self, field_name, None)
179 if isinstance(field_value, (list, tuple)):
180 field_value = ",".join(str(i) for i in field_value)
181 txt = "%s(%s)" % (txt, field_value)
182 return txt
183
184
185
186
187 -class OpPostInitCluster(OpCode):
188 """Post cluster initialization.
189
190 This opcode does not touch the cluster at all. Its purpose is to run hooks
191 after the cluster has been initialized.
192
193 """
194 OP_ID = "OP_CLUSTER_POST_INIT"
195 __slots__ = []
196
199 """Destroy the cluster.
200
201 This opcode has no other parameters. All the state is irreversibly
202 lost after the execution of this opcode.
203
204 """
205 OP_ID = "OP_CLUSTER_DESTROY"
206 __slots__ = []
207
210 """Query cluster information."""
211 OP_ID = "OP_CLUSTER_QUERY"
212 __slots__ = []
213
216 """Verify the cluster state.
217
218 @type skip_checks: C{list}
219 @ivar skip_checks: steps to be skipped from the verify process; this
220 needs to be a subset of
221 L{constants.VERIFY_OPTIONAL_CHECKS}; currently
222 only L{constants.VERIFY_NPLUSONE_MEM} can be passed
223
224 """
225 OP_ID = "OP_CLUSTER_VERIFY"
226 __slots__ = ["skip_checks", "verbose", "error_codes",
227 "debug_simulate_errors"]
228
231 """Verify the cluster disks.
232
233 Parameters: none
234
235 Result: a tuple of four elements:
236 - list of node names with bad data returned (unreachable, etc.)
237 - dict of node names with broken volume groups (values: error msg)
238 - list of instances with degraded disks (that should be activated)
239 - dict of instances with missing logical volumes (values: (node, vol)
240 pairs with details about the missing volumes)
241
242 In normal operation, all lists should be empty. A non-empty instance
243 list (3rd element of the result) is still ok (errors were fixed) but
244 non-empty node list means some node is down, and probably there are
245 unfixable drbd errors.
246
247 Note that only instances that are drbd-based are taken into
248 consideration. This might need to be revisited in the future.
249
250 """
251 OP_ID = "OP_CLUSTER_VERIFY_DISKS"
252 __slots__ = []
253
256 """Verify the disk sizes of the instances and fixes configuration
257 mimatches.
258
259 Parameters: optional instances list, in case we want to restrict the
260 checks to only a subset of the instances.
261
262 Result: a list of tuples, (instance, disk, new-size) for changed
263 configurations.
264
265 In normal operation, the list should be empty.
266
267 @type instances: list
268 @ivar instances: the list of instances to check, or empty for all instances
269
270 """
271 OP_ID = "OP_CLUSTER_REPAIR_DISK_SIZES"
272 __slots__ = ["instances"]
273
276 """Query cluster configuration values."""
277 OP_ID = "OP_CLUSTER_CONFIG_QUERY"
278 __slots__ = ["output_fields"]
279
282 """Rename the cluster.
283
284 @type name: C{str}
285 @ivar name: The new name of the cluster. The name and/or the master IP
286 address will be changed to match the new name and its IP
287 address.
288
289 """
290 OP_ID = "OP_CLUSTER_RENAME"
291 OP_DSC_FIELD = "name"
292 __slots__ = ["name"]
293
296 """Change the parameters of the cluster.
297
298 @type vg_name: C{str} or C{None}
299 @ivar vg_name: The new volume group name or None to disable LVM usage.
300
301 """
302 OP_ID = "OP_CLUSTER_SET_PARAMS"
303 __slots__ = [
304 "vg_name",
305 "drbd_helper",
306 "enabled_hypervisors",
307 "hvparams",
308 "os_hvp",
309 "beparams",
310 "osparams",
311 "nicparams",
312 "candidate_pool_size",
313 "maintain_node_health",
314 "uid_pool",
315 "add_uids",
316 "remove_uids",
317 "default_iallocator",
318 "reserved_lvs",
319 "hidden_os",
320 "blacklisted_os",
321 "prealloc_wipe_disks",
322 ]
323
326 """Force a full push of the cluster configuration.
327
328 """
329 OP_ID = "OP_CLUSTER_REDIST_CONF"
330 __slots__ = []
331
335 """Remove a node.
336
337 @type node_name: C{str}
338 @ivar node_name: The name of the node to remove. If the node still has
339 instances on it, the operation will fail.
340
341 """
342 OP_ID = "OP_NODE_REMOVE"
343 OP_DSC_FIELD = "node_name"
344 __slots__ = ["node_name"]
345
348 """Add a node to the cluster.
349
350 @type node_name: C{str}
351 @ivar node_name: The name of the node to add. This can be a short name,
352 but it will be expanded to the FQDN.
353 @type primary_ip: IP address
354 @ivar primary_ip: The primary IP of the node. This will be ignored when the
355 opcode is submitted, but will be filled during the node
356 add (so it will be visible in the job query).
357 @type secondary_ip: IP address
358 @ivar secondary_ip: The secondary IP of the node. This needs to be passed
359 if the cluster has been initialized in 'dual-network'
360 mode, otherwise it must not be given.
361 @type readd: C{bool}
362 @ivar readd: Whether to re-add an existing node to the cluster. If
363 this is not passed, then the operation will abort if the node
364 name is already in the cluster; use this parameter to 'repair'
365 a node that had its configuration broken, or was reinstalled
366 without removal from the cluster.
367 @type group: C{str}
368 @ivar group: The node group to which this node will belong.
369 @type vm_capable: C{bool}
370 @ivar vm_capable: The vm_capable node attribute
371 @type master_capable: C{bool}
372 @ivar master_capable: The master_capable node attribute
373
374 """
375 OP_ID = "OP_NODE_ADD"
376 OP_DSC_FIELD = "node_name"
377 __slots__ = ["node_name", "primary_ip", "secondary_ip", "readd", "group",
378 "vm_capable", "master_capable"]
379
382 """Compute the list of nodes."""
383 OP_ID = "OP_NODE_QUERY"
384 __slots__ = ["output_fields", "names", "use_locking"]
385
388 """Get list of volumes on node."""
389 OP_ID = "OP_NODE_QUERYVOLS"
390 __slots__ = ["nodes", "output_fields"]
391
394 """Get information on storage for node(s)."""
395 OP_ID = "OP_NODE_QUERY_STORAGE"
396 __slots__ = [
397 "nodes",
398 "storage_type",
399 "name",
400 "output_fields",
401 ]
402
405 """Modifies the properies of a storage unit"""
406 OP_ID = "OP_NODE_MODIFY_STORAGE"
407 __slots__ = [
408 "node_name",
409 "storage_type",
410 "name",
411 "changes",
412 ]
413
416 """Repairs the volume group on a node."""
417 OP_ID = "OP_REPAIR_NODE_STORAGE"
418 OP_DSC_FIELD = "node_name"
419 __slots__ = [
420 "node_name",
421 "storage_type",
422 "name",
423 "ignore_consistency",
424 ]
425
428 """Change the parameters of a node."""
429 OP_ID = "OP_NODE_SET_PARAMS"
430 OP_DSC_FIELD = "node_name"
431 __slots__ = [
432 "node_name",
433 "force",
434 "master_candidate",
435 "offline",
436 "drained",
437 "auto_promote",
438 "master_capable",
439 "vm_capable",
440 "secondary_ip",
441 ]
442
445 """Tries to powercycle a node."""
446 OP_ID = "OP_NODE_POWERCYCLE"
447 OP_DSC_FIELD = "node_name"
448 __slots__ = [
449 "node_name",
450 "force",
451 ]
452
455 """Migrate all instances from a node."""
456 OP_ID = "OP_NODE_MIGRATE"
457 OP_DSC_FIELD = "node_name"
458 __slots__ = [
459 "node_name",
460 "mode",
461 "live",
462 ]
463
466 """Compute the evacuation strategy for a list of nodes."""
467 OP_ID = "OP_NODE_EVAC_STRATEGY"
468 OP_DSC_FIELD = "nodes"
469 __slots__ = ["nodes", "iallocator", "remote_node"]
470
475 """Create an instance.
476
477 @ivar instance_name: Instance name
478 @ivar mode: Instance creation mode (one of L{constants.INSTANCE_CREATE_MODES})
479 @ivar source_handshake: Signed handshake from source (remote import only)
480 @ivar source_x509_ca: Source X509 CA in PEM format (remote import only)
481 @ivar source_instance_name: Previous name of instance (remote import only)
482
483 """
484 OP_ID = "OP_INSTANCE_CREATE"
485 OP_DSC_FIELD = "instance_name"
486 __slots__ = [
487 "instance_name",
488 "os_type", "force_variant", "no_install",
489 "pnode", "disk_template", "snode", "mode",
490 "disks", "nics",
491 "src_node", "src_path", "start", "identify_defaults",
492 "wait_for_sync", "ip_check", "name_check",
493 "file_storage_dir", "file_driver",
494 "iallocator",
495 "hypervisor", "hvparams", "beparams", "osparams",
496 "source_handshake",
497 "source_x509_ca",
498 "source_instance_name",
499 ]
500
503 """Reinstall an instance's OS."""
504 OP_ID = "OP_INSTANCE_REINSTALL"
505 OP_DSC_FIELD = "instance_name"
506 __slots__ = ["instance_name", "os_type", "force_variant", "osparams"]
507
510 """Remove an instance."""
511 OP_ID = "OP_INSTANCE_REMOVE"
512 OP_DSC_FIELD = "instance_name"
513 __slots__ = [
514 "instance_name",
515 "ignore_failures",
516 "shutdown_timeout",
517 ]
518
521 """Rename an instance."""
522 OP_ID = "OP_INSTANCE_RENAME"
523 __slots__ = [
524 "instance_name", "ip_check", "new_name", "name_check",
525 ]
526
529 """Startup an instance."""
530 OP_ID = "OP_INSTANCE_STARTUP"
531 OP_DSC_FIELD = "instance_name"
532 __slots__ = [
533 "instance_name", "force", "hvparams", "beparams", "ignore_offline_nodes",
534 ]
535
538 """Shutdown an instance."""
539 OP_ID = "OP_INSTANCE_SHUTDOWN"
540 OP_DSC_FIELD = "instance_name"
541 __slots__ = [
542 "instance_name", "timeout", "ignore_offline_nodes",
543 ]
544
547 """Reboot an instance."""
548 OP_ID = "OP_INSTANCE_REBOOT"
549 OP_DSC_FIELD = "instance_name"
550 __slots__ = [
551 "instance_name", "reboot_type", "ignore_secondaries", "shutdown_timeout",
552 ]
553
556 """Replace the disks of an instance."""
557 OP_ID = "OP_INSTANCE_REPLACE_DISKS"
558 OP_DSC_FIELD = "instance_name"
559 __slots__ = [
560 "instance_name", "remote_node", "mode", "disks", "iallocator",
561 "early_release",
562 ]
563
566 """Failover an instance."""
567 OP_ID = "OP_INSTANCE_FAILOVER"
568 OP_DSC_FIELD = "instance_name"
569 __slots__ = [
570 "instance_name", "ignore_consistency", "shutdown_timeout",
571 ]
572
575 """Migrate an instance.
576
577 This migrates (without shutting down an instance) to its secondary
578 node.
579
580 @ivar instance_name: the name of the instance
581 @ivar mode: the migration mode (live, non-live or None for auto)
582
583 """
584 OP_ID = "OP_INSTANCE_MIGRATE"
585 OP_DSC_FIELD = "instance_name"
586 __slots__ = ["instance_name", "mode", "cleanup", "live"]
587
590 """Move an instance.
591
592 This move (with shutting down an instance and data copying) to an
593 arbitrary node.
594
595 @ivar instance_name: the name of the instance
596 @ivar target_node: the destination node
597
598 """
599 OP_ID = "OP_INSTANCE_MOVE"
600 OP_DSC_FIELD = "instance_name"
601 __slots__ = [
602 "instance_name", "target_node", "shutdown_timeout",
603 ]
604
607 """Connect to an instance's console."""
608 OP_ID = "OP_INSTANCE_CONSOLE"
609 OP_DSC_FIELD = "instance_name"
610 __slots__ = ["instance_name"]
611
614 """Activate an instance's disks."""
615 OP_ID = "OP_INSTANCE_ACTIVATE_DISKS"
616 OP_DSC_FIELD = "instance_name"
617 __slots__ = ["instance_name", "ignore_size"]
618
621 """Deactivate an instance's disks."""
622 OP_ID = "OP_INSTANCE_DEACTIVATE_DISKS"
623 OP_DSC_FIELD = "instance_name"
624 __slots__ = ["instance_name"]
625
628 """Deactivate an instance's disks."""
629 OP_ID = "OP_INSTANCE_RECREATE_DISKS"
630 OP_DSC_FIELD = "instance_name"
631 __slots__ = ["instance_name", "disks"]
632
635 """Compute the list of instances."""
636 OP_ID = "OP_INSTANCE_QUERY"
637 __slots__ = ["output_fields", "names", "use_locking"]
638
641 """Compute the run-time status of instances."""
642 OP_ID = "OP_INSTANCE_QUERY_DATA"
643 __slots__ = ["instances", "static"]
644
647 """Change the parameters of an instance."""
648 OP_ID = "OP_INSTANCE_SET_PARAMS"
649 OP_DSC_FIELD = "instance_name"
650 __slots__ = [
651 "instance_name",
652 "hvparams", "beparams", "osparams", "force",
653 "nics", "disks", "disk_template",
654 "remote_node", "os_name", "force_variant",
655 ]
656
659 """Grow a disk of an instance."""
660 OP_ID = "OP_INSTANCE_GROW_DISK"
661 OP_DSC_FIELD = "instance_name"
662 __slots__ = [
663 "instance_name", "disk", "amount", "wait_for_sync",
664 ]
665
669 """Compute the list of guest operating systems."""
670 OP_ID = "OP_OS_DIAGNOSE"
671 __slots__ = ["output_fields", "names"]
672
676 """Compute the list of exported images."""
677 OP_ID = "OP_BACKUP_QUERY"
678 __slots__ = ["nodes", "use_locking"]
679
682 """Prepares an instance export.
683
684 @ivar instance_name: Instance name
685 @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
686
687 """
688 OP_ID = "OP_BACKUP_PREPARE"
689 OP_DSC_FIELD = "instance_name"
690 __slots__ = [
691 "instance_name", "mode",
692 ]
693
696 """Export an instance.
697
698 For local exports, the export destination is the node name. For remote
699 exports, the export destination is a list of tuples, each consisting of
700 hostname/IP address, port, HMAC and HMAC salt. The HMAC is calculated using
701 the cluster domain secret over the value "${index}:${hostname}:${port}". The
702 destination X509 CA must be a signed certificate.
703
704 @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
705 @ivar target_node: Export destination
706 @ivar x509_key_name: X509 key to use (remote export only)
707 @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
708 only)
709
710 """
711 OP_ID = "OP_BACKUP_EXPORT"
712 OP_DSC_FIELD = "instance_name"
713 __slots__ = [
714
715
716 "instance_name", "target_node", "shutdown", "shutdown_timeout",
717 "remove_instance",
718 "ignore_remove_failures",
719 "mode",
720 "x509_key_name",
721 "destination_x509_ca",
722 ]
723
726 """Remove an instance's export."""
727 OP_ID = "OP_BACKUP_REMOVE"
728 OP_DSC_FIELD = "instance_name"
729 __slots__ = ["instance_name"]
730
738
745
751
757
761 """Sleeps for a configured amount of time.
762
763 This is used just for debugging and testing.
764
765 Parameters:
766 - duration: the time to sleep
767 - on_master: if true, sleep on the master
768 - on_nodes: list of nodes in which to sleep
769
770 If the on_master parameter is true, it will execute a sleep on the
771 master (before any node sleep).
772
773 If the on_nodes list is not empty, it will sleep on those nodes
774 (after the sleep on the master, if that is enabled).
775
776 As an additional feature, the case of duration < 0 will be reported
777 as an execution error, so this opcode can be used as a failure
778 generator. The case of duration == 0 will not be treated specially.
779
780 """
781 OP_ID = "OP_TEST_DELAY"
782 OP_DSC_FIELD = "duration"
783 __slots__ = ["duration", "on_master", "on_nodes", "repeat"]
784
787 """Allocator framework testing.
788
789 This opcode has two modes:
790 - gather and return allocator input for a given mode (allocate new
791 or replace secondary) and a given instance definition (direction
792 'in')
793 - run a selected allocator for a given operation (as above) and
794 return the allocator output (direction 'out')
795
796 """
797 OP_ID = "OP_TEST_ALLOCATOR"
798 OP_DSC_FIELD = "allocator"
799 __slots__ = [
800 "direction", "mode", "allocator", "name",
801 "mem_size", "disks", "disk_template",
802 "os", "tags", "nics", "vcpus", "hypervisor",
803 "evac_nodes",
804 ]
805
808 """Utility opcode to test some aspects of the job queue.
809
810 """
811 OP_ID = "OP_TEST_JQUEUE"
812 __slots__ = [
813 "notify_waitlock",
814 "notify_exec",
815 "log_messages",
816 "fail",
817 ]
818
821 """Utility opcode used by unittests.
822
823 """
824 OP_ID = "OP_TEST_DUMMY"
825 __slots__ = [
826 "result",
827 "messages",
828 "fail",
829 ]
830
831
832 OP_MAPPING = dict([(v.OP_ID, v) for v in globals().values()
833 if (isinstance(v, type) and issubclass(v, OpCode) and
834 hasattr(v, "OP_ID"))])
835