]> git.proxmox.com Git - proxmox-backup.git/blobdiff - src/bin/proxmox-backup-client.rs
comment on test output paths
[proxmox-backup.git] / src / bin / proxmox-backup-client.rs
index bd52abbcbf5a4782843e2012c419549ff390e0f8..11ad5417dcd581a8dfccb4536731e08de193b153 100644 (file)
@@ -6,7 +6,6 @@ use std::sync::{Arc, Mutex};
 use std::task::Context;
 
 use anyhow::{bail, format_err, Error};
-use futures::future::FutureExt;
 use futures::stream::{StreamExt, TryStreamExt};
 use serde_json::{json, Value};
 use tokio::sync::mpsc;
@@ -21,67 +20,73 @@ use proxmox::{
     },
     api::{
         api,
-        ApiHandler,
         ApiMethod,
         RpcEnvironment,
-        schema::*,
         cli::*,
     },
 };
 use pxar::accessor::{MaybeReady, ReadAt, ReadAtOperation};
 
-use proxmox_backup::tools::{
-    self,
-    StdChannelWriter,
-    TokioWriterAdapter,
+use pbs_api_types::CryptMode;
+use pbs_client::{
+    BACKUP_SOURCE_SCHEMA,
+    BackupReader,
+    BackupRepository,
+    BackupSpecificationType,
+    BackupStats,
+    BackupWriter,
+    HttpClient,
+    PxarBackupStream,
+    RemoteChunkReader,
+    UploadOptions,
+    delete_ticket_info,
+    parse_backup_specification,
+    view_task_result,
 };
+use pbs_client::catalog_shell::Shell;
+use pbs_client::tools::{
+    complete_archive_name, complete_auth_id, complete_backup_group, complete_backup_snapshot,
+    complete_backup_source, complete_chunk_size, complete_group_or_snapshot,
+    complete_img_archive_name, complete_pxar_archive_name, complete_repository, connect,
+    extract_repository_from_value,
+    key_source::{
+        crypto_parameters, format_key_source, get_encryption_key_password, KEYFD_SCHEMA,
+        KEYFILE_SCHEMA, MASTER_PUBKEY_FD_SCHEMA, MASTER_PUBKEY_FILE_SCHEMA,
+    },
+    CHUNK_SIZE_SCHEMA, REPO_URL_SCHEMA,
+};
+use pbs_datastore::CryptConfig;
+use pbs_datastore::backup_info::{BackupDir, BackupGroup};
+use pbs_datastore::catalog::BackupCatalogWriter;
+use pbs_datastore::dynamic_index::DynamicIndexReader;
+use pbs_datastore::fixed_index::FixedIndexReader;
+use pbs_datastore::index::IndexFile;
+use pbs_datastore::manifest::{MANIFEST_BLOB_NAME, ArchiveType, BackupManifest, archive_type};
+use pbs_datastore::read_chunk::AsyncReadChunk;
+use pbs_tools::sync::StdChannelWriter;
+use pbs_tools::tokio::TokioWriterAdapter;
+
 use proxmox_backup::api2::types::*;
 use proxmox_backup::api2::version;
-use proxmox_backup::client::*;
-use proxmox_backup::pxar::catalog::*;
 use proxmox_backup::backup::{
-    archive_type,
     decrypt_key,
     rsa_encrypt_key_config,
     verify_chunk_size,
-    ArchiveType,
-    AsyncReadChunk,
-    BackupDir,
-    BackupGroup,
-    BackupManifest,
     BufferedDynamicReader,
     CATALOG_NAME,
     CatalogReader,
     CatalogWriter,
     ChunkStream,
-    CryptConfig,
-    CryptMode,
-    DynamicIndexReader,
     ENCRYPTED_KEY_BLOB_NAME,
     FixedChunkStream,
-    FixedIndexReader,
     KeyConfig,
-    IndexFile,
-    MANIFEST_BLOB_NAME,
-    Shell,
+    PruneOptions,
 };
+use proxmox_backup::tools;
 
 mod proxmox_backup_client;
 use proxmox_backup_client::*;
 
-pub mod proxmox_client_tools;
-use proxmox_client_tools::{
-    complete_archive_name, complete_auth_id, complete_backup_group, complete_backup_snapshot,
-    complete_backup_source, complete_chunk_size, complete_group_or_snapshot,
-    complete_img_archive_name, complete_pxar_archive_name, complete_repository, connect,
-    extract_repository_from_value,
-    key_source::{
-        crypto_parameters, format_key_source, get_encryption_key_password, KEYFD_SCHEMA,
-        KEYFILE_SCHEMA, MASTER_PUBKEY_FD_SCHEMA, MASTER_PUBKEY_FILE_SCHEMA,
-    },
-    CHUNK_SIZE_SCHEMA, REPO_URL_SCHEMA,
-};
-
 fn record_repository(repo: &BackupRepository) {
 
     let base = match BaseDirectories::with_prefix("proxmox-backup") {
@@ -175,7 +180,7 @@ async fn backup_directory<P: AsRef<Path>>(
     archive_name: &str,
     chunk_size: Option<usize>,
     catalog: Arc<Mutex<CatalogWriter<TokioWriterAdapter<StdChannelWriter>>>>,
-    pxar_create_options: proxmox_backup::pxar::PxarCreateOptions,
+    pxar_create_options: pbs_client::pxar::PxarCreateOptions,
     upload_options: UploadOptions,
 ) -> Result<BackupStats, Error> {
 
@@ -280,7 +285,7 @@ async fn list_backup_groups(param: Value) -> Result<Value, Error> {
 
     let render_files = |_v: &Value, record: &Value| -> Result<String, Error> {
         let item: GroupListItem = serde_json::from_value(record.to_owned())?;
-        Ok(tools::format::render_backup_file_list(&item.files))
+        Ok(pbs_tools::format::render_backup_file_list(&item.files))
     };
 
     let options = default_table_format_options()
@@ -592,7 +597,7 @@ fn spawn_catalog_upload(
                type: Integer,
                description: "Max number of entries to hold in memory.",
                optional: true,
-               default: proxmox_backup::pxar::ENCODER_MAX_ENTRIES as isize,
+               default: pbs_client::pxar::ENCODER_MAX_ENTRIES as isize,
            },
            "verbose": {
                type: Boolean,
@@ -636,7 +641,7 @@ async fn create_backup(
     let include_dev = param["include-dev"].as_array();
 
     let entries_max = param["entries-max"].as_u64()
-        .unwrap_or(proxmox_backup::pxar::ENCODER_MAX_ENTRIES as u64);
+        .unwrap_or(pbs_client::pxar::ENCODER_MAX_ENTRIES as u64);
 
     let empty = Vec::new();
     let exclude_args = param["exclude"].as_array().unwrap_or(&empty);
@@ -859,7 +864,7 @@ async fn create_backup(
                 println!("Upload directory '{}' to '{}' as {}", filename, repo, target);
                 catalog.lock().unwrap().start_directory(std::ffi::CString::new(target.as_str())?.as_c_str())?;
 
-                let pxar_options = proxmox_backup::pxar::PxarCreateOptions {
+                let pxar_options = pbs_client::pxar::PxarCreateOptions {
                     device_set: devices.clone(),
                     patterns: pattern_list.clone(),
                     entries_max: entries_max as usize,
@@ -1171,7 +1176,7 @@ async fn restore(param: Value) -> Result<Value, Error> {
 
         let mut reader = BufferedDynamicReader::new(index, chunk_reader);
 
-        let options = proxmox_backup::pxar::PxarExtractOptions {
+        let options = pbs_client::pxar::PxarExtractOptions {
             match_list: &[],
             extract_match_default: true,
             allow_existing_dirs,
@@ -1179,10 +1184,10 @@ async fn restore(param: Value) -> Result<Value, Error> {
         };
 
         if let Some(target) = target {
-            proxmox_backup::pxar::extract_archive(
+            pbs_client::pxar::extract_archive(
                 pxar::decoder::Decoder::from_std(reader)?,
                 Path::new(target),
-                proxmox_backup::pxar::Flags::DEFAULT,
+                pbs_client::pxar::Flags::DEFAULT,
                 |path| {
                     if verbose {
                         println!("{:?}", path);
@@ -1224,60 +1229,65 @@ async fn restore(param: Value) -> Result<Value, Error> {
     Ok(Value::Null)
 }
 
-const API_METHOD_PRUNE: ApiMethod = ApiMethod::new(
-    &ApiHandler::Async(&prune),
-    &ObjectSchema::new(
-        "Prune a backup repository.",
-        &proxmox_backup::add_common_prune_prameters!([
-            ("dry-run", true, &BooleanSchema::new(
-                "Just show what prune would do, but do not delete anything.")
-             .schema()),
-            ("group", false, &StringSchema::new("Backup group.").schema()),
-        ], [
-            ("output-format", true, &OUTPUT_FORMAT),
-            (
-                "quiet",
-                true,
-                &BooleanSchema::new("Minimal output - only show removals.")
-                    .schema()
-            ),
-            ("repository", true, &REPO_URL_SCHEMA),
-        ])
-    )
-);
-
-fn prune<'a>(
-    param: Value,
-    _info: &ApiMethod,
-    _rpcenv: &'a mut dyn RpcEnvironment,
-) -> proxmox::api::ApiFuture<'a> {
-    async move {
-        prune_async(param).await
-    }.boxed()
-}
-
-async fn prune_async(mut param: Value) -> Result<Value, Error> {
+#[api(
+    input: {
+        properties: {
+            "dry-run": {
+                type: bool,
+                optional: true,
+                description: "Just show what prune would do, but do not delete anything.",
+            },
+            group: {
+                type: String,
+                description: "Backup group",
+            },
+            "prune-options": {
+                type: PruneOptions,
+                flatten: true,
+            },
+            "output-format": {
+                schema: OUTPUT_FORMAT,
+                optional: true,
+            },
+            quiet: {
+                type: bool,
+                optional: true,
+                default: false,
+                description: "Minimal output - only show removals.",
+            },
+            repository: {
+                schema: REPO_URL_SCHEMA,
+                optional: true,
+            },
+        },
+    },
+)]
+/// Prune a backup repository.
+async fn prune(
+    dry_run: Option<bool>,
+    group: String,
+    prune_options: PruneOptions,
+    quiet: bool,
+    mut param: Value
+) -> Result<Value, Error> {
     let repo = extract_repository_from_value(&param)?;
 
     let mut client = connect(&repo)?;
 
     let path = format!("api2/json/admin/datastore/{}/prune", repo.store());
 
-    let group = tools::required_string_param(&param, "group")?;
     let group: BackupGroup = group.parse()?;
 
     let output_format = extract_output_format(&mut param);
 
-    let quiet = param["quiet"].as_bool().unwrap_or(false);
-
-    param.as_object_mut().unwrap().remove("repository");
-    param.as_object_mut().unwrap().remove("group");
-    param.as_object_mut().unwrap().remove("quiet");
-
-    param["backup-type"] = group.backup_type().into();
-    param["backup-id"] = group.backup_id().into();
+    let mut api_param = serde_json::to_value(prune_options)?;
+    if let Some(dry_run) = dry_run {
+        api_param["dry-run"] = dry_run.into();
+    }
+    api_param["backup-type"] = group.backup_type().into();
+    api_param["backup-id"] = group.backup_id().into();
 
-    let mut result = client.post(&path, Some(param)).await?;
+    let mut result = client.post(&path, Some(api_param)).await?;
 
     record_repository(&repo);
 
@@ -1300,7 +1310,7 @@ async fn prune_async(mut param: Value) -> Result<Value, Error> {
         .sortby("backup-id", false)
         .sortby("backup-time", false)
         .column(ColumnConfig::new("backup-id").renderer(render_snapshot_path).header("snapshot"))
-        .column(ColumnConfig::new("backup-time").renderer(tools::format::render_epoch).header("date"))
+        .column(ColumnConfig::new("backup-time").renderer(pbs_tools::format::render_epoch).header("date"))
         .column(ColumnConfig::new("keep").renderer(render_prune_action).header("action"))
         ;
 
@@ -1375,7 +1385,6 @@ async fn status(param: Value) -> Result<Value, Error> {
     Ok(Value::Null)
 }
 
-use proxmox_backup::client::RemoteChunkReader;
 /// This is a workaround until we have cleaned up the chunk/reader/... infrastructure for better
 /// async use!
 ///
@@ -1422,13 +1431,13 @@ fn main() {
         .arg_param(&["backupspec"])
         .completion_cb("repository", complete_repository)
         .completion_cb("backupspec", complete_backup_source)
-        .completion_cb("keyfile", tools::complete_file_name)
-        .completion_cb("master-pubkey-file", tools::complete_file_name)
+        .completion_cb("keyfile", pbs_tools::fs::complete_file_name)
+        .completion_cb("master-pubkey-file", pbs_tools::fs::complete_file_name)
         .completion_cb("chunk-size", complete_chunk_size);
 
     let benchmark_cmd_def = CliCommand::new(&API_METHOD_BENCHMARK)
         .completion_cb("repository", complete_repository)
-        .completion_cb("keyfile", tools::complete_file_name);
+        .completion_cb("keyfile", pbs_tools::fs::complete_file_name);
 
     let list_cmd_def = CliCommand::new(&API_METHOD_LIST_BACKUP_GROUPS)
         .completion_cb("repository", complete_repository);
@@ -1441,7 +1450,7 @@ fn main() {
         .completion_cb("repository", complete_repository)
         .completion_cb("snapshot", complete_group_or_snapshot)
         .completion_cb("archive-name", complete_archive_name)
-        .completion_cb("target", tools::complete_file_name);
+        .completion_cb("target", pbs_tools::fs::complete_file_name);
 
     let prune_cmd_def = CliCommand::new(&API_METHOD_PRUNE)
         .arg_param(&["group"])