]> git.proxmox.com Git - proxmox-backup.git/blob - proxmox-backup-client/src/task.rs
move pbs-tools/src/percent_encoding.rs to pbs-api-types/src/percent_encoding.rs
[proxmox-backup.git] / proxmox-backup-client / src / task.rs
1 use anyhow::{Error};
2 use serde_json::{json, Value};
3
4 use proxmox_schema::api;
5 use proxmox_router::cli::*;
6
7 use pbs_client::display_task_log;
8 use pbs_api_types::percent_encoding::percent_encode_component;
9 use pbs_tools::json::required_string_param;
10
11 use pbs_api_types::UPID;
12
13 use crate::{
14 REPO_URL_SCHEMA,
15 extract_repository_from_value,
16 complete_repository,
17 connect,
18 };
19
20 #[api(
21 input: {
22 properties: {
23 repository: {
24 schema: REPO_URL_SCHEMA,
25 optional: true,
26 },
27 limit: {
28 description: "The maximal number of tasks to list.",
29 type: Integer,
30 optional: true,
31 minimum: 1,
32 maximum: 1000,
33 default: 50,
34 },
35 "output-format": {
36 schema: OUTPUT_FORMAT,
37 optional: true,
38 },
39 all: {
40 type: Boolean,
41 description: "Also list stopped tasks.",
42 optional: true,
43 },
44 }
45 }
46 )]
47 /// List running server tasks for this repo user
48 async fn task_list(param: Value) -> Result<Value, Error> {
49
50 let output_format = get_output_format(&param);
51
52 let repo = extract_repository_from_value(&param)?;
53 let client = connect(&repo)?;
54
55 let limit = param["limit"].as_u64().unwrap_or(50) as usize;
56 let running = !param["all"].as_bool().unwrap_or(false);
57
58 let args = json!({
59 "running": running,
60 "start": 0,
61 "limit": limit,
62 "userfilter": repo.auth_id(),
63 "store": repo.store(),
64 });
65
66 let mut result = client.get("api2/json/nodes/localhost/tasks", Some(args)).await?;
67 let mut data = result["data"].take();
68
69 let return_type = &pbs_api_types::NODE_TASKS_LIST_TASKS_RETURN_TYPE;
70
71 use pbs_tools::format::{render_epoch, render_task_status};
72 let options = default_table_format_options()
73 .column(ColumnConfig::new("starttime").right_align(false).renderer(render_epoch))
74 .column(ColumnConfig::new("endtime").right_align(false).renderer(render_epoch))
75 .column(ColumnConfig::new("upid"))
76 .column(ColumnConfig::new("status").renderer(render_task_status));
77
78 format_and_print_result_full(&mut data, return_type, &output_format, &options);
79
80 Ok(Value::Null)
81 }
82
83 #[api(
84 input: {
85 properties: {
86 repository: {
87 schema: REPO_URL_SCHEMA,
88 optional: true,
89 },
90 upid: {
91 type: UPID,
92 },
93 }
94 }
95 )]
96 /// Display the task log.
97 async fn task_log(param: Value) -> Result<Value, Error> {
98
99 let repo = extract_repository_from_value(&param)?;
100 let upid = required_string_param(&param, "upid")?;
101
102 let mut client = connect(&repo)?;
103
104 display_task_log(&mut client, upid, true).await?;
105
106 Ok(Value::Null)
107 }
108
109 #[api(
110 input: {
111 properties: {
112 repository: {
113 schema: REPO_URL_SCHEMA,
114 optional: true,
115 },
116 upid: {
117 type: UPID,
118 },
119 }
120 }
121 )]
122 /// Try to stop a specific task.
123 async fn task_stop(param: Value) -> Result<Value, Error> {
124
125 let repo = extract_repository_from_value(&param)?;
126 let upid_str = required_string_param(&param, "upid")?;
127
128 let mut client = connect(&repo)?;
129
130 let path = format!("api2/json/nodes/localhost/tasks/{}", percent_encode_component(upid_str));
131 let _ = client.delete(&path, None).await?;
132
133 Ok(Value::Null)
134 }
135
136 pub fn task_mgmt_cli() -> CliCommandMap {
137
138 let task_list_cmd_def = CliCommand::new(&API_METHOD_TASK_LIST)
139 .completion_cb("repository", complete_repository);
140
141 let task_log_cmd_def = CliCommand::new(&API_METHOD_TASK_LOG)
142 .arg_param(&["upid"]);
143
144 let task_stop_cmd_def = CliCommand::new(&API_METHOD_TASK_STOP)
145 .arg_param(&["upid"]);
146
147 CliCommandMap::new()
148 .insert("log", task_log_cmd_def)
149 .insert("list", task_list_cmd_def)
150 .insert("stop", task_stop_cmd_def)
151 }