]> git.proxmox.com Git - proxmox-backup.git/blob - pbs-config/src/verify.rs
update to first proxmox crate split
[proxmox-backup.git] / pbs-config / src / verify.rs
1 use std::collections::HashMap;
2
3 use anyhow::Error;
4 use lazy_static::lazy_static;
5
6 use proxmox_schema::*;
7 use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin};
8
9 use pbs_api_types::{JOB_ID_SCHEMA, VerificationJobConfig};
10
11 use crate::{open_backup_lockfile, replace_backup_config, BackupLockGuard};
12
13 lazy_static! {
14 pub static ref CONFIG: SectionConfig = init();
15 }
16
17 fn init() -> SectionConfig {
18 let obj_schema = match VerificationJobConfig::API_SCHEMA {
19 Schema::Object(ref obj_schema) => obj_schema,
20 _ => unreachable!(),
21 };
22
23 let plugin = SectionConfigPlugin::new("verification".to_string(), Some(String::from("id")), obj_schema);
24 let mut config = SectionConfig::new(&JOB_ID_SCHEMA);
25 config.register_plugin(plugin);
26
27 config
28 }
29
30 pub const VERIFICATION_CFG_FILENAME: &str = "/etc/proxmox-backup/verification.cfg";
31 pub const VERIFICATION_CFG_LOCKFILE: &str = "/etc/proxmox-backup/.verification.lck";
32
33 /// Get exclusive lock
34 pub fn lock_config() -> Result<BackupLockGuard, Error> {
35 open_backup_lockfile(VERIFICATION_CFG_LOCKFILE, None, true)
36 }
37
38 pub fn config() -> Result<(SectionConfigData, [u8;32]), Error> {
39
40 let content = proxmox::tools::fs::file_read_optional_string(VERIFICATION_CFG_FILENAME)?;
41 let content = content.unwrap_or_else(String::new);
42
43 let digest = openssl::sha::sha256(content.as_bytes());
44 let data = CONFIG.parse(VERIFICATION_CFG_FILENAME, &content)?;
45 Ok((data, digest))
46 }
47
48 pub fn save_config(config: &SectionConfigData) -> Result<(), Error> {
49 let raw = CONFIG.write(VERIFICATION_CFG_FILENAME, &config)?;
50 replace_backup_config(VERIFICATION_CFG_FILENAME, raw.as_bytes())
51 }
52
53 // shell completion helper
54 pub fn complete_verification_job_id(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
55 match config() {
56 Ok((data, _digest)) => data.sections.iter().map(|(id, _)| id.to_string()).collect(),
57 Err(_) => return vec![],
58 }
59 }