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)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 9404BBA11C for ; Wed, 13 Dec 2023 16:38:29 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 7CF77C7C1 for ; Wed, 13 Dec 2023 16:38:29 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Wed, 13 Dec 2023 16:38:28 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id A5BA7472DF for ; Wed, 13 Dec 2023 16:38:28 +0100 (CET) From: Christian Ebner To: pbs-devel@lists.proxmox.com Date: Wed, 13 Dec 2023 16:38:15 +0100 Message-Id: <20231213153819.391392-5-c.ebner@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231213153819.391392-1-c.ebner@proxmox.com> References: <20231213153819.391392-1-c.ebner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.056 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy 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 T_SCC_BODY_TEXT_LINE -0.01 - Subject: [pbs-devel] [RFC proxmox-backup 4/8] server: add sanity check job email notifications 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, 13 Dec 2023 15:38:29 -0000 Defines the email templates and method to send success/failure notification emails for the sanity check jobs. Signed-off-by: Christian Ebner --- src/server/email_notifications.rs | 78 +++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/src/server/email_notifications.rs b/src/server/email_notifications.rs index 43b55656..7672f30a 100644 --- a/src/server/email_notifications.rs +++ b/src/server/email_notifications.rs @@ -241,6 +241,36 @@ Please visit the web interface for further details: "###; +const SANITY_CHECK_OK_TEMPLATE: &str = r###" + +Job ID: {{jobname}} + +Sanity check successful. + + +Please visit the web interface for further details: + + + +"###; + +const SANITY_CHECK_ERR_TEMPLATE: &str = r###" + +Job ID: {{jobname}} + +Sanity checks failed: + +{{#each errors}} + {{this~}} +{{/each}} + + +Please visit the web interface for further details: + + + +"###; + lazy_static::lazy_static! { static ref HANDLEBARS: Handlebars<'static> = { @@ -272,6 +302,9 @@ lazy_static::lazy_static! { hb.register_template_string("certificate_renewal_err_template", ACME_CERTIFICATE_ERR_RENEWAL)?; + hb.register_template_string("sanity_check_ok_template", SANITY_CHECK_OK_TEMPLATE)?; + hb.register_template_string("sanity_check_err_template", SANITY_CHECK_ERR_TEMPLATE)?; + Ok(()) }); @@ -460,6 +493,48 @@ pub fn send_prune_status( Ok(()) } +pub fn send_sanity_check_status( + email: &str, + notify: Option, + jobname: &str, + result: &Result, Error>, +) -> Result<(), Error> { + match notify { + None => { /* send notifications by default */ } + Some(notify) => { + if notify == Notify::Never || (result.is_ok() && notify == Notify::Error) { + return Ok(()); + } + } + } + + let (fqdn, port) = get_server_url(); + let mut data = json!({ + "jobname": jobname, + "fqdn": fqdn, + "port": port, + }); + + let (subject, text) = match result { + Ok(errors) if errors.is_empty() => ( + format!("Sanity check successful"), + HANDLEBARS.render("sanity_check_ok_template", &data)?, + ), + Ok(errors) => { + data["errors"] = json!(errors); + ( + format!("Sanity check failed"), + HANDLEBARS.render("sanity_check_err_template", &data)?, + ) + } + Err(_) => return Ok(()), + }; + + send_job_status_mail(&email, &subject, &text)?; + + Ok(()) +} + pub fn send_sync_status( email: &str, notify: DatastoreNotify, @@ -760,4 +835,7 @@ fn test_template_register() { assert!(HANDLEBARS.has_template("package_update_template")); assert!(HANDLEBARS.has_template("certificate_renewal_err_template")); + + assert!(HANDLEBARS.has_template("sanity_check_ok_template")); + assert!(HANDLEBARS.has_template("sanity_check_err_template")); } -- 2.39.2