]>
Commit | Line | Data |
---|---|---|
550e0d88 | 1 | use std::path::PathBuf; |
331b869d | 2 | use std::collections::HashMap; |
ea0b8b6e | 3 | |
f7d4e4b5 | 4 | use anyhow::{bail, format_err, Error}; |
9894469e | 5 | use serde_json::{json, Value}; |
9894469e | 6 | |
9e165b5c | 7 | use proxmox::api::{api, cli::*, RpcEnvironment, ApiHandler}; |
769f8c99 | 8 | |
550e0d88 | 9 | use proxmox_backup::configdir; |
769f8c99 | 10 | use proxmox_backup::tools; |
f357390c | 11 | use proxmox_backup::config::{self, remote::{self, Remote}}; |
9894469e | 12 | use proxmox_backup::api2::{self, types::* }; |
769f8c99 DM |
13 | use proxmox_backup::client::*; |
14 | use proxmox_backup::tools::ticket::*; | |
15 | use proxmox_backup::auth_helpers::*; | |
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 | |
47d47121 DM |
34 | fn connect() -> Result<HttpClient, Error> { |
35 | ||
36 | let uid = nix::unistd::Uid::current(); | |
37 | ||
d59dbeca | 38 | let mut options = HttpClientOptions::new() |
5030b7ce | 39 | .prefix(Some("proxmox-backup".to_string())) |
d59dbeca DM |
40 | .verify_cert(false); // not required for connection to localhost |
41 | ||
47d47121 DM |
42 | let client = if uid.is_root() { |
43 | let ticket = assemble_rsa_ticket(private_auth_key(), "PBS", Some("root@pam"), None)?; | |
d59dbeca DM |
44 | options = options.password(Some(ticket)); |
45 | HttpClient::new("localhost", "root@pam", options)? | |
47d47121 | 46 | } else { |
d59dbeca DM |
47 | options = options.ticket_cache(true).interactive(true); |
48 | HttpClient::new("localhost", "root@pam", options)? | |
47d47121 DM |
49 | }; |
50 | ||
51 | Ok(client) | |
52 | } | |
53 | ||
9894469e DM |
54 | #[api( |
55 | input: { | |
56 | properties: { | |
57 | "output-format": { | |
58 | schema: OUTPUT_FORMAT, | |
59 | optional: true, | |
60 | }, | |
61 | } | |
62 | } | |
63 | )] | |
64 | /// List configured remotes. | |
9e165b5c | 65 | fn list_remotes(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> { |
688fbe07 | 66 | |
ac3faaf5 | 67 | let output_format = get_output_format(¶m); |
9894469e | 68 | |
9e165b5c DM |
69 | let info = &api2::config::remote::API_METHOD_LIST_REMOTES; |
70 | let mut data = match info.handler { | |
71 | ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, | |
72 | _ => unreachable!(), | |
73 | }; | |
9894469e | 74 | |
ac3faaf5 | 75 | let options = default_table_format_options() |
93fbb4ef DM |
76 | .column(ColumnConfig::new("name")) |
77 | .column(ColumnConfig::new("host")) | |
78 | .column(ColumnConfig::new("userid")) | |
79 | .column(ColumnConfig::new("fingerprint")) | |
80 | .column(ColumnConfig::new("comment")); | |
9894469e | 81 | |
9e165b5c | 82 | format_and_print_result_full(&mut data, info.returns, &output_format, &options); |
9894469e DM |
83 | |
84 | Ok(Value::Null) | |
85 | } | |
86 | ||
87 | fn remote_commands() -> CommandLineInterface { | |
688fbe07 DM |
88 | |
89 | let cmd_def = CliCommandMap::new() | |
9894469e | 90 | .insert("list", CliCommand::new(&&API_METHOD_LIST_REMOTES)) |
688fbe07 DM |
91 | .insert( |
92 | "create", | |
93 | // fixme: howto handle password parameter? | |
f357390c | 94 | CliCommand::new(&api2::config::remote::API_METHOD_CREATE_REMOTE) |
688fbe07 DM |
95 | .arg_param(&["name"]) |
96 | ) | |
08195ac8 DM |
97 | .insert( |
98 | "update", | |
f357390c | 99 | CliCommand::new(&api2::config::remote::API_METHOD_UPDATE_REMOTE) |
08195ac8 | 100 | .arg_param(&["name"]) |
f357390c | 101 | .completion_cb("name", config::remote::complete_remote_name) |
08195ac8 | 102 | ) |
688fbe07 DM |
103 | .insert( |
104 | "remove", | |
f357390c | 105 | CliCommand::new(&api2::config::remote::API_METHOD_DELETE_REMOTE) |
688fbe07 | 106 | .arg_param(&["name"]) |
f357390c | 107 | .completion_cb("name", config::remote::complete_remote_name) |
688fbe07 DM |
108 | ); |
109 | ||
110 | cmd_def.into() | |
111 | } | |
112 | ||
579728c6 DM |
113 | #[api( |
114 | input: { | |
115 | properties: { | |
116 | "output-format": { | |
117 | schema: OUTPUT_FORMAT, | |
118 | optional: true, | |
119 | }, | |
120 | } | |
121 | } | |
122 | )] | |
123 | /// List configured users. | |
124 | fn list_users(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> { | |
125 | ||
126 | let output_format = get_output_format(¶m); | |
127 | ||
685e1334 | 128 | let info = &api2::access::user::API_METHOD_LIST_USERS; |
579728c6 DM |
129 | let mut data = match info.handler { |
130 | ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, | |
131 | _ => unreachable!(), | |
132 | }; | |
133 | ||
134 | let options = default_table_format_options() | |
135 | .column(ColumnConfig::new("userid")) | |
136 | .column(ColumnConfig::new("enable")) | |
137 | .column(ColumnConfig::new("expire")) | |
138 | .column(ColumnConfig::new("firstname")) | |
139 | .column(ColumnConfig::new("lastname")) | |
140 | .column(ColumnConfig::new("email")) | |
141 | .column(ColumnConfig::new("comment")); | |
142 | ||
143 | format_and_print_result_full(&mut data, info.returns, &output_format, &options); | |
144 | ||
145 | Ok(Value::Null) | |
146 | } | |
147 | ||
148 | fn user_commands() -> CommandLineInterface { | |
149 | ||
150 | let cmd_def = CliCommandMap::new() | |
151 | .insert("list", CliCommand::new(&&API_METHOD_LIST_USERS)) | |
152 | .insert( | |
153 | "create", | |
154 | // fixme: howto handle password parameter? | |
685e1334 | 155 | CliCommand::new(&api2::access::user::API_METHOD_CREATE_USER) |
579728c6 DM |
156 | .arg_param(&["userid"]) |
157 | ) | |
158 | .insert( | |
159 | "update", | |
685e1334 | 160 | CliCommand::new(&api2::access::user::API_METHOD_UPDATE_USER) |
579728c6 DM |
161 | .arg_param(&["userid"]) |
162 | .completion_cb("userid", config::user::complete_user_name) | |
163 | ) | |
164 | .insert( | |
165 | "remove", | |
685e1334 | 166 | CliCommand::new(&api2::access::user::API_METHOD_DELETE_USER) |
579728c6 DM |
167 | .arg_param(&["userid"]) |
168 | .completion_cb("userid", config::user::complete_user_name) | |
169 | ); | |
170 | ||
171 | cmd_def.into() | |
172 | } | |
173 | ||
ed3e60ae DM |
174 | #[api( |
175 | input: { | |
176 | properties: { | |
177 | "output-format": { | |
178 | schema: OUTPUT_FORMAT, | |
179 | optional: true, | |
180 | }, | |
181 | } | |
182 | } | |
183 | )] | |
184 | /// Access Control list. | |
185 | fn list_acls(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> { | |
186 | ||
187 | let output_format = get_output_format(¶m); | |
188 | ||
189 | let info = &api2::access::acl::API_METHOD_READ_ACL; | |
190 | let mut data = match info.handler { | |
191 | ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, | |
192 | _ => unreachable!(), | |
193 | }; | |
194 | ||
195 | fn render_ugid(value: &Value, record: &Value) -> Result<String, Error> { | |
196 | if value.is_null() { return Ok(String::new()); } | |
197 | let ugid = value.as_str().unwrap(); | |
198 | let ugid_type = record["ugid_type"].as_str().unwrap(); | |
199 | ||
200 | if ugid_type == "user" { | |
201 | Ok(ugid.to_string()) | |
202 | } else if ugid_type == "group" { | |
203 | Ok(format!("@{}", ugid)) | |
204 | } else { | |
205 | bail!("render_ugid: got unknown ugid_type"); | |
206 | } | |
207 | } | |
208 | ||
209 | let options = default_table_format_options() | |
210 | .column(ColumnConfig::new("ugid").renderer(render_ugid)) | |
211 | .column(ColumnConfig::new("path")) | |
212 | .column(ColumnConfig::new("propagate")) | |
213 | .column(ColumnConfig::new("roleid")); | |
214 | ||
215 | format_and_print_result_full(&mut data, info.returns, &output_format, &options); | |
216 | ||
217 | Ok(Value::Null) | |
218 | } | |
219 | ||
220 | fn acl_commands() -> CommandLineInterface { | |
221 | ||
222 | let cmd_def = CliCommandMap::new() | |
9765092e DM |
223 | .insert("list", CliCommand::new(&&API_METHOD_LIST_ACLS)) |
224 | .insert( | |
225 | "update", | |
226 | CliCommand::new(&api2::access::acl::API_METHOD_UPDATE_ACL) | |
227 | .arg_param(&["path", "role"]) | |
228 | .completion_cb("userid", config::user::complete_user_name) | |
229 | .completion_cb("path", config::datastore::complete_acl_path) | |
230 | ||
231 | ); | |
ed3e60ae DM |
232 | |
233 | cmd_def.into() | |
234 | } | |
235 | ||
bf004ecd DM |
236 | #[api( |
237 | input: { | |
238 | properties: { | |
239 | "output-format": { | |
240 | schema: OUTPUT_FORMAT, | |
241 | optional: true, | |
242 | }, | |
243 | } | |
244 | } | |
245 | )] | |
246 | /// Network device list. | |
247 | fn list_network_devices(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> { | |
248 | ||
249 | let output_format = get_output_format(¶m); | |
250 | ||
251 | let info = &api2::config::network::API_METHOD_LIST_NETWORK_DEVICES; | |
252 | let mut data = match info.handler { | |
253 | ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, | |
254 | _ => unreachable!(), | |
255 | }; | |
256 | ||
2eefd9ae DM |
257 | if let Some(changes) = rpcenv.get_result_attrib("changes") { |
258 | if let Some(diff) = changes.as_str() { | |
3181f9b6 DM |
259 | if output_format == "text" { |
260 | eprintln!("pending changes:\n{}\n", diff); | |
261 | } | |
2eefd9ae DM |
262 | } |
263 | } | |
264 | ||
4cb6bd89 DM |
265 | fn render_address(_value: &Value, record: &Value) -> Result<String, Error> { |
266 | let mut text = String::new(); | |
267 | ||
268 | if let Some(cidr) = record["cidr_v4"].as_str() { | |
269 | text.push_str(cidr); | |
270 | } | |
271 | if let Some(cidr) = record["cidr_v6"].as_str() { | |
272 | if !text.is_empty() { text.push('\n'); } | |
273 | text.push_str(cidr); | |
274 | } | |
275 | ||
276 | Ok(text) | |
277 | } | |
278 | ||
279 | fn render_gateway(_value: &Value, record: &Value) -> Result<String, Error> { | |
280 | let mut text = String::new(); | |
281 | ||
282 | if let Some(gateway) = record["gateway_v4"].as_str() { | |
283 | text.push_str(gateway); | |
284 | } | |
285 | if let Some(gateway) = record["gateway_v6"].as_str() { | |
286 | if !text.is_empty() { text.push('\n'); } | |
287 | text.push_str(gateway); | |
288 | } | |
289 | ||
290 | Ok(text) | |
291 | } | |
292 | ||
bf004ecd | 293 | let options = default_table_format_options() |
02269f3d | 294 | .column(ColumnConfig::new("interface_type").header("type")) |
bf004ecd DM |
295 | .column(ColumnConfig::new("name")) |
296 | .column(ColumnConfig::new("auto")) | |
297 | .column(ColumnConfig::new("method_v4")) | |
298 | .column(ColumnConfig::new("method_v6")) | |
4cb6bd89 DM |
299 | .column(ColumnConfig::new("cidr_v4").header("address").renderer(render_address)) |
300 | .column(ColumnConfig::new("gateway_v4").header("gateway").renderer(render_gateway)); | |
bf004ecd DM |
301 | |
302 | format_and_print_result_full(&mut data, info.returns, &output_format, &options); | |
303 | ||
304 | Ok(Value::Null) | |
305 | } | |
306 | ||
ca0e5347 DM |
307 | fn network_commands() -> CommandLineInterface { |
308 | ||
309 | let cmd_def = CliCommandMap::new() | |
bf004ecd | 310 | .insert("list", CliCommand::new(&API_METHOD_LIST_NETWORK_DEVICES)) |
2eefd9ae DM |
311 | .insert( |
312 | "update", | |
313 | CliCommand::new(&api2::config::network::API_METHOD_UPDATE_INTERFACE) | |
df6bb03d DM |
314 | .arg_param(&["name"]) |
315 | .completion_cb("name", config::network::complete_interface_name) | |
316 | ) | |
2eefd9ae DM |
317 | .insert( |
318 | "remove", | |
319 | CliCommand::new(&api2::config::network::API_METHOD_DELETE_INTERFACE) | |
df6bb03d DM |
320 | .arg_param(&["name"]) |
321 | .completion_cb("name", config::network::complete_interface_name) | |
2eefd9ae DM |
322 | ) |
323 | .insert( | |
324 | "reload", | |
325 | CliCommand::new(&api2::config::network::API_METHOD_RELOAD_NETWORK_CONFIG) | |
df6bb03d | 326 | ); |
ca0e5347 DM |
327 | |
328 | cmd_def.into() | |
329 | } | |
330 | ||
b1564af2 DM |
331 | #[api( |
332 | input: { | |
333 | properties: { | |
334 | "output-format": { | |
335 | schema: OUTPUT_FORMAT, | |
336 | optional: true, | |
337 | }, | |
338 | } | |
339 | } | |
340 | )] | |
341 | /// Datastore list. | |
342 | fn list_datastores(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> { | |
343 | ||
344 | let output_format = get_output_format(¶m); | |
345 | ||
346 | let info = &api2::config::datastore::API_METHOD_LIST_DATASTORES; | |
347 | let mut data = match info.handler { | |
348 | ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, | |
349 | _ => unreachable!(), | |
350 | }; | |
351 | ||
352 | let options = default_table_format_options() | |
353 | .column(ColumnConfig::new("name")) | |
354 | .column(ColumnConfig::new("path")) | |
355 | .column(ColumnConfig::new("comment")); | |
356 | ||
357 | format_and_print_result_full(&mut data, info.returns, &output_format, &options); | |
358 | ||
359 | Ok(Value::Null) | |
360 | } | |
361 | ||
9f6ab1fc | 362 | fn datastore_commands() -> CommandLineInterface { |
ea0b8b6e | 363 | |
6460764d | 364 | let cmd_def = CliCommandMap::new() |
b1564af2 | 365 | .insert("list", CliCommand::new(&API_METHOD_LIST_DATASTORES)) |
6460764d | 366 | .insert("create", |
688fbe07 | 367 | CliCommand::new(&api2::config::datastore::API_METHOD_CREATE_DATASTORE) |
49fddd98 | 368 | .arg_param(&["name", "path"]) |
48ef3c33 | 369 | ) |
ddc52662 DM |
370 | .insert("update", |
371 | CliCommand::new(&api2::config::datastore::API_METHOD_UPDATE_DATASTORE) | |
372 | .arg_param(&["name"]) | |
3be839c6 | 373 | .completion_cb("name", config::datastore::complete_datastore_name) |
ddc52662 | 374 | ) |
6460764d | 375 | .insert("remove", |
688fbe07 | 376 | CliCommand::new(&api2::config::datastore::API_METHOD_DELETE_DATASTORE) |
49fddd98 | 377 | .arg_param(&["name"]) |
07b4694a | 378 | .completion_cb("name", config::datastore::complete_datastore_name) |
48ef3c33 | 379 | ); |
211fabd7 | 380 | |
8f62336b | 381 | cmd_def.into() |
211fabd7 DM |
382 | } |
383 | ||
691c89a0 | 384 | |
769f8c99 DM |
385 | #[api( |
386 | input: { | |
387 | properties: { | |
388 | store: { | |
389 | schema: DATASTORE_SCHEMA, | |
390 | }, | |
391 | "output-format": { | |
392 | schema: OUTPUT_FORMAT, | |
393 | optional: true, | |
394 | }, | |
395 | } | |
396 | } | |
397 | )] | |
398 | /// Start garbage collection for a specific datastore. | |
399 | async fn start_garbage_collection(param: Value) -> Result<Value, Error> { | |
691c89a0 | 400 | |
ac3faaf5 | 401 | let output_format = get_output_format(¶m); |
691c89a0 | 402 | |
769f8c99 DM |
403 | let store = tools::required_string_param(¶m, "store")?; |
404 | ||
47d47121 | 405 | let mut client = connect()?; |
769f8c99 DM |
406 | |
407 | let path = format!("api2/json/admin/datastore/{}/gc", store); | |
408 | ||
409 | let result = client.post(&path, None).await?; | |
410 | ||
411 | view_task_result(client, result, &output_format).await?; | |
412 | ||
413 | Ok(Value::Null) | |
414 | } | |
415 | ||
416 | #[api( | |
417 | input: { | |
418 | properties: { | |
419 | store: { | |
420 | schema: DATASTORE_SCHEMA, | |
421 | }, | |
422 | "output-format": { | |
423 | schema: OUTPUT_FORMAT, | |
424 | optional: true, | |
425 | }, | |
426 | } | |
427 | } | |
428 | )] | |
429 | /// Show garbage collection status for a specific datastore. | |
430 | async fn garbage_collection_status(param: Value) -> Result<Value, Error> { | |
431 | ||
ac3faaf5 | 432 | let output_format = get_output_format(¶m); |
769f8c99 DM |
433 | |
434 | let store = tools::required_string_param(¶m, "store")?; | |
435 | ||
47d47121 | 436 | let client = connect()?; |
769f8c99 DM |
437 | |
438 | let path = format!("api2/json/admin/datastore/{}/gc", store); | |
439 | ||
9894469e DM |
440 | let mut result = client.get(&path, None).await?; |
441 | let mut data = result["data"].take(); | |
442 | let schema = api2::admin::datastore::API_RETURN_SCHEMA_GARBAGE_COLLECTION_STATUS; | |
443 | ||
ac3faaf5 | 444 | let options = default_table_format_options(); |
9894469e DM |
445 | |
446 | format_and_print_result_full(&mut data, schema, &output_format, &options); | |
769f8c99 DM |
447 | |
448 | Ok(Value::Null) | |
449 | } | |
450 | ||
451 | fn garbage_collection_commands() -> CommandLineInterface { | |
691c89a0 DM |
452 | |
453 | let cmd_def = CliCommandMap::new() | |
454 | .insert("status", | |
769f8c99 | 455 | CliCommand::new(&API_METHOD_GARBAGE_COLLECTION_STATUS) |
49fddd98 | 456 | .arg_param(&["store"]) |
9ac1045c | 457 | .completion_cb("store", config::datastore::complete_datastore_name) |
48ef3c33 | 458 | ) |
691c89a0 | 459 | .insert("start", |
769f8c99 | 460 | CliCommand::new(&API_METHOD_START_GARBAGE_COLLECTION) |
49fddd98 | 461 | .arg_param(&["store"]) |
9ac1045c | 462 | .completion_cb("store", config::datastore::complete_datastore_name) |
48ef3c33 | 463 | ); |
691c89a0 DM |
464 | |
465 | cmd_def.into() | |
466 | } | |
467 | ||
47d47121 DM |
468 | #[api( |
469 | input: { | |
470 | properties: { | |
471 | limit: { | |
472 | description: "The maximal number of tasks to list.", | |
473 | type: Integer, | |
474 | optional: true, | |
475 | minimum: 1, | |
476 | maximum: 1000, | |
477 | default: 50, | |
478 | }, | |
479 | "output-format": { | |
480 | schema: OUTPUT_FORMAT, | |
481 | optional: true, | |
482 | }, | |
483 | all: { | |
484 | type: Boolean, | |
485 | description: "Also list stopped tasks.", | |
486 | optional: true, | |
487 | } | |
488 | } | |
489 | } | |
490 | )] | |
491 | /// List running server tasks. | |
492 | async fn task_list(param: Value) -> Result<Value, Error> { | |
493 | ||
ac3faaf5 | 494 | let output_format = get_output_format(¶m); |
47d47121 DM |
495 | |
496 | let client = connect()?; | |
497 | ||
498 | let limit = param["limit"].as_u64().unwrap_or(50) as usize; | |
499 | let running = !param["all"].as_bool().unwrap_or(false); | |
500 | let args = json!({ | |
501 | "running": running, | |
502 | "start": 0, | |
503 | "limit": limit, | |
504 | }); | |
9894469e | 505 | let mut result = client.get("api2/json/nodes/localhost/tasks", Some(args)).await?; |
47d47121 | 506 | |
9894469e DM |
507 | let mut data = result["data"].take(); |
508 | let schema = api2::node::tasks::API_RETURN_SCHEMA_LIST_TASKS; | |
47d47121 | 509 | |
ac3faaf5 | 510 | let options = default_table_format_options() |
4939255f DM |
511 | .column(ColumnConfig::new("starttime").right_align(false).renderer(tools::format::render_epoch)) |
512 | .column(ColumnConfig::new("endtime").right_align(false).renderer(tools::format::render_epoch)) | |
93fbb4ef | 513 | .column(ColumnConfig::new("upid")) |
4939255f | 514 | .column(ColumnConfig::new("status").renderer(tools::format::render_task_status)); |
9894469e DM |
515 | |
516 | format_and_print_result_full(&mut data, schema, &output_format, &options); | |
47d47121 DM |
517 | |
518 | Ok(Value::Null) | |
519 | } | |
520 | ||
521 | #[api( | |
522 | input: { | |
523 | properties: { | |
524 | upid: { | |
525 | schema: UPID_SCHEMA, | |
526 | }, | |
527 | } | |
528 | } | |
529 | )] | |
530 | /// Display the task log. | |
531 | async fn task_log(param: Value) -> Result<Value, Error> { | |
532 | ||
533 | let upid = tools::required_string_param(¶m, "upid")?; | |
534 | ||
535 | let client = connect()?; | |
536 | ||
537 | display_task_log(client, upid, true).await?; | |
538 | ||
539 | Ok(Value::Null) | |
540 | } | |
541 | ||
542 | #[api( | |
543 | input: { | |
544 | properties: { | |
545 | upid: { | |
546 | schema: UPID_SCHEMA, | |
547 | }, | |
548 | } | |
549 | } | |
550 | )] | |
551 | /// Try to stop a specific task. | |
552 | async fn task_stop(param: Value) -> Result<Value, Error> { | |
553 | ||
554 | let upid_str = tools::required_string_param(¶m, "upid")?; | |
555 | ||
556 | let mut client = connect()?; | |
557 | ||
558 | let path = format!("api2/json/nodes/localhost/tasks/{}", upid_str); | |
559 | let _ = client.delete(&path, None).await?; | |
560 | ||
561 | Ok(Value::Null) | |
562 | } | |
563 | ||
564 | fn task_mgmt_cli() -> CommandLineInterface { | |
565 | ||
566 | let task_log_cmd_def = CliCommand::new(&API_METHOD_TASK_LOG) | |
567 | .arg_param(&["upid"]); | |
568 | ||
569 | let task_stop_cmd_def = CliCommand::new(&API_METHOD_TASK_STOP) | |
570 | .arg_param(&["upid"]); | |
571 | ||
572 | let cmd_def = CliCommandMap::new() | |
573 | .insert("list", CliCommand::new(&API_METHOD_TASK_LIST)) | |
574 | .insert("log", task_log_cmd_def) | |
575 | .insert("stop", task_stop_cmd_def); | |
576 | ||
577 | cmd_def.into() | |
578 | } | |
579 | ||
e739a8d8 DM |
580 | fn x509name_to_string(name: &openssl::x509::X509NameRef) -> Result<String, Error> { |
581 | let mut parts = Vec::new(); | |
582 | for entry in name.entries() { | |
583 | parts.push(format!("{} = {}", entry.object().nid().short_name()?, entry.data().as_utf8()?)); | |
584 | } | |
585 | Ok(parts.join(", ")) | |
586 | } | |
587 | ||
588 | #[api] | |
589 | /// Diplay node certificate information. | |
590 | fn cert_info() -> Result<(), Error> { | |
591 | ||
592 | let cert_path = PathBuf::from(configdir!("/proxy.pem")); | |
593 | ||
594 | let cert_pem = proxmox::tools::fs::file_get_contents(&cert_path)?; | |
595 | ||
596 | let cert = openssl::x509::X509::from_pem(&cert_pem)?; | |
597 | ||
598 | println!("Subject: {}", x509name_to_string(cert.subject_name())?); | |
599 | ||
600 | if let Some(san) = cert.subject_alt_names() { | |
601 | for name in san.iter() { | |
602 | if let Some(v) = name.dnsname() { | |
603 | println!(" DNS:{}", v); | |
604 | } else if let Some(v) = name.ipaddress() { | |
605 | println!(" IP:{:?}", v); | |
606 | } else if let Some(v) = name.email() { | |
607 | println!(" EMAIL:{}", v); | |
608 | } else if let Some(v) = name.uri() { | |
609 | println!(" URI:{}", v); | |
610 | } | |
611 | } | |
612 | } | |
613 | ||
614 | println!("Issuer: {}", x509name_to_string(cert.issuer_name())?); | |
615 | println!("Validity:"); | |
616 | println!(" Not Before: {}", cert.not_before()); | |
617 | println!(" Not After : {}", cert.not_after()); | |
618 | ||
619 | let fp = cert.digest(openssl::hash::MessageDigest::sha256())?; | |
620 | let fp_string = proxmox::tools::digest_to_hex(&fp); | |
621 | let fp_string = fp_string.as_bytes().chunks(2).map(|v| std::str::from_utf8(v).unwrap()) | |
622 | .collect::<Vec<&str>>().join(":"); | |
623 | ||
624 | println!("Fingerprint (sha256): {}", fp_string); | |
625 | ||
626 | let pubkey = cert.public_key()?; | |
627 | println!("Public key type: {}", openssl::nid::Nid::from_raw(pubkey.id().as_raw()).long_name()?); | |
628 | println!("Public key bits: {}", pubkey.bits()); | |
629 | ||
630 | Ok(()) | |
631 | } | |
632 | ||
550e0d88 DM |
633 | #[api( |
634 | input: { | |
635 | properties: { | |
636 | force: { | |
637 | description: "Force generation of new SSL certifate.", | |
638 | type: Boolean, | |
639 | optional:true, | |
640 | }, | |
641 | } | |
642 | }, | |
643 | )] | |
644 | /// Update node certificates and generate all needed files/directories. | |
645 | fn update_certs(force: Option<bool>) -> Result<(), Error> { | |
646 | ||
550e0d88 DM |
647 | config::create_configdir()?; |
648 | ||
649 | if let Err(err) = generate_auth_key() { | |
650 | bail!("unable to generate auth key - {}", err); | |
651 | } | |
652 | ||
653 | if let Err(err) = generate_csrf_key() { | |
654 | bail!("unable to generate csrf key - {}", err); | |
655 | } | |
656 | ||
f8fd5095 | 657 | config::update_self_signed_cert(force.unwrap_or(false))?; |
550e0d88 DM |
658 | |
659 | Ok(()) | |
660 | } | |
661 | ||
662 | fn cert_mgmt_cli() -> CommandLineInterface { | |
663 | ||
550e0d88 | 664 | let cmd_def = CliCommandMap::new() |
e739a8d8 DM |
665 | .insert("info", CliCommand::new(&API_METHOD_CERT_INFO)) |
666 | .insert("update", CliCommand::new(&API_METHOD_UPDATE_CERTS)); | |
550e0d88 DM |
667 | |
668 | cmd_def.into() | |
669 | } | |
670 | ||
4b4eba0b | 671 | // fixme: avoid API redefinition |
0eb0e024 DM |
672 | #[api( |
673 | input: { | |
674 | properties: { | |
eb506c83 | 675 | "local-store": { |
0eb0e024 DM |
676 | schema: DATASTORE_SCHEMA, |
677 | }, | |
678 | remote: { | |
167971ed | 679 | schema: REMOTE_ID_SCHEMA, |
0eb0e024 DM |
680 | }, |
681 | "remote-store": { | |
682 | schema: DATASTORE_SCHEMA, | |
683 | }, | |
4b4eba0b DM |
684 | delete: { |
685 | description: "Delete vanished backups. This remove the local copy if the remote backup was deleted.", | |
686 | type: Boolean, | |
687 | optional: true, | |
688 | default: true, | |
689 | }, | |
0eb0e024 DM |
690 | "output-format": { |
691 | schema: OUTPUT_FORMAT, | |
692 | optional: true, | |
693 | }, | |
694 | } | |
695 | } | |
696 | )] | |
eb506c83 DM |
697 | /// Sync datastore from another repository |
698 | async fn pull_datastore( | |
0eb0e024 DM |
699 | remote: String, |
700 | remote_store: String, | |
eb506c83 | 701 | local_store: String, |
4b4eba0b | 702 | delete: Option<bool>, |
ac3faaf5 | 703 | param: Value, |
0eb0e024 DM |
704 | ) -> Result<Value, Error> { |
705 | ||
ac3faaf5 | 706 | let output_format = get_output_format(¶m); |
0eb0e024 DM |
707 | |
708 | let mut client = connect()?; | |
709 | ||
4b4eba0b | 710 | let mut args = json!({ |
eb506c83 | 711 | "store": local_store, |
94609e23 | 712 | "remote": remote, |
0eb0e024 | 713 | "remote-store": remote_store, |
0eb0e024 DM |
714 | }); |
715 | ||
4b4eba0b DM |
716 | if let Some(delete) = delete { |
717 | args["delete"] = delete.into(); | |
718 | } | |
719 | ||
eb506c83 | 720 | let result = client.post("api2/json/pull", Some(args)).await?; |
0eb0e024 DM |
721 | |
722 | view_task_result(client, result, &output_format).await?; | |
723 | ||
724 | Ok(Value::Null) | |
725 | } | |
726 | ||
211fabd7 DM |
727 | fn main() { |
728 | ||
6460764d | 729 | let cmd_def = CliCommandMap::new() |
ed3e60ae | 730 | .insert("acl", acl_commands()) |
48ef3c33 | 731 | .insert("datastore", datastore_commands()) |
ca0e5347 | 732 | .insert("network", network_commands()) |
579728c6 | 733 | .insert("user", user_commands()) |
f357390c | 734 | .insert("remote", remote_commands()) |
47d47121 | 735 | .insert("garbage-collection", garbage_collection_commands()) |
550e0d88 | 736 | .insert("cert", cert_mgmt_cli()) |
0eb0e024 DM |
737 | .insert("task", task_mgmt_cli()) |
738 | .insert( | |
eb506c83 DM |
739 | "pull", |
740 | CliCommand::new(&API_METHOD_PULL_DATASTORE) | |
741 | .arg_param(&["remote", "remote-store", "local-store"]) | |
742 | .completion_cb("local-store", config::datastore::complete_datastore_name) | |
f357390c | 743 | .completion_cb("remote", config::remote::complete_remote_name) |
331b869d | 744 | .completion_cb("remote-store", complete_remote_datastore_name) |
0eb0e024 | 745 | ); |
34d3ba52 | 746 | |
d08bc483 | 747 | proxmox_backup::tools::runtime::main(run_async_cli_command(cmd_def)); |
ea0b8b6e | 748 | } |
331b869d DM |
749 | |
750 | // shell completion helper | |
751 | pub fn complete_remote_datastore_name(_arg: &str, param: &HashMap<String, String>) -> Vec<String> { | |
752 | ||
753 | let mut list = Vec::new(); | |
754 | ||
9ea4bce4 | 755 | let _ = proxmox::try_block!({ |
331b869d | 756 | let remote = param.get("remote").ok_or_else(|| format_err!("no remote"))?; |
f357390c | 757 | let (remote_config, _digest) = remote::config()?; |
331b869d DM |
758 | |
759 | let remote: Remote = remote_config.lookup("remote", &remote)?; | |
760 | ||
d59dbeca DM |
761 | let options = HttpClientOptions::new() |
762 | .password(Some(remote.password.clone())) | |
763 | .fingerprint(remote.fingerprint.clone()); | |
764 | ||
331b869d DM |
765 | let client = HttpClient::new( |
766 | &remote.host, | |
767 | &remote.userid, | |
d59dbeca | 768 | options, |
331b869d DM |
769 | )?; |
770 | ||
03ac286c | 771 | let result = crate::tools::runtime::block_on(client.get("api2/json/admin/datastore", None))?; |
331b869d DM |
772 | |
773 | if let Some(data) = result["data"].as_array() { | |
774 | for item in data { | |
775 | if let Some(store) = item["store"].as_str() { | |
776 | list.push(store.to_owned()); | |
777 | } | |
778 | } | |
779 | } | |
780 | ||
781 | Ok(()) | |
782 | }).map_err(|_err: Error| { /* ignore */ }); | |
783 | ||
784 | list | |
785 | } |