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_backup
::config
;
18 use proxmox_backup
::api2
;
19 use proxmox_backup
::server
::wait_for_local_worker
;
21 mod proxmox_backup_manager
;
22 use proxmox_backup_manager
::*;
28 schema
: DATASTORE_SCHEMA
,
31 schema
: OUTPUT_FORMAT
,
37 /// Start garbage collection for a specific datastore.
38 async
fn start_garbage_collection(param
: Value
) -> Result
<Value
, Error
> {
40 let output_format
= get_output_format(¶m
);
42 let store
= required_string_param(¶m
, "store")?
;
44 let mut client
= connect_to_localhost()?
;
46 let path
= format
!("api2/json/admin/datastore/{}/gc", store
);
48 let result
= client
.post(&path
, None
).await?
;
50 view_task_result(&mut client
, result
, &output_format
).await?
;
59 schema
: DATASTORE_SCHEMA
,
62 schema
: OUTPUT_FORMAT
,
68 /// Show garbage collection status for a specific datastore.
69 async
fn garbage_collection_status(param
: Value
) -> Result
<Value
, Error
> {
71 let output_format
= get_output_format(¶m
);
73 let store
= required_string_param(¶m
, "store")?
;
75 let client
= connect_to_localhost()?
;
77 let path
= format
!("api2/json/admin/datastore/{}/gc", store
);
79 let mut result
= client
.get(&path
, None
).await?
;
80 let mut data
= result
["data"].take();
81 let return_type
= &api2
::admin
::datastore
::API_METHOD_GARBAGE_COLLECTION_STATUS
.returns
;
83 let options
= default_table_format_options();
85 format_and_print_result_full(&mut data
, return_type
, &output_format
, &options
);
90 fn garbage_collection_commands() -> CommandLineInterface
{
92 let cmd_def
= CliCommandMap
::new()
94 CliCommand
::new(&API_METHOD_GARBAGE_COLLECTION_STATUS
)
95 .arg_param(&["store"])
96 .completion_cb("store", pbs_config
::datastore
::complete_datastore_name
)
99 CliCommand
::new(&API_METHOD_START_GARBAGE_COLLECTION
)
100 .arg_param(&["store"])
101 .completion_cb("store", pbs_config
::datastore
::complete_datastore_name
)
111 description
: "The maximal number of tasks to list.",
119 schema
: OUTPUT_FORMAT
,
124 description
: "Also list stopped tasks.",
130 /// List running server tasks.
131 async
fn task_list(param
: Value
) -> Result
<Value
, Error
> {
133 let output_format
= get_output_format(¶m
);
135 let client
= connect_to_localhost()?
;
137 let limit
= param
["limit"].as_u64().unwrap_or(50) as usize;
138 let running
= !param
["all"].as_bool().unwrap_or(false);
144 let mut result
= client
.get("api2/json/nodes/localhost/tasks", Some(args
)).await?
;
146 let mut data
= result
["data"].take();
147 let return_type
= &api2
::node
::tasks
::API_METHOD_LIST_TASKS
.returns
;
149 use pbs_tools
::format
::{render_epoch, render_task_status}
;
150 let options
= default_table_format_options()
151 .column(ColumnConfig
::new("starttime").right_align(false).renderer(render_epoch
))
152 .column(ColumnConfig
::new("endtime").right_align(false).renderer(render_epoch
))
153 .column(ColumnConfig
::new("upid"))
154 .column(ColumnConfig
::new("status").renderer(render_task_status
));
156 format_and_print_result_full(&mut data
, return_type
, &output_format
, &options
);
170 /// Display the task log.
171 async
fn task_log(param
: Value
) -> Result
<Value
, Error
> {
173 let upid
= required_string_param(¶m
, "upid")?
;
175 let mut client
= connect_to_localhost()?
;
177 display_task_log(&mut client
, upid
, true).await?
;
191 /// Try to stop a specific task.
192 async
fn task_stop(param
: Value
) -> Result
<Value
, Error
> {
194 let upid_str
= required_string_param(¶m
, "upid")?
;
196 let mut client
= connect_to_localhost()?
;
198 let path
= format
!("api2/json/nodes/localhost/tasks/{}", percent_encode_component(upid_str
));
199 let _
= client
.delete(&path
, None
).await?
;
204 fn task_mgmt_cli() -> CommandLineInterface
{
206 let task_log_cmd_def
= CliCommand
::new(&API_METHOD_TASK_LOG
)
207 .arg_param(&["upid"]);
209 let task_stop_cmd_def
= CliCommand
::new(&API_METHOD_TASK_STOP
)
210 .arg_param(&["upid"]);
212 let cmd_def
= CliCommandMap
::new()
213 .insert("list", CliCommand
::new(&API_METHOD_TASK_LIST
))
214 .insert("log", task_log_cmd_def
)
215 .insert("stop", task_stop_cmd_def
);
220 // fixme: avoid API redefinition
225 schema
: DATASTORE_SCHEMA
,
228 schema
: REMOTE_ID_SCHEMA
,
231 schema
: DATASTORE_SCHEMA
,
234 schema
: REMOVE_VANISHED_BACKUPS_SCHEMA
,
238 schema
: OUTPUT_FORMAT
,
244 /// Sync datastore from another repository
245 async
fn pull_datastore(
247 remote_store
: String
,
249 remove_vanished
: Option
<bool
>,
251 ) -> Result
<Value
, Error
> {
253 let output_format
= get_output_format(¶m
);
255 let mut client
= connect_to_localhost()?
;
257 let mut args
= json
!({
258 "store": local_store
,
260 "remote-store": remote_store
,
263 if let Some(remove_vanished
) = remove_vanished
{
264 args
["remove-vanished"] = Value
::from(remove_vanished
);
267 let result
= client
.post("api2/json/pull", Some(args
)).await?
;
269 view_task_result(&mut client
, result
, &output_format
).await?
;
278 schema
: DATASTORE_SCHEMA
,
281 schema
: IGNORE_VERIFIED_BACKUPS_SCHEMA
,
285 schema
: VERIFICATION_OUTDATED_AFTER_SCHEMA
,
289 schema
: OUTPUT_FORMAT
,
299 ) -> Result
<Value
, Error
> {
301 let output_format
= extract_output_format(&mut param
);
303 let mut client
= connect_to_localhost()?
;
305 let args
= json
!(param
);
307 let path
= format
!("api2/json/admin/datastore/{}/verify", store
);
309 let result
= client
.post(&path
, Some(args
)).await?
;
311 view_task_result(&mut client
, result
, &output_format
).await?
;
318 async
fn report() -> Result
<Value
, Error
> {
319 let report
= proxmox_backup
::server
::generate_report();
320 io
::stdout().write_all(report
.as_bytes())?
;
331 description
: "Output verbose package information. It is ignored if output-format is specified.",
334 schema
: OUTPUT_FORMAT
,
340 /// List package versions for important Proxmox Backup Server packages.
341 async
fn get_versions(verbose
: bool
, param
: Value
) -> Result
<Value
, Error
> {
342 let output_format
= get_output_format(¶m
);
344 let packages
= crate::api2
::node
::apt
::get_versions()?
;
345 let mut packages
= json
!(if verbose { &packages[..] }
else { &packages[1..2] }
);
347 let options
= default_table_format_options()
349 .noborder(true) // just not helpful for version info which gets copy pasted often
350 .column(ColumnConfig
::new("Package"))
351 .column(ColumnConfig
::new("Version"))
352 .column(ColumnConfig
::new("ExtraInfo").header("Extra Info"))
354 let return_type
= &crate::api2
::node
::apt
::API_METHOD_GET_VERSIONS
.returns
;
356 format_and_print_result_full(&mut packages
, return_type
, &output_format
, &options
);
363 proxmox_backup
::tools
::setup_safe_path_env();
365 let cmd_def
= CliCommandMap
::new()
366 .insert("acl", acl_commands())
367 .insert("datastore", datastore_commands())
368 .insert("disk", disk_commands())
369 .insert("dns", dns_commands())
370 .insert("network", network_commands())
371 .insert("node", node_commands())
372 .insert("user", user_commands())
373 .insert("openid", openid_commands())
374 .insert("remote", remote_commands())
375 .insert("garbage-collection", garbage_collection_commands())
376 .insert("acme", acme_mgmt_cli())
377 .insert("cert", cert_mgmt_cli())
378 .insert("subscription", subscription_commands())
379 .insert("sync-job", sync_job_commands())
380 .insert("verify-job", verify_job_commands())
381 .insert("task", task_mgmt_cli())
384 CliCommand
::new(&API_METHOD_PULL_DATASTORE
)
385 .arg_param(&["remote", "remote-store", "local-store"])
386 .completion_cb("local-store", pbs_config
::datastore
::complete_datastore_name
)
387 .completion_cb("remote", pbs_config
::remote
::complete_remote_name
)
388 .completion_cb("remote-store", complete_remote_datastore_name
)
392 CliCommand
::new(&API_METHOD_VERIFY
)
393 .arg_param(&["store"])
394 .completion_cb("store", pbs_config
::datastore
::complete_datastore_name
)
397 CliCommand
::new(&API_METHOD_REPORT
)
400 CliCommand
::new(&API_METHOD_GET_VERSIONS
)
405 let mut rpcenv
= CliEnvironment
::new();
406 rpcenv
.set_auth_id(Some(String
::from("root@pam")));
408 pbs_runtime
::main(run_async_cli_command(cmd_def
, rpcenv
));
411 // shell completion helper
412 pub fn complete_remote_datastore_name(_arg
: &str, param
: &HashMap
<String
, String
>) -> Vec
<String
> {
414 let mut list
= Vec
::new();
416 let _
= proxmox
::try_block
!({
417 let remote
= param
.get("remote").ok_or_else(|| format_err
!("no remote"))?
;
419 let data
= pbs_runtime
::block_on(async
move {
420 crate::api2
::config
::remote
::scan_remote_datastores(remote
.clone()).await
424 list
.push(item
.store
);
428 }).map_err(|_err
: Error
| { /* ignore */ }
);