diff --git a/lib/config.py b/lib/config.py index 2cd0646e6152b66ce1d3b3b0a70cfd43674e74a1..9c9a48f22fd11d0bc1706a10d4c4155f2e558d96 100644 --- a/lib/config.py +++ b/lib/config.py @@ -39,7 +39,6 @@ import random import logging import time import itertools -from functools import wraps from ganeti import errors from ganeti import locking @@ -162,17 +161,6 @@ def _CheckInstanceDiskIvNames(disks): return result -def _GenerateMACSuffix(): - """Generate one mac address - - """ - byte1 = random.randrange(0, 256) - byte2 = random.randrange(0, 256) - byte3 = random.randrange(0, 256) - suffix = "%02x:%02x:%02x" % (byte1, byte2, byte3) - return suffix - - class ConfigWriter: """The interface to the cluster configuration. @@ -230,21 +218,6 @@ class ConfigWriter: """ return os.path.exists(pathutils.CLUSTER_CONF_FILE) - def _GenerateMACPrefix(self, net=None): - def _get_mac_prefix(view_func): - def _decorator(*args, **kwargs): - prefix = self._config_data.cluster.mac_prefix - if net: - net_uuid = self._UnlockedLookupNetwork(net) - if net_uuid: - nobj = self._UnlockedGetNetwork(net_uuid) - if nobj.mac_prefix: - prefix = nobj.mac_prefix - suffix = view_func(*args, **kwargs) - return prefix + ':' + suffix - return wraps(view_func)(_decorator) - return _get_mac_prefix - @locking.ssynchronized(_config_lock, shared=1) def GetNdParams(self, node): """Get the node params populated with cluster defaults. @@ -291,6 +264,38 @@ class ConfigWriter: """ return self._config_data.cluster.SimpleFillDP(group.diskparams) + def _UnlockedGetNetworkMACPrefix(self, net): + """Return the network mac prefix if it exists or the cluster level default. + + """ + prefix = None + if net: + net_uuid = self._UnlockedLookupNetwork(net) + if net_uuid: + nobj = self._UnlockedGetNetwork(net_uuid) + if nobj.mac_prefix: + prefix = nobj.mac_prefix + + return prefix + + def _GenerateOneMAC(self, prefix=None): + """Return a function that randomly generates a MAC suffic + and appends it to the given prefix. If prefix is not given get + the cluster level default. + + """ + if not prefix: + prefix = self._config_data.cluster.mac_prefix + + def GenMac(): + byte1 = random.randrange(0, 256) + byte2 = random.randrange(0, 256) + byte3 = random.randrange(0, 256) + mac = "%s:%02x:%02x:%02x" % (prefix, byte1, byte2, byte3) + return mac + + return GenMac + @locking.ssynchronized(_config_lock, shared=1) def GenerateMAC(self, net, ec_id): """Generate a MAC for an instance. @@ -299,7 +304,8 @@ class ConfigWriter: """ existing = self._AllMACs() - gen_mac = self._GenerateMACPrefix(net)(_GenerateMACSuffix) + prefix = self._UnlockedGetNetworkMACPrefix(net) + gen_mac = self._GenerateOneMAC(prefix) return self._temporary_ids.Generate(existing, gen_mac, ec_id) @locking.ssynchronized(_config_lock, shared=1)