Commit 032a7d71 authored by Dimitris Aragiorgis's avatar Dimitris Aragiorgis Committed by Iustin Pop
Browse files

Simplify mac generation funtions



Introduce new config method _UnlockedGetNetworkMACPrefix and refactor
original _GenerateOneMAC so that it takes prefix as an optional
argument and returns a function that actually generates a MAC. If
prefix is not given or None it uses the cluster level default.
Signed-off-by: default avatarDimitris Aragiorgis <dimara@grnet.gr>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 538427e6
......@@ -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)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment