2020-10-20 09:10:04 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2021-10-08 09:19:37 +00:00
|
|
|
use anyhow::Error;
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
|
|
|
|
use proxmox_schema::*;
|
|
|
|
use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin};
|
2020-10-20 09:10:04 +00:00
|
|
|
|
2021-09-07 10:32:01 +00:00
|
|
|
use pbs_api_types::{JOB_ID_SCHEMA, VerificationJobConfig};
|
2020-10-20 09:10:04 +00:00
|
|
|
|
2021-09-08 06:01:07 +00:00
|
|
|
use crate::{open_backup_lockfile, replace_backup_config, BackupLockGuard};
|
|
|
|
|
2020-10-20 09:10:04 +00:00
|
|
|
lazy_static! {
|
2021-02-11 12:40:11 +00:00
|
|
|
pub static ref CONFIG: SectionConfig = init();
|
2020-10-20 09:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn init() -> SectionConfig {
|
|
|
|
let obj_schema = match VerificationJobConfig::API_SCHEMA {
|
|
|
|
Schema::Object(ref obj_schema) => obj_schema,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let plugin = SectionConfigPlugin::new("verification".to_string(), Some(String::from("id")), obj_schema);
|
|
|
|
let mut config = SectionConfig::new(&JOB_ID_SCHEMA);
|
|
|
|
config.register_plugin(plugin);
|
|
|
|
|
|
|
|
config
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const VERIFICATION_CFG_FILENAME: &str = "/etc/proxmox-backup/verification.cfg";
|
|
|
|
pub const VERIFICATION_CFG_LOCKFILE: &str = "/etc/proxmox-backup/.verification.lck";
|
|
|
|
|
2021-09-08 06:01:07 +00:00
|
|
|
/// Get exclusive lock
|
|
|
|
pub fn lock_config() -> Result<BackupLockGuard, Error> {
|
|
|
|
open_backup_lockfile(VERIFICATION_CFG_LOCKFILE, None, true)
|
|
|
|
}
|
|
|
|
|
2020-10-20 09:10:04 +00:00
|
|
|
pub fn config() -> Result<(SectionConfigData, [u8;32]), Error> {
|
|
|
|
|
2021-11-23 16:57:00 +00:00
|
|
|
let content = proxmox_sys::fs::file_read_optional_string(VERIFICATION_CFG_FILENAME)?;
|
2020-10-20 09:10:04 +00:00
|
|
|
let content = content.unwrap_or_else(String::new);
|
|
|
|
|
|
|
|
let digest = openssl::sha::sha256(content.as_bytes());
|
|
|
|
let data = CONFIG.parse(VERIFICATION_CFG_FILENAME, &content)?;
|
|
|
|
Ok((data, digest))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn save_config(config: &SectionConfigData) -> Result<(), Error> {
|
2021-12-30 11:57:37 +00:00
|
|
|
let raw = CONFIG.write(VERIFICATION_CFG_FILENAME, config)?;
|
2021-09-08 06:01:07 +00:00
|
|
|
replace_backup_config(VERIFICATION_CFG_FILENAME, raw.as_bytes())
|
2020-10-20 09:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// shell completion helper
|
|
|
|
pub fn complete_verification_job_id(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
|
|
|
|
match config() {
|
|
|
|
Ok((data, _digest)) => data.sections.iter().map(|(id, _)| id.to_string()).collect(),
|
|
|
|
Err(_) => return vec![],
|
|
|
|
}
|
2020-10-28 12:56:49 +00:00
|
|
|
}
|