opcodes: document OP_DSC_FIELD in OpCode and OpCode.Summary()
[ganeti-local] / lib / opcodes.py
1 #
2 #
3
4 # Copyright (C) 2006, 2007, 2008, 2009, 2010 Google Inc.
5 #
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.
10 #
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.
15 #
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
19 # 02110-1301, USA.
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 # this are practically structures, so disable the message about too
33 # few public methods:
34 # pylint: disable-msg=R0903
35
36
37 class BaseOpCode(object):
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
46   def __init__(self, **kwargs):
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
62   def __getstate__(self):
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
78   def __setstate__(self, state):
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
100   def _all_slots(cls):
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
109
110 class OpCode(BaseOpCode):
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   @cvar OP_DSC_FIELD: The name of a field whose value will be included in the
119                       string returned by Summary(); see the docstring of that
120                       method for details).
121   @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
122                  the check steps
123   @ivar priority: Opcode priority for queue
124
125   """
126   OP_ID = "OP_ABSTRACT"
127   __slots__ = ["dry_run", "debug_level", "priority"]
128
129   def __getstate__(self):
130     """Specialized getstate for opcodes.
131
132     This method adds to the state dictionary the OP_ID of the class,
133     so that on unload we can identify the correct class for
134     instantiating the opcode.
135
136     @rtype:   C{dict}
137     @return:  the state as a dictionary
138
139     """
140     data = BaseOpCode.__getstate__(self)
141     data["OP_ID"] = self.OP_ID
142     return data
143
144   @classmethod
145   def LoadOpCode(cls, data):
146     """Generic load opcode method.
147
148     The method identifies the correct opcode class from the dict-form
149     by looking for a OP_ID key, if this is not found, or its value is
150     not available in this module as a child of this class, we fail.
151
152     @type data:  C{dict}
153     @param data: the serialized opcode
154
155     """
156     if not isinstance(data, dict):
157       raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
158     if "OP_ID" not in data:
159       raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
160     op_id = data["OP_ID"]
161     op_class = None
162     if op_id in OP_MAPPING:
163       op_class = OP_MAPPING[op_id]
164     else:
165       raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
166                        op_id)
167     op = op_class()
168     new_data = data.copy()
169     del new_data["OP_ID"]
170     op.__setstate__(new_data)
171     return op
172
173   def Summary(self):
174     """Generates a summary description of this opcode.
175
176     The summary is the value of the OP_ID attribute (without the "OP_" prefix),
177     plus the value of the OP_DSC_FIELD attribute, if one was defined; this field
178     should allow to easily identify the operation (for an instance creation job,
179     e.g., it would be the instance name).
180
181     """
182     # all OP_ID start with OP_, we remove that
183     txt = self.OP_ID[3:]
184     field_name = getattr(self, "OP_DSC_FIELD", None)
185     if field_name:
186       field_value = getattr(self, field_name, None)
187       if isinstance(field_value, (list, tuple)):
188         field_value = ",".join(str(i) for i in field_value)
189       txt = "%s(%s)" % (txt, field_value)
190     return txt
191
192
193 # cluster opcodes
194
195 class OpPostInitCluster(OpCode):
196   """Post cluster initialization.
197
198   This opcode does not touch the cluster at all. Its purpose is to run hooks
199   after the cluster has been initialized.
200
201   """
202   OP_ID = "OP_CLUSTER_POST_INIT"
203   __slots__ = []
204
205
206 class OpDestroyCluster(OpCode):
207   """Destroy the cluster.
208
209   This opcode has no other parameters. All the state is irreversibly
210   lost after the execution of this opcode.
211
212   """
213   OP_ID = "OP_CLUSTER_DESTROY"
214   __slots__ = []
215
216
217 class OpQueryClusterInfo(OpCode):
218   """Query cluster information."""
219   OP_ID = "OP_CLUSTER_QUERY"
220   __slots__ = []
221
222
223 class OpVerifyCluster(OpCode):
224   """Verify the cluster state.
225
226   @type skip_checks: C{list}
227   @ivar skip_checks: steps to be skipped from the verify process; this
228                      needs to be a subset of
229                      L{constants.VERIFY_OPTIONAL_CHECKS}; currently
230                      only L{constants.VERIFY_NPLUSONE_MEM} can be passed
231
232   """
233   OP_ID = "OP_CLUSTER_VERIFY"
234   __slots__ = ["skip_checks", "verbose", "error_codes",
235                "debug_simulate_errors"]
236
237
238 class OpVerifyDisks(OpCode):
239   """Verify the cluster disks.
240
241   Parameters: none
242
243   Result: a tuple of four elements:
244     - list of node names with bad data returned (unreachable, etc.)
245     - dict of node names with broken volume groups (values: error msg)
246     - list of instances with degraded disks (that should be activated)
247     - dict of instances with missing logical volumes (values: (node, vol)
248       pairs with details about the missing volumes)
249
250   In normal operation, all lists should be empty. A non-empty instance
251   list (3rd element of the result) is still ok (errors were fixed) but
252   non-empty node list means some node is down, and probably there are
253   unfixable drbd errors.
254
255   Note that only instances that are drbd-based are taken into
256   consideration. This might need to be revisited in the future.
257
258   """
259   OP_ID = "OP_CLUSTER_VERIFY_DISKS"
260   __slots__ = []
261
262
263 class OpRepairDiskSizes(OpCode):
264   """Verify the disk sizes of the instances and fixes configuration
265   mimatches.
266
267   Parameters: optional instances list, in case we want to restrict the
268   checks to only a subset of the instances.
269
270   Result: a list of tuples, (instance, disk, new-size) for changed
271   configurations.
272
273   In normal operation, the list should be empty.
274
275   @type instances: list
276   @ivar instances: the list of instances to check, or empty for all instances
277
278   """
279   OP_ID = "OP_CLUSTER_REPAIR_DISK_SIZES"
280   __slots__ = ["instances"]
281
282
283 class OpQueryConfigValues(OpCode):
284   """Query cluster configuration values."""
285   OP_ID = "OP_CLUSTER_CONFIG_QUERY"
286   __slots__ = ["output_fields"]
287
288
289 class OpRenameCluster(OpCode):
290   """Rename the cluster.
291
292   @type name: C{str}
293   @ivar name: The new name of the cluster. The name and/or the master IP
294               address will be changed to match the new name and its IP
295               address.
296
297   """
298   OP_ID = "OP_CLUSTER_RENAME"
299   OP_DSC_FIELD = "name"
300   __slots__ = ["name"]
301
302
303 class OpSetClusterParams(OpCode):
304   """Change the parameters of the cluster.
305
306   @type vg_name: C{str} or C{None}
307   @ivar vg_name: The new volume group name or None to disable LVM usage.
308
309   """
310   OP_ID = "OP_CLUSTER_SET_PARAMS"
311   __slots__ = [
312     "vg_name",
313     "drbd_helper",
314     "enabled_hypervisors",
315     "hvparams",
316     "os_hvp",
317     "beparams",
318     "osparams",
319     "nicparams",
320     "ndparams",
321     "candidate_pool_size",
322     "maintain_node_health",
323     "uid_pool",
324     "add_uids",
325     "remove_uids",
326     "default_iallocator",
327     "reserved_lvs",
328     "hidden_os",
329     "blacklisted_os",
330     "prealloc_wipe_disks",
331     ]
332
333
334 class OpRedistributeConfig(OpCode):
335   """Force a full push of the cluster configuration.
336
337   """
338   OP_ID = "OP_CLUSTER_REDIST_CONF"
339   __slots__ = []
340
341
342 class OpQuery(OpCode):
343   """Query for resources/items.
344
345   @ivar what: Resources to query for, must be one of L{constants.QR_OP_QUERY}
346   @ivar fields: List of fields to retrieve
347   @ivar filter: Query filter
348
349   """
350   OP_ID = "OP_QUERY"
351   __slots__ = [
352     "what",
353     "fields",
354     "filter",
355     ]
356
357
358 class OpQueryFields(OpCode):
359   """Query for available resource/item fields.
360
361   @ivar what: Resources to query for, must be one of L{constants.QR_OP_QUERY}
362   @ivar fields: List of fields to retrieve
363
364   """
365   OP_ID = "OP_QUERY_FIELDS"
366   __slots__ = [
367     "what",
368     "fields",
369     ]
370
371
372 # node opcodes
373
374 class OpRemoveNode(OpCode):
375   """Remove a node.
376
377   @type node_name: C{str}
378   @ivar node_name: The name of the node to remove. If the node still has
379                    instances on it, the operation will fail.
380
381   """
382   OP_ID = "OP_NODE_REMOVE"
383   OP_DSC_FIELD = "node_name"
384   __slots__ = ["node_name"]
385
386
387 class OpAddNode(OpCode):
388   """Add a node to the cluster.
389
390   @type node_name: C{str}
391   @ivar node_name: The name of the node to add. This can be a short name,
392                    but it will be expanded to the FQDN.
393   @type primary_ip: IP address
394   @ivar primary_ip: The primary IP of the node. This will be ignored when the
395                     opcode is submitted, but will be filled during the node
396                     add (so it will be visible in the job query).
397   @type secondary_ip: IP address
398   @ivar secondary_ip: The secondary IP of the node. This needs to be passed
399                       if the cluster has been initialized in 'dual-network'
400                       mode, otherwise it must not be given.
401   @type readd: C{bool}
402   @ivar readd: Whether to re-add an existing node to the cluster. If
403                this is not passed, then the operation will abort if the node
404                name is already in the cluster; use this parameter to 'repair'
405                a node that had its configuration broken, or was reinstalled
406                without removal from the cluster.
407   @type group: C{str}
408   @ivar group: The node group to which this node will belong.
409   @type vm_capable: C{bool}
410   @ivar vm_capable: The vm_capable node attribute
411   @type master_capable: C{bool}
412   @ivar master_capable: The master_capable node attribute
413
414   """
415   OP_ID = "OP_NODE_ADD"
416   OP_DSC_FIELD = "node_name"
417   __slots__ = ["node_name", "primary_ip", "secondary_ip", "readd", "group",
418                "vm_capable", "master_capable", "ndparams"]
419
420
421 class OpQueryNodes(OpCode):
422   """Compute the list of nodes."""
423   OP_ID = "OP_NODE_QUERY"
424   __slots__ = ["output_fields", "names", "use_locking"]
425
426
427 class OpQueryNodeVolumes(OpCode):
428   """Get list of volumes on node."""
429   OP_ID = "OP_NODE_QUERYVOLS"
430   __slots__ = ["nodes", "output_fields"]
431
432
433 class OpQueryNodeStorage(OpCode):
434   """Get information on storage for node(s)."""
435   OP_ID = "OP_NODE_QUERY_STORAGE"
436   __slots__ = [
437     "nodes",
438     "storage_type",
439     "name",
440     "output_fields",
441     ]
442
443
444 class OpModifyNodeStorage(OpCode):
445   """Modifies the properies of a storage unit"""
446   OP_ID = "OP_NODE_MODIFY_STORAGE"
447   __slots__ = [
448     "node_name",
449     "storage_type",
450     "name",
451     "changes",
452     ]
453
454
455 class OpRepairNodeStorage(OpCode):
456   """Repairs the volume group on a node."""
457   OP_ID = "OP_REPAIR_NODE_STORAGE"
458   OP_DSC_FIELD = "node_name"
459   __slots__ = [
460     "node_name",
461     "storage_type",
462     "name",
463     "ignore_consistency",
464     ]
465
466
467 class OpSetNodeParams(OpCode):
468   """Change the parameters of a node."""
469   OP_ID = "OP_NODE_SET_PARAMS"
470   OP_DSC_FIELD = "node_name"
471   __slots__ = [
472     "node_name",
473     "force",
474     "master_candidate",
475     "offline",
476     "drained",
477     "auto_promote",
478     "master_capable",
479     "vm_capable",
480     "secondary_ip",
481     "ndparams",
482     ]
483
484
485 class OpPowercycleNode(OpCode):
486   """Tries to powercycle a node."""
487   OP_ID = "OP_NODE_POWERCYCLE"
488   OP_DSC_FIELD = "node_name"
489   __slots__ = [
490     "node_name",
491     "force",
492     ]
493
494
495 class OpMigrateNode(OpCode):
496   """Migrate all instances from a node."""
497   OP_ID = "OP_NODE_MIGRATE"
498   OP_DSC_FIELD = "node_name"
499   __slots__ = [
500     "node_name",
501     "mode",
502     "live",
503     ]
504
505
506 class OpNodeEvacuationStrategy(OpCode):
507   """Compute the evacuation strategy for a list of nodes."""
508   OP_ID = "OP_NODE_EVAC_STRATEGY"
509   OP_DSC_FIELD = "nodes"
510   __slots__ = ["nodes", "iallocator", "remote_node"]
511
512
513 # instance opcodes
514
515 class OpCreateInstance(OpCode):
516   """Create an instance.
517
518   @ivar instance_name: Instance name
519   @ivar mode: Instance creation mode (one of L{constants.INSTANCE_CREATE_MODES})
520   @ivar source_handshake: Signed handshake from source (remote import only)
521   @ivar source_x509_ca: Source X509 CA in PEM format (remote import only)
522   @ivar source_instance_name: Previous name of instance (remote import only)
523   @ivar source_shutdown_timeout: Shutdown timeout used for source instance
524     (remote import only)
525
526   """
527   OP_ID = "OP_INSTANCE_CREATE"
528   OP_DSC_FIELD = "instance_name"
529   __slots__ = [
530     "instance_name",
531     "os_type", "force_variant", "no_install",
532     "pnode", "disk_template", "snode", "mode",
533     "disks", "nics",
534     "src_node", "src_path", "start", "identify_defaults",
535     "wait_for_sync", "ip_check", "name_check",
536     "file_storage_dir", "file_driver",
537     "iallocator",
538     "hypervisor", "hvparams", "beparams", "osparams",
539     "source_handshake",
540     "source_x509_ca",
541     "source_instance_name",
542     "source_shutdown_timeout",
543     ]
544
545
546 class OpReinstallInstance(OpCode):
547   """Reinstall an instance's OS."""
548   OP_ID = "OP_INSTANCE_REINSTALL"
549   OP_DSC_FIELD = "instance_name"
550   __slots__ = ["instance_name", "os_type", "force_variant", "osparams"]
551
552
553 class OpRemoveInstance(OpCode):
554   """Remove an instance."""
555   OP_ID = "OP_INSTANCE_REMOVE"
556   OP_DSC_FIELD = "instance_name"
557   __slots__ = [
558     "instance_name",
559     "ignore_failures",
560     "shutdown_timeout",
561     ]
562
563
564 class OpRenameInstance(OpCode):
565   """Rename an instance."""
566   OP_ID = "OP_INSTANCE_RENAME"
567   __slots__ = [
568     "instance_name", "ip_check", "new_name", "name_check",
569     ]
570
571
572 class OpStartupInstance(OpCode):
573   """Startup an instance."""
574   OP_ID = "OP_INSTANCE_STARTUP"
575   OP_DSC_FIELD = "instance_name"
576   __slots__ = [
577     "instance_name", "force", "hvparams", "beparams", "ignore_offline_nodes",
578     ]
579
580
581 class OpShutdownInstance(OpCode):
582   """Shutdown an instance."""
583   OP_ID = "OP_INSTANCE_SHUTDOWN"
584   OP_DSC_FIELD = "instance_name"
585   __slots__ = [
586     "instance_name", "timeout", "ignore_offline_nodes",
587     ]
588
589
590 class OpRebootInstance(OpCode):
591   """Reboot an instance."""
592   OP_ID = "OP_INSTANCE_REBOOT"
593   OP_DSC_FIELD = "instance_name"
594   __slots__ = [
595     "instance_name", "reboot_type", "ignore_secondaries", "shutdown_timeout",
596     ]
597
598
599 class OpReplaceDisks(OpCode):
600   """Replace the disks of an instance."""
601   OP_ID = "OP_INSTANCE_REPLACE_DISKS"
602   OP_DSC_FIELD = "instance_name"
603   __slots__ = [
604     "instance_name", "remote_node", "mode", "disks", "iallocator",
605     "early_release",
606     ]
607
608
609 class OpFailoverInstance(OpCode):
610   """Failover an instance."""
611   OP_ID = "OP_INSTANCE_FAILOVER"
612   OP_DSC_FIELD = "instance_name"
613   __slots__ = [
614     "instance_name", "ignore_consistency", "shutdown_timeout",
615     ]
616
617
618 class OpMigrateInstance(OpCode):
619   """Migrate an instance.
620
621   This migrates (without shutting down an instance) to its secondary
622   node.
623
624   @ivar instance_name: the name of the instance
625   @ivar mode: the migration mode (live, non-live or None for auto)
626
627   """
628   OP_ID = "OP_INSTANCE_MIGRATE"
629   OP_DSC_FIELD = "instance_name"
630   __slots__ = ["instance_name", "mode", "cleanup", "live"]
631
632
633 class OpMoveInstance(OpCode):
634   """Move an instance.
635
636   This move (with shutting down an instance and data copying) to an
637   arbitrary node.
638
639   @ivar instance_name: the name of the instance
640   @ivar target_node: the destination node
641
642   """
643   OP_ID = "OP_INSTANCE_MOVE"
644   OP_DSC_FIELD = "instance_name"
645   __slots__ = [
646     "instance_name", "target_node", "shutdown_timeout",
647     ]
648
649
650 class OpConnectConsole(OpCode):
651   """Connect to an instance's console."""
652   OP_ID = "OP_INSTANCE_CONSOLE"
653   OP_DSC_FIELD = "instance_name"
654   __slots__ = ["instance_name"]
655
656
657 class OpActivateInstanceDisks(OpCode):
658   """Activate an instance's disks."""
659   OP_ID = "OP_INSTANCE_ACTIVATE_DISKS"
660   OP_DSC_FIELD = "instance_name"
661   __slots__ = ["instance_name", "ignore_size"]
662
663
664 class OpDeactivateInstanceDisks(OpCode):
665   """Deactivate an instance's disks."""
666   OP_ID = "OP_INSTANCE_DEACTIVATE_DISKS"
667   OP_DSC_FIELD = "instance_name"
668   __slots__ = ["instance_name"]
669
670
671 class OpRecreateInstanceDisks(OpCode):
672   """Deactivate an instance's disks."""
673   OP_ID = "OP_INSTANCE_RECREATE_DISKS"
674   OP_DSC_FIELD = "instance_name"
675   __slots__ = ["instance_name", "disks"]
676
677
678 class OpQueryInstances(OpCode):
679   """Compute the list of instances."""
680   OP_ID = "OP_INSTANCE_QUERY"
681   __slots__ = ["output_fields", "names", "use_locking"]
682
683
684 class OpQueryInstanceData(OpCode):
685   """Compute the run-time status of instances."""
686   OP_ID = "OP_INSTANCE_QUERY_DATA"
687   __slots__ = ["instances", "static"]
688
689
690 class OpSetInstanceParams(OpCode):
691   """Change the parameters of an instance."""
692   OP_ID = "OP_INSTANCE_SET_PARAMS"
693   OP_DSC_FIELD = "instance_name"
694   __slots__ = [
695     "instance_name",
696     "hvparams", "beparams", "osparams", "force",
697     "nics", "disks", "disk_template",
698     "remote_node", "os_name", "force_variant",
699     ]
700
701
702 class OpGrowDisk(OpCode):
703   """Grow a disk of an instance."""
704   OP_ID = "OP_INSTANCE_GROW_DISK"
705   OP_DSC_FIELD = "instance_name"
706   __slots__ = [
707     "instance_name", "disk", "amount", "wait_for_sync",
708     ]
709
710
711 # Node group opcodes
712
713 class OpQueryGroups(OpCode):
714   """Compute the list of node groups."""
715   OP_ID = "OP_GROUP_QUERY"
716   __slots__ = ["output_fields", "names"]
717
718
719 # OS opcodes
720 class OpDiagnoseOS(OpCode):
721   """Compute the list of guest operating systems."""
722   OP_ID = "OP_OS_DIAGNOSE"
723   __slots__ = ["output_fields", "names"]
724
725
726 # Exports opcodes
727 class OpQueryExports(OpCode):
728   """Compute the list of exported images."""
729   OP_ID = "OP_BACKUP_QUERY"
730   __slots__ = ["nodes", "use_locking"]
731
732
733 class OpPrepareExport(OpCode):
734   """Prepares an instance export.
735
736   @ivar instance_name: Instance name
737   @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
738
739   """
740   OP_ID = "OP_BACKUP_PREPARE"
741   OP_DSC_FIELD = "instance_name"
742   __slots__ = [
743     "instance_name", "mode",
744     ]
745
746
747 class OpExportInstance(OpCode):
748   """Export an instance.
749
750   For local exports, the export destination is the node name. For remote
751   exports, the export destination is a list of tuples, each consisting of
752   hostname/IP address, port, HMAC and HMAC salt. The HMAC is calculated using
753   the cluster domain secret over the value "${index}:${hostname}:${port}". The
754   destination X509 CA must be a signed certificate.
755
756   @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
757   @ivar target_node: Export destination
758   @ivar x509_key_name: X509 key to use (remote export only)
759   @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
760                              only)
761
762   """
763   OP_ID = "OP_BACKUP_EXPORT"
764   OP_DSC_FIELD = "instance_name"
765   __slots__ = [
766     # TODO: Rename target_node as it changes meaning for different export modes
767     # (e.g. "destination")
768     "instance_name", "target_node", "shutdown", "shutdown_timeout",
769     "remove_instance",
770     "ignore_remove_failures",
771     "mode",
772     "x509_key_name",
773     "destination_x509_ca",
774     ]
775
776
777 class OpRemoveExport(OpCode):
778   """Remove an instance's export."""
779   OP_ID = "OP_BACKUP_REMOVE"
780   OP_DSC_FIELD = "instance_name"
781   __slots__ = ["instance_name"]
782
783
784 # Tags opcodes
785 class OpGetTags(OpCode):
786   """Returns the tags of the given object."""
787   OP_ID = "OP_TAGS_GET"
788   OP_DSC_FIELD = "name"
789   __slots__ = ["kind", "name"]
790
791
792 class OpSearchTags(OpCode):
793   """Searches the tags in the cluster for a given pattern."""
794   OP_ID = "OP_TAGS_SEARCH"
795   OP_DSC_FIELD = "pattern"
796   __slots__ = ["pattern"]
797
798
799 class OpAddTags(OpCode):
800   """Add a list of tags on a given object."""
801   OP_ID = "OP_TAGS_SET"
802   __slots__ = ["kind", "name", "tags"]
803
804
805 class OpDelTags(OpCode):
806   """Remove a list of tags from a given object."""
807   OP_ID = "OP_TAGS_DEL"
808   __slots__ = ["kind", "name", "tags"]
809
810
811 # Test opcodes
812 class OpTestDelay(OpCode):
813   """Sleeps for a configured amount of time.
814
815   This is used just for debugging and testing.
816
817   Parameters:
818     - duration: the time to sleep
819     - on_master: if true, sleep on the master
820     - on_nodes: list of nodes in which to sleep
821
822   If the on_master parameter is true, it will execute a sleep on the
823   master (before any node sleep).
824
825   If the on_nodes list is not empty, it will sleep on those nodes
826   (after the sleep on the master, if that is enabled).
827
828   As an additional feature, the case of duration < 0 will be reported
829   as an execution error, so this opcode can be used as a failure
830   generator. The case of duration == 0 will not be treated specially.
831
832   """
833   OP_ID = "OP_TEST_DELAY"
834   OP_DSC_FIELD = "duration"
835   __slots__ = ["duration", "on_master", "on_nodes", "repeat"]
836
837
838 class OpTestAllocator(OpCode):
839   """Allocator framework testing.
840
841   This opcode has two modes:
842     - gather and return allocator input for a given mode (allocate new
843       or replace secondary) and a given instance definition (direction
844       'in')
845     - run a selected allocator for a given operation (as above) and
846       return the allocator output (direction 'out')
847
848   """
849   OP_ID = "OP_TEST_ALLOCATOR"
850   OP_DSC_FIELD = "allocator"
851   __slots__ = [
852     "direction", "mode", "allocator", "name",
853     "mem_size", "disks", "disk_template",
854     "os", "tags", "nics", "vcpus", "hypervisor",
855     "evac_nodes",
856     ]
857
858
859 class OpTestJobqueue(OpCode):
860   """Utility opcode to test some aspects of the job queue.
861
862   """
863   OP_ID = "OP_TEST_JQUEUE"
864   __slots__ = [
865     "notify_waitlock",
866     "notify_exec",
867     "log_messages",
868     "fail",
869     ]
870
871
872 class OpTestDummy(OpCode):
873   """Utility opcode used by unittests.
874
875   """
876   OP_ID = "OP_TEST_DUMMY"
877   __slots__ = [
878     "result",
879     "messages",
880     "fail",
881     ]
882
883
884 OP_MAPPING = dict([(v.OP_ID, v) for v in globals().values()
885                    if (isinstance(v, type) and issubclass(v, OpCode) and
886                        hasattr(v, "OP_ID"))])