4 # Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 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 explicitly initialise its members
34 # W0201: Attribute '%s' defined outside __init__
36 # R0902: Allow instances of these objects to have more than 20 attributes
43 from cStringIO import StringIO
45 from ganeti import errors
46 from ganeti import constants
47 from ganeti import netutils
48 from ganeti import outils
49 from ganeti import utils
51 from socket import AF_INET
54 __all__ = ["ConfigObject", "ConfigData", "NIC", "Disk", "Instance",
55 "OS", "Node", "NodeGroup", "Cluster", "FillDict", "Network"]
57 _TIMESTAMPS = ["ctime", "mtime"]
61 def FillDict(defaults_dict, custom_dict, skip_keys=None):
62 """Basic function to apply settings on top a default dict.
64 @type defaults_dict: dict
65 @param defaults_dict: dictionary holding the default values
66 @type custom_dict: dict
67 @param custom_dict: dictionary holding customized value
69 @param skip_keys: which keys not to fill
71 @return: dict with the 'full' values
74 ret_dict = copy.deepcopy(defaults_dict)
75 ret_dict.update(custom_dict)
85 def FillIPolicy(default_ipolicy, custom_ipolicy, skip_keys=None):
86 """Fills an instance policy with defaults.
89 assert frozenset(default_ipolicy.keys()) == constants.IPOLICY_ALL_KEYS
91 for key in constants.IPOLICY_ISPECS:
92 ret_dict[key] = FillDict(default_ipolicy[key],
93 custom_ipolicy.get(key, {}),
96 for key in [constants.IPOLICY_DTS]:
97 ret_dict[key] = list(custom_ipolicy.get(key, default_ipolicy[key]))
98 # other items which we know we can directly copy (immutables)
99 for key in constants.IPOLICY_PARAMETERS:
100 ret_dict[key] = custom_ipolicy.get(key, default_ipolicy[key])
105 def FillDiskParams(default_dparams, custom_dparams, skip_keys=None):
106 """Fills the disk parameter defaults.
108 @see: L{FillDict} for parameters and return value
111 assert frozenset(default_dparams.keys()) == constants.DISK_TEMPLATES
113 return dict((dt, FillDict(default_dparams[dt], custom_dparams.get(dt, {}),
114 skip_keys=skip_keys))
115 for dt in constants.DISK_TEMPLATES)
118 def UpgradeGroupedParams(target, defaults):
119 """Update all groups for the target parameter.
121 @type target: dict of dicts
122 @param target: {group: {parameter: value}}
124 @param defaults: default parameter values
128 target = {constants.PP_DEFAULT: defaults}
131 target[group] = FillDict(defaults, target[group])
135 def UpgradeBeParams(target):
136 """Update the be parameters dict to the new format.
139 @param target: "be" parameters dict
142 if constants.BE_MEMORY in target:
143 memory = target[constants.BE_MEMORY]
144 target[constants.BE_MAXMEM] = memory
145 target[constants.BE_MINMEM] = memory
146 del target[constants.BE_MEMORY]
149 def UpgradeDiskParams(diskparams):
150 """Upgrade the disk parameters.
152 @type diskparams: dict
153 @param diskparams: disk parameters to upgrade
155 @return: the upgraded disk parameters dict
161 result = FillDiskParams(constants.DISK_DT_DEFAULTS, diskparams)
166 def UpgradeNDParams(ndparams):
167 """Upgrade ndparams structure.
170 @param ndparams: disk parameters to upgrade
172 @return: the upgraded node parameters dict
178 if (constants.ND_OOB_PROGRAM in ndparams and
179 ndparams[constants.ND_OOB_PROGRAM] is None):
180 # will be reset by the line below
181 del ndparams[constants.ND_OOB_PROGRAM]
182 return FillDict(constants.NDC_DEFAULTS, ndparams)
185 def MakeEmptyIPolicy():
186 """Create empty IPolicy dictionary.
190 (constants.ISPECS_MIN, {}),
191 (constants.ISPECS_MAX, {}),
192 (constants.ISPECS_STD, {}),
196 class ConfigObject(outils.ValidatedSlots):
197 """A generic config object.
199 It has the following properties:
201 - provides somewhat safe recursive unpickling and pickling for its classes
202 - unset attributes which are defined in slots are always returned
203 as None instead of raising an error
205 Classes derived from this must always declare __slots__ (we use many
206 config objects and the memory reduction is useful)
211 def __getattr__(self, name):
212 if name not in self.GetAllSlots():
213 raise AttributeError("Invalid object attribute %s.%s" %
214 (type(self).__name__, name))
217 def __setstate__(self, state):
218 slots = self.GetAllSlots()
221 setattr(self, name, state[name])
224 """Validates the slots.
229 """Convert to a dict holding only standard python types.
231 The generic routine just dumps all of this object's attributes in
232 a dict. It does not work if the class has children who are
233 ConfigObjects themselves (e.g. the nics list in an Instance), in
234 which case the object should subclass the function in order to
235 make sure all objects returned are only standard python types.
239 for name in self.GetAllSlots():
240 value = getattr(self, name, None)
241 if value is not None:
245 __getstate__ = ToDict
248 def FromDict(cls, val):
249 """Create an object from a dictionary.
251 This generic routine takes a dict, instantiates a new instance of
252 the given class, and sets attributes based on the dict content.
254 As for `ToDict`, this does not work if the class has children
255 who are ConfigObjects themselves (e.g. the nics list in an
256 Instance), in which case the object should subclass the function
257 and alter the objects.
260 if not isinstance(val, dict):
261 raise errors.ConfigurationError("Invalid object passed to FromDict:"
262 " expected dict, got %s" % type(val))
263 val_str = dict([(str(k), v) for k, v in val.iteritems()])
264 obj = cls(**val_str) # pylint: disable=W0142
268 def _ContainerToDicts(container):
269 """Convert the elements of a container to standard python types.
271 This method converts a container with elements derived from
272 ConfigData to standard python types. If the container is a dict,
273 we don't touch the keys, only the values.
276 if isinstance(container, dict):
277 ret = dict([(k, v.ToDict()) for k, v in container.iteritems()])
278 elif isinstance(container, (list, tuple, set, frozenset)):
279 ret = [elem.ToDict() for elem in container]
281 raise TypeError("Invalid type %s passed to _ContainerToDicts" %
286 def _ContainerFromDicts(source, c_type, e_type):
287 """Convert a container from standard python types.
289 This method converts a container with standard python types to
290 ConfigData objects. If the container is a dict, we don't touch the
291 keys, only the values.
294 if not isinstance(c_type, type):
295 raise TypeError("Container type %s passed to _ContainerFromDicts is"
296 " not a type" % type(c_type))
300 ret = dict([(k, e_type.FromDict(v)) for k, v in source.iteritems()])
301 elif c_type in (list, tuple, set, frozenset):
302 ret = c_type([e_type.FromDict(elem) for elem in source])
304 raise TypeError("Invalid container type %s passed to"
305 " _ContainerFromDicts" % c_type)
309 """Makes a deep copy of the current object and its children.
312 dict_form = self.ToDict()
313 clone_obj = self.__class__.FromDict(dict_form)
317 """Implement __repr__ for ConfigObjects."""
318 return repr(self.ToDict())
320 def UpgradeConfig(self):
321 """Fill defaults for missing configuration values.
323 This method will be called at configuration load time, and its
324 implementation will be object dependent.
330 class TaggableObject(ConfigObject):
331 """An generic class supporting tags.
335 VALID_TAG_RE = re.compile("^[\w.+*/:@-]+$")
338 def ValidateTag(cls, tag):
339 """Check if a tag is valid.
341 If the tag is invalid, an errors.TagError will be raised. The
342 function has no return value.
345 if not isinstance(tag, basestring):
346 raise errors.TagError("Invalid tag type (not a string)")
347 if len(tag) > constants.MAX_TAG_LEN:
348 raise errors.TagError("Tag too long (>%d characters)" %
349 constants.MAX_TAG_LEN)
351 raise errors.TagError("Tags cannot be empty")
352 if not cls.VALID_TAG_RE.match(tag):
353 raise errors.TagError("Tag contains invalid characters")
356 """Return the tags list.
359 tags = getattr(self, "tags", None)
361 tags = self.tags = set()
364 def AddTag(self, tag):
368 self.ValidateTag(tag)
369 tags = self.GetTags()
370 if len(tags) >= constants.MAX_TAGS_PER_OBJ:
371 raise errors.TagError("Too many tags")
372 self.GetTags().add(tag)
374 def RemoveTag(self, tag):
378 self.ValidateTag(tag)
379 tags = self.GetTags()
383 raise errors.TagError("Tag not found")
386 """Taggable-object-specific conversion to standard python types.
388 This replaces the tags set with a list.
391 bo = super(TaggableObject, self).ToDict()
393 tags = bo.get("tags", None)
394 if isinstance(tags, set):
395 bo["tags"] = list(tags)
399 def FromDict(cls, val):
400 """Custom function for instances.
403 obj = super(TaggableObject, cls).FromDict(val)
404 if hasattr(obj, "tags") and isinstance(obj.tags, list):
405 obj.tags = set(obj.tags)
409 class MasterNetworkParameters(ConfigObject):
410 """Network configuration parameters for the master
412 @ivar name: master name
414 @ivar netmask: master netmask
415 @ivar netdev: master network device
416 @ivar ip_family: master IP family
428 class ConfigData(ConfigObject):
429 """Top-level config object."""
441 """Custom function for top-level config data.
443 This just replaces the list of instances, nodes and the cluster
444 with standard python types.
447 mydict = super(ConfigData, self).ToDict()
448 mydict["cluster"] = mydict["cluster"].ToDict()
449 for key in "nodes", "instances", "nodegroups", "networks":
450 mydict[key] = self._ContainerToDicts(mydict[key])
455 def FromDict(cls, val):
456 """Custom function for top-level config data
459 obj = super(ConfigData, cls).FromDict(val)
460 obj.cluster = Cluster.FromDict(obj.cluster)
461 obj.nodes = cls._ContainerFromDicts(obj.nodes, dict, Node)
462 obj.instances = cls._ContainerFromDicts(obj.instances, dict, Instance)
463 obj.nodegroups = cls._ContainerFromDicts(obj.nodegroups, dict, NodeGroup)
464 obj.networks = cls._ContainerFromDicts(obj.networks, dict, Network)
467 def HasAnyDiskOfType(self, dev_type):
468 """Check if in there is at disk of the given type in the configuration.
470 @type dev_type: L{constants.LDS_BLOCK}
471 @param dev_type: the type to look for
473 @return: boolean indicating if a disk of the given type was found or not
476 for instance in self.instances.values():
477 for disk in instance.disks:
478 if disk.IsBasedOnDiskType(dev_type):
482 def UpgradeConfig(self):
483 """Fill defaults for missing configuration values.
486 self.cluster.UpgradeConfig()
487 for node in self.nodes.values():
489 for instance in self.instances.values():
490 instance.UpgradeConfig()
491 if self.nodegroups is None:
493 for nodegroup in self.nodegroups.values():
494 nodegroup.UpgradeConfig()
495 if self.cluster.drbd_usermode_helper is None:
496 # To decide if we set an helper let's check if at least one instance has
497 # a DRBD disk. This does not cover all the possible scenarios but it
498 # gives a good approximation.
499 if self.HasAnyDiskOfType(constants.LD_DRBD8):
500 self.cluster.drbd_usermode_helper = constants.DEFAULT_DRBD_HELPER
501 if self.networks is None:
503 for network in self.networks.values():
504 network.UpgradeConfig()
507 class NIC(ConfigObject):
508 """Config object representing a network card."""
509 __slots__ = ["mac", "ip", "network", "nicparams", "netinfo"]
512 def CheckParameterSyntax(cls, nicparams):
513 """Check the given parameters for validity.
515 @type nicparams: dict
516 @param nicparams: dictionary with parameter names/value
517 @raise errors.ConfigurationError: when a parameter is not valid
520 mode = nicparams[constants.NIC_MODE]
521 if (mode not in constants.NIC_VALID_MODES and
522 mode != constants.VALUE_AUTO):
523 raise errors.ConfigurationError("Invalid NIC mode '%s'" % mode)
525 if (mode == constants.NIC_MODE_BRIDGED and
526 not nicparams[constants.NIC_LINK]):
527 raise errors.ConfigurationError("Missing bridged NIC link")
530 class Disk(ConfigObject):
531 """Config object representing a block device."""
532 __slots__ = ["dev_type", "logical_id", "physical_id",
533 "children", "iv_name", "size", "mode", "params"]
535 def CreateOnSecondary(self):
536 """Test if this device needs to be created on a secondary node."""
537 return self.dev_type in (constants.LD_DRBD8, constants.LD_LV)
539 def AssembleOnSecondary(self):
540 """Test if this device needs to be assembled on a secondary node."""
541 return self.dev_type in (constants.LD_DRBD8, constants.LD_LV)
543 def OpenOnSecondary(self):
544 """Test if this device needs to be opened on a secondary node."""
545 return self.dev_type in (constants.LD_LV,)
547 def StaticDevPath(self):
548 """Return the device path if this device type has a static one.
550 Some devices (LVM for example) live always at the same /dev/ path,
551 irrespective of their status. For such devices, we return this
552 path, for others we return None.
554 @warning: The path returned is not a normalized pathname; callers
555 should check that it is a valid path.
558 if self.dev_type == constants.LD_LV:
559 return "/dev/%s/%s" % (self.logical_id[0], self.logical_id[1])
560 elif self.dev_type == constants.LD_BLOCKDEV:
561 return self.logical_id[1]
562 elif self.dev_type == constants.LD_RBD:
563 return "/dev/%s/%s" % (self.logical_id[0], self.logical_id[1])
566 def ChildrenNeeded(self):
567 """Compute the needed number of children for activation.
569 This method will return either -1 (all children) or a positive
570 number denoting the minimum number of children needed for
571 activation (only mirrored devices will usually return >=0).
573 Currently, only DRBD8 supports diskless activation (therefore we
574 return 0), for all other we keep the previous semantics and return
578 if self.dev_type == constants.LD_DRBD8:
582 def IsBasedOnDiskType(self, dev_type):
583 """Check if the disk or its children are based on the given type.
585 @type dev_type: L{constants.LDS_BLOCK}
586 @param dev_type: the type to look for
588 @return: boolean indicating if a device of the given type was found or not
592 for child in self.children:
593 if child.IsBasedOnDiskType(dev_type):
595 return self.dev_type == dev_type
597 def GetNodes(self, node):
598 """This function returns the nodes this device lives on.
600 Given the node on which the parent of the device lives on (or, in
601 case of a top-level device, the primary node of the devices'
602 instance), this function will return a list of nodes on which this
603 devices needs to (or can) be assembled.
606 if self.dev_type in [constants.LD_LV, constants.LD_FILE,
607 constants.LD_BLOCKDEV, constants.LD_RBD,
610 elif self.dev_type in constants.LDS_DRBD:
611 result = [self.logical_id[0], self.logical_id[1]]
612 if node not in result:
613 raise errors.ConfigurationError("DRBD device passed unknown node")
615 raise errors.ProgrammerError("Unhandled device type %s" % self.dev_type)
618 def ComputeNodeTree(self, parent_node):
619 """Compute the node/disk tree for this disk and its children.
621 This method, given the node on which the parent disk lives, will
622 return the list of all (node, disk) pairs which describe the disk
623 tree in the most compact way. For example, a drbd/lvm stack
624 will be returned as (primary_node, drbd) and (secondary_node, drbd)
625 which represents all the top-level devices on the nodes.
628 my_nodes = self.GetNodes(parent_node)
629 result = [(node, self) for node in my_nodes]
630 if not self.children:
633 for node in my_nodes:
634 for child in self.children:
635 child_result = child.ComputeNodeTree(node)
636 if len(child_result) == 1:
637 # child (and all its descendants) is simple, doesn't split
638 # over multiple hosts, so we don't need to describe it, our
639 # own entry for this node describes it completely
642 # check if child nodes differ from my nodes; note that
643 # subdisk can differ from the child itself, and be instead
644 # one of its descendants
645 for subnode, subdisk in child_result:
646 if subnode not in my_nodes:
647 result.append((subnode, subdisk))
648 # otherwise child is under our own node, so we ignore this
649 # entry (but probably the other results in the list will
653 def ComputeGrowth(self, amount):
654 """Compute the per-VG growth requirements.
656 This only works for VG-based disks.
658 @type amount: integer
659 @param amount: the desired increase in (user-visible) disk space
661 @return: a dictionary of volume-groups and the required size
664 if self.dev_type == constants.LD_LV:
665 return {self.logical_id[0]: amount}
666 elif self.dev_type == constants.LD_DRBD8:
668 return self.children[0].ComputeGrowth(amount)
672 # Other disk types do not require VG space
675 def RecordGrow(self, amount):
676 """Update the size of this disk after growth.
678 This method recurses over the disks's children and updates their
679 size correspondigly. The method needs to be kept in sync with the
680 actual algorithms from bdev.
683 if self.dev_type in (constants.LD_LV, constants.LD_FILE,
684 constants.LD_RBD, constants.LD_EXT):
686 elif self.dev_type == constants.LD_DRBD8:
688 self.children[0].RecordGrow(amount)
691 raise errors.ProgrammerError("Disk.RecordGrow called for unsupported"
692 " disk type %s" % self.dev_type)
694 def Update(self, size=None, mode=None):
695 """Apply changes to size and mode.
698 if self.dev_type == constants.LD_DRBD8:
700 self.children[0].Update(size=size, mode=mode)
702 assert not self.children
710 """Sets recursively the size to zero for the disk and its children.
714 for child in self.children:
718 def SetPhysicalID(self, target_node, nodes_ip):
719 """Convert the logical ID to the physical ID.
721 This is used only for drbd, which needs ip/port configuration.
723 The routine descends down and updates its children also, because
724 this helps when the only the top device is passed to the remote
728 - target_node: the node we wish to configure for
729 - nodes_ip: a mapping of node name to ip
731 The target_node must exist in in nodes_ip, and must be one of the
732 nodes in the logical ID for each of the DRBD devices encountered
737 for child in self.children:
738 child.SetPhysicalID(target_node, nodes_ip)
740 if self.logical_id is None and self.physical_id is not None:
742 if self.dev_type in constants.LDS_DRBD:
743 pnode, snode, port, pminor, sminor, secret = self.logical_id
744 if target_node not in (pnode, snode):
745 raise errors.ConfigurationError("DRBD device not knowing node %s" %
747 pnode_ip = nodes_ip.get(pnode, None)
748 snode_ip = nodes_ip.get(snode, None)
749 if pnode_ip is None or snode_ip is None:
750 raise errors.ConfigurationError("Can't find primary or secondary node"
751 " for %s" % str(self))
752 p_data = (pnode_ip, port)
753 s_data = (snode_ip, port)
754 if pnode == target_node:
755 self.physical_id = p_data + s_data + (pminor, secret)
756 else: # it must be secondary, we tested above
757 self.physical_id = s_data + p_data + (sminor, secret)
759 self.physical_id = self.logical_id
763 """Disk-specific conversion to standard python types.
765 This replaces the children lists of objects with lists of
766 standard python types.
769 bo = super(Disk, self).ToDict()
771 for attr in ("children",):
772 alist = bo.get(attr, None)
774 bo[attr] = self._ContainerToDicts(alist)
778 def FromDict(cls, val):
779 """Custom function for Disks
782 obj = super(Disk, cls).FromDict(val)
784 obj.children = cls._ContainerFromDicts(obj.children, list, Disk)
785 if obj.logical_id and isinstance(obj.logical_id, list):
786 obj.logical_id = tuple(obj.logical_id)
787 if obj.physical_id and isinstance(obj.physical_id, list):
788 obj.physical_id = tuple(obj.physical_id)
789 if obj.dev_type in constants.LDS_DRBD:
790 # we need a tuple of length six here
791 if len(obj.logical_id) < 6:
792 obj.logical_id += (None,) * (6 - len(obj.logical_id))
796 """Custom str() formatter for disks.
799 if self.dev_type == constants.LD_LV:
800 val = "<LogicalVolume(/dev/%s/%s" % self.logical_id
801 elif self.dev_type in constants.LDS_DRBD:
802 node_a, node_b, port, minor_a, minor_b = self.logical_id[:5]
804 if self.physical_id is None:
807 phy = ("configured as %s:%s %s:%s" %
808 (self.physical_id[0], self.physical_id[1],
809 self.physical_id[2], self.physical_id[3]))
811 val += ("hosts=%s/%d-%s/%d, port=%s, %s, " %
812 (node_a, minor_a, node_b, minor_b, port, phy))
813 if self.children and self.children.count(None) == 0:
814 val += "backend=%s, metadev=%s" % (self.children[0], self.children[1])
816 val += "no local storage"
818 val = ("<Disk(type=%s, logical_id=%s, physical_id=%s, children=%s" %
819 (self.dev_type, self.logical_id, self.physical_id, self.children))
820 if self.iv_name is None:
821 val += ", not visible"
823 val += ", visible as /dev/%s" % self.iv_name
824 if isinstance(self.size, int):
825 val += ", size=%dm)>" % self.size
827 val += ", size='%s')>" % (self.size,)
831 """Checks that this disk is correctly configured.
835 if self.mode not in constants.DISK_ACCESS_SET:
836 all_errors.append("Disk access mode '%s' is invalid" % (self.mode, ))
839 def UpgradeConfig(self):
840 """Fill defaults for missing configuration values.
844 for child in self.children:
845 child.UpgradeConfig()
847 # FIXME: Make this configurable in Ganeti 2.7
849 # add here config upgrade for this disk
852 def ComputeLDParams(disk_template, disk_params):
853 """Computes Logical Disk parameters from Disk Template parameters.
855 @type disk_template: string
856 @param disk_template: disk template, one of L{constants.DISK_TEMPLATES}
857 @type disk_params: dict
858 @param disk_params: disk template parameters;
859 dict(template_name -> parameters
861 @return: a list of dicts, one for each node of the disk hierarchy. Each dict
862 contains the LD parameters of the node. The tree is flattened in-order.
865 if disk_template not in constants.DISK_TEMPLATES:
866 raise errors.ProgrammerError("Unknown disk template %s" % disk_template)
868 assert disk_template in disk_params
871 dt_params = disk_params[disk_template]
872 if disk_template == constants.DT_DRBD8:
873 result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.LD_DRBD8], {
874 constants.LDP_RESYNC_RATE: dt_params[constants.DRBD_RESYNC_RATE],
875 constants.LDP_BARRIERS: dt_params[constants.DRBD_DISK_BARRIERS],
876 constants.LDP_NO_META_FLUSH: dt_params[constants.DRBD_META_BARRIERS],
877 constants.LDP_DEFAULT_METAVG: dt_params[constants.DRBD_DEFAULT_METAVG],
878 constants.LDP_DISK_CUSTOM: dt_params[constants.DRBD_DISK_CUSTOM],
879 constants.LDP_NET_CUSTOM: dt_params[constants.DRBD_NET_CUSTOM],
880 constants.LDP_DYNAMIC_RESYNC: dt_params[constants.DRBD_DYNAMIC_RESYNC],
881 constants.LDP_PLAN_AHEAD: dt_params[constants.DRBD_PLAN_AHEAD],
882 constants.LDP_FILL_TARGET: dt_params[constants.DRBD_FILL_TARGET],
883 constants.LDP_DELAY_TARGET: dt_params[constants.DRBD_DELAY_TARGET],
884 constants.LDP_MAX_RATE: dt_params[constants.DRBD_MAX_RATE],
885 constants.LDP_MIN_RATE: dt_params[constants.DRBD_MIN_RATE],
889 result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.LD_LV], {
890 constants.LDP_STRIPES: dt_params[constants.DRBD_DATA_STRIPES],
894 result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.LD_LV], {
895 constants.LDP_STRIPES: dt_params[constants.DRBD_META_STRIPES],
898 elif disk_template in (constants.DT_FILE, constants.DT_SHARED_FILE):
899 result.append(constants.DISK_LD_DEFAULTS[constants.LD_FILE])
901 elif disk_template == constants.DT_PLAIN:
902 result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.LD_LV], {
903 constants.LDP_STRIPES: dt_params[constants.LV_STRIPES],
906 elif disk_template == constants.DT_BLOCK:
907 result.append(constants.DISK_LD_DEFAULTS[constants.LD_BLOCKDEV])
909 elif disk_template == constants.DT_RBD:
910 result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.LD_RBD], {
911 constants.LDP_POOL: dt_params[constants.RBD_POOL],
914 elif disk_template == constants.DT_EXT:
915 result.append(constants.DISK_LD_DEFAULTS[constants.LD_EXT])
920 class InstancePolicy(ConfigObject):
921 """Config object representing instance policy limits dictionary.
924 Note that this object is not actually used in the config, it's just
925 used as a placeholder for a few functions.
929 def CheckParameterSyntax(cls, ipolicy, check_std):
930 """ Check the instance policy for validity.
933 for param in constants.ISPECS_PARAMETERS:
934 InstancePolicy.CheckISpecSyntax(ipolicy, param, check_std)
935 if constants.IPOLICY_DTS in ipolicy:
936 InstancePolicy.CheckDiskTemplates(ipolicy[constants.IPOLICY_DTS])
937 for key in constants.IPOLICY_PARAMETERS:
939 InstancePolicy.CheckParameter(key, ipolicy[key])
940 wrong_keys = frozenset(ipolicy.keys()) - constants.IPOLICY_ALL_KEYS
942 raise errors.ConfigurationError("Invalid keys in ipolicy: %s" %
943 utils.CommaJoin(wrong_keys))
946 def CheckISpecSyntax(cls, ipolicy, name, check_std):
947 """Check the instance policy for validity on a given key.
949 We check if the instance policy makes sense for a given key, that is
950 if ipolicy[min][name] <= ipolicy[std][name] <= ipolicy[max][name].
953 @param ipolicy: dictionary with min, max, std specs
955 @param name: what are the limits for
956 @type check_std: bool
957 @param check_std: Whether to check std value or just assume compliance
958 @raise errors.ConfigureError: when specs for given name are not valid
961 min_v = ipolicy[constants.ISPECS_MIN].get(name, 0)
964 std_v = ipolicy[constants.ISPECS_STD].get(name, min_v)
970 max_v = ipolicy[constants.ISPECS_MAX].get(name, std_v)
971 err = ("Invalid specification of min/max/std values for %s: %s/%s/%s" %
973 ipolicy[constants.ISPECS_MIN].get(name, "-"),
974 ipolicy[constants.ISPECS_MAX].get(name, "-"),
976 if min_v > std_v or std_v > max_v:
977 raise errors.ConfigurationError(err)
980 def CheckDiskTemplates(cls, disk_templates):
981 """Checks the disk templates for validity.
984 if not disk_templates:
985 raise errors.ConfigurationError("Instance policy must contain" +
986 " at least one disk template")
987 wrong = frozenset(disk_templates).difference(constants.DISK_TEMPLATES)
989 raise errors.ConfigurationError("Invalid disk template(s) %s" %
990 utils.CommaJoin(wrong))
993 def CheckParameter(cls, key, value):
994 """Checks a parameter.
996 Currently we expect all parameters to be float values.
1001 except (TypeError, ValueError), err:
1002 raise errors.ConfigurationError("Invalid value for key" " '%s':"
1003 " '%s', error: %s" % (key, value, err))
1006 class Instance(TaggableObject):
1007 """Config object representing an instance."""
1022 ] + _TIMESTAMPS + _UUID
1024 def _ComputeSecondaryNodes(self):
1025 """Compute the list of secondary nodes.
1027 This is a simple wrapper over _ComputeAllNodes.
1030 all_nodes = set(self._ComputeAllNodes())
1031 all_nodes.discard(self.primary_node)
1032 return tuple(all_nodes)
1034 secondary_nodes = property(_ComputeSecondaryNodes, None, None,
1035 "List of names of secondary nodes")
1037 def _ComputeAllNodes(self):
1038 """Compute the list of all nodes.
1040 Since the data is already there (in the drbd disks), keeping it as
1041 a separate normal attribute is redundant and if not properly
1042 synchronised can cause problems. Thus it's better to compute it
1046 def _Helper(nodes, device):
1047 """Recursively computes nodes given a top device."""
1048 if device.dev_type in constants.LDS_DRBD:
1049 nodea, nodeb = device.logical_id[:2]
1053 for child in device.children:
1054 _Helper(nodes, child)
1057 all_nodes.add(self.primary_node)
1058 for device in self.disks:
1059 _Helper(all_nodes, device)
1060 return tuple(all_nodes)
1062 all_nodes = property(_ComputeAllNodes, None, None,
1063 "List of names of all the nodes of the instance")
1065 def MapLVsByNode(self, lvmap=None, devs=None, node=None):
1066 """Provide a mapping of nodes to LVs this instance owns.
1068 This function figures out what logical volumes should belong on
1069 which nodes, recursing through a device tree.
1071 @param lvmap: optional dictionary to receive the
1072 'node' : ['lv', ...] data.
1074 @return: None if lvmap arg is given, otherwise, a dictionary of
1075 the form { 'nodename' : ['volume1', 'volume2', ...], ... };
1076 volumeN is of the form "vg_name/lv_name", compatible with
1081 node = self.primary_node
1089 if not node in lvmap:
1097 if dev.dev_type == constants.LD_LV:
1098 lvmap[node].append(dev.logical_id[0] + "/" + dev.logical_id[1])
1100 elif dev.dev_type in constants.LDS_DRBD:
1102 self.MapLVsByNode(lvmap, dev.children, dev.logical_id[0])
1103 self.MapLVsByNode(lvmap, dev.children, dev.logical_id[1])
1106 self.MapLVsByNode(lvmap, dev.children, node)
1110 def FindDisk(self, idx):
1111 """Find a disk given having a specified index.
1113 This is just a wrapper that does validation of the index.
1116 @param idx: the disk index
1118 @return: the corresponding disk
1119 @raise errors.OpPrereqError: when the given index is not valid
1124 return self.disks[idx]
1125 except (TypeError, ValueError), err:
1126 raise errors.OpPrereqError("Invalid disk index: '%s'" % str(err),
1129 raise errors.OpPrereqError("Invalid disk index: %d (instace has disks"
1130 " 0 to %d" % (idx, len(self.disks) - 1),
1134 """Instance-specific conversion to standard python types.
1136 This replaces the children lists of objects with lists of standard
1140 bo = super(Instance, self).ToDict()
1142 for attr in "nics", "disks":
1143 alist = bo.get(attr, None)
1145 nlist = self._ContainerToDicts(alist)
1152 def FromDict(cls, val):
1153 """Custom function for instances.
1156 if "admin_state" not in val:
1157 if val.get("admin_up", False):
1158 val["admin_state"] = constants.ADMINST_UP
1160 val["admin_state"] = constants.ADMINST_DOWN
1161 if "admin_up" in val:
1163 obj = super(Instance, cls).FromDict(val)
1164 obj.nics = cls._ContainerFromDicts(obj.nics, list, NIC)
1165 obj.disks = cls._ContainerFromDicts(obj.disks, list, Disk)
1168 def UpgradeConfig(self):
1169 """Fill defaults for missing configuration values.
1172 for nic in self.nics:
1174 for disk in self.disks:
1175 disk.UpgradeConfig()
1177 for key in constants.HVC_GLOBALS:
1179 del self.hvparams[key]
1182 if self.osparams is None:
1184 UpgradeBeParams(self.beparams)
1187 class OS(ConfigObject):
1188 """Config object representing an operating system.
1190 @type supported_parameters: list
1191 @ivar supported_parameters: a list of tuples, name and description,
1192 containing the supported parameters by this OS
1194 @type VARIANT_DELIM: string
1195 @cvar VARIANT_DELIM: the variant delimiter
1207 "supported_variants",
1208 "supported_parameters",
1214 def SplitNameVariant(cls, name):
1215 """Splits the name into the proper name and variant.
1217 @param name: the OS (unprocessed) name
1219 @return: a list of two elements; if the original name didn't
1220 contain a variant, it's returned as an empty string
1223 nv = name.split(cls.VARIANT_DELIM, 1)
1229 def GetName(cls, name):
1230 """Returns the proper name of the os (without the variant).
1232 @param name: the OS (unprocessed) name
1235 return cls.SplitNameVariant(name)[0]
1238 def GetVariant(cls, name):
1239 """Returns the variant the os (without the base name).
1241 @param name: the OS (unprocessed) name
1244 return cls.SplitNameVariant(name)[1]
1247 class ExtStorage(ConfigObject):
1248 """Config object representing an External Storage Provider.
1261 "supported_parameters",
1265 class NodeHvState(ConfigObject):
1266 """Hypvervisor state on a node.
1268 @ivar mem_total: Total amount of memory
1269 @ivar mem_node: Memory used by, or reserved for, the node itself (not always
1271 @ivar mem_hv: Memory used by hypervisor or lost due to instance allocation
1273 @ivar mem_inst: Memory used by instances living on node
1274 @ivar cpu_total: Total node CPU core count
1275 @ivar cpu_node: Number of CPU cores reserved for the node itself
1288 class NodeDiskState(ConfigObject):
1289 """Disk state on a node.
1299 class Node(TaggableObject):
1300 """Config object representing a node.
1302 @ivar hv_state: Hypervisor state (e.g. number of CPUs)
1303 @ivar hv_state_static: Hypervisor state overriden by user
1304 @ivar disk_state: Disk state (e.g. free space)
1305 @ivar disk_state_static: Disk state overriden by user
1324 "disk_state_static",
1325 ] + _TIMESTAMPS + _UUID
1327 def UpgradeConfig(self):
1328 """Fill defaults for missing configuration values.
1331 # pylint: disable=E0203
1332 # because these are "defined" via slots, not manually
1333 if self.master_capable is None:
1334 self.master_capable = True
1336 if self.vm_capable is None:
1337 self.vm_capable = True
1339 if self.ndparams is None:
1341 # And remove any global parameter
1342 for key in constants.NDC_GLOBALS:
1343 if key in self.ndparams:
1344 logging.warning("Ignoring %s node parameter for node %s",
1346 del self.ndparams[key]
1348 if self.powered is None:
1352 """Custom function for serializing.
1355 data = super(Node, self).ToDict()
1357 hv_state = data.get("hv_state", None)
1358 if hv_state is not None:
1359 data["hv_state"] = self._ContainerToDicts(hv_state)
1361 disk_state = data.get("disk_state", None)
1362 if disk_state is not None:
1363 data["disk_state"] = \
1364 dict((key, self._ContainerToDicts(value))
1365 for (key, value) in disk_state.items())
1370 def FromDict(cls, val):
1371 """Custom function for deserializing.
1374 obj = super(Node, cls).FromDict(val)
1376 if obj.hv_state is not None:
1377 obj.hv_state = cls._ContainerFromDicts(obj.hv_state, dict, NodeHvState)
1379 if obj.disk_state is not None:
1381 dict((key, cls._ContainerFromDicts(value, dict, NodeDiskState))
1382 for (key, value) in obj.disk_state.items())
1387 class NodeGroup(TaggableObject):
1388 """Config object representing a node group."""
1397 "disk_state_static",
1400 ] + _TIMESTAMPS + _UUID
1403 """Custom function for nodegroup.
1405 This discards the members object, which gets recalculated and is only kept
1409 mydict = super(NodeGroup, self).ToDict()
1410 del mydict["members"]
1414 def FromDict(cls, val):
1415 """Custom function for nodegroup.
1417 The members slot is initialized to an empty list, upon deserialization.
1420 obj = super(NodeGroup, cls).FromDict(val)
1424 def UpgradeConfig(self):
1425 """Fill defaults for missing configuration values.
1428 if self.ndparams is None:
1431 if self.serial_no is None:
1434 if self.alloc_policy is None:
1435 self.alloc_policy = constants.ALLOC_POLICY_PREFERRED
1437 # We only update mtime, and not ctime, since we would not be able
1438 # to provide a correct value for creation time.
1439 if self.mtime is None:
1440 self.mtime = time.time()
1442 if self.diskparams is None:
1443 self.diskparams = {}
1444 if self.ipolicy is None:
1445 self.ipolicy = MakeEmptyIPolicy()
1447 if self.networks is None:
1450 def FillND(self, node):
1451 """Return filled out ndparams for L{objects.Node}
1453 @type node: L{objects.Node}
1454 @param node: A Node object to fill
1455 @return a copy of the node's ndparams with defaults filled
1458 return self.SimpleFillND(node.ndparams)
1460 def SimpleFillND(self, ndparams):
1461 """Fill a given ndparams dict with defaults.
1463 @type ndparams: dict
1464 @param ndparams: the dict to fill
1466 @return: a copy of the passed in ndparams with missing keys filled
1467 from the node group defaults
1470 return FillDict(self.ndparams, ndparams)
1473 class Cluster(TaggableObject):
1474 """Config object representing the cluster."""
1478 "highest_used_port",
1481 "volume_group_name",
1483 "drbd_usermode_helper",
1485 "default_hypervisor",
1490 "use_external_mip_script",
1493 "shared_file_storage_dir",
1494 "enabled_hypervisors",
1503 "candidate_pool_size",
1506 "maintain_node_health",
1508 "default_iallocator",
1511 "primary_ip_family",
1512 "prealloc_wipe_disks",
1514 "disk_state_static",
1515 ] + _TIMESTAMPS + _UUID
1517 def UpgradeConfig(self):
1518 """Fill defaults for missing configuration values.
1521 # pylint: disable=E0203
1522 # because these are "defined" via slots, not manually
1523 if self.hvparams is None:
1524 self.hvparams = constants.HVC_DEFAULTS
1526 for hypervisor in self.hvparams:
1527 self.hvparams[hypervisor] = FillDict(
1528 constants.HVC_DEFAULTS[hypervisor], self.hvparams[hypervisor])
1530 if self.os_hvp is None:
1533 # osparams added before 2.2
1534 if self.osparams is None:
1537 self.ndparams = UpgradeNDParams(self.ndparams)
1539 self.beparams = UpgradeGroupedParams(self.beparams,
1540 constants.BEC_DEFAULTS)
1541 for beparams_group in self.beparams:
1542 UpgradeBeParams(self.beparams[beparams_group])
1544 migrate_default_bridge = not self.nicparams
1545 self.nicparams = UpgradeGroupedParams(self.nicparams,
1546 constants.NICC_DEFAULTS)
1547 if migrate_default_bridge:
1548 self.nicparams[constants.PP_DEFAULT][constants.NIC_LINK] = \
1551 if self.modify_etc_hosts is None:
1552 self.modify_etc_hosts = True
1554 if self.modify_ssh_setup is None:
1555 self.modify_ssh_setup = True
1557 # default_bridge is no longer used in 2.1. The slot is left there to
1558 # support auto-upgrading. It can be removed once we decide to deprecate
1559 # upgrading straight from 2.0.
1560 if self.default_bridge is not None:
1561 self.default_bridge = None
1563 # default_hypervisor is just the first enabled one in 2.1. This slot and
1564 # code can be removed once upgrading straight from 2.0 is deprecated.
1565 if self.default_hypervisor is not None:
1566 self.enabled_hypervisors = ([self.default_hypervisor] +
1567 [hvname for hvname in self.enabled_hypervisors
1568 if hvname != self.default_hypervisor])
1569 self.default_hypervisor = None
1571 # maintain_node_health added after 2.1.1
1572 if self.maintain_node_health is None:
1573 self.maintain_node_health = False
1575 if self.uid_pool is None:
1578 if self.default_iallocator is None:
1579 self.default_iallocator = ""
1581 # reserved_lvs added before 2.2
1582 if self.reserved_lvs is None:
1583 self.reserved_lvs = []
1585 # hidden and blacklisted operating systems added before 2.2.1
1586 if self.hidden_os is None:
1589 if self.blacklisted_os is None:
1590 self.blacklisted_os = []
1592 # primary_ip_family added before 2.3
1593 if self.primary_ip_family is None:
1594 self.primary_ip_family = AF_INET
1596 if self.master_netmask is None:
1597 ipcls = netutils.IPAddress.GetClassFromIpFamily(self.primary_ip_family)
1598 self.master_netmask = ipcls.iplen
1600 if self.prealloc_wipe_disks is None:
1601 self.prealloc_wipe_disks = False
1603 # shared_file_storage_dir added before 2.5
1604 if self.shared_file_storage_dir is None:
1605 self.shared_file_storage_dir = ""
1607 if self.use_external_mip_script is None:
1608 self.use_external_mip_script = False
1611 self.diskparams = UpgradeDiskParams(self.diskparams)
1613 self.diskparams = constants.DISK_DT_DEFAULTS.copy()
1615 # instance policy added before 2.6
1616 if self.ipolicy is None:
1617 self.ipolicy = FillIPolicy(constants.IPOLICY_DEFAULTS, {})
1619 # we can either make sure to upgrade the ipolicy always, or only
1620 # do it in some corner cases (e.g. missing keys); note that this
1621 # will break any removal of keys from the ipolicy dict
1622 wrongkeys = frozenset(self.ipolicy.keys()) - constants.IPOLICY_ALL_KEYS
1624 # These keys would be silently removed by FillIPolicy()
1625 msg = ("Cluster instance policy contains spourious keys: %s" %
1626 utils.CommaJoin(wrongkeys))
1627 raise errors.ConfigurationError(msg)
1628 self.ipolicy = FillIPolicy(constants.IPOLICY_DEFAULTS, self.ipolicy)
1631 def primary_hypervisor(self):
1632 """The first hypervisor is the primary.
1634 Useful, for example, for L{Node}'s hv/disk state.
1637 return self.enabled_hypervisors[0]
1640 """Custom function for cluster.
1643 mydict = super(Cluster, self).ToDict()
1644 mydict["tcpudp_port_pool"] = list(self.tcpudp_port_pool)
1648 def FromDict(cls, val):
1649 """Custom function for cluster.
1652 obj = super(Cluster, cls).FromDict(val)
1653 if not isinstance(obj.tcpudp_port_pool, set):
1654 obj.tcpudp_port_pool = set(obj.tcpudp_port_pool)
1657 def SimpleFillDP(self, diskparams):
1658 """Fill a given diskparams dict with cluster defaults.
1660 @param diskparams: The diskparams
1661 @return: The defaults dict
1664 return FillDiskParams(self.diskparams, diskparams)
1666 def GetHVDefaults(self, hypervisor, os_name=None, skip_keys=None):
1667 """Get the default hypervisor parameters for the cluster.
1669 @param hypervisor: the hypervisor name
1670 @param os_name: if specified, we'll also update the defaults for this OS
1671 @param skip_keys: if passed, list of keys not to use
1672 @return: the defaults dict
1675 if skip_keys is None:
1678 fill_stack = [self.hvparams.get(hypervisor, {})]
1679 if os_name is not None:
1680 os_hvp = self.os_hvp.get(os_name, {}).get(hypervisor, {})
1681 fill_stack.append(os_hvp)
1684 for o_dict in fill_stack:
1685 ret_dict = FillDict(ret_dict, o_dict, skip_keys=skip_keys)
1689 def SimpleFillHV(self, hv_name, os_name, hvparams, skip_globals=False):
1690 """Fill a given hvparams dict with cluster defaults.
1692 @type hv_name: string
1693 @param hv_name: the hypervisor to use
1694 @type os_name: string
1695 @param os_name: the OS to use for overriding the hypervisor defaults
1696 @type skip_globals: boolean
1697 @param skip_globals: if True, the global hypervisor parameters will
1700 @return: a copy of the given hvparams with missing keys filled from
1701 the cluster defaults
1705 skip_keys = constants.HVC_GLOBALS
1709 def_dict = self.GetHVDefaults(hv_name, os_name, skip_keys=skip_keys)
1710 return FillDict(def_dict, hvparams, skip_keys=skip_keys)
1712 def FillHV(self, instance, skip_globals=False):
1713 """Fill an instance's hvparams dict with cluster defaults.
1715 @type instance: L{objects.Instance}
1716 @param instance: the instance parameter to fill
1717 @type skip_globals: boolean
1718 @param skip_globals: if True, the global hypervisor parameters will
1721 @return: a copy of the instance's hvparams with missing keys filled from
1722 the cluster defaults
1725 return self.SimpleFillHV(instance.hypervisor, instance.os,
1726 instance.hvparams, skip_globals)
1728 def SimpleFillBE(self, beparams):
1729 """Fill a given beparams dict with cluster defaults.
1731 @type beparams: dict
1732 @param beparams: the dict to fill
1734 @return: a copy of the passed in beparams with missing keys filled
1735 from the cluster defaults
1738 return FillDict(self.beparams.get(constants.PP_DEFAULT, {}), beparams)
1740 def FillBE(self, instance):
1741 """Fill an instance's beparams dict with cluster defaults.
1743 @type instance: L{objects.Instance}
1744 @param instance: the instance parameter to fill
1746 @return: a copy of the instance's beparams with missing keys filled from
1747 the cluster defaults
1750 return self.SimpleFillBE(instance.beparams)
1752 def SimpleFillNIC(self, nicparams):
1753 """Fill a given nicparams dict with cluster defaults.
1755 @type nicparams: dict
1756 @param nicparams: the dict to fill
1758 @return: a copy of the passed in nicparams with missing keys filled
1759 from the cluster defaults
1762 return FillDict(self.nicparams.get(constants.PP_DEFAULT, {}), nicparams)
1764 def SimpleFillOS(self, os_name, os_params):
1765 """Fill an instance's osparams dict with cluster defaults.
1767 @type os_name: string
1768 @param os_name: the OS name to use
1769 @type os_params: dict
1770 @param os_params: the dict to fill with default values
1772 @return: a copy of the instance's osparams with missing keys filled from
1773 the cluster defaults
1776 name_only = os_name.split("+", 1)[0]
1778 result = self.osparams.get(name_only, {})
1780 result = FillDict(result, self.osparams.get(os_name, {}))
1782 return FillDict(result, os_params)
1785 def SimpleFillHvState(hv_state):
1786 """Fill an hv_state sub dict with cluster defaults.
1789 return FillDict(constants.HVST_DEFAULTS, hv_state)
1792 def SimpleFillDiskState(disk_state):
1793 """Fill an disk_state sub dict with cluster defaults.
1796 return FillDict(constants.DS_DEFAULTS, disk_state)
1798 def FillND(self, node, nodegroup):
1799 """Return filled out ndparams for L{objects.NodeGroup} and L{objects.Node}
1801 @type node: L{objects.Node}
1802 @param node: A Node object to fill
1803 @type nodegroup: L{objects.NodeGroup}
1804 @param nodegroup: A Node object to fill
1805 @return a copy of the node's ndparams with defaults filled
1808 return self.SimpleFillND(nodegroup.FillND(node))
1810 def SimpleFillND(self, ndparams):
1811 """Fill a given ndparams dict with defaults.
1813 @type ndparams: dict
1814 @param ndparams: the dict to fill
1816 @return: a copy of the passed in ndparams with missing keys filled
1817 from the cluster defaults
1820 return FillDict(self.ndparams, ndparams)
1822 def SimpleFillIPolicy(self, ipolicy):
1823 """ Fill instance policy dict with defaults.
1826 @param ipolicy: the dict to fill
1828 @return: a copy of passed ipolicy with missing keys filled from
1829 the cluster defaults
1832 return FillIPolicy(self.ipolicy, ipolicy)
1835 class BlockDevStatus(ConfigObject):
1836 """Config object representing the status of a block device."""
1848 class ImportExportStatus(ConfigObject):
1849 """Config object representing the status of an import or export."""
1855 "progress_throughput",
1863 class ImportExportOptions(ConfigObject):
1864 """Options for import/export daemon
1866 @ivar key_name: X509 key name (None for cluster certificate)
1867 @ivar ca_pem: Remote peer CA in PEM format (None for cluster certificate)
1868 @ivar compress: Compression method (one of L{constants.IEC_ALL})
1869 @ivar magic: Used to ensure the connection goes to the right disk
1870 @ivar ipv6: Whether to use IPv6
1871 @ivar connect_timeout: Number of seconds for establishing connection
1884 class ConfdRequest(ConfigObject):
1885 """Object holding a confd request.
1887 @ivar protocol: confd protocol version
1888 @ivar type: confd query type
1889 @ivar query: query request
1890 @ivar rsalt: requested reply salt
1901 class ConfdReply(ConfigObject):
1902 """Object holding a confd reply.
1904 @ivar protocol: confd protocol version
1905 @ivar status: reply status code (ok, error)
1906 @ivar answer: confd query reply
1907 @ivar serial: configuration serial number
1918 class QueryFieldDefinition(ConfigObject):
1919 """Object holding a query field definition.
1921 @ivar name: Field name
1922 @ivar title: Human-readable title
1923 @ivar kind: Field type
1924 @ivar doc: Human-readable description
1935 class _QueryResponseBase(ConfigObject):
1941 """Custom function for serializing.
1944 mydict = super(_QueryResponseBase, self).ToDict()
1945 mydict["fields"] = self._ContainerToDicts(mydict["fields"])
1949 def FromDict(cls, val):
1950 """Custom function for de-serializing.
1953 obj = super(_QueryResponseBase, cls).FromDict(val)
1954 obj.fields = cls._ContainerFromDicts(obj.fields, list, QueryFieldDefinition)
1958 class QueryResponse(_QueryResponseBase):
1959 """Object holding the response to a query.
1961 @ivar fields: List of L{QueryFieldDefinition} objects
1962 @ivar data: Requested data
1970 class QueryFieldsRequest(ConfigObject):
1971 """Object holding a request for querying available fields.
1980 class QueryFieldsResponse(_QueryResponseBase):
1981 """Object holding the response to a query for fields.
1983 @ivar fields: List of L{QueryFieldDefinition} objects
1989 class MigrationStatus(ConfigObject):
1990 """Object holding the status of a migration.
2000 class InstanceConsole(ConfigObject):
2001 """Object describing how to access the console of an instance.
2016 """Validates contents of this object.
2019 assert self.kind in constants.CONS_ALL, "Unknown console type"
2020 assert self.instance, "Missing instance name"
2021 assert self.message or self.kind in [constants.CONS_SSH,
2022 constants.CONS_SPICE,
2024 assert self.host or self.kind == constants.CONS_MESSAGE
2025 assert self.port or self.kind in [constants.CONS_MESSAGE,
2027 assert self.user or self.kind in [constants.CONS_MESSAGE,
2028 constants.CONS_SPICE,
2030 assert self.command or self.kind in [constants.CONS_MESSAGE,
2031 constants.CONS_SPICE,
2033 assert self.display or self.kind in [constants.CONS_MESSAGE,
2034 constants.CONS_SPICE,
2039 class Network(TaggableObject):
2040 """Object representing a network definition for ganeti.
2053 ] + _TIMESTAMPS + _UUID
2055 def HooksDict(self, prefix=""):
2056 """Export a dictionary used by hooks with a network's information.
2058 @type prefix: String
2059 @param prefix: Prefix to prepend to the dict entries
2063 "%sNETWORK_NAME" % prefix: self.name,
2064 "%sNETWORK_UUID" % prefix: self.uuid,
2065 "%sNETWORK_TAGS" % prefix: " ".join(self.GetTags()),
2068 result["%sNETWORK_SUBNET" % prefix] = self.network
2070 result["%sNETWORK_GATEWAY" % prefix] = self.gateway
2072 result["%sNETWORK_SUBNET6" % prefix] = self.network6
2074 result["%sNETWORK_GATEWAY6" % prefix] = self.gateway6
2076 result["%sNETWORK_MAC_PREFIX" % prefix] = self.mac_prefix
2081 def FromDict(cls, val):
2082 """Custom function for networks.
2084 Remove deprecated network_type and family.
2087 if "network_type" in val:
2088 del val["network_type"]
2091 obj = super(Network, cls).FromDict(val)
2095 class SerializableConfigParser(ConfigParser.SafeConfigParser):
2096 """Simple wrapper over ConfigParse that allows serialization.
2098 This class is basically ConfigParser.SafeConfigParser with two
2099 additional methods that allow it to serialize/unserialize to/from a
2104 """Dump this instance and return the string representation."""
2107 return buf.getvalue()
2110 def Loads(cls, data):
2111 """Load data from a string."""
2112 buf = StringIO(data)
2118 class LvmPvInfo(ConfigObject):
2119 """Information about an LVM physical volume (PV).
2122 @ivar name: name of the PV
2123 @type vg_name: string
2124 @ivar vg_name: name of the volume group containing the PV
2126 @ivar size: size of the PV in MiB
2128 @ivar free: free space in the PV, in MiB
2129 @type attributes: string
2130 @ivar attributes: PV attributes
2131 @type lv_list: list of strings
2132 @ivar lv_list: names of the LVs hosted on the PV
2144 """Is this PV empty?
2147 return self.size <= (self.free + 1)
2149 def IsAllocatable(self):
2150 """Is this PV allocatable?
2153 return ("a" in self.attributes)