From: Lukas Wagner <l.wagner@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH v3 pve-manager 2/5] vzdump: support 'notification-mode' parameter
Date: Tue, 21 Nov 2023 13:23:25 +0100 [thread overview]
Message-ID: <20231121122328.134930-3-l.wagner@proxmox.com> (raw)
In-Reply-To: <20231121122328.134930-1-l.wagner@proxmox.com>
This parameter lets us choose between the 'legacy' notification
system (sendmail to some email addresses) and the 'new' notification
system (pub-sub based system with targets and matchers).
'auto' (default) will use the 'legacy' system if a mail address is
provided and the 'new' system if not.
This is allows users to opt-in/opt-out from the new notification
system, which might be a bit chatty by default.
Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
PVE/VZDump.pm | 95 +++++++++++++++++++++++++++++++--------------------
1 file changed, 58 insertions(+), 37 deletions(-)
diff --git a/PVE/VZDump.pm b/PVE/VZDump.pm
index b0574d41..4185ed62 100644
--- a/PVE/VZDump.pm
+++ b/PVE/VZDump.pm
@@ -452,12 +452,8 @@ sub send_notification {
my $opts = $self->{opts};
my $mailto = $opts->{mailto};
my $cmdline = $self->{cmdline};
- # Old-style notification policy. This parameter will influce
- # if an ad-hoc notification target/matcher will be created.
- my $policy = $opts->{"notification-policy"} //
- $opts->{mailnotification} //
- 'always';
-
+ my $policy = $opts->{mailnotification} // 'always';
+ my $mode = $opts->{"notification-mode"} // 'auto';
sanitize_task_list($tasklist);
my $error_count = count_failed_tasks($tasklist);
@@ -499,44 +495,69 @@ sub send_notification {
};
my $fields = {
+ # TODO: There is no straight-forward way yet to get the
+ # backup job id here... (I think pvescheduler would need
+ # to pass that to the vzdump call?)
type => "vzdump",
hostname => $hostname,
};
- my $notification_config = PVE::Notify::read_config();
-
- my $legacy_sendmail = $policy eq "always" || ($policy eq "failure" && $failed);
-
- if ($mailto && scalar(@$mailto) && $legacy_sendmail) {
- # <, >, @ are not allowed in endpoint names, but that is only
- # verified once the config is serialized. That means that
- # we can rely on that fact that no other endpoint with this name exists.
- my $endpoint_name = "<mail-to-" . join(",", @$mailto) . ">";
- $notification_config->add_sendmail_endpoint(
- $endpoint_name,
- $mailto,
- undef,
- undef,
- "vzdump backup tool");
-
- my $endpoints = [$endpoint_name];
+ my $severity = $failed ? "error" : "info";
+ my $email_configured = $mailto && scalar(@$mailto);
+
+ if (($mode eq 'auto' && $email_configured) || $mode eq 'legacy-sendmail') {
+ if ($email_configured && ($policy eq "always" || ($policy eq "failure" && $failed))) {
+ # Start out with an empty config. Might still contain
+ # built-ins, so we need to disable/remove them.
+ my $notification_config = Proxmox::RS::Notify->parse_config('', '');
+
+ # Remove built-in matchers, since we only want to send an
+ # email to the specified recipients and nobody else.
+ for my $matcher (@{$notification_config->get_matchers()}) {
+ $notification_config->delete_matcher($matcher->{name});
+ }
- $notification_config->add_matcher(
- "<matcher-$endpoint_name>",
- $endpoints,
+ # <, >, @ are not allowed in endpoint names, but that is only
+ # verified once the config is serialized. That means that
+ # we can rely on that fact that no other endpoint with this name exists.
+ my $endpoint_name = "<" . join(",", @$mailto) . ">";
+ $notification_config->add_sendmail_endpoint(
+ $endpoint_name,
+ $mailto,
+ undef,
+ undef,
+ "vzdump backup tool"
+ );
+
+ my $endpoints = [$endpoint_name];
+
+ # Add a matcher that matches all notifications, set our
+ # newly created target as a target.
+ $notification_config->add_matcher(
+ "<matcher-$endpoint_name>",
+ $endpoints,
+ );
+
+ PVE::Notify::notify(
+ $severity,
+ $subject_template,
+ $body_template,
+ $notification_props,
+ $fields,
+ $notification_config
+ );
+ }
+ } else {
+ # We use the 'new' system, or we are set to 'auto' and
+ # no email addresses were configured.
+ PVE::Notify::notify(
+ $severity,
+ $subject_template,
+ $body_template,
+ $notification_props,
+ $fields,
);
}
-
- my $severity = $failed ? "error" : "info";
-
- PVE::Notify::notify(
- $severity,
- $subject_template,
- $body_template,
- $notification_props,
- $fields,
- $notification_config
- );
};
sub new {
--
2.39.2
next prev parent reply other threads:[~2023-11-21 12:23 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-21 12:23 [pve-devel] [PATCH v3 guest-common/manager 0/5] vzdump: add " Lukas Wagner
2023-11-21 12:23 ` [pve-devel] [PATCH v3 pve-guest-common 1/5] vzdump: config: add 'notification-mode' param for backup jobs Lukas Wagner
2023-11-21 12:23 ` Lukas Wagner [this message]
2023-11-21 12:23 ` [pve-devel] [PATCH v3 pve-manager 3/5] ui: backup jobs: add 'notification-mode' selector " Lukas Wagner
2023-11-21 12:51 ` Philipp Hufnagl
2023-11-21 12:23 ` [pve-devel] [PATCH v3 pve-manager 4/5] ui: backup: add 'notification-mode' param for one-shot " Lukas Wagner
[not found] ` <6e27a91f-2a4b-4072-87ee-f097dd175a9e@proxmox.com>
2023-11-21 12:42 ` Lukas Wagner
2023-11-21 12:23 ` [pve-devel] [PATCH v3 pve-manager 5/5] ui: backup job: change field text for 'mailnotification' field Lukas Wagner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231121122328.134930-3-l.wagner@proxmox.com \
--to=l.wagner@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal