QA: remove the --default-hypervisor option
[ganeti-local] / lib / opcodes.py
1 #
2 #
3
4 # Copyright (C) 2006, 2007 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     for key in kwargs:
56       if key not in self.__slots__:
57         raise TypeError("Object %s doesn't support the parameter '%s'" %
58                         (self.__class__.__name__, key))
59       setattr(self, key, kwargs[key])
60
61   def __getstate__(self):
62     """Generic serializer.
63
64     This method just returns the contents of the instance as a
65     dictionary.
66
67     @rtype:  C{dict}
68     @return: the instance attributes and their values
69
70     """
71     state = {}
72     for name in self.__slots__:
73       if hasattr(self, name):
74         state[name] = getattr(self, name)
75     return state
76
77   def __setstate__(self, state):
78     """Generic unserializer.
79
80     This method just restores from the serialized state the attributes
81     of the current instance.
82
83     @param state: the serialized opcode data
84     @type state:  C{dict}
85
86     """
87     if not isinstance(state, dict):
88       raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
89                        type(state))
90
91     for name in self.__slots__:
92       if name not in state:
93         delattr(self, name)
94
95     for name in state:
96       setattr(self, name, state[name])
97
98
99 class OpCode(BaseOpCode):
100   """Abstract OpCode.
101
102   This is the root of the actual OpCode hierarchy. All clases derived
103   from this class should override OP_ID.
104
105   @cvar OP_ID: The ID of this opcode. This should be unique amongst all
106                children of this class.
107   @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
108                  the check steps
109
110   """
111   OP_ID = "OP_ABSTRACT"
112   __slots__ = BaseOpCode.__slots__ + ["dry_run"]
113
114   def __getstate__(self):
115     """Specialized getstate for opcodes.
116
117     This method adds to the state dictionary the OP_ID of the class,
118     so that on unload we can identify the correct class for
119     instantiating the opcode.
120
121     @rtype:   C{dict}
122     @return:  the state as a dictionary
123
124     """
125     data = BaseOpCode.__getstate__(self)
126     data["OP_ID"] = self.OP_ID
127     return data
128
129   @classmethod
130   def LoadOpCode(cls, data):
131     """Generic load opcode method.
132
133     The method identifies the correct opcode class from the dict-form
134     by looking for a OP_ID key, if this is not found, or its value is
135     not available in this module as a child of this class, we fail.
136
137     @type data:  C{dict}
138     @param data: the serialized opcode
139
140     """
141     if not isinstance(data, dict):
142       raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
143     if "OP_ID" not in data:
144       raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
145     op_id = data["OP_ID"]
146     op_class = None
147     if op_id in OP_MAPPING:
148       op_class = OP_MAPPING[op_id]
149     else:
150       raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
151                        op_id)
152     op = op_class()
153     new_data = data.copy()
154     del new_data["OP_ID"]
155     op.__setstate__(new_data)
156     return op
157
158   def Summary(self):
159     """Generates a summary description of this opcode.
160
161     """
162     # all OP_ID start with OP_, we remove that
163     txt = self.OP_ID[3:]
164     field_name = getattr(self, "OP_DSC_FIELD", None)
165     if field_name:
166       field_value = getattr(self, field_name, None)
167       txt = "%s(%s)" % (txt, field_value)
168     return txt
169
170
171 # cluster opcodes
172
173 class OpPostInitCluster(OpCode):
174   """Post cluster initialization.
175
176   This opcode does not touch the cluster at all. Its purpose is to run hooks
177   after the cluster has been initialized.
178
179   """
180   OP_ID = "OP_CLUSTER_POST_INIT"
181   __slots__ = OpCode.__slots__ + []
182
183
184 class OpDestroyCluster(OpCode):
185   """Destroy the cluster.
186
187   This opcode has no other parameters. All the state is irreversibly
188   lost after the execution of this opcode.
189
190   """
191   OP_ID = "OP_CLUSTER_DESTROY"
192   __slots__ = OpCode.__slots__ + []
193
194
195 class OpQueryClusterInfo(OpCode):
196   """Query cluster information."""
197   OP_ID = "OP_CLUSTER_QUERY"
198   __slots__ = OpCode.__slots__ + []
199
200
201 class OpVerifyCluster(OpCode):
202   """Verify the cluster state.
203
204   @type skip_checks: C{list}
205   @ivar skip_checks: steps to be skipped from the verify process; this
206                      needs to be a subset of
207                      L{constants.VERIFY_OPTIONAL_CHECKS}; currently
208                      only L{constants.VERIFY_NPLUSONE_MEM} can be passed
209
210   """
211   OP_ID = "OP_CLUSTER_VERIFY"
212   __slots__ = OpCode.__slots__ + ["skip_checks"]
213
214
215 class OpVerifyDisks(OpCode):
216   """Verify the cluster disks.
217
218   Parameters: none
219
220   Result: a tuple of four elements:
221     - list of node names with bad data returned (unreachable, etc.)
222     - dict of node names with broken volume groups (values: error msg)
223     - list of instances with degraded disks (that should be activated)
224     - dict of instances with missing logical volumes (values: (node, vol)
225       pairs with details about the missing volumes)
226
227   In normal operation, all lists should be empty. A non-empty instance
228   list (3rd element of the result) is still ok (errors were fixed) but
229   non-empty node list means some node is down, and probably there are
230   unfixable drbd errors.
231
232   Note that only instances that are drbd-based are taken into
233   consideration. This might need to be revisited in the future.
234
235   """
236   OP_ID = "OP_CLUSTER_VERIFY_DISKS"
237   __slots__ = OpCode.__slots__ + []
238
239
240 class OpRepairDiskSizes(OpCode):
241   """Verify the disk sizes of the instances and fixes configuration
242   mimatches.
243
244   Parameters: optional instances list, in case we want to restrict the
245   checks to only a subset of the instances.
246
247   Result: a list of tuples, (instance, disk, new-size) for changed
248   configurations.
249
250   In normal operation, the list should be empty.
251
252   @type instances: list
253   @ivar instances: the list of instances to check, or empty for all instances
254
255   """
256   OP_ID = "OP_CLUSTER_REPAIR_DISK_SIZES"
257   __slots__ = ["instances"]
258
259
260 class OpQueryConfigValues(OpCode):
261   """Query cluster configuration values."""
262   OP_ID = "OP_CLUSTER_CONFIG_QUERY"
263   __slots__ = OpCode.__slots__ + ["output_fields"]
264
265
266 class OpRenameCluster(OpCode):
267   """Rename the cluster.
268
269   @type name: C{str}
270   @ivar name: The new name of the cluster. The name and/or the master IP
271               address will be changed to match the new name and its IP
272               address.
273
274   """
275   OP_ID = "OP_CLUSTER_RENAME"
276   OP_DSC_FIELD = "name"
277   __slots__ = OpCode.__slots__ + ["name"]
278
279
280 class OpSetClusterParams(OpCode):
281   """Change the parameters of the cluster.
282
283   @type vg_name: C{str} or C{None}
284   @ivar vg_name: The new volume group name or None to disable LVM usage.
285
286   """
287   OP_ID = "OP_CLUSTER_SET_PARAMS"
288   __slots__ = OpCode.__slots__ + [
289     "vg_name",
290     "enabled_hypervisors",
291     "hvparams",
292     "beparams",
293     "nicparams",
294     "candidate_pool_size",
295     ]
296
297
298 class OpRedistributeConfig(OpCode):
299   """Force a full push of the cluster configuration.
300
301   """
302   OP_ID = "OP_CLUSTER_REDIST_CONF"
303   __slots__ = OpCode.__slots__ + [
304     ]
305
306 # node opcodes
307
308 class OpRemoveNode(OpCode):
309   """Remove a node.
310
311   @type node_name: C{str}
312   @ivar node_name: The name of the node to remove. If the node still has
313                    instances on it, the operation will fail.
314
315   """
316   OP_ID = "OP_NODE_REMOVE"
317   OP_DSC_FIELD = "node_name"
318   __slots__ = OpCode.__slots__ + ["node_name"]
319
320
321 class OpAddNode(OpCode):
322   """Add a node to the cluster.
323
324   @type node_name: C{str}
325   @ivar node_name: The name of the node to add. This can be a short name,
326                    but it will be expanded to the FQDN.
327   @type primary_ip: IP address
328   @ivar primary_ip: The primary IP of the node. This will be ignored when the
329                     opcode is submitted, but will be filled during the node
330                     add (so it will be visible in the job query).
331   @type secondary_ip: IP address
332   @ivar secondary_ip: The secondary IP of the node. This needs to be passed
333                       if the cluster has been initialized in 'dual-network'
334                       mode, otherwise it must not be given.
335   @type readd: C{bool}
336   @ivar readd: Whether to re-add an existing node to the cluster. If
337                this is not passed, then the operation will abort if the node
338                name is already in the cluster; use this parameter to 'repair'
339                a node that had its configuration broken, or was reinstalled
340                without removal from the cluster.
341
342   """
343   OP_ID = "OP_NODE_ADD"
344   OP_DSC_FIELD = "node_name"
345   __slots__ = OpCode.__slots__ + [
346     "node_name", "primary_ip", "secondary_ip", "readd",
347     ]
348
349
350 class OpQueryNodes(OpCode):
351   """Compute the list of nodes."""
352   OP_ID = "OP_NODE_QUERY"
353   __slots__ = OpCode.__slots__ + ["output_fields", "names", "use_locking"]
354
355
356 class OpQueryNodeVolumes(OpCode):
357   """Get list of volumes on node."""
358   OP_ID = "OP_NODE_QUERYVOLS"
359   __slots__ = OpCode.__slots__ + ["nodes", "output_fields"]
360
361
362 class OpQueryNodeStorage(OpCode):
363   """Get information on storage for node(s)."""
364   OP_ID = "OP_NODE_QUERY_STORAGE"
365   __slots__ = OpCode.__slots__ + [
366     "nodes",
367     "storage_type",
368     "name",
369     "output_fields",
370     ]
371
372
373 class OpModifyNodeStorage(OpCode):
374   """"""
375   OP_ID = "OP_NODE_MODIFY_STORAGE"
376   __slots__ = OpCode.__slots__ + [
377     "node_name",
378     "storage_type",
379     "name",
380     "changes",
381     ]
382
383
384 class OpRepairNodeStorage(OpCode):
385   """Repairs the volume group on a node."""
386   OP_ID = "OP_REPAIR_NODE_STORAGE"
387   OP_DSC_FIELD = "node_name"
388   __slots__ = OpCode.__slots__ + [
389     "node_name",
390     "storage_type",
391     "name",
392     ]
393
394
395 class OpSetNodeParams(OpCode):
396   """Change the parameters of a node."""
397   OP_ID = "OP_NODE_SET_PARAMS"
398   OP_DSC_FIELD = "node_name"
399   __slots__ = OpCode.__slots__ + [
400     "node_name",
401     "force",
402     "master_candidate",
403     "offline",
404     "drained",
405     ]
406
407
408 class OpPowercycleNode(OpCode):
409   """Tries to powercycle a node."""
410   OP_ID = "OP_NODE_POWERCYCLE"
411   OP_DSC_FIELD = "node_name"
412   __slots__ = OpCode.__slots__ + [
413     "node_name",
414     "force",
415     ]
416
417
418 class OpEvacuateNode(OpCode):
419   """Relocate secondary instances from a node."""
420   OP_ID = "OP_NODE_EVACUATE"
421   OP_DSC_FIELD = "node_name"
422   __slots__ = OpCode.__slots__ + [
423     "node_name", "remote_node", "iallocator",
424     ]
425
426
427 class OpMigrateNode(OpCode):
428   """Migrate all instances from a node."""
429   OP_ID = "OP_NODE_MIGRATE"
430   OP_DSC_FIELD = "node_name"
431   __slots__ = OpCode.__slots__ + [
432     "node_name",
433     "live",
434     ]
435
436
437 # instance opcodes
438
439 class OpCreateInstance(OpCode):
440   """Create an instance."""
441   OP_ID = "OP_INSTANCE_CREATE"
442   OP_DSC_FIELD = "instance_name"
443   __slots__ = OpCode.__slots__ + [
444     "instance_name", "os_type", "pnode",
445     "disk_template", "snode", "mode",
446     "disks", "nics",
447     "src_node", "src_path", "start",
448     "wait_for_sync", "ip_check",
449     "file_storage_dir", "file_driver",
450     "iallocator",
451     "hypervisor", "hvparams", "beparams",
452     "dry_run",
453     ]
454
455
456 class OpReinstallInstance(OpCode):
457   """Reinstall an instance's OS."""
458   OP_ID = "OP_INSTANCE_REINSTALL"
459   OP_DSC_FIELD = "instance_name"
460   __slots__ = OpCode.__slots__ + ["instance_name", "os_type"]
461
462
463 class OpRemoveInstance(OpCode):
464   """Remove an instance."""
465   OP_ID = "OP_INSTANCE_REMOVE"
466   OP_DSC_FIELD = "instance_name"
467   __slots__ = OpCode.__slots__ + ["instance_name", "ignore_failures"]
468
469
470 class OpRenameInstance(OpCode):
471   """Rename an instance."""
472   OP_ID = "OP_INSTANCE_RENAME"
473   __slots__ = OpCode.__slots__ + [
474     "instance_name", "ignore_ip", "new_name",
475     ]
476
477
478 class OpStartupInstance(OpCode):
479   """Startup an instance."""
480   OP_ID = "OP_INSTANCE_STARTUP"
481   OP_DSC_FIELD = "instance_name"
482   __slots__ = OpCode.__slots__ + [
483     "instance_name", "force", "hvparams", "beparams",
484     ]
485
486
487 class OpShutdownInstance(OpCode):
488   """Shutdown an instance."""
489   OP_ID = "OP_INSTANCE_SHUTDOWN"
490   OP_DSC_FIELD = "instance_name"
491   __slots__ = OpCode.__slots__ + ["instance_name"]
492
493
494 class OpRebootInstance(OpCode):
495   """Reboot an instance."""
496   OP_ID = "OP_INSTANCE_REBOOT"
497   OP_DSC_FIELD = "instance_name"
498   __slots__ = OpCode.__slots__ + [
499     "instance_name", "reboot_type", "ignore_secondaries",
500     ]
501
502
503 class OpReplaceDisks(OpCode):
504   """Replace the disks of an instance."""
505   OP_ID = "OP_INSTANCE_REPLACE_DISKS"
506   OP_DSC_FIELD = "instance_name"
507   __slots__ = OpCode.__slots__ + [
508     "instance_name", "remote_node", "mode", "disks", "iallocator",
509     ]
510
511
512 class OpFailoverInstance(OpCode):
513   """Failover an instance."""
514   OP_ID = "OP_INSTANCE_FAILOVER"
515   OP_DSC_FIELD = "instance_name"
516   __slots__ = OpCode.__slots__ + ["instance_name", "ignore_consistency"]
517
518
519 class OpMigrateInstance(OpCode):
520   """Migrate an instance.
521
522   This migrates (without shutting down an instance) to its secondary
523   node.
524
525   @ivar instance_name: the name of the instance
526
527   """
528   OP_ID = "OP_INSTANCE_MIGRATE"
529   OP_DSC_FIELD = "instance_name"
530   __slots__ = OpCode.__slots__ + ["instance_name", "live", "cleanup"]
531
532
533 class OpConnectConsole(OpCode):
534   """Connect to an instance's console."""
535   OP_ID = "OP_INSTANCE_CONSOLE"
536   OP_DSC_FIELD = "instance_name"
537   __slots__ = OpCode.__slots__ + ["instance_name"]
538
539
540 class OpActivateInstanceDisks(OpCode):
541   """Activate an instance's disks."""
542   OP_ID = "OP_INSTANCE_ACTIVATE_DISKS"
543   OP_DSC_FIELD = "instance_name"
544   __slots__ = OpCode.__slots__ + ["instance_name", "ignore_size"]
545
546
547 class OpDeactivateInstanceDisks(OpCode):
548   """Deactivate an instance's disks."""
549   OP_ID = "OP_INSTANCE_DEACTIVATE_DISKS"
550   OP_DSC_FIELD = "instance_name"
551   __slots__ = OpCode.__slots__ + ["instance_name"]
552
553
554 class OpRecreateInstanceDisks(OpCode):
555   """Deactivate an instance's disks."""
556   OP_ID = "OP_INSTANCE_RECREATE_DISKS"
557   OP_DSC_FIELD = "instance_name"
558   __slots__ = OpCode.__slots__ + ["instance_name", "disks"]
559
560
561 class OpQueryInstances(OpCode):
562   """Compute the list of instances."""
563   OP_ID = "OP_INSTANCE_QUERY"
564   __slots__ = OpCode.__slots__ + ["output_fields", "names", "use_locking"]
565
566
567 class OpQueryInstanceData(OpCode):
568   """Compute the run-time status of instances."""
569   OP_ID = "OP_INSTANCE_QUERY_DATA"
570   __slots__ = OpCode.__slots__ + ["instances", "static"]
571
572
573 class OpSetInstanceParams(OpCode):
574   """Change the parameters of an instance."""
575   OP_ID = "OP_INSTANCE_SET_PARAMS"
576   OP_DSC_FIELD = "instance_name"
577   __slots__ = OpCode.__slots__ + [
578     "instance_name",
579     "hvparams", "beparams", "force",
580     "nics", "disks",
581     ]
582
583
584 class OpGrowDisk(OpCode):
585   """Grow a disk of an instance."""
586   OP_ID = "OP_INSTANCE_GROW_DISK"
587   OP_DSC_FIELD = "instance_name"
588   __slots__ = OpCode.__slots__ + [
589     "instance_name", "disk", "amount", "wait_for_sync",
590     ]
591
592
593 # OS opcodes
594 class OpDiagnoseOS(OpCode):
595   """Compute the list of guest operating systems."""
596   OP_ID = "OP_OS_DIAGNOSE"
597   __slots__ = OpCode.__slots__ + ["output_fields", "names"]
598
599
600 # Exports opcodes
601 class OpQueryExports(OpCode):
602   """Compute the list of exported images."""
603   OP_ID = "OP_BACKUP_QUERY"
604   __slots__ = OpCode.__slots__ + ["nodes", "use_locking"]
605
606
607 class OpExportInstance(OpCode):
608   """Export an instance."""
609   OP_ID = "OP_BACKUP_EXPORT"
610   OP_DSC_FIELD = "instance_name"
611   __slots__ = OpCode.__slots__ + ["instance_name", "target_node", "shutdown"]
612
613
614 class OpRemoveExport(OpCode):
615   """Remove an instance's export."""
616   OP_ID = "OP_BACKUP_REMOVE"
617   OP_DSC_FIELD = "instance_name"
618   __slots__ = OpCode.__slots__ + ["instance_name"]
619
620
621 # Tags opcodes
622 class OpGetTags(OpCode):
623   """Returns the tags of the given object."""
624   OP_ID = "OP_TAGS_GET"
625   OP_DSC_FIELD = "name"
626   __slots__ = OpCode.__slots__ + ["kind", "name"]
627
628
629 class OpSearchTags(OpCode):
630   """Searches the tags in the cluster for a given pattern."""
631   OP_ID = "OP_TAGS_SEARCH"
632   OP_DSC_FIELD = "pattern"
633   __slots__ = OpCode.__slots__ + ["pattern"]
634
635
636 class OpAddTags(OpCode):
637   """Add a list of tags on a given object."""
638   OP_ID = "OP_TAGS_SET"
639   __slots__ = OpCode.__slots__ + ["kind", "name", "tags"]
640
641
642 class OpDelTags(OpCode):
643   """Remove a list of tags from a given object."""
644   OP_ID = "OP_TAGS_DEL"
645   __slots__ = OpCode.__slots__ + ["kind", "name", "tags"]
646
647
648 # Test opcodes
649 class OpTestDelay(OpCode):
650   """Sleeps for a configured amount of time.
651
652   This is used just for debugging and testing.
653
654   Parameters:
655     - duration: the time to sleep
656     - on_master: if true, sleep on the master
657     - on_nodes: list of nodes in which to sleep
658
659   If the on_master parameter is true, it will execute a sleep on the
660   master (before any node sleep).
661
662   If the on_nodes list is not empty, it will sleep on those nodes
663   (after the sleep on the master, if that is enabled).
664
665   As an additional feature, the case of duration < 0 will be reported
666   as an execution error, so this opcode can be used as a failure
667   generator. The case of duration == 0 will not be treated specially.
668
669   """
670   OP_ID = "OP_TEST_DELAY"
671   OP_DSC_FIELD = "duration"
672   __slots__ = OpCode.__slots__ + ["duration", "on_master", "on_nodes"]
673
674
675 class OpTestAllocator(OpCode):
676   """Allocator framework testing.
677
678   This opcode has two modes:
679     - gather and return allocator input for a given mode (allocate new
680       or replace secondary) and a given instance definition (direction
681       'in')
682     - run a selected allocator for a given operation (as above) and
683       return the allocator output (direction 'out')
684
685   """
686   OP_ID = "OP_TEST_ALLOCATOR"
687   OP_DSC_FIELD = "allocator"
688   __slots__ = OpCode.__slots__ + [
689     "direction", "mode", "allocator", "name",
690     "mem_size", "disks", "disk_template",
691     "os", "tags", "nics", "vcpus", "hypervisor",
692     ]
693
694
695 OP_MAPPING = dict([(v.OP_ID, v) for v in globals().values()
696                    if (isinstance(v, type) and issubclass(v, OpCode) and
697                        hasattr(v, "OP_ID"))])