]> git.proxmox.com Git - proxmox-backup.git/blob - pbs-config/src/remote.rs
fb19cb53c90548ea04b30cecd35d51bd45d4af44
[proxmox-backup.git] / pbs-config / src / remote.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::{Remote, REMOTE_ID_SCHEMA};
10
11 use crate::{open_backup_lockfile, BackupLockGuard};
12
13 lazy_static! {
14 pub static ref CONFIG: SectionConfig = init();
15 }
16
17 fn init() -> SectionConfig {
18 let obj_schema = match Remote::API_SCHEMA {
19 Schema::AllOf(ref allof_schema) => allof_schema,
20 _ => unreachable!(),
21 };
22
23 let plugin = SectionConfigPlugin::new("remote".to_string(), Some("name".to_string()), obj_schema);
24 let mut config = SectionConfig::new(&REMOTE_ID_SCHEMA);
25 config.register_plugin(plugin);
26
27 config
28 }
29
30 pub const REMOTE_CFG_FILENAME: &str = "/etc/proxmox-backup/remote.cfg";
31 pub const REMOTE_CFG_LOCKFILE: &str = "/etc/proxmox-backup/.remote.lck";
32
33 /// Get exclusive lock
34 pub fn lock_config() -> Result<BackupLockGuard, Error> {
35 open_backup_lockfile(REMOTE_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(REMOTE_CFG_FILENAME)?
41 .unwrap_or_else(|| "".to_string());
42
43 let digest = openssl::sha::sha256(content.as_bytes());
44 let data = CONFIG.parse(REMOTE_CFG_FILENAME, &content)?;
45 Ok((data, digest))
46 }
47
48 pub fn save_config(config: &SectionConfigData) -> Result<(), Error> {
49 let raw = CONFIG.write(REMOTE_CFG_FILENAME, &config)?;
50 crate::replace_backup_config(REMOTE_CFG_FILENAME, raw.as_bytes())
51 }
52
53 // shell completion helper
54 pub fn complete_remote_name(_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 }