Skip to content

Improve debugging for faulty scripts #12766

@candlerb

Description

@candlerb

NetBox version

v3.5.2

Feature type

Change to existing functionality

Proposed functionality

If you upload a script but there's a problem in it, there's no information displayed beyond this:

image

Nothing is sent to Netbox logs either. I would like the actual exception to be displayed.

Use case

It's very hard to develop a script if you don't know what's wrong with it!

Using runscript from the command line doesn't help: it just returns the same as if you'd specified the wrong name for the class.

# python3 /opt/netbox/netbox/manage.py runscript --loglevel debug add_device_type_components.AddDeviceTypeComponents
Traceback (most recent call last):
  File "/opt/netbox/netbox/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/opt/netbox/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/opt/netbox/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/netbox/venv/lib/python3.8/site-packages/django/core/management/base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/opt/netbox/venv/lib/python3.8/site-packages/django/core/management/base.py", line 448, in execute
    output = self.handle(*args, **options)
  File "/opt/netbox/netbox/extras/management/commands/runscript.py", line 93, in handle
    logger = logging.getLogger(f"netbox.scripts.{script.full_name}")
AttributeError: 'NoneType' object has no attribute 'full_name'

Trying to run the script directly from the command line, it doesn't have the right environment set up:

# python3 add_device_type_components.py
Traceback (most recent call last):
  File "add_device_type_components.py", line 5, in <module>
    from dcim.models import (Manufacturer, DeviceType, Device,
ModuleNotFoundError: No module named 'dcim'

Setting PYTHONPATH by itself is insufficient. In the end I used this recipe to run the script, by inserting the following lines at the top:

#!/opt/netbox/venv/bin/python
import django, os, sys
sys.path.append('/opt/netbox/netbox')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'netbox.settings')
django.setup()

And finally I got the error I was looking for:

# python3 add_device_type_components.py
Traceback (most recent call last):
  File "add_device_type_components.py", line 17, in <module>
    class AddDeviceTypeComponents(Script):
  File "add_device_type_components.py", line 22, in AddDeviceTypeComponents
    manufacturer = ObjectVar(model=Manufacturer)
NameError: name 'ObjectVar' is not defined

But this seems to be way too hard :-(

Database changes

None

External dependencies

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    pending closureRequires immediate attention to avoid being closed for inactivitystatus: needs ownerThis issue is tentatively accepted pending a volunteer committed to its implementationtype: featureIntroduction of new functionality to the application

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions