mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-16 04:02:52 -06:00
130 lines
3.6 KiB
Python
130 lines
3.6 KiB
Python
# Generated by Django 5.0.9 on 2025-02-20 16:49
|
|
|
|
import sys
|
|
import time
|
|
|
|
from django.db import migrations, models
|
|
|
|
from ipam.choices import PrefixStatusChoices
|
|
|
|
|
|
def draw_progress(count, total, length=20):
|
|
if total == 0:
|
|
return
|
|
progress = count / total
|
|
percent = int(progress * 100)
|
|
bar = int(progress * length)
|
|
sys.stdout.write('\r')
|
|
sys.stdout.write(f"[{'=' * bar:{length}s}] {percent}%")
|
|
sys.stdout.flush()
|
|
|
|
|
|
def set_prefix(apps, schema_editor, model, attr='address', parent_model='Prefix'):
|
|
start = time.time()
|
|
ChildModel = apps.get_model('ipam', model)
|
|
ParentModel = apps.get_model('ipam', parent_model)
|
|
|
|
addresses = ChildModel.objects.all()
|
|
total = addresses.count()
|
|
if total == 0:
|
|
return
|
|
|
|
print('\r\n')
|
|
i = 0
|
|
draw_progress(i, total, 50)
|
|
for address in addresses:
|
|
i += 1
|
|
address_attr = getattr(address, attr)
|
|
prefixes = ParentModel.objects.filter(
|
|
prefix__net_contains_or_equals=str(address_attr.ip),
|
|
prefix__net_mask_length__lte=address_attr.prefixlen,
|
|
)
|
|
if hasattr(ParentModel, 'vrf'):
|
|
prefixes = prefixes.filter(vrf=address.vrf)
|
|
address.prefix = prefixes.last()
|
|
address.save()
|
|
draw_progress(i, total, 50)
|
|
|
|
end = time.time()
|
|
print(f"\r\nElapsed Time: {end - start:.2f}s")
|
|
|
|
|
|
def set_ipaddress_prefix(apps, schema_editor):
|
|
set_prefix(apps, schema_editor, 'IPAddress')
|
|
|
|
|
|
def unset_ipaddress_prefix(apps, schema_editor):
|
|
IPAddress = apps.get_model('ipam', 'IPAddress')
|
|
IPAddress.objects.update(prefix=None)
|
|
|
|
|
|
def set_iprange_prefix(apps, schema_editor):
|
|
set_prefix(apps, schema_editor, 'IPRange', 'start_address')
|
|
|
|
|
|
def unset_iprange_prefix(apps, schema_editor):
|
|
IPRange = apps.get_model('ipam', 'IPRange')
|
|
IPRange.objects.update(prefix=None)
|
|
|
|
|
|
def set_prefix_aggregate(apps, schema_editor):
|
|
set_prefix(apps, schema_editor, 'Prefix', 'prefix', 'Aggregate')
|
|
|
|
|
|
def unset_prefix_aggregate(apps, schema_editor):
|
|
Prefix = apps.get_model('ipam', 'Prefix')
|
|
Prefix.objects.update(aggregate=None)
|
|
|
|
|
|
def set_prefix_parent(apps, schema_editor):
|
|
Prefix = apps.get_model('ipam', 'Prefix')
|
|
start = time.time()
|
|
addresses = Prefix.objects.all()
|
|
i = 0
|
|
total = addresses.count()
|
|
if total == 0:
|
|
return
|
|
|
|
print('\r\n')
|
|
draw_progress(i, total, 50)
|
|
for address in addresses:
|
|
i += 1
|
|
prefixes = Prefix.objects.exclude(pk=address.pk).filter(
|
|
models.Q(
|
|
vrf=address.vrf,
|
|
prefix__net_contains=str(address.prefix.ip)
|
|
) | models.Q(
|
|
vrf=None,
|
|
status=PrefixStatusChoices.STATUS_CONTAINER,
|
|
prefix__net_contains=str(address.prefix.ip),
|
|
)
|
|
)
|
|
if not prefixes.exists():
|
|
draw_progress(i, total, 50)
|
|
continue
|
|
|
|
address.parent = prefixes.last()
|
|
address.save()
|
|
draw_progress(i, total, 50)
|
|
end = time.time()
|
|
print(f"\r\nElapsed Time: {end - start:.2f}s")
|
|
|
|
|
|
def unset_prefix_parent(apps, schema_editor):
|
|
Prefix = apps.get_model('ipam', 'Prefix')
|
|
Prefix.objects.update(parent=None)
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
|
('ipam', '0082_ipaddress_iprange_prefix_parent'),
|
|
]
|
|
|
|
operations = [
|
|
migrations.RunPython(set_ipaddress_prefix, unset_ipaddress_prefix),
|
|
migrations.RunPython(set_iprange_prefix, unset_iprange_prefix),
|
|
migrations.RunPython(set_prefix_aggregate, unset_prefix_aggregate),
|
|
migrations.RunPython(set_prefix_parent, unset_prefix_parent),
|
|
]
|