2019-11-21 13:36:28 +00:00
|
|
|
use std::path::PathBuf;
|
2018-12-08 13:44:55 +00:00
|
|
|
|
2019-11-21 13:36:28 +00:00
|
|
|
use failure::*;
|
2019-12-20 11:28:28 +00:00
|
|
|
use serde_json::Value;
|
2018-12-08 13:44:55 +00:00
|
|
|
|
2020-04-17 12:51:29 +00:00
|
|
|
use proxmox::api::{api, ApiMethod, Router, RpcEnvironment, Permission};
|
2019-11-21 13:36:28 +00:00
|
|
|
|
2019-12-11 12:45:23 +00:00
|
|
|
use crate::api2::types::*;
|
2019-11-21 13:36:28 +00:00
|
|
|
use crate::backup::*;
|
2018-12-08 13:51:08 +00:00
|
|
|
use crate::config::datastore;
|
2020-04-17 12:51:29 +00:00
|
|
|
use crate::config::acl::{PRIV_DATASTORE_AUDIT, PRIV_DATASTORE_ALLOCATE};
|
2018-12-08 13:51:08 +00:00
|
|
|
|
2020-01-11 09:42:09 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {},
|
|
|
|
},
|
|
|
|
returns: {
|
2020-01-14 13:55:58 +00:00
|
|
|
description: "List the configured datastores (with config digest).",
|
2020-01-11 09:42:09 +00:00
|
|
|
type: Array,
|
|
|
|
items: {
|
|
|
|
type: datastore::DataStoreConfig,
|
|
|
|
},
|
|
|
|
},
|
2020-04-17 12:51:29 +00:00
|
|
|
access: {
|
|
|
|
permission: &Permission::Privilege(&["datastore"], PRIV_DATASTORE_AUDIT, false),
|
|
|
|
},
|
2020-01-11 09:42:09 +00:00
|
|
|
)]
|
|
|
|
/// List all datastores
|
|
|
|
pub fn list_datastores(
|
2019-01-26 13:50:37 +00:00
|
|
|
_param: Value,
|
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
2019-01-26 13:50:37 +00:00
|
|
|
) -> Result<Value, Error> {
|
2018-12-08 13:51:08 +00:00
|
|
|
|
2020-01-14 11:57:03 +00:00
|
|
|
let (config, digest) = datastore::config()?;
|
2018-12-09 09:23:19 +00:00
|
|
|
|
2020-01-30 12:26:46 +00:00
|
|
|
Ok(config.convert_to_array("name", Some(&digest), &[]))
|
2018-12-09 10:59:32 +00:00
|
|
|
}
|
|
|
|
|
2020-01-11 09:42:09 +00:00
|
|
|
#[api(
|
|
|
|
protected: true,
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
name: {
|
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
|
|
|
comment: {
|
|
|
|
optional: true,
|
2020-01-13 11:02:13 +00:00
|
|
|
schema: SINGLE_LINE_COMMENT_SCHEMA,
|
2020-01-11 09:42:09 +00:00
|
|
|
},
|
|
|
|
path: {
|
|
|
|
schema: datastore::DIR_NAME_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-04-17 12:51:29 +00:00
|
|
|
access: {
|
|
|
|
permission: &Permission::Privilege(&["datastore"], PRIV_DATASTORE_ALLOCATE, false),
|
|
|
|
},
|
2020-01-11 09:42:09 +00:00
|
|
|
)]
|
|
|
|
/// Create new datastore config.
|
|
|
|
pub fn create_datastore(name: String, param: Value) -> Result<(), Error> {
|
2018-12-09 10:59:32 +00:00
|
|
|
|
2020-01-15 10:57:12 +00:00
|
|
|
let _lock = crate::tools::open_file_locked(datastore::DATASTORE_CFG_LOCKFILE, std::time::Duration::new(10, 0))?;
|
2018-12-09 11:51:31 +00:00
|
|
|
|
2020-01-11 09:42:09 +00:00
|
|
|
let datastore: datastore::DataStoreConfig = serde_json::from_value(param.clone())?;
|
2018-12-09 11:51:31 +00:00
|
|
|
|
2020-01-14 11:57:03 +00:00
|
|
|
let (mut config, _digest) = datastore::config()?;
|
2018-12-09 11:51:31 +00:00
|
|
|
|
2020-01-11 09:42:09 +00:00
|
|
|
if let Some(_) = config.sections.get(&name) {
|
2018-12-09 11:51:31 +00:00
|
|
|
bail!("datastore '{}' already exists.", name);
|
|
|
|
}
|
|
|
|
|
2020-01-11 09:42:09 +00:00
|
|
|
let path: PathBuf = datastore.path.clone().into();
|
|
|
|
|
|
|
|
let backup_user = crate::backup::backup_user()?;
|
|
|
|
let _store = ChunkStore::create(&name, path, backup_user.uid, backup_user.gid)?;
|
|
|
|
|
2020-01-11 10:09:27 +00:00
|
|
|
config.set_data(&name, "datastore", &datastore)?;
|
2018-12-09 11:51:31 +00:00
|
|
|
|
|
|
|
datastore::save_config(&config)?;
|
|
|
|
|
2020-01-11 09:42:09 +00:00
|
|
|
Ok(())
|
2018-12-08 13:44:55 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 13:45:56 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
name: {
|
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-01-14 13:55:58 +00:00
|
|
|
returns: {
|
|
|
|
description: "The datastore configuration (with config digest).",
|
|
|
|
type: datastore::DataStoreConfig,
|
|
|
|
},
|
2020-04-17 12:51:29 +00:00
|
|
|
access: {
|
|
|
|
permission: &Permission::Privilege(&["datastore", "{name}"], PRIV_DATASTORE_AUDIT, false),
|
|
|
|
},
|
2020-01-14 13:45:56 +00:00
|
|
|
)]
|
|
|
|
/// Read a datastore configuration.
|
|
|
|
pub fn read_datastore(name: String) -> Result<Value, Error> {
|
|
|
|
let (config, digest) = datastore::config()?;
|
|
|
|
let mut data = config.lookup_json("datastore", &name)?;
|
|
|
|
data.as_object_mut().unwrap()
|
|
|
|
.insert("digest".into(), proxmox::tools::digest_to_hex(&digest).into());
|
|
|
|
Ok(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
protected: true,
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
name: {
|
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
|
|
|
comment: {
|
|
|
|
optional: true,
|
|
|
|
schema: SINGLE_LINE_COMMENT_SCHEMA,
|
|
|
|
},
|
2020-01-15 11:27:05 +00:00
|
|
|
digest: {
|
|
|
|
optional: true,
|
|
|
|
schema: PROXMOX_CONFIG_DIGEST_SCHEMA,
|
|
|
|
},
|
2020-01-14 13:45:56 +00:00
|
|
|
},
|
|
|
|
},
|
2020-04-17 12:51:29 +00:00
|
|
|
access: {
|
|
|
|
permission: &Permission::Privilege(&["datastore", "{name}"], PRIV_DATASTORE_ALLOCATE, false),
|
|
|
|
},
|
2020-01-14 13:45:56 +00:00
|
|
|
)]
|
|
|
|
/// Create new datastore config.
|
|
|
|
pub fn update_datastore(
|
|
|
|
name: String,
|
|
|
|
comment: Option<String>,
|
2020-01-15 11:27:05 +00:00
|
|
|
digest: Option<String>,
|
2020-01-14 13:45:56 +00:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
2020-01-15 10:57:12 +00:00
|
|
|
let _lock = crate::tools::open_file_locked(datastore::DATASTORE_CFG_LOCKFILE, std::time::Duration::new(10, 0))?;
|
|
|
|
|
2020-01-14 13:45:56 +00:00
|
|
|
// pass/compare digest
|
2020-01-15 11:27:05 +00:00
|
|
|
let (mut config, expected_digest) = datastore::config()?;
|
|
|
|
|
|
|
|
if let Some(ref digest) = digest {
|
|
|
|
let digest = proxmox::tools::hex_to_digest(digest)?;
|
|
|
|
crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?;
|
|
|
|
}
|
2020-01-14 13:45:56 +00:00
|
|
|
|
|
|
|
let mut data: datastore::DataStoreConfig = config.lookup("datastore", &name)?;
|
|
|
|
|
|
|
|
if let Some(comment) = comment {
|
|
|
|
let comment = comment.trim().to_string();
|
|
|
|
if comment.is_empty() {
|
|
|
|
data.comment = None;
|
|
|
|
} else {
|
|
|
|
data.comment = Some(comment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
config.set_data(&name, "datastore", &data)?;
|
|
|
|
|
|
|
|
datastore::save_config(&config)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-01-11 09:42:09 +00:00
|
|
|
#[api(
|
|
|
|
protected: true,
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
name: {
|
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
2020-04-17 12:51:29 +00:00
|
|
|
digest: {
|
|
|
|
optional: true,
|
|
|
|
schema: PROXMOX_CONFIG_DIGEST_SCHEMA,
|
|
|
|
},
|
2020-01-11 09:42:09 +00:00
|
|
|
},
|
|
|
|
},
|
2020-04-17 12:51:29 +00:00
|
|
|
access: {
|
|
|
|
permission: &Permission::Privilege(&["datastore", "{name}"], PRIV_DATASTORE_ALLOCATE, false),
|
|
|
|
},
|
2020-01-11 09:42:09 +00:00
|
|
|
)]
|
|
|
|
/// Remove a datastore configuration.
|
2020-04-17 12:51:29 +00:00
|
|
|
pub fn delete_datastore(name: String, digest: Option<String>) -> Result<(), Error> {
|
2018-12-09 15:52:32 +00:00
|
|
|
|
2020-04-17 12:51:29 +00:00
|
|
|
let _lock = crate::tools::open_file_locked(datastore::DATASTORE_CFG_LOCKFILE, std::time::Duration::new(10, 0))?;
|
2018-12-09 15:52:32 +00:00
|
|
|
|
2020-04-17 12:51:29 +00:00
|
|
|
let (mut config, expected_digest) = datastore::config()?;
|
|
|
|
|
|
|
|
if let Some(ref digest) = digest {
|
|
|
|
let digest = proxmox::tools::hex_to_digest(digest)?;
|
|
|
|
crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?;
|
|
|
|
}
|
2018-12-09 15:52:32 +00:00
|
|
|
|
2020-01-11 09:42:09 +00:00
|
|
|
match config.sections.get(&name) {
|
|
|
|
Some(_) => { config.sections.remove(&name); },
|
2018-12-09 15:52:32 +00:00
|
|
|
None => bail!("datastore '{}' does not exist.", name),
|
|
|
|
}
|
|
|
|
|
|
|
|
datastore::save_config(&config)?;
|
|
|
|
|
2020-01-11 09:42:09 +00:00
|
|
|
Ok(())
|
2018-12-09 15:52:32 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 13:45:56 +00:00
|
|
|
const ITEM_ROUTER: Router = Router::new()
|
|
|
|
.get(&API_METHOD_READ_DATASTORE)
|
|
|
|
.put(&API_METHOD_UPDATE_DATASTORE)
|
|
|
|
.delete(&API_METHOD_DELETE_DATASTORE);
|
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
pub const ROUTER: Router = Router::new()
|
2020-01-11 09:42:09 +00:00
|
|
|
.get(&API_METHOD_LIST_DATASTORES)
|
|
|
|
.post(&API_METHOD_CREATE_DATASTORE)
|
2020-01-14 13:45:56 +00:00
|
|
|
.match_all("name", &ITEM_ROUTER);
|