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):
86 """Fills an instance policy with defaults.
89 assert frozenset(default_ipolicy.keys()) == constants.IPOLICY_ALL_KEYS
90 ret_dict = copy.deepcopy(custom_ipolicy)
91 for key in default_ipolicy:
92 if key not in ret_dict:
93 ret_dict[key] = copy.deepcopy(default_ipolicy[key])
94 elif key == constants.ISPECS_STD:
95 ret_dict[key] = FillDict(default_ipolicy[key], ret_dict[key])
99 def FillDiskParams(default_dparams, custom_dparams, skip_keys=None):
100 """Fills the disk parameter defaults.
102 @see: L{FillDict} for parameters and return value
105 assert frozenset(default_dparams.keys()) == constants.DISK_TEMPLATES
107 return dict((dt, FillDict(default_dparams[dt], custom_dparams.get(dt, {}),
108 skip_keys=skip_keys))
109 for dt in constants.DISK_TEMPLATES)
112 def UpgradeGroupedParams(target, defaults):
113 """Update all groups for the target parameter.
115 @type target: dict of dicts
116 @param target: {group: {parameter: value}}
118 @param defaults: default parameter values
122 target = {constants.PP_DEFAULT: defaults}
125 target[group] = FillDict(defaults, target[group])
129 def UpgradeBeParams(target):
130 """Update the be parameters dict to the new format.
133 @param target: "be" parameters dict
136 if constants.BE_MEMORY in target:
137 memory = target[constants.BE_MEMORY]
138 target[constants.BE_MAXMEM] = memory
139 target[constants.BE_MINMEM] = memory
140 del target[constants.BE_MEMORY]
143 def UpgradeDiskParams(diskparams):
144 """Upgrade the disk parameters.
146 @type diskparams: dict
147 @param diskparams: disk parameters to upgrade
149 @return: the upgraded disk parameters dict
155 result = FillDiskParams(constants.DISK_DT_DEFAULTS, diskparams)
160 def UpgradeNDParams(ndparams):
161 """Upgrade ndparams structure.
164 @param ndparams: disk parameters to upgrade
166 @return: the upgraded node parameters dict
172 if (constants.ND_OOB_PROGRAM in ndparams and
173 ndparams[constants.ND_OOB_PROGRAM] is None):
174 # will be reset by the line below
175 del ndparams[constants.ND_OOB_PROGRAM]
176 return FillDict(constants.NDC_DEFAULTS, ndparams)
179 def MakeEmptyIPolicy():
180 """Create empty IPolicy dictionary.
186 class ConfigObject(outils.ValidatedSlots):
187 """A generic config object.
189 It has the following properties:
191 - provides somewhat safe recursive unpickling and pickling for its classes
192 - unset attributes which are defined in slots are always returned
193 as None instead of raising an error
195 Classes derived from this must always declare __slots__ (we use many
196 config objects and the memory reduction is useful)
201 def __getattr__(self, name):
202 if name not in self.GetAllSlots():
203 raise AttributeError("Invalid object attribute %s.%s" %
204 (type(self).__name__, name))
207 def __setstate__(self, state):
208 slots = self.GetAllSlots()
211 setattr(self, name, state[name])
214 """Validates the slots.
219 """Convert to a dict holding only standard python types.
221 The generic routine just dumps all of this object's attributes in
222 a dict. It does not work if the class has children who are
223 ConfigObjects themselves (e.g. the nics list in an Instance), in
224 which case the object should subclass the function in order to
225 make sure all objects returned are only standard python types.
229 for name in self.GetAllSlots():
230 value = getattr(self, name, None)
231 if value is not None:
235 __getstate__ = ToDict
238 def FromDict(cls, val):
239 """Create an object from a dictionary.
241 This generic routine takes a dict, instantiates a new instance of
242 the given class, and sets attributes based on the dict content.
244 As for `ToDict`, this does not work if the class has children
245 who are ConfigObjects themselves (e.g. the nics list in an
246 Instance), in which case the object should subclass the function
247 and alter the objects.
250 if not isinstance(val, dict):
251 raise errors.ConfigurationError("Invalid object passed to FromDict:"
252 " expected dict, got %s" % type(val))
253 val_str = dict([(str(k), v) for k, v in val.iteritems()])
254 obj = cls(**val_str) # pylint: disable=W0142
258 """Makes a deep copy of the current object and its children.
261 dict_form = self.ToDict()
262 clone_obj = self.__class__.FromDict(dict_form)
266 """Implement __repr__ for ConfigObjects."""
267 return repr(self.ToDict())
269 def __eq__(self, other):
270 """Implement __eq__ for ConfigObjects."""
271 return isinstance(other, self.__class__) and self.ToDict() == other.ToDict()
273 def UpgradeConfig(self):
274 """Fill defaults for missing configuration values.
276 This method will be called at configuration load time, and its
277 implementation will be object dependent.
283 class TaggableObject(ConfigObject):
284 """An generic class supporting tags.
288 VALID_TAG_RE = re.compile("^[\w.+*/:@-]+$")
291 def ValidateTag(cls, tag):
292 """Check if a tag is valid.
294 If the tag is invalid, an errors.TagError will be raised. The
295 function has no return value.
298 if not isinstance(tag, basestring):
299 raise errors.TagError("Invalid tag type (not a string)")
300 if len(tag) > constants.MAX_TAG_LEN:
301 raise errors.TagError("Tag too long (>%d characters)" %
302 constants.MAX_TAG_LEN)
304 raise errors.TagError("Tags cannot be empty")
305 if not cls.VALID_TAG_RE.match(tag):
306 raise errors.TagError("Tag contains invalid characters")
309 """Return the tags list.
312 tags = getattr(self, "tags", None)
314 tags = self.tags = set()
317 def AddTag(self, tag):
321 self.ValidateTag(tag)
322 tags = self.GetTags()
323 if len(tags) >= constants.MAX_TAGS_PER_OBJ:
324 raise errors.TagError("Too many tags")
325 self.GetTags().add(tag)
327 def RemoveTag(self, tag):
331 self.ValidateTag(tag)
332 tags = self.GetTags()
336 raise errors.TagError("Tag not found")
339 """Taggable-object-specific conversion to standard python types.
341 This replaces the tags set with a list.
344 bo = super(TaggableObject, self).ToDict()
346 tags = bo.get("tags", None)
347 if isinstance(tags, set):
348 bo["tags"] = list(tags)
352 def FromDict(cls, val):
353 """Custom function for instances.
356 obj = super(TaggableObject, cls).FromDict(val)
357 if hasattr(obj, "tags") and isinstance(obj.tags, list):
358 obj.tags = set(obj.tags)
362 class MasterNetworkParameters(ConfigObject):
363 """Network configuration parameters for the master
365 @ivar uuid: master nodes UUID
367 @ivar netmask: master netmask
368 @ivar netdev: master network device
369 @ivar ip_family: master IP family
381 class ConfigData(ConfigObject):
382 """Top-level config object."""
394 """Custom function for top-level config data.
396 This just replaces the list of instances, nodes and the cluster
397 with standard python types.
400 mydict = super(ConfigData, self).ToDict()
401 mydict["cluster"] = mydict["cluster"].ToDict()
402 for key in "nodes", "instances", "nodegroups", "networks":
403 mydict[key] = outils.ContainerToDicts(mydict[key])
408 def FromDict(cls, val):
409 """Custom function for top-level config data
412 obj = super(ConfigData, cls).FromDict(val)
413 obj.cluster = Cluster.FromDict(obj.cluster)
414 obj.nodes = outils.ContainerFromDicts(obj.nodes, dict, Node)
416 outils.ContainerFromDicts(obj.instances, dict, Instance)
418 outils.ContainerFromDicts(obj.nodegroups, dict, NodeGroup)
419 obj.networks = outils.ContainerFromDicts(obj.networks, dict, Network)
422 def HasAnyDiskOfType(self, dev_type):
423 """Check if in there is at disk of the given type in the configuration.
425 @type dev_type: L{constants.LDS_BLOCK}
426 @param dev_type: the type to look for
428 @return: boolean indicating if a disk of the given type was found or not
431 for instance in self.instances.values():
432 for disk in instance.disks:
433 if disk.IsBasedOnDiskType(dev_type):
437 def UpgradeConfig(self):
438 """Fill defaults for missing configuration values.
441 self.cluster.UpgradeConfig()
442 for node in self.nodes.values():
444 for instance in self.instances.values():
445 instance.UpgradeConfig()
446 if self.nodegroups is None:
448 for nodegroup in self.nodegroups.values():
449 nodegroup.UpgradeConfig()
450 if self.cluster.drbd_usermode_helper is None:
451 # To decide if we set an helper let's check if at least one instance has
452 # a DRBD disk. This does not cover all the possible scenarios but it
453 # gives a good approximation.
454 if self.HasAnyDiskOfType(constants.LD_DRBD8):
455 self.cluster.drbd_usermode_helper = constants.DEFAULT_DRBD_HELPER
456 if self.networks is None:
458 for network in self.networks.values():
459 network.UpgradeConfig()
460 self._UpgradeEnabledDiskTemplates()
462 def _UpgradeEnabledDiskTemplates(self):
463 """Upgrade the cluster's enabled disk templates by inspecting the currently
464 enabled and/or used disk templates.
467 # enabled_disk_templates in the cluster config were introduced in 2.8.
468 # Remove this code once upgrading from earlier versions is deprecated.
469 if not self.cluster.enabled_disk_templates:
471 set([inst.disk_template for inst in self.instances.values()])
472 # Add drbd and plain, if lvm is enabled (by specifying a volume group)
473 if self.cluster.volume_group_name:
474 template_set.add(constants.DT_DRBD8)
475 template_set.add(constants.DT_PLAIN)
476 # Set enabled_disk_templates to the inferred disk templates. Order them
477 # according to a preference list that is based on Ganeti's history of
478 # supported disk templates.
479 self.cluster.enabled_disk_templates = []
480 for preferred_template in constants.DISK_TEMPLATE_PREFERENCE:
481 if preferred_template in template_set:
482 self.cluster.enabled_disk_templates.append(preferred_template)
483 template_set.remove(preferred_template)
484 self.cluster.enabled_disk_templates.extend(list(template_set))
487 class NIC(ConfigObject):
488 """Config object representing a network card."""
489 __slots__ = ["name", "mac", "ip", "network", "nicparams", "netinfo"] + _UUID
492 def CheckParameterSyntax(cls, nicparams):
493 """Check the given parameters for validity.
495 @type nicparams: dict
496 @param nicparams: dictionary with parameter names/value
497 @raise errors.ConfigurationError: when a parameter is not valid
500 mode = nicparams[constants.NIC_MODE]
501 if (mode not in constants.NIC_VALID_MODES and
502 mode != constants.VALUE_AUTO):
503 raise errors.ConfigurationError("Invalid NIC mode '%s'" % mode)
505 if (mode == constants.NIC_MODE_BRIDGED and
506 not nicparams[constants.NIC_LINK]):
507 raise errors.ConfigurationError("Missing bridged NIC link")
510 class Disk(ConfigObject):
511 """Config object representing a block device."""
512 __slots__ = (["name", "dev_type", "logical_id", "physical_id",
513 "children", "iv_name", "size", "mode", "params", "spindles"] +
516 def CreateOnSecondary(self):
517 """Test if this device needs to be created on a secondary node."""
518 return self.dev_type in (constants.LD_DRBD8, constants.LD_LV)
520 def AssembleOnSecondary(self):
521 """Test if this device needs to be assembled on a secondary node."""
522 return self.dev_type in (constants.LD_DRBD8, constants.LD_LV)
524 def OpenOnSecondary(self):
525 """Test if this device needs to be opened on a secondary node."""
526 return self.dev_type in (constants.LD_LV,)
528 def StaticDevPath(self):
529 """Return the device path if this device type has a static one.
531 Some devices (LVM for example) live always at the same /dev/ path,
532 irrespective of their status. For such devices, we return this
533 path, for others we return None.
535 @warning: The path returned is not a normalized pathname; callers
536 should check that it is a valid path.
539 if self.dev_type == constants.LD_LV:
540 return "/dev/%s/%s" % (self.logical_id[0], self.logical_id[1])
541 elif self.dev_type == constants.LD_BLOCKDEV:
542 return self.logical_id[1]
543 elif self.dev_type == constants.LD_RBD:
544 return "/dev/%s/%s" % (self.logical_id[0], self.logical_id[1])
547 def ChildrenNeeded(self):
548 """Compute the needed number of children for activation.
550 This method will return either -1 (all children) or a positive
551 number denoting the minimum number of children needed for
552 activation (only mirrored devices will usually return >=0).
554 Currently, only DRBD8 supports diskless activation (therefore we
555 return 0), for all other we keep the previous semantics and return
559 if self.dev_type == constants.LD_DRBD8:
563 def IsBasedOnDiskType(self, dev_type):
564 """Check if the disk or its children are based on the given type.
566 @type dev_type: L{constants.LDS_BLOCK}
567 @param dev_type: the type to look for
569 @return: boolean indicating if a device of the given type was found or not
573 for child in self.children:
574 if child.IsBasedOnDiskType(dev_type):
576 return self.dev_type == dev_type
578 def GetNodes(self, node_uuid):
579 """This function returns the nodes this device lives on.
581 Given the node on which the parent of the device lives on (or, in
582 case of a top-level device, the primary node of the devices'
583 instance), this function will return a list of nodes on which this
584 devices needs to (or can) be assembled.
587 if self.dev_type in [constants.LD_LV, constants.LD_FILE,
588 constants.LD_BLOCKDEV, constants.LD_RBD,
591 elif self.dev_type in constants.LDS_DRBD:
592 result = [self.logical_id[0], self.logical_id[1]]
593 if node_uuid not in result:
594 raise errors.ConfigurationError("DRBD device passed unknown node")
596 raise errors.ProgrammerError("Unhandled device type %s" % self.dev_type)
599 def ComputeNodeTree(self, parent_node_uuid):
600 """Compute the node/disk tree for this disk and its children.
602 This method, given the node on which the parent disk lives, will
603 return the list of all (node UUID, disk) pairs which describe the disk
604 tree in the most compact way. For example, a drbd/lvm stack
605 will be returned as (primary_node, drbd) and (secondary_node, drbd)
606 which represents all the top-level devices on the nodes.
609 my_nodes = self.GetNodes(parent_node_uuid)
610 result = [(node, self) for node in my_nodes]
611 if not self.children:
614 for node in my_nodes:
615 for child in self.children:
616 child_result = child.ComputeNodeTree(node)
617 if len(child_result) == 1:
618 # child (and all its descendants) is simple, doesn't split
619 # over multiple hosts, so we don't need to describe it, our
620 # own entry for this node describes it completely
623 # check if child nodes differ from my nodes; note that
624 # subdisk can differ from the child itself, and be instead
625 # one of its descendants
626 for subnode, subdisk in child_result:
627 if subnode not in my_nodes:
628 result.append((subnode, subdisk))
629 # otherwise child is under our own node, so we ignore this
630 # entry (but probably the other results in the list will
634 def ComputeGrowth(self, amount):
635 """Compute the per-VG growth requirements.
637 This only works for VG-based disks.
639 @type amount: integer
640 @param amount: the desired increase in (user-visible) disk space
642 @return: a dictionary of volume-groups and the required size
645 if self.dev_type == constants.LD_LV:
646 return {self.logical_id[0]: amount}
647 elif self.dev_type == constants.LD_DRBD8:
649 return self.children[0].ComputeGrowth(amount)
653 # Other disk types do not require VG space
656 def RecordGrow(self, amount):
657 """Update the size of this disk after growth.
659 This method recurses over the disks's children and updates their
660 size correspondigly. The method needs to be kept in sync with the
661 actual algorithms from bdev.
664 if self.dev_type in (constants.LD_LV, constants.LD_FILE,
665 constants.LD_RBD, constants.LD_EXT):
667 elif self.dev_type == constants.LD_DRBD8:
669 self.children[0].RecordGrow(amount)
672 raise errors.ProgrammerError("Disk.RecordGrow called for unsupported"
673 " disk type %s" % self.dev_type)
675 def Update(self, size=None, mode=None, spindles=None):
676 """Apply changes to size, spindles and mode.
679 if self.dev_type == constants.LD_DRBD8:
681 self.children[0].Update(size=size, mode=mode)
683 assert not self.children
689 if spindles is not None:
690 self.spindles = spindles
693 """Sets recursively the size to zero for the disk and its children.
697 for child in self.children:
701 def SetPhysicalID(self, target_node_uuid, nodes_ip):
702 """Convert the logical ID to the physical ID.
704 This is used only for drbd, which needs ip/port configuration.
706 The routine descends down and updates its children also, because
707 this helps when the only the top device is passed to the remote
711 - target_node_uuid: the node UUID we wish to configure for
712 - nodes_ip: a mapping of node name to ip
714 The target_node must exist in in nodes_ip, and must be one of the
715 nodes in the logical ID for each of the DRBD devices encountered
720 for child in self.children:
721 child.SetPhysicalID(target_node_uuid, nodes_ip)
723 if self.logical_id is None and self.physical_id is not None:
725 if self.dev_type in constants.LDS_DRBD:
726 pnode_uuid, snode_uuid, port, pminor, sminor, secret = self.logical_id
727 if target_node_uuid not in (pnode_uuid, snode_uuid):
728 raise errors.ConfigurationError("DRBD device not knowing node %s" %
730 pnode_ip = nodes_ip.get(pnode_uuid, None)
731 snode_ip = nodes_ip.get(snode_uuid, None)
732 if pnode_ip is None or snode_ip is None:
733 raise errors.ConfigurationError("Can't find primary or secondary node"
734 " for %s" % str(self))
735 p_data = (pnode_ip, port)
736 s_data = (snode_ip, port)
737 if pnode_uuid == target_node_uuid:
738 self.physical_id = p_data + s_data + (pminor, secret)
739 else: # it must be secondary, we tested above
740 self.physical_id = s_data + p_data + (sminor, secret)
742 self.physical_id = self.logical_id
746 """Disk-specific conversion to standard python types.
748 This replaces the children lists of objects with lists of
749 standard python types.
752 bo = super(Disk, self).ToDict()
754 for attr in ("children",):
755 alist = bo.get(attr, None)
757 bo[attr] = outils.ContainerToDicts(alist)
761 def FromDict(cls, val):
762 """Custom function for Disks
765 obj = super(Disk, cls).FromDict(val)
767 obj.children = outils.ContainerFromDicts(obj.children, list, Disk)
768 if obj.logical_id and isinstance(obj.logical_id, list):
769 obj.logical_id = tuple(obj.logical_id)
770 if obj.physical_id and isinstance(obj.physical_id, list):
771 obj.physical_id = tuple(obj.physical_id)
772 if obj.dev_type in constants.LDS_DRBD:
773 # we need a tuple of length six here
774 if len(obj.logical_id) < 6:
775 obj.logical_id += (None,) * (6 - len(obj.logical_id))
779 """Custom str() formatter for disks.
782 if self.dev_type == constants.LD_LV:
783 val = "<LogicalVolume(/dev/%s/%s" % self.logical_id
784 elif self.dev_type in constants.LDS_DRBD:
785 node_a, node_b, port, minor_a, minor_b = self.logical_id[:5]
787 if self.physical_id is None:
790 phy = ("configured as %s:%s %s:%s" %
791 (self.physical_id[0], self.physical_id[1],
792 self.physical_id[2], self.physical_id[3]))
794 val += ("hosts=%s/%d-%s/%d, port=%s, %s, " %
795 (node_a, minor_a, node_b, minor_b, port, phy))
796 if self.children and self.children.count(None) == 0:
797 val += "backend=%s, metadev=%s" % (self.children[0], self.children[1])
799 val += "no local storage"
801 val = ("<Disk(type=%s, logical_id=%s, physical_id=%s, children=%s" %
802 (self.dev_type, self.logical_id, self.physical_id, self.children))
803 if self.iv_name is None:
804 val += ", not visible"
806 val += ", visible as /dev/%s" % self.iv_name
807 if self.spindles is not None:
808 val += ", spindles=%s" % self.spindles
809 if isinstance(self.size, int):
810 val += ", size=%dm)>" % self.size
812 val += ", size='%s')>" % (self.size,)
816 """Checks that this disk is correctly configured.
820 if self.mode not in constants.DISK_ACCESS_SET:
821 all_errors.append("Disk access mode '%s' is invalid" % (self.mode, ))
824 def UpgradeConfig(self):
825 """Fill defaults for missing configuration values.
829 for child in self.children:
830 child.UpgradeConfig()
832 # FIXME: Make this configurable in Ganeti 2.7
834 # add here config upgrade for this disk
837 def ComputeLDParams(disk_template, disk_params):
838 """Computes Logical Disk parameters from Disk Template parameters.
840 @type disk_template: string
841 @param disk_template: disk template, one of L{constants.DISK_TEMPLATES}
842 @type disk_params: dict
843 @param disk_params: disk template parameters;
844 dict(template_name -> parameters
846 @return: a list of dicts, one for each node of the disk hierarchy. Each dict
847 contains the LD parameters of the node. The tree is flattened in-order.
850 if disk_template not in constants.DISK_TEMPLATES:
851 raise errors.ProgrammerError("Unknown disk template %s" % disk_template)
853 assert disk_template in disk_params
856 dt_params = disk_params[disk_template]
857 if disk_template == constants.DT_DRBD8:
858 result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.LD_DRBD8], {
859 constants.LDP_RESYNC_RATE: dt_params[constants.DRBD_RESYNC_RATE],
860 constants.LDP_BARRIERS: dt_params[constants.DRBD_DISK_BARRIERS],
861 constants.LDP_NO_META_FLUSH: dt_params[constants.DRBD_META_BARRIERS],
862 constants.LDP_DEFAULT_METAVG: dt_params[constants.DRBD_DEFAULT_METAVG],
863 constants.LDP_DISK_CUSTOM: dt_params[constants.DRBD_DISK_CUSTOM],
864 constants.LDP_NET_CUSTOM: dt_params[constants.DRBD_NET_CUSTOM],
865 constants.LDP_PROTOCOL: dt_params[constants.DRBD_PROTOCOL],
866 constants.LDP_DYNAMIC_RESYNC: dt_params[constants.DRBD_DYNAMIC_RESYNC],
867 constants.LDP_PLAN_AHEAD: dt_params[constants.DRBD_PLAN_AHEAD],
868 constants.LDP_FILL_TARGET: dt_params[constants.DRBD_FILL_TARGET],
869 constants.LDP_DELAY_TARGET: dt_params[constants.DRBD_DELAY_TARGET],
870 constants.LDP_MAX_RATE: dt_params[constants.DRBD_MAX_RATE],
871 constants.LDP_MIN_RATE: dt_params[constants.DRBD_MIN_RATE],
875 result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.LD_LV], {
876 constants.LDP_STRIPES: dt_params[constants.DRBD_DATA_STRIPES],
880 result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.LD_LV], {
881 constants.LDP_STRIPES: dt_params[constants.DRBD_META_STRIPES],
884 elif disk_template in (constants.DT_FILE, constants.DT_SHARED_FILE):
885 result.append(constants.DISK_LD_DEFAULTS[constants.LD_FILE])
887 elif disk_template == constants.DT_PLAIN:
888 result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.LD_LV], {
889 constants.LDP_STRIPES: dt_params[constants.LV_STRIPES],
892 elif disk_template == constants.DT_BLOCK:
893 result.append(constants.DISK_LD_DEFAULTS[constants.LD_BLOCKDEV])
895 elif disk_template == constants.DT_RBD:
896 result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.LD_RBD], {
897 constants.LDP_POOL: dt_params[constants.RBD_POOL],
900 elif disk_template == constants.DT_EXT:
901 result.append(constants.DISK_LD_DEFAULTS[constants.LD_EXT])
906 class InstancePolicy(ConfigObject):
907 """Config object representing instance policy limits dictionary.
909 Note that this object is not actually used in the config, it's just
910 used as a placeholder for a few functions.
914 def CheckParameterSyntax(cls, ipolicy, check_std):
915 """ Check the instance policy for validity.
918 @param ipolicy: dictionary with min/max/std specs and policies
919 @type check_std: bool
920 @param check_std: Whether to check std value or just assume compliance
921 @raise errors.ConfigurationError: when the policy is not legal
924 InstancePolicy.CheckISpecSyntax(ipolicy, check_std)
925 if constants.IPOLICY_DTS in ipolicy:
926 InstancePolicy.CheckDiskTemplates(ipolicy[constants.IPOLICY_DTS])
927 for key in constants.IPOLICY_PARAMETERS:
929 InstancePolicy.CheckParameter(key, ipolicy[key])
930 wrong_keys = frozenset(ipolicy.keys()) - constants.IPOLICY_ALL_KEYS
932 raise errors.ConfigurationError("Invalid keys in ipolicy: %s" %
933 utils.CommaJoin(wrong_keys))
936 def _CheckIncompleteSpec(cls, spec, keyname):
937 missing_params = constants.ISPECS_PARAMETERS - frozenset(spec.keys())
939 msg = ("Missing instance specs parameters for %s: %s" %
940 (keyname, utils.CommaJoin(missing_params)))
941 raise errors.ConfigurationError(msg)
944 def CheckISpecSyntax(cls, ipolicy, check_std):
945 """Check the instance policy specs for validity.
948 @param ipolicy: dictionary with min/max/std specs
949 @type check_std: bool
950 @param check_std: Whether to check std value or just assume compliance
951 @raise errors.ConfigurationError: when specs are not valid
954 if constants.ISPECS_MINMAX not in ipolicy:
958 if check_std and constants.ISPECS_STD not in ipolicy:
959 msg = "Missing key in ipolicy: %s" % constants.ISPECS_STD
960 raise errors.ConfigurationError(msg)
961 stdspec = ipolicy.get(constants.ISPECS_STD)
963 InstancePolicy._CheckIncompleteSpec(stdspec, constants.ISPECS_STD)
965 if not ipolicy[constants.ISPECS_MINMAX]:
966 raise errors.ConfigurationError("Empty minmax specifications")
968 for minmaxspecs in ipolicy[constants.ISPECS_MINMAX]:
969 missing = constants.ISPECS_MINMAX_KEYS - frozenset(minmaxspecs.keys())
971 msg = "Missing instance specification: %s" % utils.CommaJoin(missing)
972 raise errors.ConfigurationError(msg)
973 for (key, spec) in minmaxspecs.items():
974 InstancePolicy._CheckIncompleteSpec(spec, key)
977 for param in constants.ISPECS_PARAMETERS:
978 par_std_ok = InstancePolicy._CheckISpecParamSyntax(minmaxspecs, stdspec,
980 spec_std_ok = spec_std_ok and par_std_ok
981 std_is_good = std_is_good or spec_std_ok
983 raise errors.ConfigurationError("Invalid std specifications")
986 def _CheckISpecParamSyntax(cls, minmaxspecs, stdspec, name, check_std):
987 """Check the instance policy specs for validity on a given key.
989 We check if the instance specs makes sense for a given key, that is
990 if minmaxspecs[min][name] <= stdspec[name] <= minmaxspec[max][name].
992 @type minmaxspecs: dict
993 @param minmaxspecs: dictionary with min and max instance spec
995 @param stdspec: dictionary with standard instance spec
997 @param name: what are the limits for
998 @type check_std: bool
999 @param check_std: Whether to check std value or just assume compliance
1001 @return: C{True} when specs are valid, C{False} when standard spec for the
1002 given name is not valid
1003 @raise errors.ConfigurationError: when min/max specs for the given name
1007 minspec = minmaxspecs[constants.ISPECS_MIN]
1008 maxspec = minmaxspecs[constants.ISPECS_MAX]
1009 min_v = minspec[name]
1010 max_v = maxspec[name]
1013 err = ("Invalid specification of min/max values for %s: %s/%s" %
1014 (name, min_v, max_v))
1015 raise errors.ConfigurationError(err)
1017 std_v = stdspec.get(name, min_v)
1018 return std_v >= min_v and std_v <= max_v
1023 def CheckDiskTemplates(cls, disk_templates):
1024 """Checks the disk templates for validity.
1027 if not disk_templates:
1028 raise errors.ConfigurationError("Instance policy must contain" +
1029 " at least one disk template")
1030 wrong = frozenset(disk_templates).difference(constants.DISK_TEMPLATES)
1032 raise errors.ConfigurationError("Invalid disk template(s) %s" %
1033 utils.CommaJoin(wrong))
1036 def CheckParameter(cls, key, value):
1037 """Checks a parameter.
1039 Currently we expect all parameters to be float values.
1044 except (TypeError, ValueError), err:
1045 raise errors.ConfigurationError("Invalid value for key" " '%s':"
1046 " '%s', error: %s" % (key, value, err))
1049 class Instance(TaggableObject):
1050 """Config object representing an instance."""
1066 ] + _TIMESTAMPS + _UUID
1068 def _ComputeSecondaryNodes(self):
1069 """Compute the list of secondary nodes.
1071 This is a simple wrapper over _ComputeAllNodes.
1074 all_nodes = set(self._ComputeAllNodes())
1075 all_nodes.discard(self.primary_node)
1076 return tuple(all_nodes)
1078 secondary_nodes = property(_ComputeSecondaryNodes, None, None,
1079 "List of names of secondary nodes")
1081 def _ComputeAllNodes(self):
1082 """Compute the list of all nodes.
1084 Since the data is already there (in the drbd disks), keeping it as
1085 a separate normal attribute is redundant and if not properly
1086 synchronised can cause problems. Thus it's better to compute it
1090 def _Helper(nodes, device):
1091 """Recursively computes nodes given a top device."""
1092 if device.dev_type in constants.LDS_DRBD:
1093 nodea, nodeb = device.logical_id[:2]
1097 for child in device.children:
1098 _Helper(nodes, child)
1101 all_nodes.add(self.primary_node)
1102 for device in self.disks:
1103 _Helper(all_nodes, device)
1104 return tuple(all_nodes)
1106 all_nodes = property(_ComputeAllNodes, None, None,
1107 "List of names of all the nodes of the instance")
1109 def MapLVsByNode(self, lvmap=None, devs=None, node_uuid=None):
1110 """Provide a mapping of nodes to LVs this instance owns.
1112 This function figures out what logical volumes should belong on
1113 which nodes, recursing through a device tree.
1116 @param lvmap: optional dictionary to receive the
1117 'node' : ['lv', ...] data.
1118 @type devs: list of L{Disk}
1119 @param devs: disks to get the LV name for. If None, all disk of this
1121 @type node_uuid: string
1122 @param node_uuid: UUID of the node to get the LV names for. If None, the
1123 primary node of this instance is used.
1124 @return: None if lvmap arg is given, otherwise, a dictionary of
1125 the form { 'node_uuid' : ['volume1', 'volume2', ...], ... };
1126 volumeN is of the form "vg_name/lv_name", compatible with
1130 if node_uuid is None:
1131 node_uuid = self.primary_node
1139 if not node_uuid in lvmap:
1140 lvmap[node_uuid] = []
1147 if dev.dev_type == constants.LD_LV:
1148 lvmap[node_uuid].append(dev.logical_id[0] + "/" + dev.logical_id[1])
1150 elif dev.dev_type in constants.LDS_DRBD:
1152 self.MapLVsByNode(lvmap, dev.children, dev.logical_id[0])
1153 self.MapLVsByNode(lvmap, dev.children, dev.logical_id[1])
1156 self.MapLVsByNode(lvmap, dev.children, node_uuid)
1160 def FindDisk(self, idx):
1161 """Find a disk given having a specified index.
1163 This is just a wrapper that does validation of the index.
1166 @param idx: the disk index
1168 @return: the corresponding disk
1169 @raise errors.OpPrereqError: when the given index is not valid
1174 return self.disks[idx]
1175 except (TypeError, ValueError), err:
1176 raise errors.OpPrereqError("Invalid disk index: '%s'" % str(err),
1179 raise errors.OpPrereqError("Invalid disk index: %d (instace has disks"
1180 " 0 to %d" % (idx, len(self.disks) - 1),
1184 """Instance-specific conversion to standard python types.
1186 This replaces the children lists of objects with lists of standard
1190 bo = super(Instance, self).ToDict()
1192 for attr in "nics", "disks":
1193 alist = bo.get(attr, None)
1195 nlist = outils.ContainerToDicts(alist)
1202 def FromDict(cls, val):
1203 """Custom function for instances.
1206 if "admin_state" not in val:
1207 if val.get("admin_up", False):
1208 val["admin_state"] = constants.ADMINST_UP
1210 val["admin_state"] = constants.ADMINST_DOWN
1211 if "admin_up" in val:
1213 obj = super(Instance, cls).FromDict(val)
1214 obj.nics = outils.ContainerFromDicts(obj.nics, list, NIC)
1215 obj.disks = outils.ContainerFromDicts(obj.disks, list, Disk)
1218 def UpgradeConfig(self):
1219 """Fill defaults for missing configuration values.
1222 for nic in self.nics:
1224 for disk in self.disks:
1225 disk.UpgradeConfig()
1227 for key in constants.HVC_GLOBALS:
1229 del self.hvparams[key]
1232 if self.osparams is None:
1234 UpgradeBeParams(self.beparams)
1235 if self.disks_active is None:
1236 self.disks_active = self.admin_state == constants.ADMINST_UP
1239 class OS(ConfigObject):
1240 """Config object representing an operating system.
1242 @type supported_parameters: list
1243 @ivar supported_parameters: a list of tuples, name and description,
1244 containing the supported parameters by this OS
1246 @type VARIANT_DELIM: string
1247 @cvar VARIANT_DELIM: the variant delimiter
1259 "supported_variants",
1260 "supported_parameters",
1266 def SplitNameVariant(cls, name):
1267 """Splits the name into the proper name and variant.
1269 @param name: the OS (unprocessed) name
1271 @return: a list of two elements; if the original name didn't
1272 contain a variant, it's returned as an empty string
1275 nv = name.split(cls.VARIANT_DELIM, 1)
1281 def GetName(cls, name):
1282 """Returns the proper name of the os (without the variant).
1284 @param name: the OS (unprocessed) name
1287 return cls.SplitNameVariant(name)[0]
1290 def GetVariant(cls, name):
1291 """Returns the variant the os (without the base name).
1293 @param name: the OS (unprocessed) name
1296 return cls.SplitNameVariant(name)[1]
1299 class ExtStorage(ConfigObject):
1300 """Config object representing an External Storage Provider.
1313 "supported_parameters",
1317 class NodeHvState(ConfigObject):
1318 """Hypvervisor state on a node.
1320 @ivar mem_total: Total amount of memory
1321 @ivar mem_node: Memory used by, or reserved for, the node itself (not always
1323 @ivar mem_hv: Memory used by hypervisor or lost due to instance allocation
1325 @ivar mem_inst: Memory used by instances living on node
1326 @ivar cpu_total: Total node CPU core count
1327 @ivar cpu_node: Number of CPU cores reserved for the node itself
1340 class NodeDiskState(ConfigObject):
1341 """Disk state on a node.
1351 class Node(TaggableObject):
1352 """Config object representing a node.
1354 @ivar hv_state: Hypervisor state (e.g. number of CPUs)
1355 @ivar hv_state_static: Hypervisor state overriden by user
1356 @ivar disk_state: Disk state (e.g. free space)
1357 @ivar disk_state_static: Disk state overriden by user
1376 "disk_state_static",
1377 ] + _TIMESTAMPS + _UUID
1379 def UpgradeConfig(self):
1380 """Fill defaults for missing configuration values.
1383 # pylint: disable=E0203
1384 # because these are "defined" via slots, not manually
1385 if self.master_capable is None:
1386 self.master_capable = True
1388 if self.vm_capable is None:
1389 self.vm_capable = True
1391 if self.ndparams is None:
1393 # And remove any global parameter
1394 for key in constants.NDC_GLOBALS:
1395 if key in self.ndparams:
1396 logging.warning("Ignoring %s node parameter for node %s",
1398 del self.ndparams[key]
1400 if self.powered is None:
1404 """Custom function for serializing.
1407 data = super(Node, self).ToDict()
1409 hv_state = data.get("hv_state", None)
1410 if hv_state is not None:
1411 data["hv_state"] = outils.ContainerToDicts(hv_state)
1413 disk_state = data.get("disk_state", None)
1414 if disk_state is not None:
1415 data["disk_state"] = \
1416 dict((key, outils.ContainerToDicts(value))
1417 for (key, value) in disk_state.items())
1422 def FromDict(cls, val):
1423 """Custom function for deserializing.
1426 obj = super(Node, cls).FromDict(val)
1428 if obj.hv_state is not None:
1430 outils.ContainerFromDicts(obj.hv_state, dict, NodeHvState)
1432 if obj.disk_state is not None:
1434 dict((key, outils.ContainerFromDicts(value, dict, NodeDiskState))
1435 for (key, value) in obj.disk_state.items())
1440 class NodeGroup(TaggableObject):
1441 """Config object representing a node group."""
1450 "disk_state_static",
1453 ] + _TIMESTAMPS + _UUID
1456 """Custom function for nodegroup.
1458 This discards the members object, which gets recalculated and is only kept
1462 mydict = super(NodeGroup, self).ToDict()
1463 del mydict["members"]
1467 def FromDict(cls, val):
1468 """Custom function for nodegroup.
1470 The members slot is initialized to an empty list, upon deserialization.
1473 obj = super(NodeGroup, cls).FromDict(val)
1477 def UpgradeConfig(self):
1478 """Fill defaults for missing configuration values.
1481 if self.ndparams is None:
1484 if self.serial_no is None:
1487 if self.alloc_policy is None:
1488 self.alloc_policy = constants.ALLOC_POLICY_PREFERRED
1490 # We only update mtime, and not ctime, since we would not be able
1491 # to provide a correct value for creation time.
1492 if self.mtime is None:
1493 self.mtime = time.time()
1495 if self.diskparams is None:
1496 self.diskparams = {}
1497 if self.ipolicy is None:
1498 self.ipolicy = MakeEmptyIPolicy()
1500 if self.networks is None:
1503 def FillND(self, node):
1504 """Return filled out ndparams for L{objects.Node}
1506 @type node: L{objects.Node}
1507 @param node: A Node object to fill
1508 @return a copy of the node's ndparams with defaults filled
1511 return self.SimpleFillND(node.ndparams)
1513 def SimpleFillND(self, ndparams):
1514 """Fill a given ndparams dict with defaults.
1516 @type ndparams: dict
1517 @param ndparams: the dict to fill
1519 @return: a copy of the passed in ndparams with missing keys filled
1520 from the node group defaults
1523 return FillDict(self.ndparams, ndparams)
1526 class Cluster(TaggableObject):
1527 """Config object representing the cluster."""
1532 "highest_used_port",
1535 "volume_group_name",
1537 "drbd_usermode_helper",
1539 "default_hypervisor",
1544 "use_external_mip_script",
1547 "shared_file_storage_dir",
1548 "enabled_hypervisors",
1557 "candidate_pool_size",
1560 "maintain_node_health",
1562 "default_iallocator",
1565 "primary_ip_family",
1566 "prealloc_wipe_disks",
1568 "disk_state_static",
1569 "enabled_disk_templates",
1570 ] + _TIMESTAMPS + _UUID
1572 def UpgradeConfig(self):
1573 """Fill defaults for missing configuration values.
1576 # pylint: disable=E0203
1577 # because these are "defined" via slots, not manually
1578 if self.hvparams is None:
1579 self.hvparams = constants.HVC_DEFAULTS
1581 for hypervisor in self.hvparams:
1582 self.hvparams[hypervisor] = FillDict(
1583 constants.HVC_DEFAULTS[hypervisor], self.hvparams[hypervisor])
1585 if self.os_hvp is None:
1588 # osparams added before 2.2
1589 if self.osparams is None:
1592 self.ndparams = UpgradeNDParams(self.ndparams)
1594 self.beparams = UpgradeGroupedParams(self.beparams,
1595 constants.BEC_DEFAULTS)
1596 for beparams_group in self.beparams:
1597 UpgradeBeParams(self.beparams[beparams_group])
1599 migrate_default_bridge = not self.nicparams
1600 self.nicparams = UpgradeGroupedParams(self.nicparams,
1601 constants.NICC_DEFAULTS)
1602 if migrate_default_bridge:
1603 self.nicparams[constants.PP_DEFAULT][constants.NIC_LINK] = \
1606 if self.modify_etc_hosts is None:
1607 self.modify_etc_hosts = True
1609 if self.modify_ssh_setup is None:
1610 self.modify_ssh_setup = True
1612 # default_bridge is no longer used in 2.1. The slot is left there to
1613 # support auto-upgrading. It can be removed once we decide to deprecate
1614 # upgrading straight from 2.0.
1615 if self.default_bridge is not None:
1616 self.default_bridge = None
1618 # default_hypervisor is just the first enabled one in 2.1. This slot and
1619 # code can be removed once upgrading straight from 2.0 is deprecated.
1620 if self.default_hypervisor is not None:
1621 self.enabled_hypervisors = ([self.default_hypervisor] +
1622 [hvname for hvname in self.enabled_hypervisors
1623 if hvname != self.default_hypervisor])
1624 self.default_hypervisor = None
1626 # maintain_node_health added after 2.1.1
1627 if self.maintain_node_health is None:
1628 self.maintain_node_health = False
1630 if self.uid_pool is None:
1633 if self.default_iallocator is None:
1634 self.default_iallocator = ""
1636 # reserved_lvs added before 2.2
1637 if self.reserved_lvs is None:
1638 self.reserved_lvs = []
1640 # hidden and blacklisted operating systems added before 2.2.1
1641 if self.hidden_os is None:
1644 if self.blacklisted_os is None:
1645 self.blacklisted_os = []
1647 # primary_ip_family added before 2.3
1648 if self.primary_ip_family is None:
1649 self.primary_ip_family = AF_INET
1651 if self.master_netmask is None:
1652 ipcls = netutils.IPAddress.GetClassFromIpFamily(self.primary_ip_family)
1653 self.master_netmask = ipcls.iplen
1655 if self.prealloc_wipe_disks is None:
1656 self.prealloc_wipe_disks = False
1658 # shared_file_storage_dir added before 2.5
1659 if self.shared_file_storage_dir is None:
1660 self.shared_file_storage_dir = ""
1662 if self.use_external_mip_script is None:
1663 self.use_external_mip_script = False
1666 self.diskparams = UpgradeDiskParams(self.diskparams)
1668 self.diskparams = constants.DISK_DT_DEFAULTS.copy()
1670 # instance policy added before 2.6
1671 if self.ipolicy is None:
1672 self.ipolicy = FillIPolicy(constants.IPOLICY_DEFAULTS, {})
1674 # we can either make sure to upgrade the ipolicy always, or only
1675 # do it in some corner cases (e.g. missing keys); note that this
1676 # will break any removal of keys from the ipolicy dict
1677 wrongkeys = frozenset(self.ipolicy.keys()) - constants.IPOLICY_ALL_KEYS
1679 # These keys would be silently removed by FillIPolicy()
1680 msg = ("Cluster instance policy contains spurious keys: %s" %
1681 utils.CommaJoin(wrongkeys))
1682 raise errors.ConfigurationError(msg)
1683 self.ipolicy = FillIPolicy(constants.IPOLICY_DEFAULTS, self.ipolicy)
1686 def primary_hypervisor(self):
1687 """The first hypervisor is the primary.
1689 Useful, for example, for L{Node}'s hv/disk state.
1692 return self.enabled_hypervisors[0]
1695 """Custom function for cluster.
1698 mydict = super(Cluster, self).ToDict()
1700 if self.tcpudp_port_pool is None:
1701 tcpudp_port_pool = []
1703 tcpudp_port_pool = list(self.tcpudp_port_pool)
1705 mydict["tcpudp_port_pool"] = tcpudp_port_pool
1710 def FromDict(cls, val):
1711 """Custom function for cluster.
1714 obj = super(Cluster, cls).FromDict(val)
1716 if obj.tcpudp_port_pool is None:
1717 obj.tcpudp_port_pool = set()
1718 elif not isinstance(obj.tcpudp_port_pool, set):
1719 obj.tcpudp_port_pool = set(obj.tcpudp_port_pool)
1723 def SimpleFillDP(self, diskparams):
1724 """Fill a given diskparams dict with cluster defaults.
1726 @param diskparams: The diskparams
1727 @return: The defaults dict
1730 return FillDiskParams(self.diskparams, diskparams)
1732 def GetHVDefaults(self, hypervisor, os_name=None, skip_keys=None):
1733 """Get the default hypervisor parameters for the cluster.
1735 @param hypervisor: the hypervisor name
1736 @param os_name: if specified, we'll also update the defaults for this OS
1737 @param skip_keys: if passed, list of keys not to use
1738 @return: the defaults dict
1741 if skip_keys is None:
1744 fill_stack = [self.hvparams.get(hypervisor, {})]
1745 if os_name is not None:
1746 os_hvp = self.os_hvp.get(os_name, {}).get(hypervisor, {})
1747 fill_stack.append(os_hvp)
1750 for o_dict in fill_stack:
1751 ret_dict = FillDict(ret_dict, o_dict, skip_keys=skip_keys)
1755 def SimpleFillHV(self, hv_name, os_name, hvparams, skip_globals=False):
1756 """Fill a given hvparams dict with cluster defaults.
1758 @type hv_name: string
1759 @param hv_name: the hypervisor to use
1760 @type os_name: string
1761 @param os_name: the OS to use for overriding the hypervisor defaults
1762 @type skip_globals: boolean
1763 @param skip_globals: if True, the global hypervisor parameters will
1766 @return: a copy of the given hvparams with missing keys filled from
1767 the cluster defaults
1771 skip_keys = constants.HVC_GLOBALS
1775 def_dict = self.GetHVDefaults(hv_name, os_name, skip_keys=skip_keys)
1776 return FillDict(def_dict, hvparams, skip_keys=skip_keys)
1778 def FillHV(self, instance, skip_globals=False):
1779 """Fill an instance's hvparams dict with cluster defaults.
1781 @type instance: L{objects.Instance}
1782 @param instance: the instance parameter to fill
1783 @type skip_globals: boolean
1784 @param skip_globals: if True, the global hypervisor parameters will
1787 @return: a copy of the instance's hvparams with missing keys filled from
1788 the cluster defaults
1791 return self.SimpleFillHV(instance.hypervisor, instance.os,
1792 instance.hvparams, skip_globals)
1794 def SimpleFillBE(self, beparams):
1795 """Fill a given beparams dict with cluster defaults.
1797 @type beparams: dict
1798 @param beparams: the dict to fill
1800 @return: a copy of the passed in beparams with missing keys filled
1801 from the cluster defaults
1804 return FillDict(self.beparams.get(constants.PP_DEFAULT, {}), beparams)
1806 def FillBE(self, instance):
1807 """Fill an instance's beparams dict with cluster defaults.
1809 @type instance: L{objects.Instance}
1810 @param instance: the instance parameter to fill
1812 @return: a copy of the instance's beparams with missing keys filled from
1813 the cluster defaults
1816 return self.SimpleFillBE(instance.beparams)
1818 def SimpleFillNIC(self, nicparams):
1819 """Fill a given nicparams dict with cluster defaults.
1821 @type nicparams: dict
1822 @param nicparams: the dict to fill
1824 @return: a copy of the passed in nicparams with missing keys filled
1825 from the cluster defaults
1828 return FillDict(self.nicparams.get(constants.PP_DEFAULT, {}), nicparams)
1830 def SimpleFillOS(self, os_name, os_params):
1831 """Fill an instance's osparams dict with cluster defaults.
1833 @type os_name: string
1834 @param os_name: the OS name to use
1835 @type os_params: dict
1836 @param os_params: the dict to fill with default values
1838 @return: a copy of the instance's osparams with missing keys filled from
1839 the cluster defaults
1842 name_only = os_name.split("+", 1)[0]
1844 result = self.osparams.get(name_only, {})
1846 result = FillDict(result, self.osparams.get(os_name, {}))
1848 return FillDict(result, os_params)
1851 def SimpleFillHvState(hv_state):
1852 """Fill an hv_state sub dict with cluster defaults.
1855 return FillDict(constants.HVST_DEFAULTS, hv_state)
1858 def SimpleFillDiskState(disk_state):
1859 """Fill an disk_state sub dict with cluster defaults.
1862 return FillDict(constants.DS_DEFAULTS, disk_state)
1864 def FillND(self, node, nodegroup):
1865 """Return filled out ndparams for L{objects.NodeGroup} and L{objects.Node}
1867 @type node: L{objects.Node}
1868 @param node: A Node object to fill
1869 @type nodegroup: L{objects.NodeGroup}
1870 @param nodegroup: A Node object to fill
1871 @return a copy of the node's ndparams with defaults filled
1874 return self.SimpleFillND(nodegroup.FillND(node))
1876 def SimpleFillND(self, ndparams):
1877 """Fill a given ndparams dict with defaults.
1879 @type ndparams: dict
1880 @param ndparams: the dict to fill
1882 @return: a copy of the passed in ndparams with missing keys filled
1883 from the cluster defaults
1886 return FillDict(self.ndparams, ndparams)
1888 def SimpleFillIPolicy(self, ipolicy):
1889 """ Fill instance policy dict with defaults.
1892 @param ipolicy: the dict to fill
1894 @return: a copy of passed ipolicy with missing keys filled from
1895 the cluster defaults
1898 return FillIPolicy(self.ipolicy, ipolicy)
1900 def IsDiskTemplateEnabled(self, disk_template):
1901 """Checks if a particular disk template is enabled.
1904 return utils.storage.IsDiskTemplateEnabled(
1905 disk_template, self.enabled_disk_templates)
1907 def IsFileStorageEnabled(self):
1908 """Checks if file storage is enabled.
1911 return utils.storage.IsFileStorageEnabled(self.enabled_disk_templates)
1913 def IsSharedFileStorageEnabled(self):
1914 """Checks if shared file storage is enabled.
1917 return utils.storage.IsSharedFileStorageEnabled(
1918 self.enabled_disk_templates)
1921 class BlockDevStatus(ConfigObject):
1922 """Config object representing the status of a block device."""
1934 class ImportExportStatus(ConfigObject):
1935 """Config object representing the status of an import or export."""
1941 "progress_throughput",
1949 class ImportExportOptions(ConfigObject):
1950 """Options for import/export daemon
1952 @ivar key_name: X509 key name (None for cluster certificate)
1953 @ivar ca_pem: Remote peer CA in PEM format (None for cluster certificate)
1954 @ivar compress: Compression method (one of L{constants.IEC_ALL})
1955 @ivar magic: Used to ensure the connection goes to the right disk
1956 @ivar ipv6: Whether to use IPv6
1957 @ivar connect_timeout: Number of seconds for establishing connection
1970 class ConfdRequest(ConfigObject):
1971 """Object holding a confd request.
1973 @ivar protocol: confd protocol version
1974 @ivar type: confd query type
1975 @ivar query: query request
1976 @ivar rsalt: requested reply salt
1987 class ConfdReply(ConfigObject):
1988 """Object holding a confd reply.
1990 @ivar protocol: confd protocol version
1991 @ivar status: reply status code (ok, error)
1992 @ivar answer: confd query reply
1993 @ivar serial: configuration serial number
2004 class QueryFieldDefinition(ConfigObject):
2005 """Object holding a query field definition.
2007 @ivar name: Field name
2008 @ivar title: Human-readable title
2009 @ivar kind: Field type
2010 @ivar doc: Human-readable description
2021 class _QueryResponseBase(ConfigObject):
2027 """Custom function for serializing.
2030 mydict = super(_QueryResponseBase, self).ToDict()
2031 mydict["fields"] = outils.ContainerToDicts(mydict["fields"])
2035 def FromDict(cls, val):
2036 """Custom function for de-serializing.
2039 obj = super(_QueryResponseBase, cls).FromDict(val)
2041 outils.ContainerFromDicts(obj.fields, list, QueryFieldDefinition)
2045 class QueryResponse(_QueryResponseBase):
2046 """Object holding the response to a query.
2048 @ivar fields: List of L{QueryFieldDefinition} objects
2049 @ivar data: Requested data
2057 class QueryFieldsRequest(ConfigObject):
2058 """Object holding a request for querying available fields.
2067 class QueryFieldsResponse(_QueryResponseBase):
2068 """Object holding the response to a query for fields.
2070 @ivar fields: List of L{QueryFieldDefinition} objects
2076 class MigrationStatus(ConfigObject):
2077 """Object holding the status of a migration.
2087 class InstanceConsole(ConfigObject):
2088 """Object describing how to access the console of an instance.
2103 """Validates contents of this object.
2106 assert self.kind in constants.CONS_ALL, "Unknown console type"
2107 assert self.instance, "Missing instance name"
2108 assert self.message or self.kind in [constants.CONS_SSH,
2109 constants.CONS_SPICE,
2111 assert self.host or self.kind == constants.CONS_MESSAGE
2112 assert self.port or self.kind in [constants.CONS_MESSAGE,
2114 assert self.user or self.kind in [constants.CONS_MESSAGE,
2115 constants.CONS_SPICE,
2117 assert self.command or self.kind in [constants.CONS_MESSAGE,
2118 constants.CONS_SPICE,
2120 assert self.display or self.kind in [constants.CONS_MESSAGE,
2121 constants.CONS_SPICE,
2126 class Network(TaggableObject):
2127 """Object representing a network definition for ganeti.
2140 ] + _TIMESTAMPS + _UUID
2142 def HooksDict(self, prefix=""):
2143 """Export a dictionary used by hooks with a network's information.
2145 @type prefix: String
2146 @param prefix: Prefix to prepend to the dict entries
2150 "%sNETWORK_NAME" % prefix: self.name,
2151 "%sNETWORK_UUID" % prefix: self.uuid,
2152 "%sNETWORK_TAGS" % prefix: " ".join(self.GetTags()),
2155 result["%sNETWORK_SUBNET" % prefix] = self.network
2157 result["%sNETWORK_GATEWAY" % prefix] = self.gateway
2159 result["%sNETWORK_SUBNET6" % prefix] = self.network6
2161 result["%sNETWORK_GATEWAY6" % prefix] = self.gateway6
2163 result["%sNETWORK_MAC_PREFIX" % prefix] = self.mac_prefix
2168 def FromDict(cls, val):
2169 """Custom function for networks.
2171 Remove deprecated network_type and family.
2174 if "network_type" in val:
2175 del val["network_type"]
2178 obj = super(Network, cls).FromDict(val)
2182 class SerializableConfigParser(ConfigParser.SafeConfigParser):
2183 """Simple wrapper over ConfigParse that allows serialization.
2185 This class is basically ConfigParser.SafeConfigParser with two
2186 additional methods that allow it to serialize/unserialize to/from a
2191 """Dump this instance and return the string representation."""
2194 return buf.getvalue()
2197 def Loads(cls, data):
2198 """Load data from a string."""
2199 buf = StringIO(data)
2205 class LvmPvInfo(ConfigObject):
2206 """Information about an LVM physical volume (PV).
2209 @ivar name: name of the PV
2210 @type vg_name: string
2211 @ivar vg_name: name of the volume group containing the PV
2213 @ivar size: size of the PV in MiB
2215 @ivar free: free space in the PV, in MiB
2216 @type attributes: string
2217 @ivar attributes: PV attributes
2218 @type lv_list: list of strings
2219 @ivar lv_list: names of the LVs hosted on the PV
2231 """Is this PV empty?
2234 return self.size <= (self.free + 1)
2236 def IsAllocatable(self):
2237 """Is this PV allocatable?
2240 return ("a" in self.attributes)