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", "FillDict"]
42 def FillDict(defaults_dict, custom_dict):
43 """Basic function to apply settings on top a default dict.
45 @type defaults_dict: dict
46 @param defaults_dict: dictionary holding the default values
47 @type custom_dict: dict
48 @param custom_dict: dictionary holding customized value
50 @return: dict with the 'full' values
53 ret_dict = copy.deepcopy(defaults_dict)
54 ret_dict.update(custom_dict)
57 class ConfigObject(object):
58 """A generic config object.
60 It has the following properties:
62 - provides somewhat safe recursive unpickling and pickling for its classes
63 - unset attributes which are defined in slots are always returned
64 as None instead of raising an error
66 Classes derived from this must always declare __slots__ (we use many
67 config objects and the memory reduction is useful)
72 def __init__(self, **kwargs):
73 for k, v in kwargs.iteritems():
77 def __getattr__(self, name):
78 if name not in self.__slots__:
79 raise AttributeError("Invalid object attribute %s.%s" %
80 (type(self).__name__, name))
83 def __setitem__(self, key, value):
84 if key not in self.__slots__:
86 setattr(self, key, value)
88 def __getstate__(self):
90 for name in self.__slots__:
91 if hasattr(self, name):
92 state[name] = getattr(self, name)
95 def __setstate__(self, state):
97 if name in self.__slots__:
98 setattr(self, name, state[name])
101 """Convert to a dict holding only standard python types.
103 The generic routine just dumps all of this object's attributes in
104 a dict. It does not work if the class has children who are
105 ConfigObjects themselves (e.g. the nics list in an Instance), in
106 which case the object should subclass the function in order to
107 make sure all objects returned are only standard python types.
110 return dict([(k, getattr(self, k, None)) for k in self.__slots__])
113 def FromDict(cls, val):
114 """Create an object from a dictionary.
116 This generic routine takes a dict, instantiates a new instance of
117 the given class, and sets attributes based on the dict content.
119 As for `ToDict`, this does not work if the class has children
120 who are ConfigObjects themselves (e.g. the nics list in an
121 Instance), in which case the object should subclass the function
122 and alter the objects.
125 if not isinstance(val, dict):
126 raise errors.ConfigurationError("Invalid object passed to FromDict:"
127 " expected dict, got %s" % type(val))
128 val_str = dict([(str(k), v) for k, v in val.iteritems()])
133 def _ContainerToDicts(container):
134 """Convert the elements of a container to standard python types.
136 This method converts a container with elements derived from
137 ConfigData to standard python types. If the container is a dict,
138 we don't touch the keys, only the values.
141 if isinstance(container, dict):
142 ret = dict([(k, v.ToDict()) for k, v in container.iteritems()])
143 elif isinstance(container, (list, tuple, set, frozenset)):
144 ret = [elem.ToDict() for elem in container]
146 raise TypeError("Invalid type %s passed to _ContainerToDicts" %
151 def _ContainerFromDicts(source, c_type, e_type):
152 """Convert a container from standard python types.
154 This method converts a container with standard python types to
155 ConfigData objects. If the container is a dict, we don't touch the
156 keys, only the values.
159 if not isinstance(c_type, type):
160 raise TypeError("Container type %s passed to _ContainerFromDicts is"
161 " not a type" % type(c_type))
163 ret = dict([(k, e_type.FromDict(v)) for k, v in source.iteritems()])
164 elif c_type in (list, tuple, set, frozenset):
165 ret = c_type([e_type.FromDict(elem) for elem in source])
167 raise TypeError("Invalid container type %s passed to"
168 " _ContainerFromDicts" % c_type)
172 """Implement __repr__ for ConfigObjects."""
173 return repr(self.ToDict())
175 def UpgradeConfig(self):
176 """Fill defaults for missing configuration values.
178 This method will be called at object init time, and its implementation will
185 class TaggableObject(ConfigObject):
186 """An generic class supporting tags.
189 __slots__ = ConfigObject.__slots__ + ["tags"]
192 def ValidateTag(tag):
193 """Check if a tag is valid.
195 If the tag is invalid, an errors.TagError will be raised. The
196 function has no return value.
199 if not isinstance(tag, basestring):
200 raise errors.TagError("Invalid tag type (not a string)")
201 if len(tag) > constants.MAX_TAG_LEN:
202 raise errors.TagError("Tag too long (>%d characters)" %
203 constants.MAX_TAG_LEN)
205 raise errors.TagError("Tags cannot be empty")
206 if not re.match("^[\w.+*/:-]+$", tag):
207 raise errors.TagError("Tag contains invalid characters")
210 """Return the tags list.
213 tags = getattr(self, "tags", None)
215 tags = self.tags = set()
218 def AddTag(self, tag):
222 self.ValidateTag(tag)
223 tags = self.GetTags()
224 if len(tags) >= constants.MAX_TAGS_PER_OBJ:
225 raise errors.TagError("Too many tags")
226 self.GetTags().add(tag)
228 def RemoveTag(self, tag):
232 self.ValidateTag(tag)
233 tags = self.GetTags()
237 raise errors.TagError("Tag not found")
240 """Taggable-object-specific conversion to standard python types.
242 This replaces the tags set with a list.
245 bo = super(TaggableObject, self).ToDict()
247 tags = bo.get("tags", None)
248 if isinstance(tags, set):
249 bo["tags"] = list(tags)
253 def FromDict(cls, val):
254 """Custom function for instances.
257 obj = super(TaggableObject, cls).FromDict(val)
258 if hasattr(obj, "tags") and isinstance(obj.tags, list):
259 obj.tags = set(obj.tags)
263 class ConfigData(ConfigObject):
264 """Top-level config object."""
265 __slots__ = ["version", "cluster", "nodes", "instances", "serial_no"]
268 """Custom function for top-level config data.
270 This just replaces the list of instances, nodes and the cluster
271 with standard python types.
274 mydict = super(ConfigData, self).ToDict()
275 mydict["cluster"] = mydict["cluster"].ToDict()
276 for key in "nodes", "instances":
277 mydict[key] = self._ContainerToDicts(mydict[key])
282 def FromDict(cls, val):
283 """Custom function for top-level config data
286 obj = super(ConfigData, cls).FromDict(val)
287 obj.cluster = Cluster.FromDict(obj.cluster)
288 obj.nodes = cls._ContainerFromDicts(obj.nodes, dict, Node)
289 obj.instances = cls._ContainerFromDicts(obj.instances, dict, Instance)
293 class NIC(ConfigObject):
294 """Config object representing a network card."""
295 __slots__ = ["mac", "ip", "bridge"]
298 class Disk(ConfigObject):
299 """Config object representing a block device."""
300 __slots__ = ["dev_type", "logical_id", "physical_id",
301 "children", "iv_name", "size", "mode"]
303 def CreateOnSecondary(self):
304 """Test if this device needs to be created on a secondary node."""
305 return self.dev_type in (constants.LD_DRBD8, constants.LD_LV)
307 def AssembleOnSecondary(self):
308 """Test if this device needs to be assembled on a secondary node."""
309 return self.dev_type in (constants.LD_DRBD8, constants.LD_LV)
311 def OpenOnSecondary(self):
312 """Test if this device needs to be opened on a secondary node."""
313 return self.dev_type in (constants.LD_LV,)
315 def StaticDevPath(self):
316 """Return the device path if this device type has a static one.
318 Some devices (LVM for example) live always at the same /dev/ path,
319 irrespective of their status. For such devices, we return this
320 path, for others we return None.
323 if self.dev_type == constants.LD_LV:
324 return "/dev/%s/%s" % (self.logical_id[0], self.logical_id[1])
327 def ChildrenNeeded(self):
328 """Compute the needed number of children for activation.
330 This method will return either -1 (all children) or a positive
331 number denoting the minimum number of children needed for
332 activation (only mirrored devices will usually return >=0).
334 Currently, only DRBD8 supports diskless activation (therefore we
335 return 0), for all other we keep the previous semantics and return
339 if self.dev_type == constants.LD_DRBD8:
343 def GetNodes(self, node):
344 """This function returns the nodes this device lives on.
346 Given the node on which the parent of the device lives on (or, in
347 case of a top-level device, the primary node of the devices'
348 instance), this function will return a list of nodes on which this
349 devices needs to (or can) be assembled.
352 if self.dev_type in [constants.LD_LV, constants.LD_FILE]:
354 elif self.dev_type in constants.LDS_DRBD:
355 result = [self.logical_id[0], self.logical_id[1]]
356 if node not in result:
357 raise errors.ConfigurationError("DRBD device passed unknown node")
359 raise errors.ProgrammerError("Unhandled device type %s" % self.dev_type)
362 def ComputeNodeTree(self, parent_node):
363 """Compute the node/disk tree for this disk and its children.
365 This method, given the node on which the parent disk lives, will
366 return the list of all (node, disk) pairs which describe the disk
367 tree in the most compact way. For example, a drbd/lvm stack
368 will be returned as (primary_node, drbd) and (secondary_node, drbd)
369 which represents all the top-level devices on the nodes.
372 my_nodes = self.GetNodes(parent_node)
373 result = [(node, self) for node in my_nodes]
374 if not self.children:
377 for node in my_nodes:
378 for child in self.children:
379 child_result = child.ComputeNodeTree(node)
380 if len(child_result) == 1:
381 # child (and all its descendants) is simple, doesn't split
382 # over multiple hosts, so we don't need to describe it, our
383 # own entry for this node describes it completely
386 # check if child nodes differ from my nodes; note that
387 # subdisk can differ from the child itself, and be instead
388 # one of its descendants
389 for subnode, subdisk in child_result:
390 if subnode not in my_nodes:
391 result.append((subnode, subdisk))
392 # otherwise child is under our own node, so we ignore this
393 # entry (but probably the other results in the list will
397 def RecordGrow(self, amount):
398 """Update the size of this disk after growth.
400 This method recurses over the disks's children and updates their
401 size correspondigly. The method needs to be kept in sync with the
402 actual algorithms from bdev.
405 if self.dev_type == constants.LD_LV:
407 elif self.dev_type == constants.LD_DRBD8:
409 self.children[0].RecordGrow(amount)
412 raise errors.ProgrammerError("Disk.RecordGrow called for unsupported"
413 " disk type %s" % self.dev_type)
415 def SetPhysicalID(self, target_node, nodes_ip):
416 """Convert the logical ID to the physical ID.
418 This is used only for drbd, which needs ip/port configuration.
420 The routine descends down and updates its children also, because
421 this helps when the only the top device is passed to the remote
425 - target_node: the node we wish to configure for
426 - nodes_ip: a mapping of node name to ip
428 The target_node must exist in in nodes_ip, and must be one of the
429 nodes in the logical ID for each of the DRBD devices encountered
434 for child in self.children:
435 child.SetPhysicalID(target_node, nodes_ip)
437 if self.logical_id is None and self.physical_id is not None:
439 if self.dev_type in constants.LDS_DRBD:
440 pnode, snode, port, pminor, sminor, secret = self.logical_id
441 if target_node not in (pnode, snode):
442 raise errors.ConfigurationError("DRBD device not knowing node %s" %
444 pnode_ip = nodes_ip.get(pnode, None)
445 snode_ip = nodes_ip.get(snode, None)
446 if pnode_ip is None or snode_ip is None:
447 raise errors.ConfigurationError("Can't find primary or secondary node"
448 " for %s" % str(self))
449 p_data = (pnode_ip, port)
450 s_data = (snode_ip, port)
451 if pnode == target_node:
452 self.physical_id = p_data + s_data + (pminor, secret)
453 else: # it must be secondary, we tested above
454 self.physical_id = s_data + p_data + (sminor, secret)
456 self.physical_id = self.logical_id
460 """Disk-specific conversion to standard python types.
462 This replaces the children lists of objects with lists of
463 standard python types.
466 bo = super(Disk, self).ToDict()
468 for attr in ("children",):
469 alist = bo.get(attr, None)
471 bo[attr] = self._ContainerToDicts(alist)
475 def FromDict(cls, val):
476 """Custom function for Disks
479 obj = super(Disk, cls).FromDict(val)
481 obj.children = cls._ContainerFromDicts(obj.children, list, Disk)
482 if obj.logical_id and isinstance(obj.logical_id, list):
483 obj.logical_id = tuple(obj.logical_id)
484 if obj.physical_id and isinstance(obj.physical_id, list):
485 obj.physical_id = tuple(obj.physical_id)
486 if obj.dev_type in constants.LDS_DRBD:
487 # we need a tuple of length six here
488 if len(obj.logical_id) < 6:
489 obj.logical_id += (None,) * (6 - len(obj.logical_id))
493 """Custom str() formatter for disks.
496 if self.dev_type == constants.LD_LV:
497 val = "<LogicalVolume(/dev/%s/%s" % self.logical_id
498 elif self.dev_type in constants.LDS_DRBD:
499 node_a, node_b, port, minor_a, minor_b = self.logical_id[:5]
501 if self.physical_id is None:
504 phy = ("configured as %s:%s %s:%s" %
505 (self.physical_id[0], self.physical_id[1],
506 self.physical_id[2], self.physical_id[3]))
508 val += ("hosts=%s/%d-%s/%d, port=%s, %s, " %
509 (node_a, minor_a, node_b, minor_b, port, phy))
510 if self.children and self.children.count(None) == 0:
511 val += "backend=%s, metadev=%s" % (self.children[0], self.children[1])
513 val += "no local storage"
515 val = ("<Disk(type=%s, logical_id=%s, physical_id=%s, children=%s" %
516 (self.dev_type, self.logical_id, self.physical_id, self.children))
517 if self.iv_name is None:
518 val += ", not visible"
520 val += ", visible as /dev/%s" % self.iv_name
521 if isinstance(self.size, int):
522 val += ", size=%dm)>" % self.size
524 val += ", size='%s')>" % (self.size,)
528 """Checks that this disk is correctly configured.
532 if self.mode not in constants.DISK_ACCESS_SET:
533 errors.append("Disk access mode '%s' is invalid" % (self.mode, ))
537 class Instance(TaggableObject):
538 """Config object representing an instance."""
539 __slots__ = TaggableObject.__slots__ + [
554 def _ComputeSecondaryNodes(self):
555 """Compute the list of secondary nodes.
557 This is a simple wrapper over _ComputeAllNodes.
560 all_nodes = set(self._ComputeAllNodes())
561 all_nodes.discard(self.primary_node)
562 return tuple(all_nodes)
564 secondary_nodes = property(_ComputeSecondaryNodes, None, None,
565 "List of secondary nodes")
567 def _ComputeAllNodes(self):
568 """Compute the list of all nodes.
570 Since the data is already there (in the drbd disks), keeping it as
571 a separate normal attribute is redundant and if not properly
572 synchronised can cause problems. Thus it's better to compute it
576 def _Helper(nodes, device):
577 """Recursively computes nodes given a top device."""
578 if device.dev_type in constants.LDS_DRBD:
579 nodea, nodeb = device.logical_id[:2]
583 for child in device.children:
584 _Helper(nodes, child)
587 all_nodes.add(self.primary_node)
588 for device in self.disks:
589 _Helper(all_nodes, device)
590 return tuple(all_nodes)
592 all_nodes = property(_ComputeAllNodes, None, None,
593 "List of all nodes of the instance")
595 def MapLVsByNode(self, lvmap=None, devs=None, node=None):
596 """Provide a mapping of nodes to LVs this instance owns.
598 This function figures out what logical volumes should belong on
599 which nodes, recursing through a device tree.
601 @param lvmap: optional dictionary to receive the
602 'node' : ['lv', ...] data.
604 @return: None if lvmap arg is given, otherwise, a dictionary
605 of the form { 'nodename' : ['volume1', 'volume2', ...], ... }
609 node = self.primary_node
612 lvmap = { node : [] }
615 if not node in lvmap:
623 if dev.dev_type == constants.LD_LV:
624 lvmap[node].append(dev.logical_id[1])
626 elif dev.dev_type in constants.LDS_DRBD:
628 self.MapLVsByNode(lvmap, dev.children, dev.logical_id[0])
629 self.MapLVsByNode(lvmap, dev.children, dev.logical_id[1])
632 self.MapLVsByNode(lvmap, dev.children, node)
636 def FindDisk(self, idx):
637 """Find a disk given having a specified index.
639 This is just a wrapper that does validation of the index.
642 @param idx: the disk index
644 @return: the corresponding disk
645 @raise errors.OpPrereqError: when the given index is not valid
650 return self.disks[idx]
651 except ValueError, err:
652 raise errors.OpPrereqError("Invalid disk index: '%s'" % str(err))
654 raise errors.OpPrereqError("Invalid disk index: %d (instace has disks"
655 " 0 to %d" % (idx, len(self.disks)))
658 """Instance-specific conversion to standard python types.
660 This replaces the children lists of objects with lists of standard
664 bo = super(Instance, self).ToDict()
666 for attr in "nics", "disks":
667 alist = bo.get(attr, None)
669 nlist = self._ContainerToDicts(alist)
676 def FromDict(cls, val):
677 """Custom function for instances.
680 obj = super(Instance, cls).FromDict(val)
681 obj.nics = cls._ContainerFromDicts(obj.nics, list, NIC)
682 obj.disks = cls._ContainerFromDicts(obj.disks, list, Disk)
686 class OS(ConfigObject):
687 """Config object representing an operating system."""
700 def FromInvalidOS(cls, err):
701 """Create an OS from an InvalidOS error.
703 This routine knows how to convert an InvalidOS error to an OS
704 object representing the broken OS with a meaningful error message.
707 if not isinstance(err, errors.InvalidOS):
708 raise errors.ProgrammerError("Trying to initialize an OS from an"
709 " invalid object of type %s" % type(err))
711 return cls(name=err.args[0], path=err.args[1], status=err.args[2])
713 def __nonzero__(self):
714 return self.status == constants.OS_VALID_STATUS
716 __bool__ = __nonzero__
719 class Node(TaggableObject):
720 """Config object representing a node."""
721 __slots__ = TaggableObject.__slots__ + [
732 class Cluster(TaggableObject):
733 """Config object representing the cluster."""
734 __slots__ = TaggableObject.__slots__ + [
742 "default_hypervisor",
748 "enabled_hypervisors",
751 "candidate_pool_size",
755 def UpgradeConfig(self):
756 """Fill defaults for missing configuration values.
759 if self.hvparams is None:
760 self.hvparams = constants.HVC_DEFAULTS
762 for hypervisor in self.hvparams:
763 self.hvparams[hypervisor] = FillDict(
764 constants.HVC_DEFAULTS[hypervisor], self.hvparams[hypervisor])
766 if self.beparams is None:
767 self.beparams = {constants.PP_DEFAULT: constants.BEC_DEFAULTS}
769 for begroup in self.beparams:
770 self.beparams[begroup] = FillDict(constants.BEC_DEFAULTS,
771 self.beparams[begroup])
773 if self.modify_etc_hosts is None:
774 self.modify_etc_hosts = True
777 """Custom function for cluster.
780 mydict = super(Cluster, self).ToDict()
781 mydict["tcpudp_port_pool"] = list(self.tcpudp_port_pool)
785 def FromDict(cls, val):
786 """Custom function for cluster.
789 obj = super(Cluster, cls).FromDict(val)
790 if not isinstance(obj.tcpudp_port_pool, set):
791 obj.tcpudp_port_pool = set(obj.tcpudp_port_pool)
794 def FillHV(self, instance):
795 """Fill an instance's hvparams dict.
797 @type instance: object
798 @param instance: the instance parameter to fill
800 @return: a copy of the instance's hvparams with missing keys filled from
804 return FillDict(self.hvparams.get(instance.hypervisor, {}),
807 def FillBE(self, instance):
808 """Fill an instance's beparams dict.
810 @type instance: object
811 @param instance: the instance parameter to fill
813 @return: a copy of the instance's beparams with missing keys filled from
817 return FillDict(self.beparams.get(constants.PP_DEFAULT, {}),
821 class SerializableConfigParser(ConfigParser.SafeConfigParser):
822 """Simple wrapper over ConfigParse that allows serialization.
824 This class is basically ConfigParser.SafeConfigParser with two
825 additional methods that allow it to serialize/unserialize to/from a
830 """Dump this instance and return the string representation."""
833 return buf.getvalue()
837 """Load data from a string."""
839 cfp = SerializableConfigParser()