Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / db / migrations / 0077_migrate_networks_to_subnets.py @ e9b5ecff

History | View | Annotate | Download (20 kB)

1
# -*- coding: utf-8 -*-
2
import datetime
3
from south.db import db
4
from south.v2 import DataMigration
5
from django.db import models
6
import ipaddr
7

    
8
class Migration(DataMigration):
9

    
10
    def forwards(self, orm):
11
        "Write your forwards methods here."
12
        # Note: Remember to use orm['appname.ModelName'] rather than "from appname.models..."
13
        for network in orm.Network.objects.select_related('pool').all():
14
            if network.subnet:
15
                subnet = orm.Subnet.objects.create(network=network,
16
                                                   ipversion=4,
17
                                                   cidr=network.subnet,
18
                                                   gateway=network.gateway,
19
                                                   dhcp=network.dhcp,
20
                                                   deleted=network.deleted)
21

    
22
                if not network.deleted:
23
                    ip_pool = network.pool
24
                    if ip_pool is None:
25
                        ip_pool = orm.IPPoolTable()
26
                    ip_pool.subnet = subnet
27
                    ip_pool.base = subnet.cidr
28
                    ip_pool.offset = 0
29
                    ip_pool.size = ipaddr.IPNetwork(network.subnet).numhosts
30
                    ip_pool.save()
31

    
32
            if network.subnet6:
33
                orm.Subnet.objects.create(network=network,
34
                                          ipversion=6,
35
                                          cidr=network.subnet6,
36
                                          gateway=network.gateway6,
37
                                          dhcp=network.dhcp,
38
                                          deleted=network.deleted)
39

    
40
    def backwards(self, orm):
41
        warning_msg_printed = False
42
        for subnet in orm.Subnet.objects.filter(ipversion=4):
43
            network = subnet.network
44
            network.dhcp = subnet.dhcp
45
            network.subnet = subnet.cidr
46
            network.gateway = subnet.gateway
47
            # Create an empty pool
48
            network.pool = orm.IPPoolTable.objects.create(available_map="",
49
                                                          reserved_map="",
50
                                                          size=0)
51
            network.save()
52
            try:
53
                ip_pool = network.get_pool()
54
                for nic in network.nics.filter(ipv4__isnull=False):
55
                    ip_pool.reserve(nic.ipv4)
56
                ip_pool.save()
57
            except:
58
                if not warning_msg_printed:
59
                    msg = ("WARNING: Cannot perform backwards migration for IP pools!"
60
                           " IP pools for all networks are not consistent."
61
                           " Make sure to run 'snf-manage reconcile-pools'.")
62
                    print msg
63
                    warning_msg_printed = True
64

    
65
            subnet.ip_pools.all().delete()
66

    
67
        for subnet in orm.Subnet.objects.filter(ipversion=6):
68
            network = subnet.network
69
            network.dhcp = subnet.dhcp
70
            network.subnet6 = subnet.cidr
71
            network.gateway6 = subnet.gateway
72
            network.save()
73

    
74
    models = {
75
        'db.backend': {
76
            'Meta': {'ordering': "['clustername']", 'object_name': 'Backend'},
77
            'clustername': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '128'}),
78
            'ctotal': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
79
            'dfree': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
80
            'disk_templates': ('synnefo.db.fields.SeparatedValuesField', [], {'null': 'True'}),
81
            'drained': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
82
            'dtotal': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
83
            'hash': ('django.db.models.fields.CharField', [], {'max_length': '40'}),
84
            'hypervisor': ('django.db.models.fields.CharField', [], {'default': "'kvm'", 'max_length': '32'}),
85
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
86
            'index': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0', 'unique': 'True'}),
87
            'mfree': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
88
            'mtotal': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
89
            'offline': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
90
            'password_hash': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True', 'blank': 'True'}),
91
            'pinst_cnt': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
92
            'port': ('django.db.models.fields.PositiveIntegerField', [], {'default': '5080'}),
93
            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
94
            'username': ('django.db.models.fields.CharField', [], {'max_length': '64', 'null': 'True', 'blank': 'True'})
95
        },
96
        'db.backendnetwork': {
97
            'Meta': {'unique_together': "(('network', 'backend'),)", 'object_name': 'BackendNetwork'},
98
            'backend': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'networks'", 'on_delete': 'models.PROTECT', 'to': "orm['db.Backend']"}),
99
            'backendjobid': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True'}),
100
            'backendjobstatus': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
101
            'backendlogmsg': ('django.db.models.fields.TextField', [], {'null': 'True'}),
102
            'backendopcode': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
103
            'backendtime': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(1, 1, 1, 0, 0)'}),
104
            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
105
            'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
106
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
107
            'mac_prefix': ('django.db.models.fields.CharField', [], {'max_length': '32'}),
108
            'network': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'backend_networks'", 'to': "orm['db.Network']"}),
109
            'operstate': ('django.db.models.fields.CharField', [], {'default': "'PENDING'", 'max_length': '30'}),
110
            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'})
111
        },
112
        'db.bridgepooltable': {
113
            'Meta': {'object_name': 'BridgePoolTable'},
114
            'available_map': ('django.db.models.fields.TextField', [], {'default': "''"}),
115
            'base': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True'}),
116
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
117
            'offset': ('django.db.models.fields.IntegerField', [], {'null': 'True'}),
118
            'reserved_map': ('django.db.models.fields.TextField', [], {'default': "''"}),
119
            'size': ('django.db.models.fields.IntegerField', [], {})
120
        },
121
        'db.flavor': {
122
            'Meta': {'unique_together': "(('cpu', 'ram', 'disk', 'disk_template'),)", 'object_name': 'Flavor'},
123
            'cpu': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
124
            'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
125
            'disk': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
126
            'disk_template': ('django.db.models.fields.CharField', [], {'max_length': '32'}),
127
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
128
            'ram': ('django.db.models.fields.IntegerField', [], {'default': '0'})
129
        },
130
        'db.floatingip': {
131
            'Meta': {'object_name': 'FloatingIP'},
132
            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
133
            'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
134
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
135
            'ipv4': ('django.db.models.fields.IPAddressField', [], {'unique': 'True', 'max_length': '15', 'db_index': 'True'}),
136
            'machine': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'floating_ips'", 'null': 'True', 'to': "orm['db.VirtualMachine']"}),
137
            'network': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'floating_ips'", 'to': "orm['db.Network']"}),
138
            'serial': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'floating_ips'", 'null': 'True', 'on_delete': 'models.SET_NULL', 'to': "orm['db.QuotaHolderSerial']"}),
139
            'userid': ('django.db.models.fields.CharField', [], {'max_length': '128', 'db_index': 'True'})
140
        },
141
        'db.ippooltable': {
142
            'Meta': {'object_name': 'IPPoolTable'},
143
            'available_map': ('django.db.models.fields.TextField', [], {'default': "''"}),
144
            'base': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True'}),
145
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
146
            'offset': ('django.db.models.fields.IntegerField', [], {'null': 'True'}),
147
            'reserved_map': ('django.db.models.fields.TextField', [], {'default': "''"}),
148
            'size': ('django.db.models.fields.IntegerField', [], {}),
149
            'subnet': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'ip_pools'", 'null': 'True', 'to': "orm['db.Subnet']"})
150
        },
151
        'db.macprefixpooltable': {
152
            'Meta': {'object_name': 'MacPrefixPoolTable'},
153
            'available_map': ('django.db.models.fields.TextField', [], {'default': "''"}),
154
            'base': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True'}),
155
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
156
            'offset': ('django.db.models.fields.IntegerField', [], {'null': 'True'}),
157
            'reserved_map': ('django.db.models.fields.TextField', [], {'default': "''"}),
158
            'size': ('django.db.models.fields.IntegerField', [], {})
159
        },
160
        'db.network': {
161
            'Meta': {'object_name': 'Network'},
162
            'action': ('django.db.models.fields.CharField', [], {'default': 'None', 'max_length': '32', 'null': 'True'}),
163
            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
164
            'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
165
            'dhcp': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
166
            'drained': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
167
            'flavor': ('django.db.models.fields.CharField', [], {'max_length': '32'}),
168
            'floating_ip_pool': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
169
            'gateway': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True'}),
170
            'gateway6': ('django.db.models.fields.CharField', [], {'max_length': '64', 'null': 'True'}),
171
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
172
            'link': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True'}),
173
            'mac_prefix': ('django.db.models.fields.CharField', [], {'max_length': '32'}),
174
            'machines': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['db.VirtualMachine']", 'through': "orm['db.NetworkInterface']", 'symmetrical': 'False'}),
175
            'mode': ('django.db.models.fields.CharField', [], {'max_length': '16', 'null': 'True'}),
176
            'name': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
177
            'pool': ('django.db.models.fields.related.OneToOneField', [], {'related_name': "'network'", 'unique': 'True', 'null': 'True', 'to': "orm['db.IPPoolTable']"}),
178
            'public': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
179
            'serial': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'network'", 'null': 'True', 'on_delete': 'models.SET_NULL', 'to': "orm['db.QuotaHolderSerial']"}),
180
            'state': ('django.db.models.fields.CharField', [], {'default': "'PENDING'", 'max_length': '32'}),
181
            'subnet': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True'}),
182
            'subnet6': ('django.db.models.fields.CharField', [], {'max_length': '64', 'null': 'True'}),
183
            'tags': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True'}),
184
            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
185
            'userid': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True', 'db_index': 'True'})
186
        },
187
        'db.networkinterface': {
188
            'Meta': {'unique_together': "(('network', 'ipv4'),)", 'object_name': 'NetworkInterface'},
189
            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
190
            'dirty': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
191
            'firewall_profile': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
192
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
193
            'index': ('django.db.models.fields.IntegerField', [], {'null': 'True'}),
194
            'ipv4': ('django.db.models.fields.CharField', [], {'max_length': '15', 'null': 'True'}),
195
            'ipv6': ('django.db.models.fields.CharField', [], {'max_length': '100', 'null': 'True'}),
196
            'mac': ('django.db.models.fields.CharField', [], {'max_length': '32', 'unique': 'True', 'null': 'True'}),
197
            'machine': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'nics'", 'to': "orm['db.VirtualMachine']"}),
198
            'network': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'nics'", 'to': "orm['db.Network']"}),
199
            'state': ('django.db.models.fields.CharField', [], {'default': "'ACTIVE'", 'max_length': '32'}),
200
            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'})
201
        },
202
        'db.quotaholderserial': {
203
            'Meta': {'ordering': "['serial']", 'object_name': 'QuotaHolderSerial'},
204
            'accept': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
205
            'pending': ('django.db.models.fields.BooleanField', [], {'default': 'True', 'db_index': 'True'}),
206
            'resolved': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
207
            'serial': ('django.db.models.fields.BigIntegerField', [], {'primary_key': 'True', 'db_index': 'True'})
208
        },
209
        'db.subnet': {
210
            'Meta': {'object_name': 'Subnet'},
211
            'cidr': ('django.db.models.fields.CharField', [], {'max_length': '64', 'null': 'True'}),
212
            'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
213
            'dhcp': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
214
            'dns_nameservers': ('synnefo.db.fields.SeparatedValuesField', [], {'null': 'True'}),
215
            'gateway': ('django.db.models.fields.CharField', [], {'max_length': '64', 'null': 'True'}),
216
            'host_routes': ('synnefo.db.fields.SeparatedValuesField', [], {'null': 'True'}),
217
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
218
            'ipversion': ('django.db.models.fields.IntegerField', [], {'default': '4'}),
219
            'name': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True'}),
220
            'network': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'subnets'", 'to': "orm['db.Network']"})
221
        },
222
        'db.virtualmachine': {
223
            'Meta': {'object_name': 'VirtualMachine'},
224
            'action': ('django.db.models.fields.CharField', [], {'default': 'None', 'max_length': '30', 'null': 'True'}),
225
            'backend': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'virtual_machines'", 'null': 'True', 'on_delete': 'models.PROTECT', 'to': "orm['db.Backend']"}),
226
            'backend_hash': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True'}),
227
            'backendjobid': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True'}),
228
            'backendjobstatus': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
229
            'backendlogmsg': ('django.db.models.fields.TextField', [], {'null': 'True'}),
230
            'backendopcode': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
231
            'backendtime': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(1, 1, 1, 0, 0)'}),
232
            'buildpercentage': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
233
            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
234
            'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
235
            'flavor': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['db.Flavor']", 'on_delete': 'models.PROTECT'}),
236
            'hostid': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
237
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
238
            'imageid': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
239
            'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
240
            'operstate': ('django.db.models.fields.CharField', [], {'default': "'BUILD'", 'max_length': '30'}),
241
            'serial': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'virtual_machine'", 'null': 'True', 'on_delete': 'models.SET_NULL', 'to': "orm['db.QuotaHolderSerial']"}),
242
            'suspended': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
243
            'task': ('django.db.models.fields.CharField', [], {'max_length': '64', 'null': 'True'}),
244
            'task_job_id': ('django.db.models.fields.BigIntegerField', [], {'null': 'True'}),
245
            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
246
            'userid': ('django.db.models.fields.CharField', [], {'max_length': '100', 'db_index': 'True'})
247
        },
248
        'db.virtualmachinediagnostic': {
249
            'Meta': {'ordering': "['-created']", 'object_name': 'VirtualMachineDiagnostic'},
250
            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
251
            'details': ('django.db.models.fields.TextField', [], {'null': 'True'}),
252
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
253
            'level': ('django.db.models.fields.CharField', [], {'max_length': '20'}),
254
            'machine': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'diagnostics'", 'to': "orm['db.VirtualMachine']"}),
255
            'message': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
256
            'source': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
257
            'source_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True'})
258
        },
259
        'db.virtualmachinemetadata': {
260
            'Meta': {'unique_together': "(('meta_key', 'vm'),)", 'object_name': 'VirtualMachineMetadata'},
261
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
262
            'meta_key': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
263
            'meta_value': ('django.db.models.fields.CharField', [], {'max_length': '500'}),
264
            'vm': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'metadata'", 'to': "orm['db.VirtualMachine']"})
265
        }
266
    }
267

    
268
    complete_apps = ['db']
269
    symmetrical = True