From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id BF96865926 for ; Wed, 4 Nov 2020 11:32:45 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id B53A0C3EB for ; Wed, 4 Nov 2020 11:32:45 +0100 (CET) Received: from elsa.proxmox.com (212-186-127-178.static.upcbusiness.at [212.186.127.178]) by firstgate.proxmox.com (Proxmox) with ESMTP id 12BFFC3E3 for ; Wed, 4 Nov 2020 11:32:45 +0100 (CET) Received: by elsa.proxmox.com (Postfix, from userid 0) id E5974AE985C; Wed, 4 Nov 2020 11:32:44 +0100 (CET) From: Dietmar Maurer To: pbs-devel@lists.proxmox.com Date: Wed, 4 Nov 2020 11:32:15 +0100 Message-Id: <20201104103215.14262-2-dietmar@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20201104103215.14262-1-dietmar@proxmox.com> References: <20201104103215.14262-1-dietmar@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -1.079 Adjusted score from AWL reputation of From: address 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 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [pull.rs] Subject: [pbs-devel] [PATCH proxmox-backup 2/2] proxy: use new datastore notify settings X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 04 Nov 2020 10:32:45 -0000 --- 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, 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 { +fn lookup_user_email(userid: &Userid) -> Option { use crate::config::user::{self, User}; @@ -379,6 +396,36 @@ pub fn lookup_user_email(userid: &Userid) -> Option { None } +/// Lookup Datastore notify settings +pub fn lookup_datastore_notify_settings( + store: &str, +) -> (Option, 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 { - 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