Skip to content

Fixes #17323: Associate job with script object when executed using runscript command #17343

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

Merged
merged 1 commit into from
Sep 3, 2024
Merged
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
15 changes: 7 additions & 8 deletions netbox/extras/management/commands/runscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def add_arguments(self, parser):

def handle(self, *args, **options):

def _run_script():
def _run_script(script):
"""
Core script execution task. We capture this within a subfunction to allow for conditionally wrapping it with
the event_tracking context manager (which is bypassed if commit == False).
Expand Down Expand Up @@ -85,7 +85,6 @@ def _run_script():

module_name, script_name = script.split('.', 1)
module, script = get_module_and_script(module_name, script_name)
script = script.python_class

# Take user from command line if provided and exists, other
if options['user']:
Expand All @@ -102,7 +101,7 @@ def _run_script():
stdouthandler.setLevel(logging.DEBUG)
stdouthandler.setFormatter(formatter)

logger = logging.getLogger(f"netbox.scripts.{script.full_name}")
logger = logging.getLogger(f"netbox.scripts.{script.python_class.full_name}")
logger.addHandler(stdouthandler)

try:
Expand All @@ -118,13 +117,13 @@ def _run_script():
raise CommandError(f"Invalid log level: {loglevel}")

# Initialize the script form
script = script()
form = script.as_form(data, None)
script_instance = script.python_class()
form = script_instance.as_form(data, None)

# Create the job
job = Job.objects.create(
object=module,
name=script.class_name,
object=script,
name=script_instance.class_name,
user=user,
job_id=uuid.uuid4()
)
Expand All @@ -149,7 +148,7 @@ def _run_script():
# Execute the script. If commit is True, wrap it with the event_tracking context manager to ensure we process
# change logging, webhooks, etc.
with event_tracking(request):
_run_script()
_run_script(script_instance)
else:
logger.error('Data is not valid:')
for field, errors in form.errors.get_json_data().items():
Expand Down