[pve-devel] [PATCH pve-manager 15/18] api: apt: send notification via new notification module

Lukas Wagner l.wagner at proxmox.com
Mon Mar 27 17:18:54 CEST 2023


... instead of using sendmail directly

As with the VZDump patch, the changes were implemented in such a way
that existing mail notifications continue to work as before. Here this
means that if root at pam has an email address configured, we add an
ephemeral sendmail endpoint for that email address.

Potential side effect: If there is another sendmail endpoint configured
in notifications.cfg which also sends to root at pam, root may receive
two emails.

Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
---
 PVE/API2/APT.pm | 55 ++++++++++++++++++++++++++++++-------------------
 1 file changed, 34 insertions(+), 21 deletions(-)

diff --git a/PVE/API2/APT.pm b/PVE/API2/APT.pm
index 6694dbeb..543037de 100644
--- a/PVE/API2/APT.pm
+++ b/PVE/API2/APT.pm
@@ -19,6 +19,7 @@ use PVE::DataCenterConfig;
 use PVE::SafeSyslog;
 use PVE::INotify;
 use PVE::Exception;
+use PVE::Notification;
 use PVE::RESTHandler;
 use PVE::RPCEnvironment;
 use PVE::API2Tools;
@@ -341,34 +342,46 @@ __PACKAGE__->register_method({
 		my $rootcfg = $usercfg->{users}->{'root at pam'} || {};
 		my $mailto = $rootcfg->{email};
 
+		my $config = PVE::Notification::config();
+
 		if ($mailto) {
-		    my $hostname = `hostname -f` || PVE::INotify::nodename();
-		    chomp $hostname;
 		    my $mailfrom = $dcconf->{email_from} || "root";
-		    my $subject = "New software packages available ($hostname)";
-
-		    my $data = "The following updates are available:\n\n";
-
-		    my $count = 0;
-		    foreach my $p (sort {$a->{Package} cmp $b->{Package} } @$pkglist) {
-			next if $p->{NotifyStatus} && $p->{NotifyStatus} eq $p->{Version};
-			$count++;
-			if ($p->{OldVersion}) {
-			    $data .= "$p->{Package}: $p->{OldVersion} ==> $p->{Version}\n";
-			} else {
-			    $data .= "$p->{Package}: $p->{Version} (new)\n";
-			}
-		    }
+		    # add ephemeral sendmail endpoint, in order to not break existing
+		    # mail notifications. The changed config is not persisted.
+		    # TODO: Remove in PVE x.y ?
+		    $config->add_sendmail_endpoint(
+			"legacy-apt-sendmail",
+			[$mailto],
+			$mailfrom,
+			"");
+
+		}
 
-		    return if !$count;
+		my $hostname = `hostname -f` || PVE::INotify::nodename();
+		chomp $hostname;
+		my $subject = "New software packages available ($hostname)";
 
-		    PVE::Tools::sendmail($mailto, $subject, $data, undef, $mailfrom, '');
+		my $data = "The following updates are available:\n\n";
 
-		    foreach my $pi (@$pkglist) {
-			$pi->{NotifyStatus} = $pi->{Version};
+		my $count = 0;
+		foreach my $p (sort {$a->{Package} cmp $b->{Package} } @$pkglist) {
+		    next if $p->{NotifyStatus} && $p->{NotifyStatus} eq $p->{Version};
+		    $count++;
+		    if ($p->{OldVersion}) {
+			$data .= "$p->{Package}: $p->{OldVersion} ==> $p->{Version}\n";
+		    } else {
+			$data .= "$p->{Package}: $p->{Version} (new)\n";
 		    }
-		    PVE::Tools::file_set_contents($pve_pkgstatus_fn, encode_json($pkglist));
 		}
+
+		return if !$count;
+
+		PVE::Notification::info($subject, $data, $config);
+
+		foreach my $pi (@$pkglist) {
+		    $pi->{NotifyStatus} = $pi->{Version};
+		}
+		PVE::Tools::file_set_contents($pve_pkgstatus_fn, encode_json($pkglist));
 	    }
 
 	    return;
-- 
2.30.2






More information about the pve-devel mailing list