Skip to content

Commit

Permalink
support for custom email templates
Browse files Browse the repository at this point in the history
  • Loading branch information
ansibleguy committed May 12, 2024
1 parent f15ce99 commit 3da922a
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 13 deletions.
4 changes: 3 additions & 1 deletion docs/source/usage/alerts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ After that you can receive e-mails on job finish/failure.

|alert_email|

You can modify the email templates by setting the :code:`Template Directory` in your system config. If you want to do so - copy `the existing templates <https://github.com/ansibleguy/webui/tree/latest/src/ansibleguy-webui/aw/templates/email>`_ and modify them as needed. Note: the `Django template syntax <https://docs.djangoproject.com/en/5.0/ref/templates/language/>`_ is required.

----

Plugins
Expand All @@ -63,7 +65,7 @@ There is a generic alert-plugin interface for custom solutions.
"name": "ansible",
"first_name": "",
"last_name": "",
"email": "guy@ansibleguy.net",
"email": "ansible@localhost",
"phone": null,
"description": "test",
"is_active": true,
Expand Down
1 change: 1 addition & 0 deletions src/ansibleguy-webui/aw/config/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def _get_defaults_docker(var: str) -> any:
'path_run': '/tmp/ansible-webui',
'path_play': getcwd(),
'path_log': f"{environ['HOME']}/.local/share/ansible-webui",
'path_template': None, # only for custom overrides
'db': f"{environ['HOME']}/.config/ansible-webui",
'timezone': datetime.now().astimezone().tzname(),
'secret': ''.join(random_choice(ascii_letters + digits + punctuation) for _ in range(50)),
Expand Down
2 changes: 2 additions & 0 deletions src/ansibleguy-webui/aw/config/form_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
'path_run': 'Runtime directory',
'path_play': 'Playbook base-directory',
'path_log': 'Directory for execution-logs',
'path_template': 'Directory for templates',
'run_timeout': 'Timeout for playbook execution',
'session_timeout': 'Timeout for WebUI login-sessions',
'path_ansible_config': 'Ansible Config-File',
Expand Down Expand Up @@ -200,6 +201,7 @@
'path_play': 'Path to the <a href="https://docs.ansible.com/ansible/2.8/user_guide/'
'playbooks_best_practices.html#directory-layout">Ansible base/playbook directory</a>',
'path_log': 'Define the path where full job-logs are saved',
'path_template': 'Define the path where custom templates are placed',
'path_ansible_config': 'Path to a <a href="https://docs.ansible.com/ansible/latest/installation_guide'
'/intro_configuration.html#configuration-file">Ansible config-file</a> to use',
'path_ssh_known_hosts': 'Path to a <a href="https://en.wikibooks.org/wiki/OpenSSH/'
Expand Down
17 changes: 14 additions & 3 deletions src/ansibleguy-webui/aw/execute/alert_plugin/plugin_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.template.loader import get_template

from aw.base import USERS
from aw.utils.util import valid_email
from aw.utils.util import valid_email, is_set
from aw.utils.debug import log
from aw.config.main import config
from aw.model.job import JobExecution
Expand All @@ -22,9 +22,20 @@ def _email_send(server: SMTP, user: USERS, stats: dict, execution: JobExecution,
msg['From'] = config['mail_sender']
msg['To'] = user.email

tmpl_html, tmpl_text = 'email/alert.html', 'email/alert.txt'
if is_set(config['path_template']):
_tmpl_base = Path(config['path_template'])
_tmpl_html = _tmpl_base / 'alert.html'
_tmpl_text = _tmpl_base / 'alert.txt'
if _tmpl_html.is_file():
tmpl_html = str(_tmpl_html)

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}
text_content = get_template('email/alert.txt').render(tmpl_ctx)
html_content = get_template('email/alert.html').render(tmpl_ctx)
text_content = get_template(tmpl_text).render(tmpl_ctx)
html_content = get_template(tmpl_html).render(tmpl_ctx)

msg.attach(MIMEText(text_content, 'plain'))
msg.attach(MIMEText(html_content, 'html'))
Expand Down
5 changes: 3 additions & 2 deletions src/ansibleguy-webui/aw/model/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
class SystemConfig(BaseModel):
SECRET_ATTRS = ['mail_pass']
api_fields_read = [
'path_run', 'path_play', 'path_log', 'timezone', 'run_timeout', 'session_timeout', 'path_ansible_config',
'path_ssh_known_hosts', 'debug', 'logo_url', 'ara_server', 'global_environment_vars',
'path_run', 'path_play', 'path_log', 'path_template', 'timezone', 'run_timeout', 'session_timeout',
'path_ansible_config', 'path_ssh_known_hosts', 'debug', 'logo_url', 'ara_server', 'global_environment_vars',
'mail_server', 'mail_transport', 'mail_ssl_verify', 'mail_sender', 'mail_user',
]

Expand All @@ -38,6 +38,7 @@ class SystemConfig(BaseModel):
path_run = models.CharField(max_length=500, default='/tmp/ansible-webui')
path_play = models.CharField(max_length=500, default=None)
path_log = models.CharField(max_length=500, default=None)
path_template = models.CharField(max_length=500, **DEFAULT_NONE)
timezone = models.CharField(max_length=300, default='UTC') # UTC to keep model migrations static
run_timeout = models.PositiveIntegerField(default=CONFIG_DEFAULTS['run_timeout'])
session_timeout = models.PositiveIntegerField(default=CONFIG_DEFAULTS['session_timeout'])
Expand Down
4 changes: 1 addition & 3 deletions src/ansibleguy-webui/aw/templates/settings/alert.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ <h2>Group Alerts</h2>
</tr>
<tr>
<td colspan="100%">
{% include "../button/refresh.html" %}
<a href="/ui/settings/alerts/group/0">
<button class="btn btn-success aw-btn-action" title="Add Group Alert">
{% include "../button/icon/add.html" %}
Expand Down Expand Up @@ -86,7 +85,6 @@ <h2>Global Alerts</h2>
</tr>
<tr>
<td colspan="100%">
{% include "../button/refresh.html" %}
<a href="/ui/settings/alerts/global/0">
<button class="btn btn-success aw-btn-action" title="Add Global Alert">
{% include "../button/icon/add.html" %}
Expand Down Expand Up @@ -117,12 +115,12 @@ <h2>Alert Plugins</h2>
</tr>
<tr>
<td colspan="100%">
{% include "../button/refresh.html" %}
<a href="/ui/settings/alerts/plugin/0">
<button class="btn btn-success aw-btn-action" title="Add Alert-Plugin">
{% include "../button/icon/add.html" %}
</button>
</a>
{% include "../button/refresh.html" %}
</td>
</tr>
<tr id="aw-api-data-tmpl-row2" hidden="hidden">
Expand Down
19 changes: 15 additions & 4 deletions src/ansibleguy-webui/aw/views/forms/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,31 @@ class Meta:
labels = FORM_LABEL['system']['config']
help_texts = FORM_HELP['system']['config']

path_run = forms.CharField(max_length=500, initial=CONFIG_DEFAULTS['path_run'], required=True)
path_play = forms.CharField(max_length=500, initial=CONFIG_DEFAULTS['path_play'], required=True)
path_log = forms.CharField(max_length=500, initial=CONFIG_DEFAULTS['path_log'], required=True)
path_run = forms.CharField(
max_length=500, initial=CONFIG_DEFAULTS['path_run'], required=True,
label=Meta.labels['path_run'],
)
path_play = forms.CharField(
max_length=500, initial=CONFIG_DEFAULTS['path_play'], required=True,
label=Meta.labels['path_play'],
)
path_log = forms.CharField(
max_length=500, initial=CONFIG_DEFAULTS['path_log'], required=True,
label=Meta.labels['path_log'],
)
path_ansible_config = forms.CharField(
max_length=500, initial=CONFIG_DEFAULTS['path_ansible_config'], required=False,
label=Meta.labels['path_ansible_config'],
)
path_ssh_known_hosts = forms.CharField(
max_length=500, initial=CONFIG_DEFAULTS['path_ssh_known_hosts'], required=False,
label=Meta.labels['path_ssh_known_hosts'],
)
timezone = forms.ChoiceField(
required=False,
widget=forms.Select,
choices=[(tz, tz) for tz in sorted(all_timezones)],
label=FORM_LABEL['system']['config']['timezone'],
label=Meta.labels['timezone'],
)
debug = forms.ChoiceField(
initial=CONFIG_DEFAULTS['debug'] or deployment_dev(), choices=CHOICES_BOOL,
Expand Down

0 comments on commit 3da922a

Please sign in to comment.