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
22 """Transportable objects for Ganeti.
24 This module provides small, mostly data-only objects which are safe to
25 pass to and from external parties.
33 from cStringIO import StringIO
35 from ganeti import errors
36 from ganeti import constants
39 __all__ = ["ConfigObject", "ConfigData", "NIC", "Disk", "Instance",
40 "OS", "Node", "Cluster"]
43 class ConfigObject(object):
44 """A generic config object.
46 It has the following properties:
48 - provides somewhat safe recursive unpickling and pickling for its classes
49 - unset attributes which are defined in slots are always returned
50 as None instead of raising an error
52 Classes derived from this must always declare __slots__ (we use many
53 config objects and the memory reduction is useful)
58 def __init__(self, **kwargs):
59 for k, v in kwargs.iteritems():
63 def __getattr__(self, name):
64 if name not in self.__slots__:
65 raise AttributeError("Invalid object attribute %s.%s" %
66 (type(self).__name__, name))
69 def __setitem__(self, key, value):
70 if key not in self.__slots__:
72 setattr(self, key, value)
74 def __getstate__(self):
76 for name in self.__slots__:
77 if hasattr(self, name):
78 state[name] = getattr(self, name)
81 def __setstate__(self, state):
83 if name in self.__slots__:
84 setattr(self, name, state[name])
87 """Convert to a dict holding only standard python types.
89 The generic routine just dumps all of this object's attributes in
90 a dict. It does not work if the class has children who are
91 ConfigObjects themselves (e.g. the nics list in an Instance), in
92 which case the object should subclass the function in order to
93 make sure all objects returned are only standard python types.
96 return dict([(k, getattr(self, k, None)) for k in self.__slots__])
99 def FromDict(cls, val):
100 """Create an object from a dictionary.
102 This generic routine takes a dict, instantiates a new instance of
103 the given class, and sets attributes based on the dict content.
105 As for `ToDict`, this does not work if the class has children
106 who are ConfigObjects themselves (e.g. the nics list in an
107 Instance), in which case the object should subclass the function
108 and alter the objects.
111 if not isinstance(val, dict):
112 raise errors.ConfigurationError("Invalid object passed to FromDict:"
113 " expected dict, got %s" % type(val))
114 val_str = dict([(str(k), v) for k, v in val.iteritems()])
119 def _ContainerToDicts(container):
120 """Convert the elements of a container to standard python types.
122 This method converts a container with elements derived from
123 ConfigData to standard python types. If the container is a dict,
124 we don't touch the keys, only the values.
127 if isinstance(container, dict):
128 ret = dict([(k, v.ToDict()) for k, v in container.iteritems()])
129 elif isinstance(container, (list, tuple, set, frozenset)):
130 ret = [elem.ToDict() for elem in container]
132 raise TypeError("Invalid type %s passed to _ContainerToDicts" %
137 def _ContainerFromDicts(source, c_type, e_type):
138 """Convert a container from standard python types.
140 This method converts a container with standard python types to
141 ConfigData objects. If the container is a dict, we don't touch the
142 keys, only the values.
145 if not isinstance(c_type, type):
146 raise TypeError("Container type %s passed to _ContainerFromDicts is"
147 " not a type" % type(c_type))
149 ret = dict([(k, e_type.FromDict(v)) for k, v in source.iteritems()])
150 elif c_type in (list, tuple, set, frozenset):
151 ret = c_type([e_type.FromDict(elem) for elem in source])
153 raise TypeError("Invalid container type %s passed to"
154 " _ContainerFromDicts" % c_type)
158 """Implement __repr__ for ConfigObjects."""
159 return repr(self.ToDict())
161 def UpgradeConfig(self):
162 """Fill defaults for missing configuration values.
164 This method will be called at object init time, and its implementation will
171 class TaggableObject(ConfigObject):
172 """An generic class supporting tags.
175 __slots__ = ConfigObject.__slots__ + ["tags"]
178 def ValidateTag(tag):
179 """Check if a tag is valid.
181 If the tag is invalid, an errors.TagError will be raised. The
182 function has no return value.
185 if not isinstance(tag, basestring):
186 raise errors.TagError("Invalid tag type (not a string)")
187 if len(tag) > constants.MAX_TAG_LEN:
188 raise errors.TagError("Tag too long (>%d characters)" %
189 constants.MAX_TAG_LEN)
191 raise errors.TagError("Tags cannot be empty")
192 if not re.match("^[\w.+*/:-]+$", tag):
193 raise errors.TagError("Tag contains invalid characters")
196 """Return the tags list.
199 tags = getattr(self, "tags", None)
201 tags = self.tags = set()
204 def AddTag(self, tag):
208 self.ValidateTag(tag)
209 tags = self.GetTags()
210 if len(tags) >= constants.MAX_TAGS_PER_OBJ:
211 raise errors.TagError("Too many tags")
212 self.GetTags().add(tag)
214 def RemoveTag(self, tag):
218 self.ValidateTag(tag)
219 tags = self.GetTags()
223 raise errors.TagError("Tag not found")
226 """Taggable-object-specific conversion to standard python types.
228 This replaces the tags set with a list.
231 bo = super(TaggableObject, self).ToDict()
233 tags = bo.get("tags", None)
234 if isinstance(tags, set):
235 bo["tags"] = list(tags)
239 def FromDict(cls, val):
240 """Custom function for instances.
243 obj = super(TaggableObject, cls).FromDict(val)
244 if hasattr(obj, "tags") and isinstance(obj.tags, list):
245 obj.tags = set(obj.tags)
249 class ConfigData(ConfigObject):
250 """Top-level config object."""
251 __slots__ = ["version", "cluster", "nodes", "instances", "serial_no"]
254 """Custom function for top-level config data.
256 This just replaces the list of instances, nodes and the cluster
257 with standard python types.
260 mydict = super(ConfigData, self).ToDict()
261 mydict["cluster"] = mydict["cluster"].ToDict()
262 for key in "nodes", "instances":
263 mydict[key] = self._ContainerToDicts(mydict[key])
268 def FromDict(cls, val):
269 """Custom function for top-level config data
272 obj = super(ConfigData, cls).FromDict(val)
273 obj.cluster = Cluster.FromDict(obj.cluster)
274 obj.nodes = cls._ContainerFromDicts(obj.nodes, dict, Node)
275 obj.instances = cls._ContainerFromDicts(obj.instances, dict, Instance)
279 class NIC(ConfigObject):
280 """Config object representing a network card."""
281 __slots__ = ["mac", "ip", "bridge"]
284 class Disk(ConfigObject):
285 """Config object representing a block device."""
286 __slots__ = ["dev_type", "logical_id", "physical_id",
287 "children", "iv_name", "size", "mode"]
289 def CreateOnSecondary(self):
290 """Test if this device needs to be created on a secondary node."""
291 return self.dev_type in (constants.LD_DRBD8, constants.LD_LV)
293 def AssembleOnSecondary(self):
294 """Test if this device needs to be assembled on a secondary node."""
295 return self.dev_type in (constants.LD_DRBD8, constants.LD_LV)
297 def OpenOnSecondary(self):
298 """Test if this device needs to be opened on a secondary node."""
299 return self.dev_type in (constants.LD_LV,)
301 def StaticDevPath(self):
302 """Return the device path if this device type has a static one.
304 Some devices (LVM for example) live always at the same /dev/ path,
305 irrespective of their status. For such devices, we return this
306 path, for others we return None.
309 if self.dev_type == constants.LD_LV:
310 return "/dev/%s/%s" % (self.logical_id[0], self.logical_id[1])
313 def ChildrenNeeded(self):
314 """Compute the needed number of children for activation.
316 This method will return either -1 (all children) or a positive
317 number denoting the minimum number of children needed for
318 activation (only mirrored devices will usually return >=0).
320 Currently, only DRBD8 supports diskless activation (therefore we
321 return 0), for all other we keep the previous semantics and return
325 if self.dev_type == constants.LD_DRBD8:
329 def GetNodes(self, node):
330 """This function returns the nodes this device lives on.
332 Given the node on which the parent of the device lives on (or, in
333 case of a top-level device, the primary node of the devices'
334 instance), this function will return a list of nodes on which this
335 devices needs to (or can) be assembled.
338 if self.dev_type in [constants.LD_LV, constants.LD_FILE]:
340 elif self.dev_type in constants.LDS_DRBD:
341 result = [self.logical_id[0], self.logical_id[1]]
342 if node not in result:
343 raise errors.ConfigurationError("DRBD device passed unknown node")
345 raise errors.ProgrammerError("Unhandled device type %s" % self.dev_type)
348 def ComputeNodeTree(self, parent_node):
349 """Compute the node/disk tree for this disk and its children.
351 This method, given the node on which the parent disk lives, will
352 return the list of all (node, disk) pairs which describe the disk
353 tree in the most compact way. For example, a drbd/lvm stack
354 will be returned as (primary_node, drbd) and (secondary_node, drbd)
355 which represents all the top-level devices on the nodes.
358 my_nodes = self.GetNodes(parent_node)
359 result = [(node, self) for node in my_nodes]
360 if not self.children:
363 for node in my_nodes:
364 for child in self.children:
365 child_result = child.ComputeNodeTree(node)
366 if len(child_result) == 1:
367 # child (and all its descendants) is simple, doesn't split
368 # over multiple hosts, so we don't need to describe it, our
369 # own entry for this node describes it completely
372 # check if child nodes differ from my nodes; note that
373 # subdisk can differ from the child itself, and be instead
374 # one of its descendants
375 for subnode, subdisk in child_result:
376 if subnode not in my_nodes:
377 result.append((subnode, subdisk))
378 # otherwise child is under our own node, so we ignore this
379 # entry (but probably the other results in the list will
383 def RecordGrow(self, amount):
384 """Update the size of this disk after growth.
386 This method recurses over the disks's children and updates their
387 size correspondigly. The method needs to be kept in sync with the
388 actual algorithms from bdev.
391 if self.dev_type == constants.LD_LV:
393 elif self.dev_type == constants.LD_DRBD8:
395 self.children[0].RecordGrow(amount)
398 raise errors.ProgrammerError("Disk.RecordGrow called for unsupported"
399 " disk type %s" % self.dev_type)
401 def SetPhysicalID(self, target_node, nodes_ip):
402 """Convert the logical ID to the physical ID.
404 This is used only for drbd, which needs ip/port configuration.
406 The routine descends down and updates its children also, because
407 this helps when the only the top device is passed to the remote
411 - target_node: the node we wish to configure for
412 - nodes_ip: a mapping of node name to ip
414 The target_node must exist in in nodes_ip, and must be one of the
415 nodes in the logical ID for each of the DRBD devices encountered
420 for child in self.children:
421 child.SetPhysicalID(target_node, nodes_ip)
423 if self.logical_id is None and self.physical_id is not None:
425 if self.dev_type in constants.LDS_DRBD:
426 pnode, snode, port, pminor, sminor, secret = self.logical_id
427 if target_node not in (pnode, snode):
428 raise errors.ConfigurationError("DRBD device not knowing node %s" %
430 pnode_ip = nodes_ip.get(pnode, None)
431 snode_ip = nodes_ip.get(snode, None)
432 if pnode_ip is None or snode_ip is None:
433 raise errors.ConfigurationError("Can't find primary or secondary node"
434 " for %s" % str(self))
435 p_data = (pnode_ip, port)
436 s_data = (snode_ip, port)
437 if pnode == target_node:
438 self.physical_id = p_data + s_data + (pminor, secret)
439 else: # it must be secondary, we tested above
440 self.physical_id = s_data + p_data + (sminor, secret)
442 self.physical_id = self.logical_id
446 """Disk-specific conversion to standard python types.
448 This replaces the children lists of objects with lists of
449 standard python types.
452 bo = super(Disk, self).ToDict()
454 for attr in ("children",):
455 alist = bo.get(attr, None)
457 bo[attr] = self._ContainerToDicts(alist)
461 def FromDict(cls, val):
462 """Custom function for Disks
465 obj = super(Disk, cls).FromDict(val)
467 obj.children = cls._ContainerFromDicts(obj.children, list, Disk)
468 if obj.logical_id and isinstance(obj.logical_id, list):
469 obj.logical_id = tuple(obj.logical_id)
470 if obj.physical_id and isinstance(obj.physical_id, list):
471 obj.physical_id = tuple(obj.physical_id)
472 if obj.dev_type in constants.LDS_DRBD:
473 # we need a tuple of length six here
474 if len(obj.logical_id) < 6:
475 obj.logical_id += (None,) * (6 - len(obj.logical_id))
479 """Custom str() formatter for disks.
482 if self.dev_type == constants.LD_LV:
483 val = "<LogicalVolume(/dev/%s/%s" % self.logical_id
484 elif self.dev_type in constants.LDS_DRBD:
485 node_a, node_b, port, minor_a, minor_b = self.logical_id[:5]
487 if self.physical_id is None:
490 phy = ("configured as %s:%s %s:%s" %
491 (self.physical_id[0], self.physical_id[1],
492 self.physical_id[2], self.physical_id[3]))
494 val += ("hosts=%s/%d-%s/%d, port=%s, %s, " %
495 (node_a, minor_a, node_b, minor_b, port, phy))
496 if self.children and self.children.count(None) == 0:
497 val += "backend=%s, metadev=%s" % (self.children[0], self.children[1])
499 val += "no local storage"
501 val = ("<Disk(type=%s, logical_id=%s, physical_id=%s, children=%s" %
502 (self.dev_type, self.logical_id, self.physical_id, self.children))
503 if self.iv_name is None:
504 val += ", not visible"
506 val += ", visible as /dev/%s" % self.iv_name
507 if isinstance(self.size, int):
508 val += ", size=%dm)>" % self.size
510 val += ", size='%s')>" % (self.size,)
514 """Checks that this disk is correctly configured.
518 if self.mode not in constants.DISK_ACCESS_SET:
519 errors.append("Disk access mode '%s' is invalid" % (self.mode, ))
523 class Instance(TaggableObject):
524 """Config object representing an instance."""
525 __slots__ = TaggableObject.__slots__ + [
540 def _ComputeSecondaryNodes(self):
541 """Compute the list of secondary nodes.
543 This is a simple wrapper over _ComputeAllNodes.
546 all_nodes = set(self._ComputeAllNodes())
547 all_nodes.discard(self.primary_node)
548 return tuple(all_nodes)
550 secondary_nodes = property(_ComputeSecondaryNodes, None, None,
551 "List of secondary nodes")
553 def _ComputeAllNodes(self):
554 """Compute the list of all nodes.
556 Since the data is already there (in the drbd disks), keeping it as
557 a separate normal attribute is redundant and if not properly
558 synchronised can cause problems. Thus it's better to compute it
562 def _Helper(nodes, device):
563 """Recursively computes nodes given a top device."""
564 if device.dev_type in constants.LDS_DRBD:
565 nodea, nodeb = device.logical_id[:2]
569 for child in device.children:
570 _Helper(nodes, child)
573 all_nodes.add(self.primary_node)
574 for device in self.disks:
575 _Helper(all_nodes, device)
576 return tuple(all_nodes)
578 all_nodes = property(_ComputeAllNodes, None, None,
579 "List of all nodes of the instance")
581 def MapLVsByNode(self, lvmap=None, devs=None, node=None):
582 """Provide a mapping of nodes to LVs this instance owns.
584 This function figures out what logical volumes should belong on
585 which nodes, recursing through a device tree.
587 @param lvmap: optional dictionary to receive the
588 'node' : ['lv', ...] data.
590 @return: None if lvmap arg is given, otherwise, a dictionary
591 of the form { 'nodename' : ['volume1', 'volume2', ...], ... }
595 node = self.primary_node
598 lvmap = { node : [] }
601 if not node in lvmap:
609 if dev.dev_type == constants.LD_LV:
610 lvmap[node].append(dev.logical_id[1])
612 elif dev.dev_type in constants.LDS_DRBD:
614 self.MapLVsByNode(lvmap, dev.children, dev.logical_id[0])
615 self.MapLVsByNode(lvmap, dev.children, dev.logical_id[1])
618 self.MapLVsByNode(lvmap, dev.children, node)
622 def FindDisk(self, idx):
623 """Find a disk given having a specified index.
625 This is just a wrapper that does validation of the index.
628 @param idx: the disk index
630 @return: the corresponding disk
631 @raise errors.OpPrereqError: when the given index is not valid
636 return self.disks[idx]
637 except ValueError, err:
638 raise errors.OpPrereqError("Invalid disk index: '%s'" % str(err))
640 raise errors.OpPrereqError("Invalid disk index: %d (instace has disks"
641 " 0 to %d" % (idx, len(self.disks)))
644 """Instance-specific conversion to standard python types.
646 This replaces the children lists of objects with lists of standard
650 bo = super(Instance, self).ToDict()
652 for attr in "nics", "disks":
653 alist = bo.get(attr, None)
655 nlist = self._ContainerToDicts(alist)
662 def FromDict(cls, val):
663 """Custom function for instances.
666 obj = super(Instance, cls).FromDict(val)
667 obj.nics = cls._ContainerFromDicts(obj.nics, list, NIC)
668 obj.disks = cls._ContainerFromDicts(obj.disks, list, Disk)
672 class OS(ConfigObject):
673 """Config object representing an operating system."""
686 def FromInvalidOS(cls, err):
687 """Create an OS from an InvalidOS error.
689 This routine knows how to convert an InvalidOS error to an OS
690 object representing the broken OS with a meaningful error message.
693 if not isinstance(err, errors.InvalidOS):
694 raise errors.ProgrammerError("Trying to initialize an OS from an"
695 " invalid object of type %s" % type(err))
697 return cls(name=err.args[0], path=err.args[1], status=err.args[2])
699 def __nonzero__(self):
700 return self.status == constants.OS_VALID_STATUS
702 __bool__ = __nonzero__
705 class Node(TaggableObject):
706 """Config object representing a node."""
707 __slots__ = TaggableObject.__slots__ + [
718 class Cluster(TaggableObject):
719 """Config object representing the cluster."""
720 __slots__ = TaggableObject.__slots__ + [
728 "default_hypervisor",
734 "enabled_hypervisors",
737 "candidate_pool_size",
741 def UpgradeConfig(self):
742 """Fill defaults for missing configuration values.
745 if self.hvparams is None:
746 self.hvparams = constants.HVC_DEFAULTS
748 for hypervisor in self.hvparams:
749 self.hvparams[hypervisor] = self.FillDict(
750 constants.HVC_DEFAULTS[hypervisor], self.hvparams[hypervisor])
752 if self.beparams is None:
753 self.beparams = {constants.BEGR_DEFAULT: constants.BEC_DEFAULTS}
755 for begroup in self.beparams:
756 self.beparams[begroup] = self.FillDict(constants.BEC_DEFAULTS,
757 self.beparams[begroup])
759 if self.modify_etc_hosts is None:
760 self.modify_etc_hosts = True
763 """Custom function for cluster.
766 mydict = super(Cluster, self).ToDict()
767 mydict["tcpudp_port_pool"] = list(self.tcpudp_port_pool)
771 def FromDict(cls, val):
772 """Custom function for cluster.
775 obj = super(Cluster, cls).FromDict(val)
776 if not isinstance(obj.tcpudp_port_pool, set):
777 obj.tcpudp_port_pool = set(obj.tcpudp_port_pool)
781 def FillDict(defaults_dict, custom_dict):
782 """Basic function to apply settings on top a default dict.
784 @type defaults_dict: dict
785 @param defaults_dict: dictionary holding the default values
786 @type custom_dict: dict
787 @param custom_dict: dictionary holding customized value
789 @return: dict with the 'full' values
792 ret_dict = copy.deepcopy(defaults_dict)
793 ret_dict.update(custom_dict)
796 def FillHV(self, instance):
797 """Fill an instance's hvparams dict.
799 @type instance: object
800 @param instance: the instance parameter to fill
802 @return: a copy of the instance's hvparams with missing keys filled from
806 return self.FillDict(self.hvparams.get(instance.hypervisor, {}),
809 def FillBE(self, instance):
810 """Fill an instance's beparams dict.
812 @type instance: object
813 @param instance: the instance parameter to fill
815 @return: a copy of the instance's beparams with missing keys filled from
819 return self.FillDict(self.beparams.get(constants.BEGR_DEFAULT, {}),
823 class SerializableConfigParser(ConfigParser.SafeConfigParser):
824 """Simple wrapper over ConfigParse that allows serialization.
826 This class is basically ConfigParser.SafeConfigParser with two
827 additional methods that allow it to serialize/unserialize to/from a
832 """Dump this instance and return the string representation."""
835 return buf.getvalue()
839 """Load data from a string."""
841 cfp = SerializableConfigParser()