2019-03-12 13:39:51 +00:00
|
|
|
use std::collections::HashMap;
|
2019-08-21 12:09:27 +00:00
|
|
|
use std::io::Read;
|
2018-12-08 12:58:45 +00:00
|
|
|
|
2019-08-21 12:09:27 +00:00
|
|
|
use failure::*;
|
|
|
|
use lazy_static::lazy_static;
|
2018-12-08 12:58:45 +00:00
|
|
|
|
2019-08-21 12:09:27 +00:00
|
|
|
use proxmox::tools::{fs::file_set_contents, try_block};
|
2018-12-08 12:58:45 +00:00
|
|
|
|
2019-08-21 12:09:27 +00:00
|
|
|
use crate::api_schema::{ObjectSchema, StringSchema};
|
|
|
|
use crate::section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin};
|
2018-12-08 12:58:45 +00:00
|
|
|
|
2019-08-21 12:11:07 +00:00
|
|
|
lazy_static! {
|
2018-12-08 12:58:45 +00:00
|
|
|
static ref CONFIG: SectionConfig = init();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init() -> SectionConfig {
|
|
|
|
let plugin = SectionConfigPlugin::new(
|
|
|
|
"datastore".to_string(),
|
|
|
|
ObjectSchema::new("DataStore properties")
|
2019-08-21 12:11:07 +00:00
|
|
|
.required("path", StringSchema::new("Directory name")),
|
2018-12-08 12:58:45 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let id_schema = StringSchema::new("DataStore ID schema.")
|
|
|
|
.min_length(3)
|
|
|
|
.into();
|
|
|
|
|
|
|
|
let mut config = SectionConfig::new(id_schema);
|
|
|
|
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> {
|
2018-12-08 12:58:45 +00:00
|
|
|
let mut contents = String::new();
|
2019-02-15 11:13:15 +00:00
|
|
|
|
|
|
|
try_block!({
|
2019-02-16 09:06:08 +00:00
|
|
|
match std::fs::File::open(DATASTORE_CFG_FILENAME) {
|
|
|
|
Ok(mut file) => file.read_to_string(&mut contents),
|
|
|
|
Err(err) => {
|
|
|
|
if err.kind() == std::io::ErrorKind::NotFound {
|
|
|
|
contents = String::from("");
|
|
|
|
Ok(0)
|
|
|
|
} else {
|
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-21 12:11:07 +00:00
|
|
|
})
|
|
|
|
.map_err(|e| format_err!("unable to read '{}' - {}", DATASTORE_CFG_FILENAME, e))?;
|
2018-12-08 12:58:45 +00:00
|
|
|
|
2018-12-08 13:44:55 +00:00
|
|
|
CONFIG.parse(DATASTORE_CFG_FILENAME, &contents)
|
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)?;
|
|
|
|
|
2019-08-03 11:05:38 +00:00
|
|
|
file_set_contents(DATASTORE_CFG_FILENAME, raw.as_bytes(), None)?;
|
2018-12-09 11:51:31 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-12-12 11:18:28 +00:00
|
|
|
|
|
|
|
// shell completion helper
|
2019-03-12 13:39:51 +00:00
|
|
|
pub fn complete_datastore_name(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
|
2018-12-12 11:41:59 +00:00
|
|
|
match config() {
|
2019-08-21 12:11:07 +00:00
|
|
|
Ok(data) => data.sections.iter().map(|(id, _)| id.to_string()).collect(),
|
2018-12-12 11:18:28 +00:00
|
|
|
Err(_) => return vec![],
|
|
|
|
}
|
|
|
|
}
|