Skip to content

18136 Allow Script running within a Branch #18137

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/configuration/system.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,11 @@ The time zone NetBox will use when dealing with dates and times. It is recommend
Default: True

Enables language translation for the user interface. (This parameter maps to Django's [USE_I18N](https://docs.djangoproject.com/en/stable/ref/settings/#std-setting-USE_I18N) setting.)

---

## SCRIPT_CONTEXT_MANAGERS

Default: []

The dotted path to any additional context managers to use when running scripts.
24 changes: 22 additions & 2 deletions netbox/extras/jobs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import logging
import traceback
from contextlib import nullcontext
from contextlib import ExitStack, nullcontext

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db import transaction
from django.utils.module_loading import import_string
from django.utils.translation import gettext as _

from core.signals import clear_events
Expand Down Expand Up @@ -88,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 @@ -98,7 +102,23 @@ def run(self, data, request=None, commit=True, **kwargs):
# Add the current request as a property of the script
script.request = request

context_managers = []
for context_manager_str in settings.SCRIPT_CONTEXT_MANAGERS:
try:
context_managers.append(import_string(context_manager_str))
except AttributeError:
message = _("Failed to import configured SCRIPT_CONTEXT_MANAGERS: ") + context_manager_str
logger.error(message)
raise ImproperlyConfigured(message)

# 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():
self.run_script(script, request, data, commit)
if context_managers:
with ExitStack() as stack:
for cm in context_managers:
stack.enter_context(cm(request))

self.run_script(script, request, data, commit)
else:
self.run_script(script, request, data, commit)
1 change: 1 addition & 0 deletions netbox/netbox/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
STORAGE_CONFIG = getattr(configuration, 'STORAGE_CONFIG', {})
TIME_ZONE = getattr(configuration, 'TIME_ZONE', 'UTC')
TRANSLATION_ENABLED = getattr(configuration, 'TRANSLATION_ENABLED', True)
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
Loading