Revision 6f011a2d snf-cyclades-app/synnefo/db/migrations/0037_network_migration.py

b/snf-cyclades-app/synnefo/db/migrations/0037_network_migration.py
5 5
from django.db import models
6 6
from synnefo import settings
7 7

  
8
def get_available_bridge(orm):
9
    try:
10
        entry = orm.BridgePool.objects.filter(available=True)[0]
11
        entry.available = False
12
        entry.save()
13
        return entry
14
    except IndexError:
15
        try:
16
            last = orm.BridgePool.objects.order_by('-index')[0]
17
            index = last.index + 1
18
        except IndexError:
19
            index = 1
20

  
21
        if index <= settings.PRIVATE_PHYSICAL_VLAN_MAX_NUMBER:
22
            create_bridge = orm.BridgePool.objects.create
23
            return create_bridge(index=index,
24
                                 value=value_from_index(index),
25
                                 available=False)
26
        raise Exception('Pool of bridges exhausted. Can not'
27
                        ' allocate bridge')
28

  
29
def value_from_index(index):
30
    return settings.PRIVATE_PHYSICAL_VLAN_BRIDGE_PREFIX + str(index)
31

  
32 8
class Migration(DataMigration):
33
    
9

  
34 10
    def forwards(self, orm):
35 11
        "Write your forwards methods here."
36 12

  
37 13
        print '\033[91m' + 'Subnet of all networks is set to 10.0.0.0/24.'
38 14
        print 'Gateway of all networks is set to null.'
39
        print "Use `snf-manage modifynetwork` to change them." + '\033[0m'
15
        print "Use `snf-manage network-modify` to change them." + '\033[0m'
16

  
17
        create_bridge = orm.BridgePool.objects.create
18
        for link in orm.NetworkLink.objects.all():
19
            create_bridge(index=link.index,
20
                          value=link.name,
21
                          available=link.available)
40 22

  
41 23
        for network in orm.Network.objects.all():
42 24
            if network.state == 'DELETED':
43 25
                network.deleted = True
44 26

  
27
            network.netlink = network.link.name
28
            try:
29
                base = settings.MAC_POOL_BASE
30
                assert(len(base) == 7)
31
                network.mac_prefix = base
32
            except AttributeError:
33
                network.mac_prefix = 'aa:00:0'
34

  
45 35
            if network.public == True:
46 36
                # Public Network
47
                network.netlink = network.link.name
48 37
                network.dhcp = True
49 38
                network.type = 'PUBLIC_ROUTED'
50 39
            else:
51 40
                network.dhcp = False
52 41
                network.type = 'PRIVATE_PHYSICAL_VLAN'
53
                if network.deleted:
54
                    try:
55
                        link = settings.PRIVATE_PHYSICAL_VLAN_BRIDGE_PREFIX + '0'
56
                    except AttributeError:
57
                        link = 'prv0'
58
                    network.netlink = link
59
                else:
60
                    # Non-deleted Network. Allocate A Bridge
61
                    entry = get_available_bridge(orm)
62
                    bridge = entry
63
                    network.netlink = bridge.value
64 42

  
65 43
            network.save()
66
    
67
    
44

  
45

  
68 46
    def backwards(self, orm):
69 47
        "Write your backwards methods here."
70 48
        pass
71
    
49

  
72 50
    models = {
73 51
        'db.backend': {
74 52
            'Meta': {'object_name': 'Backend'},

Also available in: Unified diff