]>
Commit | Line | Data |
---|---|---|
331b869d | 1 | use std::collections::HashMap; |
941342f7 | 2 | use std::io::{self, Write}; |
ea0b8b6e | 3 | |
b29d046e | 4 | use anyhow::{format_err, Error}; |
9894469e | 5 | use serde_json::{json, Value}; |
9894469e | 6 | |
380bd7df | 7 | use proxmox::api::{api, cli::*, RpcEnvironment}; |
769f8c99 DM |
8 | |
9 | use proxmox_backup::tools; | |
a220a456 | 10 | use proxmox_backup::config; |
9894469e | 11 | use proxmox_backup::api2::{self, types::* }; |
769f8c99 | 12 | use proxmox_backup::client::*; |
769f8c99 | 13 | |
a220a456 DM |
14 | mod proxmox_backup_manager; |
15 | use proxmox_backup_manager::*; | |
16 | ||
769f8c99 DM |
17 | async fn view_task_result( |
18 | client: HttpClient, | |
19 | result: Value, | |
20 | output_format: &str, | |
21 | ) -> Result<(), Error> { | |
22 | let data = &result["data"]; | |
23 | if output_format == "text" { | |
24 | if let Some(upid) = data.as_str() { | |
25 | display_task_log(client, upid, true).await?; | |
26 | } | |
27 | } else { | |
28 | format_and_print_result(&data, &output_format); | |
29 | } | |
30 | ||
31 | Ok(()) | |
32 | } | |
211fabd7 | 33 | |
dfb31de8 | 34 | // Note: local workers should print logs to stdout, so there is no need |
707974fd DM |
35 | // to fetch/display logs. We just wait for the worker to finish. |
36 | pub async fn wait_for_local_worker(upid_str: &str) -> Result<(), Error> { | |
37 | ||
38 | let upid: proxmox_backup::server::UPID = upid_str.parse()?; | |
39 | ||
40 | let sleep_duration = core::time::Duration::new(0, 100_000_000); | |
41 | ||
42 | loop { | |
43 | if proxmox_backup::server::worker_is_active_local(&upid) { | |
44 | tokio::time::delay_for(sleep_duration).await; | |
45 | } else { | |
46 | break; | |
47 | } | |
48 | } | |
49 | Ok(()) | |
50 | } | |
51 | ||
769f8c99 DM |
52 | #[api( |
53 | input: { | |
54 | properties: { | |
55 | store: { | |
56 | schema: DATASTORE_SCHEMA, | |
57 | }, | |
58 | "output-format": { | |
59 | schema: OUTPUT_FORMAT, | |
60 | optional: true, | |
61 | }, | |
62 | } | |
63 | } | |
64 | )] | |
65 | /// Start garbage collection for a specific datastore. | |
66 | async fn start_garbage_collection(param: Value) -> Result<Value, Error> { | |
691c89a0 | 67 | |
ac3faaf5 | 68 | let output_format = get_output_format(¶m); |
691c89a0 | 69 | |
769f8c99 DM |
70 | let store = tools::required_string_param(¶m, "store")?; |
71 | ||
6b68e5d5 | 72 | let mut client = connect_to_localhost()?; |
769f8c99 DM |
73 | |
74 | let path = format!("api2/json/admin/datastore/{}/gc", store); | |
75 | ||
76 | let result = client.post(&path, None).await?; | |
77 | ||
78 | view_task_result(client, result, &output_format).await?; | |
79 | ||
80 | Ok(Value::Null) | |
81 | } | |
82 | ||
83 | #[api( | |
84 | input: { | |
85 | properties: { | |
86 | store: { | |
87 | schema: DATASTORE_SCHEMA, | |
88 | }, | |
89 | "output-format": { | |
90 | schema: OUTPUT_FORMAT, | |
91 | optional: true, | |
92 | }, | |
93 | } | |
94 | } | |
95 | )] | |
96 | /// Show garbage collection status for a specific datastore. | |
97 | async fn garbage_collection_status(param: Value) -> Result<Value, Error> { | |
98 | ||
ac3faaf5 | 99 | let output_format = get_output_format(¶m); |
769f8c99 DM |
100 | |
101 | let store = tools::required_string_param(¶m, "store")?; | |
102 | ||
6b68e5d5 | 103 | let client = connect_to_localhost()?; |
769f8c99 DM |
104 | |
105 | let path = format!("api2/json/admin/datastore/{}/gc", store); | |
106 | ||
9894469e DM |
107 | let mut result = client.get(&path, None).await?; |
108 | let mut data = result["data"].take(); | |
660a3489 | 109 | let schema = &api2::admin::datastore::API_RETURN_SCHEMA_GARBAGE_COLLECTION_STATUS; |
9894469e | 110 | |
ac3faaf5 | 111 | let options = default_table_format_options(); |
9894469e DM |
112 | |
113 | format_and_print_result_full(&mut data, schema, &output_format, &options); | |
769f8c99 DM |
114 | |
115 | Ok(Value::Null) | |
116 | } | |
117 | ||
118 | fn garbage_collection_commands() -> CommandLineInterface { | |
691c89a0 DM |
119 | |
120 | let cmd_def = CliCommandMap::new() | |
121 | .insert("status", | |
769f8c99 | 122 | CliCommand::new(&API_METHOD_GARBAGE_COLLECTION_STATUS) |
49fddd98 | 123 | .arg_param(&["store"]) |
9ac1045c | 124 | .completion_cb("store", config::datastore::complete_datastore_name) |
48ef3c33 | 125 | ) |
691c89a0 | 126 | .insert("start", |
769f8c99 | 127 | CliCommand::new(&API_METHOD_START_GARBAGE_COLLECTION) |
49fddd98 | 128 | .arg_param(&["store"]) |
9ac1045c | 129 | .completion_cb("store", config::datastore::complete_datastore_name) |
48ef3c33 | 130 | ); |
691c89a0 DM |
131 | |
132 | cmd_def.into() | |
133 | } | |
134 | ||
47d47121 DM |
135 | #[api( |
136 | input: { | |
137 | properties: { | |
138 | limit: { | |
139 | description: "The maximal number of tasks to list.", | |
140 | type: Integer, | |
141 | optional: true, | |
142 | minimum: 1, | |
143 | maximum: 1000, | |
144 | default: 50, | |
145 | }, | |
146 | "output-format": { | |
147 | schema: OUTPUT_FORMAT, | |
148 | optional: true, | |
149 | }, | |
150 | all: { | |
151 | type: Boolean, | |
152 | description: "Also list stopped tasks.", | |
153 | optional: true, | |
154 | } | |
155 | } | |
156 | } | |
157 | )] | |
158 | /// List running server tasks. | |
159 | async fn task_list(param: Value) -> Result<Value, Error> { | |
160 | ||
ac3faaf5 | 161 | let output_format = get_output_format(¶m); |
47d47121 | 162 | |
6b68e5d5 | 163 | let client = connect_to_localhost()?; |
47d47121 DM |
164 | |
165 | let limit = param["limit"].as_u64().unwrap_or(50) as usize; | |
166 | let running = !param["all"].as_bool().unwrap_or(false); | |
167 | let args = json!({ | |
168 | "running": running, | |
169 | "start": 0, | |
170 | "limit": limit, | |
171 | }); | |
9894469e | 172 | let mut result = client.get("api2/json/nodes/localhost/tasks", Some(args)).await?; |
47d47121 | 173 | |
9894469e | 174 | let mut data = result["data"].take(); |
660a3489 | 175 | let schema = &api2::node::tasks::API_RETURN_SCHEMA_LIST_TASKS; |
47d47121 | 176 | |
ac3faaf5 | 177 | let options = default_table_format_options() |
4939255f DM |
178 | .column(ColumnConfig::new("starttime").right_align(false).renderer(tools::format::render_epoch)) |
179 | .column(ColumnConfig::new("endtime").right_align(false).renderer(tools::format::render_epoch)) | |
93fbb4ef | 180 | .column(ColumnConfig::new("upid")) |
4939255f | 181 | .column(ColumnConfig::new("status").renderer(tools::format::render_task_status)); |
9894469e DM |
182 | |
183 | format_and_print_result_full(&mut data, schema, &output_format, &options); | |
47d47121 DM |
184 | |
185 | Ok(Value::Null) | |
186 | } | |
187 | ||
188 | #[api( | |
189 | input: { | |
190 | properties: { | |
191 | upid: { | |
192 | schema: UPID_SCHEMA, | |
193 | }, | |
194 | } | |
195 | } | |
196 | )] | |
197 | /// Display the task log. | |
198 | async fn task_log(param: Value) -> Result<Value, Error> { | |
199 | ||
200 | let upid = tools::required_string_param(¶m, "upid")?; | |
201 | ||
6b68e5d5 | 202 | let client = connect_to_localhost()?; |
47d47121 DM |
203 | |
204 | display_task_log(client, upid, true).await?; | |
205 | ||
206 | Ok(Value::Null) | |
207 | } | |
208 | ||
209 | #[api( | |
210 | input: { | |
211 | properties: { | |
212 | upid: { | |
213 | schema: UPID_SCHEMA, | |
214 | }, | |
215 | } | |
216 | } | |
217 | )] | |
218 | /// Try to stop a specific task. | |
219 | async fn task_stop(param: Value) -> Result<Value, Error> { | |
220 | ||
221 | let upid_str = tools::required_string_param(¶m, "upid")?; | |
222 | ||
6b68e5d5 | 223 | let mut client = connect_to_localhost()?; |
47d47121 | 224 | |
968a0ab2 | 225 | let path = format!("api2/json/nodes/localhost/tasks/{}", tools::percent_encode_component(upid_str)); |
47d47121 DM |
226 | let _ = client.delete(&path, None).await?; |
227 | ||
228 | Ok(Value::Null) | |
229 | } | |
230 | ||
231 | fn task_mgmt_cli() -> CommandLineInterface { | |
232 | ||
233 | let task_log_cmd_def = CliCommand::new(&API_METHOD_TASK_LOG) | |
234 | .arg_param(&["upid"]); | |
235 | ||
236 | let task_stop_cmd_def = CliCommand::new(&API_METHOD_TASK_STOP) | |
237 | .arg_param(&["upid"]); | |
238 | ||
239 | let cmd_def = CliCommandMap::new() | |
240 | .insert("list", CliCommand::new(&API_METHOD_TASK_LIST)) | |
241 | .insert("log", task_log_cmd_def) | |
242 | .insert("stop", task_stop_cmd_def); | |
243 | ||
244 | cmd_def.into() | |
245 | } | |
246 | ||
4b4eba0b | 247 | // fixme: avoid API redefinition |
0eb0e024 DM |
248 | #[api( |
249 | input: { | |
250 | properties: { | |
eb506c83 | 251 | "local-store": { |
0eb0e024 DM |
252 | schema: DATASTORE_SCHEMA, |
253 | }, | |
254 | remote: { | |
167971ed | 255 | schema: REMOTE_ID_SCHEMA, |
0eb0e024 DM |
256 | }, |
257 | "remote-store": { | |
258 | schema: DATASTORE_SCHEMA, | |
259 | }, | |
40dc1031 DC |
260 | "remove-vanished": { |
261 | schema: REMOVE_VANISHED_BACKUPS_SCHEMA, | |
4b4eba0b | 262 | optional: true, |
4b4eba0b | 263 | }, |
0eb0e024 DM |
264 | "output-format": { |
265 | schema: OUTPUT_FORMAT, | |
266 | optional: true, | |
267 | }, | |
268 | } | |
269 | } | |
270 | )] | |
eb506c83 DM |
271 | /// Sync datastore from another repository |
272 | async fn pull_datastore( | |
0eb0e024 DM |
273 | remote: String, |
274 | remote_store: String, | |
eb506c83 | 275 | local_store: String, |
40dc1031 | 276 | remove_vanished: Option<bool>, |
ac3faaf5 | 277 | param: Value, |
0eb0e024 DM |
278 | ) -> Result<Value, Error> { |
279 | ||
ac3faaf5 | 280 | let output_format = get_output_format(¶m); |
0eb0e024 | 281 | |
6b68e5d5 | 282 | let mut client = connect_to_localhost()?; |
0eb0e024 | 283 | |
8c877436 | 284 | let mut args = json!({ |
eb506c83 | 285 | "store": local_store, |
94609e23 | 286 | "remote": remote, |
0eb0e024 | 287 | "remote-store": remote_store, |
0eb0e024 DM |
288 | }); |
289 | ||
8c877436 DC |
290 | if let Some(remove_vanished) = remove_vanished { |
291 | args["remove-vanished"] = Value::from(remove_vanished); | |
292 | } | |
293 | ||
eb506c83 | 294 | let result = client.post("api2/json/pull", Some(args)).await?; |
0eb0e024 DM |
295 | |
296 | view_task_result(client, result, &output_format).await?; | |
297 | ||
298 | Ok(Value::Null) | |
299 | } | |
300 | ||
355c055e DM |
301 | #[api( |
302 | input: { | |
303 | properties: { | |
304 | "store": { | |
305 | schema: DATASTORE_SCHEMA, | |
306 | }, | |
307 | "output-format": { | |
308 | schema: OUTPUT_FORMAT, | |
309 | optional: true, | |
310 | }, | |
311 | } | |
312 | } | |
313 | )] | |
314 | /// Verify backups | |
315 | async fn verify( | |
316 | store: String, | |
317 | param: Value, | |
318 | ) -> Result<Value, Error> { | |
319 | ||
320 | let output_format = get_output_format(¶m); | |
321 | ||
6b68e5d5 | 322 | let mut client = connect_to_localhost()?; |
355c055e DM |
323 | |
324 | let args = json!({}); | |
325 | ||
326 | let path = format!("api2/json/admin/datastore/{}/verify", store); | |
327 | ||
328 | let result = client.post(&path, Some(args)).await?; | |
329 | ||
330 | view_task_result(client, result, &output_format).await?; | |
331 | ||
332 | Ok(Value::Null) | |
333 | } | |
334 | ||
9a556c8a HL |
335 | #[api()] |
336 | /// System report | |
337 | async fn report() -> Result<Value, Error> { | |
941342f7 TL |
338 | let report = proxmox_backup::server::generate_report(); |
339 | io::stdout().write_all(report.as_bytes())?; | |
9a556c8a HL |
340 | Ok(Value::Null) |
341 | } | |
342 | ||
c100fe91 ML |
343 | #[api( |
344 | input: { | |
345 | properties: { | |
346 | verbose: { | |
347 | type: Boolean, | |
348 | optional: true, | |
349 | default: false, | |
350 | description: "Output verbose package information. It is ignored if output-format is specified.", | |
351 | }, | |
352 | "output-format": { | |
353 | schema: OUTPUT_FORMAT, | |
354 | optional: true, | |
355 | } | |
356 | } | |
357 | } | |
358 | )] | |
359 | /// List package versions for important Proxmox Backup Server packages. | |
360 | async fn get_versions(verbose: bool, param: Value) -> Result<Value, Error> { | |
294466ee | 361 | let output_format = get_output_format(¶m); |
c100fe91 | 362 | |
5e293f13 | 363 | let packages = crate::api2::node::apt::get_versions()?; |
fc5a0120 | 364 | let mut packages = json!(if verbose { &packages[..] } else { &packages[1..2] }); |
c100fe91 | 365 | |
294466ee TL |
366 | let options = default_table_format_options() |
367 | .disable_sort() | |
368 | .noborder(true) // just not helpfull for version info which gets copy pasted often | |
369 | .column(ColumnConfig::new("Package")) | |
370 | .column(ColumnConfig::new("Version")) | |
371 | .column(ColumnConfig::new("ExtraInfo").header("Extra Info")) | |
372 | ; | |
373 | let schema = &crate::api2::node::apt::API_RETURN_SCHEMA_GET_VERSIONS; | |
374 | ||
375 | format_and_print_result_full(&mut packages, schema, &output_format, &options); | |
c100fe91 ML |
376 | |
377 | Ok(Value::Null) | |
378 | } | |
379 | ||
211fabd7 DM |
380 | fn main() { |
381 | ||
ac7513e3 DM |
382 | proxmox_backup::tools::setup_safe_path_env(); |
383 | ||
6460764d | 384 | let cmd_def = CliCommandMap::new() |
ed3e60ae | 385 | .insert("acl", acl_commands()) |
48ef3c33 | 386 | .insert("datastore", datastore_commands()) |
8e40aa63 | 387 | .insert("disk", disk_commands()) |
14627d67 | 388 | .insert("dns", dns_commands()) |
ca0e5347 | 389 | .insert("network", network_commands()) |
579728c6 | 390 | .insert("user", user_commands()) |
f357390c | 391 | .insert("remote", remote_commands()) |
47d47121 | 392 | .insert("garbage-collection", garbage_collection_commands()) |
550e0d88 | 393 | .insert("cert", cert_mgmt_cli()) |
2762481c | 394 | .insert("subscription", subscription_commands()) |
a3016d65 | 395 | .insert("sync-job", sync_job_commands()) |
0eb0e024 DM |
396 | .insert("task", task_mgmt_cli()) |
397 | .insert( | |
eb506c83 DM |
398 | "pull", |
399 | CliCommand::new(&API_METHOD_PULL_DATASTORE) | |
400 | .arg_param(&["remote", "remote-store", "local-store"]) | |
401 | .completion_cb("local-store", config::datastore::complete_datastore_name) | |
f357390c | 402 | .completion_cb("remote", config::remote::complete_remote_name) |
331b869d | 403 | .completion_cb("remote-store", complete_remote_datastore_name) |
355c055e DM |
404 | ) |
405 | .insert( | |
406 | "verify", | |
407 | CliCommand::new(&API_METHOD_VERIFY) | |
408 | .arg_param(&["store"]) | |
409 | .completion_cb("store", config::datastore::complete_datastore_name) | |
9a556c8a HL |
410 | ) |
411 | .insert("report", | |
412 | CliCommand::new(&API_METHOD_REPORT) | |
c100fe91 ML |
413 | ) |
414 | .insert("versions", | |
415 | CliCommand::new(&API_METHOD_GET_VERSIONS) | |
0eb0e024 | 416 | ); |
34d3ba52 | 417 | |
355c055e DM |
418 | |
419 | ||
7b22acd0 | 420 | let mut rpcenv = CliEnvironment::new(); |
e6dc35ac | 421 | rpcenv.set_auth_id(Some(String::from("root@pam"))); |
525008f7 | 422 | |
7b22acd0 | 423 | proxmox_backup::tools::runtime::main(run_async_cli_command(cmd_def, rpcenv)); |
ea0b8b6e | 424 | } |
331b869d DM |
425 | |
426 | // shell completion helper | |
427 | pub fn complete_remote_datastore_name(_arg: &str, param: &HashMap<String, String>) -> Vec<String> { | |
428 | ||
429 | let mut list = Vec::new(); | |
430 | ||
9ea4bce4 | 431 | let _ = proxmox::try_block!({ |
331b869d | 432 | let remote = param.get("remote").ok_or_else(|| format_err!("no remote"))?; |
331b869d | 433 | |
e0100d61 FG |
434 | let data = crate::tools::runtime::block_on(async move { |
435 | crate::api2::config::remote::scan_remote_datastores(remote.clone()).await | |
436 | })?; | |
331b869d | 437 | |
e0100d61 FG |
438 | for item in data { |
439 | list.push(item.store); | |
331b869d DM |
440 | } |
441 | ||
442 | Ok(()) | |
443 | }).map_err(|_err: Error| { /* ignore */ }); | |
444 | ||
445 | list | |
446 | } |