Commit c64ee93e authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

Fix migration files

Fix south migration files. In data migrations, access the Models from
the orm, instead importing Models from synnefo.db. This is necessary as
the orm provides access to the version of the models that existed when
the migration file was created.
parent b5f389b7
......@@ -3,7 +3,31 @@ import datetime
from south.db import db
from south.v2 import DataMigration
from django.db import models
from synnefo.db.models import BridgePool
from synnefo import settings
def get_available_bridge(orm):
try:
entry = orm.BridgePool.objects.filter(available=True)[0]
entry.available = False
entry.save()
return entry
except IndexError:
try:
last = orm.BridgePool.objects.order_by('-index')[0]
index = last.index + 1
except IndexError:
index = 1
if index <= settings.PRIVATE_PHYSICAL_VLAN_MAX_NUMBER:
create_bridge = orm.BridgePool.objects.create
return create_bridge(index=index,
value=value_from_index(index),
available=False)
raise Exception('Pool of bridges exhausted. Can not'
' allocate bridge')
def value_from_index(index):
return settings.PRIVATE_PHYSICAL_VLAN_BRIDGE_PREFIX + str(index)
class Migration(DataMigration):
......@@ -26,7 +50,8 @@ class Migration(DataMigration):
else:
network.dhcp = False
network.type = 'PRIVATE_PHYSICAL_VLAN'
bridge = BridgePool.get_available()
entry = get_available_bridge(orm)
bridge = entry
network.netlink = bridge.value
network.save()
......
......@@ -3,17 +3,26 @@ import datetime
from south.db import db
from south.v2 import DataMigration
from django.db import models
from synnefo.db.models import Backend
try:
from synnefo.db.aes_encrypt import encrypt_db_charfield as encrypt
from synnefo.db.aes_encrypt import decrypt_db_charfield as decrypt
except ImportError:
encrypt = None
decrypt = None
class Migration(DataMigration):
def forwards(self, orm):
"Write your forwards methods here."
for backend in Backend.objects.all():
# Dummy update password in order to be encrypted
password = backend.password
backend.password = password
backend.save()
if encrypt and decrypt:
for backend in orm.Backend.objects.all():
# Dummy update password in order to be encrypted
password = decrypt(backend.password_hash)
backend.password_hash = encrypt(password)
backend.save()
else:
pass
def backwards(self, orm):
......
......@@ -18,7 +18,7 @@ class Migration(SchemaMigration):
db.add_column('db_backend', 'index', self.gf('django.db.models.fields.PositiveIntegerField')(default=0, unique=True), keep_default=False)
# Changing field 'Network.mac_prefix'
db.alter_column('db_network', 'mac_prefix', self.gf('django.db.models.fields.CharField')(max_length=32))
db.alter_column('db_network', 'mac_prefix',self.gf('django.db.models.fields.CharField')(default='', max_length=32, null=True))
# Adding field 'BackendNetwork.mac_prefix'
db.add_column('db_backendnetwork', 'mac_prefix', self.gf('django.db.models.fields.CharField')(default='', max_length=32), keep_default=False)
......@@ -111,7 +111,8 @@ class Migration(SchemaMigration):
'gateway6': ('django.db.models.fields.CharField', [], {'max_length': '64', 'null': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'link': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True'}),
'mac_prefix': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '32'}),
'mac_prefix': ('django.db.models.fields.CharField', [], {'default':
"''", 'max_length': '32', 'null':'True'}),
'machines': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['db.VirtualMachine']", 'through': "orm['db.NetworkInterface']", 'symmetrical': 'False'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
'public': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}),
......
......@@ -3,7 +3,37 @@ import datetime
from south.db import db
from south.v2 import DataMigration
from django.db import models
from synnefo.db.models import MacPrefixPool
from synnefo import settings
def get_available_mac_prefix(orm):
try:
entry = orm.MacPrefixPool.objects.filter(available=True)[0]
entry.available = False
entry.save()
return entry
except IndexError:
try:
last = orm.MacPrefixPool.objects.order_by('-index')[0]
index = last.index + 1
except IndexError:
index = 1
if index <= settings.MAC_POOL_LIMIT:
create_mac_prefix = orm.MacPrefixPool.objects.create
return create_mac_prefix(index=index,
value=value_from_index(index),
available=False)
raise Exception('Pool of mac_prefixes exhausted. Can not'
' allocate mac_prefix.')
def value_from_index(index):
"""Convert number to mac prefix
"""
base = settings.MAC_POOL_BASE
a = hex(int(base.replace(":", ""), 16) + index).replace("0x", '')
mac_prefix = ":".join([a[x:x + 2] for x in xrange(0, len(a), 2)])
return mac_prefix
class Migration(DataMigration):
......@@ -15,10 +45,10 @@ class Migration(DataMigration):
for network in orm.Network.objects.order_by('id'):
if network.deleted:
network.mac_prefix = 'aa:00:0'
network.mac_prefix = settings.MAC_POOL_BASE
else:
mac_prefix = MacPrefixPool.get_available().value
network.mac_prefix = mac_prefix
mac_prefix = get_available_mac_prefix(orm)
network.mac_prefix = mac_prefix.value
network.save()
for backend_network in orm.BackendNetwork.objects.order_by('id'):
......
# encoding: utf-8
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
# Changing field 'Network.mac_prefix'
db.alter_column('db_network', 'mac_prefix', self.gf('django.db.models.fields.CharField')(max_length=32, null=False))
def backwards(self, orm):
# Changing field 'Network.mac_prefix'
db.alter_column('db_network', 'mac_prefix', self.gf('django.db.models.fields.CharField')(default='', max_length=32, null=True))
models = {
'db.backend': {
'Meta': {'object_name': 'Backend'},
'clustername': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '128'}),
'ctotal': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
'dfree': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
'drained': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}),
'dtotal': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
'hash': ('django.db.models.fields.CharField', [], {'max_length': '40'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'index': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0', 'unique': 'True'}),
'mfree': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
'mtotal': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
'offline': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}),
'password_hash': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True', 'blank': 'True'}),
'pinst_cnt': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
'port': ('django.db.models.fields.PositiveIntegerField', [], {'default': '5080'}),
'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'username': ('django.db.models.fields.CharField', [], {'max_length': '64', 'null': 'True', 'blank': 'True'})
},
'db.backendnetwork': {
'Meta': {'object_name': 'BackendNetwork'},
'backend': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'networks'", 'to': "orm['db.Backend']"}),
'backendjobid': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True'}),
'backendjobstatus': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
'backendlogmsg': ('django.db.models.fields.TextField', [], {'null': 'True'}),
'backendopcode': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
'backendtime': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(1, 1, 1, 0, 0)'}),
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'mac_prefix': ('django.db.models.fields.CharField', [], {'max_length': '32'}),
'network': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'backend_networks'", 'to': "orm['db.Network']"}),
'operstate': ('django.db.models.fields.CharField', [], {'default': "'PENDING'", 'max_length': '30'}),
'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'})
},
'db.bridgepool': {
'Meta': {'object_name': 'BridgePool'},
'available': ('django.db.models.fields.BooleanField', [], {'default': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'index': ('django.db.models.fields.IntegerField', [], {'unique': 'True'}),
'value': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '128'})
},
'db.flavor': {
'Meta': {'unique_together': "(('cpu', 'ram', 'disk', 'disk_template'),)", 'object_name': 'Flavor'},
'cpu': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}),
'disk': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
'disk_template': ('django.db.models.fields.CharField', [], {'default': "'drbd'", 'max_length': '32'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'ram': ('django.db.models.fields.IntegerField', [], {'default': '0'})
},
'db.macprefixpool': {
'Meta': {'object_name': 'MacPrefixPool'},
'available': ('django.db.models.fields.BooleanField', [], {'default': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'index': ('django.db.models.fields.IntegerField', [], {'unique': 'True'}),
'value': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '128'})
},
'db.network': {
'Meta': {'object_name': 'Network'},
'action': ('django.db.models.fields.CharField', [], {'default': 'None', 'max_length': '32', 'null': 'True'}),
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}),
'dhcp': ('django.db.models.fields.BooleanField', [], {'default': 'True', 'blank': 'True'}),
'gateway': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True'}),
'gateway6': ('django.db.models.fields.CharField', [], {'max_length': '64', 'null': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'link': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True'}),
'mac_prefix': ('django.db.models.fields.CharField', [], {'max_length': '32'}),
'machines': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['db.VirtualMachine']", 'through': "orm['db.NetworkInterface']", 'symmetrical': 'False'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
'public': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}),
'reservations': ('django.db.models.fields.TextField', [], {'default': "''"}),
'state': ('django.db.models.fields.CharField', [], {'default': "'PENDING'", 'max_length': '32'}),
'subnet': ('django.db.models.fields.CharField', [], {'default': "'10.0.0.0/24'", 'max_length': '32'}),
'subnet6': ('django.db.models.fields.CharField', [], {'max_length': '64', 'null': 'True'}),
'type': ('django.db.models.fields.CharField', [], {'default': "'PRIVATE_PHYSICAL_VLAN'", 'max_length': '50'}),
'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
'userid': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True'})
},
'db.networkinterface': {
'Meta': {'object_name': 'NetworkInterface'},
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'dirty': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}),
'firewall_profile': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'index': ('django.db.models.fields.IntegerField', [], {}),
'ipv4': ('django.db.models.fields.CharField', [], {'max_length': '15', 'null': 'True'}),
'ipv6': ('django.db.models.fields.CharField', [], {'max_length': '100', 'null': 'True'}),
'mac': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '32'}),
'machine': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'nics'", 'to': "orm['db.VirtualMachine']"}),
'network': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'nics'", 'to': "orm['db.Network']"}),
'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'})
},
'db.virtualmachine': {
'Meta': {'object_name': 'VirtualMachine'},
'action': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
'backend': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'virtual_machines'", 'null': 'True', 'to': "orm['db.Backend']"}),
'backend_hash': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True'}),
'backendjobid': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True'}),
'backendjobstatus': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
'backendlogmsg': ('django.db.models.fields.TextField', [], {'null': 'True'}),
'backendopcode': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
'backendtime': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(1, 1, 1, 0, 0)'}),
'buildpercentage': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}),
'flavor': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['db.Flavor']"}),
'hostid': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'imageid': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
'operstate': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
'suspended': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}),
'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
'userid': ('django.db.models.fields.CharField', [], {'max_length': '100'})
},
'db.virtualmachinemetadata': {
'Meta': {'unique_together': "(('meta_key', 'vm'),)", 'object_name': 'VirtualMachineMetadata'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'meta_key': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
'meta_value': ('django.db.models.fields.CharField', [], {'max_length': '500'}),
'vm': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'metadata'", 'to': "orm['db.VirtualMachine']"})
}
}
complete_apps = ['db']
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment