pve-rs: common: send apt update notification via proxmox-notify

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@proxmox.com>
This commit is contained in:
Lukas Wagner 2024-07-03 12:41:33 +02:00 committed by Wolfgang Bumiller
parent 89d9debadb
commit 6789b14986
4 changed files with 77 additions and 1 deletions

View File

@ -63,8 +63,9 @@ pub mod export {
proxmox_apt::update_database( proxmox_apt::update_database(
apt_state_file, apt_state_file,
&options, &options,
|_updates: &[&APTUpdateInfo]| -> Result<(), Error> { |updates: &[&APTUpdateInfo]| -> Result<(), Error> {
// fixme: howto send notifgications? // fixme: howto send notifgications?
crate::send_updates_available(updates)?;
Ok(()) Ok(())
}, },
) )

View File

@ -19,6 +19,7 @@ env_logger = "0.10"
hex = "0.4" hex = "0.4"
http = "0.2.7" http = "0.2.7"
libc = "0.2" libc = "0.2"
log = "0.4.17"
nix = "0.26" nix = "0.26"
openssl = "0.10.40" openssl = "0.10.40"
serde = "1.0" serde = "1.0"

View File

@ -6,6 +6,10 @@ pub mod apt;
pub mod csr; pub mod csr;
pub mod tfa; pub mod tfa;
use anyhow::Error;
use proxmox_apt_api_types::APTUpdateInfo;
#[perlmod::package(name = "Proxmox::Lib::PMG", lib = "pmg_rs")] #[perlmod::package(name = "Proxmox::Lib::PMG", lib = "pmg_rs")]
mod export { mod export {
use crate::common; use crate::common;
@ -23,3 +27,9 @@ mod export {
perlmod::ffi::use_safe_putenv(true); 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(())
}

View File

@ -8,6 +8,14 @@ pub mod openid;
pub mod resource_scheduling; pub mod resource_scheduling;
pub mod tfa; 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")] #[perlmod::package(name = "Proxmox::Lib::PVE", lib = "pve_rs")]
mod export { mod export {
use proxmox_notify::context::pve::PVE_CONTEXT; use proxmox_notify::context::pve::PVE_CONTEXT;
@ -20,3 +28,59 @@ mod export {
proxmox_notify::context::set_context(&PVE_CONTEXT); 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(())
}