Skip to content

CLI script invocation is broken in NetBox 4.3.1 #19529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
peteeckel opened this issue May 18, 2025 · 7 comments · May be fixed by #19568
Open

CLI script invocation is broken in NetBox 4.3.1 #19529

peteeckel opened this issue May 18, 2025 · 7 comments · May be fixed by #19568
Assignees
Labels
severity: low Does not significantly disrupt application functionality, or a workaround is available status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application

Comments

@peteeckel
Copy link
Contributor

peteeckel commented May 18, 2025

Deployment Type

Self-hosted

NetBox Version

v4.3.1

Python Version

3.11

Steps to Reproduce

  1. Set SCRIPTS_ROOT = '/opt/netbox/netbox/scripts' in configuration.py
  2. Create a custom script and save it as dummy.py:
from extras.scripts import Script

name = "Dummy"


class Dummy(Script):

    class Meta:
        name = "Dummy Script"
        commit_default = True

    def run(self, data, commit):
        self.log_info(f"Script was executed")
  1. Add the script to NetBox using "Customization/Scripts/Add"
  2. Try to run the script using the GUI
  3. Verify that the script gets executed properly
Image 6. Now try to run the script using the API:
# /opt/netbox/netbox/manage.py runscript dummy.Dummy

Expected Behavior

The script gets executed from the command line, like in NetBox 4.2.9:

# /opt/netbox/netbox/manage.py runscript dummy.Dummy
[2025-05-18 09:40:21,967][INFO] - Running script (commit=False)
[2025-05-18 09:40:21,967][INFO] - Script was executed
[2025-05-18 09:40:21,968][INFO] - Database changes have been reverted automatically.
[2025-05-18 09:40:21,973][INFO] - Script completed in 0 minutes, 0.01 seconds

Observed Behavior

Script execution failed with an exception:

# /opt/netbox/netbox/manage.py runscript dummy.Dummy
Traceback (most recent call last):
  File "/opt/netbox/netbox/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/opt/netbox/lib64/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/opt/netbox/lib64/python3.11/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/netbox/lib64/python3.11/site-packages/django/core/management/base.py", line 416, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/opt/netbox/lib64/python3.11/site-packages/django/core/management/base.py", line 460, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/netbox/extras/management/commands/runscript.py", line 41, in handle
    script_obj = get_module_and_script(module_name, script_name)[1]
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/netbox/extras/scripts.py", line 649, in get_module_and_script
    module = ScriptModule.objects.get(file_path=f'{module_name}.py')
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/lib64/python3.11/site-packages/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/lib64/python3.11/site-packages/django/db/models/query.py", line 633, in get
    raise self.model.DoesNotExist(
extras.models.scripts.ScriptModule.DoesNotExist: ScriptModule matching query does not exist.

The script can be made to run in NetBox 4.3.1 with a modified invocation (which is definitely not how it's supposed to work):

# /opt/netbox/netbox/manage.py runscript /opt/netbox/netbox/scripts/dummy.Dummy
[2025-05-18 09:36:02,840][INFO] - Running script (commit=False)
[2025-05-18 09:36:02,840][INFO] - Script was executed
[2025-05-18 09:36:02,840][INFO] - Database changes have been reverted automatically.
[2025-05-18 09:36:02,847][INFO] - Script completed in 0 minutes, 0.01 seconds
@peteeckel peteeckel added type: bug A confirmed report of unexpected behavior in the application status: needs triage This issue is awaiting triage by a maintainer labels May 18, 2025
@jnovinger
Copy link
Member

Hrm, the workaround is not working for me, at least in my dev environment.

@jnovinger jnovinger added status: accepted This issue has been accepted for implementation severity: medium Results in substantial degraded or broken functionality for specfic workflows and removed status: needs triage This issue is awaiting triage by a maintainer labels May 19, 2025
@jnovinger jnovinger self-assigned this May 19, 2025
@peteeckel
Copy link
Contributor Author

Hrm, the workaround is not working for me, at least in my dev environment.

Permissions issue perhaps? It worked for me in two environments.

Anyway, it's just a side note.

@jnovinger
Copy link
Member

Permissions issue perhaps? It worked for me in two environments.

Anyway, it's just a side note.

Turns out I was invoking the workaround incorrectly, my bad. Your workaround does work for me.

@jnovinger
Copy link
Member

This also affects Event Rule bulk import as it also relies on extras.scripts.get_module_and_script, which is where the issue is. Will have a fix up shortly.

@jnovinger jnovinger added severity: low Does not significantly disrupt application functionality, or a workaround is available and removed severity: medium Results in substantial degraded or broken functionality for specfic workflows labels May 20, 2025
jnovinger added a commit that referenced this issue May 23, 2025
ScriptFileSystemStorage was introduced in #18896 and stores a full path
in Script.file_path. This fix handles looking for scripts by relative
path as well as full path.
@ninabel
Copy link

ninabel commented May 28, 2025

This also affects invocation scripts through API.
After upgrade old scripts are still working. But a new or recreated script can't by found by the name.
Also in browser you can find a new scrip instance by id:
/api/extras/scripts/
but not by name
/api/extras/scripts/.

@ninabel
Copy link

ninabel commented May 28, 2025

@jnovinger your fix doesn't help me.
Really, old scripts in the same directory works fine.
It seems that netbox looks in some cash and don't go farther if doesn't find the script there.

@ninabel
Copy link

ninabel commented May 29, 2025

It seems that I've found the problem. New scripts was stored in database with full path.

netbox=# select id, file_path from core_managedfile;
 id |               file_path
----+----------------------------------------
  3 | old_script.py
 13 | /opt/netbox/netbox/scripts/test.py
(4 rows)

Was it change done intentionally?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
severity: low Does not significantly disrupt application functionality, or a workaround is available status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants