Skip to content

Commit

Permalink
added job history/logs url to alert email (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
ansibleguy committed May 14, 2024
1 parent 939b29c commit 7ba593d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docs/source/usage/alerts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ There is a generic alert-plugin interface for custom solutions.
"failed": true,
"status": "Failed",
"job_name": "test2",
"job_id": 1,
"execution_id": 85,
"user_name": "ansible",
"time_start": 1715502006,
"time_start_pretty": "2024-05-12 08:20:06 CEST",
Expand All @@ -85,6 +87,7 @@ There is a generic alert-plugin interface for custom solutions.
"time_duration_pretty": "1s",
"error_short": null,
"error_med": null,
"log_url": "http://localhost:8000/ui/jobs/log?job=1",
"log_stdout": "/home/guy/.local/share/ansible-webui/test2_2024-05-12_08-20-06_ansible_stdout.log",
"log_stdout_url": "http://localhost:8000/api/job/1/85/log?type=stdout",
"log_stderr": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _email_send(server: SMTP, user: USERS, stats: dict, execution: JobExecution,
if _tmpl_text.is_file():
tmpl_text = str(_tmpl_text)

tmpl_ctx = {'execution': execution, 'stats': stats, 'web_addr': get_main_web_address(), 'error_msgs': error_msgs}
tmpl_ctx = {'execution': execution, 'stats': stats, 'url': get_main_web_address(), 'error_msgs': error_msgs}
text_content = get_template(tmpl_text).render(tmpl_ctx)
html_content = get_template(tmpl_html).render(tmpl_ctx)
html_content = html_transform_styles(html=html_content, pretty_print=True, allow_network=False)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def alert_plugin_wrapper(
)
return

url = get_main_web_address()

data = {
'alert': {
'type': 'global',
Expand All @@ -48,6 +50,8 @@ def alert_plugin_wrapper(
'failed': failed,
'status': execution.status_name,
'job_name': execution.job.name,
'job_id': execution.job.id,
'execution_id': execution.id,
'user_name': execution.user_name,
'time_start': int(unix_timestamp(execution.time_created_dt.timetuple())),
'time_start_pretty': execution.time_created_str,
Expand All @@ -57,6 +61,7 @@ def alert_plugin_wrapper(
'time_duration_pretty': None,
'error_short': None,
'error_med': None,
'log_url': f"{url}/ui/jobs/log?job={execution.job.id}",
},
'errors': error_msgs,
'stats': stats,
Expand Down Expand Up @@ -91,7 +96,7 @@ def alert_plugin_wrapper(
file = getattr(execution, log_attr)
if Path(file).is_file():
data['execution'][log_attr] = file
data['execution'][url_attr] = f"{get_main_web_address()}{getattr(execution, url_attr)}"
data['execution'][url_attr] = f"{url}{getattr(execution, url_attr)}"

else:
data['execution'][log_attr] = None
Expand Down
8 changes: 7 additions & 1 deletion src/ansibleguy-webui/aw/templates/email/alert.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
}
a {
color: #0000EE;
text-decoration: none;
}
a:hover {
text-decoration: underline;
color: #B3D8FA;
}
@media (prefers-color-scheme: dark ) {
body {
Expand Down Expand Up @@ -70,11 +75,12 @@ <h2><b>Job</b>: {{ execution.job.name }}</h2>
<br>
{% endif %}
{% endif %}
<b>Job History</b>: <a href="{{ url }}/ui/jobs/log?job={{ execution.job.id }}">Link</a><br>
{% for log_attr in execution.log_file_fields %}
{% set_var execution|get_value:log_attr as log_file %}
{% set_var log_attr|concat:'_url' as log_url %}
{% if log_file|file_exists %}
<b>{{ log_attr|whitespace_char:'_'|capitalize }}</b>: <a href="{{ web_addr }}{{ execution|get_value:log_url }}">Download</a><br>
<b>{{ log_attr|whitespace_char:'_'|capitalize }}</b>: <a href="{{ url }}{{ execution|get_value:log_url }}">Download</a><br>
{% endif %}
{% endfor %}
{% if stats|exists %}
Expand Down

0 comments on commit 7ba593d

Please sign in to comment.