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(r"^[\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.DTS_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 self._UpgradeEnabledDiskTemplates()
447 if self.nodegroups is None:
449 for nodegroup in self.nodegroups.values():
450 nodegroup.UpgradeConfig()
451 InstancePolicy.UpgradeDiskTemplates(
452 nodegroup.ipolicy, self.cluster.enabled_disk_templates)
453 if self.cluster.drbd_usermode_helper is None:
454 if self.cluster.IsDiskTemplateEnabled(constants.DT_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()
461 def _UpgradeEnabledDiskTemplates(self):
462 """Upgrade the cluster's enabled disk templates by inspecting the currently
463 enabled and/or used disk templates.
466 if not self.cluster.enabled_disk_templates:
468 set([inst.disk_template for inst in self.instances.values()])
469 # Add drbd and plain, if lvm is enabled (by specifying a volume group)
470 if self.cluster.volume_group_name:
471 template_set.add(constants.DT_DRBD8)
472 template_set.add(constants.DT_PLAIN)
473 # Set enabled_disk_templates to the inferred disk templates. Order them
474 # according to a preference list that is based on Ganeti's history of
475 # supported disk templates.
476 self.cluster.enabled_disk_templates = []
477 for preferred_template in constants.DISK_TEMPLATE_PREFERENCE:
478 if preferred_template in template_set:
479 self.cluster.enabled_disk_templates.append(preferred_template)
480 template_set.remove(preferred_template)
481 self.cluster.enabled_disk_templates.extend(list(template_set))
482 InstancePolicy.UpgradeDiskTemplates(
483 self.cluster.ipolicy, self.cluster.enabled_disk_templates)
486 class NIC(ConfigObject):
487 """Config object representing a network card."""
488 __slots__ = ["name", "mac", "ip", "network",
489 "nicparams", "netinfo", "pci"] + _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", "children", "iv_name",
513 "size", "mode", "params", "spindles", "pci"] + _UUID +
514 # dynamic_params is special. It depends on the node this instance
515 # is sent to, and should not be persisted.
518 def CreateOnSecondary(self):
519 """Test if this device needs to be created on a secondary node."""
520 return self.dev_type in (constants.DT_DRBD8, constants.DT_PLAIN)
522 def AssembleOnSecondary(self):
523 """Test if this device needs to be assembled on a secondary node."""
524 return self.dev_type in (constants.DT_DRBD8, constants.DT_PLAIN)
526 def OpenOnSecondary(self):
527 """Test if this device needs to be opened on a secondary node."""
528 return self.dev_type in (constants.DT_PLAIN,)
530 def StaticDevPath(self):
531 """Return the device path if this device type has a static one.
533 Some devices (LVM for example) live always at the same /dev/ path,
534 irrespective of their status. For such devices, we return this
535 path, for others we return None.
537 @warning: The path returned is not a normalized pathname; callers
538 should check that it is a valid path.
541 if self.dev_type == constants.DT_PLAIN:
542 return "/dev/%s/%s" % (self.logical_id[0], self.logical_id[1])
543 elif self.dev_type == constants.DT_BLOCK:
544 return self.logical_id[1]
545 elif self.dev_type == constants.DT_RBD:
546 return "/dev/%s/%s" % (self.logical_id[0], self.logical_id[1])
549 def ChildrenNeeded(self):
550 """Compute the needed number of children for activation.
552 This method will return either -1 (all children) or a positive
553 number denoting the minimum number of children needed for
554 activation (only mirrored devices will usually return >=0).
556 Currently, only DRBD8 supports diskless activation (therefore we
557 return 0), for all other we keep the previous semantics and return
561 if self.dev_type == constants.DT_DRBD8:
565 def IsBasedOnDiskType(self, dev_type):
566 """Check if the disk or its children are based on the given type.
568 @type dev_type: L{constants.DTS_BLOCK}
569 @param dev_type: the type to look for
571 @return: boolean indicating if a device of the given type was found or not
575 for child in self.children:
576 if child.IsBasedOnDiskType(dev_type):
578 return self.dev_type == dev_type
580 def GetNodes(self, node_uuid):
581 """This function returns the nodes this device lives on.
583 Given the node on which the parent of the device lives on (or, in
584 case of a top-level device, the primary node of the devices'
585 instance), this function will return a list of nodes on which this
586 devices needs to (or can) be assembled.
589 if self.dev_type in [constants.DT_PLAIN, constants.DT_FILE,
590 constants.DT_BLOCK, constants.DT_RBD,
591 constants.DT_EXT, constants.DT_SHARED_FILE]:
593 elif self.dev_type in constants.DTS_DRBD:
594 result = [self.logical_id[0], self.logical_id[1]]
595 if node_uuid not in result:
596 raise errors.ConfigurationError("DRBD device passed unknown node")
598 raise errors.ProgrammerError("Unhandled device type %s" % self.dev_type)
601 def ComputeNodeTree(self, parent_node_uuid):
602 """Compute the node/disk tree for this disk and its children.
604 This method, given the node on which the parent disk lives, will
605 return the list of all (node UUID, disk) pairs which describe the disk
606 tree in the most compact way. For example, a drbd/lvm stack
607 will be returned as (primary_node, drbd) and (secondary_node, drbd)
608 which represents all the top-level devices on the nodes.
611 my_nodes = self.GetNodes(parent_node_uuid)
612 result = [(node, self) for node in my_nodes]
613 if not self.children:
616 for node in my_nodes:
617 for child in self.children:
618 child_result = child.ComputeNodeTree(node)
619 if len(child_result) == 1:
620 # child (and all its descendants) is simple, doesn't split
621 # over multiple hosts, so we don't need to describe it, our
622 # own entry for this node describes it completely
625 # check if child nodes differ from my nodes; note that
626 # subdisk can differ from the child itself, and be instead
627 # one of its descendants
628 for subnode, subdisk in child_result:
629 if subnode not in my_nodes:
630 result.append((subnode, subdisk))
631 # otherwise child is under our own node, so we ignore this
632 # entry (but probably the other results in the list will
636 def ComputeGrowth(self, amount):
637 """Compute the per-VG growth requirements.
639 This only works for VG-based disks.
641 @type amount: integer
642 @param amount: the desired increase in (user-visible) disk space
644 @return: a dictionary of volume-groups and the required size
647 if self.dev_type == constants.DT_PLAIN:
648 return {self.logical_id[0]: amount}
649 elif self.dev_type == constants.DT_DRBD8:
651 return self.children[0].ComputeGrowth(amount)
655 # Other disk types do not require VG space
658 def RecordGrow(self, amount):
659 """Update the size of this disk after growth.
661 This method recurses over the disks's children and updates their
662 size correspondigly. The method needs to be kept in sync with the
663 actual algorithms from bdev.
666 if self.dev_type in (constants.DT_PLAIN, constants.DT_FILE,
667 constants.DT_RBD, constants.DT_EXT,
668 constants.DT_SHARED_FILE):
670 elif self.dev_type == constants.DT_DRBD8:
672 self.children[0].RecordGrow(amount)
675 raise errors.ProgrammerError("Disk.RecordGrow called for unsupported"
676 " disk type %s" % self.dev_type)
678 def Update(self, size=None, mode=None, spindles=None):
679 """Apply changes to size, spindles and mode.
682 if self.dev_type == constants.DT_DRBD8:
684 self.children[0].Update(size=size, mode=mode)
686 assert not self.children
692 if spindles is not None:
693 self.spindles = spindles
696 """Sets recursively the size to zero for the disk and its children.
700 for child in self.children:
704 def UpdateDynamicDiskParams(self, target_node_uuid, nodes_ip):
705 """Updates the dynamic disk params for the given node.
707 This is mainly used for drbd, which needs ip/port configuration.
710 - target_node_uuid: the node UUID we wish to configure for
711 - nodes_ip: a mapping of node name to ip
713 The target_node must exist in nodes_ip, and should be one of the
714 nodes in the logical ID if this device is a DRBD device.
718 for child in self.children:
719 child.UpdateDynamicDiskParams(target_node_uuid, nodes_ip)
722 if self.logical_id is not None and self.dev_type in constants.DTS_DRBD:
723 pnode_uuid, snode_uuid, _, pminor, sminor, _ = self.logical_id
724 if target_node_uuid not in (pnode_uuid, snode_uuid):
725 # disk object is being sent to neither the primary nor the secondary
726 # node. reset the dynamic parameters, the target node is not
727 # supposed to use them.
728 self.dynamic_params = dyn_disk_params
731 pnode_ip = nodes_ip.get(pnode_uuid, None)
732 snode_ip = nodes_ip.get(snode_uuid, None)
733 if pnode_ip is None or snode_ip is None:
734 raise errors.ConfigurationError("Can't find primary or secondary node"
735 " for %s" % str(self))
736 if pnode_uuid == target_node_uuid:
737 dyn_disk_params[constants.DDP_LOCAL_IP] = pnode_ip
738 dyn_disk_params[constants.DDP_REMOTE_IP] = snode_ip
739 dyn_disk_params[constants.DDP_LOCAL_MINOR] = pminor
740 dyn_disk_params[constants.DDP_REMOTE_MINOR] = sminor
741 else: # it must be secondary, we tested above
742 dyn_disk_params[constants.DDP_LOCAL_IP] = snode_ip
743 dyn_disk_params[constants.DDP_REMOTE_IP] = pnode_ip
744 dyn_disk_params[constants.DDP_LOCAL_MINOR] = sminor
745 dyn_disk_params[constants.DDP_REMOTE_MINOR] = pminor
747 self.dynamic_params = dyn_disk_params
749 # pylint: disable=W0221
750 def ToDict(self, include_dynamic_params=False):
751 """Disk-specific conversion to standard python types.
753 This replaces the children lists of objects with lists of
754 standard python types.
757 bo = super(Disk, self).ToDict()
758 if not include_dynamic_params and "dynamic_params" in bo:
759 del bo["dynamic_params"]
761 for attr in ("children",):
762 alist = bo.get(attr, None)
764 bo[attr] = outils.ContainerToDicts(alist)
768 def FromDict(cls, val):
769 """Custom function for Disks
772 obj = super(Disk, cls).FromDict(val)
774 obj.children = outils.ContainerFromDicts(obj.children, list, Disk)
775 if obj.logical_id and isinstance(obj.logical_id, list):
776 obj.logical_id = tuple(obj.logical_id)
777 if obj.dev_type in constants.DTS_DRBD:
778 # we need a tuple of length six here
779 if len(obj.logical_id) < 6:
780 obj.logical_id += (None,) * (6 - len(obj.logical_id))
784 """Custom str() formatter for disks.
787 if self.dev_type == constants.DT_PLAIN:
788 val = "<LogicalVolume(/dev/%s/%s" % self.logical_id
789 elif self.dev_type in constants.DTS_DRBD:
790 node_a, node_b, port, minor_a, minor_b = self.logical_id[:5]
793 val += ("hosts=%s/%d-%s/%d, port=%s, " %
794 (node_a, minor_a, node_b, minor_b, port))
795 if self.children and self.children.count(None) == 0:
796 val += "backend=%s, metadev=%s" % (self.children[0], self.children[1])
798 val += "no local storage"
800 val = ("<Disk(type=%s, logical_id=%s, children=%s" %
801 (self.dev_type, self.logical_id, self.children))
802 if self.iv_name is None:
803 val += ", not visible"
805 val += ", visible as /dev/%s" % self.iv_name
806 if self.spindles is not None:
807 val += ", spindles=%s" % self.spindles
808 if isinstance(self.size, int):
809 val += ", size=%dm)>" % self.size
811 val += ", size='%s')>" % (self.size,)
815 """Checks that this disk is correctly configured.
819 if self.mode not in constants.DISK_ACCESS_SET:
820 all_errors.append("Disk access mode '%s' is invalid" % (self.mode, ))
823 def UpgradeConfig(self):
824 """Fill defaults for missing configuration values.
828 for child in self.children:
829 child.UpgradeConfig()
831 # FIXME: Make this configurable in Ganeti 2.7
833 # add here config upgrade for this disk
835 # map of legacy device types (mapping differing LD constants to new
837 LEG_DEV_TYPE_MAP = {"lvm": constants.DT_PLAIN, "drbd8": constants.DT_DRBD8}
838 if self.dev_type in LEG_DEV_TYPE_MAP:
839 self.dev_type = LEG_DEV_TYPE_MAP[self.dev_type]
842 def ComputeLDParams(disk_template, disk_params):
843 """Computes Logical Disk parameters from Disk Template parameters.
845 @type disk_template: string
846 @param disk_template: disk template, one of L{constants.DISK_TEMPLATES}
847 @type disk_params: dict
848 @param disk_params: disk template parameters;
849 dict(template_name -> parameters
851 @return: a list of dicts, one for each node of the disk hierarchy. Each dict
852 contains the LD parameters of the node. The tree is flattened in-order.
855 if disk_template not in constants.DISK_TEMPLATES:
856 raise errors.ProgrammerError("Unknown disk template %s" % disk_template)
858 assert disk_template in disk_params
861 dt_params = disk_params[disk_template]
862 if disk_template == constants.DT_DRBD8:
863 result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.DT_DRBD8], {
864 constants.LDP_RESYNC_RATE: dt_params[constants.DRBD_RESYNC_RATE],
865 constants.LDP_BARRIERS: dt_params[constants.DRBD_DISK_BARRIERS],
866 constants.LDP_NO_META_FLUSH: dt_params[constants.DRBD_META_BARRIERS],
867 constants.LDP_DEFAULT_METAVG: dt_params[constants.DRBD_DEFAULT_METAVG],
868 constants.LDP_DISK_CUSTOM: dt_params[constants.DRBD_DISK_CUSTOM],
869 constants.LDP_NET_CUSTOM: dt_params[constants.DRBD_NET_CUSTOM],
870 constants.LDP_PROTOCOL: dt_params[constants.DRBD_PROTOCOL],
871 constants.LDP_DYNAMIC_RESYNC: dt_params[constants.DRBD_DYNAMIC_RESYNC],
872 constants.LDP_PLAN_AHEAD: dt_params[constants.DRBD_PLAN_AHEAD],
873 constants.LDP_FILL_TARGET: dt_params[constants.DRBD_FILL_TARGET],
874 constants.LDP_DELAY_TARGET: dt_params[constants.DRBD_DELAY_TARGET],
875 constants.LDP_MAX_RATE: dt_params[constants.DRBD_MAX_RATE],
876 constants.LDP_MIN_RATE: dt_params[constants.DRBD_MIN_RATE],
880 result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.DT_PLAIN], {
881 constants.LDP_STRIPES: dt_params[constants.DRBD_DATA_STRIPES],
885 result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.DT_PLAIN], {
886 constants.LDP_STRIPES: dt_params[constants.DRBD_META_STRIPES],
889 elif disk_template in (constants.DT_FILE, constants.DT_SHARED_FILE):
890 result.append(constants.DISK_LD_DEFAULTS[disk_template])
892 elif disk_template == constants.DT_PLAIN:
893 result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.DT_PLAIN], {
894 constants.LDP_STRIPES: dt_params[constants.LV_STRIPES],
897 elif disk_template == constants.DT_BLOCK:
898 result.append(constants.DISK_LD_DEFAULTS[constants.DT_BLOCK])
900 elif disk_template == constants.DT_RBD:
901 result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.DT_RBD], {
902 constants.LDP_POOL: dt_params[constants.RBD_POOL],
903 constants.LDP_ACCESS: dt_params[constants.RBD_ACCESS],
906 elif disk_template == constants.DT_EXT:
907 result.append(constants.DISK_LD_DEFAULTS[constants.DT_EXT])
912 class InstancePolicy(ConfigObject):
913 """Config object representing instance policy limits dictionary.
915 Note that this object is not actually used in the config, it's just
916 used as a placeholder for a few functions.
920 def UpgradeDiskTemplates(cls, ipolicy, enabled_disk_templates):
921 """Upgrades the ipolicy configuration."""
922 if constants.IPOLICY_DTS in ipolicy:
923 if not set(ipolicy[constants.IPOLICY_DTS]).issubset(
924 set(enabled_disk_templates)):
925 ipolicy[constants.IPOLICY_DTS] = list(
926 set(ipolicy[constants.IPOLICY_DTS]) & set(enabled_disk_templates))
929 def CheckParameterSyntax(cls, ipolicy, check_std):
930 """ Check the instance policy for validity.
933 @param ipolicy: dictionary with min/max/std specs and policies
934 @type check_std: bool
935 @param check_std: Whether to check std value or just assume compliance
936 @raise errors.ConfigurationError: when the policy is not legal
939 InstancePolicy.CheckISpecSyntax(ipolicy, check_std)
940 if constants.IPOLICY_DTS in ipolicy:
941 InstancePolicy.CheckDiskTemplates(ipolicy[constants.IPOLICY_DTS])
942 for key in constants.IPOLICY_PARAMETERS:
944 InstancePolicy.CheckParameter(key, ipolicy[key])
945 wrong_keys = frozenset(ipolicy.keys()) - constants.IPOLICY_ALL_KEYS
947 raise errors.ConfigurationError("Invalid keys in ipolicy: %s" %
948 utils.CommaJoin(wrong_keys))
951 def _CheckIncompleteSpec(cls, spec, keyname):
952 missing_params = constants.ISPECS_PARAMETERS - frozenset(spec.keys())
954 msg = ("Missing instance specs parameters for %s: %s" %
955 (keyname, utils.CommaJoin(missing_params)))
956 raise errors.ConfigurationError(msg)
959 def CheckISpecSyntax(cls, ipolicy, check_std):
960 """Check the instance policy specs for validity.
963 @param ipolicy: dictionary with min/max/std specs
964 @type check_std: bool
965 @param check_std: Whether to check std value or just assume compliance
966 @raise errors.ConfigurationError: when specs are not valid
969 if constants.ISPECS_MINMAX not in ipolicy:
973 if check_std and constants.ISPECS_STD not in ipolicy:
974 msg = "Missing key in ipolicy: %s" % constants.ISPECS_STD
975 raise errors.ConfigurationError(msg)
976 stdspec = ipolicy.get(constants.ISPECS_STD)
978 InstancePolicy._CheckIncompleteSpec(stdspec, constants.ISPECS_STD)
980 if not ipolicy[constants.ISPECS_MINMAX]:
981 raise errors.ConfigurationError("Empty minmax specifications")
983 for minmaxspecs in ipolicy[constants.ISPECS_MINMAX]:
984 missing = constants.ISPECS_MINMAX_KEYS - frozenset(minmaxspecs.keys())
986 msg = "Missing instance specification: %s" % utils.CommaJoin(missing)
987 raise errors.ConfigurationError(msg)
988 for (key, spec) in minmaxspecs.items():
989 InstancePolicy._CheckIncompleteSpec(spec, key)
992 for param in constants.ISPECS_PARAMETERS:
993 par_std_ok = InstancePolicy._CheckISpecParamSyntax(minmaxspecs, stdspec,
995 spec_std_ok = spec_std_ok and par_std_ok
996 std_is_good = std_is_good or spec_std_ok
998 raise errors.ConfigurationError("Invalid std specifications")
1001 def _CheckISpecParamSyntax(cls, minmaxspecs, stdspec, name, check_std):
1002 """Check the instance policy specs for validity on a given key.
1004 We check if the instance specs makes sense for a given key, that is
1005 if minmaxspecs[min][name] <= stdspec[name] <= minmaxspec[max][name].
1007 @type minmaxspecs: dict
1008 @param minmaxspecs: dictionary with min and max instance spec
1010 @param stdspec: dictionary with standard instance spec
1012 @param name: what are the limits for
1013 @type check_std: bool
1014 @param check_std: Whether to check std value or just assume compliance
1016 @return: C{True} when specs are valid, C{False} when standard spec for the
1017 given name is not valid
1018 @raise errors.ConfigurationError: when min/max specs for the given name
1022 minspec = minmaxspecs[constants.ISPECS_MIN]
1023 maxspec = minmaxspecs[constants.ISPECS_MAX]
1024 min_v = minspec[name]
1025 max_v = maxspec[name]
1028 err = ("Invalid specification of min/max values for %s: %s/%s" %
1029 (name, min_v, max_v))
1030 raise errors.ConfigurationError(err)
1032 std_v = stdspec.get(name, min_v)
1033 return std_v >= min_v and std_v <= max_v
1038 def CheckDiskTemplates(cls, disk_templates):
1039 """Checks the disk templates for validity.
1042 if not disk_templates:
1043 raise errors.ConfigurationError("Instance policy must contain" +
1044 " at least one disk template")
1045 wrong = frozenset(disk_templates).difference(constants.DISK_TEMPLATES)
1047 raise errors.ConfigurationError("Invalid disk template(s) %s" %
1048 utils.CommaJoin(wrong))
1051 def CheckParameter(cls, key, value):
1052 """Checks a parameter.
1054 Currently we expect all parameters to be float values.
1059 except (TypeError, ValueError), err:
1060 raise errors.ConfigurationError("Invalid value for key" " '%s':"
1061 " '%s', error: %s" % (key, value, err))
1064 class Instance(TaggableObject):
1065 """Config object representing an instance."""
1081 ] + _TIMESTAMPS + _UUID
1083 def _ComputeSecondaryNodes(self):
1084 """Compute the list of secondary nodes.
1086 This is a simple wrapper over _ComputeAllNodes.
1089 all_nodes = set(self._ComputeAllNodes())
1090 all_nodes.discard(self.primary_node)
1091 return tuple(all_nodes)
1093 secondary_nodes = property(_ComputeSecondaryNodes, None, None,
1094 "List of names of secondary nodes")
1096 def _ComputeAllNodes(self):
1097 """Compute the list of all nodes.
1099 Since the data is already there (in the drbd disks), keeping it as
1100 a separate normal attribute is redundant and if not properly
1101 synchronised can cause problems. Thus it's better to compute it
1105 def _Helper(nodes, device):
1106 """Recursively computes nodes given a top device."""
1107 if device.dev_type in constants.DTS_DRBD:
1108 nodea, nodeb = device.logical_id[:2]
1112 for child in device.children:
1113 _Helper(nodes, child)
1116 all_nodes.add(self.primary_node)
1117 for device in self.disks:
1118 _Helper(all_nodes, device)
1119 return tuple(all_nodes)
1121 all_nodes = property(_ComputeAllNodes, None, None,
1122 "List of names of all the nodes of the instance")
1124 def MapLVsByNode(self, lvmap=None, devs=None, node_uuid=None):
1125 """Provide a mapping of nodes to LVs this instance owns.
1127 This function figures out what logical volumes should belong on
1128 which nodes, recursing through a device tree.
1131 @param lvmap: optional dictionary to receive the
1132 'node' : ['lv', ...] data.
1133 @type devs: list of L{Disk}
1134 @param devs: disks to get the LV name for. If None, all disk of this
1136 @type node_uuid: string
1137 @param node_uuid: UUID of the node to get the LV names for. If None, the
1138 primary node of this instance is used.
1139 @return: None if lvmap arg is given, otherwise, a dictionary of
1140 the form { 'node_uuid' : ['volume1', 'volume2', ...], ... };
1141 volumeN is of the form "vg_name/lv_name", compatible with
1145 if node_uuid is None:
1146 node_uuid = self.primary_node
1154 if not node_uuid in lvmap:
1155 lvmap[node_uuid] = []
1162 if dev.dev_type == constants.DT_PLAIN:
1163 lvmap[node_uuid].append(dev.logical_id[0] + "/" + dev.logical_id[1])
1165 elif dev.dev_type in constants.DTS_DRBD:
1167 self.MapLVsByNode(lvmap, dev.children, dev.logical_id[0])
1168 self.MapLVsByNode(lvmap, dev.children, dev.logical_id[1])
1171 self.MapLVsByNode(lvmap, dev.children, node_uuid)
1175 def FindDisk(self, idx):
1176 """Find a disk given having a specified index.
1178 This is just a wrapper that does validation of the index.
1181 @param idx: the disk index
1183 @return: the corresponding disk
1184 @raise errors.OpPrereqError: when the given index is not valid
1189 return self.disks[idx]
1190 except (TypeError, ValueError), err:
1191 raise errors.OpPrereqError("Invalid disk index: '%s'" % str(err),
1194 raise errors.OpPrereqError("Invalid disk index: %d (instace has disks"
1195 " 0 to %d" % (idx, len(self.disks) - 1),
1199 """Instance-specific conversion to standard python types.
1201 This replaces the children lists of objects with lists of standard
1205 bo = super(Instance, self).ToDict()
1207 for attr in "nics", "disks":
1208 alist = bo.get(attr, None)
1210 nlist = outils.ContainerToDicts(alist)
1217 def FromDict(cls, val):
1218 """Custom function for instances.
1221 if "admin_state" not in val:
1222 if val.get("admin_up", False):
1223 val["admin_state"] = constants.ADMINST_UP
1225 val["admin_state"] = constants.ADMINST_DOWN
1226 if "admin_up" in val:
1228 obj = super(Instance, cls).FromDict(val)
1229 obj.nics = outils.ContainerFromDicts(obj.nics, list, NIC)
1230 obj.disks = outils.ContainerFromDicts(obj.disks, list, Disk)
1233 def UpgradeConfig(self):
1234 """Fill defaults for missing configuration values.
1237 for nic in self.nics:
1239 for disk in self.disks:
1240 disk.UpgradeConfig()
1242 for key in constants.HVC_GLOBALS:
1244 del self.hvparams[key]
1247 if self.osparams is None:
1249 UpgradeBeParams(self.beparams)
1250 if self.disks_active is None:
1251 self.disks_active = self.admin_state == constants.ADMINST_UP
1254 class OS(ConfigObject):
1255 """Config object representing an operating system.
1257 @type supported_parameters: list
1258 @ivar supported_parameters: a list of tuples, name and description,
1259 containing the supported parameters by this OS
1261 @type VARIANT_DELIM: string
1262 @cvar VARIANT_DELIM: the variant delimiter
1274 "supported_variants",
1275 "supported_parameters",
1281 def SplitNameVariant(cls, name):
1282 """Splits the name into the proper name and variant.
1284 @param name: the OS (unprocessed) name
1286 @return: a list of two elements; if the original name didn't
1287 contain a variant, it's returned as an empty string
1290 nv = name.split(cls.VARIANT_DELIM, 1)
1296 def GetName(cls, name):
1297 """Returns the proper name of the os (without the variant).
1299 @param name: the OS (unprocessed) name
1302 return cls.SplitNameVariant(name)[0]
1305 def GetVariant(cls, name):
1306 """Returns the variant the os (without the base name).
1308 @param name: the OS (unprocessed) name
1311 return cls.SplitNameVariant(name)[1]
1314 class ExtStorage(ConfigObject):
1315 """Config object representing an External Storage Provider.
1328 "supported_parameters",
1332 class NodeHvState(ConfigObject):
1333 """Hypvervisor state on a node.
1335 @ivar mem_total: Total amount of memory
1336 @ivar mem_node: Memory used by, or reserved for, the node itself (not always
1338 @ivar mem_hv: Memory used by hypervisor or lost due to instance allocation
1340 @ivar mem_inst: Memory used by instances living on node
1341 @ivar cpu_total: Total node CPU core count
1342 @ivar cpu_node: Number of CPU cores reserved for the node itself
1355 class NodeDiskState(ConfigObject):
1356 """Disk state on a node.
1366 class Node(TaggableObject):
1367 """Config object representing a node.
1369 @ivar hv_state: Hypervisor state (e.g. number of CPUs)
1370 @ivar hv_state_static: Hypervisor state overriden by user
1371 @ivar disk_state: Disk state (e.g. free space)
1372 @ivar disk_state_static: Disk state overriden by user
1391 "disk_state_static",
1392 ] + _TIMESTAMPS + _UUID
1394 def UpgradeConfig(self):
1395 """Fill defaults for missing configuration values.
1398 # pylint: disable=E0203
1399 # because these are "defined" via slots, not manually
1400 if self.master_capable is None:
1401 self.master_capable = True
1403 if self.vm_capable is None:
1404 self.vm_capable = True
1406 if self.ndparams is None:
1408 # And remove any global parameter
1409 for key in constants.NDC_GLOBALS:
1410 if key in self.ndparams:
1411 logging.warning("Ignoring %s node parameter for node %s",
1413 del self.ndparams[key]
1415 if self.powered is None:
1419 """Custom function for serializing.
1422 data = super(Node, self).ToDict()
1424 hv_state = data.get("hv_state", None)
1425 if hv_state is not None:
1426 data["hv_state"] = outils.ContainerToDicts(hv_state)
1428 disk_state = data.get("disk_state", None)
1429 if disk_state is not None:
1430 data["disk_state"] = \
1431 dict((key, outils.ContainerToDicts(value))
1432 for (key, value) in disk_state.items())
1437 def FromDict(cls, val):
1438 """Custom function for deserializing.
1441 obj = super(Node, cls).FromDict(val)
1443 if obj.hv_state is not None:
1445 outils.ContainerFromDicts(obj.hv_state, dict, NodeHvState)
1447 if obj.disk_state is not None:
1449 dict((key, outils.ContainerFromDicts(value, dict, NodeDiskState))
1450 for (key, value) in obj.disk_state.items())
1455 class NodeGroup(TaggableObject):
1456 """Config object representing a node group."""
1465 "disk_state_static",
1468 ] + _TIMESTAMPS + _UUID
1471 """Custom function for nodegroup.
1473 This discards the members object, which gets recalculated and is only kept
1477 mydict = super(NodeGroup, self).ToDict()
1478 del mydict["members"]
1482 def FromDict(cls, val):
1483 """Custom function for nodegroup.
1485 The members slot is initialized to an empty list, upon deserialization.
1488 obj = super(NodeGroup, cls).FromDict(val)
1492 def UpgradeConfig(self):
1493 """Fill defaults for missing configuration values.
1496 if self.ndparams is None:
1499 if self.serial_no is None:
1502 if self.alloc_policy is None:
1503 self.alloc_policy = constants.ALLOC_POLICY_PREFERRED
1505 # We only update mtime, and not ctime, since we would not be able
1506 # to provide a correct value for creation time.
1507 if self.mtime is None:
1508 self.mtime = time.time()
1510 if self.diskparams is None:
1511 self.diskparams = {}
1512 if self.ipolicy is None:
1513 self.ipolicy = MakeEmptyIPolicy()
1515 if self.networks is None:
1518 def FillND(self, node):
1519 """Return filled out ndparams for L{objects.Node}
1521 @type node: L{objects.Node}
1522 @param node: A Node object to fill
1523 @return a copy of the node's ndparams with defaults filled
1526 return self.SimpleFillND(node.ndparams)
1528 def SimpleFillND(self, ndparams):
1529 """Fill a given ndparams dict with defaults.
1531 @type ndparams: dict
1532 @param ndparams: the dict to fill
1534 @return: a copy of the passed in ndparams with missing keys filled
1535 from the node group defaults
1538 return FillDict(self.ndparams, ndparams)
1541 class Cluster(TaggableObject):
1542 """Config object representing the cluster."""
1547 "highest_used_port",
1550 "volume_group_name",
1552 "drbd_usermode_helper",
1554 "default_hypervisor",
1559 "use_external_mip_script",
1562 "shared_file_storage_dir",
1563 "enabled_hypervisors",
1572 "candidate_pool_size",
1575 "maintain_node_health",
1577 "default_iallocator",
1580 "primary_ip_family",
1581 "prealloc_wipe_disks",
1583 "disk_state_static",
1584 "enabled_disk_templates",
1585 ] + _TIMESTAMPS + _UUID
1587 def UpgradeConfig(self):
1588 """Fill defaults for missing configuration values.
1591 # pylint: disable=E0203
1592 # because these are "defined" via slots, not manually
1593 if self.hvparams is None:
1594 self.hvparams = constants.HVC_DEFAULTS
1596 for hypervisor in self.hvparams:
1597 self.hvparams[hypervisor] = FillDict(
1598 constants.HVC_DEFAULTS[hypervisor], self.hvparams[hypervisor])
1600 if self.os_hvp is None:
1603 # osparams added before 2.2
1604 if self.osparams is None:
1607 self.ndparams = UpgradeNDParams(self.ndparams)
1609 self.beparams = UpgradeGroupedParams(self.beparams,
1610 constants.BEC_DEFAULTS)
1611 for beparams_group in self.beparams:
1612 UpgradeBeParams(self.beparams[beparams_group])
1614 migrate_default_bridge = not self.nicparams
1615 self.nicparams = UpgradeGroupedParams(self.nicparams,
1616 constants.NICC_DEFAULTS)
1617 if migrate_default_bridge:
1618 self.nicparams[constants.PP_DEFAULT][constants.NIC_LINK] = \
1621 if self.modify_etc_hosts is None:
1622 self.modify_etc_hosts = True
1624 if self.modify_ssh_setup is None:
1625 self.modify_ssh_setup = True
1627 # default_bridge is no longer used in 2.1. The slot is left there to
1628 # support auto-upgrading. It can be removed once we decide to deprecate
1629 # upgrading straight from 2.0.
1630 if self.default_bridge is not None:
1631 self.default_bridge = None
1633 # default_hypervisor is just the first enabled one in 2.1. This slot and
1634 # code can be removed once upgrading straight from 2.0 is deprecated.
1635 if self.default_hypervisor is not None:
1636 self.enabled_hypervisors = ([self.default_hypervisor] +
1637 [hvname for hvname in self.enabled_hypervisors
1638 if hvname != self.default_hypervisor])
1639 self.default_hypervisor = None
1641 # maintain_node_health added after 2.1.1
1642 if self.maintain_node_health is None:
1643 self.maintain_node_health = False
1645 if self.uid_pool is None:
1648 if self.default_iallocator is None:
1649 self.default_iallocator = ""
1651 # reserved_lvs added before 2.2
1652 if self.reserved_lvs is None:
1653 self.reserved_lvs = []
1655 # hidden and blacklisted operating systems added before 2.2.1
1656 if self.hidden_os is None:
1659 if self.blacklisted_os is None:
1660 self.blacklisted_os = []
1662 # primary_ip_family added before 2.3
1663 if self.primary_ip_family is None:
1664 self.primary_ip_family = AF_INET
1666 if self.master_netmask is None:
1667 ipcls = netutils.IPAddress.GetClassFromIpFamily(self.primary_ip_family)
1668 self.master_netmask = ipcls.iplen
1670 if self.prealloc_wipe_disks is None:
1671 self.prealloc_wipe_disks = False
1673 # shared_file_storage_dir added before 2.5
1674 if self.shared_file_storage_dir is None:
1675 self.shared_file_storage_dir = ""
1677 if self.use_external_mip_script is None:
1678 self.use_external_mip_script = False
1681 self.diskparams = UpgradeDiskParams(self.diskparams)
1683 self.diskparams = constants.DISK_DT_DEFAULTS.copy()
1685 # instance policy added before 2.6
1686 if self.ipolicy is None:
1687 self.ipolicy = FillIPolicy(constants.IPOLICY_DEFAULTS, {})
1689 # we can either make sure to upgrade the ipolicy always, or only
1690 # do it in some corner cases (e.g. missing keys); note that this
1691 # will break any removal of keys from the ipolicy dict
1692 wrongkeys = frozenset(self.ipolicy.keys()) - constants.IPOLICY_ALL_KEYS
1694 # These keys would be silently removed by FillIPolicy()
1695 msg = ("Cluster instance policy contains spurious keys: %s" %
1696 utils.CommaJoin(wrongkeys))
1697 raise errors.ConfigurationError(msg)
1698 self.ipolicy = FillIPolicy(constants.IPOLICY_DEFAULTS, self.ipolicy)
1701 def primary_hypervisor(self):
1702 """The first hypervisor is the primary.
1704 Useful, for example, for L{Node}'s hv/disk state.
1707 return self.enabled_hypervisors[0]
1710 """Custom function for cluster.
1713 mydict = super(Cluster, self).ToDict()
1715 if self.tcpudp_port_pool is None:
1716 tcpudp_port_pool = []
1718 tcpudp_port_pool = list(self.tcpudp_port_pool)
1720 mydict["tcpudp_port_pool"] = tcpudp_port_pool
1725 def FromDict(cls, val):
1726 """Custom function for cluster.
1729 obj = super(Cluster, cls).FromDict(val)
1731 if obj.tcpudp_port_pool is None:
1732 obj.tcpudp_port_pool = set()
1733 elif not isinstance(obj.tcpudp_port_pool, set):
1734 obj.tcpudp_port_pool = set(obj.tcpudp_port_pool)
1738 def SimpleFillDP(self, diskparams):
1739 """Fill a given diskparams dict with cluster defaults.
1741 @param diskparams: The diskparams
1742 @return: The defaults dict
1745 return FillDiskParams(self.diskparams, diskparams)
1747 def GetHVDefaults(self, hypervisor, os_name=None, skip_keys=None):
1748 """Get the default hypervisor parameters for the cluster.
1750 @param hypervisor: the hypervisor name
1751 @param os_name: if specified, we'll also update the defaults for this OS
1752 @param skip_keys: if passed, list of keys not to use
1753 @return: the defaults dict
1756 if skip_keys is None:
1759 fill_stack = [self.hvparams.get(hypervisor, {})]
1760 if os_name is not None:
1761 os_hvp = self.os_hvp.get(os_name, {}).get(hypervisor, {})
1762 fill_stack.append(os_hvp)
1765 for o_dict in fill_stack:
1766 ret_dict = FillDict(ret_dict, o_dict, skip_keys=skip_keys)
1770 def SimpleFillHV(self, hv_name, os_name, hvparams, skip_globals=False):
1771 """Fill a given hvparams dict with cluster defaults.
1773 @type hv_name: string
1774 @param hv_name: the hypervisor to use
1775 @type os_name: string
1776 @param os_name: the OS to use for overriding the hypervisor defaults
1777 @type skip_globals: boolean
1778 @param skip_globals: if True, the global hypervisor parameters will
1781 @return: a copy of the given hvparams with missing keys filled from
1782 the cluster defaults
1786 skip_keys = constants.HVC_GLOBALS
1790 def_dict = self.GetHVDefaults(hv_name, os_name, skip_keys=skip_keys)
1791 return FillDict(def_dict, hvparams, skip_keys=skip_keys)
1793 def FillHV(self, instance, skip_globals=False):
1794 """Fill an instance's hvparams dict with cluster defaults.
1796 @type instance: L{objects.Instance}
1797 @param instance: the instance parameter to fill
1798 @type skip_globals: boolean
1799 @param skip_globals: if True, the global hypervisor parameters will
1802 @return: a copy of the instance's hvparams with missing keys filled from
1803 the cluster defaults
1806 return self.SimpleFillHV(instance.hypervisor, instance.os,
1807 instance.hvparams, skip_globals)
1809 def SimpleFillBE(self, beparams):
1810 """Fill a given beparams dict with cluster defaults.
1812 @type beparams: dict
1813 @param beparams: the dict to fill
1815 @return: a copy of the passed in beparams with missing keys filled
1816 from the cluster defaults
1819 return FillDict(self.beparams.get(constants.PP_DEFAULT, {}), beparams)
1821 def FillBE(self, instance):
1822 """Fill an instance's beparams dict with cluster defaults.
1824 @type instance: L{objects.Instance}
1825 @param instance: the instance parameter to fill
1827 @return: a copy of the instance's beparams with missing keys filled from
1828 the cluster defaults
1831 return self.SimpleFillBE(instance.beparams)
1833 def SimpleFillNIC(self, nicparams):
1834 """Fill a given nicparams dict with cluster defaults.
1836 @type nicparams: dict
1837 @param nicparams: the dict to fill
1839 @return: a copy of the passed in nicparams with missing keys filled
1840 from the cluster defaults
1843 return FillDict(self.nicparams.get(constants.PP_DEFAULT, {}), nicparams)
1845 def SimpleFillOS(self, os_name, os_params):
1846 """Fill an instance's osparams dict with cluster defaults.
1848 @type os_name: string
1849 @param os_name: the OS name to use
1850 @type os_params: dict
1851 @param os_params: the dict to fill with default values
1853 @return: a copy of the instance's osparams with missing keys filled from
1854 the cluster defaults
1857 name_only = os_name.split("+", 1)[0]
1859 result = self.osparams.get(name_only, {})
1861 result = FillDict(result, self.osparams.get(os_name, {}))
1863 return FillDict(result, os_params)
1866 def SimpleFillHvState(hv_state):
1867 """Fill an hv_state sub dict with cluster defaults.
1870 return FillDict(constants.HVST_DEFAULTS, hv_state)
1873 def SimpleFillDiskState(disk_state):
1874 """Fill an disk_state sub dict with cluster defaults.
1877 return FillDict(constants.DS_DEFAULTS, disk_state)
1879 def FillND(self, node, nodegroup):
1880 """Return filled out ndparams for L{objects.NodeGroup} and L{objects.Node}
1882 @type node: L{objects.Node}
1883 @param node: A Node object to fill
1884 @type nodegroup: L{objects.NodeGroup}
1885 @param nodegroup: A Node object to fill
1886 @return a copy of the node's ndparams with defaults filled
1889 return self.SimpleFillND(nodegroup.FillND(node))
1891 def SimpleFillND(self, ndparams):
1892 """Fill a given ndparams dict with defaults.
1894 @type ndparams: dict
1895 @param ndparams: the dict to fill
1897 @return: a copy of the passed in ndparams with missing keys filled
1898 from the cluster defaults
1901 return FillDict(self.ndparams, ndparams)
1903 def SimpleFillIPolicy(self, ipolicy):
1904 """ Fill instance policy dict with defaults.
1907 @param ipolicy: the dict to fill
1909 @return: a copy of passed ipolicy with missing keys filled from
1910 the cluster defaults
1913 return FillIPolicy(self.ipolicy, ipolicy)
1915 def IsDiskTemplateEnabled(self, disk_template):
1916 """Checks if a particular disk template is enabled.
1919 return utils.storage.IsDiskTemplateEnabled(
1920 disk_template, self.enabled_disk_templates)
1922 def IsFileStorageEnabled(self):
1923 """Checks if file storage is enabled.
1926 return utils.storage.IsFileStorageEnabled(self.enabled_disk_templates)
1928 def IsSharedFileStorageEnabled(self):
1929 """Checks if shared file storage is enabled.
1932 return utils.storage.IsSharedFileStorageEnabled(
1933 self.enabled_disk_templates)
1936 class BlockDevStatus(ConfigObject):
1937 """Config object representing the status of a block device."""
1949 class ImportExportStatus(ConfigObject):
1950 """Config object representing the status of an import or export."""
1956 "progress_throughput",
1964 class ImportExportOptions(ConfigObject):
1965 """Options for import/export daemon
1967 @ivar key_name: X509 key name (None for cluster certificate)
1968 @ivar ca_pem: Remote peer CA in PEM format (None for cluster certificate)
1969 @ivar compress: Compression method (one of L{constants.IEC_ALL})
1970 @ivar magic: Used to ensure the connection goes to the right disk
1971 @ivar ipv6: Whether to use IPv6
1972 @ivar connect_timeout: Number of seconds for establishing connection
1985 class ConfdRequest(ConfigObject):
1986 """Object holding a confd request.
1988 @ivar protocol: confd protocol version
1989 @ivar type: confd query type
1990 @ivar query: query request
1991 @ivar rsalt: requested reply salt
2002 class ConfdReply(ConfigObject):
2003 """Object holding a confd reply.
2005 @ivar protocol: confd protocol version
2006 @ivar status: reply status code (ok, error)
2007 @ivar answer: confd query reply
2008 @ivar serial: configuration serial number
2019 class QueryFieldDefinition(ConfigObject):
2020 """Object holding a query field definition.
2022 @ivar name: Field name
2023 @ivar title: Human-readable title
2024 @ivar kind: Field type
2025 @ivar doc: Human-readable description
2036 class _QueryResponseBase(ConfigObject):
2042 """Custom function for serializing.
2045 mydict = super(_QueryResponseBase, self).ToDict()
2046 mydict["fields"] = outils.ContainerToDicts(mydict["fields"])
2050 def FromDict(cls, val):
2051 """Custom function for de-serializing.
2054 obj = super(_QueryResponseBase, cls).FromDict(val)
2056 outils.ContainerFromDicts(obj.fields, list, QueryFieldDefinition)
2060 class QueryResponse(_QueryResponseBase):
2061 """Object holding the response to a query.
2063 @ivar fields: List of L{QueryFieldDefinition} objects
2064 @ivar data: Requested data
2072 class QueryFieldsRequest(ConfigObject):
2073 """Object holding a request for querying available fields.
2082 class QueryFieldsResponse(_QueryResponseBase):
2083 """Object holding the response to a query for fields.
2085 @ivar fields: List of L{QueryFieldDefinition} objects
2091 class MigrationStatus(ConfigObject):
2092 """Object holding the status of a migration.
2102 class InstanceConsole(ConfigObject):
2103 """Object describing how to access the console of an instance.
2118 """Validates contents of this object.
2121 assert self.kind in constants.CONS_ALL, "Unknown console type"
2122 assert self.instance, "Missing instance name"
2123 assert self.message or self.kind in [constants.CONS_SSH,
2124 constants.CONS_SPICE,
2126 assert self.host or self.kind == constants.CONS_MESSAGE
2127 assert self.port or self.kind in [constants.CONS_MESSAGE,
2129 assert self.user or self.kind in [constants.CONS_MESSAGE,
2130 constants.CONS_SPICE,
2132 assert self.command or self.kind in [constants.CONS_MESSAGE,
2133 constants.CONS_SPICE,
2135 assert self.display or self.kind in [constants.CONS_MESSAGE,
2136 constants.CONS_SPICE,
2141 class Network(TaggableObject):
2142 """Object representing a network definition for ganeti.
2155 ] + _TIMESTAMPS + _UUID
2157 def HooksDict(self, prefix=""):
2158 """Export a dictionary used by hooks with a network's information.
2160 @type prefix: String
2161 @param prefix: Prefix to prepend to the dict entries
2165 "%sNETWORK_NAME" % prefix: self.name,
2166 "%sNETWORK_UUID" % prefix: self.uuid,
2167 "%sNETWORK_TAGS" % prefix: " ".join(self.GetTags()),
2170 result["%sNETWORK_SUBNET" % prefix] = self.network
2172 result["%sNETWORK_GATEWAY" % prefix] = self.gateway
2174 result["%sNETWORK_SUBNET6" % prefix] = self.network6
2176 result["%sNETWORK_GATEWAY6" % prefix] = self.gateway6
2178 result["%sNETWORK_MAC_PREFIX" % prefix] = self.mac_prefix
2183 def FromDict(cls, val):
2184 """Custom function for networks.
2186 Remove deprecated network_type and family.
2189 if "network_type" in val:
2190 del val["network_type"]
2193 obj = super(Network, cls).FromDict(val)
2197 class SerializableConfigParser(ConfigParser.SafeConfigParser):
2198 """Simple wrapper over ConfigParse that allows serialization.
2200 This class is basically ConfigParser.SafeConfigParser with two
2201 additional methods that allow it to serialize/unserialize to/from a
2206 """Dump this instance and return the string representation."""
2209 return buf.getvalue()
2212 def Loads(cls, data):
2213 """Load data from a string."""
2214 buf = StringIO(data)
2220 class LvmPvInfo(ConfigObject):
2221 """Information about an LVM physical volume (PV).
2224 @ivar name: name of the PV
2225 @type vg_name: string
2226 @ivar vg_name: name of the volume group containing the PV
2228 @ivar size: size of the PV in MiB
2230 @ivar free: free space in the PV, in MiB
2231 @type attributes: string
2232 @ivar attributes: PV attributes
2233 @type lv_list: list of strings
2234 @ivar lv_list: names of the LVs hosted on the PV
2246 """Is this PV empty?
2249 return self.size <= (self.free + 1)
2251 def IsAllocatable(self):
2252 """Is this PV allocatable?
2255 return ("a" in self.attributes)