2020-09-10 17:43:41 -04:00
|
|
|
import time
|
|
|
|
|
2017-09-21 16:32:05 -04:00
|
|
|
from django.core.management.base import BaseCommand
|
2017-09-19 17:47:42 -04:00
|
|
|
from django.utils import timezone
|
|
|
|
|
2023-03-27 11:43:12 -04:00
|
|
|
from core.choices import JobStatusChoices
|
|
|
|
from core.models import Job
|
|
|
|
from extras.models import ReportModule
|
2023-03-24 21:00:36 -04:00
|
|
|
from extras.reports import run_report
|
2017-09-19 17:47:42 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = "Run a report to validate data in NetBox"
|
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
parser.add_argument('reports', nargs='+', help="Report(s) to run")
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
|
2023-03-24 21:00:36 -04:00
|
|
|
for module in ReportModule.objects.all():
|
|
|
|
for report in module.reports.values():
|
|
|
|
if module.name in options['reports'] or report.full_name in options['reports']:
|
2017-09-21 16:32:05 -04:00
|
|
|
|
2023-03-27 11:43:12 -04:00
|
|
|
# Run the report and create a new Job
|
2017-09-21 16:32:05 -04:00
|
|
|
self.stdout.write(
|
2017-09-28 12:50:52 -04:00
|
|
|
"[{:%H:%M:%S}] Running {}...".format(timezone.now(), report.full_name)
|
2017-09-21 16:32:05 -04:00
|
|
|
)
|
2020-09-10 17:43:41 -04:00
|
|
|
|
2023-03-28 15:47:09 -04:00
|
|
|
job = Job.enqueue(
|
2020-09-10 17:43:41 -04:00
|
|
|
run_report,
|
2023-03-28 15:47:09 -04:00
|
|
|
instance=module,
|
|
|
|
name=report.class_name,
|
2022-04-04 18:00:38 +02:00
|
|
|
job_timeout=report.job_timeout
|
2020-09-10 17:43:41 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
# Wait on the job to finish
|
2023-03-27 11:43:12 -04:00
|
|
|
while job.status not in JobStatusChoices.TERMINAL_STATE_CHOICES:
|
2020-09-10 17:43:41 -04:00
|
|
|
time.sleep(1)
|
2023-03-27 11:43:12 -04:00
|
|
|
job = Job.objects.get(pk=job.pk)
|
2017-09-22 12:11:10 -04:00
|
|
|
|
2017-09-21 16:32:05 -04:00
|
|
|
# Report on success/failure
|
2023-03-27 11:43:12 -04:00
|
|
|
if job.status == JobStatusChoices.STATUS_FAILED:
|
2020-09-10 17:43:41 -04:00
|
|
|
status = self.style.ERROR('FAILED')
|
2023-03-27 11:43:12 -04:00
|
|
|
elif job == JobStatusChoices.STATUS_ERRORED:
|
2020-09-10 17:43:41 -04:00
|
|
|
status = self.style.ERROR('ERRORED')
|
|
|
|
else:
|
|
|
|
status = self.style.SUCCESS('SUCCESS')
|
|
|
|
|
2023-03-27 11:43:12 -04:00
|
|
|
for test_name, attrs in job.data.items():
|
2017-09-21 16:32:05 -04:00
|
|
|
self.stdout.write(
|
2017-09-28 14:57:54 -04:00
|
|
|
"\t{}: {} success, {} info, {} warning, {} failure".format(
|
|
|
|
test_name, attrs['success'], attrs['info'], attrs['warning'], attrs['failure']
|
2017-09-21 16:32:05 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
self.stdout.write(
|
2017-09-28 12:50:52 -04:00
|
|
|
"[{:%H:%M:%S}] {}: {}".format(timezone.now(), report.full_name, status)
|
2017-09-21 16:32:05 -04:00
|
|
|
)
|
2020-09-10 17:43:41 -04:00
|
|
|
self.stdout.write(
|
2023-03-27 11:43:12 -04:00
|
|
|
"[{:%H:%M:%S}] {}: Duration {}".format(timezone.now(), report.full_name, job.duration)
|
2020-09-10 17:43:41 -04:00
|
|
|
)
|
2017-09-21 16:32:05 -04:00
|
|
|
|
|
|
|
# Wrap things up
|
|
|
|
self.stdout.write(
|
|
|
|
"[{:%H:%M:%S}] Finished".format(timezone.now())
|
|
|
|
)
|