From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pbs-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9])
	by lore.proxmox.com (Postfix) with ESMTPS id 6D13C1FF56B
	for <inbox@lore.proxmox.com>; Mon, 22 Apr 2024 09:51:10 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id BA3CC9A41;
	Mon, 22 Apr 2024 09:51:08 +0200 (CEST)
From: Lukas Wagner <l.wagner@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Mon, 22 Apr 2024 09:50:12 +0200
Message-Id: <20240422075030.73895-25-l.wagner@proxmox.com>
X-Mailer: git-send-email 2.39.2
In-Reply-To: <20240422075030.73895-1-l.wagner@proxmox.com>
References: <20240422075030.73895-1-l.wagner@proxmox.com>
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.001 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pbs-devel] [PATCH proxmox-backup v3 24/42] server: notifications:
 send update notifications via notification system
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox Backup Server development discussion
 <pbs-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pbs-devel-bounces@lists.proxmox.com
Sender: "pbs-devel" <pbs-devel-bounces@lists.proxmox.com>

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
Tested-by: Gabriel Goller <g.goller@proxmox.com>
Reviewed-by: Gabriel Goller <g.goller@proxmox.com>
---
 debian/proxmox-backup-server.install          |  2 +
 src/server/notifications.rs                   | 47 +++++++------------
 templates/Makefile                            |  2 +
 .../default/package-updates-body.txt.hbs      |  8 ++++
 .../default/package-updates-subject.txt.hbs   |  1 +
 5 files changed, 29 insertions(+), 31 deletions(-)
 create mode 100644 templates/default/package-updates-body.txt.hbs
 create mode 100644 templates/default/package-updates-subject.txt.hbs

diff --git a/debian/proxmox-backup-server.install b/debian/proxmox-backup-server.install
index 86e2b543..52a01ca5 100644
--- a/debian/proxmox-backup-server.install
+++ b/debian/proxmox-backup-server.install
@@ -45,6 +45,8 @@ usr/share/proxmox-backup/templates/default/gc-err-body.txt.hbs
 usr/share/proxmox-backup/templates/default/gc-ok-body.txt.hbs
 usr/share/proxmox-backup/templates/default/gc-err-subject.txt.hbs
 usr/share/proxmox-backup/templates/default/gc-ok-subject.txt.hbs
+usr/share/proxmox-backup/templates/default/package-updates-body.txt.hbs
+usr/share/proxmox-backup/templates/default/package-updates-subject.txt.hbs
 usr/share/proxmox-backup/templates/default/prune-err-body.txt.hbs
 usr/share/proxmox-backup/templates/default/prune-ok-body.txt.hbs
 usr/share/proxmox-backup/templates/default/prune-err-subject.txt.hbs
diff --git a/src/server/notifications.rs b/src/server/notifications.rs
index 5f854e4c..8459a8ce 100644
--- a/src/server/notifications.rs
+++ b/src/server/notifications.rs
@@ -23,18 +23,6 @@ use proxmox_notify::{Endpoint, Notification, Severity};
 
 const SPOOL_DIR: &str = concatcp!(pbs_buildcfg::PROXMOX_BACKUP_STATE_DIR, "/notifications");
 
-const PACKAGE_UPDATES_TEMPLATE: &str = r###"
-Proxmox Backup Server has the following updates available:
-{{#each updates }}
-  {{Package}}: {{OldVersion}} -> {{Version~}}
-{{/each }}
-
-To upgrade visit the web interface:
-
-<https://{{fqdn}}:{{port}}/#pbsServerAdministration:updates>
-
-"###;
-
 const TAPE_BACKUP_OK_TEMPLATE: &str = r###"
 
 {{#if id ~}}
@@ -122,8 +110,6 @@ lazy_static::lazy_static! {
             hb.register_template_string("tape_backup_ok_template", TAPE_BACKUP_OK_TEMPLATE)?;
             hb.register_template_string("tape_backup_err_template", TAPE_BACKUP_ERR_TEMPLATE)?;
 
-            hb.register_template_string("package_update_template", PACKAGE_UPDATES_TEMPLATE)?;
-
             hb.register_template_string("certificate_renewal_err_template", ACME_CERTIFICATE_ERR_RENEWAL)?;
 
             Ok(())
@@ -583,24 +569,25 @@ fn get_server_url() -> (String, usize) {
 }
 
 pub fn send_updates_available(updates: &[&APTUpdateInfo]) -> Result<(), Error> {
-    // update mails always go to the root@pam configured email..
-    if let Some(email) = lookup_user_email(Userid::root_userid()) {
-        let nodename = proxmox_sys::nodename();
-        let subject = format!("New software packages available ({nodename})");
+    let (fqdn, port) = get_server_url();
+    let hostname = proxmox_sys::nodename().to_string();
 
-        let (fqdn, port) = get_server_url();
+    let data = json!({
+        "fqdn": fqdn,
+        "hostname": &hostname,
+        "port": port,
+        "updates": updates,
+    });
 
-        let text = HANDLEBARS.render(
-            "package_update_template",
-            &json!({
-                "fqdn": fqdn,
-                "port": port,
-                "updates": updates,
-            }),
-        )?;
+    let metadata = HashMap::from([
+        ("hostname".into(), hostname),
+        ("type".into(), "package-updates".into()),
+    ]);
 
-        send_job_status_mail(&email, &subject, &text)?;
-    }
+    let notification =
+        Notification::from_template(Severity::Info, "package-updates", data, metadata);
+
+    send_notification(notification)?;
     Ok(())
 }
 
@@ -687,7 +674,5 @@ fn test_template_register() {
     assert!(HANDLEBARS.has_template("tape_backup_ok_template"));
     assert!(HANDLEBARS.has_template("tape_backup_err_template"));
 
-    assert!(HANDLEBARS.has_template("package_update_template"));
-
     assert!(HANDLEBARS.has_template("certificate_renewal_err_template"));
 }
diff --git a/templates/Makefile b/templates/Makefile
index 41cdd2da..56965400 100644
--- a/templates/Makefile
+++ b/templates/Makefile
@@ -5,6 +5,8 @@ NOTIFICATION_TEMPLATES=					\
 	default/gc-ok-body.txt.hbs			\
 	default/gc-err-subject.txt.hbs		\
 	default/gc-ok-subject.txt.hbs		\
+	default/package-updates-body.txt.hbs			\
+	default/package-updates-subject.txt.hbs		\
 	default/prune-err-body.txt.hbs			\
 	default/prune-ok-body.txt.hbs			\
 	default/prune-err-subject.txt.hbs		\
diff --git a/templates/default/package-updates-body.txt.hbs b/templates/default/package-updates-body.txt.hbs
new file mode 100644
index 00000000..62f9c7c4
--- /dev/null
+++ b/templates/default/package-updates-body.txt.hbs
@@ -0,0 +1,8 @@
+Proxmox Backup Server has the following updates available:
+{{#each updates }}
+    {{Package}}: {{OldVersion}} -> {{Version~}}
+{{/each }}
+
+To upgrade visit the web interface:
+
+<https://{{fqdn}}:{{port}}/#pbsServerAdministration:updates>
diff --git a/templates/default/package-updates-subject.txt.hbs b/templates/default/package-updates-subject.txt.hbs
new file mode 100644
index 00000000..c8a775d5
--- /dev/null
+++ b/templates/default/package-updates-subject.txt.hbs
@@ -0,0 +1 @@
+New software packages available ({{ hostname }})
-- 
2.39.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel