Skip to content

Commit

Permalink
Merge pull request #94 from DanSheps/master
Browse files Browse the repository at this point in the history
Update develop
  • Loading branch information
DanSheps authored May 7, 2024
2 parents 04206ac + 8fc4d80 commit 1146a43
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 24 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/ci-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: CI
on: [push, pull_request]

permissions:
contents: read

jobs:
build:
runs-on: ubuntu-latest
env:
NETBOX_CONFIGURATION: netbox.configuration_testing
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11']
services:
redis:
image: redis
ports:
- 6379:6379
postgres:
image: postgres
env:
POSTGRES_USER: netbox
POSTGRES_PASSWORD: netbox
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432

steps:
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Check out NetBox
uses: actions/checkout@v4
with:
repository: netbox-community/netbox
ref: master
path: netbox

- name: Check out repo
uses: actions/checkout@v4
with:
path: netbox-config-backup

- name: Install dependencies & set up configuration
run: |
python -m pip install --upgrade pip
pip install -r netbox\requirements.txt
pip install pycodestyle coverage tblib
pip install -e netbox-config-backup
- name: Run tests
run: coverage run --source="netbox-config-backup/" netbox/manage.py test netbox-config-backup/ --parallel
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 = metadata.get('Author')
author_email = metadata.get('Author-email')
base_url = 'configbackup'
min_version = '4.0.0-dev'
min_version = '3.5.8'
max_version = '4.0.99'
required_settings = [
'repository',
Expand Down
6 changes: 2 additions & 4 deletions netbox_config_backup/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from dcim.models import Device
from ipam.models import IPAddress
from netbox_config_backup.models import Backup
from utilities.forms import BootstrapMixin
from utilities.forms.fields import DynamicModelChoiceField, DynamicModelMultipleChoiceField

__all__ = (
Expand All @@ -16,7 +15,7 @@
)


class BackupForm(BootstrapMixin, forms.ModelForm):
class BackupForm(forms.ModelForm):
device = DynamicModelChoiceField(
label='Device',
required=False,
Expand Down Expand Up @@ -54,7 +53,7 @@ def clean(self):
raise ValidationError({'device': f'{device}\'s platform ({device.platform}) has no napalm driver'})


class BackupFilterSetForm(BootstrapMixin, forms.Form):
class BackupFilterSetForm(forms.Form):
model = Backup
field_order = [
'q', 'name', 'device_id', 'ip'
Expand All @@ -68,7 +67,6 @@ class BackupFilterSetForm(BootstrapMixin, forms.Form):
queryset=Device.objects.all(),
required=False,
label=_('Device'),
fetch_trigger='open',
query_params={
'status': [DeviceStatusChoices.STATUS_ACTIVE],
'platform__napalm__ne': None,
Expand Down
34 changes: 34 additions & 0 deletions netbox_config_backup/management/commands/fork.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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 add_arguments(self, parser):
parser.add_argument('--time', dest='time', help="time")
parser.add_argument('--device', dest='device', help="Device Name")

def handle(self, *args, **options):
from multiprocessing import Process
import time
def test(i):
self.stdout.write(f"Child {i} is running")
self.stdout.write(f"Child {i} sleeping 10 seconds")
time.sleep(10)
self.stdout.write(f"Child {i} sleep complete")

processes = []
for i in range(1, 2):
p = Process(target=test, args=(i,))
p.start()
p.join(1)
self.stdout.write(f"Child {i} running")
processes.append(p)

4 changes: 0 additions & 4 deletions netbox_config_backup/models/backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ class Backup(NetBoxModel):
class Meta:
ordering = ['name']

@property
def backup_count(self):
return self.changes.count()

def get_absolute_url(self):
return reverse('plugins:netbox_config_backup:backup', args=[self.pk])

Expand Down
2 changes: 1 addition & 1 deletion netbox_config_backup/navigation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from netbox.choices import ButtonColorChoices
from netbox.plugins import PluginMenuItem, PluginMenuButton, PluginMenu
from utilities.choices import ButtonColorChoices

assigned = PluginMenuItem(
link='plugins:netbox_config_backup:backup_list',
Expand Down
21 changes: 11 additions & 10 deletions netbox_config_backup/querysets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models
from django.db.models import Count

from extras.choices import JobResultStatusChoices
from utilities.querysets import RestrictedQuerySet
Expand All @@ -9,14 +10,14 @@ class BackupQuerySet(RestrictedQuerySet):
def default_annotate(self):
from netbox_config_backup.models import BackupJob, BackupCommitTreeChange

return self.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]
return self.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')).order_by('-id').values('commit__time')[:1]
)
)
)
9 changes: 8 additions & 1 deletion netbox_config_backup/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,23 @@ class BackupTable(BaseTable):
)
last_backup = tables.DateTimeColumn()
next_attempt = tables.DateTimeColumn()
last_change = tables.DateTimeColumn()
backup_count = tables.Column(
accessor='changes'
)

class Meta(BaseTable.Meta):
model = Backup
fields = (
'pk', 'name', 'device', 'last_backup', 'next_attempt', 'backup_count'
'pk', 'name', 'device', 'last_backup', 'next_attempt', 'last_change', 'backup_count'
)
default_columns = (
'pk', 'name', 'device', 'last_backup', 'next_attempt', 'backup_count'
)

def render_backup_count(self, value):
return f'{value.count()}'


class BackupsTable(NetBoxTable):
date = tables.Column(
Expand Down
4 changes: 2 additions & 2 deletions netbox_config_backup/template_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from netbox_config_backup.models import Backup, BackupJob, BackupCommitTreeChange
from netbox_config_backup.tables import BackupsTable
from netbox_config_backup.utils.backups import get_backup_tables
from utilities.htmx import is_htmx
from utilities.htmx import htmx_partial

logger = logging.getLogger(f"netbox_config_backup")

Expand Down Expand Up @@ -35,7 +35,7 @@ def build_table(instance):
logger.debug(f'{instance}: Queuing Job')
BackupJob.enqueue(instance)

if is_htmx(request):
if htmx_partial(request):
return self.render('htmx/table.html', extra_context={
'object': instance,
'table': table,
Expand Down
Empty file.
30 changes: 30 additions & 0 deletions netbox_config_backup/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from django.core.exceptions import ValidationError
from django.test import TestCase

from dcim.models import Site, Manufacturer, DeviceType, DeviceRole, Device, Interface
from ipam.models import IPAddress
from netbox_config_backup.models import *

class TestBackup(TestCase):

@classmethod
def setUpTestData(cls):

site = Site.objects.create(name='Site 1')
manufacturer = Manufacturer.objects.create(name='Manufacturer 1')
device_type = DeviceType.objects.create(model='Device Type 1', manufacturer=manufacturer)
role = DeviceRole.objects.create(name='Switch')
device = Device.objects.create(
name='Device 1',
site=site,
device_type=device_type,
role=role,
status='active'
)
interface = Interface.objects.create(name='Interface 1', device=device, type='1000baset')
address = IPAddress.objects.create(assigned_object=interface, address='10.0.0.1/32')
device.primary_ip4 = address
device.save()

def test_create_backup(self):
pass
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='netbox_config_backup',
version='2.0.0',
version='2.0.1',
description='NetBox Configuration Backup',
long_description='Plugin to backup device configuration',
url='https://github.com/dansheps/netbox-config-backup/',
Expand Down

0 comments on commit 1146a43

Please sign in to comment.