X-Git-Url: https://code.grnet.gr/git/ganeti-local/blobdiff_plain/b894f5a892a14327e9bd63260f4cdfd9c205fd34..087ed2edee08da7bd3c4872cabde13c57585ca5a:/lib/objects.py diff --git a/lib/objects.py b/lib/objects.py index 5282e9f..e2154db 100644 --- a/lib/objects.py +++ b/lib/objects.py @@ -29,6 +29,7 @@ pass to and from external parties. import ConfigParser import re +import copy from cStringIO import StringIO from ganeti import errors @@ -36,7 +37,41 @@ from ganeti import constants __all__ = ["ConfigObject", "ConfigData", "NIC", "Disk", "Instance", - "OS", "Node", "Cluster"] + "OS", "Node", "Cluster", "FillDict"] + +_TIMESTAMPS = ["ctime", "mtime"] + +def FillDict(defaults_dict, custom_dict): + """Basic function to apply settings on top a default dict. + + @type defaults_dict: dict + @param defaults_dict: dictionary holding the default values + @type custom_dict: dict + @param custom_dict: dictionary holding customized value + @rtype: dict + @return: dict with the 'full' values + + """ + ret_dict = copy.deepcopy(defaults_dict) + ret_dict.update(custom_dict) + return ret_dict + + +def UpgradeGroupedParams(target, defaults): + """Update all groups for the target parameter. + + @type target: dict of dicts + @param target: {group: {parameter: value}} + @type defaults: dict + @param defaults: default parameter values + + """ + if target is None: + target = {constants.PP_DEFAULT: defaults} + else: + for group in target: + target[group] = FillDict(defaults, target[group]) + return target class ConfigObject(object): @@ -49,7 +84,7 @@ class ConfigObject(object): as None instead of raising an error Classes derived from this must always declare __slots__ (we use many - config objects and the memory reduction is useful. + config objects and the memory reduction is useful) """ __slots__ = [] @@ -57,6 +92,7 @@ class ConfigObject(object): def __init__(self, **kwargs): for k, v in kwargs.iteritems(): setattr(self, k, v) + self.UpgradeConfig() def __getattr__(self, name): if name not in self.__slots__: @@ -64,18 +100,6 @@ class ConfigObject(object): (type(self).__name__, name)) return None - def __setitem__(self, key, value): - if key not in self.__slots__: - raise KeyError(key) - setattr(self, key, value) - - def __getstate__(self): - state = {} - for name in self.__slots__: - if hasattr(self, name): - state[name] = getattr(self, name) - return state - def __setstate__(self, state): for name in state: if name in self.__slots__: @@ -91,7 +115,14 @@ class ConfigObject(object): make sure all objects returned are only standard python types. """ - return dict([(k, getattr(self, k, None)) for k in self.__slots__]) + result = {} + for name in self.__slots__: + value = getattr(self, name, None) + if value is not None: + result[name] = value + return result + + __getstate__ = ToDict @classmethod def FromDict(cls, val): @@ -152,10 +183,27 @@ class ConfigObject(object): " _ContainerFromDicts" % c_type) return ret + def Copy(self): + """Makes a deep copy of the current object and its children. + + """ + dict_form = self.ToDict() + clone_obj = self.__class__.FromDict(dict_form) + return clone_obj + def __repr__(self): """Implement __repr__ for ConfigObjects.""" return repr(self.ToDict()) + def UpgradeConfig(self): + """Fill defaults for missing configuration values. + + This method will be called at object init time, and its implementation will + be object dependent. + + """ + pass + class TaggableObject(ConfigObject): """An generic class supporting tags. @@ -178,7 +226,7 @@ class TaggableObject(ConfigObject): constants.MAX_TAG_LEN) if not tag: raise errors.TagError("Tags cannot be empty") - if not re.match("^[ \w.+*/:-]+$", tag): + if not re.match("^[\w.+*/:-]+$", tag): raise errors.TagError("Tag contains invalid characters") def GetTags(self): @@ -237,7 +285,8 @@ class TaggableObject(ConfigObject): class ConfigData(ConfigObject): """Top-level config object.""" - __slots__ = ["cluster", "nodes", "instances"] + __slots__ = (["version", "cluster", "nodes", "instances", "serial_no"] + + _TIMESTAMPS) def ToDict(self): """Custom function for top-level config data. @@ -267,13 +316,45 @@ class ConfigData(ConfigObject): class NIC(ConfigObject): """Config object representing a network card.""" - __slots__ = ["mac", "ip", "bridge"] + __slots__ = ["mac", "ip", "bridge", "nicparams"] + + @classmethod + def CheckParameterSyntax(cls, nicparams): + """Check the given parameters for validity. + + @type nicparams: dict + @param nicparams: dictionary with parameter names/value + @raise errors.ConfigurationError: when a parameter is not valid + + """ + if nicparams[constants.NIC_MODE] not in constants.NIC_VALID_MODES: + err = "Invalid nic mode: %s" % nicparams[constants.NIC_MODE] + raise errors.ConfigurationError(err) + + if (nicparams[constants.NIC_MODE] is constants.NIC_MODE_BRIDGED and + not nicparams[constants.NIC_LINK]): + err = "Missing bridged nic link" + raise errors.ConfigurationError(err) + + def UpgradeConfig(self): + """Fill defaults for missing configuration values. + + """ + if self.nicparams is None: + self.nicparams = {} + if self.bridge is not None: + self.nicparams[constants.NIC_MODE] = constants.NIC_MODE_BRIDGED + self.nicparams[constants.NIC_LINK] = self.bridge + # bridge is no longer used it 2.1. The slot is left there to support + # upgrading, but will be removed in 2.2 + if self.bridge is not None: + self.bridge = None class Disk(ConfigObject): """Config object representing a block device.""" __slots__ = ["dev_type", "logical_id", "physical_id", - "children", "iv_name", "size"] + "children", "iv_name", "size", "mode"] def CreateOnSecondary(self): """Test if this device needs to be created on a secondary node.""" @@ -387,6 +468,15 @@ class Disk(ConfigObject): raise errors.ProgrammerError("Disk.RecordGrow called for unsupported" " disk type %s" % self.dev_type) + def UnsetSize(self): + """Sets recursively the size to zero for the disk and its children. + + """ + if self.children: + for child in self.children: + child.UnsetSize() + self.size = 0 + def SetPhysicalID(self, target_node, nodes_ip): """Convert the logical ID to the physical ID. @@ -412,7 +502,7 @@ class Disk(ConfigObject): if self.logical_id is None and self.physical_id is not None: return if self.dev_type in constants.LDS_DRBD: - pnode, snode, port = self.logical_id + pnode, snode, port, pminor, sminor, secret = self.logical_id if target_node not in (pnode, snode): raise errors.ConfigurationError("DRBD device not knowing node %s" % target_node) @@ -421,12 +511,12 @@ class Disk(ConfigObject): if pnode_ip is None or snode_ip is None: raise errors.ConfigurationError("Can't find primary or secondary node" " for %s" % str(self)) + p_data = (pnode_ip, port) + s_data = (snode_ip, port) if pnode == target_node: - self.physical_id = (pnode_ip, port, - snode_ip, port) + self.physical_id = p_data + s_data + (pminor, secret) else: # it must be secondary, we tested above - self.physical_id = (snode_ip, port, - pnode_ip, port) + self.physical_id = s_data + p_data + (sminor, secret) else: self.physical_id = self.logical_id return @@ -458,6 +548,10 @@ class Disk(ConfigObject): obj.logical_id = tuple(obj.logical_id) if obj.physical_id and isinstance(obj.physical_id, list): obj.physical_id = tuple(obj.physical_id) + if obj.dev_type in constants.LDS_DRBD: + # we need a tuple of length six here + if len(obj.logical_id) < 6: + obj.logical_id += (None,) * (6 - len(obj.logical_id)) return obj def __str__(self): @@ -467,6 +561,7 @@ class Disk(ConfigObject): if self.dev_type == constants.LD_LV: val = "