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 use proxmox_backup
::config
;
14 use proxmox_backup
::api2
::{self, types::* }
;
15 use proxmox_backup
::server
::wait_for_local_worker
;
17 mod proxmox_backup_manager
;
18 use proxmox_backup_manager
::*;
24 schema
: DATASTORE_SCHEMA
,
27 schema
: OUTPUT_FORMAT
,
33 /// Start garbage collection for a specific datastore.
34 async
fn start_garbage_collection(param
: Value
) -> Result
<Value
, Error
> {
36 let output_format
= get_output_format(¶m
);
38 let store
= required_string_param(¶m
, "store")?
;
40 let mut client
= connect_to_localhost()?
;
42 let path
= format
!("api2/json/admin/datastore/{}/gc", store
);
44 let result
= client
.post(&path
, None
).await?
;
46 view_task_result(&mut client
, result
, &output_format
).await?
;
55 schema
: DATASTORE_SCHEMA
,
58 schema
: OUTPUT_FORMAT
,
64 /// Show garbage collection status for a specific datastore.
65 async
fn garbage_collection_status(param
: Value
) -> Result
<Value
, Error
> {
67 let output_format
= get_output_format(¶m
);
69 let store
= required_string_param(¶m
, "store")?
;
71 let client
= connect_to_localhost()?
;
73 let path
= format
!("api2/json/admin/datastore/{}/gc", store
);
75 let mut result
= client
.get(&path
, None
).await?
;
76 let mut data
= result
["data"].take();
77 let return_type
= &api2
::admin
::datastore
::API_METHOD_GARBAGE_COLLECTION_STATUS
.returns
;
79 let options
= default_table_format_options();
81 format_and_print_result_full(&mut data
, return_type
, &output_format
, &options
);
86 fn garbage_collection_commands() -> CommandLineInterface
{
88 let cmd_def
= CliCommandMap
::new()
90 CliCommand
::new(&API_METHOD_GARBAGE_COLLECTION_STATUS
)
91 .arg_param(&["store"])
92 .completion_cb("store", config
::datastore
::complete_datastore_name
)
95 CliCommand
::new(&API_METHOD_START_GARBAGE_COLLECTION
)
96 .arg_param(&["store"])
97 .completion_cb("store", config
::datastore
::complete_datastore_name
)
107 description
: "The maximal number of tasks to list.",
115 schema
: OUTPUT_FORMAT
,
120 description
: "Also list stopped tasks.",
126 /// List running server tasks.
127 async
fn task_list(param
: Value
) -> Result
<Value
, Error
> {
129 let output_format
= get_output_format(¶m
);
131 let client
= connect_to_localhost()?
;
133 let limit
= param
["limit"].as_u64().unwrap_or(50) as usize;
134 let running
= !param
["all"].as_bool().unwrap_or(false);
140 let mut result
= client
.get("api2/json/nodes/localhost/tasks", Some(args
)).await?
;
142 let mut data
= result
["data"].take();
143 let return_type
= &api2
::node
::tasks
::API_METHOD_LIST_TASKS
.returns
;
145 use pbs_tools
::format
::{render_epoch, render_task_status}
;
146 let options
= default_table_format_options()
147 .column(ColumnConfig
::new("starttime").right_align(false).renderer(render_epoch
))
148 .column(ColumnConfig
::new("endtime").right_align(false).renderer(render_epoch
))
149 .column(ColumnConfig
::new("upid"))
150 .column(ColumnConfig
::new("status").renderer(render_task_status
));
152 format_and_print_result_full(&mut data
, return_type
, &output_format
, &options
);
166 /// Display the task log.
167 async
fn task_log(param
: Value
) -> Result
<Value
, Error
> {
169 let upid
= required_string_param(¶m
, "upid")?
;
171 let mut client
= connect_to_localhost()?
;
173 display_task_log(&mut client
, upid
, true).await?
;
187 /// Try to stop a specific task.
188 async
fn task_stop(param
: Value
) -> Result
<Value
, Error
> {
190 let upid_str
= required_string_param(¶m
, "upid")?
;
192 let mut client
= connect_to_localhost()?
;
194 let path
= format
!("api2/json/nodes/localhost/tasks/{}", percent_encode_component(upid_str
));
195 let _
= client
.delete(&path
, None
).await?
;
200 fn task_mgmt_cli() -> CommandLineInterface
{
202 let task_log_cmd_def
= CliCommand
::new(&API_METHOD_TASK_LOG
)
203 .arg_param(&["upid"]);
205 let task_stop_cmd_def
= CliCommand
::new(&API_METHOD_TASK_STOP
)
206 .arg_param(&["upid"]);
208 let cmd_def
= CliCommandMap
::new()
209 .insert("list", CliCommand
::new(&API_METHOD_TASK_LIST
))
210 .insert("log", task_log_cmd_def
)
211 .insert("stop", task_stop_cmd_def
);
216 // fixme: avoid API redefinition
221 schema
: DATASTORE_SCHEMA
,
224 schema
: REMOTE_ID_SCHEMA
,
227 schema
: DATASTORE_SCHEMA
,
230 schema
: REMOVE_VANISHED_BACKUPS_SCHEMA
,
234 schema
: OUTPUT_FORMAT
,
240 /// Sync datastore from another repository
241 async
fn pull_datastore(
243 remote_store
: String
,
245 remove_vanished
: Option
<bool
>,
247 ) -> Result
<Value
, Error
> {
249 let output_format
= get_output_format(¶m
);
251 let mut client
= connect_to_localhost()?
;
253 let mut args
= json
!({
254 "store": local_store
,
256 "remote-store": remote_store
,
259 if let Some(remove_vanished
) = remove_vanished
{
260 args
["remove-vanished"] = Value
::from(remove_vanished
);
263 let result
= client
.post("api2/json/pull", Some(args
)).await?
;
265 view_task_result(&mut client
, result
, &output_format
).await?
;
274 schema
: DATASTORE_SCHEMA
,
277 schema
: IGNORE_VERIFIED_BACKUPS_SCHEMA
,
281 schema
: VERIFICATION_OUTDATED_AFTER_SCHEMA
,
285 schema
: OUTPUT_FORMAT
,
295 ) -> Result
<Value
, Error
> {
297 let output_format
= extract_output_format(&mut param
);
299 let mut client
= connect_to_localhost()?
;
301 let args
= json
!(param
);
303 let path
= format
!("api2/json/admin/datastore/{}/verify", store
);
305 let result
= client
.post(&path
, Some(args
)).await?
;
307 view_task_result(&mut client
, result
, &output_format
).await?
;
314 async
fn report() -> Result
<Value
, Error
> {
315 let report
= proxmox_backup
::server
::generate_report();
316 io
::stdout().write_all(report
.as_bytes())?
;
327 description
: "Output verbose package information. It is ignored if output-format is specified.",
330 schema
: OUTPUT_FORMAT
,
336 /// List package versions for important Proxmox Backup Server packages.
337 async
fn get_versions(verbose
: bool
, param
: Value
) -> Result
<Value
, Error
> {
338 let output_format
= get_output_format(¶m
);
340 let packages
= crate::api2
::node
::apt
::get_versions()?
;
341 let mut packages
= json
!(if verbose { &packages[..] }
else { &packages[1..2] }
);
343 let options
= default_table_format_options()
345 .noborder(true) // just not helpful for version info which gets copy pasted often
346 .column(ColumnConfig
::new("Package"))
347 .column(ColumnConfig
::new("Version"))
348 .column(ColumnConfig
::new("ExtraInfo").header("Extra Info"))
350 let return_type
= &crate::api2
::node
::apt
::API_METHOD_GET_VERSIONS
.returns
;
352 format_and_print_result_full(&mut packages
, return_type
, &output_format
, &options
);
359 proxmox_backup
::tools
::setup_safe_path_env();
361 let cmd_def
= CliCommandMap
::new()
362 .insert("acl", acl_commands())
363 .insert("datastore", datastore_commands())
364 .insert("disk", disk_commands())
365 .insert("dns", dns_commands())
366 .insert("network", network_commands())
367 .insert("node", node_commands())
368 .insert("user", user_commands())
369 .insert("openid", openid_commands())
370 .insert("remote", remote_commands())
371 .insert("garbage-collection", garbage_collection_commands())
372 .insert("acme", acme_mgmt_cli())
373 .insert("cert", cert_mgmt_cli())
374 .insert("subscription", subscription_commands())
375 .insert("sync-job", sync_job_commands())
376 .insert("verify-job", verify_job_commands())
377 .insert("task", task_mgmt_cli())
380 CliCommand
::new(&API_METHOD_PULL_DATASTORE
)
381 .arg_param(&["remote", "remote-store", "local-store"])
382 .completion_cb("local-store", config
::datastore
::complete_datastore_name
)
383 .completion_cb("remote", pbs_config
::remote
::complete_remote_name
)
384 .completion_cb("remote-store", complete_remote_datastore_name
)
388 CliCommand
::new(&API_METHOD_VERIFY
)
389 .arg_param(&["store"])
390 .completion_cb("store", config
::datastore
::complete_datastore_name
)
393 CliCommand
::new(&API_METHOD_REPORT
)
396 CliCommand
::new(&API_METHOD_GET_VERSIONS
)
401 let mut rpcenv
= CliEnvironment
::new();
402 rpcenv
.set_auth_id(Some(String
::from("root@pam")));
404 pbs_runtime
::main(run_async_cli_command(cmd_def
, rpcenv
));
407 // shell completion helper
408 pub fn complete_remote_datastore_name(_arg
: &str, param
: &HashMap
<String
, String
>) -> Vec
<String
> {
410 let mut list
= Vec
::new();
412 let _
= proxmox
::try_block
!({
413 let remote
= param
.get("remote").ok_or_else(|| format_err
!("no remote"))?
;
415 let data
= pbs_runtime
::block_on(async
move {
416 crate::api2
::config
::remote
::scan_remote_datastores(remote
.clone()).await
420 list
.push(item
.store
);
424 }).map_err(|_err
: Error
| { /* ignore */ }
);