notifications: use named templates instead of in-code templates

This commit adapts notification sending for
    - package update
    - replication
    - backups

to use named templates (installed in /usr/share/pve-manager/templates)
instead of passing template strings defined in code to the
notification stack.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
Tested-by: Max Carrara <m.carrara@proxmox.com>
Reviewed-by: Max Carrara <m.carrara@proxmox.com>
This commit is contained in:
Lukas Wagner 2024-05-21 15:31:48 +02:00 committed by Wolfgang Bumiller
parent 79a02b0e48
commit fede7e87e4
17 changed files with 95 additions and 46 deletions

View File

@ -10,7 +10,7 @@ DSC=$(PACKAGE)_$(DEB_VERSION).dsc
DEB=$(PACKAGE)_$(DEB_VERSION)_$(DEB_HOST_ARCH).deb
DESTDIR=
SUBDIRS = aplinfo PVE bin www services configs network-hooks test
SUBDIRS = aplinfo PVE bin www services configs network-hooks test templates
all: $(SUBDIRS)
set -e && for i in $(SUBDIRS); do $(MAKE) -C $$i; done

View File

@ -238,12 +238,6 @@ __PACKAGE__->register_method({
return $pkglist;
}});
my $updates_available_subject_template = "New software packages available ({{hostname}})";
my $updates_available_body_template = <<EOT;
The following updates are available:
{{table updates}}
EOT
__PACKAGE__->register_method({
name => 'update_database',
path => 'update',
@ -358,8 +352,7 @@ __PACKAGE__->register_method({
};
PVE::Notify::info(
$updates_available_subject_template,
$updates_available_body_template,
"package-updates",
$template_data,
$metadata_fields,
);

View File

@ -92,23 +92,6 @@ my sub _should_mail_at_failcount {
return $i * 48 == $fail_count;
};
my $replication_error_subject_template = "Replication Job: '{{job-id}}' failed";
my $replication_error_body_template = <<EOT;
{{#verbatim}}
Replication job '{{job-id}}' with target '{{job-target}}' and schedule '{{job-schedule}}' failed!
Last successful sync: {{timestamp last-sync}}
Next sync try: {{timestamp next-sync}}
Failure count: {{failure-count}}
{{#if (eq failure-count 3)}}
Note: The system will now reduce the frequency of error reports, as the job
appears to be stuck.
{{/if}}
Error:
{{verbatim-monospaced error}}
{{/verbatim}}
EOT
my sub _handle_job_err {
my ($job, $err, $mail) = @_;
@ -146,8 +129,7 @@ my sub _handle_job_err {
eval {
PVE::Notify::error(
$replication_error_subject_template,
$replication_error_body_template,
"replication",
$template_data,
$metadata_fields
);

View File

@ -477,20 +477,6 @@ my sub get_hostname {
return $hostname;
}
my $subject_template = "vzdump backup status ({{hostname}}): {{status-text}}";
my $body_template = <<EOT;
{{error-message}}
{{heading-1 "Details"}}
{{table guest-table}}
{{#verbatim}}
Total running time: {{duration total-time}}
Total size: {{human-bytes total-size}}
{{/verbatim}}
{{heading-1 "Logs"}}
{{verbatim-monospaced logs}}
EOT
use constant MAX_LOG_SIZE => 1024*1024;
sub send_notification {
@ -588,8 +574,7 @@ sub send_notification {
PVE::Notify::notify(
$severity,
$subject_template,
$body_template,
"vzdump",
$notification_props,
$fields,
$notification_config
@ -600,8 +585,7 @@ sub send_notification {
# no email addresses were configured.
PVE::Notify::notify(
$severity,
$subject_template,
$body_template,
"vzdump",
$notification_props,
$fields,
);

24
templates/Makefile Normal file
View File

@ -0,0 +1,24 @@
NOTIFICATION_TEMPLATES= \
default/test-subject.txt.hbs \
default/test-body.txt.hbs \
default/test-body.html.hbs \
default/vzdump-subject.txt.hbs \
default/vzdump-body.txt.hbs \
default/vzdump-body.html.hbs \
default/replication-subject.txt.hbs \
default/replication-body.txt.hbs \
default/replication-body.html.hbs \
default/package-updates-subject.txt.hbs \
default/package-updates-body.txt.hbs \
default/package-updates-body.html.hbs \
all:
.PHONY: install
install:
install -dm 0755 $(DESTDIR)/usr/share/pve-manager/templates/default
$(foreach i,$(NOTIFICATION_TEMPLATES), \
install -m644 $(i) $(DESTDIR)/usr/share/pve-manager/templates/$(i) ;)
clean:

View File

@ -0,0 +1,6 @@
<html>
<body>
The following updates are available:
{{table updates}}
</body>
</html>

View File

@ -0,0 +1,3 @@
The following updates are available:
{{table updates}}

View File

@ -0,0 +1 @@
New software packages available ({{hostname}})

View File

@ -0,0 +1,18 @@
<html>
<body>
Replication job '{{job-id}}' with target '{{job-target}}' and schedule '{{job-schedule}}' failed!<br/><br/>
Last successful sync: {{timestamp last-sync}}<br/>
Next sync try: {{timestamp next-sync}}<br/>
Failure count: {{failure-count}}<br/>
{{#if (eq failure-count 3)}}
Note: The system will now reduce the frequency of error reports, as the job appears to be stuck.
{{/if}}
<br/>
Error:<br/>
<pre>
{{error}}
</pre>
</body>
</html>

View File

@ -0,0 +1,12 @@
Replication job '{{job-id}}' with target '{{job-target}}' and schedule '{{job-schedule}}' failed!
Last successful sync: {{timestamp last-sync}}
Next sync try: {{timestamp next-sync}}
Failure count: {{failure-count}}
{{#if (eq failure-count 3)}}
Note: The system will now reduce the frequency of error reports, as the job
appears to be stuck.
{{/if}}
Error:
{{ error }}

View File

@ -0,0 +1 @@
Replication Job: '{{job-id}}' failed

View File

@ -0,0 +1 @@
This is a test of the notification target '{{ target }}'.

View File

@ -0,0 +1 @@
This is a test of the notification target '{{ target }}'.

View File

@ -0,0 +1 @@
Test notification

View File

@ -0,0 +1,11 @@
<html>
<body>
{{error-message}}
<h1 style="font-size: 1.2em">Details</h1>
{{table guest-table}}
Total running time: {{duration total-time}}<br/>
Total size: {{human-bytes total-size}}<br/>
<h1 style="font-size: 1.2em">Logs</h1>
<pre>{{logs}}</pre>
</body>
</html>

View File

@ -0,0 +1,10 @@
{{error-message}}
Details
=======
{{table guest-table}}
Total running time: {{duration total-time}}
Total size: {{human-bytes total-size}}
Logs
====
{{logs}}

View File

@ -0,0 +1 @@
vzdump backup status ({{hostname}}): {{status-text}}