]> git.proxmox.com Git - proxmox-backup.git/blobdiff - src/bin/proxmox-backup-manager.rs
move required_X_param to pbs_tools::json
[proxmox-backup.git] / src / bin / proxmox-backup-manager.rs
index 80e699f107802d6b865a7722f77a4b72ea8aac2b..93d6de57b4a8f9daf805a060c5b45f93b6f16ad9 100644 (file)
@@ -6,72 +6,17 @@ use serde_json::{json, Value};
 
 use proxmox::api::{api, cli::*, RpcEnvironment};
 
-use proxmox_backup::tools;
+use pbs_client::{connect_to_localhost, display_task_log, view_task_result};
+use pbs_tools::percent_encoding::percent_encode_component;
+use pbs_tools::json::required_string_param;
+
 use proxmox_backup::config;
 use proxmox_backup::api2::{self, types::* };
-use proxmox_backup::client::*;
-use proxmox_backup::tools::ticket::Ticket;
-use proxmox_backup::auth_helpers::*;
+use proxmox_backup::server::wait_for_local_worker;
 
 mod proxmox_backup_manager;
 use proxmox_backup_manager::*;
 
-async fn view_task_result(
-    client: HttpClient,
-    result: Value,
-    output_format: &str,
-) -> Result<(), Error> {
-    let data = &result["data"];
-    if output_format == "text" {
-        if let Some(upid) = data.as_str() {
-            display_task_log(client, upid, true).await?;
-        }
-    } else {
-        format_and_print_result(&data, &output_format);
-    }
-
-    Ok(())
-}
-
-// Note: local workers should print logs to stdout, so there is no need
-// to fetch/display logs. We just wait for the worker to finish.
-pub async fn wait_for_local_worker(upid_str: &str) -> Result<(), Error> {
-
-    let upid: proxmox_backup::server::UPID = upid_str.parse()?;
-
-    let sleep_duration = core::time::Duration::new(0, 100_000_000);
-
-    loop {
-        if proxmox_backup::server::worker_is_active_local(&upid) {
-            tokio::time::delay_for(sleep_duration).await;
-        } else {
-            break;
-        }
-    }
-    Ok(())
-}
-
-fn connect() -> Result<HttpClient, Error> {
-
-    let uid = nix::unistd::Uid::current();
-
-    let mut options = HttpClientOptions::new()
-        .prefix(Some("proxmox-backup".to_string()))
-        .verify_cert(false); // not required for connection to localhost
-
-    let client = if uid.is_root()  {
-        let ticket = Ticket::new("PBS", Userid::root_userid())?
-            .sign(private_auth_key(), None)?;
-        options = options.password(Some(ticket));
-        HttpClient::new("localhost", 8007, Authid::root_auth_id(), options)?
-    } else {
-        options = options.ticket_cache(true).interactive(true);
-        HttpClient::new("localhost", 8007, Authid::root_auth_id(), options)?
-    };
-
-    Ok(client)
-}
-
 #[api(
    input: {
         properties: {
@@ -90,15 +35,15 @@ async fn start_garbage_collection(param: Value) -> Result<Value, Error> {
 
     let output_format = get_output_format(&param);
 
-    let store = tools::required_string_param(&param, "store")?;
+    let store = required_string_param(&param, "store")?;
 
-    let mut client = connect()?;
+    let mut client = connect_to_localhost()?;
 
     let path = format!("api2/json/admin/datastore/{}/gc", store);
 
     let result = client.post(&path, None).await?;
 
-    view_task_result(client, result, &output_format).await?;
+    view_task_result(&mut client, result, &output_format).await?;
 
     Ok(Value::Null)
 }
@@ -121,19 +66,19 @@ async fn garbage_collection_status(param: Value) -> Result<Value, Error> {
 
     let output_format = get_output_format(&param);
 
-    let store = tools::required_string_param(&param, "store")?;
+    let store = required_string_param(&param, "store")?;
 
-    let client = connect()?;
+    let client = connect_to_localhost()?;
 
     let path = format!("api2/json/admin/datastore/{}/gc", store);
 
     let mut result = client.get(&path, None).await?;
     let mut data = result["data"].take();
-    let schema = &api2::admin::datastore::API_RETURN_SCHEMA_GARBAGE_COLLECTION_STATUS;
+    let return_type = &api2::admin::datastore::API_METHOD_GARBAGE_COLLECTION_STATUS.returns;
 
     let options = default_table_format_options();
 
-    format_and_print_result_full(&mut data, schema, &output_format, &options);
+    format_and_print_result_full(&mut data, return_type, &output_format, &options);
 
     Ok(Value::Null)
 }
@@ -183,7 +128,7 @@ async fn task_list(param: Value) -> Result<Value, Error> {
 
     let output_format = get_output_format(&param);
 
-    let client = connect()?;
+    let client = connect_to_localhost()?;
 
     let limit = param["limit"].as_u64().unwrap_or(50) as usize;
     let running = !param["all"].as_bool().unwrap_or(false);
@@ -195,15 +140,16 @@ async fn task_list(param: Value) -> Result<Value, Error> {
     let mut result = client.get("api2/json/nodes/localhost/tasks", Some(args)).await?;
 
     let mut data = result["data"].take();
-    let schema = &api2::node::tasks::API_RETURN_SCHEMA_LIST_TASKS;
+    let return_type = &api2::node::tasks::API_METHOD_LIST_TASKS.returns;
 
+    use pbs_tools::format::{render_epoch, render_task_status};
     let options = default_table_format_options()
-        .column(ColumnConfig::new("starttime").right_align(false).renderer(tools::format::render_epoch))
-        .column(ColumnConfig::new("endtime").right_align(false).renderer(tools::format::render_epoch))
+        .column(ColumnConfig::new("starttime").right_align(false).renderer(render_epoch))
+        .column(ColumnConfig::new("endtime").right_align(false).renderer(render_epoch))
         .column(ColumnConfig::new("upid"))
-        .column(ColumnConfig::new("status").renderer(tools::format::render_task_status));
+        .column(ColumnConfig::new("status").renderer(render_task_status));
 
-    format_and_print_result_full(&mut data, schema, &output_format, &options);
+    format_and_print_result_full(&mut data, return_type, &output_format, &options);
 
     Ok(Value::Null)
 }
@@ -220,11 +166,11 @@ async fn task_list(param: Value) -> Result<Value, Error> {
 /// Display the task log.
 async fn task_log(param: Value) -> Result<Value, Error> {
 
-    let upid = tools::required_string_param(&param, "upid")?;
+    let upid = required_string_param(&param, "upid")?;
 
-    let client = connect()?;
+    let mut client = connect_to_localhost()?;
 
-    display_task_log(client, upid, true).await?;
+    display_task_log(&mut client, upid, true).await?;
 
     Ok(Value::Null)
 }
@@ -241,11 +187,11 @@ async fn task_log(param: Value) -> Result<Value, Error> {
 /// Try to stop a specific task.
 async fn task_stop(param: Value) -> Result<Value, Error> {
 
-    let upid_str = tools::required_string_param(&param, "upid")?;
+    let upid_str = required_string_param(&param, "upid")?;
 
-    let mut client = connect()?;
+    let mut client = connect_to_localhost()?;
 
-    let path = format!("api2/json/nodes/localhost/tasks/{}", upid_str);
+    let path = format!("api2/json/nodes/localhost/tasks/{}", percent_encode_component(upid_str));
     let _ = client.delete(&path, None).await?;
 
     Ok(Value::Null)
@@ -302,7 +248,7 @@ async fn pull_datastore(
 
     let output_format = get_output_format(&param);
 
-    let mut client = connect()?;
+    let mut client = connect_to_localhost()?;
 
     let mut args = json!({
         "store": local_store,
@@ -316,7 +262,7 @@ async fn pull_datastore(
 
     let result = client.post("api2/json/pull", Some(args)).await?;
 
-    view_task_result(client, result, &output_format).await?;
+    view_task_result(&mut client, result, &output_format).await?;
 
     Ok(Value::Null)
 }
@@ -327,6 +273,14 @@ async fn pull_datastore(
             "store": {
                 schema: DATASTORE_SCHEMA,
             },
+            "ignore-verified": {
+                schema: IGNORE_VERIFIED_BACKUPS_SCHEMA,
+                optional: true,
+            },
+            "outdated-after": {
+                schema: VERIFICATION_OUTDATED_AFTER_SCHEMA,
+                optional: true,
+            },
             "output-format": {
                 schema: OUTPUT_FORMAT,
                 optional: true,
@@ -337,20 +291,20 @@ async fn pull_datastore(
 /// Verify backups
 async fn verify(
     store: String,
-    param: Value,
+    mut param: Value,
 ) -> Result<Value, Error> {
 
-    let output_format = get_output_format(&param);
+    let output_format = extract_output_format(&mut param);
 
-    let mut client = connect()?;
+    let mut client = connect_to_localhost()?;
 
-    let args = json!({});
+    let args = json!(param);
 
     let path = format!("api2/json/admin/datastore/{}/verify", store);
 
     let result = client.post(&path, Some(args)).await?;
 
-    view_task_result(client, result, &output_format).await?;
+    view_task_result(&mut client, result, &output_format).await?;
 
     Ok(Value::Null)
 }
@@ -384,18 +338,18 @@ async fn get_versions(verbose: bool, param: Value) -> Result<Value, Error> {
     let output_format = get_output_format(&param);
 
     let packages = crate::api2::node::apt::get_versions()?;
-    let mut packages = json!(if verbose { &packages[..] } else { &packages[0..1] });
+    let mut packages = json!(if verbose { &packages[..] } else { &packages[1..2] });
 
     let options = default_table_format_options()
         .disable_sort()
-        .noborder(true) // just not helpfull for version info which gets copy pasted often
+        .noborder(true) // just not helpful for version info which gets copy pasted often
         .column(ColumnConfig::new("Package"))
         .column(ColumnConfig::new("Version"))
         .column(ColumnConfig::new("ExtraInfo").header("Extra Info"))
         ;
-    let schema = &crate::api2::node::apt::API_RETURN_SCHEMA_GET_VERSIONS;
+    let return_type = &crate::api2::node::apt::API_METHOD_GET_VERSIONS.returns;
 
-    format_and_print_result_full(&mut packages, schema, &output_format, &options);
+    format_and_print_result_full(&mut packages, return_type, &output_format, &options);
 
     Ok(Value::Null)
 }
@@ -410,12 +364,16 @@ fn main() {
         .insert("disk", disk_commands())
         .insert("dns", dns_commands())
         .insert("network", network_commands())
+        .insert("node", node_commands())
         .insert("user", user_commands())
+        .insert("openid", openid_commands())
         .insert("remote", remote_commands())
         .insert("garbage-collection", garbage_collection_commands())
+        .insert("acme", acme_mgmt_cli())
         .insert("cert", cert_mgmt_cli())
         .insert("subscription", subscription_commands())
         .insert("sync-job", sync_job_commands())
+        .insert("verify-job", verify_job_commands())
         .insert("task", task_mgmt_cli())
         .insert(
             "pull",
@@ -443,7 +401,7 @@ fn main() {
     let mut rpcenv = CliEnvironment::new();
     rpcenv.set_auth_id(Some(String::from("root@pam")));
 
-   proxmox_backup::tools::runtime::main(run_async_cli_command(cmd_def, rpcenv));
+   pbs_runtime::main(run_async_cli_command(cmd_def, rpcenv));
 }
 
 // shell completion helper
@@ -454,7 +412,7 @@ pub fn complete_remote_datastore_name(_arg: &str, param: &HashMap<String, String
     let _ = proxmox::try_block!({
         let remote = param.get("remote").ok_or_else(|| format_err!("no remote"))?;
 
-        let data = crate::tools::runtime::block_on(async move {
+        let data = pbs_runtime::block_on(async move {
             crate::api2::config::remote::scan_remote_datastores(remote.clone()).await
         })?;