tape/backup: add success/error notifications

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2021-03-08 14:10:49 +01:00 committed by Dietmar Maurer
parent 9bcdade85f
commit 8703a68a31
2 changed files with 126 additions and 6 deletions

View File

@ -193,15 +193,30 @@ pub fn do_tape_backup_job(
task_log!(worker,"task triggered by schedule '{}'", event_str); task_log!(worker,"task triggered by schedule '{}'", event_str);
} }
let notify_user = setup.notify_user.as_ref().unwrap_or_else(|| &Userid::root_userid());
let email = lookup_user_email(notify_user);
let job_result = backup_worker( let job_result = backup_worker(
&worker, &worker,
datastore, datastore,
&pool_config, &pool_config,
&setup, &setup,
email.clone(),
); );
let status = worker.create_state(&job_result); let status = worker.create_state(&job_result);
if let Some(email) = email {
if let Err(err) = crate::server::send_tape_backup_status(
&email,
Some(job.jobname()),
&setup,
&job_result,
) {
eprintln!("send tape backup notification failed: {}", err);
}
}
if let Err(err) = job.finish(status) { if let Err(err) = job.finish(status) {
eprintln!( eprintln!(
"could not finish job state for {}: {}", "could not finish job state for {}: {}",
@ -312,6 +327,9 @@ pub fn backup(
let job_id = format!("{}:{}:{}", setup.store, setup.pool, setup.drive); let job_id = format!("{}:{}:{}", setup.store, setup.pool, setup.drive);
let notify_user = setup.notify_user.as_ref().unwrap_or_else(|| &Userid::root_userid());
let email = lookup_user_email(notify_user);
let upid_str = WorkerTask::new_thread( let upid_str = WorkerTask::new_thread(
"tape-backup", "tape-backup",
Some(job_id), Some(job_id),
@ -320,16 +338,28 @@ pub fn backup(
move |worker| { move |worker| {
let _drive_lock = drive_lock; // keep lock guard let _drive_lock = drive_lock; // keep lock guard
set_tape_device_state(&setup.drive, &worker.upid().to_string())?; set_tape_device_state(&setup.drive, &worker.upid().to_string())?;
backup_worker( let job_result = backup_worker(
&worker, &worker,
datastore, datastore,
&pool_config, &pool_config,
&setup, &setup,
)?; email.clone(),
);
if let Some(email) = email {
if let Err(err) = crate::server::send_tape_backup_status(
&email,
None,
&setup,
&job_result,
) {
eprintln!("send tape backup notification failed: {}", err);
}
}
// ignore errors // ignore errors
let _ = set_tape_device_state(&setup.drive, ""); let _ = set_tape_device_state(&setup.drive, "");
Ok(()) job_result
} }
)?; )?;
@ -341,6 +371,7 @@ fn backup_worker(
datastore: Arc<DataStore>, datastore: Arc<DataStore>,
pool_config: &MediaPoolConfig, pool_config: &MediaPoolConfig,
setup: &TapeBackupJobSetup, setup: &TapeBackupJobSetup,
email: Option<String>,
) -> Result<(), Error> { ) -> Result<(), Error> {
let status_path = Path::new(TAPE_STATUS_DIR); let status_path = Path::new(TAPE_STATUS_DIR);
@ -352,9 +383,6 @@ fn backup_worker(
let pool = MediaPool::with_config(status_path, &pool_config, changer_name)?; let pool = MediaPool::with_config(status_path, &pool_config, changer_name)?;
let notify_user = setup.notify_user.as_ref().unwrap_or_else(|| &Userid::root_userid());
let email = lookup_user_email(notify_user);
let mut pool_writer = PoolWriter::new(pool, &setup.drive, worker, email)?; let mut pool_writer = PoolWriter::new(pool, &setup.drive, worker, email)?;
let mut group_list = BackupInfo::list_backup_groups(&datastore.base_path())?; let mut group_list = BackupInfo::list_backup_groups(&datastore.base_path())?;

View File

@ -10,6 +10,7 @@ use crate::{
config::datastore::DataStoreConfig, config::datastore::DataStoreConfig,
config::verify::VerificationJobConfig, config::verify::VerificationJobConfig,
config::sync::SyncJobConfig, config::sync::SyncJobConfig,
config::tape_job::TapeBackupJobSetup,
api2::types::{ api2::types::{
APTUpdateInfo, APTUpdateInfo,
GarbageCollectionStatus, GarbageCollectionStatus,
@ -138,6 +139,43 @@ To upgrade visit the web interface:
"###; "###;
const TAPE_BACKUP_OK_TEMPLATE: &str = r###"
{{#if id ~}}
Job ID: {{id}}
{{/if~}}
Datastore: {{job.store}}
Tape Pool: {{job.pool}}
Tape Drive: {{job.drive}}
Tape Backup successful.
Please visit the web interface for futher details:
<https://{{fqdn}}:{{port}}/#DataStore-{{job.store}}>
"###;
const TAPE_BACKUP_ERR_TEMPLATE: &str = r###"
{{#if id ~}}
Job ID: {{id}}
{{/if~}}
Datastore: {{job.store}}
Tape Pool: {{job.pool}}
Tape Drive: {{job.drive}}
Tape Backup failed: {{error}}
Please visit the web interface for futher details:
<https://{{fqdn}}:{{port}}/#pbsServerAdministration:tasks>
"###;
lazy_static::lazy_static!{ lazy_static::lazy_static!{
@ -158,6 +196,9 @@ lazy_static::lazy_static!{
hb.register_template_string("sync_ok_template", SYNC_OK_TEMPLATE).unwrap(); hb.register_template_string("sync_ok_template", SYNC_OK_TEMPLATE).unwrap();
hb.register_template_string("sync_err_template", SYNC_ERR_TEMPLATE).unwrap(); hb.register_template_string("sync_err_template", SYNC_ERR_TEMPLATE).unwrap();
hb.register_template_string("tape_backup_ok_template", TAPE_BACKUP_OK_TEMPLATE).unwrap();
hb.register_template_string("tape_backup_err_template", TAPE_BACKUP_ERR_TEMPLATE).unwrap();
hb.register_template_string("package_update_template", PACKAGE_UPDATES_TEMPLATE).unwrap(); hb.register_template_string("package_update_template", PACKAGE_UPDATES_TEMPLATE).unwrap();
hb hb
@ -356,6 +397,57 @@ pub fn send_sync_status(
Ok(()) Ok(())
} }
pub fn send_tape_backup_status(
email: &str,
id: Option<&str>,
job: &TapeBackupJobSetup,
result: &Result<(), Error>,
) -> Result<(), Error> {
let (fqdn, port) = get_server_url();
let mut data = json!({
"job": job,
"fqdn": fqdn,
"port": port,
"id": id,
});
let text = match result {
Ok(()) => {
HANDLEBARS.render("tape_backup_ok_template", &data)?
}
Err(err) => {
data["error"] = err.to_string().into();
HANDLEBARS.render("tape_backup_err_template", &data)?
}
};
let subject = match (result, id) {
(Ok(()), Some(id)) => format!(
"Tape Backup '{}' datastore '{}' successful",
id,
job.store,
),
(Ok(()), None) => format!(
"Tape Backup datastore '{}' successful",
job.store,
),
(Err(_), Some(id)) => format!(
"Tape Backup '{}' datastore '{}' failed",
id,
job.store,
),
(Err(_), None) => format!(
"Tape Backup datastore '{}' failed",
job.store,
),
};
send_job_status_mail(email, &subject, &text)?;
Ok(())
}
fn get_server_url() -> (String, usize) { fn get_server_url() -> (String, usize) {
// user will surely request that they can change this // user will surely request that they can change this