mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-18 05:28:16 -06:00
12510 cleanup
This commit is contained in:
parent
73190fa996
commit
23a8b2a929
@ -271,8 +271,8 @@ class BaseScript(object):
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._logs = {}
|
self._logs = {}
|
||||||
self.failed = False
|
self._failed = False
|
||||||
self._current_method = 'total'
|
self._current_method = 'totals'
|
||||||
self._output = ''
|
self._output = ''
|
||||||
|
|
||||||
# Initiate the log
|
# Initiate the log
|
||||||
@ -283,7 +283,7 @@ class BaseScript(object):
|
|||||||
self.request = None
|
self.request = None
|
||||||
|
|
||||||
# Compile test methods and initialize results skeleton
|
# Compile test methods and initialize results skeleton
|
||||||
self._logs['total'] = {
|
self._logs['totals'] = {
|
||||||
'success': 0,
|
'success': 0,
|
||||||
'info': 0,
|
'info': 0,
|
||||||
'warning': 0,
|
'warning': 0,
|
||||||
@ -454,8 +454,8 @@ class BaseScript(object):
|
|||||||
if log_level != LogLevelChoices.LOG_DEFAULT:
|
if log_level != LogLevelChoices.LOG_DEFAULT:
|
||||||
self._logs[self._current_method][log_level] += 1
|
self._logs[self._current_method][log_level] += 1
|
||||||
|
|
||||||
if self._current_method != 'total':
|
if self._current_method != 'totals':
|
||||||
self._logs['total'][log_level] += 1
|
self._logs['totals'][log_level] += 1
|
||||||
|
|
||||||
if obj:
|
if obj:
|
||||||
self.logger.log(level, f"{log_level.capitalize()} | {obj}: {message}")
|
self.logger.log(level, f"{log_level.capitalize()} | {obj}: {message}")
|
||||||
@ -482,6 +482,7 @@ class BaseScript(object):
|
|||||||
|
|
||||||
def log_failure(self, message, obj=None):
|
def log_failure(self, message, obj=None):
|
||||||
self._log(str(message), obj, log_level=LogLevelChoices.LOG_FAILURE, level=logging.ERROR)
|
self._log(str(message), obj, log_level=LogLevelChoices.LOG_FAILURE, level=logging.ERROR)
|
||||||
|
self._failed = True
|
||||||
|
|
||||||
# Convenience functions
|
# Convenience functions
|
||||||
|
|
||||||
@ -524,23 +525,17 @@ class BaseScript(object):
|
|||||||
self._current_method = method_name
|
self._current_method = method_name
|
||||||
test_method = getattr(self, method_name)
|
test_method = getattr(self, method_name)
|
||||||
test_method()
|
test_method()
|
||||||
if self.failed:
|
|
||||||
self.logger.warning("Report failed")
|
|
||||||
job.terminate(status=JobStatusChoices.STATUS_FAILED)
|
|
||||||
else:
|
|
||||||
self.logger.info("Report completed successfully")
|
|
||||||
job.terminate()
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
stacktrace = traceback.format_exc()
|
self.post_run()
|
||||||
self.log_failure(None, f"An exception occurred: {type(e).__name__}: {e} <pre>{stacktrace}</pre>")
|
self._current_method = 'totals'
|
||||||
logger.error(f"Exception raised during report execution: {e}")
|
raise e
|
||||||
job.terminate(status=JobStatusChoices.STATUS_ERRORED, error=repr(e))
|
|
||||||
|
|
||||||
# Perform any post-run tasks
|
# Perform any post-run tasks
|
||||||
self.post_run()
|
self.post_run()
|
||||||
|
self._current_method = 'totals'
|
||||||
|
|
||||||
def run(self, data, commit, job):
|
def run(self, data, commit):
|
||||||
self.run_test_scripts(job)
|
self.run_test_scripts()
|
||||||
|
|
||||||
def pre_run(self):
|
def pre_run(self):
|
||||||
"""
|
"""
|
||||||
@ -655,6 +650,16 @@ def run_script(data, job, request=None, commit=True, **kwargs):
|
|||||||
|
|
||||||
return fn(**kwargs)
|
return fn(**kwargs)
|
||||||
|
|
||||||
|
def set_job_data(job, script):
|
||||||
|
logs = script._logs
|
||||||
|
totals = logs.pop('totals')
|
||||||
|
job.data = {
|
||||||
|
'logs': logs,
|
||||||
|
'totals': totals,
|
||||||
|
'output': script._output,
|
||||||
|
}
|
||||||
|
return job
|
||||||
|
|
||||||
def _run_script():
|
def _run_script():
|
||||||
"""
|
"""
|
||||||
Core script execution task. We capture this within a subfunction to allow for conditionally wrapping it with
|
Core script execution task. We capture this within a subfunction to allow for conditionally wrapping it with
|
||||||
@ -663,17 +668,18 @@ def run_script(data, job, request=None, commit=True, **kwargs):
|
|||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
script._output = call_with_appropriate(script.run, kwargs={'data': data, 'commit': commit, 'job': job})
|
script._output = script.run(data, commit)
|
||||||
if not commit:
|
if not commit:
|
||||||
raise AbortTransaction()
|
raise AbortTransaction()
|
||||||
except AbortTransaction:
|
except AbortTransaction:
|
||||||
call_with_appropriate(script.log_info, kwargs={'message': "Database changes have been reverted automatically."})
|
call_with_appropriate(script.log_info, kwargs={'message': "Database changes have been reverted automatically."})
|
||||||
if request:
|
if request:
|
||||||
clear_events.send(request)
|
clear_events.send(request)
|
||||||
job.data = {
|
job = set_job_data(job, script)
|
||||||
'logs': script._logs,
|
if script._failed:
|
||||||
'output': script._output,
|
logger.warning(f"Script failed")
|
||||||
}
|
job.terminate(status=JobStatusChoices.STATUS_FAILED)
|
||||||
|
else:
|
||||||
job.terminate()
|
job.terminate()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if type(e) is AbortScript:
|
if type(e) is AbortScript:
|
||||||
@ -684,10 +690,7 @@ def run_script(data, job, request=None, commit=True, **kwargs):
|
|||||||
call_with_appropriate(script.log_failure, kwargs={'message': f"An exception occurred: `{type(e).__name__}: {e}`\n```\n{stacktrace}\n```"})
|
call_with_appropriate(script.log_failure, kwargs={'message': f"An exception occurred: `{type(e).__name__}: {e}`\n```\n{stacktrace}\n```"})
|
||||||
logger.error(f"Exception raised during script execution: {e}")
|
logger.error(f"Exception raised during script execution: {e}")
|
||||||
call_with_appropriate(script.log_info, kwargs={'message': "Database changes have been reverted due to error."})
|
call_with_appropriate(script.log_info, kwargs={'message': "Database changes have been reverted due to error."})
|
||||||
job.data = {
|
job = set_job_data(job, script)
|
||||||
'logs': script._logs,
|
|
||||||
'output': script._output,
|
|
||||||
}
|
|
||||||
job.terminate(status=JobStatusChoices.STATUS_ERRORED, error=repr(e))
|
job.terminate(status=JobStatusChoices.STATUS_ERRORED, error=repr(e))
|
||||||
if request:
|
if request:
|
||||||
clear_events.send(request)
|
clear_events.send(request)
|
||||||
|
Loading…
Reference in New Issue
Block a user