Skip to content
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

Custom Script CLI and API execution not working #17923

Closed
wz4 opened this issue Nov 4, 2024 · 7 comments
Closed

Custom Script CLI and API execution not working #17923

wz4 opened this issue Nov 4, 2024 · 7 comments
Assignees
Labels
severity: low Does not significantly disrupt application functionality, or a workaround is available status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application

Comments

@wz4
Copy link

wz4 commented Nov 4, 2024

Deployment Type

Self-hosted

Triage priority

N/A

NetBox Version

v4.1.6

Python Version

3.11

Steps to Reproduce

Unable to execute custom scripts via the CLI and API. This is the same issue referenced in #17885. The fix implemented only applies to the GUI.

Adding the script name as an argument to the enqueue method fixes the problem.

ScriptJob.enqueue(
instance=script,
user=request.user,
data=input_serializer.data['data'],
request=copy_safe_request(request),
commit=input_serializer.data['commit'],
job_timeout=script.python_class.job_timeout,
schedule_at=input_serializer.validated_data.get('schedule_at'),
interval=input_serializer.validated_data.get('interval')
)

# Execute the script.
job = ScriptJob.enqueue(
instance=script_obj,
user=user,
immediate=True,
data=data,
request=NetBoxFakeRequest({
'META': {},
'POST': data,
'GET': {},
'FILES': {},
'user': user,
'path': '',
'id': uuid.uuid4()
}),
commit=commit,
)

Expected Behavior

Executing a script for the CLI and API should work

Observed Behavior

The following error is produced when attempting to use the CLI runscript command

  File "/opt/netbox/netbox/extras/management/commands/runscript.py", line 85, in handle
    job = ScriptJob.enqueue(
          ^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/netbox/netbox/jobs.py", line 106, in enqueue
    return Job.enqueue(cls.handle, name=name, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/netbox/core/models/jobs.py", line 236, in enqueue
    job.full_clean()
  File "/opt/netbox/venv/lib/python3.12/site-packages/django/db/models/base.py", line 1561, in full_clean
    raise ValidationError(errors)
django.core.exceptions.ValidationError: {'name': ['This field cannot be blank.']}
@wz4 wz4 added status: needs triage This issue is awaiting triage by a maintainer type: bug A confirmed report of unexpected behavior in the application labels Nov 4, 2024
@cruse1977
Copy link
Member

+1 to this

@alehaa
Copy link
Contributor

alehaa commented Nov 4, 2024

I repeat my comment from #17885 and volunteer for submitting a PR:

The bug originates from the following code. The justification of this was to not have the script name twice (as job name and related object) listed in the job table. Just having "SciptJob" as generic name seemed a bit odd to me at that time.

class Meta:
# An explicit job name is not set because it doesn't make sense in this context. Currently, there's no scenario
# where jobs other than this one are used. Therefore, it is hidden, resulting in a cleaner job table overview.
name = ''

[…] Reverting #17886 and removing the code linked above should fix the issue and avoid confusion. ScriptJob will be used as default name afterwards and the script linked as job object.

@arthanson
Copy link
Collaborator

@alehaa do you want to submit a PR, can you also check if it addresses #17921?

@arthanson arthanson added status: accepted This issue has been accepted for implementation and removed status: needs triage This issue is awaiting triage by a maintainer labels Nov 4, 2024
@arthanson arthanson added the severity: low Does not significantly disrupt application functionality, or a workaround is available label Nov 4, 2024
alehaa added a commit to alehaa/netbox that referenced this issue Nov 4, 2024
With c34a0e2, validation of job object fields is enabled, so ScriptJob
must not set required fields to empty strings. This commit reverts
b18f193 and (hopefully) fixes this issue not only for UI views, but for
all interactions with scripts.

Fixes: netbox-community#17923
@llamafilm
Copy link
Contributor

@alehaa are you still planning on making a PR?

@alehaa
Copy link
Contributor

alehaa commented Nov 10, 2024

There is already one linked to this issue. :)

@llamafilm
Copy link
Contributor

Thanks! I missed that. I tested your change now and I can confirm it fixes the problem for both CLI and scheduled script execution.

@jeremystretch
Copy link
Member

I believe this has been resolved by PR #17932.

This was referenced Nov 21, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 20, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
severity: low Does not significantly disrupt application functionality, or a workaround is available status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application
Projects
None yet
Development

No branches or pull requests

6 participants