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 for item in globals().values():
146 if (isinstance(item, type) and
147 issubclass(item, cls) and
148 hasattr(item, "OP_ID") and
149 getattr(item, "OP_ID") == op_id):
153 raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
156 new_data = data.copy()
157 del new_data["OP_ID"]
158 op.__setstate__(new_data)
162 """Generates a summary description of this opcode.
165 # all OP_ID start with OP_, we remove that
167 field_name = getattr(self, "OP_DSC_FIELD", None)
169 field_value = getattr(self, field_name, None)
170 txt = "%s(%s)" % (txt, field_value)
176 class OpDestroyCluster(OpCode):
177 """Destroy the cluster.
179 This opcode has no other parameters. All the state is irreversibly
180 lost after the execution of this opcode.
183 OP_ID = "OP_CLUSTER_DESTROY"
187 class OpQueryClusterInfo(OpCode):
188 """Query cluster information."""
189 OP_ID = "OP_CLUSTER_QUERY"
193 class OpVerifyCluster(OpCode):
194 """Verify the cluster state.
196 @type skip_checks: C{list}
197 @ivar skip_checks: steps to be skipped from the verify process; this
198 needs to be a subset of
199 L{constants.VERIFY_OPTIONAL_CHECKS}; currently
200 only L{constants.VERIFY_NPLUSONE_MEM} can be passed
203 OP_ID = "OP_CLUSTER_VERIFY"
204 __slots__ = ["skip_checks"]
207 class OpVerifyDisks(OpCode):
208 """Verify the cluster disks.
212 Result: a tuple of four elements:
213 - list of node names with bad data returned (unreachable, etc.)
214 - dict of node names with broken volume groups (values: error msg)
215 - list of instances with degraded disks (that should be activated)
216 - dict of instances with missing logical volumes (values: (node, vol)
217 pairs with details about the missing volumes)
219 In normal operation, all lists should be empty. A non-empty instance
220 list (3rd element of the result) is still ok (errors were fixed) but
221 non-empty node list means some node is down, and probably there are
222 unfixable drbd errors.
224 Note that only instances that are drbd-based are taken into
225 consideration. This might need to be revisited in the future.
228 OP_ID = "OP_CLUSTER_VERIFY_DISKS"
232 class OpQueryConfigValues(OpCode):
233 """Query cluster configuration values."""
234 OP_ID = "OP_CLUSTER_CONFIG_QUERY"
235 __slots__ = ["output_fields"]
238 class OpRenameCluster(OpCode):
239 """Rename the cluster.
242 @ivar name: The new name of the cluster. The name and/or the master IP
243 address will be changed to match the new name and its IP
247 OP_ID = "OP_CLUSTER_RENAME"
248 OP_DSC_FIELD = "name"
252 class OpSetClusterParams(OpCode):
253 """Change the parameters of the cluster.
255 @type vg_name: C{str} or C{None}
256 @ivar vg_name: The new volume group name or None to disable LVM usage.
259 OP_ID = "OP_CLUSTER_SET_PARAMS"
262 "enabled_hypervisors",
265 "candidate_pool_size",
269 class OpRedistributeConfig(OpCode):
270 """Force a full push of the cluster configuration.
273 OP_ID = "OP_CLUSTER_REDIST_CONF"
279 class OpRemoveNode(OpCode):
282 @type node_name: C{str}
283 @ivar node_name: The name of the node to remove. If the node still has
284 instances on it, the operation will fail.
287 OP_ID = "OP_NODE_REMOVE"
288 OP_DSC_FIELD = "node_name"
289 __slots__ = ["node_name"]
292 class OpAddNode(OpCode):
293 """Add a node to the cluster.
295 @type node_name: C{str}
296 @ivar node_name: The name of the node to add. This can be a short name,
297 but it will be expanded to the FQDN.
298 @type primary_ip: IP address
299 @ivar primary_ip: The primary IP of the node. This will be ignored when the
300 opcode is submitted, but will be filled during the node
301 add (so it will be visible in the job query).
302 @type secondary_ip: IP address
303 @ivar secondary_ip: The secondary IP of the node. This needs to be passed
304 if the cluster has been initialized in 'dual-network'
305 mode, otherwise it must not be given.
307 @ivar readd: Whether to re-add an existing node to the cluster. If
308 this is not passed, then the operation will abort if the node
309 name is already in the cluster; use this parameter to 'repair'
310 a node that had its configuration broken, or was reinstalled
311 without removal from the cluster.
314 OP_ID = "OP_NODE_ADD"
315 OP_DSC_FIELD = "node_name"
316 __slots__ = ["node_name", "primary_ip", "secondary_ip", "readd"]
319 class OpQueryNodes(OpCode):
320 """Compute the list of nodes."""
321 OP_ID = "OP_NODE_QUERY"
322 __slots__ = ["output_fields", "names", "use_locking"]
325 class OpQueryNodeVolumes(OpCode):
326 """Get list of volumes on node."""
327 OP_ID = "OP_NODE_QUERYVOLS"
328 __slots__ = ["nodes", "output_fields"]
331 class OpSetNodeParams(OpCode):
332 """Change the parameters of a node."""
333 OP_ID = "OP_NODE_SET_PARAMS"
334 OP_DSC_FIELD = "node_name"
345 class OpCreateInstance(OpCode):
346 """Create an instance."""
347 OP_ID = "OP_INSTANCE_CREATE"
348 OP_DSC_FIELD = "instance_name"
350 "instance_name", "os_type", "pnode",
351 "disk_template", "snode", "mode",
353 "src_node", "src_path", "start",
354 "wait_for_sync", "ip_check",
355 "file_storage_dir", "file_driver",
357 "hypervisor", "hvparams", "beparams",
361 class OpReinstallInstance(OpCode):
362 """Reinstall an instance's OS."""
363 OP_ID = "OP_INSTANCE_REINSTALL"
364 OP_DSC_FIELD = "instance_name"
365 __slots__ = ["instance_name", "os_type"]
368 class OpRemoveInstance(OpCode):
369 """Remove an instance."""
370 OP_ID = "OP_INSTANCE_REMOVE"
371 OP_DSC_FIELD = "instance_name"
372 __slots__ = ["instance_name", "ignore_failures"]
375 class OpRenameInstance(OpCode):
376 """Rename an instance."""
377 OP_ID = "OP_INSTANCE_RENAME"
378 __slots__ = ["instance_name", "ignore_ip", "new_name"]
381 class OpStartupInstance(OpCode):
382 """Startup an instance."""
383 OP_ID = "OP_INSTANCE_STARTUP"
384 OP_DSC_FIELD = "instance_name"
385 __slots__ = ["instance_name", "force", "extra_args"]
388 class OpShutdownInstance(OpCode):
389 """Shutdown an instance."""
390 OP_ID = "OP_INSTANCE_SHUTDOWN"
391 OP_DSC_FIELD = "instance_name"
392 __slots__ = ["instance_name"]
395 class OpRebootInstance(OpCode):
396 """Reboot an instance."""
397 OP_ID = "OP_INSTANCE_REBOOT"
398 OP_DSC_FIELD = "instance_name"
399 __slots__ = ["instance_name", "reboot_type", "extra_args",
400 "ignore_secondaries" ]
403 class OpReplaceDisks(OpCode):
404 """Replace the disks of an instance."""
405 OP_ID = "OP_INSTANCE_REPLACE_DISKS"
406 OP_DSC_FIELD = "instance_name"
407 __slots__ = ["instance_name", "remote_node", "mode", "disks", "iallocator"]
410 class OpFailoverInstance(OpCode):
411 """Failover an instance."""
412 OP_ID = "OP_INSTANCE_FAILOVER"
413 OP_DSC_FIELD = "instance_name"
414 __slots__ = ["instance_name", "ignore_consistency"]
417 class OpMigrateInstance(OpCode):
418 """Migrate an instance.
420 This migrates (without shutting down an instance) to its secondary
423 @ivar instance_name: the name of the instance
426 OP_ID = "OP_INSTANCE_MIGRATE"
427 OP_DSC_FIELD = "instance_name"
428 __slots__ = ["instance_name", "live", "cleanup"]
431 class OpConnectConsole(OpCode):
432 """Connect to an instance's console."""
433 OP_ID = "OP_INSTANCE_CONSOLE"
434 OP_DSC_FIELD = "instance_name"
435 __slots__ = ["instance_name"]
438 class OpActivateInstanceDisks(OpCode):
439 """Activate an instance's disks."""
440 OP_ID = "OP_INSTANCE_ACTIVATE_DISKS"
441 OP_DSC_FIELD = "instance_name"
442 __slots__ = ["instance_name"]
445 class OpDeactivateInstanceDisks(OpCode):
446 """Deactivate an instance's disks."""
447 OP_ID = "OP_INSTANCE_DEACTIVATE_DISKS"
448 OP_DSC_FIELD = "instance_name"
449 __slots__ = ["instance_name"]
452 class OpQueryInstances(OpCode):
453 """Compute the list of instances."""
454 OP_ID = "OP_INSTANCE_QUERY"
455 __slots__ = ["output_fields", "names", "use_locking"]
458 class OpQueryInstanceData(OpCode):
459 """Compute the run-time status of instances."""
460 OP_ID = "OP_INSTANCE_QUERY_DATA"
461 __slots__ = ["instances", "static"]
464 class OpSetInstanceParams(OpCode):
465 """Change the parameters of an instance."""
466 OP_ID = "OP_INSTANCE_SET_PARAMS"
467 OP_DSC_FIELD = "instance_name"
470 "hvparams", "beparams", "force",
475 class OpGrowDisk(OpCode):
476 """Grow a disk of an instance."""
477 OP_ID = "OP_INSTANCE_GROW_DISK"
478 OP_DSC_FIELD = "instance_name"
479 __slots__ = ["instance_name", "disk", "amount", "wait_for_sync"]
483 class OpDiagnoseOS(OpCode):
484 """Compute the list of guest operating systems."""
485 OP_ID = "OP_OS_DIAGNOSE"
486 __slots__ = ["output_fields", "names"]
490 class OpQueryExports(OpCode):
491 """Compute the list of exported images."""
492 OP_ID = "OP_BACKUP_QUERY"
493 __slots__ = ["nodes", "use_locking"]
496 class OpExportInstance(OpCode):
497 """Export an instance."""
498 OP_ID = "OP_BACKUP_EXPORT"
499 OP_DSC_FIELD = "instance_name"
500 __slots__ = ["instance_name", "target_node", "shutdown"]
503 class OpRemoveExport(OpCode):
504 """Remove an instance's export."""
505 OP_ID = "OP_BACKUP_REMOVE"
506 OP_DSC_FIELD = "instance_name"
507 __slots__ = ["instance_name"]
511 class OpGetTags(OpCode):
512 """Returns the tags of the given object."""
513 OP_ID = "OP_TAGS_GET"
514 OP_DSC_FIELD = "name"
515 __slots__ = ["kind", "name"]
518 class OpSearchTags(OpCode):
519 """Searches the tags in the cluster for a given pattern."""
520 OP_ID = "OP_TAGS_SEARCH"
521 OP_DSC_FIELD = "pattern"
522 __slots__ = ["pattern"]
525 class OpAddTags(OpCode):
526 """Add a list of tags on a given object."""
527 OP_ID = "OP_TAGS_SET"
528 __slots__ = ["kind", "name", "tags"]
531 class OpDelTags(OpCode):
532 """Remove a list of tags from a given object."""
533 OP_ID = "OP_TAGS_DEL"
534 __slots__ = ["kind", "name", "tags"]
538 class OpTestDelay(OpCode):
539 """Sleeps for a configured amount of time.
541 This is used just for debugging and testing.
544 - duration: the time to sleep
545 - on_master: if true, sleep on the master
546 - on_nodes: list of nodes in which to sleep
548 If the on_master parameter is true, it will execute a sleep on the
549 master (before any node sleep).
551 If the on_nodes list is not empty, it will sleep on those nodes
552 (after the sleep on the master, if that is enabled).
554 As an additional feature, the case of duration < 0 will be reported
555 as an execution error, so this opcode can be used as a failure
556 generator. The case of duration == 0 will not be treated specially.
559 OP_ID = "OP_TEST_DELAY"
560 OP_DSC_FIELD = "duration"
561 __slots__ = ["duration", "on_master", "on_nodes"]
564 class OpTestAllocator(OpCode):
565 """Allocator framework testing.
567 This opcode has two modes:
568 - gather and return allocator input for a given mode (allocate new
569 or replace secondary) and a given instance definition (direction
571 - run a selected allocator for a given operation (as above) and
572 return the allocator output (direction 'out')
575 OP_ID = "OP_TEST_ALLOCATOR"
576 OP_DSC_FIELD = "allocator"
578 "direction", "mode", "allocator", "name",
579 "mem_size", "disks", "disk_template",
580 "os", "tags", "nics", "vcpus", "hypervisor",