2020-06-22 13:10:56 -04:00
|
|
|
import sys
|
|
|
|
|
|
|
|
from django.db import migrations
|
|
|
|
|
|
|
|
|
|
|
|
def replicate_interfaces(apps, schema_editor):
|
2020-07-13 13:13:37 -04:00
|
|
|
show_output = 'test' not in sys.argv
|
|
|
|
|
2020-06-22 13:10:56 -04:00
|
|
|
ContentType = apps.get_model('contenttypes', 'ContentType')
|
2020-06-23 12:50:22 -04:00
|
|
|
TaggedItem = apps.get_model('extras', 'TaggedItem')
|
2020-06-22 13:10:56 -04:00
|
|
|
Interface = apps.get_model('dcim', 'Interface')
|
|
|
|
IPAddress = apps.get_model('ipam', 'IPAddress')
|
2020-06-23 13:16:21 -04:00
|
|
|
VMInterface = apps.get_model('virtualization', 'VMInterface')
|
2020-06-22 13:10:56 -04:00
|
|
|
|
|
|
|
interface_ct = ContentType.objects.get_for_model(Interface)
|
2020-06-23 13:16:21 -04:00
|
|
|
vminterface_ct = ContentType.objects.get_for_model(VMInterface)
|
2020-06-22 13:10:56 -04:00
|
|
|
|
|
|
|
# Replicate dcim.Interface instances assigned to VirtualMachines
|
2020-08-10 13:53:28 -04:00
|
|
|
original_interfaces = Interface.objects.prefetch_related('tagged_vlans').filter(
|
|
|
|
virtual_machine__isnull=False
|
|
|
|
)
|
|
|
|
interfaces_count = len(original_interfaces)
|
2020-07-13 13:13:37 -04:00
|
|
|
if show_output:
|
2020-08-10 13:53:28 -04:00
|
|
|
print(f"\n Replicating {interfaces_count} VM interfaces...", flush=True)
|
|
|
|
new_interfaces = [
|
|
|
|
VMInterface(
|
2020-06-22 13:10:56 -04:00
|
|
|
virtual_machine=interface.virtual_machine,
|
|
|
|
name=interface.name,
|
|
|
|
enabled=interface.enabled,
|
|
|
|
mac_address=interface.mac_address,
|
|
|
|
mtu=interface.mtu,
|
|
|
|
mode=interface.mode,
|
|
|
|
description=interface.description,
|
|
|
|
untagged_vlan=interface.untagged_vlan,
|
2020-08-10 13:53:28 -04:00
|
|
|
) for interface in original_interfaces
|
|
|
|
]
|
|
|
|
VMInterface.objects.bulk_create(new_interfaces, batch_size=1000)
|
|
|
|
|
|
|
|
# Pre-fetch the PKs of interfaces with tags/IP addresses
|
|
|
|
interfaces_with_tags = TaggedItem.objects.filter(
|
|
|
|
content_type=interface_ct
|
|
|
|
).values_list('object_id', flat=True)
|
|
|
|
interfaces_with_ips = IPAddress.objects.filter(
|
|
|
|
assigned_object_id__isnull=False
|
|
|
|
).values_list('assigned_object_id', flat=True)
|
|
|
|
|
|
|
|
if show_output:
|
|
|
|
print(f" Replicating assigned objects...", flush=True)
|
|
|
|
for i, interface in enumerate(original_interfaces):
|
|
|
|
vminterface = new_interfaces[i]
|
2020-06-22 13:10:56 -04:00
|
|
|
|
|
|
|
# Copy tagged VLANs
|
2020-08-10 13:53:28 -04:00
|
|
|
if interface.tagged_vlans.exists():
|
|
|
|
vminterface.tagged_vlans.set(interface.tagged_vlans.all())
|
2020-06-22 13:10:56 -04:00
|
|
|
|
|
|
|
# Reassign tags to the new instance
|
2020-08-10 13:53:28 -04:00
|
|
|
if interface.pk in interfaces_with_tags:
|
|
|
|
TaggedItem.objects.filter(content_type=interface_ct, object_id=interface.pk).update(
|
|
|
|
content_type=vminterface_ct,
|
|
|
|
object_id=vminterface.pk
|
|
|
|
)
|
2020-06-22 13:10:56 -04:00
|
|
|
|
|
|
|
# Update any assigned IPAddresses
|
2020-08-10 13:53:28 -04:00
|
|
|
if interface.pk in interfaces_with_ips:
|
|
|
|
IPAddress.objects.filter(assigned_object_type=interface_ct, assigned_object_id=interface.pk).update(
|
|
|
|
assigned_object_type=vminterface_ct,
|
|
|
|
assigned_object_id=vminterface.pk
|
|
|
|
)
|
2020-06-22 13:10:56 -04:00
|
|
|
|
2020-07-13 13:13:37 -04:00
|
|
|
# Progress counter
|
2020-08-10 13:53:28 -04:00
|
|
|
if show_output and not i % 1000:
|
|
|
|
percentage = int(i / interfaces_count * 100)
|
|
|
|
print(f" {i}/{interfaces_count} ({percentage}%)", flush=True)
|
2020-06-22 13:10:56 -04:00
|
|
|
|
|
|
|
# Verify that all interfaces have been replicated
|
2020-07-13 13:13:37 -04:00
|
|
|
replicated_count = VMInterface.objects.count()
|
2020-06-22 13:10:56 -04:00
|
|
|
assert replicated_count == original_interfaces.count(), "Replicated interfaces count does not match original count!"
|
|
|
|
|
2020-07-02 13:12:37 -04:00
|
|
|
# Delete all interfaces not assigned to a Device
|
|
|
|
Interface.objects.filter(device__isnull=True).delete()
|
2020-06-22 13:10:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
|
|
|
|
dependencies = [
|
|
|
|
('ipam', '0037_ipaddress_assignment'),
|
2020-06-23 13:16:21 -04:00
|
|
|
('virtualization', '0015_vminterface'),
|
2020-06-22 13:10:56 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
|
|
|
migrations.RunPython(
|
|
|
|
code=replicate_interfaces
|
|
|
|
),
|
|
|
|
]
|