]> git.proxmox.com Git - pve-manager.git/commitdiff
vzdump: support 'notification-mode' parameter
authorLukas Wagner <l.wagner@proxmox.com>
Tue, 21 Nov 2023 12:52:37 +0000 (13:52 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Tue, 21 Nov 2023 13:41:15 +0000 (14:41 +0100)
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

index b0574d412506d63746f7aad522689012501ee9fd..4185ed6253ca497c241a912a3f6f31416da6dbe9 100644 (file)
@@ -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 {