]> git.proxmox.com Git - proxmox-backup.git/commitdiff
src/bin/proxmox-backup-client.rs: add simple task management cli
authorDietmar Maurer <dietmar@proxmox.com>
Tue, 10 Dec 2019 12:43:53 +0000 (13:43 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Tue, 10 Dec 2019 12:43:53 +0000 (13:43 +0100)
src/api2/node/tasks.rs
src/api2/types.rs
src/bin/proxmox-backup-client.rs

index f06c109996d9db19e25b3af8ea21973c1a909d94..d7f45eec67b53664bea9cb9b8f0d722f95cc96a4 100644 (file)
@@ -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 = &[
     (
index d7dca592730a05d0d0dcd40459b0b549b3fcc9c1..ac4c69793e5fd5c90c8dcee846c07112ab723e88 100644 (file)
@@ -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();
index fd78057516450989320a6d06904a8bdb0578d463..f84d8b2fc7b896bbe17d6367a4ed89893423cc6d 100644 (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::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(&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() {
 
@@ -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);
 }