2019-12-22 16:35:06 +00:00
|
|
|
use std::path::PathBuf;
|
2020-01-10 10:58:31 +00:00
|
|
|
use std::collections::HashMap;
|
2018-12-09 10:59:32 +00:00
|
|
|
|
2020-01-30 12:29:39 +00:00
|
|
|
use failure::*;
|
|
|
|
use serde_json::{json, Value};
|
|
|
|
use chrono::{Local, TimeZone};
|
|
|
|
|
2019-12-19 07:45:27 +00:00
|
|
|
use proxmox::api::{api, cli::*};
|
|
|
|
|
2019-12-22 16:35:06 +00:00
|
|
|
use proxmox_backup::configdir;
|
2019-12-19 07:45:27 +00:00
|
|
|
use proxmox_backup::tools;
|
2020-01-16 13:32:06 +00:00
|
|
|
use proxmox_backup::config::{self, remote::{self, Remote}};
|
2020-01-30 12:29:39 +00:00
|
|
|
use proxmox_backup::api2::{self, types::* };
|
2019-12-19 07:45:27 +00:00
|
|
|
use proxmox_backup::client::*;
|
|
|
|
use proxmox_backup::tools::ticket::*;
|
|
|
|
use proxmox_backup::auth_helpers::*;
|
|
|
|
|
2020-01-30 12:29:39 +00:00
|
|
|
fn render_epoch(value: &Value, _record: &Value) -> Result<String, Error> {
|
|
|
|
if value.is_null() { return Ok(String::new()); }
|
|
|
|
let text = match value.as_i64() {
|
|
|
|
Some(epoch) => {
|
|
|
|
Local.timestamp(epoch, 0).format("%c").to_string()
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
value.to_string()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(text)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_status(value: &Value, record: &Value) -> Result<String, Error> {
|
|
|
|
if record["endtime"].is_null() {
|
|
|
|
Ok(value.as_str().unwrap_or("running").to_string())
|
|
|
|
} else {
|
|
|
|
Ok(value.as_str().unwrap_or("unknown").to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-19 07:45:27 +00:00
|
|
|
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
|
|
|
|
2019-12-19 10:27:36 +00:00
|
|
|
fn connect() -> Result<HttpClient, Error> {
|
|
|
|
|
|
|
|
let uid = nix::unistd::Uid::current();
|
|
|
|
|
2020-01-25 11:18:00 +00:00
|
|
|
let mut options = HttpClientOptions::new()
|
2020-01-27 08:34:02 +00:00
|
|
|
.prefix(Some("proxmox-backup".to_string()))
|
2020-01-25 11:18:00 +00:00
|
|
|
.verify_cert(false); // not required for connection to localhost
|
|
|
|
|
2019-12-19 10:27:36 +00:00
|
|
|
let client = if uid.is_root() {
|
|
|
|
let ticket = assemble_rsa_ticket(private_auth_key(), "PBS", Some("root@pam"), None)?;
|
2020-01-25 11:18:00 +00:00
|
|
|
options = options.password(Some(ticket));
|
|
|
|
HttpClient::new("localhost", "root@pam", options)?
|
2019-12-19 10:27:36 +00:00
|
|
|
} else {
|
2020-01-25 11:18:00 +00:00
|
|
|
options = options.ticket_cache(true).interactive(true);
|
|
|
|
HttpClient::new("localhost", "root@pam", options)?
|
2019-12-19 10:27:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(client)
|
|
|
|
}
|
|
|
|
|
2020-01-30 12:29:39 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
"output-format": {
|
|
|
|
schema: OUTPUT_FORMAT,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)]
|
|
|
|
/// List configured remotes.
|
|
|
|
async fn list_remotes(param: Value) -> Result<Value, Error> {
|
2020-01-11 09:42:09 +00:00
|
|
|
|
2020-01-30 12:29:39 +00:00
|
|
|
let output_format = param["output-format"].as_str().unwrap_or("text").to_owned();
|
|
|
|
|
|
|
|
let client = connect()?;
|
|
|
|
|
|
|
|
let mut result = client.get("api2/json/config/remote", None).await?;
|
|
|
|
|
|
|
|
let mut data = result["data"].take();
|
|
|
|
let schema = api2::config::remote::API_RETURN_SCHEMA_LIST_REMOTES;
|
|
|
|
|
|
|
|
let mut column_config = Vec::new();
|
|
|
|
column_config.push(ColumnConfig::new("name"));
|
|
|
|
column_config.push(ColumnConfig::new("host"));
|
|
|
|
column_config.push(ColumnConfig::new("userid"));
|
|
|
|
column_config.push(ColumnConfig::new("fingerprint"));
|
|
|
|
column_config.push(ColumnConfig::new("comment"));
|
|
|
|
|
|
|
|
let options = TableFormatOptions::new()
|
|
|
|
.noborder(false)
|
|
|
|
.noheader(false)
|
|
|
|
.column_config(column_config);
|
|
|
|
|
|
|
|
|
|
|
|
format_and_print_result_full(&mut data, schema, &output_format, &options);
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn remote_commands() -> CommandLineInterface {
|
2020-01-11 09:42:09 +00:00
|
|
|
|
|
|
|
let cmd_def = CliCommandMap::new()
|
2020-01-30 12:29:39 +00:00
|
|
|
//.insert("list", CliCommand::new(&api2::config::remote::API_METHOD_LIST_REMOTES))
|
|
|
|
.insert("list", CliCommand::new(&&API_METHOD_LIST_REMOTES))
|
2020-01-11 09:42:09 +00:00
|
|
|
.insert(
|
|
|
|
"create",
|
|
|
|
// fixme: howto handle password parameter?
|
2020-01-16 13:32:06 +00:00
|
|
|
CliCommand::new(&api2::config::remote::API_METHOD_CREATE_REMOTE)
|
2020-01-11 09:42:09 +00:00
|
|
|
.arg_param(&["name"])
|
|
|
|
)
|
2020-01-14 13:20:16 +00:00
|
|
|
.insert(
|
|
|
|
"update",
|
2020-01-16 13:32:06 +00:00
|
|
|
CliCommand::new(&api2::config::remote::API_METHOD_UPDATE_REMOTE)
|
2020-01-14 13:20:16 +00:00
|
|
|
.arg_param(&["name"])
|
2020-01-16 13:32:06 +00:00
|
|
|
.completion_cb("name", config::remote::complete_remote_name)
|
2020-01-14 13:20:16 +00:00
|
|
|
)
|
2020-01-11 09:42:09 +00:00
|
|
|
.insert(
|
|
|
|
"remove",
|
2020-01-16 13:32:06 +00:00
|
|
|
CliCommand::new(&api2::config::remote::API_METHOD_DELETE_REMOTE)
|
2020-01-11 09:42:09 +00:00
|
|
|
.arg_param(&["name"])
|
2020-01-16 13:32:06 +00:00
|
|
|
.completion_cb("name", config::remote::complete_remote_name)
|
2020-01-11 09:42:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
cmd_def.into()
|
|
|
|
}
|
|
|
|
|
2018-12-10 12:40:10 +00:00
|
|
|
fn datastore_commands() -> CommandLineInterface {
|
2018-12-09 10:59:32 +00:00
|
|
|
|
2018-12-11 10:31:36 +00:00
|
|
|
let cmd_def = CliCommandMap::new()
|
2020-01-11 09:42:09 +00:00
|
|
|
.insert("list", CliCommand::new(&api2::config::datastore::API_METHOD_LIST_DATASTORES))
|
2018-12-11 10:31:36 +00:00
|
|
|
.insert("create",
|
2020-01-11 09:42:09 +00:00
|
|
|
CliCommand::new(&api2::config::datastore::API_METHOD_CREATE_DATASTORE)
|
2019-11-24 10:00:53 +00:00
|
|
|
.arg_param(&["name", "path"])
|
2019-12-09 16:40:34 +00:00
|
|
|
)
|
2020-01-14 13:47:26 +00:00
|
|
|
.insert("update",
|
|
|
|
CliCommand::new(&api2::config::datastore::API_METHOD_UPDATE_DATASTORE)
|
|
|
|
.arg_param(&["name"])
|
2020-01-15 11:42:13 +00:00
|
|
|
.completion_cb("name", config::datastore::complete_datastore_name)
|
2020-01-14 13:47:26 +00:00
|
|
|
)
|
2018-12-11 10:31:36 +00:00
|
|
|
.insert("remove",
|
2020-01-11 09:42:09 +00:00
|
|
|
CliCommand::new(&api2::config::datastore::API_METHOD_DELETE_DATASTORE)
|
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")?;
|
|
|
|
|
2019-12-19 10:27:36 +00:00
|
|
|
let mut client = connect()?;
|
2019-12-19 07:45:27 +00:00
|
|
|
|
|
|
|
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")?;
|
|
|
|
|
2019-12-19 10:27:36 +00:00
|
|
|
let client = connect()?;
|
2019-12-19 07:45:27 +00:00
|
|
|
|
|
|
|
let path = format!("api2/json/admin/datastore/{}/gc", store);
|
|
|
|
|
2020-01-30 12:29:39 +00:00
|
|
|
let mut result = client.get(&path, None).await?;
|
|
|
|
let mut data = result["data"].take();
|
|
|
|
let schema = api2::admin::datastore::API_RETURN_SCHEMA_GARBAGE_COLLECTION_STATUS;
|
|
|
|
|
|
|
|
let options = TableFormatOptions::new()
|
|
|
|
.noborder(false)
|
|
|
|
.noheader(false);
|
|
|
|
|
|
|
|
format_and_print_result_full(&mut data, schema, &output_format, &options);
|
2019-12-19 07:45:27 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2019-12-19 10:27:36 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
limit: {
|
|
|
|
description: "The maximal number of tasks to list.",
|
|
|
|
type: Integer,
|
|
|
|
optional: true,
|
|
|
|
minimum: 1,
|
|
|
|
maximum: 1000,
|
|
|
|
default: 50,
|
|
|
|
},
|
|
|
|
"output-format": {
|
|
|
|
schema: OUTPUT_FORMAT,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
all: {
|
|
|
|
type: Boolean,
|
|
|
|
description: "Also list stopped tasks.",
|
|
|
|
optional: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)]
|
|
|
|
/// List running server tasks.
|
|
|
|
async fn task_list(param: Value) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let output_format = param["output-format"].as_str().unwrap_or("text").to_owned();
|
|
|
|
|
|
|
|
let client = connect()?;
|
|
|
|
|
|
|
|
let limit = param["limit"].as_u64().unwrap_or(50) as usize;
|
|
|
|
let running = !param["all"].as_bool().unwrap_or(false);
|
|
|
|
let args = json!({
|
|
|
|
"running": running,
|
|
|
|
"start": 0,
|
|
|
|
"limit": limit,
|
|
|
|
});
|
2020-01-30 12:29:39 +00:00
|
|
|
let mut result = client.get("api2/json/nodes/localhost/tasks", Some(args)).await?;
|
2019-12-19 10:27:36 +00:00
|
|
|
|
2020-01-30 12:29:39 +00:00
|
|
|
let mut data = result["data"].take();
|
|
|
|
let schema = api2::node::tasks::API_RETURN_SCHEMA_LIST_TASKS;
|
2019-12-19 10:27:36 +00:00
|
|
|
|
2020-01-30 12:29:39 +00:00
|
|
|
let mut column_config = Vec::new();
|
|
|
|
column_config.push(ColumnConfig::new("starttime").right_align(false).renderer(render_epoch));
|
|
|
|
column_config.push(ColumnConfig::new("endtime").right_align(false).renderer(render_epoch));
|
|
|
|
column_config.push(ColumnConfig::new("upid"));
|
|
|
|
column_config.push(ColumnConfig::new("status").renderer(render_status));
|
|
|
|
|
|
|
|
let options = TableFormatOptions::new()
|
|
|
|
.noborder(false)
|
|
|
|
.noheader(false)
|
|
|
|
.column_config(column_config);
|
|
|
|
|
|
|
|
format_and_print_result_full(&mut data, schema, &output_format, &options);
|
2019-12-19 10:27:36 +00:00
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
upid: {
|
|
|
|
schema: UPID_SCHEMA,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)]
|
|
|
|
/// Display the task log.
|
|
|
|
async fn task_log(param: Value) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let upid = tools::required_string_param(¶m, "upid")?;
|
|
|
|
|
|
|
|
let client = connect()?;
|
|
|
|
|
|
|
|
display_task_log(client, upid, true).await?;
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
upid: {
|
|
|
|
schema: UPID_SCHEMA,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)]
|
|
|
|
/// Try to stop a specific task.
|
|
|
|
async fn task_stop(param: Value) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let upid_str = tools::required_string_param(¶m, "upid")?;
|
|
|
|
|
|
|
|
let mut client = connect()?;
|
|
|
|
|
|
|
|
let path = format!("api2/json/nodes/localhost/tasks/{}", upid_str);
|
|
|
|
let _ = client.delete(&path, None).await?;
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn task_mgmt_cli() -> CommandLineInterface {
|
|
|
|
|
|
|
|
let task_log_cmd_def = CliCommand::new(&API_METHOD_TASK_LOG)
|
|
|
|
.arg_param(&["upid"]);
|
|
|
|
|
|
|
|
let task_stop_cmd_def = CliCommand::new(&API_METHOD_TASK_STOP)
|
|
|
|
.arg_param(&["upid"]);
|
|
|
|
|
|
|
|
let cmd_def = CliCommandMap::new()
|
|
|
|
.insert("list", CliCommand::new(&API_METHOD_TASK_LIST))
|
|
|
|
.insert("log", task_log_cmd_def)
|
|
|
|
.insert("stop", task_stop_cmd_def);
|
|
|
|
|
|
|
|
cmd_def.into()
|
|
|
|
}
|
|
|
|
|
2019-12-23 12:06:43 +00:00
|
|
|
fn x509name_to_string(name: &openssl::x509::X509NameRef) -> Result<String, Error> {
|
|
|
|
let mut parts = Vec::new();
|
|
|
|
for entry in name.entries() {
|
|
|
|
parts.push(format!("{} = {}", entry.object().nid().short_name()?, entry.data().as_utf8()?));
|
|
|
|
}
|
|
|
|
Ok(parts.join(", "))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[api]
|
|
|
|
/// Diplay node certificate information.
|
|
|
|
fn cert_info() -> Result<(), Error> {
|
|
|
|
|
|
|
|
let cert_path = PathBuf::from(configdir!("/proxy.pem"));
|
|
|
|
|
|
|
|
let cert_pem = proxmox::tools::fs::file_get_contents(&cert_path)?;
|
|
|
|
|
|
|
|
let cert = openssl::x509::X509::from_pem(&cert_pem)?;
|
|
|
|
|
|
|
|
println!("Subject: {}", x509name_to_string(cert.subject_name())?);
|
|
|
|
|
|
|
|
if let Some(san) = cert.subject_alt_names() {
|
|
|
|
for name in san.iter() {
|
|
|
|
if let Some(v) = name.dnsname() {
|
|
|
|
println!(" DNS:{}", v);
|
|
|
|
} else if let Some(v) = name.ipaddress() {
|
|
|
|
println!(" IP:{:?}", v);
|
|
|
|
} else if let Some(v) = name.email() {
|
|
|
|
println!(" EMAIL:{}", v);
|
|
|
|
} else if let Some(v) = name.uri() {
|
|
|
|
println!(" URI:{}", v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("Issuer: {}", x509name_to_string(cert.issuer_name())?);
|
|
|
|
println!("Validity:");
|
|
|
|
println!(" Not Before: {}", cert.not_before());
|
|
|
|
println!(" Not After : {}", cert.not_after());
|
|
|
|
|
|
|
|
let fp = cert.digest(openssl::hash::MessageDigest::sha256())?;
|
|
|
|
let fp_string = proxmox::tools::digest_to_hex(&fp);
|
|
|
|
let fp_string = fp_string.as_bytes().chunks(2).map(|v| std::str::from_utf8(v).unwrap())
|
|
|
|
.collect::<Vec<&str>>().join(":");
|
|
|
|
|
|
|
|
println!("Fingerprint (sha256): {}", fp_string);
|
|
|
|
|
|
|
|
let pubkey = cert.public_key()?;
|
|
|
|
println!("Public key type: {}", openssl::nid::Nid::from_raw(pubkey.id().as_raw()).long_name()?);
|
|
|
|
println!("Public key bits: {}", pubkey.bits());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-12-22 16:35:06 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
force: {
|
|
|
|
description: "Force generation of new SSL certifate.",
|
|
|
|
type: Boolean,
|
|
|
|
optional:true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Update node certificates and generate all needed files/directories.
|
|
|
|
fn update_certs(force: Option<bool>) -> Result<(), Error> {
|
|
|
|
|
|
|
|
config::create_configdir()?;
|
|
|
|
|
|
|
|
if let Err(err) = generate_auth_key() {
|
|
|
|
bail!("unable to generate auth key - {}", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Err(err) = generate_csrf_key() {
|
|
|
|
bail!("unable to generate csrf key - {}", err);
|
|
|
|
}
|
|
|
|
|
2019-12-27 10:20:36 +00:00
|
|
|
config::update_self_signed_cert(force.unwrap_or(false))?;
|
2019-12-22 16:35:06 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cert_mgmt_cli() -> CommandLineInterface {
|
|
|
|
|
|
|
|
let cmd_def = CliCommandMap::new()
|
2019-12-23 12:06:43 +00:00
|
|
|
.insert("info", CliCommand::new(&API_METHOD_CERT_INFO))
|
|
|
|
.insert("update", CliCommand::new(&API_METHOD_UPDATE_CERTS));
|
2019-12-22 16:35:06 +00:00
|
|
|
|
|
|
|
cmd_def.into()
|
|
|
|
}
|
|
|
|
|
2020-01-17 10:24:55 +00:00
|
|
|
// fixme: avoid API redefinition
|
2020-01-09 13:52:29 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
2020-01-10 10:09:55 +00:00
|
|
|
"local-store": {
|
2020-01-09 13:52:29 +00:00
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
|
|
|
remote: {
|
2020-01-13 10:47:07 +00:00
|
|
|
schema: REMOTE_ID_SCHEMA,
|
2020-01-09 13:52:29 +00:00
|
|
|
},
|
|
|
|
"remote-store": {
|
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
2020-01-17 10:24:55 +00:00
|
|
|
delete: {
|
|
|
|
description: "Delete vanished backups. This remove the local copy if the remote backup was deleted.",
|
|
|
|
type: Boolean,
|
|
|
|
optional: true,
|
|
|
|
default: true,
|
|
|
|
},
|
2020-01-09 13:52:29 +00:00
|
|
|
"output-format": {
|
|
|
|
schema: OUTPUT_FORMAT,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)]
|
2020-01-10 10:09:55 +00:00
|
|
|
/// Sync datastore from another repository
|
|
|
|
async fn pull_datastore(
|
2020-01-09 13:52:29 +00:00
|
|
|
remote: String,
|
|
|
|
remote_store: String,
|
2020-01-10 10:09:55 +00:00
|
|
|
local_store: String,
|
2020-01-17 10:24:55 +00:00
|
|
|
delete: Option<bool>,
|
2020-01-09 13:52:29 +00:00
|
|
|
output_format: Option<String>,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let output_format = output_format.unwrap_or("text".to_string());
|
|
|
|
|
|
|
|
let mut client = connect()?;
|
|
|
|
|
2020-01-17 10:24:55 +00:00
|
|
|
let mut args = json!({
|
2020-01-10 10:09:55 +00:00
|
|
|
"store": local_store,
|
2020-01-16 12:54:29 +00:00
|
|
|
"remote": remote,
|
2020-01-09 13:52:29 +00:00
|
|
|
"remote-store": remote_store,
|
|
|
|
});
|
|
|
|
|
2020-01-17 10:24:55 +00:00
|
|
|
if let Some(delete) = delete {
|
|
|
|
args["delete"] = delete.into();
|
|
|
|
}
|
|
|
|
|
2020-01-10 10:09:55 +00:00
|
|
|
let result = client.post("api2/json/pull", Some(args)).await?;
|
2020-01-09 13:52:29 +00:00
|
|
|
|
|
|
|
view_task_result(client, result, &output_format).await?;
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
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())
|
2020-01-16 13:32:06 +00:00
|
|
|
.insert("remote", remote_commands())
|
2019-12-19 10:27:36 +00:00
|
|
|
.insert("garbage-collection", garbage_collection_commands())
|
2019-12-22 16:35:06 +00:00
|
|
|
.insert("cert", cert_mgmt_cli())
|
2020-01-09 13:52:29 +00:00
|
|
|
.insert("task", task_mgmt_cli())
|
|
|
|
.insert(
|
2020-01-10 10:09:55 +00:00
|
|
|
"pull",
|
|
|
|
CliCommand::new(&API_METHOD_PULL_DATASTORE)
|
|
|
|
.arg_param(&["remote", "remote-store", "local-store"])
|
|
|
|
.completion_cb("local-store", config::datastore::complete_datastore_name)
|
2020-01-16 13:32:06 +00:00
|
|
|
.completion_cb("remote", config::remote::complete_remote_name)
|
2020-01-10 10:58:31 +00:00
|
|
|
.completion_cb("remote-store", complete_remote_datastore_name)
|
2020-01-09 13:52:29 +00:00
|
|
|
);
|
2018-12-09 15:52:32 +00:00
|
|
|
|
2020-01-27 17:08:43 +00:00
|
|
|
proxmox_backup::tools::runtime::main(run_async_cli_command(cmd_def));
|
2018-12-09 10:59:32 +00:00
|
|
|
}
|
2020-01-10 10:58:31 +00:00
|
|
|
|
|
|
|
// shell completion helper
|
|
|
|
pub fn complete_remote_datastore_name(_arg: &str, param: &HashMap<String, String>) -> Vec<String> {
|
|
|
|
|
|
|
|
let mut list = Vec::new();
|
|
|
|
|
2020-01-21 11:28:01 +00:00
|
|
|
let _ = proxmox::try_block!({
|
2020-01-10 10:58:31 +00:00
|
|
|
let remote = param.get("remote").ok_or_else(|| format_err!("no remote"))?;
|
2020-01-16 13:32:06 +00:00
|
|
|
let (remote_config, _digest) = remote::config()?;
|
2020-01-10 10:58:31 +00:00
|
|
|
|
|
|
|
let remote: Remote = remote_config.lookup("remote", &remote)?;
|
|
|
|
|
2020-01-25 11:18:00 +00:00
|
|
|
let options = HttpClientOptions::new()
|
|
|
|
.password(Some(remote.password.clone()))
|
|
|
|
.fingerprint(remote.fingerprint.clone());
|
|
|
|
|
2020-01-10 10:58:31 +00:00
|
|
|
let client = HttpClient::new(
|
|
|
|
&remote.host,
|
|
|
|
&remote.userid,
|
2020-01-25 11:18:00 +00:00
|
|
|
options,
|
2020-01-10 10:58:31 +00:00
|
|
|
)?;
|
|
|
|
|
|
|
|
let mut rt = tokio::runtime::Runtime::new().unwrap();
|
|
|
|
let result = rt.block_on(client.get("api2/json/admin/datastore", None))?;
|
|
|
|
|
|
|
|
if let Some(data) = result["data"].as_array() {
|
|
|
|
for item in data {
|
|
|
|
if let Some(store) = item["store"].as_str() {
|
|
|
|
list.push(store.to_owned());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}).map_err(|_err: Error| { /* ignore */ });
|
|
|
|
|
|
|
|
list
|
|
|
|
}
|