Revision 032a7d71

b/lib/config.py
39 39
import logging
40 40
import time
41 41
import itertools
42
from functools import wraps
43 42

  
44 43
from ganeti import errors
45 44
from ganeti import locking
......
162 161
  return result
163 162

  
164 163

  
165
def _GenerateMACSuffix():
166
  """Generate one mac address
167

  
168
  """
169
  byte1 = random.randrange(0, 256)
170
  byte2 = random.randrange(0, 256)
171
  byte3 = random.randrange(0, 256)
172
  suffix = "%02x:%02x:%02x" % (byte1, byte2, byte3)
173
  return suffix
174

  
175

  
176 164
class ConfigWriter:
177 165
  """The interface to the cluster configuration.
178 166

  
......
230 218
    """
231 219
    return os.path.exists(pathutils.CLUSTER_CONF_FILE)
232 220

  
233
  def _GenerateMACPrefix(self, net=None):
234
    def _get_mac_prefix(view_func):
235
      def _decorator(*args, **kwargs):
236
        prefix = self._config_data.cluster.mac_prefix
237
        if net:
238
          net_uuid = self._UnlockedLookupNetwork(net)
239
          if net_uuid:
240
            nobj = self._UnlockedGetNetwork(net_uuid)
241
            if nobj.mac_prefix:
242
              prefix = nobj.mac_prefix
243
        suffix = view_func(*args, **kwargs)
244
        return prefix + ':' + suffix
245
      return wraps(view_func)(_decorator)
246
    return _get_mac_prefix
247

  
248 221
  @locking.ssynchronized(_config_lock, shared=1)
249 222
  def GetNdParams(self, node):
250 223
    """Get the node params populated with cluster defaults.
......
291 264
    """
292 265
    return self._config_data.cluster.SimpleFillDP(group.diskparams)
293 266

  
267
  def _UnlockedGetNetworkMACPrefix(self, net):
268
    """Return the network mac prefix if it exists or the cluster level default.
269

  
270
    """
271
    prefix = None
272
    if net:
273
      net_uuid = self._UnlockedLookupNetwork(net)
274
      if net_uuid:
275
        nobj = self._UnlockedGetNetwork(net_uuid)
276
        if nobj.mac_prefix:
277
          prefix = nobj.mac_prefix
278

  
279
    return prefix
280

  
281
  def _GenerateOneMAC(self, prefix=None):
282
    """Return a function that randomly generates a MAC suffic
283
       and appends it to the given prefix. If prefix is not given get
284
       the cluster level default.
285

  
286
    """
287
    if not prefix:
288
      prefix = self._config_data.cluster.mac_prefix
289

  
290
    def GenMac():
291
      byte1 = random.randrange(0, 256)
292
      byte2 = random.randrange(0, 256)
293
      byte3 = random.randrange(0, 256)
294
      mac = "%s:%02x:%02x:%02x" % (prefix, byte1, byte2, byte3)
295
      return mac
296

  
297
    return GenMac
298

  
294 299
  @locking.ssynchronized(_config_lock, shared=1)
295 300
  def GenerateMAC(self, net, ec_id):
296 301
    """Generate a MAC for an instance.
......
299 304

  
300 305
    """
301 306
    existing = self._AllMACs()
302
    gen_mac = self._GenerateMACPrefix(net)(_GenerateMACSuffix)
307
    prefix = self._UnlockedGetNetworkMACPrefix(net)
308
    gen_mac = self._GenerateOneMAC(prefix)
303 309
    return self._temporary_ids.Generate(existing, gen_mac, ec_id)
304 310

  
305 311
  @locking.ssynchronized(_config_lock, shared=1)

Also available in: Unified diff