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 UpgradeConfig(self):
270 """Fill defaults for missing configuration values.
272 This method will be called at configuration load time, and its
273 implementation will be object dependent.
279 class TaggableObject(ConfigObject):
280 """An generic class supporting tags.
284 VALID_TAG_RE = re.compile(r"^[\w.+*/:@-]+$")
287 def ValidateTag(cls, tag):
288 """Check if a tag is valid.
290 If the tag is invalid, an errors.TagError will be raised. The
291 function has no return value.
294 if not isinstance(tag, basestring):
295 raise errors.TagError("Invalid tag type (not a string)")
296 if len(tag) > constants.MAX_TAG_LEN:
297 raise errors.TagError("Tag too long (>%d characters)" %
298 constants.MAX_TAG_LEN)
300 raise errors.TagError("Tags cannot be empty")
301 if not cls.VALID_TAG_RE.match(tag):
302 raise errors.TagError("Tag contains invalid characters")
305 """Return the tags list.
308 tags = getattr(self, "tags", None)
310 tags = self.tags = set()
313 def AddTag(self, tag):
317 self.ValidateTag(tag)
318 tags = self.GetTags()
319 if len(tags) >= constants.MAX_TAGS_PER_OBJ:
320 raise errors.TagError("Too many tags")
321 self.GetTags().add(tag)
323 def RemoveTag(self, tag):
327 self.ValidateTag(tag)
328 tags = self.GetTags()
332 raise errors.TagError("Tag not found")
335 """Taggable-object-specific conversion to standard python types.
337 This replaces the tags set with a list.
340 bo = super(TaggableObject, self).ToDict()
342 tags = bo.get("tags", None)
343 if isinstance(tags, set):
344 bo["tags"] = list(tags)
348 def FromDict(cls, val):
349 """Custom function for instances.
352 obj = super(TaggableObject, cls).FromDict(val)
353 if hasattr(obj, "tags") and isinstance(obj.tags, list):
354 obj.tags = set(obj.tags)
358 class MasterNetworkParameters(ConfigObject):
359 """Network configuration parameters for the master
361 @ivar uuid: master nodes UUID
363 @ivar netmask: master netmask
364 @ivar netdev: master network device
365 @ivar ip_family: master IP family
377 class ConfigData(ConfigObject):
378 """Top-level config object."""
390 """Custom function for top-level config data.
392 This just replaces the list of instances, nodes and the cluster
393 with standard python types.
396 mydict = super(ConfigData, self).ToDict()
397 mydict["cluster"] = mydict["cluster"].ToDict()
398 for key in "nodes", "instances", "nodegroups", "networks":
399 mydict[key] = outils.ContainerToDicts(mydict[key])
404 def FromDict(cls, val):
405 """Custom function for top-level config data
408 obj = super(ConfigData, cls).FromDict(val)
409 obj.cluster = Cluster.FromDict(obj.cluster)
410 obj.nodes = outils.ContainerFromDicts(obj.nodes, dict, Node)
412 outils.ContainerFromDicts(obj.instances, dict, Instance)
414 outils.ContainerFromDicts(obj.nodegroups, dict, NodeGroup)
415 obj.networks = outils.ContainerFromDicts(obj.networks, dict, Network)
418 def HasAnyDiskOfType(self, dev_type):
419 """Check if in there is at disk of the given type in the configuration.
421 @type dev_type: L{constants.DTS_BLOCK}
422 @param dev_type: the type to look for
424 @return: boolean indicating if a disk of the given type was found or not
427 for instance in self.instances.values():
428 for disk in instance.disks:
429 if disk.IsBasedOnDiskType(dev_type):
433 def UpgradeConfig(self):
434 """Fill defaults for missing configuration values.
437 self.cluster.UpgradeConfig()
438 for node in self.nodes.values():
440 for instance in self.instances.values():
441 instance.UpgradeConfig()
442 self._UpgradeEnabledDiskTemplates()
443 if self.nodegroups is None:
445 for nodegroup in self.nodegroups.values():
446 nodegroup.UpgradeConfig()
447 InstancePolicy.UpgradeDiskTemplates(
448 nodegroup.ipolicy, self.cluster.enabled_disk_templates)
449 if self.cluster.drbd_usermode_helper is None:
450 # To decide if we set an helper let's check if at least one instance has
451 # a DRBD disk. This does not cover all the possible scenarios but it
452 # gives a good approximation.
453 if self.HasAnyDiskOfType(constants.DT_DRBD8):
454 self.cluster.drbd_usermode_helper = constants.DEFAULT_DRBD_HELPER
455 if self.networks is None:
457 for network in self.networks.values():
458 network.UpgradeConfig()
460 def _UpgradeEnabledDiskTemplates(self):
461 """Upgrade the cluster's enabled disk templates by inspecting the currently
462 enabled and/or used disk templates.
465 if not self.cluster.enabled_disk_templates:
467 set([inst.disk_template for inst in self.instances.values()])
468 # Add drbd and plain, if lvm is enabled (by specifying a volume group)
469 if self.cluster.volume_group_name:
470 template_set.add(constants.DT_DRBD8)
471 template_set.add(constants.DT_PLAIN)
472 # Set enabled_disk_templates to the inferred disk templates. Order them
473 # according to a preference list that is based on Ganeti's history of
474 # supported disk templates.
475 self.cluster.enabled_disk_templates = []
476 for preferred_template in constants.DISK_TEMPLATE_PREFERENCE:
477 if preferred_template in template_set:
478 self.cluster.enabled_disk_templates.append(preferred_template)
479 template_set.remove(preferred_template)
480 self.cluster.enabled_disk_templates.extend(list(template_set))
481 InstancePolicy.UpgradeDiskTemplates(
482 self.cluster.ipolicy, self.cluster.enabled_disk_templates)
485 class NIC(ConfigObject):
486 """Config object representing a network card."""
487 __slots__ = ["name", "mac", "ip", "network", "nicparams", "netinfo"] + _UUID
490 def CheckParameterSyntax(cls, nicparams):
491 """Check the given parameters for validity.
493 @type nicparams: dict
494 @param nicparams: dictionary with parameter names/value
495 @raise errors.ConfigurationError: when a parameter is not valid
498 mode = nicparams[constants.NIC_MODE]
499 if (mode not in constants.NIC_VALID_MODES and
500 mode != constants.VALUE_AUTO):
501 raise errors.ConfigurationError("Invalid NIC mode '%s'" % mode)
503 if (mode == constants.NIC_MODE_BRIDGED and
504 not nicparams[constants.NIC_LINK]):
505 raise errors.ConfigurationError("Missing bridged NIC link")
508 class Disk(ConfigObject):
509 """Config object representing a block device."""
510 __slots__ = (["name", "dev_type", "logical_id", "physical_id",
511 "children", "iv_name", "size", "mode", "params", "spindles"] +
514 def CreateOnSecondary(self):
515 """Test if this device needs to be created on a secondary node."""
516 return self.dev_type in (constants.DT_DRBD8, constants.DT_PLAIN)
518 def AssembleOnSecondary(self):
519 """Test if this device needs to be assembled on a secondary node."""
520 return self.dev_type in (constants.DT_DRBD8, constants.DT_PLAIN)
522 def OpenOnSecondary(self):
523 """Test if this device needs to be opened on a secondary node."""
524 return self.dev_type in (constants.DT_PLAIN,)
526 def StaticDevPath(self):
527 """Return the device path if this device type has a static one.
529 Some devices (LVM for example) live always at the same /dev/ path,
530 irrespective of their status. For such devices, we return this
531 path, for others we return None.
533 @warning: The path returned is not a normalized pathname; callers
534 should check that it is a valid path.
537 if self.dev_type == constants.DT_PLAIN:
538 return "/dev/%s/%s" % (self.logical_id[0], self.logical_id[1])
539 elif self.dev_type == constants.DT_BLOCK:
540 return self.logical_id[1]
541 elif self.dev_type == constants.DT_RBD:
542 return "/dev/%s/%s" % (self.logical_id[0], self.logical_id[1])
545 def ChildrenNeeded(self):
546 """Compute the needed number of children for activation.
548 This method will return either -1 (all children) or a positive
549 number denoting the minimum number of children needed for
550 activation (only mirrored devices will usually return >=0).
552 Currently, only DRBD8 supports diskless activation (therefore we
553 return 0), for all other we keep the previous semantics and return
557 if self.dev_type == constants.DT_DRBD8:
561 def IsBasedOnDiskType(self, dev_type):
562 """Check if the disk or its children are based on the given type.
564 @type dev_type: L{constants.DTS_BLOCK}
565 @param dev_type: the type to look for
567 @return: boolean indicating if a device of the given type was found or not
571 for child in self.children:
572 if child.IsBasedOnDiskType(dev_type):
574 return self.dev_type == dev_type
576 def GetNodes(self, node_uuid):
577 """This function returns the nodes this device lives on.
579 Given the node on which the parent of the device lives on (or, in
580 case of a top-level device, the primary node of the devices'
581 instance), this function will return a list of nodes on which this
582 devices needs to (or can) be assembled.
585 if self.dev_type in [constants.DT_PLAIN, constants.DT_FILE,
586 constants.DT_BLOCK, constants.DT_RBD,
587 constants.DT_EXT, constants.DT_SHARED_FILE]:
589 elif self.dev_type in constants.DTS_DRBD:
590 result = [self.logical_id[0], self.logical_id[1]]
591 if node_uuid not in result:
592 raise errors.ConfigurationError("DRBD device passed unknown node")
594 raise errors.ProgrammerError("Unhandled device type %s" % self.dev_type)
597 def ComputeNodeTree(self, parent_node_uuid):
598 """Compute the node/disk tree for this disk and its children.
600 This method, given the node on which the parent disk lives, will
601 return the list of all (node UUID, disk) pairs which describe the disk
602 tree in the most compact way. For example, a drbd/lvm stack
603 will be returned as (primary_node, drbd) and (secondary_node, drbd)
604 which represents all the top-level devices on the nodes.
607 my_nodes = self.GetNodes(parent_node_uuid)
608 result = [(node, self) for node in my_nodes]
609 if not self.children:
612 for node in my_nodes:
613 for child in self.children:
614 child_result = child.ComputeNodeTree(node)
615 if len(child_result) == 1:
616 # child (and all its descendants) is simple, doesn't split
617 # over multiple hosts, so we don't need to describe it, our
618 # own entry for this node describes it completely
621 # check if child nodes differ from my nodes; note that
622 # subdisk can differ from the child itself, and be instead
623 # one of its descendants
624 for subnode, subdisk in child_result:
625 if subnode not in my_nodes:
626 result.append((subnode, subdisk))
627 # otherwise child is under our own node, so we ignore this
628 # entry (but probably the other results in the list will
632 def ComputeGrowth(self, amount):
633 """Compute the per-VG growth requirements.
635 This only works for VG-based disks.
637 @type amount: integer
638 @param amount: the desired increase in (user-visible) disk space
640 @return: a dictionary of volume-groups and the required size
643 if self.dev_type == constants.DT_PLAIN:
644 return {self.logical_id[0]: amount}
645 elif self.dev_type == constants.DT_DRBD8:
647 return self.children[0].ComputeGrowth(amount)
651 # Other disk types do not require VG space
654 def RecordGrow(self, amount):
655 """Update the size of this disk after growth.
657 This method recurses over the disks's children and updates their
658 size correspondigly. The method needs to be kept in sync with the
659 actual algorithms from bdev.
662 if self.dev_type in (constants.DT_PLAIN, constants.DT_FILE,
663 constants.DT_RBD, constants.DT_EXT,
664 constants.DT_SHARED_FILE):
666 elif self.dev_type == constants.DT_DRBD8:
668 self.children[0].RecordGrow(amount)
671 raise errors.ProgrammerError("Disk.RecordGrow called for unsupported"
672 " disk type %s" % self.dev_type)
674 def Update(self, size=None, mode=None, spindles=None):
675 """Apply changes to size, spindles and mode.
678 if self.dev_type == constants.DT_DRBD8:
680 self.children[0].Update(size=size, mode=mode)
682 assert not self.children
688 if spindles is not None:
689 self.spindles = spindles
692 """Sets recursively the size to zero for the disk and its children.
696 for child in self.children:
700 def SetPhysicalID(self, target_node_uuid, nodes_ip):
701 """Convert the logical ID to the physical ID.
703 This is used only for drbd, which needs ip/port configuration.
705 The routine descends down and updates its children also, because
706 this helps when the only the top device is passed to the remote
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 in nodes_ip, and must be one of the
714 nodes in the logical ID for each of the DRBD devices encountered
719 for child in self.children:
720 child.SetPhysicalID(target_node_uuid, nodes_ip)
722 if self.logical_id is None and self.physical_id is not None:
724 if self.dev_type in constants.DTS_DRBD:
725 pnode_uuid, snode_uuid, port, pminor, sminor, secret = self.logical_id
726 if target_node_uuid not in (pnode_uuid, snode_uuid):
727 raise errors.ConfigurationError("DRBD device not knowing node %s" %
729 pnode_ip = nodes_ip.get(pnode_uuid, None)
730 snode_ip = nodes_ip.get(snode_uuid, None)
731 if pnode_ip is None or snode_ip is None:
732 raise errors.ConfigurationError("Can't find primary or secondary node"
733 " for %s" % str(self))
734 p_data = (pnode_ip, port)
735 s_data = (snode_ip, port)
736 if pnode_uuid == target_node_uuid:
737 self.physical_id = p_data + s_data + (pminor, secret)
738 else: # it must be secondary, we tested above
739 self.physical_id = s_data + p_data + (sminor, secret)
741 self.physical_id = self.logical_id
745 """Disk-specific conversion to standard python types.
747 This replaces the children lists of objects with lists of
748 standard python types.
751 bo = super(Disk, self).ToDict()
753 for attr in ("children",):
754 alist = bo.get(attr, None)
756 bo[attr] = outils.ContainerToDicts(alist)
760 def FromDict(cls, val):
761 """Custom function for Disks
764 obj = super(Disk, cls).FromDict(val)
766 obj.children = outils.ContainerFromDicts(obj.children, list, Disk)
767 if obj.logical_id and isinstance(obj.logical_id, list):
768 obj.logical_id = tuple(obj.logical_id)
769 if obj.physical_id and isinstance(obj.physical_id, list):
770 obj.physical_id = tuple(obj.physical_id)
771 if obj.dev_type in constants.DTS_DRBD:
772 # we need a tuple of length six here
773 if len(obj.logical_id) < 6:
774 obj.logical_id += (None,) * (6 - len(obj.logical_id))
778 """Custom str() formatter for disks.
781 if self.dev_type == constants.DT_PLAIN:
782 val = "<LogicalVolume(/dev/%s/%s" % self.logical_id
783 elif self.dev_type in constants.DTS_DRBD:
784 node_a, node_b, port, minor_a, minor_b = self.logical_id[:5]
786 if self.physical_id is None:
789 phy = ("configured as %s:%s %s:%s" %
790 (self.physical_id[0], self.physical_id[1],
791 self.physical_id[2], self.physical_id[3]))
793 val += ("hosts=%s/%d-%s/%d, port=%s, %s, " %
794 (node_a, minor_a, node_b, minor_b, port, phy))
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, physical_id=%s, children=%s" %
801 (self.dev_type, self.logical_id, self.physical_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
832 # Params should be an empty dict that gets filled any time needed
833 # In case of ext template we allow arbitrary params that should not
834 # be overrided during a config reload/upgrade.
835 if not self.params or not isinstance(self.params, dict):
838 # add here config upgrade for this disk
840 # map of legacy device types (mapping differing LD constants to new
842 LEG_DEV_TYPE_MAP = {"lvm": constants.DT_PLAIN, "drbd8": constants.DT_DRBD8}
843 if self.dev_type in LEG_DEV_TYPE_MAP:
844 self.dev_type = LEG_DEV_TYPE_MAP[self.dev_type]
847 def ComputeLDParams(disk_template, disk_params):
848 """Computes Logical Disk parameters from Disk Template parameters.
850 @type disk_template: string
851 @param disk_template: disk template, one of L{constants.DISK_TEMPLATES}
852 @type disk_params: dict
853 @param disk_params: disk template parameters;
854 dict(template_name -> parameters
856 @return: a list of dicts, one for each node of the disk hierarchy. Each dict
857 contains the LD parameters of the node. The tree is flattened in-order.
860 if disk_template not in constants.DISK_TEMPLATES:
861 raise errors.ProgrammerError("Unknown disk template %s" % disk_template)
863 assert disk_template in disk_params
866 dt_params = disk_params[disk_template]
867 if disk_template == constants.DT_DRBD8:
868 result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.DT_DRBD8], {
869 constants.LDP_RESYNC_RATE: dt_params[constants.DRBD_RESYNC_RATE],
870 constants.LDP_BARRIERS: dt_params[constants.DRBD_DISK_BARRIERS],
871 constants.LDP_NO_META_FLUSH: dt_params[constants.DRBD_META_BARRIERS],
872 constants.LDP_DEFAULT_METAVG: dt_params[constants.DRBD_DEFAULT_METAVG],
873 constants.LDP_DISK_CUSTOM: dt_params[constants.DRBD_DISK_CUSTOM],
874 constants.LDP_NET_CUSTOM: dt_params[constants.DRBD_NET_CUSTOM],
875 constants.LDP_PROTOCOL: dt_params[constants.DRBD_PROTOCOL],
876 constants.LDP_DYNAMIC_RESYNC: dt_params[constants.DRBD_DYNAMIC_RESYNC],
877 constants.LDP_PLAN_AHEAD: dt_params[constants.DRBD_PLAN_AHEAD],
878 constants.LDP_FILL_TARGET: dt_params[constants.DRBD_FILL_TARGET],
879 constants.LDP_DELAY_TARGET: dt_params[constants.DRBD_DELAY_TARGET],
880 constants.LDP_MAX_RATE: dt_params[constants.DRBD_MAX_RATE],
881 constants.LDP_MIN_RATE: dt_params[constants.DRBD_MIN_RATE],
885 result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.DT_PLAIN], {
886 constants.LDP_STRIPES: dt_params[constants.DRBD_DATA_STRIPES],
890 result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.DT_PLAIN], {
891 constants.LDP_STRIPES: dt_params[constants.DRBD_META_STRIPES],
894 elif disk_template in (constants.DT_FILE, constants.DT_SHARED_FILE):
895 result.append(constants.DISK_LD_DEFAULTS[disk_template])
897 elif disk_template == constants.DT_PLAIN:
898 result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.DT_PLAIN], {
899 constants.LDP_STRIPES: dt_params[constants.LV_STRIPES],
902 elif disk_template == constants.DT_BLOCK:
903 result.append(constants.DISK_LD_DEFAULTS[constants.DT_BLOCK])
905 elif disk_template == constants.DT_RBD:
906 result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.DT_RBD], {
907 constants.LDP_POOL: dt_params[constants.RBD_POOL],
910 elif disk_template == constants.DT_EXT:
911 result.append(constants.DISK_LD_DEFAULTS[constants.DT_EXT])
916 class InstancePolicy(ConfigObject):
917 """Config object representing instance policy limits dictionary.
919 Note that this object is not actually used in the config, it's just
920 used as a placeholder for a few functions.
924 def UpgradeDiskTemplates(cls, ipolicy, enabled_disk_templates):
925 """Upgrades the ipolicy configuration."""
926 if constants.IPOLICY_DTS in ipolicy:
927 if not set(ipolicy[constants.IPOLICY_DTS]).issubset(
928 set(enabled_disk_templates)):
929 ipolicy[constants.IPOLICY_DTS] = list(
930 set(ipolicy[constants.IPOLICY_DTS]) & set(enabled_disk_templates))
933 def CheckParameterSyntax(cls, ipolicy, check_std):
934 """ Check the instance policy for validity.
937 @param ipolicy: dictionary with min/max/std specs and policies
938 @type check_std: bool
939 @param check_std: Whether to check std value or just assume compliance
940 @raise errors.ConfigurationError: when the policy is not legal
943 InstancePolicy.CheckISpecSyntax(ipolicy, check_std)
944 if constants.IPOLICY_DTS in ipolicy:
945 InstancePolicy.CheckDiskTemplates(ipolicy[constants.IPOLICY_DTS])
946 for key in constants.IPOLICY_PARAMETERS:
948 InstancePolicy.CheckParameter(key, ipolicy[key])
949 wrong_keys = frozenset(ipolicy.keys()) - constants.IPOLICY_ALL_KEYS
951 raise errors.ConfigurationError("Invalid keys in ipolicy: %s" %
952 utils.CommaJoin(wrong_keys))
955 def _CheckIncompleteSpec(cls, spec, keyname):
956 missing_params = constants.ISPECS_PARAMETERS - frozenset(spec.keys())
958 msg = ("Missing instance specs parameters for %s: %s" %
959 (keyname, utils.CommaJoin(missing_params)))
960 raise errors.ConfigurationError(msg)
963 def CheckISpecSyntax(cls, ipolicy, check_std):
964 """Check the instance policy specs for validity.
967 @param ipolicy: dictionary with min/max/std specs
968 @type check_std: bool
969 @param check_std: Whether to check std value or just assume compliance
970 @raise errors.ConfigurationError: when specs are not valid
973 if constants.ISPECS_MINMAX not in ipolicy:
977 if check_std and constants.ISPECS_STD not in ipolicy:
978 msg = "Missing key in ipolicy: %s" % constants.ISPECS_STD
979 raise errors.ConfigurationError(msg)
980 stdspec = ipolicy.get(constants.ISPECS_STD)
982 InstancePolicy._CheckIncompleteSpec(stdspec, constants.ISPECS_STD)
984 if not ipolicy[constants.ISPECS_MINMAX]:
985 raise errors.ConfigurationError("Empty minmax specifications")
987 for minmaxspecs in ipolicy[constants.ISPECS_MINMAX]:
988 missing = constants.ISPECS_MINMAX_KEYS - frozenset(minmaxspecs.keys())
990 msg = "Missing instance specification: %s" % utils.CommaJoin(missing)
991 raise errors.ConfigurationError(msg)
992 for (key, spec) in minmaxspecs.items():
993 InstancePolicy._CheckIncompleteSpec(spec, key)
996 for param in constants.ISPECS_PARAMETERS:
997 par_std_ok = InstancePolicy._CheckISpecParamSyntax(minmaxspecs, stdspec,
999 spec_std_ok = spec_std_ok and par_std_ok
1000 std_is_good = std_is_good or spec_std_ok
1002 raise errors.ConfigurationError("Invalid std specifications")
1005 def _CheckISpecParamSyntax(cls, minmaxspecs, stdspec, name, check_std):
1006 """Check the instance policy specs for validity on a given key.
1008 We check if the instance specs makes sense for a given key, that is
1009 if minmaxspecs[min][name] <= stdspec[name] <= minmaxspec[max][name].
1011 @type minmaxspecs: dict
1012 @param minmaxspecs: dictionary with min and max instance spec
1014 @param stdspec: dictionary with standard instance spec
1016 @param name: what are the limits for
1017 @type check_std: bool
1018 @param check_std: Whether to check std value or just assume compliance
1020 @return: C{True} when specs are valid, C{False} when standard spec for the
1021 given name is not valid
1022 @raise errors.ConfigurationError: when min/max specs for the given name
1026 minspec = minmaxspecs[constants.ISPECS_MIN]
1027 maxspec = minmaxspecs[constants.ISPECS_MAX]
1028 min_v = minspec[name]
1029 max_v = maxspec[name]
1032 err = ("Invalid specification of min/max values for %s: %s/%s" %
1033 (name, min_v, max_v))
1034 raise errors.ConfigurationError(err)
1036 std_v = stdspec.get(name, min_v)
1037 return std_v >= min_v and std_v <= max_v
1042 def CheckDiskTemplates(cls, disk_templates):
1043 """Checks the disk templates for validity.
1046 if not disk_templates:
1047 raise errors.ConfigurationError("Instance policy must contain" +
1048 " at least one disk template")
1049 wrong = frozenset(disk_templates).difference(constants.DISK_TEMPLATES)
1051 raise errors.ConfigurationError("Invalid disk template(s) %s" %
1052 utils.CommaJoin(wrong))
1055 def CheckParameter(cls, key, value):
1056 """Checks a parameter.
1058 Currently we expect all parameters to be float values.
1063 except (TypeError, ValueError), err:
1064 raise errors.ConfigurationError("Invalid value for key" " '%s':"
1065 " '%s', error: %s" % (key, value, err))
1068 class Instance(TaggableObject):
1069 """Config object representing an instance."""
1085 ] + _TIMESTAMPS + _UUID
1087 def _ComputeSecondaryNodes(self):
1088 """Compute the list of secondary nodes.
1090 This is a simple wrapper over _ComputeAllNodes.
1093 all_nodes = set(self._ComputeAllNodes())
1094 all_nodes.discard(self.primary_node)
1095 return tuple(all_nodes)
1097 secondary_nodes = property(_ComputeSecondaryNodes, None, None,
1098 "List of names of secondary nodes")
1100 def _ComputeAllNodes(self):
1101 """Compute the list of all nodes.
1103 Since the data is already there (in the drbd disks), keeping it as
1104 a separate normal attribute is redundant and if not properly
1105 synchronised can cause problems. Thus it's better to compute it
1109 def _Helper(nodes, device):
1110 """Recursively computes nodes given a top device."""
1111 if device.dev_type in constants.DTS_DRBD:
1112 nodea, nodeb = device.logical_id[:2]
1116 for child in device.children:
1117 _Helper(nodes, child)
1120 all_nodes.add(self.primary_node)
1121 for device in self.disks:
1122 _Helper(all_nodes, device)
1123 return tuple(all_nodes)
1125 all_nodes = property(_ComputeAllNodes, None, None,
1126 "List of names of all the nodes of the instance")
1128 def MapLVsByNode(self, lvmap=None, devs=None, node_uuid=None):
1129 """Provide a mapping of nodes to LVs this instance owns.
1131 This function figures out what logical volumes should belong on
1132 which nodes, recursing through a device tree.
1135 @param lvmap: optional dictionary to receive the
1136 'node' : ['lv', ...] data.
1137 @type devs: list of L{Disk}
1138 @param devs: disks to get the LV name for. If None, all disk of this
1140 @type node_uuid: string
1141 @param node_uuid: UUID of the node to get the LV names for. If None, the
1142 primary node of this instance is used.
1143 @return: None if lvmap arg is given, otherwise, a dictionary of
1144 the form { 'node_uuid' : ['volume1', 'volume2', ...], ... };
1145 volumeN is of the form "vg_name/lv_name", compatible with
1149 if node_uuid is None:
1150 node_uuid = self.primary_node
1158 if not node_uuid in lvmap:
1159 lvmap[node_uuid] = []
1166 if dev.dev_type == constants.DT_PLAIN:
1167 lvmap[node_uuid].append(dev.logical_id[0] + "/" + dev.logical_id[1])
1169 elif dev.dev_type in constants.DTS_DRBD:
1171 self.MapLVsByNode(lvmap, dev.children, dev.logical_id[0])
1172 self.MapLVsByNode(lvmap, dev.children, dev.logical_id[1])
1175 self.MapLVsByNode(lvmap, dev.children, node_uuid)
1179 def FindDisk(self, idx):
1180 """Find a disk given having a specified index.
1182 This is just a wrapper that does validation of the index.
1185 @param idx: the disk index
1187 @return: the corresponding disk
1188 @raise errors.OpPrereqError: when the given index is not valid
1193 return self.disks[idx]
1194 except (TypeError, ValueError), err:
1195 raise errors.OpPrereqError("Invalid disk index: '%s'" % str(err),
1198 raise errors.OpPrereqError("Invalid disk index: %d (instace has disks"
1199 " 0 to %d" % (idx, len(self.disks) - 1),
1203 """Instance-specific conversion to standard python types.
1205 This replaces the children lists of objects with lists of standard
1209 bo = super(Instance, self).ToDict()
1211 for attr in "nics", "disks":
1212 alist = bo.get(attr, None)
1214 nlist = outils.ContainerToDicts(alist)
1221 def FromDict(cls, val):
1222 """Custom function for instances.
1225 if "admin_state" not in val:
1226 if val.get("admin_up", False):
1227 val["admin_state"] = constants.ADMINST_UP
1229 val["admin_state"] = constants.ADMINST_DOWN
1230 if "admin_up" in val:
1232 obj = super(Instance, cls).FromDict(val)
1233 obj.nics = outils.ContainerFromDicts(obj.nics, list, NIC)
1234 obj.disks = outils.ContainerFromDicts(obj.disks, list, Disk)
1237 def UpgradeConfig(self):
1238 """Fill defaults for missing configuration values.
1241 for nic in self.nics:
1243 for disk in self.disks:
1244 disk.UpgradeConfig()
1246 for key in constants.HVC_GLOBALS:
1248 del self.hvparams[key]
1251 if self.osparams is None:
1253 UpgradeBeParams(self.beparams)
1254 if self.disks_active is None:
1255 self.disks_active = self.admin_state == constants.ADMINST_UP
1258 class OS(ConfigObject):
1259 """Config object representing an operating system.
1261 @type supported_parameters: list
1262 @ivar supported_parameters: a list of tuples, name and description,
1263 containing the supported parameters by this OS
1265 @type VARIANT_DELIM: string
1266 @cvar VARIANT_DELIM: the variant delimiter
1278 "supported_variants",
1279 "supported_parameters",
1285 def SplitNameVariant(cls, name):
1286 """Splits the name into the proper name and variant.
1288 @param name: the OS (unprocessed) name
1290 @return: a list of two elements; if the original name didn't
1291 contain a variant, it's returned as an empty string
1294 nv = name.split(cls.VARIANT_DELIM, 1)
1300 def GetName(cls, name):
1301 """Returns the proper name of the os (without the variant).
1303 @param name: the OS (unprocessed) name
1306 return cls.SplitNameVariant(name)[0]
1309 def GetVariant(cls, name):
1310 """Returns the variant the os (without the base name).
1312 @param name: the OS (unprocessed) name
1315 return cls.SplitNameVariant(name)[1]
1318 class ExtStorage(ConfigObject):
1319 """Config object representing an External Storage Provider.
1332 "supported_parameters",
1336 class NodeHvState(ConfigObject):
1337 """Hypvervisor state on a node.
1339 @ivar mem_total: Total amount of memory
1340 @ivar mem_node: Memory used by, or reserved for, the node itself (not always
1342 @ivar mem_hv: Memory used by hypervisor or lost due to instance allocation
1344 @ivar mem_inst: Memory used by instances living on node
1345 @ivar cpu_total: Total node CPU core count
1346 @ivar cpu_node: Number of CPU cores reserved for the node itself
1359 class NodeDiskState(ConfigObject):
1360 """Disk state on a node.
1370 class Node(TaggableObject):
1371 """Config object representing a node.
1373 @ivar hv_state: Hypervisor state (e.g. number of CPUs)
1374 @ivar hv_state_static: Hypervisor state overriden by user
1375 @ivar disk_state: Disk state (e.g. free space)
1376 @ivar disk_state_static: Disk state overriden by user
1395 "disk_state_static",
1396 ] + _TIMESTAMPS + _UUID
1398 def UpgradeConfig(self):
1399 """Fill defaults for missing configuration values.
1402 # pylint: disable=E0203
1403 # because these are "defined" via slots, not manually
1404 if self.master_capable is None:
1405 self.master_capable = True
1407 if self.vm_capable is None:
1408 self.vm_capable = True
1410 if self.ndparams is None:
1412 # And remove any global parameter
1413 for key in constants.NDC_GLOBALS:
1414 if key in self.ndparams:
1415 logging.warning("Ignoring %s node parameter for node %s",
1417 del self.ndparams[key]
1419 if self.powered is None:
1423 """Custom function for serializing.
1426 data = super(Node, self).ToDict()
1428 hv_state = data.get("hv_state", None)
1429 if hv_state is not None:
1430 data["hv_state"] = outils.ContainerToDicts(hv_state)
1432 disk_state = data.get("disk_state", None)
1433 if disk_state is not None:
1434 data["disk_state"] = \
1435 dict((key, outils.ContainerToDicts(value))
1436 for (key, value) in disk_state.items())
1441 def FromDict(cls, val):
1442 """Custom function for deserializing.
1445 obj = super(Node, cls).FromDict(val)
1447 if obj.hv_state is not None:
1449 outils.ContainerFromDicts(obj.hv_state, dict, NodeHvState)
1451 if obj.disk_state is not None:
1453 dict((key, outils.ContainerFromDicts(value, dict, NodeDiskState))
1454 for (key, value) in obj.disk_state.items())
1459 class NodeGroup(TaggableObject):
1460 """Config object representing a node group."""
1469 "disk_state_static",
1472 ] + _TIMESTAMPS + _UUID
1475 """Custom function for nodegroup.
1477 This discards the members object, which gets recalculated and is only kept
1481 mydict = super(NodeGroup, self).ToDict()
1482 del mydict["members"]
1486 def FromDict(cls, val):
1487 """Custom function for nodegroup.
1489 The members slot is initialized to an empty list, upon deserialization.
1492 obj = super(NodeGroup, cls).FromDict(val)
1496 def UpgradeConfig(self):
1497 """Fill defaults for missing configuration values.
1500 if self.ndparams is None:
1503 if self.serial_no is None:
1506 if self.alloc_policy is None:
1507 self.alloc_policy = constants.ALLOC_POLICY_PREFERRED
1509 # We only update mtime, and not ctime, since we would not be able
1510 # to provide a correct value for creation time.
1511 if self.mtime is None:
1512 self.mtime = time.time()
1514 if self.diskparams is None:
1515 self.diskparams = {}
1516 if self.ipolicy is None:
1517 self.ipolicy = MakeEmptyIPolicy()
1519 if self.networks is None:
1522 def FillND(self, node):
1523 """Return filled out ndparams for L{objects.Node}
1525 @type node: L{objects.Node}
1526 @param node: A Node object to fill
1527 @return a copy of the node's ndparams with defaults filled
1530 return self.SimpleFillND(node.ndparams)
1532 def SimpleFillND(self, ndparams):
1533 """Fill a given ndparams dict with defaults.
1535 @type ndparams: dict
1536 @param ndparams: the dict to fill
1538 @return: a copy of the passed in ndparams with missing keys filled
1539 from the node group defaults
1542 return FillDict(self.ndparams, ndparams)
1545 class Cluster(TaggableObject):
1546 """Config object representing the cluster."""
1551 "highest_used_port",
1554 "volume_group_name",
1556 "drbd_usermode_helper",
1558 "default_hypervisor",
1563 "use_external_mip_script",
1566 "shared_file_storage_dir",
1567 "enabled_hypervisors",
1576 "candidate_pool_size",
1579 "maintain_node_health",
1581 "default_iallocator",
1584 "primary_ip_family",
1585 "prealloc_wipe_disks",
1587 "disk_state_static",
1588 "enabled_disk_templates",
1589 ] + _TIMESTAMPS + _UUID
1591 def UpgradeConfig(self):
1592 """Fill defaults for missing configuration values.
1595 # pylint: disable=E0203
1596 # because these are "defined" via slots, not manually
1597 if self.hvparams is None:
1598 self.hvparams = constants.HVC_DEFAULTS
1600 for hypervisor in constants.HYPER_TYPES:
1602 existing_params = self.hvparams[hypervisor]
1604 existing_params = {}
1605 self.hvparams[hypervisor] = FillDict(
1606 constants.HVC_DEFAULTS[hypervisor], existing_params)
1608 if self.os_hvp is None:
1611 # osparams added before 2.2
1612 if self.osparams is None:
1615 self.ndparams = UpgradeNDParams(self.ndparams)
1617 self.beparams = UpgradeGroupedParams(self.beparams,
1618 constants.BEC_DEFAULTS)
1619 for beparams_group in self.beparams:
1620 UpgradeBeParams(self.beparams[beparams_group])
1622 migrate_default_bridge = not self.nicparams
1623 self.nicparams = UpgradeGroupedParams(self.nicparams,
1624 constants.NICC_DEFAULTS)
1625 if migrate_default_bridge:
1626 self.nicparams[constants.PP_DEFAULT][constants.NIC_LINK] = \
1629 if self.modify_etc_hosts is None:
1630 self.modify_etc_hosts = True
1632 if self.modify_ssh_setup is None:
1633 self.modify_ssh_setup = True
1635 # default_bridge is no longer used in 2.1. The slot is left there to
1636 # support auto-upgrading. It can be removed once we decide to deprecate
1637 # upgrading straight from 2.0.
1638 if self.default_bridge is not None:
1639 self.default_bridge = None
1641 # default_hypervisor is just the first enabled one in 2.1. This slot and
1642 # code can be removed once upgrading straight from 2.0 is deprecated.
1643 if self.default_hypervisor is not None:
1644 self.enabled_hypervisors = ([self.default_hypervisor] +
1645 [hvname for hvname in self.enabled_hypervisors
1646 if hvname != self.default_hypervisor])
1647 self.default_hypervisor = None
1649 # maintain_node_health added after 2.1.1
1650 if self.maintain_node_health is None:
1651 self.maintain_node_health = False
1653 if self.uid_pool is None:
1656 if self.default_iallocator is None:
1657 self.default_iallocator = ""
1659 # reserved_lvs added before 2.2
1660 if self.reserved_lvs is None:
1661 self.reserved_lvs = []
1663 # hidden and blacklisted operating systems added before 2.2.1
1664 if self.hidden_os is None:
1667 if self.blacklisted_os is None:
1668 self.blacklisted_os = []
1670 # primary_ip_family added before 2.3
1671 if self.primary_ip_family is None:
1672 self.primary_ip_family = AF_INET
1674 if self.master_netmask is None:
1675 ipcls = netutils.IPAddress.GetClassFromIpFamily(self.primary_ip_family)
1676 self.master_netmask = ipcls.iplen
1678 if self.prealloc_wipe_disks is None:
1679 self.prealloc_wipe_disks = False
1681 # shared_file_storage_dir added before 2.5
1682 if self.shared_file_storage_dir is None:
1683 self.shared_file_storage_dir = ""
1685 if self.use_external_mip_script is None:
1686 self.use_external_mip_script = False
1689 self.diskparams = UpgradeDiskParams(self.diskparams)
1691 self.diskparams = constants.DISK_DT_DEFAULTS.copy()
1693 # instance policy added before 2.6
1694 if self.ipolicy is None:
1695 self.ipolicy = FillIPolicy(constants.IPOLICY_DEFAULTS, {})
1697 # we can either make sure to upgrade the ipolicy always, or only
1698 # do it in some corner cases (e.g. missing keys); note that this
1699 # will break any removal of keys from the ipolicy dict
1700 wrongkeys = frozenset(self.ipolicy.keys()) - constants.IPOLICY_ALL_KEYS
1702 # These keys would be silently removed by FillIPolicy()
1703 msg = ("Cluster instance policy contains spurious keys: %s" %
1704 utils.CommaJoin(wrongkeys))
1705 raise errors.ConfigurationError(msg)
1706 self.ipolicy = FillIPolicy(constants.IPOLICY_DEFAULTS, self.ipolicy)
1709 def primary_hypervisor(self):
1710 """The first hypervisor is the primary.
1712 Useful, for example, for L{Node}'s hv/disk state.
1715 return self.enabled_hypervisors[0]
1718 """Custom function for cluster.
1721 mydict = super(Cluster, self).ToDict()
1723 if self.tcpudp_port_pool is None:
1724 tcpudp_port_pool = []
1726 tcpudp_port_pool = list(self.tcpudp_port_pool)
1728 mydict["tcpudp_port_pool"] = tcpudp_port_pool
1733 def FromDict(cls, val):
1734 """Custom function for cluster.
1737 obj = super(Cluster, cls).FromDict(val)
1739 if obj.tcpudp_port_pool is None:
1740 obj.tcpudp_port_pool = set()
1741 elif not isinstance(obj.tcpudp_port_pool, set):
1742 obj.tcpudp_port_pool = set(obj.tcpudp_port_pool)
1746 def SimpleFillDP(self, diskparams):
1747 """Fill a given diskparams dict with cluster defaults.
1749 @param diskparams: The diskparams
1750 @return: The defaults dict
1753 return FillDiskParams(self.diskparams, diskparams)
1755 def GetHVDefaults(self, hypervisor, os_name=None, skip_keys=None):
1756 """Get the default hypervisor parameters for the cluster.
1758 @param hypervisor: the hypervisor name
1759 @param os_name: if specified, we'll also update the defaults for this OS
1760 @param skip_keys: if passed, list of keys not to use
1761 @return: the defaults dict
1764 if skip_keys is None:
1767 fill_stack = [self.hvparams.get(hypervisor, {})]
1768 if os_name is not None:
1769 os_hvp = self.os_hvp.get(os_name, {}).get(hypervisor, {})
1770 fill_stack.append(os_hvp)
1773 for o_dict in fill_stack:
1774 ret_dict = FillDict(ret_dict, o_dict, skip_keys=skip_keys)
1778 def SimpleFillHV(self, hv_name, os_name, hvparams, skip_globals=False):
1779 """Fill a given hvparams dict with cluster defaults.
1781 @type hv_name: string
1782 @param hv_name: the hypervisor to use
1783 @type os_name: string
1784 @param os_name: the OS to use for overriding the hypervisor defaults
1785 @type skip_globals: boolean
1786 @param skip_globals: if True, the global hypervisor parameters will
1789 @return: a copy of the given hvparams with missing keys filled from
1790 the cluster defaults
1794 skip_keys = constants.HVC_GLOBALS
1798 def_dict = self.GetHVDefaults(hv_name, os_name, skip_keys=skip_keys)
1799 return FillDict(def_dict, hvparams, skip_keys=skip_keys)
1801 def FillHV(self, instance, skip_globals=False):
1802 """Fill an instance's hvparams dict with cluster defaults.
1804 @type instance: L{objects.Instance}
1805 @param instance: the instance parameter to fill
1806 @type skip_globals: boolean
1807 @param skip_globals: if True, the global hypervisor parameters will
1810 @return: a copy of the instance's hvparams with missing keys filled from
1811 the cluster defaults
1814 return self.SimpleFillHV(instance.hypervisor, instance.os,
1815 instance.hvparams, skip_globals)
1817 def SimpleFillBE(self, beparams):
1818 """Fill a given beparams dict with cluster defaults.
1820 @type beparams: dict
1821 @param beparams: the dict to fill
1823 @return: a copy of the passed in beparams with missing keys filled
1824 from the cluster defaults
1827 return FillDict(self.beparams.get(constants.PP_DEFAULT, {}), beparams)
1829 def FillBE(self, instance):
1830 """Fill an instance's beparams dict with cluster defaults.
1832 @type instance: L{objects.Instance}
1833 @param instance: the instance parameter to fill
1835 @return: a copy of the instance's beparams with missing keys filled from
1836 the cluster defaults
1839 return self.SimpleFillBE(instance.beparams)
1841 def SimpleFillNIC(self, nicparams):
1842 """Fill a given nicparams dict with cluster defaults.
1844 @type nicparams: dict
1845 @param nicparams: the dict to fill
1847 @return: a copy of the passed in nicparams with missing keys filled
1848 from the cluster defaults
1851 return FillDict(self.nicparams.get(constants.PP_DEFAULT, {}), nicparams)
1853 def SimpleFillOS(self, os_name, os_params):
1854 """Fill an instance's osparams dict with cluster defaults.
1856 @type os_name: string
1857 @param os_name: the OS name to use
1858 @type os_params: dict
1859 @param os_params: the dict to fill with default values
1861 @return: a copy of the instance's osparams with missing keys filled from
1862 the cluster defaults
1865 name_only = os_name.split("+", 1)[0]
1867 result = self.osparams.get(name_only, {})
1869 result = FillDict(result, self.osparams.get(os_name, {}))
1871 return FillDict(result, os_params)
1874 def SimpleFillHvState(hv_state):
1875 """Fill an hv_state sub dict with cluster defaults.
1878 return FillDict(constants.HVST_DEFAULTS, hv_state)
1881 def SimpleFillDiskState(disk_state):
1882 """Fill an disk_state sub dict with cluster defaults.
1885 return FillDict(constants.DS_DEFAULTS, disk_state)
1887 def FillND(self, node, nodegroup):
1888 """Return filled out ndparams for L{objects.NodeGroup} and L{objects.Node}
1890 @type node: L{objects.Node}
1891 @param node: A Node object to fill
1892 @type nodegroup: L{objects.NodeGroup}
1893 @param nodegroup: A Node object to fill
1894 @return a copy of the node's ndparams with defaults filled
1897 return self.SimpleFillND(nodegroup.FillND(node))
1899 def SimpleFillND(self, ndparams):
1900 """Fill a given ndparams dict with defaults.
1902 @type ndparams: dict
1903 @param ndparams: the dict to fill
1905 @return: a copy of the passed in ndparams with missing keys filled
1906 from the cluster defaults
1909 return FillDict(self.ndparams, ndparams)
1911 def SimpleFillIPolicy(self, ipolicy):
1912 """ Fill instance policy dict with defaults.
1915 @param ipolicy: the dict to fill
1917 @return: a copy of passed ipolicy with missing keys filled from
1918 the cluster defaults
1921 return FillIPolicy(self.ipolicy, ipolicy)
1923 def IsDiskTemplateEnabled(self, disk_template):
1924 """Checks if a particular disk template is enabled.
1927 return utils.storage.IsDiskTemplateEnabled(
1928 disk_template, self.enabled_disk_templates)
1930 def IsFileStorageEnabled(self):
1931 """Checks if file storage is enabled.
1934 return utils.storage.IsFileStorageEnabled(self.enabled_disk_templates)
1936 def IsSharedFileStorageEnabled(self):
1937 """Checks if shared file storage is enabled.
1940 return utils.storage.IsSharedFileStorageEnabled(
1941 self.enabled_disk_templates)
1944 class BlockDevStatus(ConfigObject):
1945 """Config object representing the status of a block device."""
1957 class ImportExportStatus(ConfigObject):
1958 """Config object representing the status of an import or export."""
1964 "progress_throughput",
1972 class ImportExportOptions(ConfigObject):
1973 """Options for import/export daemon
1975 @ivar key_name: X509 key name (None for cluster certificate)
1976 @ivar ca_pem: Remote peer CA in PEM format (None for cluster certificate)
1977 @ivar compress: Compression method (one of L{constants.IEC_ALL})
1978 @ivar magic: Used to ensure the connection goes to the right disk
1979 @ivar ipv6: Whether to use IPv6
1980 @ivar connect_timeout: Number of seconds for establishing connection
1993 class ConfdRequest(ConfigObject):
1994 """Object holding a confd request.
1996 @ivar protocol: confd protocol version
1997 @ivar type: confd query type
1998 @ivar query: query request
1999 @ivar rsalt: requested reply salt
2010 class ConfdReply(ConfigObject):
2011 """Object holding a confd reply.
2013 @ivar protocol: confd protocol version
2014 @ivar status: reply status code (ok, error)
2015 @ivar answer: confd query reply
2016 @ivar serial: configuration serial number
2027 class QueryFieldDefinition(ConfigObject):
2028 """Object holding a query field definition.
2030 @ivar name: Field name
2031 @ivar title: Human-readable title
2032 @ivar kind: Field type
2033 @ivar doc: Human-readable description
2044 class _QueryResponseBase(ConfigObject):
2050 """Custom function for serializing.
2053 mydict = super(_QueryResponseBase, self).ToDict()
2054 mydict["fields"] = outils.ContainerToDicts(mydict["fields"])
2058 def FromDict(cls, val):
2059 """Custom function for de-serializing.
2062 obj = super(_QueryResponseBase, cls).FromDict(val)
2064 outils.ContainerFromDicts(obj.fields, list, QueryFieldDefinition)
2068 class QueryResponse(_QueryResponseBase):
2069 """Object holding the response to a query.
2071 @ivar fields: List of L{QueryFieldDefinition} objects
2072 @ivar data: Requested data
2080 class QueryFieldsRequest(ConfigObject):
2081 """Object holding a request for querying available fields.
2090 class QueryFieldsResponse(_QueryResponseBase):
2091 """Object holding the response to a query for fields.
2093 @ivar fields: List of L{QueryFieldDefinition} objects
2099 class MigrationStatus(ConfigObject):
2100 """Object holding the status of a migration.
2110 class InstanceConsole(ConfigObject):
2111 """Object describing how to access the console of an instance.
2126 """Validates contents of this object.
2129 assert self.kind in constants.CONS_ALL, "Unknown console type"
2130 assert self.instance, "Missing instance name"
2131 assert self.message or self.kind in [constants.CONS_SSH,
2132 constants.CONS_SPICE,
2134 assert self.host or self.kind == constants.CONS_MESSAGE
2135 assert self.port or self.kind in [constants.CONS_MESSAGE,
2137 assert self.user or self.kind in [constants.CONS_MESSAGE,
2138 constants.CONS_SPICE,
2140 assert self.command or self.kind in [constants.CONS_MESSAGE,
2141 constants.CONS_SPICE,
2143 assert self.display or self.kind in [constants.CONS_MESSAGE,
2144 constants.CONS_SPICE,
2149 class Network(TaggableObject):
2150 """Object representing a network definition for ganeti.
2163 ] + _TIMESTAMPS + _UUID
2165 def HooksDict(self, prefix=""):
2166 """Export a dictionary used by hooks with a network's information.
2168 @type prefix: String
2169 @param prefix: Prefix to prepend to the dict entries
2173 "%sNETWORK_NAME" % prefix: self.name,
2174 "%sNETWORK_UUID" % prefix: self.uuid,
2175 "%sNETWORK_TAGS" % prefix: " ".join(self.GetTags()),
2178 result["%sNETWORK_SUBNET" % prefix] = self.network
2180 result["%sNETWORK_GATEWAY" % prefix] = self.gateway
2182 result["%sNETWORK_SUBNET6" % prefix] = self.network6
2184 result["%sNETWORK_GATEWAY6" % prefix] = self.gateway6
2186 result["%sNETWORK_MAC_PREFIX" % prefix] = self.mac_prefix
2191 def FromDict(cls, val):
2192 """Custom function for networks.
2194 Remove deprecated network_type and family.
2197 if "network_type" in val:
2198 del val["network_type"]
2201 obj = super(Network, cls).FromDict(val)
2205 # need to inherit object in order to use super()
2206 class SerializableConfigParser(ConfigParser.SafeConfigParser, object):
2207 """Simple wrapper over ConfigParse that allows serialization.
2209 This class is basically ConfigParser.SafeConfigParser with two
2210 additional methods that allow it to serialize/unserialize to/from a
2215 """Dump this instance and return the string representation."""
2218 return buf.getvalue()
2221 def Loads(cls, data):
2222 """Load data from a string."""
2223 buf = StringIO(data)
2228 def get(self, section, option, **kwargs):
2231 value = super(SerializableConfigParser, self).get(section, option,
2233 if value.lower() == constants.VALUE_NONE:
2235 except ConfigParser.NoOptionError:
2236 r = re.compile(r"(disk|nic)\d+_name|nic\d+_(network|vlan)")
2237 match = r.match(option)
2246 class LvmPvInfo(ConfigObject):
2247 """Information about an LVM physical volume (PV).
2250 @ivar name: name of the PV
2251 @type vg_name: string
2252 @ivar vg_name: name of the volume group containing the PV
2254 @ivar size: size of the PV in MiB
2256 @ivar free: free space in the PV, in MiB
2257 @type attributes: string
2258 @ivar attributes: PV attributes
2259 @type lv_list: list of strings
2260 @ivar lv_list: names of the LVs hosted on the PV
2272 """Is this PV empty?
2275 return self.size <= (self.free + 1)
2277 def IsAllocatable(self):
2278 """Is this PV allocatable?
2281 return ("a" in self.attributes)