From 5830c20560f891a75fc5b60145b36f778fca572e Mon Sep 17 00:00:00 2001 From: Dietmar Maurer <dietmar@proxmox.com> Date: Tue, 10 Dec 2019 13:43:53 +0100 Subject: [PATCH] src/bin/proxmox-backup-client.rs: add simple task management cli --- src/api2/node/tasks.rs | 4 -- src/api2/types.rs | 4 ++ src/bin/proxmox-backup-client.rs | 102 ++++++++++++++++++++++++++++++- 3 files changed, 105 insertions(+), 5 deletions(-) diff --git a/src/api2/node/tasks.rs b/src/api2/node/tasks.rs index f06c1099..d7f45eec 100644 --- a/src/api2/node/tasks.rs +++ b/src/api2/node/tasks.rs @@ -178,10 +178,6 @@ fn list_tasks( Ok(json!(result)) } -const UPID_SCHEMA: Schema = StringSchema::new("Unique Process/Task ID.") - .max_length(256) - .schema(); - #[sortable] const UPID_API_SUBDIRS: SubdirMap = &[ ( diff --git a/src/api2/types.rs b/src/api2/types.rs index d7dca592..ac4c6979 100644 --- a/src/api2/types.rs +++ b/src/api2/types.rs @@ -97,3 +97,7 @@ pub const BACKUP_TIME_SCHEMA: Schema = IntegerSchema::new("Backup time (Unix epoch.)") .minimum(1_547_797_308) .schema(); + +pub const UPID_SCHEMA: Schema = StringSchema::new("Unique Process/Task ID.") + .max_length(256) + .schema(); diff --git a/src/bin/proxmox-backup-client.rs b/src/bin/proxmox-backup-client.rs index fd780575..f84d8b2f 100644 --- a/src/bin/proxmox-backup-client.rs +++ b/src/bin/proxmox-backup-client.rs @@ -13,6 +13,7 @@ use proxmox::tools::fs::{file_get_contents, file_get_json, file_set_contents, im use proxmox::api::{ApiHandler, ApiMethod, RpcEnvironment}; use proxmox::api::schema::*; use proxmox::api::cli::*; +use proxmox::api::api; use proxmox_backup::tools; use proxmox_backup::api2::types::*; @@ -1947,6 +1948,104 @@ 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, + }, + } + } +)] +/// List running server tasks for this repo user +fn task_list(param: Value) -> Result<Value, Error> { + + async_main(async { + let output_format = param["output-format"].as_str().unwrap_or("text").to_owned(); + let repo = extract_repository_from_value(¶m)?; + let client = HttpClient::new(repo.host(), repo.user(), None)?; + + let limit = param["limit"].as_u64().unwrap_or(50) as usize; + + let args = json!({ "start": 0, "limit": limit, "userfilter": repo.user()}); + let result = client.get("api2/json/nodes/localhost/tasks", Some(args)).await?; + + let data = &result["data"]; + + if output_format == "text" { + for item in data.as_array().unwrap() { + println!( + "{} {}", + item["upid"].as_str().unwrap(), + item["status"].as_str().unwrap_or("running"), + ); + } + } else { + format_and_print_result(data, &output_format); + } + + Ok::<_, Error>(()) + })?; + + Ok(Value::Null) +} + +#[api( + input: { + properties: { + repository: { + schema: REPO_URL_SCHEMA, + optional: true, + }, + upid: { + schema: UPID_SCHEMA, + }, + } + } +)] +/// Display the task log. +fn task_log(param: Value) -> Result<Value, Error> { + + async_main(async { + let repo = extract_repository_from_value(¶m)?; + let upid = tools::required_string_param(¶m, "upid")?; + + let client = HttpClient::new(repo.host(), repo.user(), None)?; + + display_task_log(client, upid, true).await?; + + Ok::<_, Error>(()) + })?; + + 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"]); + + CliCommandMap::new() + .insert("log", task_log_cmd_def) + .insert("list", task_list_cmd_def) + +} fn main() { @@ -2297,7 +2396,8 @@ We do not extraxt '.pxar' archives when writing to stdandard output. .insert("status", status_cmd_def) .insert("key", key_mgmt_cli()) .insert("mount", mount_cmd_def) - .insert("catalog", catalog_mgmt_cli()); + .insert("catalog", catalog_mgmt_cli()) + .insert("task", task_mgmt_cli()); run_cli_command(cmd_def); }