12416 warning for missing script file (#12456)

* 12416 warning for missing script file

* 12416 widen exception catching for internal script error

* Update netbox/extras/models/scripts.py

Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>

* 12416 update from review feedback

---------

Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
This commit is contained in:
Arthur Hanson 2023-05-05 06:37:38 -07:00 committed by GitHub
parent 7a38f601de
commit 9909213c0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 39 deletions

View File

@ -1,4 +1,5 @@
import inspect import inspect
import logging
from functools import cached_property from functools import cached_property
from django.db import models from django.db import models
@ -16,6 +17,8 @@ __all__ = (
'ScriptModule', 'ScriptModule',
) )
logger = logging.getLogger('netbox.data_backends')
class Script(WebhooksMixin, models.Model): class Script(WebhooksMixin, models.Model):
""" """
@ -53,7 +56,12 @@ class ScriptModule(PythonModuleMixin, JobsMixin, ManagedFile):
# For child objects in submodules use the full import path w/o the root module as the name # For child objects in submodules use the full import path w/o the root module as the name
return cls.full_name.split(".", maxsplit=1)[1] return cls.full_name.split(".", maxsplit=1)[1]
module = self.get_module() try:
module = self.get_module()
except Exception as e:
logger.debug(f"Failed to load script: {self.python_name} error: {e}")
module = None
scripts = {} scripts = {}
ordered = getattr(module, 'script_order', []) ordered = getattr(module, 'script_order', [])

View File

@ -1033,7 +1033,6 @@ class ScriptView(ContentTypePermissionRequiredMixin, View):
return 'extras.view_script' return 'extras.view_script'
def get(self, request, module, name): def get(self, request, module, name):
print(module)
module = get_object_or_404(ScriptModule.objects.restrict(request.user), file_path__startswith=module) module = get_object_or_404(ScriptModule.objects.restrict(request.user), file_path__startswith=module)
script = module.scripts[name]() script = module.scripts[name]()
form = script.as_form(initial=normalize_querydict(request.GET)) form = script.as_form(initial=normalize_querydict(request.GET))

View File

@ -37,43 +37,49 @@
</h5> </h5>
<div class="card-body"> <div class="card-body">
{% include 'inc/sync_warning.html' with object=module %} {% include 'inc/sync_warning.html' with object=module %}
<table class="table table-hover table-headings reports"> {% if not module.scripts %}
<thead> <div class="alert alert-warning d-flex align-items-center" role="alert">
<tr> <i class="mdi mdi-alert"></i>&nbsp; Script file at: {{module.full_path}} could not be loaded.
<th width="250">Name</th> </div>
<th>Description</th> {% else %}
<th>Last Run</th> <table class="table table-hover table-headings reports">
<th class="text-end">Status</th> <thead>
</tr> <tr>
</thead> <th width="250">Name</th>
<tbody> <th>Description</th>
{% with jobs=module.get_latest_jobs %} <th>Last Run</th>
{% for script_name, script_class in module.scripts.items %} <th class="text-end">Status</th>
<tr> </tr>
<td> </thead>
<a href="{% url 'extras:script' module=module.python_name name=script_name %}" name="script.{{ script_name }}">{{ script_class.name }}</a> <tbody>
</td> {% with jobs=module.get_latest_jobs %}
<td> {% for script_name, script_class in module.scripts.items %}
{{ script_class.Meta.description|markdown|placeholder }} <tr>
</td> <td>
{% with last_result=jobs|get_key:script_class.name %} <a href="{% url 'extras:script' module=module.python_name name=script_name %}" name="script.{{ script_name }}">{{ script_class.name }}</a>
{% if last_result %} </td>
<td> <td>
<a href="{% url 'extras:script_result' job_pk=last_result.pk %}">{{ last_result.created|annotated_date }}</a> {{ script_class.Meta.description|markdown|placeholder }}
</td> </td>
<td class="text-end"> {% with last_result=jobs|get_key:script_class.name %}
{% badge last_result.get_status_display last_result.get_status_color %} {% if last_result %}
</td> <td>
{% else %} <a href="{% url 'extras:script_result' job_pk=last_result.pk %}">{{ last_result.created|annotated_date }}</a>
<td class="text-muted">Never</td> </td>
<td class="text-end">{{ ''|placeholder }}</td> <td class="text-end">
{% endif %} {% badge last_result.get_status_display last_result.get_status_color %}
{% endwith %} </td>
</tr> {% else %}
{% endfor %} <td class="text-muted">Never</td>
{% endwith %} <td class="text-end">{{ ''|placeholder }}</td>
</tbody> {% endif %}
</table> {% endwith %}
</tr>
{% endfor %}
{% endwith %}
</tbody>
</table>
{% endif %}
</div> </div>
</div> </div>
{% empty %} {% empty %}