2 use serde_json
::{json, Value}
;
4 use proxmox
::api
::{api, cli::*}
;
6 use proxmox_backup
::tools
;
7 use proxmox_backup
::config
;
8 use proxmox_backup
::api2
::types
::*;
9 use proxmox_backup
::client
::*;
10 use proxmox_backup
::tools
::ticket
::*;
11 use proxmox_backup
::auth_helpers
::*;
14 async
fn view_task_result(
18 ) -> Result
<(), Error
> {
19 let data
= &result
["data"];
20 if output_format
== "text" {
21 if let Some(upid
) = data
.as_str() {
22 display_task_log(client
, upid
, true).await?
;
25 format_and_print_result(&data
, &output_format
);
31 fn connect() -> Result
<HttpClient
, Error
> {
33 let uid
= nix
::unistd
::Uid
::current();
35 let client
= if uid
.is_root() {
36 let ticket
= assemble_rsa_ticket(private_auth_key(), "PBS", Some("root@pam"), None
)?
;
37 HttpClient
::new("localhost", "root@pam", Some(ticket
))?
39 HttpClient
::new("localhost", "root@pam", None
)?
45 fn datastore_commands() -> CommandLineInterface
{
47 use proxmox_backup
::api2
;
49 let cmd_def
= CliCommandMap
::new()
50 .insert("list", CliCommand
::new(&api2
::config
::datastore
::GET
))
52 CliCommand
::new(&api2
::config
::datastore
::POST
)
53 .arg_param(&["name", "path"])
56 CliCommand
::new(&api2
::config
::datastore
::DELETE
)
58 .completion_cb("name", config
::datastore
::complete_datastore_name
)
69 schema
: DATASTORE_SCHEMA
,
72 schema
: OUTPUT_FORMAT
,
78 /// Start garbage collection for a specific datastore.
79 async
fn start_garbage_collection(param
: Value
) -> Result
<Value
, Error
> {
81 let output_format
= param
["output-format"].as_str().unwrap_or("text").to_owned();
83 let store
= tools
::required_string_param(¶m
, "store")?
;
85 let mut client
= connect()?
;
87 let path
= format
!("api2/json/admin/datastore/{}/gc", store
);
89 let result
= client
.post(&path
, None
).await?
;
91 view_task_result(client
, result
, &output_format
).await?
;
100 schema
: DATASTORE_SCHEMA
,
103 schema
: OUTPUT_FORMAT
,
109 /// Show garbage collection status for a specific datastore.
110 async
fn garbage_collection_status(param
: Value
) -> Result
<Value
, Error
> {
112 let output_format
= param
["output-format"].as_str().unwrap_or("text").to_owned();
114 let store
= tools
::required_string_param(¶m
, "store")?
;
116 let client
= connect()?
;
118 let path
= format
!("api2/json/admin/datastore/{}/gc", store
);
120 let result
= client
.get(&path
, None
).await?
;
121 let data
= &result
["data"];
122 if output_format
== "text" {
123 format_and_print_result(&data
, "json-pretty");
125 format_and_print_result(&data
, &output_format
);
131 fn garbage_collection_commands() -> CommandLineInterface
{
133 let cmd_def
= CliCommandMap
::new()
135 CliCommand
::new(&API_METHOD_GARBAGE_COLLECTION_STATUS
)
136 .arg_param(&["store"])
137 .completion_cb("store", config
::datastore
::complete_datastore_name
)
140 CliCommand
::new(&API_METHOD_START_GARBAGE_COLLECTION
)
141 .arg_param(&["store"])
142 .completion_cb("store", config
::datastore
::complete_datastore_name
)
152 description
: "The maximal number of tasks to list.",
160 schema
: OUTPUT_FORMAT
,
165 description
: "Also list stopped tasks.",
171 /// List running server tasks.
172 async
fn task_list(param
: Value
) -> Result
<Value
, Error
> {
174 let output_format
= param
["output-format"].as_str().unwrap_or("text").to_owned();
176 let client
= connect()?
;
178 let limit
= param
["limit"].as_u64().unwrap_or(50) as usize;
179 let running
= !param
["all"].as_bool().unwrap_or(false);
185 let result
= client
.get("api2/json/nodes/localhost/tasks", Some(args
)).await?
;
187 let data
= &result
["data"];
189 if output_format
== "text" {
190 for item
in data
.as_array().unwrap() {
193 item
["upid"].as_str().unwrap(),
194 item
["status"].as_str().unwrap_or("running"),
198 format_and_print_result(data
, &output_format
);
213 /// Display the task log.
214 async
fn task_log(param
: Value
) -> Result
<Value
, Error
> {
216 let upid
= tools
::required_string_param(¶m
, "upid")?
;
218 let client
= connect()?
;
220 display_task_log(client
, upid
, true).await?
;
234 /// Try to stop a specific task.
235 async
fn task_stop(param
: Value
) -> Result
<Value
, Error
> {
237 let upid_str
= tools
::required_string_param(¶m
, "upid")?
;
239 let mut client
= connect()?
;
241 let path
= format
!("api2/json/nodes/localhost/tasks/{}", upid_str
);
242 let _
= client
.delete(&path
, None
).await?
;
247 fn task_mgmt_cli() -> CommandLineInterface
{
249 let task_log_cmd_def
= CliCommand
::new(&API_METHOD_TASK_LOG
)
250 .arg_param(&["upid"]);
252 let task_stop_cmd_def
= CliCommand
::new(&API_METHOD_TASK_STOP
)
253 .arg_param(&["upid"]);
255 let cmd_def
= CliCommandMap
::new()
256 .insert("list", CliCommand
::new(&API_METHOD_TASK_LIST
))
257 .insert("log", task_log_cmd_def
)
258 .insert("stop", task_stop_cmd_def
);
265 let cmd_def
= CliCommandMap
::new()
266 .insert("datastore", datastore_commands())
267 .insert("garbage-collection", garbage_collection_commands())
268 .insert("task", task_mgmt_cli());
270 run_cli_command(cmd_def
);