server: switch to inline template variables & small cleanups

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2023-03-09 08:03:43 +01:00
parent 15280f936f
commit dd06b7f1ee
4 changed files with 30 additions and 36 deletions

View File

@ -269,7 +269,7 @@ lazy_static::lazy_static! {
});
if let Err(err) = result {
eprintln!("error during template registration: {}", err);
eprintln!("error during template registration: {err}");
}
hb
@ -291,16 +291,13 @@ fn send_job_status_mail(email: &str, subject: &str, text: &str) -> Result<(), Er
let (config, _) = crate::config::node::config()?;
let from = config.email_from;
// Note: OX has serious problems displaying text mails,
// so we include html as well
let html = format!(
"<html><body><pre>\n{}\n<pre>",
handlebars::html_escape(text)
);
// NOTE: some (web)mailers have big problems displaying text mails, so include html as well
let escaped_text = handlebars::html_escape(text);
let html = format!("<html><body><pre>\n{escaped_text}\n<pre>");
let nodename = proxmox_sys::nodename();
let author = format!("Proxmox Backup Server - {}", nodename);
let author = format!("Proxmox Backup Server - {nodename}");
sendmail(
&[email],
@ -357,8 +354,8 @@ pub fn send_gc_status(
};
let subject = match result {
Ok(()) => format!("Garbage Collect Datastore '{}' successful", datastore,),
Err(_) => format!("Garbage Collect Datastore '{}' failed", datastore,),
Ok(()) => format!("Garbage Collect Datastore '{datastore}' successful"),
Err(_) => format!("Garbage Collect Datastore '{datastore}' failed"),
};
send_job_status_mail(email, &subject, &text)?;
@ -406,8 +403,8 @@ pub fn send_verify_status(
}
let subject = match result {
Ok(errors) if errors.is_empty() => format!("Verify Datastore '{}' successful", job.store,),
_ => format!("Verify Datastore '{}' failed", job.store,),
Ok(errors) if errors.is_empty() => format!("Verify Datastore '{}' successful", job.store),
_ => format!("Verify Datastore '{}' failed", job.store),
};
send_job_status_mail(email, &subject, &text)?;
@ -447,8 +444,8 @@ pub fn send_prune_status(
};
let subject = match result {
Ok(()) => format!("Pruning datastore '{}' successful", store,),
Err(_) => format!("Pruning datastore '{}' failed", store,),
Ok(()) => format!("Pruning datastore '{store}' successful"),
Err(_) => format!("Pruning datastore '{store}' failed"),
};
send_job_status_mail(&email, &subject, &text)?;
@ -530,9 +527,9 @@ pub fn send_tape_backup_status(
};
let subject = match (result, id) {
(Ok(()), Some(id)) => format!("Tape Backup '{}' datastore '{}' successful", id, job.store,),
(Ok(()), Some(id)) => format!("Tape Backup '{id}' datastore '{}' successful", job.store,),
(Ok(()), None) => format!("Tape Backup datastore '{}' successful", job.store,),
(Err(_), Some(id)) => format!("Tape Backup '{}' datastore '{}' failed", id, job.store,),
(Err(_), Some(id)) => format!("Tape Backup '{id}' datastore '{}' failed", job.store,),
(Err(_), None) => format!("Tape Backup datastore '{}' failed", job.store,),
};
@ -550,22 +547,21 @@ pub fn send_load_media_email(
) -> Result<(), Error> {
use std::fmt::Write as _;
let subject = format!("Load Media '{}' request for drive '{}'", label_text, drive);
let subject = format!("Load Media '{label_text}' request for drive '{drive}'");
let mut text = String::new();
if let Some(reason) = reason {
let _ = write!(
text,
"The drive has the wrong or no tape inserted. Error:\n{}\n\n",
reason
"The drive has the wrong or no tape inserted. Error:\n{reason}\n\n"
);
}
text.push_str("Please insert the requested media into the backup drive.\n\n");
let _ = writeln!(text, "Drive: {}", drive);
let _ = writeln!(text, "Media: {}", label_text);
let _ = writeln!(text, "Drive: {drive}");
let _ = writeln!(text, "Media: {label_text}");
send_job_status_mail(to, &subject, &text)
}
@ -592,7 +588,7 @@ pub fn send_updates_available(updates: &[&APTUpdateInfo]) -> Result<(), Error> {
// update mails always go to the root@pam configured email..
if let Some(email) = lookup_user_email(Userid::root_userid()) {
let nodename = proxmox_sys::nodename();
let subject = format!("New software packages available ({})", nodename);
let subject = format!("New software packages available ({nodename})");
let (fqdn, port) = get_server_url();

View File

@ -7,7 +7,7 @@ use pbs_api_types::Authid;
use pbs_datastore::DataStore;
use proxmox_rest_server::WorkerTask;
use crate::server::jobstate::Job;
use crate::server::{jobstate::Job, send_gc_status};
/// Runs a garbage collection job.
pub fn do_garbage_collection_job(
@ -30,9 +30,9 @@ pub fn do_garbage_collection_job(
move |worker| {
job.start(&worker.upid().to_string())?;
task_log!(worker, "starting garbage collection on store {}", store);
task_log!(worker, "starting garbage collection on store {store}");
if let Some(event_str) = schedule {
task_log!(worker, "task triggered by schedule '{}'", event_str);
task_log!(worker, "task triggered by schedule '{event_str}'");
}
let result = datastore.garbage_collection(&*worker, worker.upid());
@ -40,15 +40,13 @@ pub fn do_garbage_collection_job(
let status = worker.create_state(&result);
if let Err(err) = job.finish(status) {
eprintln!("could not finish job state for {}: {}", job.jobtype(), err);
eprintln!("could not finish job state for {}: {err}", job.jobtype());
}
if let Some(email) = email {
let gc_status = datastore.last_gc_status();
if let Err(err) =
crate::server::send_gc_status(&email, notify, &store, &gc_status, &result)
{
eprintln!("send gc notification failed: {}", err);
if let Err(err) = send_gc_status(&email, notify, &store, &gc_status, &result) {
eprintln!("send gc notification failed: {err}");
}
}

View File

@ -88,14 +88,14 @@ pub fn create_jobstate_dir() -> Result<(), Error> {
.group(backup_user.gid);
create_path(JOB_STATE_BASEDIR, Some(opts.clone()), Some(opts))
.map_err(|err: Error| format_err!("unable to create job state dir - {}", err))?;
.map_err(|err: Error| format_err!("unable to create job state dir - {err}"))?;
Ok(())
}
fn get_path(jobtype: &str, jobname: &str) -> PathBuf {
let mut path = PathBuf::from(JOB_STATE_BASEDIR);
path.push(format!("{}-{}.json", jobtype, jobname));
path.push(format!("{jobtype}-{jobname}.json"));
path
}
@ -176,7 +176,7 @@ pub fn last_run_time(jobtype: &str, jobname: &str) -> Result<i64, Error> {
} => {
let upid: UPID = upid
.parse()
.map_err(|err| format_err!("could not parse upid from state: {}", err))?;
.map_err(|err| format_err!("could not parse upid from state: {err}"))?;
Ok(upid.starttime)
}
}
@ -195,11 +195,11 @@ impl JobState {
JobState::Started { upid } => {
let parsed: UPID = upid
.parse()
.map_err(|err| format_err!("error parsing upid: {}", err))?;
.map_err(|err| format_err!("error parsing upid: {err}"))?;
if !worker_is_active_local(&parsed) {
let state = upid_read_status(&parsed)
.map_err(|err| format_err!("error reading upid log status: {}", err))?;
.map_err(|err| format_err!("error reading upid log status: {err}"))?;
Ok(JobState::Finished {
upid,

View File

@ -89,6 +89,6 @@ pub fn create_active_operations_dir() -> Result<(), Error> {
.group(backup_user.gid);
create_path(pbs_datastore::ACTIVE_OPERATIONS_DIR, None, Some(options))
.map_err(|err: Error| format_err!("unable to create active operations dir - {}", err))?;
.map_err(|err: Error| format_err!("unable to create active operations dir - {err}"))?;
Ok(())
}