[pve-devel] [PATCH perl-rs 4/5] pve-rs: common: send apt update notification via proxmox-notify

Wolfgang Bumiller w.bumiller at proxmox.com
Tue Jul 9 08:20:25 CEST 2024


From: Lukas Wagner <l.wagner at proxmox.com>

For PMG we for now only provide an empty stub and warn to syslog -
we need basic notification system integration there first.
On PMG, we still use a pure Perl implementation at the moment,
so this should not be an issue unless we change that.

Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
---
 common/src/apt/repositories.rs |  3 +-
 pmg-rs/Cargo.toml              |  1 +
 pmg-rs/src/lib.rs              | 10 ++++++
 pve-rs/src/lib.rs              | 64 ++++++++++++++++++++++++++++++++++
 4 files changed, 77 insertions(+), 1 deletion(-)

diff --git a/common/src/apt/repositories.rs b/common/src/apt/repositories.rs
index ccf4f33..8ad29cc 100644
--- a/common/src/apt/repositories.rs
+++ b/common/src/apt/repositories.rs
@@ -63,8 +63,9 @@ pub mod export {
         proxmox_apt::update_database(
             apt_state_file,
             &options,
-            |_updates: &[&APTUpdateInfo]| -> Result<(), Error> {
+            |updates: &[&APTUpdateInfo]| -> Result<(), Error> {
                 // fixme: howto send notifgications?
+                crate::send_updates_available(updates)?;
                 Ok(())
             },
         )
diff --git a/pmg-rs/Cargo.toml b/pmg-rs/Cargo.toml
index 2706fcb..8a6d113 100644
--- a/pmg-rs/Cargo.toml
+++ b/pmg-rs/Cargo.toml
@@ -19,6 +19,7 @@ env_logger = "0.10"
 hex = "0.4"
 http = "0.2.7"
 libc = "0.2"
+log = "0.4.17"
 nix = "0.26"
 openssl = "0.10.40"
 serde = "1.0"
diff --git a/pmg-rs/src/lib.rs b/pmg-rs/src/lib.rs
index 4a91632..8bac823 100644
--- a/pmg-rs/src/lib.rs
+++ b/pmg-rs/src/lib.rs
@@ -6,6 +6,10 @@ pub mod apt;
 pub mod csr;
 pub mod tfa;
 
+use anyhow::Error;
+
+use proxmox_apt_api_types::APTUpdateInfo;
+
 #[perlmod::package(name = "Proxmox::Lib::PMG", lib = "pmg_rs")]
 mod export {
     use crate::common;
@@ -23,3 +27,9 @@ mod export {
         perlmod::ffi::use_safe_putenv(true);
     }
 }
+
+pub fn send_updates_available(_updates: &[&APTUpdateInfo]) -> Result<(), Error> {
+    log::warn!("update notifications are not implemented for PMG yet");
+
+    Ok(())
+}
diff --git a/pve-rs/src/lib.rs b/pve-rs/src/lib.rs
index 73780f8..0fad753 100644
--- a/pve-rs/src/lib.rs
+++ b/pve-rs/src/lib.rs
@@ -8,6 +8,14 @@ pub mod openid;
 pub mod resource_scheduling;
 pub mod tfa;
 
+use std::collections::HashMap;
+
+use anyhow::Error;
+
+use proxmox_apt_api_types::APTUpdateInfo;
+use proxmox_notify::{Config, Notification, Severity};
+use serde_json::json;
+
 #[perlmod::package(name = "Proxmox::Lib::PVE", lib = "pve_rs")]
 mod export {
     use proxmox_notify::context::pve::PVE_CONTEXT;
@@ -20,3 +28,59 @@ mod export {
         proxmox_notify::context::set_context(&PVE_CONTEXT);
     }
 }
+
+fn send_notification(notification: &Notification) -> Result<(), Error> {
+    let config = proxmox_sys::fs::file_read_optional_string("/etc/pve/notifications.cfg")?
+        .unwrap_or_default();
+    let private_config =
+        proxmox_sys::fs::file_read_optional_string("/etc/pve/priv/notifications.cfg")?
+            .unwrap_or_default();
+
+    let config = Config::new(&config, &private_config)?;
+
+    proxmox_notify::api::common::send(&config, notification)?;
+
+    Ok(())
+}
+
+pub fn send_updates_available(updates: &[&APTUpdateInfo]) -> Result<(), Error> {
+    let hostname = proxmox_sys::nodename().to_string();
+
+    let metadata = HashMap::from([
+        ("hostname".into(), hostname.clone()),
+        ("type".into(), "package-updates".into()),
+    ]);
+
+    // The template uses the `table` handlebars helper, so
+    // we need to form the approriate data structure first.
+    let update_table = json!({
+        "schema": {
+            "columns": [
+                {
+                    "label": "Package",
+                    "id": "Package",
+                },
+                {
+                    "label": "Old Version",
+                    "id": "OldVersion",
+                },
+                {
+                    "label": "New Version",
+                    "id": "Version",
+                }
+            ],
+        },
+        "data": updates,
+    });
+
+    let template_data = json!({
+        "hostname": hostname,
+        "updates": update_table,
+    });
+
+    let notification =
+        Notification::from_template(Severity::Info, "package-updates", template_data, metadata);
+
+    send_notification(&notification)?;
+    Ok(())
+}
-- 
2.39.2





More information about the pve-devel mailing list