1 use std
::path
::PathBuf
;
3 use anyhow
::{bail, Error}
;
5 use ::serde
::{Deserialize, Serialize}
;
7 use proxmox
::api
::{api, Router, RpcEnvironment, Permission}
;
8 use proxmox
::api
::schema
::parse_property_string
;
9 use proxmox
::tools
::fs
::open_file_locked
;
11 use crate::api2
::types
::*;
13 use crate::config
::cached_user_info
::CachedUserInfo
;
14 use crate::config
::datastore
::{self, DataStoreConfig, DIR_NAME_SCHEMA}
;
15 use crate::config
::acl
::{PRIV_DATASTORE_ALLOCATE, PRIV_DATASTORE_AUDIT, PRIV_DATASTORE_MODIFY}
;
16 use crate::server
::jobstate
;
23 description
: "List the configured datastores (with config digest).",
25 items
: { type: datastore::DataStoreConfig }
,
28 permission
: &Permission
::Anybody
,
31 /// List all datastores
32 pub fn list_datastores(
34 mut rpcenv
: &mut dyn RpcEnvironment
,
35 ) -> Result
<Vec
<DataStoreConfig
>, Error
> {
37 let (config
, digest
) = datastore
::config()?
;
39 let auth_id
: Authid
= rpcenv
.get_auth_id().unwrap().parse()?
;
40 let user_info
= CachedUserInfo
::new()?
;
42 rpcenv
["digest"] = proxmox
::tools
::digest_to_hex(&digest
).into();
44 let list
:Vec
<DataStoreConfig
> = config
.convert_to_typed_array("datastore")?
;
45 let filter_by_privs
= |store
: &DataStoreConfig
| {
46 let user_privs
= user_info
.lookup_privs(&auth_id
, &["datastore", &store
.name
]);
47 (user_privs
& PRIV_DATASTORE_AUDIT
) != 0
50 Ok(list
.into_iter().filter(filter_by_privs
).collect())
54 // fixme: impl. const fn get_object_schema(datastore::DataStoreConfig::API_SCHEMA),
55 // but this need support for match inside const fn
56 // see: https://github.com/rust-lang/rust/issues/49146
63 schema
: DATASTORE_SCHEMA
,
66 schema
: DIR_NAME_SCHEMA
,
70 schema
: SINGLE_LINE_COMMENT_SCHEMA
,
78 schema
: DATASTORE_NOTIFY_STRING_SCHEMA
,
82 schema
: GC_SCHEDULE_SCHEMA
,
86 schema
: PRUNE_SCHEDULE_SCHEMA
,
90 schema
: PRUNE_SCHEMA_KEEP_LAST
,
94 schema
: PRUNE_SCHEMA_KEEP_HOURLY
,
98 schema
: PRUNE_SCHEMA_KEEP_DAILY
,
102 schema
: PRUNE_SCHEMA_KEEP_WEEKLY
,
106 schema
: PRUNE_SCHEMA_KEEP_MONTHLY
,
110 schema
: PRUNE_SCHEMA_KEEP_YEARLY
,
115 permission
: &Permission
::Privilege(&["datastore"], PRIV_DATASTORE_ALLOCATE
, false),
118 /// Create new datastore config.
119 pub fn create_datastore(param
: Value
) -> Result
<(), Error
> {
121 let _lock
= open_file_locked(datastore
::DATASTORE_CFG_LOCKFILE
, std
::time
::Duration
::new(10, 0), true)?
;
123 let datastore
: datastore
::DataStoreConfig
= serde_json
::from_value(param
)?
;
125 let (mut config
, _digest
) = datastore
::config()?
;
127 if let Some(_
) = config
.sections
.get(&datastore
.name
) {
128 bail
!("datastore '{}' already exists.", datastore
.name
);
131 let path
: PathBuf
= datastore
.path
.clone().into();
133 let backup_user
= crate::backup
::backup_user()?
;
134 let _store
= ChunkStore
::create(&datastore
.name
, path
, backup_user
.uid
, backup_user
.gid
)?
;
136 config
.set_data(&datastore
.name
, "datastore", &datastore
)?
;
138 datastore
::save_config(&config
)?
;
140 jobstate
::create_state_file("prune", &datastore
.name
)?
;
141 jobstate
::create_state_file("garbage_collection", &datastore
.name
)?
;
150 schema
: DATASTORE_SCHEMA
,
154 returns
: { type: datastore::DataStoreConfig }
,
156 permission
: &Permission
::Privilege(&["datastore", "{name}"], PRIV_DATASTORE_AUDIT
, false),
159 /// Read a datastore configuration.
160 pub fn read_datastore(
162 mut rpcenv
: &mut dyn RpcEnvironment
,
163 ) -> Result
<DataStoreConfig
, Error
> {
164 let (config
, digest
) = datastore
::config()?
;
166 let store_config
= config
.lookup("datastore", &name
)?
;
167 rpcenv
["digest"] = proxmox
::tools
::digest_to_hex(&digest
).into();
173 #[derive(Serialize, Deserialize)]
174 #[serde(rename_all="kebab-case")]
175 #[allow(non_camel_case_types)]
176 /// Deletable property name
177 pub enum DeletableProperty
{
178 /// Delete the comment property.
180 /// Delete the garbage collection schedule.
182 /// Delete the prune job schedule.
184 /// Delete the keep-last property
186 /// Delete the keep-hourly property
188 /// Delete the keep-daily property
190 /// Delete the keep-weekly property
192 /// Delete the keep-monthly property
194 /// Delete the keep-yearly property
196 /// Delete the verify-new property
198 /// Delete the notify-user property
200 /// Delete the notify property
209 schema
: DATASTORE_SCHEMA
,
213 schema
: SINGLE_LINE_COMMENT_SCHEMA
,
221 schema
: DATASTORE_NOTIFY_STRING_SCHEMA
,
225 schema
: GC_SCHEDULE_SCHEMA
,
229 schema
: PRUNE_SCHEDULE_SCHEMA
,
233 schema
: PRUNE_SCHEMA_KEEP_LAST
,
237 schema
: PRUNE_SCHEMA_KEEP_HOURLY
,
241 schema
: PRUNE_SCHEMA_KEEP_DAILY
,
245 schema
: PRUNE_SCHEMA_KEEP_WEEKLY
,
249 schema
: PRUNE_SCHEMA_KEEP_MONTHLY
,
253 schema
: PRUNE_SCHEMA_KEEP_YEARLY
,
256 description
: "If enabled, all new backups will be verified right after completion.",
262 description
: "List of properties to delete.",
266 type: DeletableProperty
,
271 schema
: PROXMOX_CONFIG_DIGEST_SCHEMA
,
276 permission
: &Permission
::Privilege(&["datastore", "{name}"], PRIV_DATASTORE_MODIFY
, false),
279 /// Update datastore config.
280 pub fn update_datastore(
282 comment
: Option
<String
>,
283 gc_schedule
: Option
<String
>,
284 prune_schedule
: Option
<String
>,
285 keep_last
: Option
<u64>,
286 keep_hourly
: Option
<u64>,
287 keep_daily
: Option
<u64>,
288 keep_weekly
: Option
<u64>,
289 keep_monthly
: Option
<u64>,
290 keep_yearly
: Option
<u64>,
291 verify_new
: Option
<bool
>,
292 notify
: Option
<String
>,
293 notify_user
: Option
<Userid
>,
294 delete
: Option
<Vec
<DeletableProperty
>>,
295 digest
: Option
<String
>,
296 ) -> Result
<(), Error
> {
298 let _lock
= open_file_locked(datastore
::DATASTORE_CFG_LOCKFILE
, std
::time
::Duration
::new(10, 0), true)?
;
300 // pass/compare digest
301 let (mut config
, expected_digest
) = datastore
::config()?
;
303 if let Some(ref digest
) = digest
{
304 let digest
= proxmox
::tools
::hex_to_digest(digest
)?
;
305 crate::tools
::detect_modified_configuration_file(&digest
, &expected_digest
)?
;
308 let mut data
: datastore
::DataStoreConfig
= config
.lookup("datastore", &name
)?
;
310 if let Some(delete
) = delete
{
311 for delete_prop
in delete
{
313 DeletableProperty
::comment
=> { data.comment = None; }
,
314 DeletableProperty
::gc_schedule
=> { data.gc_schedule = None; }
,
315 DeletableProperty
::prune_schedule
=> { data.prune_schedule = None; }
,
316 DeletableProperty
::keep_last
=> { data.keep_last = None; }
,
317 DeletableProperty
::keep_hourly
=> { data.keep_hourly = None; }
,
318 DeletableProperty
::keep_daily
=> { data.keep_daily = None; }
,
319 DeletableProperty
::keep_weekly
=> { data.keep_weekly = None; }
,
320 DeletableProperty
::keep_monthly
=> { data.keep_monthly = None; }
,
321 DeletableProperty
::keep_yearly
=> { data.keep_yearly = None; }
,
322 DeletableProperty
::verify_new
=> { data.verify_new = None; }
,
323 DeletableProperty
::notify
=> { data.notify = None; }
,
324 DeletableProperty
::notify_user
=> { data.notify_user = None; }
,
329 if let Some(comment
) = comment
{
330 let comment
= comment
.trim().to_string();
331 if comment
.is_empty() {
334 data
.comment
= Some(comment
);
338 let mut gc_schedule_changed
= false;
339 if gc_schedule
.is_some() {
340 gc_schedule_changed
= data
.gc_schedule
!= gc_schedule
;
341 data
.gc_schedule
= gc_schedule
;
344 let mut prune_schedule_changed
= false;
345 if prune_schedule
.is_some() {
346 prune_schedule_changed
= data
.prune_schedule
!= prune_schedule
;
347 data
.prune_schedule
= prune_schedule
;
350 if keep_last
.is_some() { data.keep_last = keep_last; }
351 if keep_hourly
.is_some() { data.keep_hourly = keep_hourly; }
352 if keep_daily
.is_some() { data.keep_daily = keep_daily; }
353 if keep_weekly
.is_some() { data.keep_weekly = keep_weekly; }
354 if keep_monthly
.is_some() { data.keep_monthly = keep_monthly; }
355 if keep_yearly
.is_some() { data.keep_yearly = keep_yearly; }
357 if let Some(notify_str
) = notify
{
358 let value
= parse_property_string(¬ify_str
, &DatastoreNotify
::API_SCHEMA
)?
;
359 let notify
: DatastoreNotify
= serde_json
::from_value(value
)?
;
360 if let DatastoreNotify { gc: None, verify: None, sync: None }
= notify
{
363 data
.notify
= Some(notify_str
);
366 if verify_new
.is_some() { data.verify_new = verify_new; }
368 if notify_user
.is_some() { data.notify_user = notify_user; }
370 config
.set_data(&name
, "datastore", &data
)?
;
372 datastore
::save_config(&config
)?
;
374 // we want to reset the statefiles, to avoid an immediate action in some cases
375 // (e.g. going from monthly to weekly in the second week of the month)
376 if gc_schedule_changed
{
377 jobstate
::create_state_file("garbage_collection", &name
)?
;
380 if prune_schedule_changed
{
381 jobstate
::create_state_file("prune", &name
)?
;
392 schema
: DATASTORE_SCHEMA
,
396 schema
: PROXMOX_CONFIG_DIGEST_SCHEMA
,
401 permission
: &Permission
::Privilege(&["datastore", "{name}"], PRIV_DATASTORE_ALLOCATE
, false),
404 /// Remove a datastore configuration.
405 pub fn delete_datastore(name
: String
, digest
: Option
<String
>) -> Result
<(), Error
> {
407 let _lock
= open_file_locked(datastore
::DATASTORE_CFG_LOCKFILE
, std
::time
::Duration
::new(10, 0), true)?
;
409 let (mut config
, expected_digest
) = datastore
::config()?
;
411 if let Some(ref digest
) = digest
{
412 let digest
= proxmox
::tools
::hex_to_digest(digest
)?
;
413 crate::tools
::detect_modified_configuration_file(&digest
, &expected_digest
)?
;
416 match config
.sections
.get(&name
) {
417 Some(_
) => { config.sections.remove(&name); }
,
418 None
=> bail
!("datastore '{}' does not exist.", name
),
421 datastore
::save_config(&config
)?
;
424 let _
= jobstate
::remove_state_file("prune", &name
);
425 let _
= jobstate
::remove_state_file("garbage_collection", &name
);
430 const ITEM_ROUTER
: Router
= Router
::new()
431 .get(&API_METHOD_READ_DATASTORE
)
432 .put(&API_METHOD_UPDATE_DATASTORE
)
433 .delete(&API_METHOD_DELETE_DATASTORE
);
435 pub const ROUTER
: Router
= Router
::new()
436 .get(&API_METHOD_LIST_DATASTORES
)
437 .post(&API_METHOD_CREATE_DATASTORE
)
438 .match_all("name", &ITEM_ROUTER
);