1 use std
::collections
::HashMap
;
2 use std
::io
::{self, Write}
;
4 use anyhow
::{format_err, Error}
;
5 use serde_json
::{json, Value}
;
7 use proxmox
::api
::{api, cli::*, RpcEnvironment}
;
9 use pbs_client
::{connect_to_localhost, display_task_log, view_task_result}
;
10 use pbs_tools
::percent_encoding
::percent_encode_component
;
11 use pbs_tools
::json
::required_string_param
;
13 DATASTORE_SCHEMA
, UPID_SCHEMA
, REMOTE_ID_SCHEMA
, REMOVE_VANISHED_BACKUPS_SCHEMA
,
14 IGNORE_VERIFIED_BACKUPS_SCHEMA
, VERIFICATION_OUTDATED_AFTER_SCHEMA
,
17 use proxmox_rest_server
::wait_for_local_worker
;
19 use proxmox_backup
::config
;
20 use proxmox_backup
::api2
;
22 mod proxmox_backup_manager
;
23 use proxmox_backup_manager
::*;
29 schema
: DATASTORE_SCHEMA
,
32 schema
: OUTPUT_FORMAT
,
38 /// Start garbage collection for a specific datastore.
39 async
fn start_garbage_collection(param
: Value
) -> Result
<Value
, Error
> {
41 let output_format
= get_output_format(¶m
);
43 let store
= required_string_param(¶m
, "store")?
;
45 let mut client
= connect_to_localhost()?
;
47 let path
= format
!("api2/json/admin/datastore/{}/gc", store
);
49 let result
= client
.post(&path
, None
).await?
;
51 view_task_result(&mut client
, result
, &output_format
).await?
;
60 schema
: DATASTORE_SCHEMA
,
63 schema
: OUTPUT_FORMAT
,
69 /// Show garbage collection status for a specific datastore.
70 async
fn garbage_collection_status(param
: Value
) -> Result
<Value
, Error
> {
72 let output_format
= get_output_format(¶m
);
74 let store
= required_string_param(¶m
, "store")?
;
76 let client
= connect_to_localhost()?
;
78 let path
= format
!("api2/json/admin/datastore/{}/gc", store
);
80 let mut result
= client
.get(&path
, None
).await?
;
81 let mut data
= result
["data"].take();
82 let return_type
= &api2
::admin
::datastore
::API_METHOD_GARBAGE_COLLECTION_STATUS
.returns
;
84 let options
= default_table_format_options();
86 format_and_print_result_full(&mut data
, return_type
, &output_format
, &options
);
91 fn garbage_collection_commands() -> CommandLineInterface
{
93 let cmd_def
= CliCommandMap
::new()
95 CliCommand
::new(&API_METHOD_GARBAGE_COLLECTION_STATUS
)
96 .arg_param(&["store"])
97 .completion_cb("store", pbs_config
::datastore
::complete_datastore_name
)
100 CliCommand
::new(&API_METHOD_START_GARBAGE_COLLECTION
)
101 .arg_param(&["store"])
102 .completion_cb("store", pbs_config
::datastore
::complete_datastore_name
)
112 description
: "The maximal number of tasks to list.",
120 schema
: OUTPUT_FORMAT
,
125 description
: "Also list stopped tasks.",
131 /// List running server tasks.
132 async
fn task_list(param
: Value
) -> Result
<Value
, Error
> {
134 let output_format
= get_output_format(¶m
);
136 let client
= connect_to_localhost()?
;
138 let limit
= param
["limit"].as_u64().unwrap_or(50) as usize;
139 let running
= !param
["all"].as_bool().unwrap_or(false);
145 let mut result
= client
.get("api2/json/nodes/localhost/tasks", Some(args
)).await?
;
147 let mut data
= result
["data"].take();
148 let return_type
= &api2
::node
::tasks
::API_METHOD_LIST_TASKS
.returns
;
150 use pbs_tools
::format
::{render_epoch, render_task_status}
;
151 let options
= default_table_format_options()
152 .column(ColumnConfig
::new("starttime").right_align(false).renderer(render_epoch
))
153 .column(ColumnConfig
::new("endtime").right_align(false).renderer(render_epoch
))
154 .column(ColumnConfig
::new("upid"))
155 .column(ColumnConfig
::new("status").renderer(render_task_status
));
157 format_and_print_result_full(&mut data
, return_type
, &output_format
, &options
);
171 /// Display the task log.
172 async
fn task_log(param
: Value
) -> Result
<Value
, Error
> {
174 let upid
= required_string_param(¶m
, "upid")?
;
176 let mut client
= connect_to_localhost()?
;
178 display_task_log(&mut client
, upid
, true).await?
;
192 /// Try to stop a specific task.
193 async
fn task_stop(param
: Value
) -> Result
<Value
, Error
> {
195 let upid_str
= required_string_param(¶m
, "upid")?
;
197 let mut client
= connect_to_localhost()?
;
199 let path
= format
!("api2/json/nodes/localhost/tasks/{}", percent_encode_component(upid_str
));
200 let _
= client
.delete(&path
, None
).await?
;
205 fn task_mgmt_cli() -> CommandLineInterface
{
207 let task_log_cmd_def
= CliCommand
::new(&API_METHOD_TASK_LOG
)
208 .arg_param(&["upid"]);
210 let task_stop_cmd_def
= CliCommand
::new(&API_METHOD_TASK_STOP
)
211 .arg_param(&["upid"]);
213 let cmd_def
= CliCommandMap
::new()
214 .insert("list", CliCommand
::new(&API_METHOD_TASK_LIST
))
215 .insert("log", task_log_cmd_def
)
216 .insert("stop", task_stop_cmd_def
);
221 // fixme: avoid API redefinition
226 schema
: DATASTORE_SCHEMA
,
229 schema
: REMOTE_ID_SCHEMA
,
232 schema
: DATASTORE_SCHEMA
,
235 schema
: REMOVE_VANISHED_BACKUPS_SCHEMA
,
239 schema
: OUTPUT_FORMAT
,
245 /// Sync datastore from another repository
246 async
fn pull_datastore(
248 remote_store
: String
,
250 remove_vanished
: Option
<bool
>,
252 ) -> Result
<Value
, Error
> {
254 let output_format
= get_output_format(¶m
);
256 let mut client
= connect_to_localhost()?
;
258 let mut args
= json
!({
259 "store": local_store
,
261 "remote-store": remote_store
,
264 if let Some(remove_vanished
) = remove_vanished
{
265 args
["remove-vanished"] = Value
::from(remove_vanished
);
268 let result
= client
.post("api2/json/pull", Some(args
)).await?
;
270 view_task_result(&mut client
, result
, &output_format
).await?
;
279 schema
: DATASTORE_SCHEMA
,
282 schema
: IGNORE_VERIFIED_BACKUPS_SCHEMA
,
286 schema
: VERIFICATION_OUTDATED_AFTER_SCHEMA
,
290 schema
: OUTPUT_FORMAT
,
300 ) -> Result
<Value
, Error
> {
302 let output_format
= extract_output_format(&mut param
);
304 let mut client
= connect_to_localhost()?
;
306 let args
= json
!(param
);
308 let path
= format
!("api2/json/admin/datastore/{}/verify", store
);
310 let result
= client
.post(&path
, Some(args
)).await?
;
312 view_task_result(&mut client
, result
, &output_format
).await?
;
319 async
fn report() -> Result
<Value
, Error
> {
320 let report
= proxmox_backup
::server
::generate_report();
321 io
::stdout().write_all(report
.as_bytes())?
;
332 description
: "Output verbose package information. It is ignored if output-format is specified.",
335 schema
: OUTPUT_FORMAT
,
341 /// List package versions for important Proxmox Backup Server packages.
342 async
fn get_versions(verbose
: bool
, param
: Value
) -> Result
<Value
, Error
> {
343 let output_format
= get_output_format(¶m
);
345 let packages
= crate::api2
::node
::apt
::get_versions()?
;
346 let mut packages
= json
!(if verbose { &packages[..] }
else { &packages[1..2] }
);
348 let options
= default_table_format_options()
350 .noborder(true) // just not helpful for version info which gets copy pasted often
351 .column(ColumnConfig
::new("Package"))
352 .column(ColumnConfig
::new("Version"))
353 .column(ColumnConfig
::new("ExtraInfo").header("Extra Info"))
355 let return_type
= &crate::api2
::node
::apt
::API_METHOD_GET_VERSIONS
.returns
;
357 format_and_print_result_full(&mut packages
, return_type
, &output_format
, &options
);
364 proxmox_backup
::tools
::setup_safe_path_env();
366 let cmd_def
= CliCommandMap
::new()
367 .insert("acl", acl_commands())
368 .insert("datastore", datastore_commands())
369 .insert("disk", disk_commands())
370 .insert("dns", dns_commands())
371 .insert("network", network_commands())
372 .insert("node", node_commands())
373 .insert("user", user_commands())
374 .insert("openid", openid_commands())
375 .insert("remote", remote_commands())
376 .insert("garbage-collection", garbage_collection_commands())
377 .insert("acme", acme_mgmt_cli())
378 .insert("cert", cert_mgmt_cli())
379 .insert("subscription", subscription_commands())
380 .insert("sync-job", sync_job_commands())
381 .insert("verify-job", verify_job_commands())
382 .insert("task", task_mgmt_cli())
385 CliCommand
::new(&API_METHOD_PULL_DATASTORE
)
386 .arg_param(&["remote", "remote-store", "local-store"])
387 .completion_cb("local-store", pbs_config
::datastore
::complete_datastore_name
)
388 .completion_cb("remote", pbs_config
::remote
::complete_remote_name
)
389 .completion_cb("remote-store", complete_remote_datastore_name
)
393 CliCommand
::new(&API_METHOD_VERIFY
)
394 .arg_param(&["store"])
395 .completion_cb("store", pbs_config
::datastore
::complete_datastore_name
)
398 CliCommand
::new(&API_METHOD_REPORT
)
401 CliCommand
::new(&API_METHOD_GET_VERSIONS
)
406 let mut rpcenv
= CliEnvironment
::new();
407 rpcenv
.set_auth_id(Some(String
::from("root@pam")));
409 pbs_runtime
::main(run_async_cli_command(cmd_def
, rpcenv
));
412 // shell completion helper
413 pub fn complete_remote_datastore_name(_arg
: &str, param
: &HashMap
<String
, String
>) -> Vec
<String
> {
415 let mut list
= Vec
::new();
417 let _
= proxmox
::try_block
!({
418 let remote
= param
.get("remote").ok_or_else(|| format_err
!("no remote"))?
;
420 let data
= pbs_runtime
::block_on(async
move {
421 crate::api2
::config
::remote
::scan_remote_datastores(remote
.clone()).await
425 list
.push(item
.store
);
429 }).map_err(|_err
: Error
| { /* ignore */ }
);