From cc7995ac40c17d6f039b62932a2092faa79a3888 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 2 Jul 2020 18:04:29 +0200 Subject: [PATCH] src/bin/proxmox_backup_client/task.rs: split out task command --- src/bin/proxmox-backup-client.rs | 132 ---------------------- src/bin/proxmox_backup_client/mod.rs | 2 + src/bin/proxmox_backup_client/mount.rs | 3 +- src/bin/proxmox_backup_client/task.rs | 148 +++++++++++++++++++++++++ 4 files changed, 151 insertions(+), 134 deletions(-) create mode 100644 src/bin/proxmox_backup_client/task.rs diff --git a/src/bin/proxmox-backup-client.rs b/src/bin/proxmox-backup-client.rs index 0d79e5e1..ff83240b 100644 --- a/src/bin/proxmox-backup-client.rs +++ b/src/bin/proxmox-backup-client.rs @@ -2160,138 +2160,6 @@ fn catalog_mgmt_cli() -> CliCommandMap { .insert("shell", catalog_shell_cmd_def) } -#[api( - input: { - properties: { - repository: { - schema: REPO_URL_SCHEMA, - optional: true, - }, - limit: { - description: "The maximal number of tasks to list.", - type: Integer, - optional: true, - minimum: 1, - maximum: 1000, - default: 50, - }, - "output-format": { - schema: OUTPUT_FORMAT, - optional: true, - }, - all: { - type: Boolean, - description: "Also list stopped tasks.", - optional: true, - }, - } - } -)] -/// List running server tasks for this repo user -async fn task_list(param: Value) -> Result { - - let output_format = get_output_format(¶m); - - let repo = extract_repository_from_value(¶m)?; - let client = connect(repo.host(), repo.user())?; - - let limit = param["limit"].as_u64().unwrap_or(50) as usize; - let running = !param["all"].as_bool().unwrap_or(false); - - let args = json!({ - "running": running, - "start": 0, - "limit": limit, - "userfilter": repo.user(), - "store": repo.store(), - }); - - let mut result = client.get("api2/json/nodes/localhost/tasks", Some(args)).await?; - let mut data = result["data"].take(); - - let schema = &proxmox_backup::api2::node::tasks::API_RETURN_SCHEMA_LIST_TASKS; - - 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("upid")) - .column(ColumnConfig::new("status").renderer(tools::format::render_task_status)); - - format_and_print_result_full(&mut data, schema, &output_format, &options); - - Ok(Value::Null) -} - -#[api( - input: { - properties: { - repository: { - schema: REPO_URL_SCHEMA, - optional: true, - }, - upid: { - schema: UPID_SCHEMA, - }, - } - } -)] -/// Display the task log. -async fn task_log(param: Value) -> Result { - - let repo = extract_repository_from_value(¶m)?; - let upid = tools::required_string_param(¶m, "upid")?; - - let client = connect(repo.host(), repo.user())?; - - display_task_log(client, upid, true).await?; - - Ok(Value::Null) -} - -#[api( - input: { - properties: { - repository: { - schema: REPO_URL_SCHEMA, - optional: true, - }, - upid: { - schema: UPID_SCHEMA, - }, - } - } -)] -/// Try to stop a specific task. -async fn task_stop(param: Value) -> Result { - - let repo = extract_repository_from_value(¶m)?; - let upid_str = tools::required_string_param(¶m, "upid")?; - - let mut client = connect(repo.host(), repo.user())?; - - let path = format!("api2/json/nodes/localhost/tasks/{}", upid_str); - let _ = client.delete(&path, None).await?; - - Ok(Value::Null) -} - -fn task_mgmt_cli() -> CliCommandMap { - - let task_list_cmd_def = CliCommand::new(&API_METHOD_TASK_LIST) - .completion_cb("repository", complete_repository); - - let task_log_cmd_def = CliCommand::new(&API_METHOD_TASK_LOG) - .arg_param(&["upid"]); - - let task_stop_cmd_def = CliCommand::new(&API_METHOD_TASK_STOP) - .arg_param(&["upid"]); - - CliCommandMap::new() - .insert("log", task_log_cmd_def) - .insert("list", task_list_cmd_def) - .insert("stop", task_stop_cmd_def) -} - fn main() { let backup_cmd_def = CliCommand::new(&API_METHOD_CREATE_BACKUP) diff --git a/src/bin/proxmox_backup_client/mod.rs b/src/bin/proxmox_backup_client/mod.rs index 0e245603..a5517005 100644 --- a/src/bin/proxmox_backup_client/mod.rs +++ b/src/bin/proxmox_backup_client/mod.rs @@ -2,3 +2,5 @@ mod benchmark; pub use benchmark::*; mod mount; pub use mount::*; +mod task; +pub use task::*; diff --git a/src/bin/proxmox_backup_client/mount.rs b/src/bin/proxmox_backup_client/mount.rs index 2231b056..19772b6c 100644 --- a/src/bin/proxmox_backup_client/mount.rs +++ b/src/bin/proxmox_backup_client/mount.rs @@ -12,7 +12,7 @@ use futures::select; use futures::future::FutureExt; use proxmox::{sortable, identity}; -use proxmox::api::{ApiHandler, ApiMethod, RpcEnvironment, schema::*}; +use proxmox::api::{ApiHandler, ApiMethod, RpcEnvironment, schema::*, cli::*}; use proxmox_backup::tools; @@ -26,7 +26,6 @@ use proxmox_backup::backup::{ }; use proxmox_backup::client::*; -use proxmox::api::cli::*; use crate::{ REPO_URL_SCHEMA, diff --git a/src/bin/proxmox_backup_client/task.rs b/src/bin/proxmox_backup_client/task.rs new file mode 100644 index 00000000..96a28be9 --- /dev/null +++ b/src/bin/proxmox_backup_client/task.rs @@ -0,0 +1,148 @@ +use anyhow::{Error}; +use serde_json::{json, Value}; + +use proxmox::api::{api, cli::*}; + +use proxmox_backup::tools; + +use proxmox_backup::client::*; +use proxmox_backup::api2::types::UPID_SCHEMA; + +use crate::{ + REPO_URL_SCHEMA, + extract_repository_from_value, + complete_repository, + connect, +}; + +#[api( + input: { + properties: { + repository: { + schema: REPO_URL_SCHEMA, + optional: true, + }, + limit: { + description: "The maximal number of tasks to list.", + type: Integer, + optional: true, + minimum: 1, + maximum: 1000, + default: 50, + }, + "output-format": { + schema: OUTPUT_FORMAT, + optional: true, + }, + all: { + type: Boolean, + description: "Also list stopped tasks.", + optional: true, + }, + } + } +)] +/// List running server tasks for this repo user +async fn task_list(param: Value) -> Result { + + let output_format = get_output_format(¶m); + + let repo = extract_repository_from_value(¶m)?; + let client = connect(repo.host(), repo.user())?; + + let limit = param["limit"].as_u64().unwrap_or(50) as usize; + let running = !param["all"].as_bool().unwrap_or(false); + + let args = json!({ + "running": running, + "start": 0, + "limit": limit, + "userfilter": repo.user(), + "store": repo.store(), + }); + + let mut result = client.get("api2/json/nodes/localhost/tasks", Some(args)).await?; + let mut data = result["data"].take(); + + let schema = &proxmox_backup::api2::node::tasks::API_RETURN_SCHEMA_LIST_TASKS; + + 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("upid")) + .column(ColumnConfig::new("status").renderer(tools::format::render_task_status)); + + format_and_print_result_full(&mut data, schema, &output_format, &options); + + Ok(Value::Null) +} + +#[api( + input: { + properties: { + repository: { + schema: REPO_URL_SCHEMA, + optional: true, + }, + upid: { + schema: UPID_SCHEMA, + }, + } + } +)] +/// Display the task log. +async fn task_log(param: Value) -> Result { + + let repo = extract_repository_from_value(¶m)?; + let upid = tools::required_string_param(¶m, "upid")?; + + let client = connect(repo.host(), repo.user())?; + + display_task_log(client, upid, true).await?; + + Ok(Value::Null) +} + +#[api( + input: { + properties: { + repository: { + schema: REPO_URL_SCHEMA, + optional: true, + }, + upid: { + schema: UPID_SCHEMA, + }, + } + } +)] +/// Try to stop a specific task. +async fn task_stop(param: Value) -> Result { + + let repo = extract_repository_from_value(¶m)?; + let upid_str = tools::required_string_param(¶m, "upid")?; + + let mut client = connect(repo.host(), repo.user())?; + + let path = format!("api2/json/nodes/localhost/tasks/{}", upid_str); + let _ = client.delete(&path, None).await?; + + Ok(Value::Null) +} + +pub fn task_mgmt_cli() -> CliCommandMap { + + let task_list_cmd_def = CliCommand::new(&API_METHOD_TASK_LIST) + .completion_cb("repository", complete_repository); + + let task_log_cmd_def = CliCommand::new(&API_METHOD_TASK_LOG) + .arg_param(&["upid"]); + + let task_stop_cmd_def = CliCommand::new(&API_METHOD_TASK_STOP) + .arg_param(&["upid"]); + + CliCommandMap::new() + .insert("log", task_log_cmd_def) + .insert("list", task_list_cmd_def) + .insert("stop", task_stop_cmd_def) +}