]> git.proxmox.com Git - proxmox-backup.git/blob - pbs-config/src/media_pool.rs
update to proxmox-sys 0.2 crate
[proxmox-backup.git] / pbs-config / src / media_pool.rs
1 //! Media Pool configuration (Tape backup)
2 //!
3 //! This configuration module is based on [`SectionConfig`], and
4 //! provides a type safe interface to store [`MediaPoolConfig`],
5 //!
6 //! [MediaPoolConfig]: crate::api2::types::MediaPoolConfig
7 //! [SectionConfig]: proxmox_section_config::SectionConfig
8
9 use std::collections::HashMap;
10
11 use anyhow::Error;
12 use lazy_static::lazy_static;
13
14 use proxmox_schema::*;
15 use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin};
16
17 use pbs_api_types::{MEDIA_POOL_NAME_SCHEMA, MediaPoolConfig};
18
19 use crate::{open_backup_lockfile, replace_backup_config, BackupLockGuard};
20
21 lazy_static! {
22 /// Static [`SectionConfig`] to access parser/writer functions.
23 pub static ref CONFIG: SectionConfig = init();
24 }
25
26 fn init() -> SectionConfig {
27 let mut config = SectionConfig::new(&MEDIA_POOL_NAME_SCHEMA);
28
29 let obj_schema = match MediaPoolConfig::API_SCHEMA {
30 Schema::Object(ref obj_schema) => obj_schema,
31 _ => unreachable!(),
32 };
33 let plugin = SectionConfigPlugin::new("pool".to_string(), Some("name".to_string()), obj_schema);
34 config.register_plugin(plugin);
35
36 config
37 }
38
39 /// Configuration file name
40 pub const MEDIA_POOL_CFG_FILENAME: &str = "/etc/proxmox-backup/media-pool.cfg";
41 /// Lock file name (used to prevent concurrent access)
42 pub const MEDIA_POOL_CFG_LOCKFILE: &str = "/etc/proxmox-backup/.media-pool.lck";
43
44 /// Get exclusive lock
45 pub fn lock() -> Result<BackupLockGuard, Error> {
46 open_backup_lockfile(MEDIA_POOL_CFG_LOCKFILE, None, true)
47 }
48
49 /// Read and parse the configuration file
50 pub fn config() -> Result<(SectionConfigData, [u8;32]), Error> {
51
52 let content = proxmox_sys::fs::file_read_optional_string(MEDIA_POOL_CFG_FILENAME)?
53 .unwrap_or_else(|| "".to_string());
54
55 let digest = openssl::sha::sha256(content.as_bytes());
56 let data = CONFIG.parse(MEDIA_POOL_CFG_FILENAME, &content)?;
57 Ok((data, digest))
58 }
59
60 /// Save the configuration file
61 pub fn save_config(config: &SectionConfigData) -> Result<(), Error> {
62 let raw = CONFIG.write(MEDIA_POOL_CFG_FILENAME, &config)?;
63 replace_backup_config(MEDIA_POOL_CFG_FILENAME, raw.as_bytes())
64 }
65
66 // shell completion helper
67
68 /// List existing pool names
69 pub fn complete_pool_name(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
70 match config() {
71 Ok((data, _digest)) => data.sections.iter().map(|(id, _)| id.to_string()).collect(),
72 Err(_) => return vec![],
73 }
74 }