proxmox-backup/src/config/datastore.rs

95 lines
2.7 KiB
Rust
Raw Normal View History

use failure::*;
use lazy_static::lazy_static;
use std::collections::HashMap;
use serde::{Serialize, Deserialize};
2018-12-08 12:58:45 +00:00
use proxmox::api::{api, schema::*};
use proxmox::tools::{fs::replace_file, fs::CreateOptions};
2018-12-08 12:58:45 +00:00
use crate::section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin};
2018-12-08 12:58:45 +00:00
lazy_static! {
2018-12-08 12:58:45 +00:00
static ref CONFIG: SectionConfig = init();
}
// fixme: define better schemas
pub const DIR_NAME_SCHEMA: Schema = StringSchema::new("Directory name").schema();
pub const COMMENT_SCHEMA: Schema = StringSchema::new("Datastore comment").schema();
pub const DATASTORE_ID_SCHEMA: Schema = StringSchema::new("DataStore ID schema.")
2019-11-21 08:36:41 +00:00
.min_length(3)
.schema();
#[api(
properties: {
comment: {
optional: true,
schema: COMMENT_SCHEMA,
},
path: {
schema: DIR_NAME_SCHEMA,
},
}
)]
#[derive(Serialize,Deserialize)]
/// Datastore configuration properties.
pub struct DataStoreConfig {
pub comment: Option<String>,
pub path: String,
}
2018-12-08 12:58:45 +00:00
2019-11-21 08:36:41 +00:00
fn init() -> SectionConfig {
let obj_schema = match DataStoreConfig::API_SCHEMA {
Schema::Object(ref obj_schema) => obj_schema,
_ => unreachable!(),
};
let plugin = SectionConfigPlugin::new("datastore".to_string(), obj_schema);
2019-11-21 08:36:41 +00:00
let mut config = SectionConfig::new(&DATASTORE_ID_SCHEMA);
2018-12-08 12:58:45 +00:00
config.register_plugin(plugin);
config
}
2018-12-08 13:44:55 +00:00
const DATASTORE_CFG_FILENAME: &str = "/etc/proxmox-backup/datastore.cfg";
2018-12-08 12:58:45 +00:00
2018-12-08 13:51:08 +00:00
pub fn config() -> Result<SectionConfigData, Error> {
let content = match std::fs::read_to_string(DATASTORE_CFG_FILENAME) {
Ok(c) => c,
Err(err) => {
if err.kind() == std::io::ErrorKind::NotFound {
String::from("")
} else {
bail!("unable to read '{}' - {}", DATASTORE_CFG_FILENAME, err);
}
}
};
2018-12-08 12:58:45 +00:00
CONFIG.parse(DATASTORE_CFG_FILENAME, &content)
2018-12-08 12:58:45 +00:00
}
2018-12-09 11:51:31 +00:00
pub fn save_config(config: &SectionConfigData) -> Result<(), Error> {
let raw = CONFIG.write(DATASTORE_CFG_FILENAME, &config)?;
let backup_user = crate::backup::backup_user()?;
let mode = nix::sys::stat::Mode::from_bits_truncate(0o0640);
// set the correct owner/group/permissions while saving file
// owner(rw) = root, group(r)= backup
let options = CreateOptions::new()
.perm(mode)
.owner(nix::unistd::ROOT)
.group(backup_user.gid);
replace_file(DATASTORE_CFG_FILENAME, raw.as_bytes(), options)?;
2018-12-09 11:51:31 +00:00
Ok(())
}
2018-12-12 11:18:28 +00:00
// shell completion helper
pub fn complete_datastore_name(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
match config() {
Ok(data) => data.sections.iter().map(|(id, _)| id.to_string()).collect(),
2018-12-12 11:18:28 +00:00
Err(_) => return vec![],
}
}