2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{bail, Error};
|
2019-08-21 12:09:27 +00:00
|
|
|
use lazy_static::lazy_static;
|
2020-01-11 08:18:42 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use serde::{Serialize, Deserialize};
|
2018-12-08 12:58:45 +00:00
|
|
|
|
2020-03-02 11:52:11 +00:00
|
|
|
use proxmox::api::{
|
|
|
|
api,
|
|
|
|
schema::*,
|
|
|
|
section_config::{
|
|
|
|
SectionConfig,
|
|
|
|
SectionConfigData,
|
|
|
|
SectionConfigPlugin,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-11 08:18:42 +00:00
|
|
|
use proxmox::tools::{fs::replace_file, fs::CreateOptions};
|
2018-12-08 12:58:45 +00:00
|
|
|
|
2020-01-11 09:42:09 +00:00
|
|
|
use crate::api2::types::*;
|
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();
|
|
|
|
}
|
|
|
|
|
2020-01-11 08:18:42 +00:00
|
|
|
// fixme: define better schemas
|
|
|
|
|
|
|
|
pub const DIR_NAME_SCHEMA: Schema = StringSchema::new("Directory name").schema();
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
properties: {
|
|
|
|
comment: {
|
|
|
|
optional: true,
|
2020-01-13 11:02:13 +00:00
|
|
|
schema: SINGLE_LINE_COMMENT_SCHEMA,
|
2020-01-11 08:18:42 +00:00
|
|
|
},
|
|
|
|
path: {
|
|
|
|
schema: DIR_NAME_SCHEMA,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)]
|
|
|
|
#[derive(Serialize,Deserialize)]
|
|
|
|
/// Datastore configuration properties.
|
|
|
|
pub struct DataStoreConfig {
|
2020-01-13 11:14:14 +00:00
|
|
|
#[serde(skip_serializing_if="Option::is_none")]
|
2020-01-11 08:18:42 +00:00
|
|
|
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 {
|
2020-01-11 08:18:42 +00:00
|
|
|
let obj_schema = match DataStoreConfig::API_SCHEMA {
|
|
|
|
Schema::Object(ref obj_schema) => obj_schema,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
2020-05-18 12:18:35 +00:00
|
|
|
let plugin = SectionConfigPlugin::new("datastore".to_string(), None, obj_schema);
|
2020-01-11 09:42:09 +00:00
|
|
|
let mut config = SectionConfig::new(&DATASTORE_SCHEMA);
|
2018-12-08 12:58:45 +00:00
|
|
|
config.register_plugin(plugin);
|
|
|
|
|
|
|
|
config
|
|
|
|
}
|
|
|
|
|
2020-01-15 10:57:12 +00:00
|
|
|
pub const DATASTORE_CFG_FILENAME: &str = "/etc/proxmox-backup/datastore.cfg";
|
|
|
|
pub const DATASTORE_CFG_LOCKFILE: &str = "/etc/proxmox-backup/.datastore.lck";
|
2018-12-08 12:58:45 +00:00
|
|
|
|
2020-01-14 11:57:03 +00:00
|
|
|
pub fn config() -> Result<(SectionConfigData, [u8;32]), Error> {
|
2020-01-11 08:18:42 +00:00
|
|
|
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);
|
2019-02-16 09:06:08 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-11 08:18:42 +00:00
|
|
|
};
|
2018-12-08 12:58:45 +00:00
|
|
|
|
2020-01-14 11:57:03 +00:00
|
|
|
let digest = openssl::sha::sha256(content.as_bytes());
|
|
|
|
let data = CONFIG.parse(DATASTORE_CFG_FILENAME, &content)?;
|
|
|
|
Ok((data, digest))
|
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-12-19 09:20:13 +00:00
|
|
|
let backup_user = crate::backup::backup_user()?;
|
2019-12-18 09:41:58 +00:00
|
|
|
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)
|
2019-12-19 09:20:13 +00:00
|
|
|
.group(backup_user.gid);
|
2019-12-18 09:41:58 +00:00
|
|
|
|
|
|
|
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
|
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() {
|
2020-01-14 11:57:03 +00:00
|
|
|
Ok((data, _digest)) => data.sections.iter().map(|(id, _)| id.to_string()).collect(),
|
2018-12-12 11:18:28 +00:00
|
|
|
Err(_) => return vec![],
|
|
|
|
}
|
|
|
|
}
|
2020-04-14 06:40:53 +00:00
|
|
|
|
|
|
|
pub fn complete_acl_path(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
|
|
|
|
let mut list = Vec::new();
|
|
|
|
|
|
|
|
list.push(String::from("/"));
|
|
|
|
list.push(String::from("/storage"));
|
|
|
|
list.push(String::from("/storage/"));
|
|
|
|
|
|
|
|
if let Ok((data, _digest)) = config() {
|
|
|
|
for id in data.sections.keys() {
|
|
|
|
list.push(format!("/storage/{}", id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list
|
|
|
|
}
|