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)
174 class OpDestroyCluster(OpCode):
175 """Destroy the cluster.
177 This opcode has no other parameters. All the state is irreversibly
178 lost after the execution of this opcode.
181 OP_ID = "OP_CLUSTER_DESTROY"
185 class OpQueryClusterInfo(OpCode):
186 """Query cluster information."""
187 OP_ID = "OP_CLUSTER_QUERY"
191 class OpVerifyCluster(OpCode):
192 """Verify the cluster state.
194 @type skip_checks: C{list}
195 @ivar skip_checks: steps to be skipped from the verify process; this
196 needs to be a subset of
197 L{constants.VERIFY_OPTIONAL_CHECKS}; currently
198 only L{constants.VERIFY_NPLUSONE_MEM} can be passed
201 OP_ID = "OP_CLUSTER_VERIFY"
202 __slots__ = ["skip_checks"]
205 class OpVerifyDisks(OpCode):
206 """Verify the cluster disks.
211 - list of node names with bad data returned (unreachable, etc.)
212 - dict of node names with broken volume groups (values: error msg)
213 - list of instances with degraded disks (that should be activated)
214 - dict of instances with missing logical volumes (values: (node, vol)
215 pairs with details about the missing volumes)
217 In normal operation, all lists should be empty. A non-empty instance
218 list (3rd element of the result) is still ok (errors were fixed) but
219 non-empty node list means some node is down, and probably there are
220 unfixable drbd errors.
222 Note that only instances that are drbd-based are taken into
223 consideration. This might need to be revisited in the future.
226 OP_ID = "OP_CLUSTER_VERIFY_DISKS"
230 class OpDumpClusterConfig(OpCode):
231 """Dump the cluster configuration."""
232 OP_ID = "OP_CLUSTER_DUMPCONFIG"
236 class OpRenameCluster(OpCode):
237 """Rename the cluster.
240 @ivar name: The new name of the cluster. The name and/or the master IP
241 address will be changed to match the new name and its IP
245 OP_ID = "OP_CLUSTER_RENAME"
246 OP_DSC_FIELD = "name"
250 class OpSetClusterParams(OpCode):
251 """Change the parameters of the cluster.
253 @type vg_name: C{str} or C{None}
254 @ivar vg_name: The new volume group name or None to disable LVM usage.
257 OP_ID = "OP_CLUSTER_SET_PARAMS"
258 __slots__ = ["vg_name"]
263 class OpRemoveNode(OpCode):
266 @type node_name: C{str}
267 @ivar node_name: The name of the node to remove. If the node still has
268 instances on it, the operation will fail.
271 OP_ID = "OP_NODE_REMOVE"
272 OP_DSC_FIELD = "node_name"
273 __slots__ = ["node_name"]
276 class OpAddNode(OpCode):
277 """Add a node to the cluster.
279 @type node_name: C{str}
280 @ivar node_name: The name of the node to add. This can be a short name,
281 but it will be expanded to the FQDN.
282 @type primary_ip: IP address
283 @ivar primary_ip: The primary IP of the node. This will be ignored when the
284 opcode is submitted, but will be filled during the node
285 add (so it will be visible in the job query).
286 @type secondary_ip: IP address
287 @ivar secondary_ip: The secondary IP of the node. This needs to be passed
288 if the cluster has been initialized in 'dual-network'
289 mode, otherwise it must not be given.
291 @ivar readd: Whether to re-add an existing node to the cluster. If
292 this is not passed, then the operation will abort if the node
293 name is already in the cluster; use this parameter to 'repair'
294 a node that had its configuration broken, or was reinstalled
295 without removal from the cluster.
298 OP_ID = "OP_NODE_ADD"
299 OP_DSC_FIELD = "node_name"
300 __slots__ = ["node_name", "primary_ip", "secondary_ip", "readd"]
303 class OpQueryNodes(OpCode):
304 """Compute the list of nodes."""
305 OP_ID = "OP_NODE_QUERY"
306 __slots__ = ["output_fields", "names"]
309 class OpQueryNodeVolumes(OpCode):
310 """Get list of volumes on node."""
311 OP_ID = "OP_NODE_QUERYVOLS"
312 __slots__ = ["nodes", "output_fields"]
317 class OpCreateInstance(OpCode):
318 """Create an instance."""
319 OP_ID = "OP_INSTANCE_CREATE"
320 OP_DSC_FIELD = "instance_name"
322 "instance_name", "mem_size", "disk_size", "os_type", "pnode",
323 "disk_template", "snode", "swap_size", "mode",
324 "vcpus", "ip", "bridge", "src_node", "src_path", "start",
325 "wait_for_sync", "ip_check", "mac",
326 "kernel_path", "initrd_path", "hvm_boot_order", "hvm_acpi",
327 "hvm_pae", "hvm_cdrom_image_path", "vnc_bind_address",
328 "file_storage_dir", "file_driver",
329 "iallocator", "hvm_nic_type", "hvm_disk_type",
333 class OpReinstallInstance(OpCode):
334 """Reinstall an instance's OS."""
335 OP_ID = "OP_INSTANCE_REINSTALL"
336 OP_DSC_FIELD = "instance_name"
337 __slots__ = ["instance_name", "os_type"]
340 class OpRemoveInstance(OpCode):
341 """Remove an instance."""
342 OP_ID = "OP_INSTANCE_REMOVE"
343 OP_DSC_FIELD = "instance_name"
344 __slots__ = ["instance_name", "ignore_failures"]
347 class OpRenameInstance(OpCode):
348 """Rename an instance."""
349 OP_ID = "OP_INSTANCE_RENAME"
350 __slots__ = ["instance_name", "ignore_ip", "new_name"]
353 class OpStartupInstance(OpCode):
354 """Startup an instance."""
355 OP_ID = "OP_INSTANCE_STARTUP"
356 OP_DSC_FIELD = "instance_name"
357 __slots__ = ["instance_name", "force", "extra_args"]
360 class OpShutdownInstance(OpCode):
361 """Shutdown an instance."""
362 OP_ID = "OP_INSTANCE_SHUTDOWN"
363 OP_DSC_FIELD = "instance_name"
364 __slots__ = ["instance_name"]
367 class OpRebootInstance(OpCode):
368 """Reboot an instance."""
369 OP_ID = "OP_INSTANCE_REBOOT"
370 OP_DSC_FIELD = "instance_name"
371 __slots__ = ["instance_name", "reboot_type", "extra_args",
372 "ignore_secondaries" ]
375 class OpReplaceDisks(OpCode):
376 """Replace the disks of an instance."""
377 OP_ID = "OP_INSTANCE_REPLACE_DISKS"
378 OP_DSC_FIELD = "instance_name"
379 __slots__ = ["instance_name", "remote_node", "mode", "disks", "iallocator"]
382 class OpFailoverInstance(OpCode):
383 """Failover an instance."""
384 OP_ID = "OP_INSTANCE_FAILOVER"
385 OP_DSC_FIELD = "instance_name"
386 __slots__ = ["instance_name", "ignore_consistency"]
389 class OpConnectConsole(OpCode):
390 """Connect to an instance's console."""
391 OP_ID = "OP_INSTANCE_CONSOLE"
392 OP_DSC_FIELD = "instance_name"
393 __slots__ = ["instance_name"]
396 class OpActivateInstanceDisks(OpCode):
397 """Activate an instance's disks."""
398 OP_ID = "OP_INSTANCE_ACTIVATE_DISKS"
399 OP_DSC_FIELD = "instance_name"
400 __slots__ = ["instance_name"]
403 class OpDeactivateInstanceDisks(OpCode):
404 """Deactivate an instance's disks."""
405 OP_ID = "OP_INSTANCE_DEACTIVATE_DISKS"
406 OP_DSC_FIELD = "instance_name"
407 __slots__ = ["instance_name"]
410 class OpQueryInstances(OpCode):
411 """Compute the list of instances."""
412 OP_ID = "OP_INSTANCE_QUERY"
413 __slots__ = ["output_fields", "names"]
416 class OpQueryInstanceData(OpCode):
417 """Compute the run-time status of instances."""
418 OP_ID = "OP_INSTANCE_QUERY_DATA"
419 __slots__ = ["instances"]
422 class OpSetInstanceParams(OpCode):
423 """Change the parameters of an instance."""
424 OP_ID = "OP_INSTANCE_SET_PARAMS"
425 OP_DSC_FIELD = "instance_name"
427 "instance_name", "mem", "vcpus", "ip", "bridge", "mac",
428 "kernel_path", "initrd_path", "hvm_boot_order", "hvm_acpi",
429 "hvm_pae", "hvm_cdrom_image_path", "vnc_bind_address",
430 "hvm_nic_type", "hvm_disk_type", "force"
434 class OpGrowDisk(OpCode):
435 """Grow a disk of an instance."""
436 OP_ID = "OP_INSTANCE_GROW_DISK"
437 OP_DSC_FIELD = "instance_name"
438 __slots__ = ["instance_name", "disk", "amount"]
442 class OpDiagnoseOS(OpCode):
443 """Compute the list of guest operating systems."""
444 OP_ID = "OP_OS_DIAGNOSE"
445 __slots__ = ["output_fields", "names"]
449 class OpQueryExports(OpCode):
450 """Compute the list of exported images."""
451 OP_ID = "OP_BACKUP_QUERY"
452 __slots__ = ["nodes"]
455 class OpExportInstance(OpCode):
456 """Export an instance."""
457 OP_ID = "OP_BACKUP_EXPORT"
458 OP_DSC_FIELD = "instance_name"
459 __slots__ = ["instance_name", "target_node", "shutdown"]
462 class OpRemoveExport(OpCode):
463 """Remove an instance's export."""
464 OP_ID = "OP_BACKUP_REMOVE"
465 OP_DSC_FIELD = "instance_name"
466 __slots__ = ["instance_name"]
470 class OpGetTags(OpCode):
471 """Returns the tags of the given object."""
472 OP_ID = "OP_TAGS_GET"
473 OP_DSC_FIELD = "name"
474 __slots__ = ["kind", "name"]
477 class OpSearchTags(OpCode):
478 """Searches the tags in the cluster for a given pattern."""
479 OP_ID = "OP_TAGS_SEARCH"
480 OP_DSC_FIELD = "pattern"
481 __slots__ = ["pattern"]
484 class OpAddTags(OpCode):
485 """Add a list of tags on a given object."""
486 OP_ID = "OP_TAGS_SET"
487 __slots__ = ["kind", "name", "tags"]
490 class OpDelTags(OpCode):
491 """Remove a list of tags from a given object."""
492 OP_ID = "OP_TAGS_DEL"
493 __slots__ = ["kind", "name", "tags"]
497 class OpTestDelay(OpCode):
498 """Sleeps for a configured amount of time.
500 This is used just for debugging and testing.
503 - duration: the time to sleep
504 - on_master: if true, sleep on the master
505 - on_nodes: list of nodes in which to sleep
507 If the on_master parameter is true, it will execute a sleep on the
508 master (before any node sleep).
510 If the on_nodes list is not empty, it will sleep on those nodes
511 (after the sleep on the master, if that is enabled).
513 As an additional feature, the case of duration < 0 will be reported
514 as an execution error, so this opcode can be used as a failure
515 generator. The case of duration == 0 will not be treated specially.
518 OP_ID = "OP_TEST_DELAY"
519 OP_DSC_FIELD = "duration"
520 __slots__ = ["duration", "on_master", "on_nodes"]
523 class OpTestAllocator(OpCode):
524 """Allocator framework testing.
526 This opcode has two modes:
527 - gather and return allocator input for a given mode (allocate new
528 or replace secondary) and a given instance definition (direction
530 - run a selected allocator for a given operation (as above) and
531 return the allocator output (direction 'out')
534 OP_ID = "OP_TEST_ALLOCATOR"
535 OP_DSC_FIELD = "allocator"
537 "direction", "mode", "allocator", "name",
538 "mem_size", "disks", "disk_template",
539 "os", "tags", "nics", "vcpus",