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 class OpDestroyCluster(OpCode):
163 """Destroy the cluster.
165 This opcode has no other parameters. All the state is irreversibly
166 lost after the execution of this opcode.
169 OP_ID = "OP_CLUSTER_DESTROY"
173 class OpQueryClusterInfo(OpCode):
174 """Query cluster information."""
175 OP_ID = "OP_CLUSTER_QUERY"
179 class OpVerifyCluster(OpCode):
180 """Verify the cluster state.
182 @type skip_checks: C{list}
183 @ivar skip_checks: steps to be skipped from the verify process; this
184 needs to be a subset of
185 L{constants.VERIFY_OPTIONAL_CHECKS}; currently
186 only L{constants.VERIFY_NPLUSONE_MEM} can be passed
189 OP_ID = "OP_CLUSTER_VERIFY"
190 __slots__ = ["skip_checks"]
193 class OpVerifyDisks(OpCode):
194 """Verify the cluster disks.
199 - list of node names with bad data returned (unreachable, etc.)
200 - dict of node names with broken volume groups (values: error msg)
201 - list of instances with degraded disks (that should be activated)
202 - dict of instances with missing logical volumes (values: (node, vol)
203 pairs with details about the missing volumes)
205 In normal operation, all lists should be empty. A non-empty instance
206 list (3rd element of the result) is still ok (errors were fixed) but
207 non-empty node list means some node is down, and probably there are
208 unfixable drbd errors.
210 Note that only instances that are drbd-based are taken into
211 consideration. This might need to be revisited in the future.
214 OP_ID = "OP_CLUSTER_VERIFY_DISKS"
218 class OpDumpClusterConfig(OpCode):
219 """Dump the cluster configuration."""
220 OP_ID = "OP_CLUSTER_DUMPCONFIG"
224 class OpRenameCluster(OpCode):
225 """Rename the cluster.
228 @ivar name: The new name of the cluster. The name and/or the master IP
229 address will be changed to match the new name and its IP
233 OP_ID = "OP_CLUSTER_RENAME"
237 class OpSetClusterParams(OpCode):
238 """Change the parameters of the cluster.
240 @type vg_name: C{str} or C{None}
241 @ivar vg_name: The new volume group name or None to disable LVM usage.
244 OP_ID = "OP_CLUSTER_SET_PARAMS"
245 __slots__ = ["vg_name"]
250 class OpRemoveNode(OpCode):
253 @type node_name: C{str}
254 @ivar node_name: The name of the node to remove. If the node still has
255 instances on it, the operation will fail.
258 OP_ID = "OP_NODE_REMOVE"
259 __slots__ = ["node_name"]
262 class OpAddNode(OpCode):
263 """Add a node to the cluster.
265 @type node_name: C{str}
266 @ivar node_name: The name of the node to add. This can be a short name,
267 but it will be expanded to the FQDN.
268 @type primary_ip: IP address
269 @ivar primary_ip: The primary IP of the node. This will be ignored when the
270 opcode is submitted, but will be filled during the node
271 add (so it will be visible in the job query).
272 @type secondary_ip: IP address
273 @ivar secondary_ip: The secondary IP of the node. This needs to be passed
274 if the cluster has been initialized in 'dual-network'
275 mode, otherwise it must not be given.
277 @ivar readd: Whether to re-add an existing node to the cluster. If
278 this is not passed, then the operation will abort if the node
279 name is already in the cluster; use this parameter to 'repair'
280 a node that had its configuration broken, or was reinstalled
281 without removal from the cluster.
284 OP_ID = "OP_NODE_ADD"
285 __slots__ = ["node_name", "primary_ip", "secondary_ip", "readd"]
288 class OpQueryNodes(OpCode):
289 """Compute the list of nodes."""
290 OP_ID = "OP_NODE_QUERY"
291 __slots__ = ["output_fields", "names"]
294 class OpQueryNodeVolumes(OpCode):
295 """Get list of volumes on node."""
296 OP_ID = "OP_NODE_QUERYVOLS"
297 __slots__ = ["nodes", "output_fields"]
302 class OpCreateInstance(OpCode):
303 """Create an instance."""
304 OP_ID = "OP_INSTANCE_CREATE"
306 "instance_name", "mem_size", "disk_size", "os_type", "pnode",
307 "disk_template", "snode", "swap_size", "mode",
308 "vcpus", "ip", "bridge", "src_node", "src_path", "start",
309 "wait_for_sync", "ip_check", "mac",
310 "kernel_path", "initrd_path", "hvm_boot_order", "hvm_acpi",
311 "hvm_pae", "hvm_cdrom_image_path", "vnc_bind_address",
312 "file_storage_dir", "file_driver",
317 class OpReinstallInstance(OpCode):
318 """Reinstall an instance's OS."""
319 OP_ID = "OP_INSTANCE_REINSTALL"
320 __slots__ = ["instance_name", "os_type"]
323 class OpRemoveInstance(OpCode):
324 """Remove an instance."""
325 OP_ID = "OP_INSTANCE_REMOVE"
326 __slots__ = ["instance_name", "ignore_failures"]
329 class OpRenameInstance(OpCode):
330 """Rename an instance."""
331 OP_ID = "OP_INSTANCE_RENAME"
332 __slots__ = ["instance_name", "ignore_ip", "new_name"]
335 class OpStartupInstance(OpCode):
336 """Startup an instance."""
337 OP_ID = "OP_INSTANCE_STARTUP"
338 __slots__ = ["instance_name", "force", "extra_args"]
341 class OpShutdownInstance(OpCode):
342 """Shutdown an instance."""
343 OP_ID = "OP_INSTANCE_SHUTDOWN"
344 __slots__ = ["instance_name"]
347 class OpRebootInstance(OpCode):
348 """Reboot an instance."""
349 OP_ID = "OP_INSTANCE_REBOOT"
350 __slots__ = ["instance_name", "reboot_type", "extra_args",
351 "ignore_secondaries" ]
354 class OpReplaceDisks(OpCode):
355 """Replace the disks of an instance."""
356 OP_ID = "OP_INSTANCE_REPLACE_DISKS"
357 __slots__ = ["instance_name", "remote_node", "mode", "disks", "iallocator"]
360 class OpFailoverInstance(OpCode):
361 """Failover an instance."""
362 OP_ID = "OP_INSTANCE_FAILOVER"
363 __slots__ = ["instance_name", "ignore_consistency"]
366 class OpConnectConsole(OpCode):
367 """Connect to an instance's console."""
368 OP_ID = "OP_INSTANCE_CONSOLE"
369 __slots__ = ["instance_name"]
372 class OpActivateInstanceDisks(OpCode):
373 """Activate an instance's disks."""
374 OP_ID = "OP_INSTANCE_ACTIVATE_DISKS"
375 __slots__ = ["instance_name"]
378 class OpDeactivateInstanceDisks(OpCode):
379 """Deactivate an instance's disks."""
380 OP_ID = "OP_INSTANCE_DEACTIVATE_DISKS"
381 __slots__ = ["instance_name"]
384 class OpQueryInstances(OpCode):
385 """Compute the list of instances."""
386 OP_ID = "OP_INSTANCE_QUERY"
387 __slots__ = ["output_fields", "names"]
390 class OpQueryInstanceData(OpCode):
391 """Compute the run-time status of instances."""
392 OP_ID = "OP_INSTANCE_QUERY_DATA"
393 __slots__ = ["instances"]
396 class OpSetInstanceParams(OpCode):
397 """Change the parameters of an instance."""
398 OP_ID = "OP_INSTANCE_SET_PARAMS"
400 "instance_name", "mem", "vcpus", "ip", "bridge", "mac",
401 "kernel_path", "initrd_path", "hvm_boot_order", "hvm_acpi",
402 "hvm_pae", "hvm_cdrom_image_path", "vnc_bind_address"
406 class OpGrowDisk(OpCode):
407 """Grow a disk of an instance."""
408 OP_ID = "OP_INSTANCE_GROW_DISK"
409 __slots__ = ["instance_name", "disk", "amount"]
413 class OpDiagnoseOS(OpCode):
414 """Compute the list of guest operating systems."""
415 OP_ID = "OP_OS_DIAGNOSE"
416 __slots__ = ["output_fields", "names"]
420 class OpQueryExports(OpCode):
421 """Compute the list of exported images."""
422 OP_ID = "OP_BACKUP_QUERY"
423 __slots__ = ["nodes"]
426 class OpExportInstance(OpCode):
427 """Export an instance."""
428 OP_ID = "OP_BACKUP_EXPORT"
429 __slots__ = ["instance_name", "target_node", "shutdown"]
432 class OpRemoveExport(OpCode):
433 """Remove an instance's export."""
434 OP_ID = "OP_BACKUP_REMOVE"
435 __slots__ = ["instance_name"]
439 class OpGetTags(OpCode):
440 """Returns the tags of the given object."""
441 OP_ID = "OP_TAGS_GET"
442 __slots__ = ["kind", "name"]
445 class OpSearchTags(OpCode):
446 """Searches the tags in the cluster for a given pattern."""
447 OP_ID = "OP_TAGS_SEARCH"
448 __slots__ = ["pattern"]
451 class OpAddTags(OpCode):
452 """Add a list of tags on a given object."""
453 OP_ID = "OP_TAGS_SET"
454 __slots__ = ["kind", "name", "tags"]
457 class OpDelTags(OpCode):
458 """Remove a list of tags from a given object."""
459 OP_ID = "OP_TAGS_DEL"
460 __slots__ = ["kind", "name", "tags"]
464 class OpTestDelay(OpCode):
465 """Sleeps for a configured amount of time.
467 This is used just for debugging and testing.
470 - duration: the time to sleep
471 - on_master: if true, sleep on the master
472 - on_nodes: list of nodes in which to sleep
474 If the on_master parameter is true, it will execute a sleep on the
475 master (before any node sleep).
477 If the on_nodes list is not empty, it will sleep on those nodes
478 (after the sleep on the master, if that is enabled).
480 As an additional feature, the case of duration < 0 will be reported
481 as an execution error, so this opcode can be used as a failure
482 generator. The case of duration == 0 will not be treated specially.
485 OP_ID = "OP_TEST_DELAY"
486 __slots__ = ["duration", "on_master", "on_nodes"]
489 class OpTestAllocator(OpCode):
490 """Allocator framework testing.
492 This opcode has two modes:
493 - gather and return allocator input for a given mode (allocate new
494 or replace secondary) and a given instance definition (direction
496 - run a selected allocator for a given operation (as above) and
497 return the allocator output (direction 'out')
500 OP_ID = "OP_TEST_ALLOCATOR"
502 "direction", "mode", "allocator", "name",
503 "mem_size", "disks", "disk_template",
504 "os", "tags", "nics", "vcpus",