Skip to content

Commit

Permalink
18136 update script runner to use SCRIPT_CONTEXT_MANAGERS
Browse files Browse the repository at this point in the history
  • Loading branch information
arthanson committed Dec 9, 2024
1 parent 768d897 commit 03a769e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
6 changes: 3 additions & 3 deletions docs/configuration/system.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ Enables language translation for the user interface. (This parameter maps to Dja

---

## BRANCHING_BACKEND
## SCRIPT_CONTEXT_MANAGERS

Default: None
Default: []

The dotted path to the desired branching class. If using [netboxlabs-netbox-branching](https://github.com/netboxlabs/netbox-branching) set this to `netbox_branching.backends.BranchingBackend`
The dotted path to any additional context managers to use when running scripts.
27 changes: 15 additions & 12 deletions netbox/extras/jobs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import traceback
from contextlib import nullcontext
from contextlib import ExitStack, nullcontext

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
Expand Down Expand Up @@ -91,6 +91,7 @@ def run(self, data, request=None, commit=True, **kwargs):
commit: Passed through to Script.run()
"""
script = ScriptModel.objects.get(pk=self.job.object_id).python_class()
logger = logging.getLogger(f"netbox.scripts.{script.full_name}")

# Add files to form data
if request:
Expand All @@ -101,21 +102,23 @@ def run(self, data, request=None, commit=True, **kwargs):
# Add the current request as a property of the script
script.request = request

# Execute the script. If commit is True, wrap it with the event_tracking context manager to ensure we process
# change logging, event rules, etc.
branch = None
if settings.BRANCHING_BACKEND:
context_managers = []
for context_manager_str in settings.SCRIPT_CONTEXT_MANAGERS:
try:
branching_cls = import_string(settings.BRANCHING_BACKEND)
context_managers.append(import_string(context_manager_str))
except AttributeError:
logger = logging.getLogger(f"netbox.scripts.{script.full_name}")
message = _("Failed to import configured BRANCHING_BACKEND: ") + settings.BRANCHING_BACKEND
message = _("Failed to import configured SCRIPT_CONTEXT_MANAGERS: ") + context_manager_str
logger.error(message)
raise ImproperlyConfigured(message)

branching_backend = branching_cls()
branch = branching_backend.get_active_branch(request)
# Execute the script. If commit is True, wrap it with the event_tracking context manager to ensure we process
# change logging, event rules, etc.
with event_tracking(request) if commit else nullcontext():
if context_managers:
with ExitStack() as stack:
for cm in context_managers:
stack.enter_context(cm(request))

with branching_backend.activate_branch(branch) if branch else nullcontext():
with event_tracking(request) if commit else nullcontext():
self.run_script(script, request, data, commit)
else:
self.run_script(script, request, data, commit)
2 changes: 1 addition & 1 deletion netbox/netbox/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@
STORAGE_CONFIG = getattr(configuration, 'STORAGE_CONFIG', {})
TIME_ZONE = getattr(configuration, 'TIME_ZONE', 'UTC')
TRANSLATION_ENABLED = getattr(configuration, 'TRANSLATION_ENABLED', True)
BRANCHING_BACKEND = getattr(configuration, 'BRANCHING_BACKEND', None)
SCRIPT_CONTEXT_MANAGERS = getattr(configuration, 'SCRIPT_CONTEXT_MANAGERS', [])

# Load any dynamic configuration parameters which have been hard-coded in the configuration file
for param in CONFIG_PARAMS:
Expand Down

0 comments on commit 03a769e

Please sign in to comment.