2020-05-21 06:31:16 +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::{ApiType, Schema};
|
|
|
|
use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin};
|
2020-05-12 11:07:49 +00:00
|
|
|
|
2021-09-07 10:32:01 +00:00
|
|
|
use pbs_api_types::{JOB_ID_SCHEMA, SyncJobConfig};
|
2020-05-12 11:07:49 +00:00
|
|
|
|
2021-09-08 04:57:23 +00:00
|
|
|
use crate::{open_backup_lockfile, replace_backup_config, BackupLockGuard};
|
|
|
|
|
2020-05-21 06:31:16 +00:00
|
|
|
lazy_static! {
|
2021-02-10 09:27:40 +00:00
|
|
|
pub static ref CONFIG: SectionConfig = init();
|
2020-05-12 11:07:49 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 08:53:38 +00:00
|
|
|
|
2020-05-21 06:31:16 +00:00
|
|
|
fn init() -> SectionConfig {
|
2020-05-21 08:29:25 +00:00
|
|
|
let obj_schema = match SyncJobConfig::API_SCHEMA {
|
2021-11-21 09:29:58 +00:00
|
|
|
Schema::AllOf(ref allof_schema) => allof_schema,
|
2020-05-21 06:31:16 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2020-05-12 11:07:49 +00:00
|
|
|
|
2020-05-21 08:29:25 +00:00
|
|
|
let plugin = SectionConfigPlugin::new("sync".to_string(), Some(String::from("id")), obj_schema);
|
2020-05-21 06:31:16 +00:00
|
|
|
let mut config = SectionConfig::new(&JOB_ID_SCHEMA);
|
|
|
|
config.register_plugin(plugin);
|
2020-05-12 11:07:49 +00:00
|
|
|
|
2020-05-21 06:31:16 +00:00
|
|
|
config
|
|
|
|
}
|
|
|
|
|
2020-05-21 08:29:25 +00:00
|
|
|
pub const SYNC_CFG_FILENAME: &str = "/etc/proxmox-backup/sync.cfg";
|
|
|
|
pub const SYNC_CFG_LOCKFILE: &str = "/etc/proxmox-backup/.sync.lck";
|
2020-05-21 06:31:16 +00:00
|
|
|
|
2021-09-08 04:57:23 +00:00
|
|
|
/// Get exclusive lock
|
|
|
|
pub fn lock_config() -> Result<BackupLockGuard, Error> {
|
|
|
|
open_backup_lockfile(SYNC_CFG_LOCKFILE, None, true)
|
|
|
|
}
|
|
|
|
|
2020-05-21 06:31:16 +00:00
|
|
|
pub fn config() -> Result<(SectionConfigData, [u8;32]), Error> {
|
2020-05-28 08:06:44 +00:00
|
|
|
|
2021-11-23 16:57:00 +00:00
|
|
|
let content = proxmox_sys::fs::file_read_optional_string(SYNC_CFG_FILENAME)?
|
2021-01-19 13:04:46 +00:00
|
|
|
.unwrap_or_else(|| "".to_string());
|
2020-05-12 11:07:49 +00:00
|
|
|
|
2020-05-21 06:31:16 +00:00
|
|
|
let digest = openssl::sha::sha256(content.as_bytes());
|
2020-05-21 08:29:25 +00:00
|
|
|
let data = CONFIG.parse(SYNC_CFG_FILENAME, &content)?;
|
2020-05-21 06:31:16 +00:00
|
|
|
Ok((data, digest))
|
2020-05-12 11:07:49 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 06:31:16 +00:00
|
|
|
pub fn save_config(config: &SectionConfigData) -> Result<(), Error> {
|
2020-05-21 08:29:25 +00:00
|
|
|
let raw = CONFIG.write(SYNC_CFG_FILENAME, &config)?;
|
2021-09-08 04:57:23 +00:00
|
|
|
replace_backup_config(SYNC_CFG_FILENAME, raw.as_bytes())
|
2020-05-12 11:07:49 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 06:31:16 +00:00
|
|
|
// shell completion helper
|
2020-05-21 08:29:25 +00:00
|
|
|
pub fn complete_sync_job_id(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
|
2020-05-21 06:31:16 +00:00
|
|
|
match config() {
|
|
|
|
Ok((data, _digest)) => data.sections.iter().map(|(id, _)| id.to_string()).collect(),
|
|
|
|
Err(_) => return vec![],
|
2020-05-12 11:07:49 +00:00
|
|
|
}
|
|
|
|
}
|