Revision d0411c0a snf-cyclades-app/synnefo/db/migrations/0048_network_mac_prefix.py
b/snf-cyclades-app/synnefo/db/migrations/0048_network_mac_prefix.py | ||
---|---|---|
3 | 3 |
from south.db import db |
4 | 4 |
from south.v2 import DataMigration |
5 | 5 |
from django.db import models |
6 |
from synnefo.db.models import MacPrefixPool |
|
6 |
from synnefo import settings |
|
7 |
|
|
8 |
def get_available_mac_prefix(orm): |
|
9 |
try: |
|
10 |
entry = orm.MacPrefixPool.objects.filter(available=True)[0] |
|
11 |
entry.available = False |
|
12 |
entry.save() |
|
13 |
return entry |
|
14 |
except IndexError: |
|
15 |
try: |
|
16 |
last = orm.MacPrefixPool.objects.order_by('-index')[0] |
|
17 |
index = last.index + 1 |
|
18 |
except IndexError: |
|
19 |
index = 1 |
|
20 |
|
|
21 |
if index <= settings.MAC_POOL_LIMIT: |
|
22 |
create_mac_prefix = orm.MacPrefixPool.objects.create |
|
23 |
return create_mac_prefix(index=index, |
|
24 |
value=value_from_index(index), |
|
25 |
available=False) |
|
26 |
raise Exception('Pool of mac_prefixes exhausted. Can not' |
|
27 |
' allocate mac_prefix.') |
|
28 |
|
|
29 |
def value_from_index(index): |
|
30 |
"""Convert number to mac prefix |
|
31 |
|
|
32 |
""" |
|
33 |
base = settings.MAC_POOL_BASE |
|
34 |
a = hex(int(base.replace(":", ""), 16) + index).replace("0x", '') |
|
35 |
mac_prefix = ":".join([a[x:x + 2] for x in xrange(0, len(a), 2)]) |
|
36 |
return mac_prefix |
|
7 | 37 |
|
8 | 38 |
class Migration(DataMigration): |
9 | 39 |
|
... | ... | |
15 | 45 |
|
16 | 46 |
for network in orm.Network.objects.order_by('id'): |
17 | 47 |
if network.deleted: |
18 |
network.mac_prefix = 'aa:00:0'
|
|
48 |
network.mac_prefix = settings.MAC_POOL_BASE
|
|
19 | 49 |
else: |
20 |
mac_prefix = MacPrefixPool.get_available().value
|
|
21 |
network.mac_prefix = mac_prefix |
|
50 |
mac_prefix = get_available_mac_prefix(orm)
|
|
51 |
network.mac_prefix = mac_prefix.value
|
|
22 | 52 |
network.save() |
23 | 53 |
|
24 | 54 |
for backend_network in orm.BackendNetwork.objects.order_by('id'): |
Also available in: Unified diff