all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Dietmar Maurer <dietmar@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox-backup 2/2] proxy: use new datastore notify settings
Date: Wed,  4 Nov 2020 11:32:15 +0100	[thread overview]
Message-ID: <20201104103215.14262-2-dietmar@proxmox.com> (raw)
In-Reply-To: <20201104103215.14262-1-dietmar@proxmox.com>

---
 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@pam", this returns the address from "root@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




      reply	other threads:[~2020-11-04 10:32 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-04 10:32 [pbs-devel] [PATCH proxmox-backup 1/2] config: allow to configure who receives job notify emails Dietmar Maurer
2020-11-04 10:32 ` Dietmar Maurer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201104103215.14262-2-dietmar@proxmox.com \
    --to=dietmar@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal