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