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 proxmox_backup
::tools
;
10 use proxmox_backup
::config
;
11 use proxmox_backup
::api2
::{self, types::* }
;
12 use proxmox_backup
::client
::*;
14 mod proxmox_backup_manager
;
15 use proxmox_backup_manager
::*;
21 schema
: DATASTORE_SCHEMA
,
24 schema
: OUTPUT_FORMAT
,
30 /// Start garbage collection for a specific datastore.
31 async
fn start_garbage_collection(param
: Value
) -> Result
<Value
, Error
> {
33 let output_format
= get_output_format(¶m
);
35 let store
= tools
::required_string_param(¶m
, "store")?
;
37 let mut client
= connect_to_localhost()?
;
39 let path
= format
!("api2/json/admin/datastore/{}/gc", store
);
41 let result
= client
.post(&path
, None
).await?
;
43 view_task_result(&mut client
, result
, &output_format
).await?
;
52 schema
: DATASTORE_SCHEMA
,
55 schema
: OUTPUT_FORMAT
,
61 /// Show garbage collection status for a specific datastore.
62 async
fn garbage_collection_status(param
: Value
) -> Result
<Value
, Error
> {
64 let output_format
= get_output_format(¶m
);
66 let store
= tools
::required_string_param(¶m
, "store")?
;
68 let client
= connect_to_localhost()?
;
70 let path
= format
!("api2/json/admin/datastore/{}/gc", store
);
72 let mut result
= client
.get(&path
, None
).await?
;
73 let mut data
= result
["data"].take();
74 let return_type
= &api2
::admin
::datastore
::API_METHOD_GARBAGE_COLLECTION_STATUS
.returns
;
76 let options
= default_table_format_options();
78 format_and_print_result_full(&mut data
, return_type
, &output_format
, &options
);
83 fn garbage_collection_commands() -> CommandLineInterface
{
85 let cmd_def
= CliCommandMap
::new()
87 CliCommand
::new(&API_METHOD_GARBAGE_COLLECTION_STATUS
)
88 .arg_param(&["store"])
89 .completion_cb("store", config
::datastore
::complete_datastore_name
)
92 CliCommand
::new(&API_METHOD_START_GARBAGE_COLLECTION
)
93 .arg_param(&["store"])
94 .completion_cb("store", config
::datastore
::complete_datastore_name
)
104 description
: "The maximal number of tasks to list.",
112 schema
: OUTPUT_FORMAT
,
117 description
: "Also list stopped tasks.",
123 /// List running server tasks.
124 async
fn task_list(param
: Value
) -> Result
<Value
, Error
> {
126 let output_format
= get_output_format(¶m
);
128 let client
= connect_to_localhost()?
;
130 let limit
= param
["limit"].as_u64().unwrap_or(50) as usize;
131 let running
= !param
["all"].as_bool().unwrap_or(false);
137 let mut result
= client
.get("api2/json/nodes/localhost/tasks", Some(args
)).await?
;
139 let mut data
= result
["data"].take();
140 let return_type
= &api2
::node
::tasks
::API_METHOD_LIST_TASKS
.returns
;
142 use pbs_tools
::format
::{render_epoch, render_task_status}
;
143 let options
= default_table_format_options()
144 .column(ColumnConfig
::new("starttime").right_align(false).renderer(render_epoch
))
145 .column(ColumnConfig
::new("endtime").right_align(false).renderer(render_epoch
))
146 .column(ColumnConfig
::new("upid"))
147 .column(ColumnConfig
::new("status").renderer(render_task_status
));
149 format_and_print_result_full(&mut data
, return_type
, &output_format
, &options
);
163 /// Display the task log.
164 async
fn task_log(param
: Value
) -> Result
<Value
, Error
> {
166 let upid
= tools
::required_string_param(¶m
, "upid")?
;
168 let mut client
= connect_to_localhost()?
;
170 display_task_log(&mut client
, upid
, true).await?
;
184 /// Try to stop a specific task.
185 async
fn task_stop(param
: Value
) -> Result
<Value
, Error
> {
187 let upid_str
= tools
::required_string_param(¶m
, "upid")?
;
189 let mut client
= connect_to_localhost()?
;
191 let path
= format
!("api2/json/nodes/localhost/tasks/{}", tools
::percent_encode_component(upid_str
));
192 let _
= client
.delete(&path
, None
).await?
;
197 fn task_mgmt_cli() -> CommandLineInterface
{
199 let task_log_cmd_def
= CliCommand
::new(&API_METHOD_TASK_LOG
)
200 .arg_param(&["upid"]);
202 let task_stop_cmd_def
= CliCommand
::new(&API_METHOD_TASK_STOP
)
203 .arg_param(&["upid"]);
205 let cmd_def
= CliCommandMap
::new()
206 .insert("list", CliCommand
::new(&API_METHOD_TASK_LIST
))
207 .insert("log", task_log_cmd_def
)
208 .insert("stop", task_stop_cmd_def
);
213 // fixme: avoid API redefinition
218 schema
: DATASTORE_SCHEMA
,
221 schema
: REMOTE_ID_SCHEMA
,
224 schema
: DATASTORE_SCHEMA
,
227 schema
: REMOVE_VANISHED_BACKUPS_SCHEMA
,
231 schema
: OUTPUT_FORMAT
,
237 /// Sync datastore from another repository
238 async
fn pull_datastore(
240 remote_store
: String
,
242 remove_vanished
: Option
<bool
>,
244 ) -> Result
<Value
, Error
> {
246 let output_format
= get_output_format(¶m
);
248 let mut client
= connect_to_localhost()?
;
250 let mut args
= json
!({
251 "store": local_store
,
253 "remote-store": remote_store
,
256 if let Some(remove_vanished
) = remove_vanished
{
257 args
["remove-vanished"] = Value
::from(remove_vanished
);
260 let result
= client
.post("api2/json/pull", Some(args
)).await?
;
262 view_task_result(&mut client
, result
, &output_format
).await?
;
271 schema
: DATASTORE_SCHEMA
,
274 schema
: IGNORE_VERIFIED_BACKUPS_SCHEMA
,
278 schema
: VERIFICATION_OUTDATED_AFTER_SCHEMA
,
282 schema
: OUTPUT_FORMAT
,
292 ) -> Result
<Value
, Error
> {
294 let output_format
= extract_output_format(&mut param
);
296 let mut client
= connect_to_localhost()?
;
298 let args
= json
!(param
);
300 let path
= format
!("api2/json/admin/datastore/{}/verify", store
);
302 let result
= client
.post(&path
, Some(args
)).await?
;
304 view_task_result(&mut client
, result
, &output_format
).await?
;
311 async
fn report() -> Result
<Value
, Error
> {
312 let report
= proxmox_backup
::server
::generate_report();
313 io
::stdout().write_all(report
.as_bytes())?
;
324 description
: "Output verbose package information. It is ignored if output-format is specified.",
327 schema
: OUTPUT_FORMAT
,
333 /// List package versions for important Proxmox Backup Server packages.
334 async
fn get_versions(verbose
: bool
, param
: Value
) -> Result
<Value
, Error
> {
335 let output_format
= get_output_format(¶m
);
337 let packages
= crate::api2
::node
::apt
::get_versions()?
;
338 let mut packages
= json
!(if verbose { &packages[..] }
else { &packages[1..2] }
);
340 let options
= default_table_format_options()
342 .noborder(true) // just not helpful for version info which gets copy pasted often
343 .column(ColumnConfig
::new("Package"))
344 .column(ColumnConfig
::new("Version"))
345 .column(ColumnConfig
::new("ExtraInfo").header("Extra Info"))
347 let return_type
= &crate::api2
::node
::apt
::API_METHOD_GET_VERSIONS
.returns
;
349 format_and_print_result_full(&mut packages
, return_type
, &output_format
, &options
);
356 proxmox_backup
::tools
::setup_safe_path_env();
358 let cmd_def
= CliCommandMap
::new()
359 .insert("acl", acl_commands())
360 .insert("datastore", datastore_commands())
361 .insert("disk", disk_commands())
362 .insert("dns", dns_commands())
363 .insert("network", network_commands())
364 .insert("node", node_commands())
365 .insert("user", user_commands())
366 .insert("openid", openid_commands())
367 .insert("remote", remote_commands())
368 .insert("garbage-collection", garbage_collection_commands())
369 .insert("acme", acme_mgmt_cli())
370 .insert("cert", cert_mgmt_cli())
371 .insert("subscription", subscription_commands())
372 .insert("sync-job", sync_job_commands())
373 .insert("verify-job", verify_job_commands())
374 .insert("task", task_mgmt_cli())
377 CliCommand
::new(&API_METHOD_PULL_DATASTORE
)
378 .arg_param(&["remote", "remote-store", "local-store"])
379 .completion_cb("local-store", config
::datastore
::complete_datastore_name
)
380 .completion_cb("remote", config
::remote
::complete_remote_name
)
381 .completion_cb("remote-store", complete_remote_datastore_name
)
385 CliCommand
::new(&API_METHOD_VERIFY
)
386 .arg_param(&["store"])
387 .completion_cb("store", config
::datastore
::complete_datastore_name
)
390 CliCommand
::new(&API_METHOD_REPORT
)
393 CliCommand
::new(&API_METHOD_GET_VERSIONS
)
398 let mut rpcenv
= CliEnvironment
::new();
399 rpcenv
.set_auth_id(Some(String
::from("root@pam")));
401 pbs_runtime
::main(run_async_cli_command(cmd_def
, rpcenv
));
404 // shell completion helper
405 pub fn complete_remote_datastore_name(_arg
: &str, param
: &HashMap
<String
, String
>) -> Vec
<String
> {
407 let mut list
= Vec
::new();
409 let _
= proxmox
::try_block
!({
410 let remote
= param
.get("remote").ok_or_else(|| format_err
!("no remote"))?
;
412 let data
= pbs_runtime
::block_on(async
move {
413 crate::api2
::config
::remote
::scan_remote_datastores(remote
.clone()).await
417 list
.push(item
.store
);
421 }).map_err(|_err
: Error
| { /* ignore */ }
);