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