src/bin/proxmox-backup-client.rs: add simple task management cli

This commit is contained in:
Dietmar Maurer 2019-12-10 13:43:53 +01:00
parent 73e57f244e
commit 5830c20560
3 changed files with 105 additions and 5 deletions

View File

@ -178,10 +178,6 @@ fn list_tasks(
Ok(json!(result)) Ok(json!(result))
} }
const UPID_SCHEMA: Schema = StringSchema::new("Unique Process/Task ID.")
.max_length(256)
.schema();
#[sortable] #[sortable]
const UPID_API_SUBDIRS: SubdirMap = &[ const UPID_API_SUBDIRS: SubdirMap = &[
( (

View File

@ -97,3 +97,7 @@ pub const BACKUP_TIME_SCHEMA: Schema =
IntegerSchema::new("Backup time (Unix epoch.)") IntegerSchema::new("Backup time (Unix epoch.)")
.minimum(1_547_797_308) .minimum(1_547_797_308)
.schema(); .schema();
pub const UPID_SCHEMA: Schema = StringSchema::new("Unique Process/Task ID.")
.max_length(256)
.schema();

View File

@ -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::{ApiHandler, ApiMethod, RpcEnvironment};
use proxmox::api::schema::*; use proxmox::api::schema::*;
use proxmox::api::cli::*; use proxmox::api::cli::*;
use proxmox::api::api;
use proxmox_backup::tools; use proxmox_backup::tools;
use proxmox_backup::api2::types::*; use proxmox_backup::api2::types::*;
@ -1947,6 +1948,104 @@ fn catalog_mgmt_cli() -> CliCommandMap {
.insert("shell", catalog_shell_cmd_def) .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(&param)?;
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(&param)?;
let upid = tools::required_string_param(&param, "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() { fn main() {
@ -2297,7 +2396,8 @@ We do not extraxt '.pxar' archives when writing to stdandard output.
.insert("status", status_cmd_def) .insert("status", status_cmd_def)
.insert("key", key_mgmt_cli()) .insert("key", key_mgmt_cli())
.insert("mount", mount_cmd_def) .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); run_cli_command(cmd_def);
} }