src/bin/proxmox-backup-manager.rs: connect to daemon to execute GC

Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
This commit is contained in:
Dietmar Maurer 2019-12-19 08:45:27 +01:00
parent 25de1c8005
commit 769f8c9998

View File

@ -1,10 +1,35 @@
extern crate proxmox_backup;
use failure::*;
use serde_json::Value;
use proxmox::api::cli::*;
use proxmox::api::{api, cli::*};
use proxmox_backup::tools;
use proxmox_backup::config;
use proxmox_backup::api2::types::*;
use proxmox_backup::client::*;
use proxmox_backup::tools::ticket::*;
use proxmox_backup::auth_helpers::*;
async fn view_task_result(
client: HttpClient,
result: Value,
output_format: &str,
) -> Result<(), Error> {
let data = &result["data"];
if output_format == "text" {
if let Some(upid) = data.as_str() {
display_task_log(client, upid, true).await?;
}
} else {
format_and_print_result(&data, &output_format);
}
Ok(())
}
fn datastore_commands() -> CommandLineInterface {
use proxmox_backup::config;
use proxmox_backup::api2;
let cmd_def = CliCommandMap::new()
@ -23,20 +48,96 @@ fn datastore_commands() -> CommandLineInterface {
}
#[api(
input: {
properties: {
store: {
schema: DATASTORE_SCHEMA,
},
"output-format": {
schema: OUTPUT_FORMAT,
optional: true,
},
}
}
)]
/// Start garbage collection for a specific datastore.
async fn start_garbage_collection(param: Value) -> Result<Value, Error> {
let output_format = param["output-format"].as_str().unwrap_or("text").to_owned();
let store = tools::required_string_param(&param, "store")?;
let uid = nix::unistd::Uid::current();
let mut client = if uid.is_root() {
let ticket = assemble_rsa_ticket(private_auth_key(), "PBS", Some("root@pam"), None)?;
HttpClient::new("localhost", "root@pam", Some(ticket))?
} else {
HttpClient::new("localhost", "root@pam", None)?
};
let path = format!("api2/json/admin/datastore/{}/gc", store);
let result = client.post(&path, None).await?;
view_task_result(client, result, &output_format).await?;
Ok(Value::Null)
}
#[api(
input: {
properties: {
store: {
schema: DATASTORE_SCHEMA,
},
"output-format": {
schema: OUTPUT_FORMAT,
optional: true,
},
}
}
)]
/// Show garbage collection status for a specific datastore.
async fn garbage_collection_status(param: Value) -> Result<Value, Error> {
let output_format = param["output-format"].as_str().unwrap_or("text").to_owned();
let store = tools::required_string_param(&param, "store")?;
let uid = nix::unistd::Uid::current();
let client = if uid.is_root() {
let ticket = assemble_rsa_ticket(private_auth_key(), "PBS", Some("root@pam"), None)?;
HttpClient::new("localhost", "root@pam", Some(ticket))?
} else {
HttpClient::new("localhost", "root@pam", None)?
};
let path = format!("api2/json/admin/datastore/{}/gc", store);
let result = client.get(&path, None).await?;
let data = &result["data"];
if output_format == "text" {
format_and_print_result(&data, "json-pretty");
} else {
format_and_print_result(&data, &output_format);
}
Ok(Value::Null)
}
fn garbage_collection_commands() -> CommandLineInterface {
use proxmox_backup::config;
use proxmox_backup::api2;
let cmd_def = CliCommandMap::new()
.insert("status",
CliCommand::new(&api2::admin::datastore::API_METHOD_GARBAGE_COLLECTION_STATUS)
CliCommand::new(&API_METHOD_GARBAGE_COLLECTION_STATUS)
.arg_param(&["store"])
.completion_cb("store", config::datastore::complete_datastore_name)
)
.insert("start",
CliCommand::new(&api2::admin::datastore::API_METHOD_START_GARBAGE_COLLECTION)
CliCommand::new(&API_METHOD_START_GARBAGE_COLLECTION)
.arg_param(&["store"])
.completion_cb("store", config::datastore::complete_datastore_name)
);