[pbs-devel] [PATCH proxmox-backup 2/2] proxy: use new datastore notify settings

Dietmar Maurer dietmar at proxmox.com
Wed Nov 4 11:32:15 CET 2020


---
 src/api2/pull.rs                  |  4 +--
 src/server/email_notifications.rs | 49 ++++++++++++++++++++++++++++++-
 src/server/gc_job.rs              |  6 ++--
 src/server/verify_job.rs          |  4 +--
 4 files changed, 55 insertions(+), 8 deletions(-)

diff --git a/src/api2/pull.rs b/src/api2/pull.rs
index 8491ab00..d9e9d31d 100644
--- a/src/api2/pull.rs
+++ b/src/api2/pull.rs
@@ -75,7 +75,7 @@ pub fn do_sync_job(
     let job_id = job.jobname().to_string();
     let worker_type = job.jobtype().to_string();
 
-    let email = crate::server::lookup_user_email(auth_id.user());
+    let (email, notify) = crate::server::lookup_datastore_notify_settings(&sync_job.store);
 
     let upid_str = WorkerTask::spawn(
         &worker_type,
@@ -126,7 +126,7 @@ pub fn do_sync_job(
             }
 
             if let Some(email) = email {
-                if let Err(err) = crate::server::send_sync_status(&email, &sync_job2, &result) {
+                if let Err(err) = crate::server::send_sync_status(&email, notify, &sync_job2, &result) {
                     eprintln!("send sync notification failed: {}", err);
                 }
             }
diff --git a/src/server/email_notifications.rs b/src/server/email_notifications.rs
index 7c5096ac..a3bca801 100644
--- a/src/server/email_notifications.rs
+++ b/src/server/email_notifications.rs
@@ -6,12 +6,14 @@ use handlebars::{Handlebars, Helper, Context, RenderError, RenderContext, Output
 use proxmox::tools::email::sendmail;
 
 use crate::{
+    config::datastore::DataStoreConfig,
     config::verify::VerificationJobConfig,
     config::sync::SyncJobConfig,
     api2::types::{
         APTUpdateInfo,
         GarbageCollectionStatus,
         Userid,
+        Notify,
     },
     tools::format::HumanByte,
 };
@@ -188,11 +190,16 @@ fn send_job_status_mail(
 
 pub fn send_gc_status(
     email: &str,
+    notify: Notify,
     datastore: &str,
     status: &GarbageCollectionStatus,
     result: &Result<(), Error>,
 ) -> Result<(), Error> {
 
+    if notify == Notify::Never || (result.is_ok() && notify == Notify::Error) {
+        return Ok(());
+    }
+
     let (fqdn, port) = get_server_url();
     let mut data = json!({
         "datastore": datastore,
@@ -237,10 +244,15 @@ pub fn send_gc_status(
 
 pub fn send_verify_status(
     email: &str,
+    notify: Notify,
     job: VerificationJobConfig,
     result: &Result<Vec<String>, Error>,
 ) -> Result<(), Error> {
 
+    if notify == Notify::Never || (result.is_ok() && notify == Notify::Error) {
+        return Ok(());
+    }
+
     let (fqdn, port) = get_server_url();
     let mut data = json!({
         "job": job,
@@ -280,10 +292,15 @@ pub fn send_verify_status(
 
 pub fn send_sync_status(
     email: &str,
+    notify: Notify,
     job: &SyncJobConfig,
     result: &Result<(), Error>,
 ) -> Result<(), Error> {
 
+    if notify == Notify::Never || (result.is_ok() && notify == Notify::Error) {
+        return Ok(());
+    }
+
     let (fqdn, port) = get_server_url();
     let mut data = json!({
         "job": job,
@@ -362,7 +379,7 @@ pub fn send_updates_available(
 /// Lookup users email address
 ///
 /// For "backup at pam", this returns the address from "root at pam".
-pub fn lookup_user_email(userid: &Userid) -> Option<String> {
+fn lookup_user_email(userid: &Userid) -> Option<String> {
 
     use crate::config::user::{self, User};
 
@@ -379,6 +396,36 @@ pub fn lookup_user_email(userid: &Userid) -> Option<String> {
     None
 }
 
+/// Lookup Datastore notify settings
+pub fn lookup_datastore_notify_settings(
+    store: &str,
+) -> (Option<String>, Notify) {
+
+    let mut notify = Notify::Always;
+    let mut email = None;
+
+    let (config, _digest) = match crate::config::datastore::config() {
+        Ok(result) => result,
+        Err(_) => return (email, notify),
+    };
+
+    let config: DataStoreConfig = match config.lookup("datastore", store) {
+        Ok(result) => result,
+        Err(_) => return (email, notify),
+    };
+
+    email = match config.notify_user {
+        Some(ref userid) => lookup_user_email(userid),
+        None => lookup_user_email(Userid::backup_userid()),
+    };
+
+    if let Some(value) = config.notify {
+        notify = value;
+    }
+
+    (email, notify)
+}
+
 // Handlerbar helper functions
 
 fn handlebars_humam_bytes_helper(
diff --git a/src/server/gc_job.rs b/src/server/gc_job.rs
index 0128a33e..7a7e6de2 100644
--- a/src/server/gc_job.rs
+++ b/src/server/gc_job.rs
@@ -17,10 +17,10 @@ pub fn do_garbage_collection_job(
     to_stdout: bool,
 ) -> Result<String, Error> {
 
-    let email = crate::server::lookup_user_email(auth_id.user());
-
     let store = datastore.name().to_string();
 
+    let (email, notify) = crate::server::lookup_datastore_notify_settings(&store);
+
     let worker_type = job.jobtype().to_string();
     let upid_str = WorkerTask::new_thread(
         &worker_type,
@@ -50,7 +50,7 @@ pub fn do_garbage_collection_job(
 
             if let Some(email) = email {
                 let gc_status = datastore.last_gc_status();
-                if let Err(err) = crate::server::send_gc_status(&email, &store, &gc_status, &result) {
+                if let Err(err) = crate::server::send_gc_status(&email, notify, &store, &gc_status, &result) {
                     eprintln!("send gc notification failed: {}", err);
                 }
             }
diff --git a/src/server/verify_job.rs b/src/server/verify_job.rs
index c98cd5b2..9cea3fee 100644
--- a/src/server/verify_job.rs
+++ b/src/server/verify_job.rs
@@ -48,7 +48,7 @@ pub fn do_verification_job(
         }
     };
 
-    let email = crate::server::lookup_user_email(auth_id.user());
+    let (email, notify) = crate::server::lookup_datastore_notify_settings(&verification_job.store);
 
     let job_id = job.jobname().to_string();
     let worker_type = job.jobtype().to_string();
@@ -84,7 +84,7 @@ pub fn do_verification_job(
             }
 
             if let Some(email) = email {
-                if let Err(err) = crate::server::send_verify_status(&email, verification_job, &result) {
+                if let Err(err) = crate::server::send_verify_status(&email, notify, verification_job, &result) {
                     eprintln!("send verify notification failed: {}", err);
                 }
             }
-- 
2.20.1





More information about the pbs-devel mailing list