mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-16 04:02:52 -06:00

Instead of maintaining two separate job execution logics, the same job is now used for both background and interactive execution.
46 lines
1.7 KiB
Python
46 lines
1.7 KiB
Python
from django.core.management.base import BaseCommand, CommandError
|
|
|
|
from core.choices import DataSourceStatusChoices
|
|
from core.models import DataSource
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = "Synchronize a data source from its remote upstream"
|
|
|
|
def add_arguments(self, parser):
|
|
parser.add_argument('name', nargs='*', help="Data source(s) to synchronize")
|
|
parser.add_argument(
|
|
"--all", action='store_true', dest='sync_all',
|
|
help="Synchronize all data sources"
|
|
)
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
# Find DataSources to sync
|
|
if options['sync_all']:
|
|
datasources = DataSource.objects.all()
|
|
elif options['name']:
|
|
datasources = DataSource.objects.filter(name__in=options['name'])
|
|
# Check for invalid names
|
|
found_names = {ds['name'] for ds in datasources.values('name')}
|
|
if invalid_names := set(options['name']) - found_names:
|
|
raise CommandError(f"Invalid data source names: {', '.join(invalid_names)}")
|
|
else:
|
|
raise CommandError(f"Must specify at least one data source, or set --all.")
|
|
|
|
if len(options['name']) > 1:
|
|
self.stdout.write(f"Syncing {len(datasources)} data sources.")
|
|
|
|
for i, datasource in enumerate(datasources, start=1):
|
|
self.stdout.write(f"[{i}] Syncing {datasource}... ", ending='')
|
|
self.stdout.flush()
|
|
|
|
datasource.enqueue_sync_job()
|
|
datasource.refresh_from_db()
|
|
|
|
self.stdout.write(datasource.get_status_display())
|
|
self.stdout.flush()
|
|
|
|
if len(options['name']) > 1:
|
|
self.stdout.write(f"Finished.")
|