Skip to content

Commit

Permalink
More 3.6 compatibility fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DanSheps committed Sep 7, 2023
1 parent 38e3d5b commit dfb88f9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 22 deletions.
1 change: 1 addition & 0 deletions netbox_config_backup/management/commands/runbackup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def run_backup(self, backup):
def handle(self, *args, **options):
from netbox_config_backup.models import Backup
if options['device']:
print(f'Running:{options.get("device")}| ')
backup = Backup.objects.filter(device__name=options['device']).first()
if backup:
self.run_backup(backup)
Expand Down
10 changes: 9 additions & 1 deletion netbox_config_backup/models/backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import uuid as uuid

from django.db import models
from django.db.models import Q
from django.urls import reverse

from django_rq import get_queue
Expand Down Expand Up @@ -89,7 +90,14 @@ def enqueue_if_needed(self):
return enqueue_if_needed(self)

def requeue(self):
self.jobs.all().delete()
self.jobs.filter(
~Q(status=JobResultStatusChoices.STATUS_COMPLETED) &
~Q(status=JobResultStatusChoices.STATUS_FAILED) &
~Q(status=JobResultStatusChoices.STATUS_ERRORED)
).update(
status=JobResultStatusChoices.STATUS_FAILED
)
remove_queued(self)
self.enqueue_if_needed()

def get_config(self, index='HEAD'):
Expand Down
32 changes: 19 additions & 13 deletions netbox_config_backup/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from netbox.api.exceptions import ServiceUnavailable
from netbox.config import get_config
from netbox_config_backup.models import Backup, BackupJob, BackupCommit
from netbox_config_backup.utils.rq import can_backup


def get_logger():
# Setup logging to Stdout
Expand All @@ -32,8 +34,8 @@ def napalm_init(device, ip=None, extra_args={}):
timeout = settings.PLUGINS_CONFIG.get('netbox_napalm_plugin', {}).get('NAPALM_TIMEOUT', None)
optional_args = settings.PLUGINS_CONFIG.get('netbox_napalm_plugin', {}).get('NAPALM_ARGS', []).copy()

if device and device.platform and device.platform.napalm_args is not None:
optional_args.update(device.platform.napalm_args)
if device and device.platform and device.platform.napalm.napalm_args is not None:
optional_args.update(device.platform.napalm.napalm_args)
if extra_args != {}:
optional_args.update(extra_args)

Expand All @@ -58,10 +60,10 @@ def napalm_init(device, ip=None, extra_args={}):

# Validate the configured driver
try:
driver = napalm.get_network_driver(device.platform.napalm_driver)
driver = napalm.get_network_driver(device.platform.napalm.napalm_driver)
except ModuleImportError:
raise ServiceUnavailable("NAPALM driver for platform {} not found: {}.".format(
device.platform, device.platform.napalm_driver
device.platform, device.platform.napalm.napalm_driver
))

# Connect to the device
Expand Down Expand Up @@ -90,6 +92,9 @@ def backup_config(backup, pk=None):
ip = backup.ip if backup.ip is not None else backup.device.primary_ip
else:
ip = None
if not can_backup(backup):
raise Exception(f'Cannot backup {backup}')

if backup.device is not None and ip is not None:
logger.info(f'{backup}: Backup started')
#logger.debug(f'[{pk}] Connecting')
Expand Down Expand Up @@ -120,6 +125,10 @@ def backup_job(pk):
logger.error(f'Cannot locate job (Id: {pk}) in DB')
raise Exception(f'Cannot locate job (Id: {pk}) in DB')
backup = job_result.backup

if not can_backup(backup):
logger.warning(f'Cannot backup due to additional factors')
return 1
delay = timedelta(seconds=settings.PLUGINS_CONFIG.get('netbox_config_backup', {}).get('frequency'))

job_result.started = timezone.now()
Expand All @@ -136,6 +145,11 @@ def backup_job(pk):
# Enqueue next job if one doesn't exist
try:
#logger.debug(f'[{pk}] Starting Enqueue')
BackupJob.objects.filter(
backup=backup
).exclude(
status__in=JobResultStatusChoices.TERMINAL_STATE_CHOICES
).update(status=JobResultStatusChoices.STATUS_FAILED)
BackupJob.enqueue_if_needed(backup, delay=delay, job_id=job_result.job_id)
#logger.debug(f'[{pk}] Finished Enqueue')
except Exception as e:
Expand All @@ -150,19 +164,11 @@ def backup_job(pk):
except Exception as e:
logger.error(f'Exception at line 148 on job: {backup}')
logger.error(e)
job_result.set_status(JobResultStatusChoices.STATUS_FAILED)
job_result.set_status(JobResultStatusChoices.STATUS_ERRORED)
BackupJob.enqueue_if_needed(backup, delay=delay, job_id=job_result.job_id)

#logger.debug(f'[{pk}] Saving result')
job_result.save()

# Clear queue of old jobs
BackupJob.objects.filter(
backup=backup,
status__in=JobResultStatusChoices.TERMINAL_STATE_CHOICES
).exclude(
pk=job_result.pk
).delete()


logger = get_logger()
16 changes: 11 additions & 5 deletions netbox_config_backup/utils/rq.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@


def can_backup(backup):
logger.debug(f'Checking backup status for {backup}')
if backup.device is None:
logger.info(f'No device for {backup}')
return False
Expand All @@ -27,13 +28,18 @@ def can_backup(backup):
logger.info(f'Backup disabled for {backup} due to device status ({backup.device.status})')
return False
elif (backup.ip is None and backup.device.primary_ip is None) or backup.device.platform is None or \
backup.device.platform.napalm_driver == '' or backup.device.platform.napalm_driver is None:
hasattr(backup.device.platform, 'napalm') is False or backup.device.platform.napalm is None or \
backup.device.platform.napalm.napalm_driver == '' or backup.device.platform.napalm.napalm_driver is None:
if backup.ip is None and backup.device.primary_ip is None:
logger.error(f'Backup disabled for {backup} due to no primary IP ({backup.device.status})')
logger.warning(f'Backup disabled for {backup} due to no primary IP ({backup.device.status})')
elif backup.device.platform is None:
logger.error(f'Backup disabled for {backup} due to platform not set ({backup.device.status})')
elif backup.device.platform.napalm_driver == '' or backup.device.platform.napalm_driver is None:
logger.error(f'Backup disabled for {backup} due to napalm driver not set ({backup.device.status})')
logger.warning(f'Backup disabled for {backup} due to platform not set ({backup.device.status})')
elif hasattr(backup.device.platform, 'napalm') is False or backup.device.platform.napalm is None:
logger.warning(
f'Backup disabled for {backup} due to platform having no napalm config ({backup.device.status})'
)
elif backup.device.platform.napalm.napalm_driver == '' or backup.device.platform.napalm.napalm_driver is None:
logger.warning(f'Backup disabled for {backup} due to napalm driver not set ({backup.device.status})')
return False

return True
Expand Down
4 changes: 1 addition & 3 deletions netbox_config_backup/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,7 @@ def get(self, request, backup, current=None, previous=None):
previous_sha = previous.commit.sha if previous.commit is not None else 'HEAD'
current_sha = current.commit.sha if current.commit is not None else None

print(f'Diffs: {current_sha}:{previous_sha}')

if backup.device and backup.device.platform.napalm_driver in ['ios', 'nxos']:
if backup.device and backup.device.platform.napalm.napalm_driver in ['ios', 'nxos']:
new = repo.read(current.file.path, current_sha)
old = repo.read(previous.file.path, previous_sha)
differ = Differ(old, new)
Expand Down

0 comments on commit dfb88f9

Please sign in to comment.