2019-12-19 07:45:27 +00:00
|
|
|
use failure::*;
|
|
|
|
use serde_json::Value;
|
2018-12-09 10:59:32 +00:00
|
|
|
|
2019-12-19 07:45:27 +00:00
|
|
|
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(())
|
|
|
|
}
|
2018-12-10 12:28:38 +00:00
|
|
|
|
2018-12-10 12:40:10 +00:00
|
|
|
fn datastore_commands() -> CommandLineInterface {
|
2018-12-09 10:59:32 +00:00
|
|
|
|
2019-01-22 11:10:38 +00:00
|
|
|
use proxmox_backup::api2;
|
2018-12-11 10:12:13 +00:00
|
|
|
|
2018-12-11 10:31:36 +00:00
|
|
|
let cmd_def = CliCommandMap::new()
|
2019-12-09 16:40:34 +00:00
|
|
|
.insert("list", CliCommand::new(&api2::config::datastore::GET))
|
2018-12-11 10:31:36 +00:00
|
|
|
.insert("create",
|
2019-11-21 08:36:41 +00:00
|
|
|
CliCommand::new(&api2::config::datastore::POST)
|
2019-11-24 10:00:53 +00:00
|
|
|
.arg_param(&["name", "path"])
|
2019-12-09 16:40:34 +00:00
|
|
|
)
|
2018-12-11 10:31:36 +00:00
|
|
|
.insert("remove",
|
2019-11-21 08:36:41 +00:00
|
|
|
CliCommand::new(&api2::config::datastore::DELETE)
|
2019-11-24 10:00:53 +00:00
|
|
|
.arg_param(&["name"])
|
2019-01-04 09:49:52 +00:00
|
|
|
.completion_cb("name", config::datastore::complete_datastore_name)
|
2019-12-09 16:40:34 +00:00
|
|
|
);
|
2018-12-10 12:28:38 +00:00
|
|
|
|
2018-12-10 12:51:10 +00:00
|
|
|
cmd_def.into()
|
2018-12-10 12:28:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 10:33:58 +00:00
|
|
|
|
2019-12-19 07:45:27 +00:00
|
|
|
#[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> {
|
2019-01-04 10:33:58 +00:00
|
|
|
|
2019-12-19 07:45:27 +00:00
|
|
|
let output_format = param["output-format"].as_str().unwrap_or("text").to_owned();
|
2019-01-04 10:33:58 +00:00
|
|
|
|
2019-12-19 07:45:27 +00:00
|
|
|
let store = tools::required_string_param(¶m, "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(¶m, "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 {
|
2019-01-04 10:33:58 +00:00
|
|
|
|
|
|
|
let cmd_def = CliCommandMap::new()
|
|
|
|
.insert("status",
|
2019-12-19 07:45:27 +00:00
|
|
|
CliCommand::new(&API_METHOD_GARBAGE_COLLECTION_STATUS)
|
2019-11-24 10:00:53 +00:00
|
|
|
.arg_param(&["store"])
|
2019-02-12 09:08:23 +00:00
|
|
|
.completion_cb("store", config::datastore::complete_datastore_name)
|
2019-12-09 16:40:34 +00:00
|
|
|
)
|
2019-01-04 10:33:58 +00:00
|
|
|
.insert("start",
|
2019-12-19 07:45:27 +00:00
|
|
|
CliCommand::new(&API_METHOD_START_GARBAGE_COLLECTION)
|
2019-11-24 10:00:53 +00:00
|
|
|
.arg_param(&["store"])
|
2019-02-12 09:08:23 +00:00
|
|
|
.completion_cb("store", config::datastore::complete_datastore_name)
|
2019-12-09 16:40:34 +00:00
|
|
|
);
|
2019-01-04 10:33:58 +00:00
|
|
|
|
|
|
|
cmd_def.into()
|
|
|
|
}
|
|
|
|
|
2018-12-10 12:28:38 +00:00
|
|
|
fn main() {
|
|
|
|
|
2018-12-11 10:31:36 +00:00
|
|
|
let cmd_def = CliCommandMap::new()
|
2019-12-09 16:40:34 +00:00
|
|
|
.insert("datastore", datastore_commands())
|
|
|
|
.insert("garbage-collection", garbage_collection_commands());
|
2018-12-09 15:52:32 +00:00
|
|
|
|
2019-12-09 16:40:34 +00:00
|
|
|
run_cli_command(cmd_def);
|
2018-12-09 10:59:32 +00:00
|
|
|
}
|