18136 update script runner to use SCRIPT_CONTEXT_MANAGERS

This commit is contained in:
Arthur Hanson 2024-12-09 09:33:13 -08:00
parent 768d89734c
commit 03a769e8ec
3 changed files with 19 additions and 16 deletions

View File

@ -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.

View File

@ -1,6 +1,6 @@
import logging import logging
import traceback import traceback
from contextlib import nullcontext from contextlib import ExitStack, nullcontext
from django.conf import settings from django.conf import settings
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
@ -91,6 +91,7 @@ class ScriptJob(JobRunner):
commit: Passed through to Script.run() commit: Passed through to Script.run()
""" """
script = ScriptModel.objects.get(pk=self.job.object_id).python_class() 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 # Add files to form data
if request: if request:
@ -101,21 +102,23 @@ class ScriptJob(JobRunner):
# Add the current request as a property of the script # Add the current request as a property of the script
script.request = request script.request = request
# Execute the script. If commit is True, wrap it with the event_tracking context manager to ensure we process context_managers = []
# change logging, event rules, etc. for context_manager_str in settings.SCRIPT_CONTEXT_MANAGERS:
branch = None
if settings.BRANCHING_BACKEND:
try: try:
branching_cls = import_string(settings.BRANCHING_BACKEND) context_managers.append(import_string(context_manager_str))
except AttributeError: except AttributeError:
logger = logging.getLogger(f"netbox.scripts.{script.full_name}") message = _("Failed to import configured SCRIPT_CONTEXT_MANAGERS: ") + context_manager_str
message = _("Failed to import configured BRANCHING_BACKEND: ") + settings.BRANCHING_BACKEND
logger.error(message) logger.error(message)
raise ImproperlyConfigured(message) raise ImproperlyConfigured(message)
branching_backend = branching_cls() # Execute the script. If commit is True, wrap it with the event_tracking context manager to ensure we process
branch = branching_backend.get_active_branch(request) # 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(): self.run_script(script, request, data, commit)
with event_tracking(request) if commit else nullcontext(): else:
self.run_script(script, request, data, commit) self.run_script(script, request, data, commit)

View File

@ -179,7 +179,7 @@ STORAGE_BACKEND = getattr(configuration, 'STORAGE_BACKEND', None)
STORAGE_CONFIG = getattr(configuration, 'STORAGE_CONFIG', {}) STORAGE_CONFIG = getattr(configuration, 'STORAGE_CONFIG', {})
TIME_ZONE = getattr(configuration, 'TIME_ZONE', 'UTC') TIME_ZONE = getattr(configuration, 'TIME_ZONE', 'UTC')
TRANSLATION_ENABLED = getattr(configuration, 'TRANSLATION_ENABLED', True) 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 # Load any dynamic configuration parameters which have been hard-coded in the configuration file
for param in CONFIG_PARAMS: for param in CONFIG_PARAMS: