From e53839ca2aa8f2fb3d9ccf603c3590a94359ed3e Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 13 Jul 2020 13:13:37 -0400 Subject: [PATCH] Add progress counter to VM interface replication migration --- .../migrations/0016_replicate_interfaces.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/netbox/virtualization/migrations/0016_replicate_interfaces.py b/netbox/virtualization/migrations/0016_replicate_interfaces.py index e141dff04..a8960b101 100644 --- a/netbox/virtualization/migrations/0016_replicate_interfaces.py +++ b/netbox/virtualization/migrations/0016_replicate_interfaces.py @@ -4,6 +4,8 @@ from django.db import migrations def replicate_interfaces(apps, schema_editor): + show_output = 'test' not in sys.argv + ContentType = apps.get_model('contenttypes', 'ContentType') TaggedItem = apps.get_model('extras', 'TaggedItem') Interface = apps.get_model('dcim', 'Interface') @@ -15,7 +17,9 @@ def replicate_interfaces(apps, schema_editor): # Replicate dcim.Interface instances assigned to VirtualMachines original_interfaces = Interface.objects.filter(virtual_machine__isnull=False) - for interface in original_interfaces: + if show_output: + print(f"\n Replicating {len(original_interfaces)} VM interfaces...", flush=True) + for i, interface in enumerate(original_interfaces, start=1): vminterface = VMInterface( virtual_machine=interface.virtual_machine, name=interface.name, @@ -44,12 +48,16 @@ def replicate_interfaces(apps, schema_editor): assigned_object_id=vminterface.pk ) - replicated_count = VMInterface.objects.count() - if 'test' not in sys.argv: - print(f"\n Replicated {replicated_count} interfaces ", end='', flush=True) + # Progress counter + if show_output and not i % 250: + percentage = int(i / len(original_interfaces) * 100) + print(f" {i}/{len(original_interfaces)} ({percentage}%)", flush=True) # Verify that all interfaces have been replicated + replicated_count = VMInterface.objects.count() assert replicated_count == original_interfaces.count(), "Replicated interfaces count does not match original count!" + if show_output: + print(f"\n Replicated {replicated_count} interfaces ", end='', flush=True) # Delete all interfaces not assigned to a Device Interface.objects.filter(device__isnull=True).delete()