amyasnikov / validity

NetBox plugin to validate network devices
MIT License
136 stars 8 forks source link

Attempts to run SyncGitRepos result in MultipleObjectsReturned exception #54

Closed jamiereid closed 1 year ago

jamiereid commented 1 year ago

Netbox Version: 3.5.8 Validity Version: 1.3.1

When attempting to sync git repos, the following exception is raised.

<class 'extras.models.scripts.ScriptModule.MultipleObjectsReturned'>

get() returned more than one ScriptModule -- it returned 2!

Python version: 3.10.12
NetBox version: 3.5.8
Plugins: 
  netbox_topology_views: 3.6.2
  validity: 1.3.1

If there is more information I can provide to help narrow down the issue, please let me know.

amyasnikov commented 1 year ago

Hi @jamiereid, please provide the following:

  1. Full traceback of the error
  2. Steps to reproduce the error on clean netbox installation.
jamiereid commented 1 year ago

Repro

  1. Download the netboxcommunity/netbox-docker files for modification

    git clone -b release https://github.com/netbox-community/netbox-docker.git
    cd netbox-docker
  2. Add 'Validity' plugin to plugin_requirements.txt

    echo 'netbox-validity==1.3.1' >> plugin_requirements.txt
  3. Create Dockerfile-Plugins for custom container with the following contents

    
    FROM netboxcommunity/netbox:latest

COPY ./plugin_requirements.txt /opt/netbox RUN /opt/netbox/venv/bin/pip install --no-warn-script-location -r /opt/netbox/plugin_requirements.txt


4. Create `docker-compose.override.yml` with the following contents
```yml
version: '3.4' # This is NOT the version of NetBox! No need to adjust :)
services:
  netbox:
    image: netbox:latest-plugins
    ports:
      - 8000:8080
    build:
      context: .
      dockerfile: Dockerfile-Plugins
  netbox-worker:
    image: netbox:latest-plugins
    build:
      context: .
      dockerfile: Dockerfile-Plugins
  netbox-housekeeping:
    image: netbox:latest-plugins
    build:
      context: .
      dockerfile: Dockerfile-Plugins
  1. Enable 'Validity' plugin in configuration/plugins.py

    echo 'PLUGINS = ["validity"]' >> configuration/plugins.py
  2. Start the containers

    docker compose up
  3. Once the container is 'healthy' (seen in docker ps) you can create an admin user

    docker compose exec netbox /opt/netbox/netbox/manage.py createsuperuser
  4. Log into Netbox as the super user you just created and add a git repo to Validity.

  5. Attempt to sync the git repo by either clicking the "Sync" button, or running the validity_git script

Full Traceback

Internal Server Error: /extras/scripts/validity_git/SyncGitRepos/
Traceback (most recent call last):
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 56, in inner
    response = get_response(request)
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/views/generic/base.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
  File "/opt/netbox/netbox/utilities/views.py", line 53, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/views/generic/base.py", line 142, in dispatch
    return handler(request, *args, **kwargs)
  File "/opt/netbox/netbox/extras/views.py", line 1073, in post
    module = get_object_or_404(ScriptModule.objects.restrict(request.user), file_path__startswith=module)
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/shortcuts.py", line 85, in get_object_or_404
    return queryset.get(*args, **kwargs)
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/models/query.py", line 653, in get
    raise self.model.MultipleObjectsReturned(
extras.models.scripts.ScriptModule.MultipleObjectsReturned: get() returned more than one ScriptModule -- it returned 2!
jamiereid commented 1 year ago

I noticed there were some .pyc files in the netbox/scripts directory:

 __init__.cpython-310.pyc
validity_git.cpython-310.pyc
validity_run_tests.cpython-310.pyc

After deleting these, I am able to run the script to sync.

amyasnikov commented 1 year ago

Yeah, the problem was these .pyc files should not become script modules but they actually did. The easiest way to fix it is just manual deletion of these .pyc modules from netbox UI as you've mentioned. I've fixed it and they won't appear in the scripts section starting from the next validity version