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-msg=E0203,W0201
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__
40 from cStringIO import StringIO
42 from ganeti import errors
43 from ganeti import constants
45 from socket import AF_INET
48 __all__ = ["ConfigObject", "ConfigData", "NIC", "Disk", "Instance",
49 "OS", "Node", "NodeGroup", "Cluster", "FillDict"]
51 _TIMESTAMPS = ["ctime", "mtime"]
55 def FillDict(defaults_dict, custom_dict, skip_keys=None):
56 """Basic function to apply settings on top a default dict.
58 @type defaults_dict: dict
59 @param defaults_dict: dictionary holding the default values
60 @type custom_dict: dict
61 @param custom_dict: dictionary holding customized value
63 @param skip_keys: which keys not to fill
65 @return: dict with the 'full' values
68 ret_dict = copy.deepcopy(defaults_dict)
69 ret_dict.update(custom_dict)
79 def UpgradeGroupedParams(target, defaults):
80 """Update all groups for the target parameter.
82 @type target: dict of dicts
83 @param target: {group: {parameter: value}}
85 @param defaults: default parameter values
89 target = {constants.PP_DEFAULT: defaults}
92 target[group] = FillDict(defaults, target[group])
96 class ConfigObject(object):
97 """A generic config object.
99 It has the following properties:
101 - provides somewhat safe recursive unpickling and pickling for its classes
102 - unset attributes which are defined in slots are always returned
103 as None instead of raising an error
105 Classes derived from this must always declare __slots__ (we use many
106 config objects and the memory reduction is useful)
111 def __init__(self, **kwargs):
112 for k, v in kwargs.iteritems():
115 def __getattr__(self, name):
116 if name not in self._all_slots():
117 raise AttributeError("Invalid object attribute %s.%s" %
118 (type(self).__name__, name))
121 def __setstate__(self, state):
122 slots = self._all_slots()
125 setattr(self, name, state[name])
129 """Compute the list of all declared slots for a class.
133 for parent in cls.__mro__:
134 slots.extend(getattr(parent, "__slots__", []))
138 """Convert to a dict holding only standard python types.
140 The generic routine just dumps all of this object's attributes in
141 a dict. It does not work if the class has children who are
142 ConfigObjects themselves (e.g. the nics list in an Instance), in
143 which case the object should subclass the function in order to
144 make sure all objects returned are only standard python types.
148 for name in self._all_slots():
149 value = getattr(self, name, None)
150 if value is not None:
154 __getstate__ = ToDict
157 def FromDict(cls, val):
158 """Create an object from a dictionary.
160 This generic routine takes a dict, instantiates a new instance of
161 the given class, and sets attributes based on the dict content.
163 As for `ToDict`, this does not work if the class has children
164 who are ConfigObjects themselves (e.g. the nics list in an
165 Instance), in which case the object should subclass the function
166 and alter the objects.
169 if not isinstance(val, dict):
170 raise errors.ConfigurationError("Invalid object passed to FromDict:"
171 " expected dict, got %s" % type(val))
172 val_str = dict([(str(k), v) for k, v in val.iteritems()])
173 obj = cls(**val_str) # pylint: disable-msg=W0142
177 def _ContainerToDicts(container):
178 """Convert the elements of a container to standard python types.
180 This method converts a container with elements derived from
181 ConfigData to standard python types. If the container is a dict,
182 we don't touch the keys, only the values.
185 if isinstance(container, dict):
186 ret = dict([(k, v.ToDict()) for k, v in container.iteritems()])
187 elif isinstance(container, (list, tuple, set, frozenset)):
188 ret = [elem.ToDict() for elem in container]
190 raise TypeError("Invalid type %s passed to _ContainerToDicts" %
195 def _ContainerFromDicts(source, c_type, e_type):
196 """Convert a container from standard python types.
198 This method converts a container with standard python types to
199 ConfigData objects. If the container is a dict, we don't touch the
200 keys, only the values.
203 if not isinstance(c_type, type):
204 raise TypeError("Container type %s passed to _ContainerFromDicts is"
205 " not a type" % type(c_type))
209 ret = dict([(k, e_type.FromDict(v)) for k, v in source.iteritems()])
210 elif c_type in (list, tuple, set, frozenset):
211 ret = c_type([e_type.FromDict(elem) for elem in source])
213 raise TypeError("Invalid container type %s passed to"
214 " _ContainerFromDicts" % c_type)
218 """Makes a deep copy of the current object and its children.
221 dict_form = self.ToDict()
222 clone_obj = self.__class__.FromDict(dict_form)
226 """Implement __repr__ for ConfigObjects."""
227 return repr(self.ToDict())
229 def UpgradeConfig(self):
230 """Fill defaults for missing configuration values.
232 This method will be called at configuration load time, and its
233 implementation will be object dependent.
239 class TaggableObject(ConfigObject):
240 """An generic class supporting tags.
244 VALID_TAG_RE = re.compile("^[\w.+*/:@-]+$")
247 def ValidateTag(cls, tag):
248 """Check if a tag is valid.
250 If the tag is invalid, an errors.TagError will be raised. The
251 function has no return value.
254 if not isinstance(tag, basestring):
255 raise errors.TagError("Invalid tag type (not a string)")
256 if len(tag) > constants.MAX_TAG_LEN:
257 raise errors.TagError("Tag too long (>%d characters)" %
258 constants.MAX_TAG_LEN)
260 raise errors.TagError("Tags cannot be empty")
261 if not cls.VALID_TAG_RE.match(tag):
262 raise errors.TagError("Tag contains invalid characters")
265 """Return the tags list.
268 tags = getattr(self, "tags", None)
270 tags = self.tags = set()
273 def AddTag(self, tag):
277 self.ValidateTag(tag)
278 tags = self.GetTags()
279 if len(tags) >= constants.MAX_TAGS_PER_OBJ:
280 raise errors.TagError("Too many tags")
281 self.GetTags().add(tag)
283 def RemoveTag(self, tag):
287 self.ValidateTag(tag)
288 tags = self.GetTags()
292 raise errors.TagError("Tag not found")
295 """Taggable-object-specific conversion to standard python types.
297 This replaces the tags set with a list.
300 bo = super(TaggableObject, self).ToDict()
302 tags = bo.get("tags", None)
303 if isinstance(tags, set):
304 bo["tags"] = list(tags)
308 def FromDict(cls, val):
309 """Custom function for instances.
312 obj = super(TaggableObject, cls).FromDict(val)
313 if hasattr(obj, "tags") and isinstance(obj.tags, list):
314 obj.tags = set(obj.tags)
318 class ConfigData(ConfigObject):
319 """Top-level config object."""
330 """Custom function for top-level config data.
332 This just replaces the list of instances, nodes and the cluster
333 with standard python types.
336 mydict = super(ConfigData, self).ToDict()
337 mydict["cluster"] = mydict["cluster"].ToDict()
338 for key in "nodes", "instances", "nodegroups":
339 mydict[key] = self._ContainerToDicts(mydict[key])
344 def FromDict(cls, val):
345 """Custom function for top-level config data
348 obj = super(ConfigData, cls).FromDict(val)
349 obj.cluster = Cluster.FromDict(obj.cluster)
350 obj.nodes = cls._ContainerFromDicts(obj.nodes, dict, Node)
351 obj.instances = cls._ContainerFromDicts(obj.instances, dict, Instance)
352 obj.nodegroups = cls._ContainerFromDicts(obj.nodegroups, dict, NodeGroup)
355 def HasAnyDiskOfType(self, dev_type):
356 """Check if in there is at disk of the given type in the configuration.
358 @type dev_type: L{constants.LDS_BLOCK}
359 @param dev_type: the type to look for
361 @return: boolean indicating if a disk of the given type was found or not
364 for instance in self.instances.values():
365 for disk in instance.disks:
366 if disk.IsBasedOnDiskType(dev_type):
370 def UpgradeConfig(self):
371 """Fill defaults for missing configuration values.
374 self.cluster.UpgradeConfig()
375 for node in self.nodes.values():
377 for instance in self.instances.values():
378 instance.UpgradeConfig()
379 if self.nodegroups is None:
381 for nodegroup in self.nodegroups.values():
382 nodegroup.UpgradeConfig()
383 if self.cluster.drbd_usermode_helper is None:
384 # To decide if we set an helper let's check if at least one instance has
385 # a DRBD disk. This does not cover all the possible scenarios but it
386 # gives a good approximation.
387 if self.HasAnyDiskOfType(constants.LD_DRBD8):
388 self.cluster.drbd_usermode_helper = constants.DEFAULT_DRBD_HELPER
391 class NIC(ConfigObject):
392 """Config object representing a network card."""
393 __slots__ = ["mac", "ip", "nicparams"]
396 def CheckParameterSyntax(cls, nicparams):
397 """Check the given parameters for validity.
399 @type nicparams: dict
400 @param nicparams: dictionary with parameter names/value
401 @raise errors.ConfigurationError: when a parameter is not valid
404 if nicparams[constants.NIC_MODE] not in constants.NIC_VALID_MODES:
405 err = "Invalid nic mode: %s" % nicparams[constants.NIC_MODE]
406 raise errors.ConfigurationError(err)
408 if (nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED and
409 not nicparams[constants.NIC_LINK]):
410 err = "Missing bridged nic link"
411 raise errors.ConfigurationError(err)
414 class Disk(ConfigObject):
415 """Config object representing a block device."""
416 __slots__ = ["dev_type", "logical_id", "physical_id",
417 "children", "iv_name", "size", "mode"]
419 def CreateOnSecondary(self):
420 """Test if this device needs to be created on a secondary node."""
421 return self.dev_type in (constants.LD_DRBD8, constants.LD_LV)
423 def AssembleOnSecondary(self):
424 """Test if this device needs to be assembled on a secondary node."""
425 return self.dev_type in (constants.LD_DRBD8, constants.LD_LV)
427 def OpenOnSecondary(self):
428 """Test if this device needs to be opened on a secondary node."""
429 return self.dev_type in (constants.LD_LV,)
431 def StaticDevPath(self):
432 """Return the device path if this device type has a static one.
434 Some devices (LVM for example) live always at the same /dev/ path,
435 irrespective of their status. For such devices, we return this
436 path, for others we return None.
438 @warning: The path returned is not a normalized pathname; callers
439 should check that it is a valid path.
442 if self.dev_type == constants.LD_LV:
443 return "/dev/%s/%s" % (self.logical_id[0], self.logical_id[1])
446 def ChildrenNeeded(self):
447 """Compute the needed number of children for activation.
449 This method will return either -1 (all children) or a positive
450 number denoting the minimum number of children needed for
451 activation (only mirrored devices will usually return >=0).
453 Currently, only DRBD8 supports diskless activation (therefore we
454 return 0), for all other we keep the previous semantics and return
458 if self.dev_type == constants.LD_DRBD8:
462 def IsBasedOnDiskType(self, dev_type):
463 """Check if the disk or its children are based on the given type.
465 @type dev_type: L{constants.LDS_BLOCK}
466 @param dev_type: the type to look for
468 @return: boolean indicating if a device of the given type was found or not
472 for child in self.children:
473 if child.IsBasedOnDiskType(dev_type):
475 return self.dev_type == dev_type
477 def GetNodes(self, node):
478 """This function returns the nodes this device lives on.
480 Given the node on which the parent of the device lives on (or, in
481 case of a top-level device, the primary node of the devices'
482 instance), this function will return a list of nodes on which this
483 devices needs to (or can) be assembled.
486 if self.dev_type in [constants.LD_LV, constants.LD_FILE]:
488 elif self.dev_type in constants.LDS_DRBD:
489 result = [self.logical_id[0], self.logical_id[1]]
490 if node not in result:
491 raise errors.ConfigurationError("DRBD device passed unknown node")
493 raise errors.ProgrammerError("Unhandled device type %s" % self.dev_type)
496 def ComputeNodeTree(self, parent_node):
497 """Compute the node/disk tree for this disk and its children.
499 This method, given the node on which the parent disk lives, will
500 return the list of all (node, disk) pairs which describe the disk
501 tree in the most compact way. For example, a drbd/lvm stack
502 will be returned as (primary_node, drbd) and (secondary_node, drbd)
503 which represents all the top-level devices on the nodes.
506 my_nodes = self.GetNodes(parent_node)
507 result = [(node, self) for node in my_nodes]
508 if not self.children:
511 for node in my_nodes:
512 for child in self.children:
513 child_result = child.ComputeNodeTree(node)
514 if len(child_result) == 1:
515 # child (and all its descendants) is simple, doesn't split
516 # over multiple hosts, so we don't need to describe it, our
517 # own entry for this node describes it completely
520 # check if child nodes differ from my nodes; note that
521 # subdisk can differ from the child itself, and be instead
522 # one of its descendants
523 for subnode, subdisk in child_result:
524 if subnode not in my_nodes:
525 result.append((subnode, subdisk))
526 # otherwise child is under our own node, so we ignore this
527 # entry (but probably the other results in the list will
531 def ComputeGrowth(self, amount):
532 """Compute the per-VG growth requirements.
534 This only works for VG-based disks.
536 @type amount: integer
537 @param amount: the desired increase in (user-visible) disk space
539 @return: a dictionary of volume-groups and the required size
542 if self.dev_type == constants.LD_LV:
543 return {self.logical_id[0]: amount}
544 elif self.dev_type == constants.LD_DRBD8:
546 return self.children[0].ComputeGrowth(amount)
550 # Other disk types do not require VG space
553 def RecordGrow(self, amount):
554 """Update the size of this disk after growth.
556 This method recurses over the disks's children and updates their
557 size correspondigly. The method needs to be kept in sync with the
558 actual algorithms from bdev.
561 if self.dev_type == constants.LD_LV or self.dev_type == constants.LD_FILE:
563 elif self.dev_type == constants.LD_DRBD8:
565 self.children[0].RecordGrow(amount)
568 raise errors.ProgrammerError("Disk.RecordGrow called for unsupported"
569 " disk type %s" % self.dev_type)
572 """Sets recursively the size to zero for the disk and its children.
576 for child in self.children:
580 def SetPhysicalID(self, target_node, nodes_ip):
581 """Convert the logical ID to the physical ID.
583 This is used only for drbd, which needs ip/port configuration.
585 The routine descends down and updates its children also, because
586 this helps when the only the top device is passed to the remote
590 - target_node: the node we wish to configure for
591 - nodes_ip: a mapping of node name to ip
593 The target_node must exist in in nodes_ip, and must be one of the
594 nodes in the logical ID for each of the DRBD devices encountered
599 for child in self.children:
600 child.SetPhysicalID(target_node, nodes_ip)
602 if self.logical_id is None and self.physical_id is not None:
604 if self.dev_type in constants.LDS_DRBD:
605 pnode, snode, port, pminor, sminor, secret = self.logical_id
606 if target_node not in (pnode, snode):
607 raise errors.ConfigurationError("DRBD device not knowing node %s" %
609 pnode_ip = nodes_ip.get(pnode, None)
610 snode_ip = nodes_ip.get(snode, None)
611 if pnode_ip is None or snode_ip is None:
612 raise errors.ConfigurationError("Can't find primary or secondary node"
613 " for %s" % str(self))
614 p_data = (pnode_ip, port)
615 s_data = (snode_ip, port)
616 if pnode == target_node:
617 self.physical_id = p_data + s_data + (pminor, secret)
618 else: # it must be secondary, we tested above
619 self.physical_id = s_data + p_data + (sminor, secret)
621 self.physical_id = self.logical_id
625 """Disk-specific conversion to standard python types.
627 This replaces the children lists of objects with lists of
628 standard python types.
631 bo = super(Disk, self).ToDict()
633 for attr in ("children",):
634 alist = bo.get(attr, None)
636 bo[attr] = self._ContainerToDicts(alist)
640 def FromDict(cls, val):
641 """Custom function for Disks
644 obj = super(Disk, cls).FromDict(val)
646 obj.children = cls._ContainerFromDicts(obj.children, list, Disk)
647 if obj.logical_id and isinstance(obj.logical_id, list):
648 obj.logical_id = tuple(obj.logical_id)
649 if obj.physical_id and isinstance(obj.physical_id, list):
650 obj.physical_id = tuple(obj.physical_id)
651 if obj.dev_type in constants.LDS_DRBD:
652 # we need a tuple of length six here
653 if len(obj.logical_id) < 6:
654 obj.logical_id += (None,) * (6 - len(obj.logical_id))
658 """Custom str() formatter for disks.
661 if self.dev_type == constants.LD_LV:
662 val = "<LogicalVolume(/dev/%s/%s" % self.logical_id
663 elif self.dev_type in constants.LDS_DRBD:
664 node_a, node_b, port, minor_a, minor_b = self.logical_id[:5]
666 if self.physical_id is None:
669 phy = ("configured as %s:%s %s:%s" %
670 (self.physical_id[0], self.physical_id[1],
671 self.physical_id[2], self.physical_id[3]))
673 val += ("hosts=%s/%d-%s/%d, port=%s, %s, " %
674 (node_a, minor_a, node_b, minor_b, port, phy))
675 if self.children and self.children.count(None) == 0:
676 val += "backend=%s, metadev=%s" % (self.children[0], self.children[1])
678 val += "no local storage"
680 val = ("<Disk(type=%s, logical_id=%s, physical_id=%s, children=%s" %
681 (self.dev_type, self.logical_id, self.physical_id, self.children))
682 if self.iv_name is None:
683 val += ", not visible"
685 val += ", visible as /dev/%s" % self.iv_name
686 if isinstance(self.size, int):
687 val += ", size=%dm)>" % self.size
689 val += ", size='%s')>" % (self.size,)
693 """Checks that this disk is correctly configured.
697 if self.mode not in constants.DISK_ACCESS_SET:
698 all_errors.append("Disk access mode '%s' is invalid" % (self.mode, ))
701 def UpgradeConfig(self):
702 """Fill defaults for missing configuration values.
706 for child in self.children:
707 child.UpgradeConfig()
708 # add here config upgrade for this disk
711 class Instance(TaggableObject):
712 """Config object representing an instance."""
727 ] + _TIMESTAMPS + _UUID
729 def _ComputeSecondaryNodes(self):
730 """Compute the list of secondary nodes.
732 This is a simple wrapper over _ComputeAllNodes.
735 all_nodes = set(self._ComputeAllNodes())
736 all_nodes.discard(self.primary_node)
737 return tuple(all_nodes)
739 secondary_nodes = property(_ComputeSecondaryNodes, None, None,
740 "List of secondary nodes")
742 def _ComputeAllNodes(self):
743 """Compute the list of all nodes.
745 Since the data is already there (in the drbd disks), keeping it as
746 a separate normal attribute is redundant and if not properly
747 synchronised can cause problems. Thus it's better to compute it
751 def _Helper(nodes, device):
752 """Recursively computes nodes given a top device."""
753 if device.dev_type in constants.LDS_DRBD:
754 nodea, nodeb = device.logical_id[:2]
758 for child in device.children:
759 _Helper(nodes, child)
762 all_nodes.add(self.primary_node)
763 for device in self.disks:
764 _Helper(all_nodes, device)
765 return tuple(all_nodes)
767 all_nodes = property(_ComputeAllNodes, None, None,
768 "List of all nodes of the instance")
770 def MapLVsByNode(self, lvmap=None, devs=None, node=None):
771 """Provide a mapping of nodes to LVs this instance owns.
773 This function figures out what logical volumes should belong on
774 which nodes, recursing through a device tree.
776 @param lvmap: optional dictionary to receive the
777 'node' : ['lv', ...] data.
779 @return: None if lvmap arg is given, otherwise, a dictionary of
780 the form { 'nodename' : ['volume1', 'volume2', ...], ... };
781 volumeN is of the form "vg_name/lv_name", compatible with
786 node = self.primary_node
789 lvmap = { node : [] }
792 if not node in lvmap:
800 if dev.dev_type == constants.LD_LV:
801 lvmap[node].append(dev.logical_id[0]+"/"+dev.logical_id[1])
803 elif dev.dev_type in constants.LDS_DRBD:
805 self.MapLVsByNode(lvmap, dev.children, dev.logical_id[0])
806 self.MapLVsByNode(lvmap, dev.children, dev.logical_id[1])
809 self.MapLVsByNode(lvmap, dev.children, node)
813 def FindDisk(self, idx):
814 """Find a disk given having a specified index.
816 This is just a wrapper that does validation of the index.
819 @param idx: the disk index
821 @return: the corresponding disk
822 @raise errors.OpPrereqError: when the given index is not valid
827 return self.disks[idx]
828 except (TypeError, ValueError), err:
829 raise errors.OpPrereqError("Invalid disk index: '%s'" % str(err),
832 raise errors.OpPrereqError("Invalid disk index: %d (instace has disks"
833 " 0 to %d" % (idx, len(self.disks) - 1),
837 """Instance-specific conversion to standard python types.
839 This replaces the children lists of objects with lists of standard
843 bo = super(Instance, self).ToDict()
845 for attr in "nics", "disks":
846 alist = bo.get(attr, None)
848 nlist = self._ContainerToDicts(alist)
855 def FromDict(cls, val):
856 """Custom function for instances.
859 obj = super(Instance, cls).FromDict(val)
860 obj.nics = cls._ContainerFromDicts(obj.nics, list, NIC)
861 obj.disks = cls._ContainerFromDicts(obj.disks, list, Disk)
864 def UpgradeConfig(self):
865 """Fill defaults for missing configuration values.
868 for nic in self.nics:
870 for disk in self.disks:
873 for key in constants.HVC_GLOBALS:
875 del self.hvparams[key]
878 if self.osparams is None:
882 class OS(ConfigObject):
883 """Config object representing an operating system.
885 @type supported_parameters: list
886 @ivar supported_parameters: a list of tuples, name and description,
887 containing the supported parameters by this OS
889 @type VARIANT_DELIM: string
890 @cvar VARIANT_DELIM: the variant delimiter
902 "supported_variants",
903 "supported_parameters",
909 def SplitNameVariant(cls, name):
910 """Splits the name into the proper name and variant.
912 @param name: the OS (unprocessed) name
914 @return: a list of two elements; if the original name didn't
915 contain a variant, it's returned as an empty string
918 nv = name.split(cls.VARIANT_DELIM, 1)
924 def GetName(cls, name):
925 """Returns the proper name of the os (without the variant).
927 @param name: the OS (unprocessed) name
930 return cls.SplitNameVariant(name)[0]
933 def GetVariant(cls, name):
934 """Returns the variant the os (without the base name).
936 @param name: the OS (unprocessed) name
939 return cls.SplitNameVariant(name)[1]
942 class Node(TaggableObject):
943 """Config object representing a node."""
957 ] + _TIMESTAMPS + _UUID
959 def UpgradeConfig(self):
960 """Fill defaults for missing configuration values.
963 # pylint: disable-msg=E0203
964 # because these are "defined" via slots, not manually
965 if self.master_capable is None:
966 self.master_capable = True
968 if self.vm_capable is None:
969 self.vm_capable = True
971 if self.ndparams is None:
974 if self.powered is None:
978 class NodeGroup(ConfigObject):
979 """Config object representing a node group."""
986 ] + _TIMESTAMPS + _UUID
989 """Custom function for nodegroup.
991 This discards the members object, which gets recalculated and is only kept
995 mydict = super(NodeGroup, self).ToDict()
996 del mydict["members"]
1000 def FromDict(cls, val):
1001 """Custom function for nodegroup.
1003 The members slot is initialized to an empty list, upon deserialization.
1006 obj = super(NodeGroup, cls).FromDict(val)
1010 def UpgradeConfig(self):
1011 """Fill defaults for missing configuration values.
1014 if self.ndparams is None:
1017 if self.serial_no is None:
1020 if self.alloc_policy is None:
1021 self.alloc_policy = constants.ALLOC_POLICY_PREFERRED
1023 # We only update mtime, and not ctime, since we would not be able to provide
1024 # a correct value for creation time.
1025 if self.mtime is None:
1026 self.mtime = time.time()
1028 def FillND(self, node):
1029 """Return filled out ndparams for L{object.Node}
1031 @type node: L{objects.Node}
1032 @param node: A Node object to fill
1033 @return a copy of the node's ndparams with defaults filled
1036 return self.SimpleFillND(node.ndparams)
1038 def SimpleFillND(self, ndparams):
1039 """Fill a given ndparams dict with defaults.
1041 @type ndparams: dict
1042 @param ndparams: the dict to fill
1044 @return: a copy of the passed in ndparams with missing keys filled
1045 from the node group defaults
1048 return FillDict(self.ndparams, ndparams)
1051 class Cluster(TaggableObject):
1052 """Config object representing the cluster."""
1056 "highest_used_port",
1059 "volume_group_name",
1061 "drbd_usermode_helper",
1063 "default_hypervisor",
1069 "enabled_hypervisors",
1076 "candidate_pool_size",
1079 "maintain_node_health",
1081 "default_iallocator",
1084 "primary_ip_family",
1085 "prealloc_wipe_disks",
1086 ] + _TIMESTAMPS + _UUID
1088 def UpgradeConfig(self):
1089 """Fill defaults for missing configuration values.
1092 # pylint: disable-msg=E0203
1093 # because these are "defined" via slots, not manually
1094 if self.hvparams is None:
1095 self.hvparams = constants.HVC_DEFAULTS
1097 for hypervisor in self.hvparams:
1098 self.hvparams[hypervisor] = FillDict(
1099 constants.HVC_DEFAULTS[hypervisor], self.hvparams[hypervisor])
1101 if self.os_hvp is None:
1104 # osparams added before 2.2
1105 if self.osparams is None:
1108 if self.ndparams is None:
1109 self.ndparams = constants.NDC_DEFAULTS
1111 self.beparams = UpgradeGroupedParams(self.beparams,
1112 constants.BEC_DEFAULTS)
1113 migrate_default_bridge = not self.nicparams
1114 self.nicparams = UpgradeGroupedParams(self.nicparams,
1115 constants.NICC_DEFAULTS)
1116 if migrate_default_bridge:
1117 self.nicparams[constants.PP_DEFAULT][constants.NIC_LINK] = \
1120 if self.modify_etc_hosts is None:
1121 self.modify_etc_hosts = True
1123 if self.modify_ssh_setup is None:
1124 self.modify_ssh_setup = True
1126 # default_bridge is no longer used it 2.1. The slot is left there to
1127 # support auto-upgrading. It can be removed once we decide to deprecate
1128 # upgrading straight from 2.0.
1129 if self.default_bridge is not None:
1130 self.default_bridge = None
1132 # default_hypervisor is just the first enabled one in 2.1. This slot and
1133 # code can be removed once upgrading straight from 2.0 is deprecated.
1134 if self.default_hypervisor is not None:
1135 self.enabled_hypervisors = ([self.default_hypervisor] +
1136 [hvname for hvname in self.enabled_hypervisors
1137 if hvname != self.default_hypervisor])
1138 self.default_hypervisor = None
1140 # maintain_node_health added after 2.1.1
1141 if self.maintain_node_health is None:
1142 self.maintain_node_health = False
1144 if self.uid_pool is None:
1147 if self.default_iallocator is None:
1148 self.default_iallocator = ""
1150 # reserved_lvs added before 2.2
1151 if self.reserved_lvs is None:
1152 self.reserved_lvs = []
1154 # hidden and blacklisted operating systems added before 2.2.1
1155 if self.hidden_os is None:
1158 if self.blacklisted_os is None:
1159 self.blacklisted_os = []
1161 # primary_ip_family added before 2.3
1162 if self.primary_ip_family is None:
1163 self.primary_ip_family = AF_INET
1165 if self.prealloc_wipe_disks is None:
1166 self.prealloc_wipe_disks = False
1169 """Custom function for cluster.
1172 mydict = super(Cluster, self).ToDict()
1173 mydict["tcpudp_port_pool"] = list(self.tcpudp_port_pool)
1177 def FromDict(cls, val):
1178 """Custom function for cluster.
1181 obj = super(Cluster, cls).FromDict(val)
1182 if not isinstance(obj.tcpudp_port_pool, set):
1183 obj.tcpudp_port_pool = set(obj.tcpudp_port_pool)
1186 def GetHVDefaults(self, hypervisor, os_name=None, skip_keys=None):
1187 """Get the default hypervisor parameters for the cluster.
1189 @param hypervisor: the hypervisor name
1190 @param os_name: if specified, we'll also update the defaults for this OS
1191 @param skip_keys: if passed, list of keys not to use
1192 @return: the defaults dict
1195 if skip_keys is None:
1198 fill_stack = [self.hvparams.get(hypervisor, {})]
1199 if os_name is not None:
1200 os_hvp = self.os_hvp.get(os_name, {}).get(hypervisor, {})
1201 fill_stack.append(os_hvp)
1204 for o_dict in fill_stack:
1205 ret_dict = FillDict(ret_dict, o_dict, skip_keys=skip_keys)
1209 def SimpleFillHV(self, hv_name, os_name, hvparams, skip_globals=False):
1210 """Fill a given hvparams dict with cluster defaults.
1212 @type hv_name: string
1213 @param hv_name: the hypervisor to use
1214 @type os_name: string
1215 @param os_name: the OS to use for overriding the hypervisor defaults
1216 @type skip_globals: boolean
1217 @param skip_globals: if True, the global hypervisor parameters will
1220 @return: a copy of the given hvparams with missing keys filled from
1221 the cluster defaults
1225 skip_keys = constants.HVC_GLOBALS
1229 def_dict = self.GetHVDefaults(hv_name, os_name, skip_keys=skip_keys)
1230 return FillDict(def_dict, hvparams, skip_keys=skip_keys)
1232 def FillHV(self, instance, skip_globals=False):
1233 """Fill an instance's hvparams dict with cluster defaults.
1235 @type instance: L{objects.Instance}
1236 @param instance: the instance parameter to fill
1237 @type skip_globals: boolean
1238 @param skip_globals: if True, the global hypervisor parameters will
1241 @return: a copy of the instance's hvparams with missing keys filled from
1242 the cluster defaults
1245 return self.SimpleFillHV(instance.hypervisor, instance.os,
1246 instance.hvparams, skip_globals)
1248 def SimpleFillBE(self, beparams):
1249 """Fill a given beparams dict with cluster defaults.
1251 @type beparams: dict
1252 @param beparams: the dict to fill
1254 @return: a copy of the passed in beparams with missing keys filled
1255 from the cluster defaults
1258 return FillDict(self.beparams.get(constants.PP_DEFAULT, {}), beparams)
1260 def FillBE(self, instance):
1261 """Fill an instance's beparams dict with cluster defaults.
1263 @type instance: L{objects.Instance}
1264 @param instance: the instance parameter to fill
1266 @return: a copy of the instance's beparams with missing keys filled from
1267 the cluster defaults
1270 return self.SimpleFillBE(instance.beparams)
1272 def SimpleFillNIC(self, nicparams):
1273 """Fill a given nicparams dict with cluster defaults.
1275 @type nicparams: dict
1276 @param nicparams: the dict to fill
1278 @return: a copy of the passed in nicparams with missing keys filled
1279 from the cluster defaults
1282 return FillDict(self.nicparams.get(constants.PP_DEFAULT, {}), nicparams)
1284 def SimpleFillOS(self, os_name, os_params):
1285 """Fill an instance's osparams dict with cluster defaults.
1287 @type os_name: string
1288 @param os_name: the OS name to use
1289 @type os_params: dict
1290 @param os_params: the dict to fill with default values
1292 @return: a copy of the instance's osparams with missing keys filled from
1293 the cluster defaults
1296 name_only = os_name.split("+", 1)[0]
1298 result = self.osparams.get(name_only, {})
1300 result = FillDict(result, self.osparams.get(os_name, {}))
1302 return FillDict(result, os_params)
1304 def FillND(self, node, nodegroup):
1305 """Return filled out ndparams for L{objects.NodeGroup} and L{object.Node}
1307 @type node: L{objects.Node}
1308 @param node: A Node object to fill
1309 @type nodegroup: L{objects.NodeGroup}
1310 @param nodegroup: A Node object to fill
1311 @return a copy of the node's ndparams with defaults filled
1314 return self.SimpleFillND(nodegroup.FillND(node))
1316 def SimpleFillND(self, ndparams):
1317 """Fill a given ndparams dict with defaults.
1319 @type ndparams: dict
1320 @param ndparams: the dict to fill
1322 @return: a copy of the passed in ndparams with missing keys filled
1323 from the cluster defaults
1326 return FillDict(self.ndparams, ndparams)
1329 class BlockDevStatus(ConfigObject):
1330 """Config object representing the status of a block device."""
1342 class ImportExportStatus(ConfigObject):
1343 """Config object representing the status of an import or export."""
1349 "progress_throughput",
1357 class ImportExportOptions(ConfigObject):
1358 """Options for import/export daemon
1360 @ivar key_name: X509 key name (None for cluster certificate)
1361 @ivar ca_pem: Remote peer CA in PEM format (None for cluster certificate)
1362 @ivar compress: Compression method (one of L{constants.IEC_ALL})
1363 @ivar magic: Used to ensure the connection goes to the right disk
1364 @ivar ipv6: Whether to use IPv6
1376 class ConfdRequest(ConfigObject):
1377 """Object holding a confd request.
1379 @ivar protocol: confd protocol version
1380 @ivar type: confd query type
1381 @ivar query: query request
1382 @ivar rsalt: requested reply salt
1393 class ConfdReply(ConfigObject):
1394 """Object holding a confd reply.
1396 @ivar protocol: confd protocol version
1397 @ivar status: reply status code (ok, error)
1398 @ivar answer: confd query reply
1399 @ivar serial: configuration serial number
1410 class QueryFieldDefinition(ConfigObject):
1411 """Object holding a query field definition.
1413 @ivar name: Field name
1414 @ivar title: Human-readable title
1415 @ivar kind: Field type
1425 class _QueryResponseBase(ConfigObject):
1431 """Custom function for serializing.
1434 mydict = super(_QueryResponseBase, self).ToDict()
1435 mydict["fields"] = self._ContainerToDicts(mydict["fields"])
1439 def FromDict(cls, val):
1440 """Custom function for de-serializing.
1443 obj = super(_QueryResponseBase, cls).FromDict(val)
1444 obj.fields = cls._ContainerFromDicts(obj.fields, list, QueryFieldDefinition)
1448 class QueryRequest(ConfigObject):
1449 """Object holding a query request.
1459 class QueryResponse(_QueryResponseBase):
1460 """Object holding the response to a query.
1462 @ivar fields: List of L{QueryFieldDefinition} objects
1463 @ivar data: Requested data
1471 class QueryFieldsRequest(ConfigObject):
1472 """Object holding a request for querying available fields.
1481 class QueryFieldsResponse(_QueryResponseBase):
1482 """Object holding the response to a query for fields.
1484 @ivar fields: List of L{QueryFieldDefinition} objects
1491 class InstanceConsole(ConfigObject):
1492 """Object describing how to access the console of an instance.
1507 """Validates contents of this object.
1510 assert self.kind in constants.CONS_ALL, "Unknown console type"
1511 assert self.instance, "Missing instance name"
1512 assert self.message or self.kind in [constants.CONS_SSH, constants.CONS_VNC]
1513 assert self.host or self.kind == constants.CONS_MESSAGE
1514 assert self.port or self.kind in [constants.CONS_MESSAGE,
1516 assert self.user or self.kind in [constants.CONS_MESSAGE,
1518 assert self.command or self.kind in [constants.CONS_MESSAGE,
1520 assert self.display or self.kind in [constants.CONS_MESSAGE,
1525 class SerializableConfigParser(ConfigParser.SafeConfigParser):
1526 """Simple wrapper over ConfigParse that allows serialization.
1528 This class is basically ConfigParser.SafeConfigParser with two
1529 additional methods that allow it to serialize/unserialize to/from a
1534 """Dump this instance and return the string representation."""
1537 return buf.getvalue()
1540 def Loads(cls, data):
1541 """Load data from a string."""
1542 buf = StringIO(data)