4 # Copyright (C) 2006, 2007 Google Inc.
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.
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.
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
24 This module implements the data structures which define the cluster
25 operations - the so-called opcodes.
27 Every operation which modifies the cluster state is expressed via
32 # this are practically structures, so disable the message about too
34 # pylint: disable-msg=R0903
37 class BaseOpCode(object):
38 """A simple serializable object.
40 This object serves as a parent class for OpCode without any custom
46 def __init__(self, **kwargs):
47 """Constructor for BaseOpCode.
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.
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])
61 def __getstate__(self):
62 """Generic serializer.
64 This method just returns the contents of the instance as a
68 @return: the instance attributes and their values
72 for name in self.__slots__:
73 if hasattr(self, name):
74 state[name] = getattr(self, name)
77 def __setstate__(self, state):
78 """Generic unserializer.
80 This method just restores from the serialized state the attributes
81 of the current instance.
83 @param state: the serialized opcode data
87 if not isinstance(state, dict):
88 raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
91 for name in self.__slots__:
96 setattr(self, name, state[name])
99 class OpCode(BaseOpCode):
102 This is the root of the actual OpCode hierarchy. All clases derived
103 from this class should override OP_ID.
105 @cvar OP_ID: The ID of this opcode. This should be unique amongst all
106 childre of this class.
109 OP_ID = "OP_ABSTRACT"
112 def __getstate__(self):
113 """Specialized getstate for opcodes.
115 This method adds to the state dictionary the OP_ID of the class,
116 so that on unload we can identify the correct class for
117 instantiating the opcode.
120 @return: the state as a dictionary
123 data = BaseOpCode.__getstate__(self)
124 data["OP_ID"] = self.OP_ID
128 def LoadOpCode(cls, data):
129 """Generic load opcode method.
131 The method identifies the correct opcode class from the dict-form
132 by looking for a OP_ID key, if this is not found, or its value is
133 not available in this module as a child of this class, we fail.
136 @param data: the serialized opcode
139 if not isinstance(data, dict):
140 raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
141 if "OP_ID" not in data:
142 raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
143 op_id = data["OP_ID"]
145 if op_id in OP_MAPPING:
146 op_class = OP_MAPPING[op_id]
148 raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
151 new_data = data.copy()
152 del new_data["OP_ID"]
153 op.__setstate__(new_data)
157 """Generates a summary description of this opcode.
160 # all OP_ID start with OP_, we remove that
162 field_name = getattr(self, "OP_DSC_FIELD", None)
164 field_value = getattr(self, field_name, None)
165 txt = "%s(%s)" % (txt, field_value)
171 class OpDestroyCluster(OpCode):
172 """Destroy the cluster.
174 This opcode has no other parameters. All the state is irreversibly
175 lost after the execution of this opcode.
178 OP_ID = "OP_CLUSTER_DESTROY"
182 class OpQueryClusterInfo(OpCode):
183 """Query cluster information."""
184 OP_ID = "OP_CLUSTER_QUERY"
188 class OpVerifyCluster(OpCode):
189 """Verify the cluster state.
191 @type skip_checks: C{list}
192 @ivar skip_checks: steps to be skipped from the verify process; this
193 needs to be a subset of
194 L{constants.VERIFY_OPTIONAL_CHECKS}; currently
195 only L{constants.VERIFY_NPLUSONE_MEM} can be passed
198 OP_ID = "OP_CLUSTER_VERIFY"
199 __slots__ = ["skip_checks"]
202 class OpVerifyDisks(OpCode):
203 """Verify the cluster disks.
207 Result: a tuple of four elements:
208 - list of node names with bad data returned (unreachable, etc.)
209 - dict of node names with broken volume groups (values: error msg)
210 - list of instances with degraded disks (that should be activated)
211 - dict of instances with missing logical volumes (values: (node, vol)
212 pairs with details about the missing volumes)
214 In normal operation, all lists should be empty. A non-empty instance
215 list (3rd element of the result) is still ok (errors were fixed) but
216 non-empty node list means some node is down, and probably there are
217 unfixable drbd errors.
219 Note that only instances that are drbd-based are taken into
220 consideration. This might need to be revisited in the future.
223 OP_ID = "OP_CLUSTER_VERIFY_DISKS"
227 class OpQueryConfigValues(OpCode):
228 """Query cluster configuration values."""
229 OP_ID = "OP_CLUSTER_CONFIG_QUERY"
230 __slots__ = ["output_fields"]
233 class OpRenameCluster(OpCode):
234 """Rename the cluster.
237 @ivar name: The new name of the cluster. The name and/or the master IP
238 address will be changed to match the new name and its IP
242 OP_ID = "OP_CLUSTER_RENAME"
243 OP_DSC_FIELD = "name"
247 class OpSetClusterParams(OpCode):
248 """Change the parameters of the cluster.
250 @type vg_name: C{str} or C{None}
251 @ivar vg_name: The new volume group name or None to disable LVM usage.
254 OP_ID = "OP_CLUSTER_SET_PARAMS"
257 "enabled_hypervisors",
260 "candidate_pool_size",
264 class OpRedistributeConfig(OpCode):
265 """Force a full push of the cluster configuration.
268 OP_ID = "OP_CLUSTER_REDIST_CONF"
274 class OpRemoveNode(OpCode):
277 @type node_name: C{str}
278 @ivar node_name: The name of the node to remove. If the node still has
279 instances on it, the operation will fail.
282 OP_ID = "OP_NODE_REMOVE"
283 OP_DSC_FIELD = "node_name"
284 __slots__ = ["node_name"]
287 class OpAddNode(OpCode):
288 """Add a node to the cluster.
290 @type node_name: C{str}
291 @ivar node_name: The name of the node to add. This can be a short name,
292 but it will be expanded to the FQDN.
293 @type primary_ip: IP address
294 @ivar primary_ip: The primary IP of the node. This will be ignored when the
295 opcode is submitted, but will be filled during the node
296 add (so it will be visible in the job query).
297 @type secondary_ip: IP address
298 @ivar secondary_ip: The secondary IP of the node. This needs to be passed
299 if the cluster has been initialized in 'dual-network'
300 mode, otherwise it must not be given.
302 @ivar readd: Whether to re-add an existing node to the cluster. If
303 this is not passed, then the operation will abort if the node
304 name is already in the cluster; use this parameter to 'repair'
305 a node that had its configuration broken, or was reinstalled
306 without removal from the cluster.
309 OP_ID = "OP_NODE_ADD"
310 OP_DSC_FIELD = "node_name"
311 __slots__ = ["node_name", "primary_ip", "secondary_ip", "readd"]
314 class OpQueryNodes(OpCode):
315 """Compute the list of nodes."""
316 OP_ID = "OP_NODE_QUERY"
317 __slots__ = ["output_fields", "names", "use_locking"]
320 class OpQueryNodeVolumes(OpCode):
321 """Get list of volumes on node."""
322 OP_ID = "OP_NODE_QUERYVOLS"
323 __slots__ = ["nodes", "output_fields"]
326 class OpSetNodeParams(OpCode):
327 """Change the parameters of a node."""
328 OP_ID = "OP_NODE_SET_PARAMS"
329 OP_DSC_FIELD = "node_name"
340 class OpCreateInstance(OpCode):
341 """Create an instance."""
342 OP_ID = "OP_INSTANCE_CREATE"
343 OP_DSC_FIELD = "instance_name"
345 "instance_name", "os_type", "pnode",
346 "disk_template", "snode", "mode",
348 "src_node", "src_path", "start",
349 "wait_for_sync", "ip_check",
350 "file_storage_dir", "file_driver",
352 "hypervisor", "hvparams", "beparams",
356 class OpReinstallInstance(OpCode):
357 """Reinstall an instance's OS."""
358 OP_ID = "OP_INSTANCE_REINSTALL"
359 OP_DSC_FIELD = "instance_name"
360 __slots__ = ["instance_name", "os_type"]
363 class OpRemoveInstance(OpCode):
364 """Remove an instance."""
365 OP_ID = "OP_INSTANCE_REMOVE"
366 OP_DSC_FIELD = "instance_name"
367 __slots__ = ["instance_name", "ignore_failures"]
370 class OpRenameInstance(OpCode):
371 """Rename an instance."""
372 OP_ID = "OP_INSTANCE_RENAME"
373 __slots__ = ["instance_name", "ignore_ip", "new_name"]
376 class OpStartupInstance(OpCode):
377 """Startup an instance."""
378 OP_ID = "OP_INSTANCE_STARTUP"
379 OP_DSC_FIELD = "instance_name"
380 __slots__ = ["instance_name", "force", "hvparams", "beparams"]
383 class OpShutdownInstance(OpCode):
384 """Shutdown an instance."""
385 OP_ID = "OP_INSTANCE_SHUTDOWN"
386 OP_DSC_FIELD = "instance_name"
387 __slots__ = ["instance_name"]
390 class OpRebootInstance(OpCode):
391 """Reboot an instance."""
392 OP_ID = "OP_INSTANCE_REBOOT"
393 OP_DSC_FIELD = "instance_name"
394 __slots__ = ["instance_name", "reboot_type", "ignore_secondaries" ]
397 class OpReplaceDisks(OpCode):
398 """Replace the disks of an instance."""
399 OP_ID = "OP_INSTANCE_REPLACE_DISKS"
400 OP_DSC_FIELD = "instance_name"
401 __slots__ = ["instance_name", "remote_node", "mode", "disks", "iallocator"]
404 class OpFailoverInstance(OpCode):
405 """Failover an instance."""
406 OP_ID = "OP_INSTANCE_FAILOVER"
407 OP_DSC_FIELD = "instance_name"
408 __slots__ = ["instance_name", "ignore_consistency"]
411 class OpMigrateInstance(OpCode):
412 """Migrate an instance.
414 This migrates (without shutting down an instance) to its secondary
417 @ivar instance_name: the name of the instance
420 OP_ID = "OP_INSTANCE_MIGRATE"
421 OP_DSC_FIELD = "instance_name"
422 __slots__ = ["instance_name", "live", "cleanup"]
425 class OpConnectConsole(OpCode):
426 """Connect to an instance's console."""
427 OP_ID = "OP_INSTANCE_CONSOLE"
428 OP_DSC_FIELD = "instance_name"
429 __slots__ = ["instance_name"]
432 class OpActivateInstanceDisks(OpCode):
433 """Activate an instance's disks."""
434 OP_ID = "OP_INSTANCE_ACTIVATE_DISKS"
435 OP_DSC_FIELD = "instance_name"
436 __slots__ = ["instance_name"]
439 class OpDeactivateInstanceDisks(OpCode):
440 """Deactivate an instance's disks."""
441 OP_ID = "OP_INSTANCE_DEACTIVATE_DISKS"
442 OP_DSC_FIELD = "instance_name"
443 __slots__ = ["instance_name"]
446 class OpQueryInstances(OpCode):
447 """Compute the list of instances."""
448 OP_ID = "OP_INSTANCE_QUERY"
449 __slots__ = ["output_fields", "names", "use_locking"]
452 class OpQueryInstanceData(OpCode):
453 """Compute the run-time status of instances."""
454 OP_ID = "OP_INSTANCE_QUERY_DATA"
455 __slots__ = ["instances", "static"]
458 class OpSetInstanceParams(OpCode):
459 """Change the parameters of an instance."""
460 OP_ID = "OP_INSTANCE_SET_PARAMS"
461 OP_DSC_FIELD = "instance_name"
464 "hvparams", "beparams", "force",
469 class OpGrowDisk(OpCode):
470 """Grow a disk of an instance."""
471 OP_ID = "OP_INSTANCE_GROW_DISK"
472 OP_DSC_FIELD = "instance_name"
473 __slots__ = ["instance_name", "disk", "amount", "wait_for_sync"]
477 class OpDiagnoseOS(OpCode):
478 """Compute the list of guest operating systems."""
479 OP_ID = "OP_OS_DIAGNOSE"
480 __slots__ = ["output_fields", "names"]
484 class OpQueryExports(OpCode):
485 """Compute the list of exported images."""
486 OP_ID = "OP_BACKUP_QUERY"
487 __slots__ = ["nodes", "use_locking"]
490 class OpExportInstance(OpCode):
491 """Export an instance."""
492 OP_ID = "OP_BACKUP_EXPORT"
493 OP_DSC_FIELD = "instance_name"
494 __slots__ = ["instance_name", "target_node", "shutdown"]
497 class OpRemoveExport(OpCode):
498 """Remove an instance's export."""
499 OP_ID = "OP_BACKUP_REMOVE"
500 OP_DSC_FIELD = "instance_name"
501 __slots__ = ["instance_name"]
505 class OpGetTags(OpCode):
506 """Returns the tags of the given object."""
507 OP_ID = "OP_TAGS_GET"
508 OP_DSC_FIELD = "name"
509 __slots__ = ["kind", "name"]
512 class OpSearchTags(OpCode):
513 """Searches the tags in the cluster for a given pattern."""
514 OP_ID = "OP_TAGS_SEARCH"
515 OP_DSC_FIELD = "pattern"
516 __slots__ = ["pattern"]
519 class OpAddTags(OpCode):
520 """Add a list of tags on a given object."""
521 OP_ID = "OP_TAGS_SET"
522 __slots__ = ["kind", "name", "tags"]
525 class OpDelTags(OpCode):
526 """Remove a list of tags from a given object."""
527 OP_ID = "OP_TAGS_DEL"
528 __slots__ = ["kind", "name", "tags"]
532 class OpTestDelay(OpCode):
533 """Sleeps for a configured amount of time.
535 This is used just for debugging and testing.
538 - duration: the time to sleep
539 - on_master: if true, sleep on the master
540 - on_nodes: list of nodes in which to sleep
542 If the on_master parameter is true, it will execute a sleep on the
543 master (before any node sleep).
545 If the on_nodes list is not empty, it will sleep on those nodes
546 (after the sleep on the master, if that is enabled).
548 As an additional feature, the case of duration < 0 will be reported
549 as an execution error, so this opcode can be used as a failure
550 generator. The case of duration == 0 will not be treated specially.
553 OP_ID = "OP_TEST_DELAY"
554 OP_DSC_FIELD = "duration"
555 __slots__ = ["duration", "on_master", "on_nodes"]
558 class OpTestAllocator(OpCode):
559 """Allocator framework testing.
561 This opcode has two modes:
562 - gather and return allocator input for a given mode (allocate new
563 or replace secondary) and a given instance definition (direction
565 - run a selected allocator for a given operation (as above) and
566 return the allocator output (direction 'out')
569 OP_ID = "OP_TEST_ALLOCATOR"
570 OP_DSC_FIELD = "allocator"
572 "direction", "mode", "allocator", "name",
573 "mem_size", "disks", "disk_template",
574 "os", "tags", "nics", "vcpus", "hypervisor",
577 OP_MAPPING = dict([(v.OP_ID, v) for v in globals().values()
578 if (isinstance(v, type) and issubclass(v, OpCode) and
579 hasattr(v, "OP_ID"))])