2021-01-22 11:01:46 +00:00
|
|
|
//! Media Pool configuration (Tape backup)
|
|
|
|
//!
|
|
|
|
//! This configuration module is based on [`SectionConfig`], and
|
|
|
|
//! provides a type safe interface to store [`MediaPoolConfig`],
|
|
|
|
//!
|
|
|
|
//! [MediaPoolConfig]: crate::api2::types::MediaPoolConfig
|
2021-10-08 09:19:37 +00:00
|
|
|
//! [SectionConfig]: proxmox_section_config::SectionConfig
|
2021-01-22 11:01:46 +00:00
|
|
|
|
2020-12-05 10:59:38 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use anyhow::Error;
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
|
2021-10-08 09:19:37 +00:00
|
|
|
use proxmox_schema::*;
|
|
|
|
use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin};
|
2020-12-05 10:59:38 +00:00
|
|
|
|
2021-09-06 06:56:04 +00:00
|
|
|
use pbs_api_types::{MEDIA_POOL_NAME_SCHEMA, MediaPoolConfig};
|
2021-09-02 10:47:11 +00:00
|
|
|
|
2021-09-06 06:56:04 +00:00
|
|
|
use crate::{open_backup_lockfile, replace_backup_config, BackupLockGuard};
|
2020-12-05 10:59:38 +00:00
|
|
|
|
|
|
|
lazy_static! {
|
2021-01-22 11:01:46 +00:00
|
|
|
/// Static [`SectionConfig`] to access parser/writer functions.
|
|
|
|
pub static ref CONFIG: SectionConfig = init();
|
2020-12-05 10:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn init() -> SectionConfig {
|
|
|
|
let mut config = SectionConfig::new(&MEDIA_POOL_NAME_SCHEMA);
|
|
|
|
|
|
|
|
let obj_schema = match MediaPoolConfig::API_SCHEMA {
|
|
|
|
Schema::Object(ref obj_schema) => obj_schema,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
let plugin = SectionConfigPlugin::new("pool".to_string(), Some("name".to_string()), obj_schema);
|
|
|
|
config.register_plugin(plugin);
|
|
|
|
|
|
|
|
config
|
|
|
|
}
|
|
|
|
|
2021-01-22 11:01:46 +00:00
|
|
|
/// Configuration file name
|
2021-01-18 12:25:04 +00:00
|
|
|
pub const MEDIA_POOL_CFG_FILENAME: &str = "/etc/proxmox-backup/media-pool.cfg";
|
2021-01-22 11:01:46 +00:00
|
|
|
/// Lock file name (used to prevent concurrent access)
|
2021-01-18 12:25:04 +00:00
|
|
|
pub const MEDIA_POOL_CFG_LOCKFILE: &str = "/etc/proxmox-backup/.media-pool.lck";
|
2020-12-05 10:59:38 +00:00
|
|
|
|
2021-01-22 11:01:46 +00:00
|
|
|
/// Get exclusive lock
|
2021-07-20 11:51:54 +00:00
|
|
|
pub fn lock() -> Result<BackupLockGuard, Error> {
|
|
|
|
open_backup_lockfile(MEDIA_POOL_CFG_LOCKFILE, None, true)
|
2020-12-05 10:59:38 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 11:01:46 +00:00
|
|
|
/// Read and parse the configuration file
|
2020-12-05 10:59:38 +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(MEDIA_POOL_CFG_FILENAME)?
|
2021-01-19 13:04:46 +00:00
|
|
|
.unwrap_or_else(|| "".to_string());
|
2020-12-05 10:59:38 +00:00
|
|
|
|
|
|
|
let digest = openssl::sha::sha256(content.as_bytes());
|
|
|
|
let data = CONFIG.parse(MEDIA_POOL_CFG_FILENAME, &content)?;
|
|
|
|
Ok((data, digest))
|
|
|
|
}
|
|
|
|
|
2021-01-22 11:01:46 +00:00
|
|
|
/// Save the configuration file
|
2020-12-05 10:59:38 +00:00
|
|
|
pub fn save_config(config: &SectionConfigData) -> Result<(), Error> {
|
|
|
|
let raw = CONFIG.write(MEDIA_POOL_CFG_FILENAME, &config)?;
|
2021-09-06 06:56:04 +00:00
|
|
|
replace_backup_config(MEDIA_POOL_CFG_FILENAME, raw.as_bytes())
|
2020-12-05 10:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// shell completion helper
|
|
|
|
|
|
|
|
/// List existing pool names
|
|
|
|
pub fn complete_pool_name(_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![],
|
|
|
|
}
|
|
|
|
}
|