use serde_json::Value;
use ::serde::{Deserialize, Serialize};
-use proxmox::api::{api, Router, RpcEnvironment, Permission};
-use proxmox::tools::fs::open_file_locked;
-
-use crate::api2::types::*;
-use crate::backup::*;
-use crate::config::cached_user_info::CachedUserInfo;
-use crate::config::datastore::{self, DataStoreConfig, DIR_NAME_SCHEMA};
-use crate::config::acl::{PRIV_DATASTORE_AUDIT, PRIV_DATASTORE_MODIFY};
+use proxmox::api::{api, Router, RpcEnvironment, RpcEnvironmentType, Permission};
+use proxmox::api::section_config::SectionConfigData;
+use proxmox::api::schema::{ApiType, parse_property_string};
+
+use pbs_datastore::chunk_store::ChunkStore;
+use pbs_config::BackupLockGuard;
+use pbs_api_types::{
+ Authid, DatastoreNotify,
+ DATASTORE_SCHEMA, PROXMOX_CONFIG_DIGEST_SCHEMA,
+ PRIV_DATASTORE_ALLOCATE, PRIV_DATASTORE_AUDIT, PRIV_DATASTORE_MODIFY,
+ DataStoreConfig, DataStoreConfigUpdater,
+};
+use pbs_tools::task::TaskState;
+
+use crate::api2::config::sync::delete_sync_job;
+use crate::api2::config::verify::delete_verification_job;
+use crate::api2::config::tape_backup_job::{list_tape_backup_jobs, delete_tape_backup_job};
+use crate::api2::admin::{
+ sync::list_sync_jobs,
+ verify::list_verification_jobs,
+};
+use pbs_config::CachedUserInfo;
+use proxmox_rest_server::WorkerTask;
+
+use crate::server::jobstate;
#[api(
input: {
returns: {
description: "List the configured datastores (with config digest).",
type: Array,
- items: { type: datastore::DataStoreConfig },
+ items: { type: DataStoreConfig },
},
access: {
permission: &Permission::Anybody,
mut rpcenv: &mut dyn RpcEnvironment,
) -> Result<Vec<DataStoreConfig>, Error> {
- let (config, digest) = datastore::config()?;
+ let (config, digest) = pbs_config::datastore::config()?;
- let userid: Userid = rpcenv.get_user().unwrap().parse()?;
+ let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
let user_info = CachedUserInfo::new()?;
rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into();
let list:Vec<DataStoreConfig> = config.convert_to_typed_array("datastore")?;
let filter_by_privs = |store: &DataStoreConfig| {
- let user_privs = user_info.lookup_privs(&userid, &["datastore", &store.name]);
+ let user_privs = user_info.lookup_privs(&auth_id, &["datastore", &store.name]);
(user_privs & PRIV_DATASTORE_AUDIT) != 0
};
Ok(list.into_iter().filter(filter_by_privs).collect())
}
+pub(crate) fn do_create_datastore(
+ _lock: BackupLockGuard,
+ mut config: SectionConfigData,
+ datastore: DataStoreConfig,
+ worker: Option<&dyn TaskState>,
+) -> Result<(), Error> {
+ let path: PathBuf = datastore.path.clone().into();
+
+ let backup_user = pbs_config::backup_user()?;
+ let _store = ChunkStore::create(&datastore.name, path, backup_user.uid, backup_user.gid, worker)?;
+
+ config.set_data(&datastore.name, "datastore", &datastore)?;
-// fixme: impl. const fn get_object_schema(datastore::DataStoreConfig::API_SCHEMA),
-// but this need support for match inside const fn
-// see: https://github.com/rust-lang/rust/issues/49146
+ pbs_config::datastore::save_config(&config)?;
+
+ jobstate::create_state_file("prune", &datastore.name)?;
+ jobstate::create_state_file("garbage_collection", &datastore.name)?;
+
+ Ok(())
+}
#[api(
protected: true,
input: {
properties: {
- name: {
- schema: DATASTORE_SCHEMA,
- },
- path: {
- schema: DIR_NAME_SCHEMA,
- },
- comment: {
- optional: true,
- schema: SINGLE_LINE_COMMENT_SCHEMA,
- },
- "gc-schedule": {
- optional: true,
- schema: GC_SCHEDULE_SCHEMA,
- },
- "prune-schedule": {
- optional: true,
- schema: PRUNE_SCHEDULE_SCHEMA,
- },
- "verify-schedule": {
- optional: true,
- schema: VERIFY_SCHEDULE_SCHEMA,
- },
- "keep-last": {
- optional: true,
- schema: PRUNE_SCHEMA_KEEP_LAST,
- },
- "keep-hourly": {
- optional: true,
- schema: PRUNE_SCHEMA_KEEP_HOURLY,
- },
- "keep-daily": {
- optional: true,
- schema: PRUNE_SCHEMA_KEEP_DAILY,
- },
- "keep-weekly": {
- optional: true,
- schema: PRUNE_SCHEMA_KEEP_WEEKLY,
- },
- "keep-monthly": {
- optional: true,
- schema: PRUNE_SCHEMA_KEEP_MONTHLY,
- },
- "keep-yearly": {
- optional: true,
- schema: PRUNE_SCHEMA_KEEP_YEARLY,
+ config: {
+ type: DataStoreConfig,
+ flatten: true,
},
},
},
access: {
- permission: &Permission::Privilege(&["datastore"], PRIV_DATASTORE_MODIFY, false),
+ permission: &Permission::Privilege(&["datastore"], PRIV_DATASTORE_ALLOCATE, false),
},
)]
/// Create new datastore config.
-pub fn create_datastore(param: Value) -> Result<(), Error> {
+pub fn create_datastore(
+ config: DataStoreConfig,
+ rpcenv: &mut dyn RpcEnvironment,
+) -> Result<String, Error> {
- let _lock = open_file_locked(datastore::DATASTORE_CFG_LOCKFILE, std::time::Duration::new(10, 0))?;
+ let lock = pbs_config::datastore::lock_config()?;
- let datastore: datastore::DataStoreConfig = serde_json::from_value(param.clone())?;
+ let (section_config, _digest) = pbs_config::datastore::config()?;
- let (mut config, _digest) = datastore::config()?;
-
- if let Some(_) = config.sections.get(&datastore.name) {
- bail!("datastore '{}' already exists.", datastore.name);
+ if section_config.sections.get(&config.name).is_some() {
+ bail!("datastore '{}' already exists.", config.name);
}
- let path: PathBuf = datastore.path.clone().into();
-
- let backup_user = crate::backup::backup_user()?;
- let _store = ChunkStore::create(&datastore.name, path, backup_user.uid, backup_user.gid)?;
-
- config.set_data(&datastore.name, "datastore", &datastore)?;
-
- datastore::save_config(&config)?;
+ let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
+ let to_stdout = rpcenv.env_type() == RpcEnvironmentType::CLI;
- Ok(())
+ WorkerTask::new_thread(
+ "create-datastore",
+ Some(config.name.to_string()),
+ auth_id.to_string(),
+ to_stdout,
+ move |worker| do_create_datastore(lock, section_config, config, Some(&worker)),
+ )
}
#[api(
},
},
},
- returns: {
- description: "The datastore configuration (with config digest).",
- type: datastore::DataStoreConfig,
- },
+ returns: { type: DataStoreConfig },
access: {
permission: &Permission::Privilege(&["datastore", "{name}"], PRIV_DATASTORE_AUDIT, false),
},
name: String,
mut rpcenv: &mut dyn RpcEnvironment,
) -> Result<DataStoreConfig, Error> {
- let (config, digest) = datastore::config()?;
+ let (config, digest) = pbs_config::datastore::config()?;
let store_config = config.lookup("datastore", &name)?;
rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into();
keep_monthly,
/// Delete the keep-yearly property
keep_yearly,
+ /// Delete the verify-new property
+ verify_new,
+ /// Delete the notify-user property
+ notify_user,
+ /// Delete the notify property
+ notify,
}
#[api(
name: {
schema: DATASTORE_SCHEMA,
},
- comment: {
- optional: true,
- schema: SINGLE_LINE_COMMENT_SCHEMA,
- },
- "gc-schedule": {
- optional: true,
- schema: GC_SCHEDULE_SCHEMA,
- },
- "prune-schedule": {
- optional: true,
- schema: PRUNE_SCHEDULE_SCHEMA,
- },
- "verify-schedule": {
- optional: true,
- schema: VERIFY_SCHEDULE_SCHEMA,
- },
- "keep-last": {
- optional: true,
- schema: PRUNE_SCHEMA_KEEP_LAST,
- },
- "keep-hourly": {
- optional: true,
- schema: PRUNE_SCHEMA_KEEP_HOURLY,
- },
- "keep-daily": {
- optional: true,
- schema: PRUNE_SCHEMA_KEEP_DAILY,
- },
- "keep-weekly": {
- optional: true,
- schema: PRUNE_SCHEMA_KEEP_WEEKLY,
- },
- "keep-monthly": {
- optional: true,
- schema: PRUNE_SCHEMA_KEEP_MONTHLY,
- },
- "keep-yearly": {
- optional: true,
- schema: PRUNE_SCHEMA_KEEP_YEARLY,
+ update: {
+ type: DataStoreConfigUpdater,
+ flatten: true,
},
delete: {
description: "List of properties to delete.",
)]
/// Update datastore config.
pub fn update_datastore(
+ update: DataStoreConfigUpdater,
name: String,
- comment: Option<String>,
- gc_schedule: Option<String>,
- prune_schedule: Option<String>,
- verify_schedule: Option<String>,
- keep_last: Option<u64>,
- keep_hourly: Option<u64>,
- keep_daily: Option<u64>,
- keep_weekly: Option<u64>,
- keep_monthly: Option<u64>,
- keep_yearly: Option<u64>,
delete: Option<Vec<DeletableProperty>>,
digest: Option<String>,
) -> Result<(), Error> {
- let _lock = open_file_locked(datastore::DATASTORE_CFG_LOCKFILE, std::time::Duration::new(10, 0))?;
+ let _lock = pbs_config::datastore::lock_config()?;
// pass/compare digest
- let (mut config, expected_digest) = datastore::config()?;
+ let (mut config, expected_digest) = pbs_config::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)?;
}
- let mut data: datastore::DataStoreConfig = config.lookup("datastore", &name)?;
+ let mut data: DataStoreConfig = config.lookup("datastore", &name)?;
if let Some(delete) = delete {
for delete_prop in delete {
DeletableProperty::keep_weekly => { data.keep_weekly = None; },
DeletableProperty::keep_monthly => { data.keep_monthly = None; },
DeletableProperty::keep_yearly => { data.keep_yearly = None; },
+ DeletableProperty::verify_new => { data.verify_new = None; },
+ DeletableProperty::notify => { data.notify = None; },
+ DeletableProperty::notify_user => { data.notify_user = None; },
}
}
}
- if let Some(comment) = comment {
+ if let Some(comment) = update.comment {
let comment = comment.trim().to_string();
if comment.is_empty() {
data.comment = None;
}
}
- if gc_schedule.is_some() { data.gc_schedule = gc_schedule; }
- if prune_schedule.is_some() { data.prune_schedule = prune_schedule; }
- if verify_schedule.is_some() { data.verify_schedule = verify_schedule; }
+ let mut gc_schedule_changed = false;
+ if update.gc_schedule.is_some() {
+ gc_schedule_changed = data.gc_schedule != update.gc_schedule;
+ data.gc_schedule = update.gc_schedule;
+ }
- if keep_last.is_some() { data.keep_last = keep_last; }
- if keep_hourly.is_some() { data.keep_hourly = keep_hourly; }
- if keep_daily.is_some() { data.keep_daily = keep_daily; }
- if keep_weekly.is_some() { data.keep_weekly = keep_weekly; }
- if keep_monthly.is_some() { data.keep_monthly = keep_monthly; }
- if keep_yearly.is_some() { data.keep_yearly = keep_yearly; }
+ let mut prune_schedule_changed = false;
+ if update.prune_schedule.is_some() {
+ prune_schedule_changed = data.prune_schedule != update.prune_schedule;
+ data.prune_schedule = update.prune_schedule;
+ }
+
+ if update.keep_last.is_some() { data.keep_last = update.keep_last; }
+ if update.keep_hourly.is_some() { data.keep_hourly = update.keep_hourly; }
+ if update.keep_daily.is_some() { data.keep_daily = update.keep_daily; }
+ if update.keep_weekly.is_some() { data.keep_weekly = update.keep_weekly; }
+ if update.keep_monthly.is_some() { data.keep_monthly = update.keep_monthly; }
+ if update.keep_yearly.is_some() { data.keep_yearly = update.keep_yearly; }
+
+ if let Some(notify_str) = update.notify {
+ let value = parse_property_string(¬ify_str, &DatastoreNotify::API_SCHEMA)?;
+ let notify: DatastoreNotify = serde_json::from_value(value)?;
+ if let DatastoreNotify { gc: None, verify: None, sync: None } = notify {
+ data.notify = None;
+ } else {
+ data.notify = Some(notify_str);
+ }
+ }
+ if update.verify_new.is_some() { data.verify_new = update.verify_new; }
+
+ if update.notify_user.is_some() { data.notify_user = update.notify_user; }
config.set_data(&name, "datastore", &data)?;
- datastore::save_config(&config)?;
+ pbs_config::datastore::save_config(&config)?;
+
+ // we want to reset the statefiles, to avoid an immediate action in some cases
+ // (e.g. going from monthly to weekly in the second week of the month)
+ if gc_schedule_changed {
+ jobstate::update_job_last_run_time("garbage_collection", &name)?;
+ }
+
+ if prune_schedule_changed {
+ jobstate::update_job_last_run_time("prune", &name)?;
+ }
Ok(())
}
name: {
schema: DATASTORE_SCHEMA,
},
+ "keep-job-configs": {
+ description: "If enabled, the job configurations related to this datastore will be kept.",
+ type: bool,
+ optional: true,
+ default: false,
+ },
digest: {
optional: true,
schema: PROXMOX_CONFIG_DIGEST_SCHEMA,
},
},
access: {
- permission: &Permission::Privilege(&["datastore", "{name}"], PRIV_DATASTORE_MODIFY, false),
+ permission: &Permission::Privilege(&["datastore", "{name}"], PRIV_DATASTORE_ALLOCATE, false),
},
)]
/// Remove a datastore configuration.
-pub fn delete_datastore(name: String, digest: Option<String>) -> Result<(), Error> {
+pub async fn delete_datastore(
+ name: String,
+ keep_job_configs: bool,
+ digest: Option<String>,
+ rpcenv: &mut dyn RpcEnvironment,
+) -> Result<(), Error> {
- let _lock = open_file_locked(datastore::DATASTORE_CFG_LOCKFILE, std::time::Duration::new(10, 0))?;
+ let _lock = pbs_config::datastore::lock_config()?;
- let (mut config, expected_digest) = datastore::config()?;
+ let (mut config, expected_digest) = pbs_config::datastore::config()?;
if let Some(ref digest) = digest {
let digest = proxmox::tools::hex_to_digest(digest)?;
None => bail!("datastore '{}' does not exist.", name),
}
- datastore::save_config(&config)?;
+ if !keep_job_configs {
+ for job in list_verification_jobs(Some(name.clone()), Value::Null, rpcenv)? {
+ delete_verification_job(job.config.id, None, rpcenv)?
+ }
+ for job in list_sync_jobs(Some(name.clone()), Value::Null, rpcenv)? {
+ delete_sync_job(job.config.id, None, rpcenv)?
+ }
+
+ let tape_jobs = list_tape_backup_jobs(Value::Null, rpcenv)?;
+ for job_config in tape_jobs.into_iter().filter(|config| config.setup.store == name) {
+ delete_tape_backup_job(job_config.id, None, rpcenv)?;
+ }
+ }
+
+ pbs_config::datastore::save_config(&config)?;
+
+ // ignore errors
+ let _ = jobstate::remove_state_file("prune", &name);
+ let _ = jobstate::remove_state_file("garbage_collection", &name);
+
+ crate::server::notify_datastore_removed().await?;
Ok(())
}