Skip to content

Commit

Permalink
Merge pull request #83 from DanSheps/develop-3.6
Browse files Browse the repository at this point in the history
Update for NB3.7
  • Loading branch information
DanSheps authored Jan 2, 2024
2 parents 887b256 + baac778 commit 4e8f51c
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 15 deletions.
2 changes: 1 addition & 1 deletion netbox_config_backup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class NetboxConfigBackup(PluginConfig):
author_email = metadata.get('Author-email')
base_url = 'configbackup'
min_version = '3.5.8'
max_version = '3.6.99'
max_version = '3.7.99'
required_settings = [
'repository',
'committer',
Expand Down
3 changes: 3 additions & 0 deletions netbox_config_backup/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ def commit(self, message):
try:
commit = porcelain.commit(self.repository, message, committer=committer, author=author)
return commit.decode('ascii')
except repo.InvalidUserIdentity:
committer = 'Your NetBox is misconfigured <netbox@localhost>'.encode('ascii')
author = 'Your NetBox is misconfigured <netbox@localhost>'.encode('ascii')
except FileExistsError:
sleep(1)
failures = failures + 1
Expand Down
40 changes: 40 additions & 0 deletions netbox_config_backup/management/commands/listbackups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import uuid

from django.core.management.base import BaseCommand
from django.db import transaction
from django.utils import timezone

from netbox_config_backup.models import BackupJob
from netbox_config_backup.tasks import backup_job
from netbox_config_backup.utils import remove_queued
from netbox_config_backup.utils.rq import can_backup


class Command(BaseCommand):

def handle(self, *args, **options):
from netbox_config_backup.models import Backup
print(f'Backup Name\t\tDevice Name\t\tIP')
for backup in Backup.objects.filter(device__isnull=False):
if backup.ip:
ip = backup.ip
else:
ip = backup.device.primary_ip

name = f'{backup.name}'
if len(backup.name) > 15:
name = f'{name}\t'
elif len(backup.name) > 7:
name = f'{name}\t\t'
else:
name = f'{name}\t\t\t'

device_name = f'{backup.device.name}'
if len(backup.device.name) > 15:
device_name = f'{device_name}\t'
elif len(backup.device.name) > 7:
device_name = f'{device_name}\t\t'
else:
device_name = f'{device_name}\t\t\t'

print(f'{name}{device_name}{ip}')
8 changes: 2 additions & 6 deletions netbox_config_backup/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,8 @@ class BackupTable(BaseTable):
'args': [Accessor('device_id')],
}
)
last_backup = tables.DateTimeColumn(
orderable=False
)
next_attempt = tables.DateTimeColumn(
orderable=False
)
last_backup = tables.DateTimeColumn()
next_attempt = tables.DateTimeColumn()

class Meta(BaseTable.Meta):
model = Backup
Expand Down
4 changes: 2 additions & 2 deletions netbox_config_backup/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ def backup_job(pk):
BackupJob.enqueue_if_needed(backup, delay=delay, job_id=job_result.job_id)
logger.warning(f'Netmiko read timeout on job: {backup}')
except ServiceUnavailable as e:
logger.info(f'Napalm service read failure on job: {backup}')
logger.info(f'Napalm service read failure on job: {backup} ({e})')
BackupJob.enqueue_if_needed(backup, delay=delay, job_id=job_result.job_id)
except Exception as e:
logger.error(f'Exception at line 148 on job: {backup}')
logger.error(f'Uncaught Exception on job: {backup}')
logger.error(e)
job_result.set_status(JobResultStatusChoices.STATUS_ERRORED)
BackupJob.enqueue_if_needed(backup, delay=delay, job_id=job_result.job_id)
Expand Down
28 changes: 24 additions & 4 deletions netbox_config_backup/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging

from django.db import models
from django.http import Http404
from django.shortcuts import get_object_or_404, render
from django.urls import reverse, NoReverseMatch
Expand All @@ -20,15 +21,36 @@


class BackupListView(ObjectListView):
queryset = Backup.objects.filter(device__isnull=False)
queryset = Backup.objects.filter(device__isnull=False).prefetch_related('jobs').annotate(
last_backup=models.Subquery(
BackupJob.objects.filter(backup=models.OuterRef('id'), status=JobResultStatusChoices.STATUS_COMPLETED).order_by('completed').values('completed')[:1]
),
next_attempt=models.Subquery(
BackupJob.objects.filter(backup=models.OuterRef('id'), status__in=['pending', 'running']).order_by('scheduled').values('scheduled')[:1]
),
last_change=models.Subquery(
BackupCommitTreeChange.objects.filter(backup=models.OuterRef('id')).values('commit__time')[:1]
)
)

filterset = BackupFilterSet
filterset_form = BackupFilterSetForm
table = BackupTable
action_buttons = ('add', )


class UnassignedBackupListView(ObjectListView):
queryset = Backup.objects.filter(device__isnull=True)
queryset = Backup.objects.filter(device__isnull=True).annotate(
last_backup=models.Subquery(
BackupJob.objects.filter(backup=models.OuterRef('id'), status=JobResultStatusChoices.STATUS_COMPLETED).order_by('completed').values('completed')[:1]
),
next_attempt=models.Subquery(
BackupJob.objects.filter(backup=models.OuterRef('id'), status__in=['pending', 'running']).order_by('scheduled').values('scheduled')[:1]
),
last_change=models.Subquery(
BackupCommitTreeChange.objects.filter(backup=models.OuterRef('id')).values('commit__time')[:1]
)
)
filterset = BackupFilterSet
filterset_form = BackupFilterSetForm
table = BackupTable
Expand Down Expand Up @@ -196,8 +218,6 @@ def post(self, request, backup, *args, **kwargs):
pk_list = [int(pk) for pk in request.POST.getlist('pk')]

backups = pk_list[:2]
print(pk_list)
print(backups)

if len(backups) == 2:
current = int(backups[0])
Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='netbox_config_backup',
version='1.5.0',
version='1.5.1',
description='NetBox Configuration Backup',
long_description='Plugin to backup device configuration',
url='https://github.com/dansheps/netbox-config-backup/',
Expand All @@ -12,6 +12,7 @@
maintainer='Daniel Sheppard',
maintainer_email='[email protected]',
install_requires=[
'netmiko>=4.0.0'
'napalm',
'uuid',
'dulwich',
Expand All @@ -28,4 +29,4 @@
'Framework :: Django',
'Programming Language :: Python :: 3',
]
)
)

0 comments on commit 4e8f51c

Please sign in to comment.