[go: nahoru, domu]

Skip to content

Commit

Permalink
Fixes #17323: Associate job with script object when executed using ru…
Browse files Browse the repository at this point in the history
…nscript command
  • Loading branch information
jeremystretch committed Sep 3, 2024
1 parent d18a853 commit 814b699
Showing 1 changed file with 7 additions and 8 deletions.
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

0 comments on commit 814b699

Please sign in to comment.