* [pbs-devel] [PATCH proxmox-backup 1/2] config: allow to configure who receives job notify emails
@ 2020-11-04 10:32 Dietmar Maurer
2020-11-04 10:32 ` [pbs-devel] [PATCH proxmox-backup 2/2] proxy: use new datastore notify settings Dietmar Maurer
0 siblings, 1 reply; 2+ messages in thread
From: Dietmar Maurer @ 2020-11-04 10:32 UTC (permalink / raw)
To: pbs-devel
---
src/api2/config/datastore.rs | 27 +++++++++++++++++++++++++++
src/api2/types/mod.rs | 13 +++++++++++++
src/config/datastore.rs | 14 ++++++++++++++
3 files changed, 54 insertions(+)
diff --git a/src/api2/config/datastore.rs b/src/api2/config/datastore.rs
index 1da82593..8848c1e3 100644
--- a/src/api2/config/datastore.rs
+++ b/src/api2/config/datastore.rs
@@ -68,6 +68,14 @@ pub fn list_datastores(
optional: true,
schema: SINGLE_LINE_COMMENT_SCHEMA,
},
+ "notify-user": {
+ optional: true,
+ type: Userid,
+ },
+ "notify": {
+ optional: true,
+ type: Notify,
+ },
"gc-schedule": {
optional: true,
schema: GC_SCHEDULE_SCHEMA,
@@ -187,6 +195,10 @@ pub enum DeletableProperty {
keep_monthly,
/// Delete the keep-yearly property
keep_yearly,
+ /// Delete the notify-user property
+ notify_user,
+ /// Delete the notify property
+ notify,
}
#[api(
@@ -200,6 +212,14 @@ pub enum DeletableProperty {
optional: true,
schema: SINGLE_LINE_COMMENT_SCHEMA,
},
+ "notify-user": {
+ optional: true,
+ type: Userid,
+ },
+ "notify": {
+ optional: true,
+ type: Notify,
+ },
"gc-schedule": {
optional: true,
schema: GC_SCHEDULE_SCHEMA,
@@ -262,6 +282,8 @@ pub fn update_datastore(
keep_weekly: Option<u64>,
keep_monthly: Option<u64>,
keep_yearly: Option<u64>,
+ notify: Option<Notify>,
+ notify_user: Option<Userid>,
delete: Option<Vec<DeletableProperty>>,
digest: Option<String>,
) -> Result<(), Error> {
@@ -290,6 +312,8 @@ pub fn update_datastore(
DeletableProperty::keep_weekly => { data.keep_weekly = None; },
DeletableProperty::keep_monthly => { data.keep_monthly = None; },
DeletableProperty::keep_yearly => { data.keep_yearly = None; },
+ DeletableProperty::notify => { data.notify = None; },
+ DeletableProperty::notify_user => { data.notify_user = None; },
}
}
}
@@ -322,6 +346,9 @@ pub fn update_datastore(
if keep_monthly.is_some() { data.keep_monthly = keep_monthly; }
if keep_yearly.is_some() { data.keep_yearly = keep_yearly; }
+ if notify.is_some() { data.notify = notify; }
+ if notify_user.is_some() { data.notify_user = notify_user; }
+
config.set_data(&name, "datastore", &data)?;
datastore::save_config(&config)?;
diff --git a/src/api2/types/mod.rs b/src/api2/types/mod.rs
index 16c299bc..7ee89f57 100644
--- a/src/api2/types/mod.rs
+++ b/src/api2/types/mod.rs
@@ -1154,3 +1154,16 @@ pub struct APTUpdateInfo {
/// URL under which the package's changelog can be retrieved
pub change_log_url: String,
}
+
+#[api()]
+#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
+#[serde(rename_all = "lowercase")]
+/// When do we send notifications
+pub enum Notify {
+ /// Never send notification
+ Never,
+ /// Send notifications for failed and sucessful jobs
+ Always,
+ /// Send notifications for failed jobs only
+ Error,
+}
diff --git a/src/config/datastore.rs b/src/config/datastore.rs
index ffc81276..7e38a783 100644
--- a/src/config/datastore.rs
+++ b/src/config/datastore.rs
@@ -32,6 +32,14 @@ pub const DIR_NAME_SCHEMA: Schema = StringSchema::new("Directory name").schema()
path: {
schema: DIR_NAME_SCHEMA,
},
+ "notify-user": {
+ optional: true,
+ type: Userid,
+ },
+ "notify": {
+ optional: true,
+ type: Notify,
+ },
comment: {
optional: true,
schema: SINGLE_LINE_COMMENT_SCHEMA,
@@ -101,6 +109,12 @@ pub struct DataStoreConfig {
/// If enabled, all backups will be verified right after completion.
#[serde(skip_serializing_if="Option::is_none")]
pub verify_new: Option<bool>,
+ /// Send job email notification to this user
+ #[serde(skip_serializing_if="Option::is_none")]
+ pub notify_user: Option<Userid>,
+ /// Send notification only for job errors
+ #[serde(skip_serializing_if="Option::is_none")]
+ pub notify: Option<Notify>,
}
fn init() -> SectionConfig {
--
2.20.1
^ permalink raw reply [flat|nested] 2+ messages in thread
* [pbs-devel] [PATCH proxmox-backup 2/2] proxy: use new datastore notify settings
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
0 siblings, 0 replies; 2+ messages in thread
From: Dietmar Maurer @ 2020-11-04 10:32 UTC (permalink / raw)
To: pbs-devel
---
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2020-11-04 10:32 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [pbs-devel] [PATCH proxmox-backup 2/2] proxy: use new datastore notify settings Dietmar Maurer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox