4 # Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 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.
29 # pylint: disable=E0203,W0201,R0902
31 # E0203: Access to member %r before its definition, since we use
32 # objects.py which doesn't explicitely initialise its members
34 # W0201: Attribute '%s' defined outside __init__
36 # R0902: Allow instances of these objects to have more than 20 attributes
42 from cStringIO import StringIO
44 from ganeti import errors
45 from ganeti import constants
46 from ganeti import netutils
47 from ganeti import utils
49 from socket import AF_INET
52 __all__ = ["ConfigObject", "ConfigData", "NIC", "Disk", "Instance",
53 "OS", "Node", "NodeGroup", "Cluster", "FillDict"]
55 _TIMESTAMPS = ["ctime", "mtime"]
58 # constants used to create InstancePolicy dictionary
59 TISPECS_GROUP_TYPES = {
60 constants.ISPECS_MIN: constants.VTYPE_INT,
61 constants.ISPECS_MAX: constants.VTYPE_INT,
64 TISPECS_CLUSTER_TYPES = {
65 constants.ISPECS_MIN: constants.VTYPE_INT,
66 constants.ISPECS_MAX: constants.VTYPE_INT,
67 constants.ISPECS_STD: constants.VTYPE_INT,
71 def FillDict(defaults_dict, custom_dict, skip_keys=None):
72 """Basic function to apply settings on top a default dict.
74 @type defaults_dict: dict
75 @param defaults_dict: dictionary holding the default values
76 @type custom_dict: dict
77 @param custom_dict: dictionary holding customized value
79 @param skip_keys: which keys not to fill
81 @return: dict with the 'full' values
84 ret_dict = copy.deepcopy(defaults_dict)
85 ret_dict.update(custom_dict)
95 def FillDictOfDicts(defaults_dict, custom_dict, skip_keys=None):
96 """Run FillDict for each key in dictionary.
100 for key in defaults_dict.keys():
101 ret_dict[key] = FillDict(defaults_dict[key],
102 custom_dict.get(key, {}),
107 def UpgradeGroupedParams(target, defaults):
108 """Update all groups for the target parameter.
110 @type target: dict of dicts
111 @param target: {group: {parameter: value}}
113 @param defaults: default parameter values
117 target = {constants.PP_DEFAULT: defaults}
120 target[group] = FillDict(defaults, target[group])
124 def UpgradeBeParams(target):
125 """Update the be parameters dict to the new format.
128 @param target: "be" parameters dict
131 if constants.BE_MEMORY in target:
132 memory = target[constants.BE_MEMORY]
133 target[constants.BE_MAXMEM] = memory
134 target[constants.BE_MINMEM] = memory
135 del target[constants.BE_MEMORY]
138 def UpgradeDiskParams(diskparams):
139 """Upgrade the disk parameters.
141 @type diskparams: dict
142 @param diskparams: disk parameters to upgrade
144 @return: the upgraded disk parameters dit
148 if diskparams is None:
149 result = constants.DISK_DT_DEFAULTS.copy()
151 # Update the disk parameter values for each disk template.
152 # The code iterates over constants.DISK_TEMPLATES because new templates
153 # might have been added.
154 for template in constants.DISK_TEMPLATES:
155 if template not in diskparams:
156 result[template] = constants.DISK_DT_DEFAULTS[template].copy()
158 result[template] = FillDict(constants.DISK_DT_DEFAULTS[template],
159 diskparams[template])
164 def MakeEmptyIPolicy():
165 """Create empty IPolicy dictionary.
169 (constants.ISPECS_MIN, dict()),
170 (constants.ISPECS_MAX, dict()),
171 (constants.ISPECS_STD, dict()),
175 def CreateIPolicyFromOpts(ispecs_mem_size=None,
176 ispecs_cpu_count=None,
177 ispecs_disk_count=None,
178 ispecs_disk_size=None,
179 ispecs_nic_count=None,
181 allowed_values=None):
182 """Creation of instane policy based on command line options.
186 # prepare ipolicy dict
187 ipolicy_transposed = {
188 constants.ISPEC_MEM_SIZE: ispecs_mem_size,
189 constants.ISPEC_CPU_COUNT: ispecs_cpu_count,
190 constants.ISPEC_DISK_COUNT: ispecs_disk_count,
191 constants.ISPEC_DISK_SIZE: ispecs_disk_size,
192 constants.ISPEC_NIC_COUNT: ispecs_nic_count,
195 # first, check that the values given are correct
197 forced_type = TISPECS_GROUP_TYPES
199 forced_type = TISPECS_CLUSTER_TYPES
201 for specs in ipolicy_transposed.values():
202 utils.ForceDictType(specs, forced_type, allowed_values=allowed_values)
205 ipolicy_out = MakeEmptyIPolicy()
206 for name, specs in ipolicy_transposed.iteritems():
207 assert name in constants.ISPECS_PARAMETERS
208 for key, val in specs.items(): # {min: .. ,max: .., std: ..}
209 ipolicy_out[key][name] = val
214 class ConfigObject(object):
215 """A generic config object.
217 It has the following properties:
219 - provides somewhat safe recursive unpickling and pickling for its classes
220 - unset attributes which are defined in slots are always returned
221 as None instead of raising an error
223 Classes derived from this must always declare __slots__ (we use many
224 config objects and the memory reduction is useful)
229 def __init__(self, **kwargs):
230 for k, v in kwargs.iteritems():
233 def __getattr__(self, name):
234 if name not in self._all_slots():
235 raise AttributeError("Invalid object attribute %s.%s" %
236 (type(self).__name__, name))
239 def __setstate__(self, state):
240 slots = self._all_slots()
243 setattr(self, name, state[name])
247 """Compute the list of all declared slots for a class.
251 for parent in cls.__mro__:
252 slots.extend(getattr(parent, "__slots__", []))
256 """Convert to a dict holding only standard python types.
258 The generic routine just dumps all of this object's attributes in
259 a dict. It does not work if the class has children who are
260 ConfigObjects themselves (e.g. the nics list in an Instance), in
261 which case the object should subclass the function in order to
262 make sure all objects returned are only standard python types.
266 for name in self._all_slots():
267 value = getattr(self, name, None)
268 if value is not None:
272 __getstate__ = ToDict
275 def FromDict(cls, val):
276 """Create an object from a dictionary.
278 This generic routine takes a dict, instantiates a new instance of
279 the given class, and sets attributes based on the dict content.
281 As for `ToDict`, this does not work if the class has children
282 who are ConfigObjects themselves (e.g. the nics list in an
283 Instance), in which case the object should subclass the function
284 and alter the objects.
287 if not isinstance(val, dict):
288 raise errors.ConfigurationError("Invalid object passed to FromDict:"
289 " expected dict, got %s" % type(val))
290 val_str = dict([(str(k), v) for k, v in val.iteritems()])
291 obj = cls(**val_str) # pylint: disable=W0142
295 def _ContainerToDicts(container):
296 """Convert the elements of a container to standard python types.
298 This method converts a container with elements derived from
299 ConfigData to standard python types. If the container is a dict,
300 we don't touch the keys, only the values.
303 if isinstance(container, dict):
304 ret = dict([(k, v.ToDict()) for k, v in container.iteritems()])
305 elif isinstance(container, (list, tuple, set, frozenset)):
306 ret = [elem.ToDict() for elem in container]
308 raise TypeError("Invalid type %s passed to _ContainerToDicts" %
313 def _ContainerFromDicts(source, c_type, e_type):
314 """Convert a container from standard python types.
316 This method converts a container with standard python types to
317 ConfigData objects. If the container is a dict, we don't touch the
318 keys, only the values.
321 if not isinstance(c_type, type):
322 raise TypeError("Container type %s passed to _ContainerFromDicts is"
323 " not a type" % type(c_type))
327 ret = dict([(k, e_type.FromDict(v)) for k, v in source.iteritems()])
328 elif c_type in (list, tuple, set, frozenset):
329 ret = c_type([e_type.FromDict(elem) for elem in source])
331 raise TypeError("Invalid container type %s passed to"
332 " _ContainerFromDicts" % c_type)
336 """Makes a deep copy of the current object and its children.
339 dict_form = self.ToDict()
340 clone_obj = self.__class__.FromDict(dict_form)
344 """Implement __repr__ for ConfigObjects."""
345 return repr(self.ToDict())
347 def UpgradeConfig(self):
348 """Fill defaults for missing configuration values.
350 This method will be called at configuration load time, and its
351 implementation will be object dependent.
357 class TaggableObject(ConfigObject):
358 """An generic class supporting tags.
362 VALID_TAG_RE = re.compile("^[\w.+*/:@-]+$")
365 def ValidateTag(cls, tag):
366 """Check if a tag is valid.
368 If the tag is invalid, an errors.TagError will be raised. The
369 function has no return value.
372 if not isinstance(tag, basestring):
373 raise errors.TagError("Invalid tag type (not a string)")
374 if len(tag) > constants.MAX_TAG_LEN:
375 raise errors.TagError("Tag too long (>%d characters)" %
376 constants.MAX_TAG_LEN)
378 raise errors.TagError("Tags cannot be empty")
379 if not cls.VALID_TAG_RE.match(tag):
380 raise errors.TagError("Tag contains invalid characters")
383 """Return the tags list.
386 tags = getattr(self, "tags", None)
388 tags = self.tags = set()
391 def AddTag(self, tag):
395 self.ValidateTag(tag)
396 tags = self.GetTags()
397 if len(tags) >= constants.MAX_TAGS_PER_OBJ:
398 raise errors.TagError("Too many tags")
399 self.GetTags().add(tag)
401 def RemoveTag(self, tag):
405 self.ValidateTag(tag)
406 tags = self.GetTags()
410 raise errors.TagError("Tag not found")
413 """Taggable-object-specific conversion to standard python types.
415 This replaces the tags set with a list.
418 bo = super(TaggableObject, self).ToDict()
420 tags = bo.get("tags", None)
421 if isinstance(tags, set):
422 bo["tags"] = list(tags)
426 def FromDict(cls, val):
427 """Custom function for instances.
430 obj = super(TaggableObject, cls).FromDict(val)
431 if hasattr(obj, "tags") and isinstance(obj.tags, list):
432 obj.tags = set(obj.tags)
436 class MasterNetworkParameters(ConfigObject):
437 """Network configuration parameters for the master
439 @ivar name: master name
441 @ivar netmask: master netmask
442 @ivar netdev: master network device
443 @ivar ip_family: master IP family
455 class ConfigData(ConfigObject):
456 """Top-level config object."""
467 """Custom function for top-level config data.
469 This just replaces the list of instances, nodes and the cluster
470 with standard python types.
473 mydict = super(ConfigData, self).ToDict()
474 mydict["cluster"] = mydict["cluster"].ToDict()
475 for key in "nodes", "instances", "nodegroups":
476 mydict[key] = self._ContainerToDicts(mydict[key])
481 def FromDict(cls, val):
482 """Custom function for top-level config data
485 obj = super(ConfigData, cls).FromDict(val)
486 obj.cluster = Cluster.FromDict(obj.cluster)
487 obj.nodes = cls._ContainerFromDicts(obj.nodes, dict, Node)
488 obj.instances = cls._ContainerFromDicts(obj.instances, dict, Instance)
489 obj.nodegroups = cls._ContainerFromDicts(obj.nodegroups, dict, NodeGroup)
492 def HasAnyDiskOfType(self, dev_type):
493 """Check if in there is at disk of the given type in the configuration.
495 @type dev_type: L{constants.LDS_BLOCK}
496 @param dev_type: the type to look for
498 @return: boolean indicating if a disk of the given type was found or not
501 for instance in self.instances.values():
502 for disk in instance.disks:
503 if disk.IsBasedOnDiskType(dev_type):
507 def UpgradeConfig(self):
508 """Fill defaults for missing configuration values.
511 self.cluster.UpgradeConfig()
512 for node in self.nodes.values():
514 for instance in self.instances.values():
515 instance.UpgradeConfig()
516 if self.nodegroups is None:
518 for nodegroup in self.nodegroups.values():
519 nodegroup.UpgradeConfig()
520 if self.cluster.drbd_usermode_helper is None:
521 # To decide if we set an helper let's check if at least one instance has
522 # a DRBD disk. This does not cover all the possible scenarios but it
523 # gives a good approximation.
524 if self.HasAnyDiskOfType(constants.LD_DRBD8):
525 self.cluster.drbd_usermode_helper = constants.DEFAULT_DRBD_HELPER
528 class NIC(ConfigObject):
529 """Config object representing a network card."""
530 __slots__ = ["mac", "ip", "nicparams"]
533 def CheckParameterSyntax(cls, nicparams):
534 """Check the given parameters for validity.
536 @type nicparams: dict
537 @param nicparams: dictionary with parameter names/value
538 @raise errors.ConfigurationError: when a parameter is not valid
541 if (nicparams[constants.NIC_MODE] not in constants.NIC_VALID_MODES and
542 nicparams[constants.NIC_MODE] != constants.VALUE_AUTO):
543 err = "Invalid nic mode: %s" % nicparams[constants.NIC_MODE]
544 raise errors.ConfigurationError(err)
546 if (nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED and
547 not nicparams[constants.NIC_LINK]):
548 err = "Missing bridged nic link"
549 raise errors.ConfigurationError(err)
552 class Disk(ConfigObject):
553 """Config object representing a block device."""
554 __slots__ = ["dev_type", "logical_id", "physical_id",
555 "children", "iv_name", "size", "mode", "params"]
557 def CreateOnSecondary(self):
558 """Test if this device needs to be created on a secondary node."""
559 return self.dev_type in (constants.LD_DRBD8, constants.LD_LV)
561 def AssembleOnSecondary(self):
562 """Test if this device needs to be assembled on a secondary node."""
563 return self.dev_type in (constants.LD_DRBD8, constants.LD_LV)
565 def OpenOnSecondary(self):
566 """Test if this device needs to be opened on a secondary node."""
567 return self.dev_type in (constants.LD_LV,)
569 def StaticDevPath(self):
570 """Return the device path if this device type has a static one.
572 Some devices (LVM for example) live always at the same /dev/ path,
573 irrespective of their status. For such devices, we return this
574 path, for others we return None.
576 @warning: The path returned is not a normalized pathname; callers
577 should check that it is a valid path.
580 if self.dev_type == constants.LD_LV:
581 return "/dev/%s/%s" % (self.logical_id[0], self.logical_id[1])
582 elif self.dev_type == constants.LD_BLOCKDEV:
583 return self.logical_id[1]
586 def ChildrenNeeded(self):
587 """Compute the needed number of children for activation.
589 This method will return either -1 (all children) or a positive
590 number denoting the minimum number of children needed for
591 activation (only mirrored devices will usually return >=0).
593 Currently, only DRBD8 supports diskless activation (therefore we
594 return 0), for all other we keep the previous semantics and return
598 if self.dev_type == constants.LD_DRBD8:
602 def IsBasedOnDiskType(self, dev_type):
603 """Check if the disk or its children are based on the given type.
605 @type dev_type: L{constants.LDS_BLOCK}
606 @param dev_type: the type to look for
608 @return: boolean indicating if a device of the given type was found or not
612 for child in self.children:
613 if child.IsBasedOnDiskType(dev_type):
615 return self.dev_type == dev_type
617 def GetNodes(self, node):
618 """This function returns the nodes this device lives on.
620 Given the node on which the parent of the device lives on (or, in
621 case of a top-level device, the primary node of the devices'
622 instance), this function will return a list of nodes on which this
623 devices needs to (or can) be assembled.
626 if self.dev_type in [constants.LD_LV, constants.LD_FILE,
627 constants.LD_BLOCKDEV]:
629 elif self.dev_type in constants.LDS_DRBD:
630 result = [self.logical_id[0], self.logical_id[1]]
631 if node not in result:
632 raise errors.ConfigurationError("DRBD device passed unknown node")
634 raise errors.ProgrammerError("Unhandled device type %s" % self.dev_type)
637 def ComputeNodeTree(self, parent_node):
638 """Compute the node/disk tree for this disk and its children.
640 This method, given the node on which the parent disk lives, will
641 return the list of all (node, disk) pairs which describe the disk
642 tree in the most compact way. For example, a drbd/lvm stack
643 will be returned as (primary_node, drbd) and (secondary_node, drbd)
644 which represents all the top-level devices on the nodes.
647 my_nodes = self.GetNodes(parent_node)
648 result = [(node, self) for node in my_nodes]
649 if not self.children:
652 for node in my_nodes:
653 for child in self.children:
654 child_result = child.ComputeNodeTree(node)
655 if len(child_result) == 1:
656 # child (and all its descendants) is simple, doesn't split
657 # over multiple hosts, so we don't need to describe it, our
658 # own entry for this node describes it completely
661 # check if child nodes differ from my nodes; note that
662 # subdisk can differ from the child itself, and be instead
663 # one of its descendants
664 for subnode, subdisk in child_result:
665 if subnode not in my_nodes:
666 result.append((subnode, subdisk))
667 # otherwise child is under our own node, so we ignore this
668 # entry (but probably the other results in the list will
672 def ComputeGrowth(self, amount):
673 """Compute the per-VG growth requirements.
675 This only works for VG-based disks.
677 @type amount: integer
678 @param amount: the desired increase in (user-visible) disk space
680 @return: a dictionary of volume-groups and the required size
683 if self.dev_type == constants.LD_LV:
684 return {self.logical_id[0]: amount}
685 elif self.dev_type == constants.LD_DRBD8:
687 return self.children[0].ComputeGrowth(amount)
691 # Other disk types do not require VG space
694 def RecordGrow(self, amount):
695 """Update the size of this disk after growth.
697 This method recurses over the disks's children and updates their
698 size correspondigly. The method needs to be kept in sync with the
699 actual algorithms from bdev.
702 if self.dev_type in (constants.LD_LV, constants.LD_FILE):
704 elif self.dev_type == constants.LD_DRBD8:
706 self.children[0].RecordGrow(amount)
709 raise errors.ProgrammerError("Disk.RecordGrow called for unsupported"
710 " disk type %s" % self.dev_type)
713 """Sets recursively the size to zero for the disk and its children.
717 for child in self.children:
721 def SetPhysicalID(self, target_node, nodes_ip):
722 """Convert the logical ID to the physical ID.
724 This is used only for drbd, which needs ip/port configuration.
726 The routine descends down and updates its children also, because
727 this helps when the only the top device is passed to the remote
731 - target_node: the node we wish to configure for
732 - nodes_ip: a mapping of node name to ip
734 The target_node must exist in in nodes_ip, and must be one of the
735 nodes in the logical ID for each of the DRBD devices encountered
740 for child in self.children:
741 child.SetPhysicalID(target_node, nodes_ip)
743 if self.logical_id is None and self.physical_id is not None:
745 if self.dev_type in constants.LDS_DRBD:
746 pnode, snode, port, pminor, sminor, secret = self.logical_id
747 if target_node not in (pnode, snode):
748 raise errors.ConfigurationError("DRBD device not knowing node %s" %
750 pnode_ip = nodes_ip.get(pnode, None)
751 snode_ip = nodes_ip.get(snode, None)
752 if pnode_ip is None or snode_ip is None:
753 raise errors.ConfigurationError("Can't find primary or secondary node"
754 " for %s" % str(self))
755 p_data = (pnode_ip, port)
756 s_data = (snode_ip, port)
757 if pnode == target_node:
758 self.physical_id = p_data + s_data + (pminor, secret)
759 else: # it must be secondary, we tested above
760 self.physical_id = s_data + p_data + (sminor, secret)
762 self.physical_id = self.logical_id
766 """Disk-specific conversion to standard python types.
768 This replaces the children lists of objects with lists of
769 standard python types.
772 bo = super(Disk, self).ToDict()
774 for attr in ("children",):
775 alist = bo.get(attr, None)
777 bo[attr] = self._ContainerToDicts(alist)
781 def FromDict(cls, val):
782 """Custom function for Disks
785 obj = super(Disk, cls).FromDict(val)
787 obj.children = cls._ContainerFromDicts(obj.children, list, Disk)
788 if obj.logical_id and isinstance(obj.logical_id, list):
789 obj.logical_id = tuple(obj.logical_id)
790 if obj.physical_id and isinstance(obj.physical_id, list):
791 obj.physical_id = tuple(obj.physical_id)
792 if obj.dev_type in constants.LDS_DRBD:
793 # we need a tuple of length six here
794 if len(obj.logical_id) < 6:
795 obj.logical_id += (None,) * (6 - len(obj.logical_id))
799 """Custom str() formatter for disks.
802 if self.dev_type == constants.LD_LV:
803 val = "<LogicalVolume(/dev/%s/%s" % self.logical_id
804 elif self.dev_type in constants.LDS_DRBD:
805 node_a, node_b, port, minor_a, minor_b = self.logical_id[:5]
807 if self.physical_id is None:
810 phy = ("configured as %s:%s %s:%s" %
811 (self.physical_id[0], self.physical_id[1],
812 self.physical_id[2], self.physical_id[3]))
814 val += ("hosts=%s/%d-%s/%d, port=%s, %s, " %
815 (node_a, minor_a, node_b, minor_b, port, phy))
816 if self.children and self.children.count(None) == 0:
817 val += "backend=%s, metadev=%s" % (self.children[0], self.children[1])
819 val += "no local storage"
821 val = ("<Disk(type=%s, logical_id=%s, physical_id=%s, children=%s" %
822 (self.dev_type, self.logical_id, self.physical_id, self.children))
823 if self.iv_name is None:
824 val += ", not visible"
826 val += ", visible as /dev/%s" % self.iv_name
827 if isinstance(self.size, int):
828 val += ", size=%dm)>" % self.size
830 val += ", size='%s')>" % (self.size,)
834 """Checks that this disk is correctly configured.
838 if self.mode not in constants.DISK_ACCESS_SET:
839 all_errors.append("Disk access mode '%s' is invalid" % (self.mode, ))
842 def UpgradeConfig(self):
843 """Fill defaults for missing configuration values.
847 for child in self.children:
848 child.UpgradeConfig()
851 self.params = constants.DISK_LD_DEFAULTS[self.dev_type].copy()
853 self.params = FillDict(constants.DISK_LD_DEFAULTS[self.dev_type],
855 # add here config upgrade for this disk
858 class InstancePolicy(ConfigObject):
859 """Config object representing instance policy limits dictionary."""
860 __slots__ = ["min", "max", "std"]
863 def CheckParameterSyntax(cls, ipolicy):
864 """ Check the instance policy for validity.
867 for param in constants.ISPECS_PARAMETERS:
868 InstancePolicy.CheckISpecSyntax(ipolicy, param)
871 def CheckISpecSyntax(cls, ipolicy, name):
872 """Check the instance policy for validity on a given key.
874 We check if the instance policy makes sense for a given key, that is
875 if ipolicy[min][name] <= ipolicy[std][name] <= ipolicy[max][name].
878 @param ipolicy: dictionary with min, max, std specs
880 @param name: what are the limits for
881 @raise errors.ConfigureError: when specs for given name are not valid
884 min_v = ipolicy[constants.ISPECS_MIN].get(name, 0)
885 std_v = ipolicy[constants.ISPECS_STD].get(name, min_v)
886 max_v = ipolicy[constants.ISPECS_MAX].get(name, std_v)
887 err = ("Invalid specification of min/max/std values for %s: %s/%s/%s" %
889 ipolicy[constants.ISPECS_MIN].get(name, "-"),
890 ipolicy[constants.ISPECS_MAX].get(name, "-"),
891 ipolicy[constants.ISPECS_STD].get(name, "-")))
892 if min_v > std_v or std_v > max_v:
893 raise errors.ConfigurationError(err)
896 class Instance(TaggableObject):
897 """Config object representing an instance."""
912 ] + _TIMESTAMPS + _UUID
914 def _ComputeSecondaryNodes(self):
915 """Compute the list of secondary nodes.
917 This is a simple wrapper over _ComputeAllNodes.
920 all_nodes = set(self._ComputeAllNodes())
921 all_nodes.discard(self.primary_node)
922 return tuple(all_nodes)
924 secondary_nodes = property(_ComputeSecondaryNodes, None, None,
925 "List of secondary nodes")
927 def _ComputeAllNodes(self):
928 """Compute the list of all nodes.
930 Since the data is already there (in the drbd disks), keeping it as
931 a separate normal attribute is redundant and if not properly
932 synchronised can cause problems. Thus it's better to compute it
936 def _Helper(nodes, device):
937 """Recursively computes nodes given a top device."""
938 if device.dev_type in constants.LDS_DRBD:
939 nodea, nodeb = device.logical_id[:2]
943 for child in device.children:
944 _Helper(nodes, child)
947 all_nodes.add(self.primary_node)
948 for device in self.disks:
949 _Helper(all_nodes, device)
950 return tuple(all_nodes)
952 all_nodes = property(_ComputeAllNodes, None, None,
953 "List of all nodes of the instance")
955 def MapLVsByNode(self, lvmap=None, devs=None, node=None):
956 """Provide a mapping of nodes to LVs this instance owns.
958 This function figures out what logical volumes should belong on
959 which nodes, recursing through a device tree.
961 @param lvmap: optional dictionary to receive the
962 'node' : ['lv', ...] data.
964 @return: None if lvmap arg is given, otherwise, a dictionary of
965 the form { 'nodename' : ['volume1', 'volume2', ...], ... };
966 volumeN is of the form "vg_name/lv_name", compatible with
971 node = self.primary_node
979 if not node in lvmap:
987 if dev.dev_type == constants.LD_LV:
988 lvmap[node].append(dev.logical_id[0] + "/" + dev.logical_id[1])
990 elif dev.dev_type in constants.LDS_DRBD:
992 self.MapLVsByNode(lvmap, dev.children, dev.logical_id[0])
993 self.MapLVsByNode(lvmap, dev.children, dev.logical_id[1])
996 self.MapLVsByNode(lvmap, dev.children, node)
1000 def FindDisk(self, idx):
1001 """Find a disk given having a specified index.
1003 This is just a wrapper that does validation of the index.
1006 @param idx: the disk index
1008 @return: the corresponding disk
1009 @raise errors.OpPrereqError: when the given index is not valid
1014 return self.disks[idx]
1015 except (TypeError, ValueError), err:
1016 raise errors.OpPrereqError("Invalid disk index: '%s'" % str(err),
1019 raise errors.OpPrereqError("Invalid disk index: %d (instace has disks"
1020 " 0 to %d" % (idx, len(self.disks) - 1),
1024 """Instance-specific conversion to standard python types.
1026 This replaces the children lists of objects with lists of standard
1030 bo = super(Instance, self).ToDict()
1032 for attr in "nics", "disks":
1033 alist = bo.get(attr, None)
1035 nlist = self._ContainerToDicts(alist)
1042 def FromDict(cls, val):
1043 """Custom function for instances.
1046 if "admin_state" not in val:
1047 if val.get("admin_up", False):
1048 val["admin_state"] = constants.ADMINST_UP
1050 val["admin_state"] = constants.ADMINST_DOWN
1051 if "admin_up" in val:
1053 obj = super(Instance, cls).FromDict(val)
1054 obj.nics = cls._ContainerFromDicts(obj.nics, list, NIC)
1055 obj.disks = cls._ContainerFromDicts(obj.disks, list, Disk)
1058 def UpgradeConfig(self):
1059 """Fill defaults for missing configuration values.
1062 for nic in self.nics:
1064 for disk in self.disks:
1065 disk.UpgradeConfig()
1067 for key in constants.HVC_GLOBALS:
1069 del self.hvparams[key]
1072 if self.osparams is None:
1074 UpgradeBeParams(self.beparams)
1077 class OS(ConfigObject):
1078 """Config object representing an operating system.
1080 @type supported_parameters: list
1081 @ivar supported_parameters: a list of tuples, name and description,
1082 containing the supported parameters by this OS
1084 @type VARIANT_DELIM: string
1085 @cvar VARIANT_DELIM: the variant delimiter
1097 "supported_variants",
1098 "supported_parameters",
1104 def SplitNameVariant(cls, name):
1105 """Splits the name into the proper name and variant.
1107 @param name: the OS (unprocessed) name
1109 @return: a list of two elements; if the original name didn't
1110 contain a variant, it's returned as an empty string
1113 nv = name.split(cls.VARIANT_DELIM, 1)
1119 def GetName(cls, name):
1120 """Returns the proper name of the os (without the variant).
1122 @param name: the OS (unprocessed) name
1125 return cls.SplitNameVariant(name)[0]
1128 def GetVariant(cls, name):
1129 """Returns the variant the os (without the base name).
1131 @param name: the OS (unprocessed) name
1134 return cls.SplitNameVariant(name)[1]
1137 class NodeHvState(ConfigObject):
1138 """Hypvervisor state on a node.
1140 @ivar mem_total: Total amount of memory
1141 @ivar mem_node: Memory used by, or reserved for, the node itself (not always
1143 @ivar mem_hv: Memory used by hypervisor or lost due to instance allocation
1145 @ivar mem_inst: Memory used by instances living on node
1146 @ivar cpu_total: Total node CPU core count
1147 @ivar cpu_node: Number of CPU cores reserved for the node itself
1160 class NodeDiskState(ConfigObject):
1161 """Disk state on a node.
1171 class Node(TaggableObject):
1172 """Config object representing a node.
1174 @ivar hv_state: Hypervisor state (e.g. number of CPUs)
1175 @ivar hv_state_static: Hypervisor state overriden by user
1176 @ivar disk_state: Disk state (e.g. free space)
1177 @ivar disk_state_static: Disk state overriden by user
1196 "disk_state_static",
1197 ] + _TIMESTAMPS + _UUID
1199 def UpgradeConfig(self):
1200 """Fill defaults for missing configuration values.
1203 # pylint: disable=E0203
1204 # because these are "defined" via slots, not manually
1205 if self.master_capable is None:
1206 self.master_capable = True
1208 if self.vm_capable is None:
1209 self.vm_capable = True
1211 if self.ndparams is None:
1214 if self.powered is None:
1218 """Custom function for serializing.
1221 data = super(Node, self).ToDict()
1223 hv_state = data.get("hv_state", None)
1224 if hv_state is not None:
1225 data["hv_state"] = self._ContainerToDicts(hv_state)
1227 disk_state = data.get("disk_state", None)
1228 if disk_state is not None:
1229 data["disk_state"] = \
1230 dict((key, self._ContainerToDicts(value))
1231 for (key, value) in disk_state.items())
1236 def FromDict(cls, val):
1237 """Custom function for deserializing.
1240 obj = super(Node, cls).FromDict(val)
1242 if obj.hv_state is not None:
1243 obj.hv_state = cls._ContainerFromDicts(obj.hv_state, dict, NodeHvState)
1245 if obj.disk_state is not None:
1247 dict((key, cls._ContainerFromDicts(value, dict, NodeDiskState))
1248 for (key, value) in obj.disk_state.items())
1253 class NodeGroup(TaggableObject):
1254 """Config object representing a node group."""
1263 "disk_state_static",
1265 ] + _TIMESTAMPS + _UUID
1268 """Custom function for nodegroup.
1270 This discards the members object, which gets recalculated and is only kept
1274 mydict = super(NodeGroup, self).ToDict()
1275 del mydict["members"]
1279 def FromDict(cls, val):
1280 """Custom function for nodegroup.
1282 The members slot is initialized to an empty list, upon deserialization.
1285 obj = super(NodeGroup, cls).FromDict(val)
1289 def UpgradeConfig(self):
1290 """Fill defaults for missing configuration values.
1293 if self.ndparams is None:
1296 if self.serial_no is None:
1299 if self.alloc_policy is None:
1300 self.alloc_policy = constants.ALLOC_POLICY_PREFERRED
1302 # We only update mtime, and not ctime, since we would not be able to provide
1303 # a correct value for creation time.
1304 if self.mtime is None:
1305 self.mtime = time.time()
1307 self.diskparams = UpgradeDiskParams(self.diskparams)
1308 if self.ipolicy is None:
1309 self.ipolicy = MakeEmptyIPolicy()
1311 def FillND(self, node):
1312 """Return filled out ndparams for L{objects.Node}
1314 @type node: L{objects.Node}
1315 @param node: A Node object to fill
1316 @return a copy of the node's ndparams with defaults filled
1319 return self.SimpleFillND(node.ndparams)
1321 def SimpleFillND(self, ndparams):
1322 """Fill a given ndparams dict with defaults.
1324 @type ndparams: dict
1325 @param ndparams: the dict to fill
1327 @return: a copy of the passed in ndparams with missing keys filled
1328 from the node group defaults
1331 return FillDict(self.ndparams, ndparams)
1334 class Cluster(TaggableObject):
1335 """Config object representing the cluster."""
1339 "highest_used_port",
1342 "volume_group_name",
1344 "drbd_usermode_helper",
1346 "default_hypervisor",
1351 "use_external_mip_script",
1354 "shared_file_storage_dir",
1355 "enabled_hypervisors",
1364 "candidate_pool_size",
1367 "maintain_node_health",
1369 "default_iallocator",
1372 "primary_ip_family",
1373 "prealloc_wipe_disks",
1375 "disk_state_static",
1376 ] + _TIMESTAMPS + _UUID
1378 def UpgradeConfig(self):
1379 """Fill defaults for missing configuration values.
1382 # pylint: disable=E0203
1383 # because these are "defined" via slots, not manually
1384 if self.hvparams is None:
1385 self.hvparams = constants.HVC_DEFAULTS
1387 for hypervisor in self.hvparams:
1388 self.hvparams[hypervisor] = FillDict(
1389 constants.HVC_DEFAULTS[hypervisor], self.hvparams[hypervisor])
1391 if self.os_hvp is None:
1394 # osparams added before 2.2
1395 if self.osparams is None:
1398 if self.ndparams is None:
1399 self.ndparams = constants.NDC_DEFAULTS
1401 self.beparams = UpgradeGroupedParams(self.beparams,
1402 constants.BEC_DEFAULTS)
1403 for beparams_group in self.beparams:
1404 UpgradeBeParams(self.beparams[beparams_group])
1406 migrate_default_bridge = not self.nicparams
1407 self.nicparams = UpgradeGroupedParams(self.nicparams,
1408 constants.NICC_DEFAULTS)
1409 if migrate_default_bridge:
1410 self.nicparams[constants.PP_DEFAULT][constants.NIC_LINK] = \
1413 if self.modify_etc_hosts is None:
1414 self.modify_etc_hosts = True
1416 if self.modify_ssh_setup is None:
1417 self.modify_ssh_setup = True
1419 # default_bridge is no longer used in 2.1. The slot is left there to
1420 # support auto-upgrading. It can be removed once we decide to deprecate
1421 # upgrading straight from 2.0.
1422 if self.default_bridge is not None:
1423 self.default_bridge = None
1425 # default_hypervisor is just the first enabled one in 2.1. This slot and
1426 # code can be removed once upgrading straight from 2.0 is deprecated.
1427 if self.default_hypervisor is not None:
1428 self.enabled_hypervisors = ([self.default_hypervisor] +
1429 [hvname for hvname in self.enabled_hypervisors
1430 if hvname != self.default_hypervisor])
1431 self.default_hypervisor = None
1433 # maintain_node_health added after 2.1.1
1434 if self.maintain_node_health is None:
1435 self.maintain_node_health = False
1437 if self.uid_pool is None:
1440 if self.default_iallocator is None:
1441 self.default_iallocator = ""
1443 # reserved_lvs added before 2.2
1444 if self.reserved_lvs is None:
1445 self.reserved_lvs = []
1447 # hidden and blacklisted operating systems added before 2.2.1
1448 if self.hidden_os is None:
1451 if self.blacklisted_os is None:
1452 self.blacklisted_os = []
1454 # primary_ip_family added before 2.3
1455 if self.primary_ip_family is None:
1456 self.primary_ip_family = AF_INET
1458 if self.master_netmask is None:
1459 ipcls = netutils.IPAddress.GetClassFromIpFamily(self.primary_ip_family)
1460 self.master_netmask = ipcls.iplen
1462 if self.prealloc_wipe_disks is None:
1463 self.prealloc_wipe_disks = False
1465 # shared_file_storage_dir added before 2.5
1466 if self.shared_file_storage_dir is None:
1467 self.shared_file_storage_dir = ""
1469 if self.use_external_mip_script is None:
1470 self.use_external_mip_script = False
1472 self.diskparams = UpgradeDiskParams(self.diskparams)
1474 # instance policy added before 2.6
1475 if self.ipolicy is None:
1476 self.ipolicy = MakeEmptyIPolicy()
1479 def primary_hypervisor(self):
1480 """The first hypervisor is the primary.
1482 Useful, for example, for L{Node}'s hv/disk state.
1485 return self.enabled_hypervisors[0]
1488 """Custom function for cluster.
1491 mydict = super(Cluster, self).ToDict()
1492 mydict["tcpudp_port_pool"] = list(self.tcpudp_port_pool)
1496 def FromDict(cls, val):
1497 """Custom function for cluster.
1500 obj = super(Cluster, cls).FromDict(val)
1501 if not isinstance(obj.tcpudp_port_pool, set):
1502 obj.tcpudp_port_pool = set(obj.tcpudp_port_pool)
1505 def GetHVDefaults(self, hypervisor, os_name=None, skip_keys=None):
1506 """Get the default hypervisor parameters for the cluster.
1508 @param hypervisor: the hypervisor name
1509 @param os_name: if specified, we'll also update the defaults for this OS
1510 @param skip_keys: if passed, list of keys not to use
1511 @return: the defaults dict
1514 if skip_keys is None:
1517 fill_stack = [self.hvparams.get(hypervisor, {})]
1518 if os_name is not None:
1519 os_hvp = self.os_hvp.get(os_name, {}).get(hypervisor, {})
1520 fill_stack.append(os_hvp)
1523 for o_dict in fill_stack:
1524 ret_dict = FillDict(ret_dict, o_dict, skip_keys=skip_keys)
1528 def SimpleFillHV(self, hv_name, os_name, hvparams, skip_globals=False):
1529 """Fill a given hvparams dict with cluster defaults.
1531 @type hv_name: string
1532 @param hv_name: the hypervisor to use
1533 @type os_name: string
1534 @param os_name: the OS to use for overriding the hypervisor defaults
1535 @type skip_globals: boolean
1536 @param skip_globals: if True, the global hypervisor parameters will
1539 @return: a copy of the given hvparams with missing keys filled from
1540 the cluster defaults
1544 skip_keys = constants.HVC_GLOBALS
1548 def_dict = self.GetHVDefaults(hv_name, os_name, skip_keys=skip_keys)
1549 return FillDict(def_dict, hvparams, skip_keys=skip_keys)
1551 def FillHV(self, instance, skip_globals=False):
1552 """Fill an instance's hvparams dict with cluster defaults.
1554 @type instance: L{objects.Instance}
1555 @param instance: the instance parameter to fill
1556 @type skip_globals: boolean
1557 @param skip_globals: if True, the global hypervisor parameters will
1560 @return: a copy of the instance's hvparams with missing keys filled from
1561 the cluster defaults
1564 return self.SimpleFillHV(instance.hypervisor, instance.os,
1565 instance.hvparams, skip_globals)
1567 def SimpleFillBE(self, beparams):
1568 """Fill a given beparams dict with cluster defaults.
1570 @type beparams: dict
1571 @param beparams: the dict to fill
1573 @return: a copy of the passed in beparams with missing keys filled
1574 from the cluster defaults
1577 return FillDict(self.beparams.get(constants.PP_DEFAULT, {}), beparams)
1579 def FillBE(self, instance):
1580 """Fill an instance's beparams dict with cluster defaults.
1582 @type instance: L{objects.Instance}
1583 @param instance: the instance parameter to fill
1585 @return: a copy of the instance's beparams with missing keys filled from
1586 the cluster defaults
1589 return self.SimpleFillBE(instance.beparams)
1591 def SimpleFillNIC(self, nicparams):
1592 """Fill a given nicparams dict with cluster defaults.
1594 @type nicparams: dict
1595 @param nicparams: the dict to fill
1597 @return: a copy of the passed in nicparams with missing keys filled
1598 from the cluster defaults
1601 return FillDict(self.nicparams.get(constants.PP_DEFAULT, {}), nicparams)
1603 def SimpleFillOS(self, os_name, os_params):
1604 """Fill an instance's osparams dict with cluster defaults.
1606 @type os_name: string
1607 @param os_name: the OS name to use
1608 @type os_params: dict
1609 @param os_params: the dict to fill with default values
1611 @return: a copy of the instance's osparams with missing keys filled from
1612 the cluster defaults
1615 name_only = os_name.split("+", 1)[0]
1617 result = self.osparams.get(name_only, {})
1619 result = FillDict(result, self.osparams.get(os_name, {}))
1621 return FillDict(result, os_params)
1624 def SimpleFillHvState(hv_state):
1625 """Fill an hv_state sub dict with cluster defaults.
1628 return FillDict(constants.HVST_DEFAULTS, hv_state)
1631 def SimpleFillDiskState(disk_state):
1632 """Fill an disk_state sub dict with cluster defaults.
1635 return FillDict(constants.DS_DEFAULTS, disk_state)
1637 def FillND(self, node, nodegroup):
1638 """Return filled out ndparams for L{objects.NodeGroup} and L{objects.Node}
1640 @type node: L{objects.Node}
1641 @param node: A Node object to fill
1642 @type nodegroup: L{objects.NodeGroup}
1643 @param nodegroup: A Node object to fill
1644 @return a copy of the node's ndparams with defaults filled
1647 return self.SimpleFillND(nodegroup.FillND(node))
1649 def SimpleFillND(self, ndparams):
1650 """Fill a given ndparams dict with defaults.
1652 @type ndparams: dict
1653 @param ndparams: the dict to fill
1655 @return: a copy of the passed in ndparams with missing keys filled
1656 from the cluster defaults
1659 return FillDict(self.ndparams, ndparams)
1661 def SimpleFillIPolicy(self, ipolicy):
1662 """ Fill instance policy dict with defaults.
1665 @param ipolicy: the dict to fill
1667 @return: a copy of passed ipolicy with missing keys filled from
1668 the cluster defaults
1671 return FillDictOfDicts(self.ipolicy, ipolicy)
1674 class BlockDevStatus(ConfigObject):
1675 """Config object representing the status of a block device."""
1687 class ImportExportStatus(ConfigObject):
1688 """Config object representing the status of an import or export."""
1694 "progress_throughput",
1702 class ImportExportOptions(ConfigObject):
1703 """Options for import/export daemon
1705 @ivar key_name: X509 key name (None for cluster certificate)
1706 @ivar ca_pem: Remote peer CA in PEM format (None for cluster certificate)
1707 @ivar compress: Compression method (one of L{constants.IEC_ALL})
1708 @ivar magic: Used to ensure the connection goes to the right disk
1709 @ivar ipv6: Whether to use IPv6
1710 @ivar connect_timeout: Number of seconds for establishing connection
1723 class ConfdRequest(ConfigObject):
1724 """Object holding a confd request.
1726 @ivar protocol: confd protocol version
1727 @ivar type: confd query type
1728 @ivar query: query request
1729 @ivar rsalt: requested reply salt
1740 class ConfdReply(ConfigObject):
1741 """Object holding a confd reply.
1743 @ivar protocol: confd protocol version
1744 @ivar status: reply status code (ok, error)
1745 @ivar answer: confd query reply
1746 @ivar serial: configuration serial number
1757 class QueryFieldDefinition(ConfigObject):
1758 """Object holding a query field definition.
1760 @ivar name: Field name
1761 @ivar title: Human-readable title
1762 @ivar kind: Field type
1763 @ivar doc: Human-readable description
1774 class _QueryResponseBase(ConfigObject):
1780 """Custom function for serializing.
1783 mydict = super(_QueryResponseBase, self).ToDict()
1784 mydict["fields"] = self._ContainerToDicts(mydict["fields"])
1788 def FromDict(cls, val):
1789 """Custom function for de-serializing.
1792 obj = super(_QueryResponseBase, cls).FromDict(val)
1793 obj.fields = cls._ContainerFromDicts(obj.fields, list, QueryFieldDefinition)
1797 class QueryRequest(ConfigObject):
1798 """Object holding a query request.
1808 class QueryResponse(_QueryResponseBase):
1809 """Object holding the response to a query.
1811 @ivar fields: List of L{QueryFieldDefinition} objects
1812 @ivar data: Requested data
1820 class QueryFieldsRequest(ConfigObject):
1821 """Object holding a request for querying available fields.
1830 class QueryFieldsResponse(_QueryResponseBase):
1831 """Object holding the response to a query for fields.
1833 @ivar fields: List of L{QueryFieldDefinition} objects
1840 class MigrationStatus(ConfigObject):
1841 """Object holding the status of a migration.
1851 class InstanceConsole(ConfigObject):
1852 """Object describing how to access the console of an instance.
1867 """Validates contents of this object.
1870 assert self.kind in constants.CONS_ALL, "Unknown console type"
1871 assert self.instance, "Missing instance name"
1872 assert self.message or self.kind in [constants.CONS_SSH,
1873 constants.CONS_SPICE,
1875 assert self.host or self.kind == constants.CONS_MESSAGE
1876 assert self.port or self.kind in [constants.CONS_MESSAGE,
1878 assert self.user or self.kind in [constants.CONS_MESSAGE,
1879 constants.CONS_SPICE,
1881 assert self.command or self.kind in [constants.CONS_MESSAGE,
1882 constants.CONS_SPICE,
1884 assert self.display or self.kind in [constants.CONS_MESSAGE,
1885 constants.CONS_SPICE,
1890 class SerializableConfigParser(ConfigParser.SafeConfigParser):
1891 """Simple wrapper over ConfigParse that allows serialization.
1893 This class is basically ConfigParser.SafeConfigParser with two
1894 additional methods that allow it to serialize/unserialize to/from a
1899 """Dump this instance and return the string representation."""
1902 return buf.getvalue()
1905 def Loads(cls, data):
1906 """Load data from a string."""
1907 buf = StringIO(data)