[pve-devel] [PATCH v2 pve-manager 31/42] api: apt: send notification via new notification module

Lukas Wagner l.wagner at proxmox.com
Wed May 24 15:56:38 CEST 2023


... instead of using sendmail directly

As of now, there is no way to configure a notification channel
for APT notifications. Thus, the implementation simply creates
an temporary channel with a sendmail endpoint sending mail
to root.

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

diff --git a/PVE/API2/APT.pm b/PVE/API2/APT.pm
index 6694dbeb..c1b7ece5 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::Notify;
 use PVE::RESTHandler;
 use PVE::RPCEnvironment;
 use PVE::API2Tools;
@@ -272,6 +273,12 @@ __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',
@@ -342,27 +349,71 @@ __PACKAGE__->register_method({
 		my $mailto = $rootcfg->{email};
 
 		if ($mailto) {
-		    my $hostname = `hostname -f` || PVE::INotify::nodename();
-		    chomp $hostname;
+		    # Add ephemeral sendmail endpoint/channel for backwards compatibility
+		    # TODO: Make notification channel configurable, then the
+		    # temporary endpoint/channel should not be necessary any more.
+		    my $notification_config = PVE::Notify::read_config();
 		    my $mailfrom = $dcconf->{email_from} || "root";
-		    my $subject = "New software packages available ($hostname)";
+		    $notification_config->add_sendmail_endpoint(
+			"anonymous-apt-sendmail",
+			[$mailto],
+			$mailfrom,
+			""
+		    );
+
+		    $notification_config->add_channel("mail", ["anonymous-apt-sendmail"]);
+
+		    my $updates_table = {
+			schema => {
+			    columns => [
+				{
+				    label => "Package",
+				    id    => "package",
+				},
+				{
+				    label => "Old Version",
+				    id    => "old-version",
+				},
+				{
+				    label => "New Version",
+				    id    => "new-version",
+				}
+			    ]
+			},
+			data => []
+		    };
 
-		    my $data = "The following updates are available:\n\n";
+		    my $hostname = `hostname -f` || PVE::INotify::nodename();
+		    chomp $hostname;
 
 		    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";
-			}
+
+			push @{$updates_table->{data}}, {
+			    "package"     => $p->{Package},
+			    "old-version" => $p->{OldVersion},
+			    "new-version" => $p->{Version}
+
+			};
 		    }
 
 		    return if !$count;
 
-		    PVE::Tools::sendmail($mailto, $subject, $data, undef, $mailfrom, '');
+		    my $properties = {
+			updates  => $updates_table,
+			hostname => $hostname,
+		    };
+
+		    PVE::Notify::send_notification(
+			"mail",
+			"info",
+			$updates_available_subject_template,
+			$updates_available_body_template,
+			$properties,
+			$notification_config
+		    );
 
 		    foreach my $pi (@$pkglist) {
 			$pi->{NotifyStatus} = $pi->{Version};
@@ -378,6 +429,8 @@ __PACKAGE__->register_method({
 
     }});
 
+
+
 __PACKAGE__->register_method({
     name => 'changelog',
     path => 'changelog',
-- 
2.30.2






More information about the pve-devel mailing list