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