[pbs-devel] [PATCH proxmox-backup 1/2] config: add default datastore creation function

Gabriel Goller g.goller at proxmox.com
Fri May 3 16:29:28 CEST 2024


Add a function to create a default datastore. This datastore will have a
default prune-job with a daily schedule and a gc-job with a daily
schedule as well.

Signed-off-by: Gabriel Goller <g.goller at proxmox.com>
---
 src/api2/config/datastore.rs | 92 ++++++++++++++++++++----------------
 1 file changed, 50 insertions(+), 42 deletions(-)

diff --git a/src/api2/config/datastore.rs b/src/api2/config/datastore.rs
index 6b742acb..6cb3b21a 100644
--- a/src/api2/config/datastore.rs
+++ b/src/api2/config/datastore.rs
@@ -1,4 +1,5 @@
 use std::path::PathBuf;
+use std::sync::Arc;
 
 use ::serde::{Deserialize, Serialize};
 use anyhow::Error;
@@ -66,6 +67,54 @@ pub fn list_datastores(
     Ok(list.into_iter().filter(filter_by_privs).collect())
 }
 
+/// Creates a default datastore with the provided config.
+/// Also adds a gc schedule (daily) and the default prune-job (daily).
+pub(crate) fn create_default_datastore(
+    config: DataStoreConfig,
+    worker: Arc<WorkerTask>,
+) -> Result<(), Error> {
+    let lock = pbs_config::datastore::lock_config()?;
+
+    let (section_config, _digest) = pbs_config::datastore::config()?;
+
+    if section_config.sections.get(&config.name).is_some() {
+        param_bail!("name", "datastore '{}' already exists.", config.name);
+    }
+
+    let prune_job_config = config.prune_schedule.as_ref().map(|schedule| {
+        let mut id = format!("default-{}-{}", config.name, Uuid::generate());
+        id.truncate(32);
+
+        PruneJobConfig {
+            id,
+            store: config.name.clone(),
+            comment: None,
+            disable: false,
+            schedule: schedule.clone(),
+            options: PruneJobOptions {
+                keep: config.keep.clone(),
+                max_depth: None,
+                ns: None,
+            },
+        }
+    });
+
+    // clearing prune settings in the datastore config, as they are now handled by prune jobs
+    let config = DataStoreConfig {
+        prune_schedule: None,
+        keep: KeepOptions::default(),
+        ..config
+    };
+
+    do_create_datastore(lock, section_config, config, Some(&worker))?;
+
+    if let Some(prune_job_config) = prune_job_config {
+        do_create_prune_job(prune_job_config, Some(&worker))
+    } else {
+        Ok(())
+    }
+}
+
 pub(crate) fn do_create_datastore(
     _lock: BackupLockGuard,
     mut config: SectionConfigData,
@@ -114,56 +163,15 @@ pub fn create_datastore(
     config: DataStoreConfig,
     rpcenv: &mut dyn RpcEnvironment,
 ) -> Result<String, Error> {
-    let lock = pbs_config::datastore::lock_config()?;
-
-    let (section_config, _digest) = pbs_config::datastore::config()?;
-
-    if section_config.sections.get(&config.name).is_some() {
-        param_bail!("name", "datastore '{}' already exists.", config.name);
-    }
-
     let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
     let to_stdout = rpcenv.env_type() == RpcEnvironmentType::CLI;
 
-    let prune_job_config = config.prune_schedule.as_ref().map(|schedule| {
-        let mut id = format!("default-{}-{}", config.name, Uuid::generate());
-        id.truncate(32);
-
-        PruneJobConfig {
-            id,
-            store: config.name.clone(),
-            comment: None,
-            disable: false,
-            schedule: schedule.clone(),
-            options: PruneJobOptions {
-                keep: config.keep.clone(),
-                max_depth: None,
-                ns: None,
-            },
-        }
-    });
-
-    // clearing prune settings in the datastore config, as they are now handled by prune jobs
-    let config = DataStoreConfig {
-        prune_schedule: None,
-        keep: KeepOptions::default(),
-        ..config
-    };
-
     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))?;
-
-            if let Some(prune_job_config) = prune_job_config {
-                do_create_prune_job(prune_job_config, Some(&worker))
-            } else {
-                Ok(())
-            }
-        },
+        move |worker| create_default_datastore(config, worker),
     )
 }
 
-- 
2.43.0





More information about the pbs-devel mailing list