Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / db / migrations / 0052_migrate_pool_entries.py @ 698306b8

History | View | Annotate | Download (15.4 kB)

1
# encoding: utf-8
2
import datetime
3
from south.db import db
4
from south.v2 import DataMigration
5
from django.db import models
6
from synnefo import settings
7
from bitarray import bitarray
8
from base64 import b64encode
9

    
10
def create_empty_pool(size):
11
    ba = bitarray(size)
12
    ba.setall(True)
13
    return ba
14

    
15
def bitarray_to_string(bitarray_):
16
    return b64encode(bitarray_.tobytes())
17

    
18
def validate_mac(value):
19
    hex_ = value.replace(":", "")
20
    bin_ = bin(int(hex_, 16))[2:].zfill(8)
21
    return bin_[6] == '1' and bin_[7] == '0'
22

    
23
def mac_from_index(base, index):
24
    """Convert number to mac prefix
25

26
    """
27
    a = hex(int(base.replace(":", ""), 16) + index).replace("0x", '')
28
    mac_prefix = ":".join([a[x:x + 2] for x in xrange(0, len(a), 2)])
29
    return mac_prefix
30

    
31
class Migration(DataMigration):
32
    
33
    def forwards(self, orm):
34
        "Write your forwards methods here."
35

    
36
        # Migrate Bridge Pool
37
        bridges = orm.BridgePool.objects.all()
38
        if bridges:
39
            try:
40
                pool_prefix = settings.PRIVATE_PHYSICAL_VLAN_BRIDGE_PREFIX
41
            except AttributeError:
42
                pool_prefix = ''
43

    
44
            try:
45
                pool_length = settings.PRIVATE_PHYSICAL_VLAN_MAX_NUMBER
46
            except:
47
                pool_length = len(bridges)
48

    
49
            reserved = create_empty_pool(pool_length)
50
            available = create_empty_pool(pool_length)
51

    
52
            for bridge in bridges:
53
                if not bridge.available:
54
                    # Bridge indexes in old BridgePool
55
                    available[bridge.index - 1] = False
56
            available_map = bitarray_to_string(available)
57
            reserved_map = bitarray_to_string(reserved)
58

    
59
            orm.BridgePoolTable.objects.create(available_map=available_map,
60
                                               reserved_map=reserved_map,
61
                                               size=pool_length,
62
                                               base=pool_prefix)
63

    
64

    
65
        mac_prefixes = orm.MacPrefixPool.objects.all()
66
        if mac_prefixes:
67
            try:
68
                macp_base = settings.MAC_POOL_BASE
69
            except AttributeError:
70
                first = mac_prefixes[0]
71
                first[-1]= '0'
72
                macp_base =  first
73

    
74
            try:
75
                macp_length = settings.MAC_POOL_LIMIT
76
            except:
77
                macp_length = len(bridges)
78

    
79
            reserved = create_empty_pool(macp_length)
80
            available = create_empty_pool(macp_length)
81

    
82
            for macp in mac_prefixes:
83
                if not macp.available:
84
                    available[macp.index - 1] = False
85

    
86
            for index in range(0, macp_length):
87
                mac = mac_from_index(macp_base, index)
88
                if not validate_mac(mac):
89
                    reserved[index] = False
90

    
91
            available_map = bitarray_to_string(available)
92
            reserved_map = bitarray_to_string(reserved)
93

    
94
            orm.MacPrefixPoolTable.objects.create(available_map=available_map,
95
                                               reserved_map=reserved_map,
96
                                               size=macp_length,
97
                                               base=macp_base)
98

    
99
    def backwards(self, orm):
100
        "Write your backwards methods here."
101
        raise RuntimeError("Can not reverse this migrations")
102

    
103
    models = {
104
        'db.backend': {
105
            'Meta': {'object_name': 'Backend'},
106
            'clustername': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '128'}),
107
            'ctotal': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
108
            'dfree': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
109
            'drained': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}),
110
            'dtotal': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
111
            'hash': ('django.db.models.fields.CharField', [], {'max_length': '40'}),
112
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
113
            'index': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0', 'unique': 'True'}),
114
            'mfree': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
115
            'mtotal': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
116
            'offline': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}),
117
            'password_hash': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True', 'blank': 'True'}),
118
            'pinst_cnt': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
119
            'port': ('django.db.models.fields.PositiveIntegerField', [], {'default': '5080'}),
120
            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
121
            'username': ('django.db.models.fields.CharField', [], {'max_length': '64', 'null': 'True', 'blank': 'True'})
122
        },
123
        'db.backendnetwork': {
124
            'Meta': {'unique_together': "(('network', 'backend'),)", 'object_name': 'BackendNetwork'},
125
            'backend': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'networks'", 'to': "orm['db.Backend']"}),
126
            'backendjobid': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True'}),
127
            'backendjobstatus': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
128
            'backendlogmsg': ('django.db.models.fields.TextField', [], {'null': 'True'}),
129
            'backendopcode': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
130
            'backendtime': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(1, 1, 1, 0, 0)'}),
131
            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
132
            'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}),
133
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
134
            'mac_prefix': ('django.db.models.fields.CharField', [], {'max_length': '32'}),
135
            'network': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'backend_networks'", 'to': "orm['db.Network']"}),
136
            'operstate': ('django.db.models.fields.CharField', [], {'default': "'PENDING'", 'max_length': '30'}),
137
            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'})
138
        },
139
        'db.bridgepool': {
140
            'Meta': {'object_name': 'BridgePool'},
141
            'available': ('django.db.models.fields.BooleanField', [], {'default': 'True', 'blank': 'True'}),
142
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
143
            'index': ('django.db.models.fields.IntegerField', [], {'unique': 'True'}),
144
            'value': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '128'})
145
        },
146
        'db.bridgepooltable': {
147
            'Meta': {'object_name': 'BridgePoolTable'},
148
            'available_map': ('django.db.models.fields.TextField', [], {'default': "''"}),
149
            'base': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True'}),
150
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
151
            'offset': ('django.db.models.fields.IntegerField', [], {'null': 'True'}),
152
            'reserved_map': ('django.db.models.fields.TextField', [], {'default': "''"}),
153
            'size': ('django.db.models.fields.IntegerField', [], {})
154
        },
155
        'db.flavor': {
156
            'Meta': {'unique_together': "(('cpu', 'ram', 'disk', 'disk_template'),)", 'object_name': 'Flavor'},
157
            'cpu': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
158
            'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}),
159
            'disk': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
160
            'disk_template': ('django.db.models.fields.CharField', [], {'default': "'plain'", 'max_length': '32'}),
161
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
162
            'ram': ('django.db.models.fields.IntegerField', [], {'default': '0'})
163
        },
164
        'db.macprefixpool': {
165
            'Meta': {'object_name': 'MacPrefixPool'},
166
            'available': ('django.db.models.fields.BooleanField', [], {'default': 'True', 'blank': 'True'}),
167
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
168
            'index': ('django.db.models.fields.IntegerField', [], {'unique': 'True'}),
169
            'value': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '128'})
170
        },
171
        'db.macprefixpooltable': {
172
            'Meta': {'object_name': 'MacPrefixPoolTable'},
173
            'available_map': ('django.db.models.fields.TextField', [], {'default': "''"}),
174
            'base': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True'}),
175
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
176
            'offset': ('django.db.models.fields.IntegerField', [], {'null': 'True'}),
177
            'reserved_map': ('django.db.models.fields.TextField', [], {'default': "''"}),
178
            'size': ('django.db.models.fields.IntegerField', [], {})
179
        },
180
        'db.network': {
181
            'Meta': {'object_name': 'Network'},
182
            'action': ('django.db.models.fields.CharField', [], {'default': 'None', 'max_length': '32', 'null': 'True'}),
183
            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
184
            'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}),
185
            'dhcp': ('django.db.models.fields.BooleanField', [], {'default': 'True', 'blank': 'True'}),
186
            'gateway': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True'}),
187
            'gateway6': ('django.db.models.fields.CharField', [], {'max_length': '64', 'null': 'True'}),
188
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
189
            'link': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True'}),
190
            'mac_prefix': ('django.db.models.fields.CharField', [], {'max_length': '32'}),
191
            'machines': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['db.VirtualMachine']", 'through': "orm['db.NetworkInterface']", 'symmetrical': 'False'}),
192
            'name': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
193
            'public': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}),
194
            'reservations': ('django.db.models.fields.TextField', [], {'default': "''"}),
195
            'state': ('django.db.models.fields.CharField', [], {'default': "'PENDING'", 'max_length': '32'}),
196
            'subnet': ('django.db.models.fields.CharField', [], {'default': "'10.0.0.0/24'", 'max_length': '32'}),
197
            'subnet6': ('django.db.models.fields.CharField', [], {'max_length': '64', 'null': 'True'}),
198
            'type': ('django.db.models.fields.CharField', [], {'default': "'PRIVATE_PHYSICAL_VLAN'", 'max_length': '50'}),
199
            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
200
            'userid': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True'})
201
        },
202
        'db.networkinterface': {
203
            'Meta': {'object_name': 'NetworkInterface'},
204
            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
205
            'dirty': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}),
206
            'firewall_profile': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
207
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
208
            'index': ('django.db.models.fields.IntegerField', [], {}),
209
            'ipv4': ('django.db.models.fields.CharField', [], {'max_length': '15', 'null': 'True'}),
210
            'ipv6': ('django.db.models.fields.CharField', [], {'max_length': '100', 'null': 'True'}),
211
            'mac': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '32'}),
212
            'machine': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'nics'", 'to': "orm['db.VirtualMachine']"}),
213
            'network': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'nics'", 'to': "orm['db.Network']"}),
214
            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'})
215
        },
216
        'db.virtualmachine': {
217
            'Meta': {'object_name': 'VirtualMachine'},
218
            'action': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
219
            'backend': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'virtual_machines'", 'null': 'True', 'to': "orm['db.Backend']"}),
220
            'backend_hash': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True'}),
221
            'backendjobid': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True'}),
222
            'backendjobstatus': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
223
            'backendlogmsg': ('django.db.models.fields.TextField', [], {'null': 'True'}),
224
            'backendopcode': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
225
            'backendtime': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(1, 1, 1, 0, 0)'}),
226
            'buildpercentage': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
227
            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
228
            'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}),
229
            'flavor': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['db.Flavor']"}),
230
            'hostid': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
231
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
232
            'imageid': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
233
            'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
234
            'operstate': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
235
            'suspended': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}),
236
            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
237
            'userid': ('django.db.models.fields.CharField', [], {'max_length': '100'})
238
        },
239
        'db.virtualmachinemetadata': {
240
            'Meta': {'unique_together': "(('meta_key', 'vm'),)", 'object_name': 'VirtualMachineMetadata'},
241
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
242
            'meta_key': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
243
            'meta_value': ('django.db.models.fields.CharField', [], {'max_length': '500'}),
244
            'vm': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'metadata'", 'to': "orm['db.VirtualMachine']"})
245
        }
246
    }
247
    
248
    complete_apps = ['db']