2020-01-10 10:58:31 +00:00
|
|
|
use std::collections::HashMap;
|
2020-11-03 16:23:43 +00:00
|
|
|
use std::io::{self, Write};
|
2018-12-09 10:59:32 +00:00
|
|
|
|
2020-05-21 09:22:20 +00:00
|
|
|
use anyhow::{format_err, Error};
|
2020-01-30 12:29:39 +00:00
|
|
|
use serde_json::{json, Value};
|
|
|
|
|
2020-05-21 09:14:34 +00:00
|
|
|
use proxmox::api::{api, cli::*, RpcEnvironment};
|
2019-12-19 07:45:27 +00:00
|
|
|
|
|
|
|
use proxmox_backup::tools;
|
2020-05-21 08:46:07 +00:00
|
|
|
use proxmox_backup::config;
|
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::*;
|
|
|
|
|
2020-05-21 08:46:07 +00:00
|
|
|
mod proxmox_backup_manager;
|
|
|
|
use proxmox_backup_manager::*;
|
|
|
|
|
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
|
|
|
|
2020-02-28 06:42:36 +00:00
|
|
|
let output_format = get_output_format(¶m);
|
2019-01-04 10:33:58 +00:00
|
|
|
|
2019-12-19 07:45:27 +00:00
|
|
|
let store = tools::required_string_param(¶m, "store")?;
|
|
|
|
|
2020-12-09 10:59:50 +00:00
|
|
|
let mut client = connect_to_localhost()?;
|
2019-12-19 07:45:27 +00:00
|
|
|
|
|
|
|
let path = format!("api2/json/admin/datastore/{}/gc", store);
|
|
|
|
|
|
|
|
let result = client.post(&path, None).await?;
|
|
|
|
|
2021-01-29 10:21:57 +00:00
|
|
|
view_task_result(&mut client, result, &output_format).await?;
|
2019-12-19 07:45:27 +00:00
|
|
|
|
|
|
|
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> {
|
|
|
|
|
2020-02-28 06:42:36 +00:00
|
|
|
let output_format = get_output_format(¶m);
|
2019-12-19 07:45:27 +00:00
|
|
|
|
|
|
|
let store = tools::required_string_param(¶m, "store")?;
|
|
|
|
|
2020-12-09 10:59:50 +00:00
|
|
|
let client = connect_to_localhost()?;
|
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();
|
2020-12-18 11:26:07 +00:00
|
|
|
let return_type = &api2::admin::datastore::API_METHOD_GARBAGE_COLLECTION_STATUS.returns;
|
2020-01-30 12:29:39 +00:00
|
|
|
|
2020-02-28 06:42:36 +00:00
|
|
|
let options = default_table_format_options();
|
2020-01-30 12:29:39 +00:00
|
|
|
|
2020-12-18 11:26:07 +00:00
|
|
|
format_and_print_result_full(&mut data, return_type, &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> {
|
|
|
|
|
2020-02-28 06:42:36 +00:00
|
|
|
let output_format = get_output_format(¶m);
|
2019-12-19 10:27:36 +00:00
|
|
|
|
2020-12-09 10:59:50 +00:00
|
|
|
let client = connect_to_localhost()?;
|
2019-12-19 10:27:36 +00:00
|
|
|
|
|
|
|
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();
|
2020-12-18 11:26:07 +00:00
|
|
|
let return_type = &api2::node::tasks::API_METHOD_LIST_TASKS.returns;
|
2019-12-19 10:27:36 +00:00
|
|
|
|
2020-02-28 06:42:36 +00:00
|
|
|
let options = default_table_format_options()
|
2020-02-28 06:30:35 +00:00
|
|
|
.column(ColumnConfig::new("starttime").right_align(false).renderer(tools::format::render_epoch))
|
|
|
|
.column(ColumnConfig::new("endtime").right_align(false).renderer(tools::format::render_epoch))
|
2020-01-31 05:59:17 +00:00
|
|
|
.column(ColumnConfig::new("upid"))
|
2020-02-28 06:30:35 +00:00
|
|
|
.column(ColumnConfig::new("status").renderer(tools::format::render_task_status));
|
2020-01-30 12:29:39 +00:00
|
|
|
|
2020-12-18 11:26:07 +00:00
|
|
|
format_and_print_result_full(&mut data, return_type, &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")?;
|
|
|
|
|
2021-01-29 10:21:57 +00:00
|
|
|
let mut client = connect_to_localhost()?;
|
2019-12-19 10:27:36 +00:00
|
|
|
|
2021-01-29 10:21:57 +00:00
|
|
|
display_task_log(&mut client, upid, true).await?;
|
2019-12-19 10:27:36 +00:00
|
|
|
|
|
|
|
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")?;
|
|
|
|
|
2020-12-09 10:59:50 +00:00
|
|
|
let mut client = connect_to_localhost()?;
|
2019-12-19 10:27:36 +00:00
|
|
|
|
2020-11-19 09:02:52 +00:00
|
|
|
let path = format!("api2/json/nodes/localhost/tasks/{}", tools::percent_encode_component(upid_str));
|
2019-12-19 10:27:36 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
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-05-26 10:23:27 +00:00
|
|
|
"remove-vanished": {
|
|
|
|
schema: REMOVE_VANISHED_BACKUPS_SCHEMA,
|
2020-01-17 10:24:55 +00:00
|
|
|
optional: 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-05-26 10:23:27 +00:00
|
|
|
remove_vanished: Option<bool>,
|
2020-02-28 06:42:36 +00:00
|
|
|
param: Value,
|
2020-01-09 13:52:29 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
2020-02-28 06:42:36 +00:00
|
|
|
let output_format = get_output_format(¶m);
|
2020-01-09 13:52:29 +00:00
|
|
|
|
2020-12-09 10:59:50 +00:00
|
|
|
let mut client = connect_to_localhost()?;
|
2020-01-09 13:52:29 +00:00
|
|
|
|
2020-05-27 14:42:23 +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-05-27 14:42:23 +00:00
|
|
|
if let Some(remove_vanished) = remove_vanished {
|
|
|
|
args["remove-vanished"] = Value::from(remove_vanished);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-01-29 10:21:57 +00:00
|
|
|
view_task_result(&mut client, result, &output_format).await?;
|
2020-01-09 13:52:29 +00:00
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2020-06-24 11:34:45 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
"store": {
|
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
|
|
|
"output-format": {
|
|
|
|
schema: OUTPUT_FORMAT,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)]
|
|
|
|
/// Verify backups
|
|
|
|
async fn verify(
|
|
|
|
store: String,
|
|
|
|
param: Value,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let output_format = get_output_format(¶m);
|
|
|
|
|
2020-12-09 10:59:50 +00:00
|
|
|
let mut client = connect_to_localhost()?;
|
2020-06-24 11:34:45 +00:00
|
|
|
|
|
|
|
let args = json!({});
|
|
|
|
|
|
|
|
let path = format!("api2/json/admin/datastore/{}/verify", store);
|
|
|
|
|
|
|
|
let result = client.post(&path, Some(args)).await?;
|
|
|
|
|
2021-01-29 10:21:57 +00:00
|
|
|
view_task_result(&mut client, result, &output_format).await?;
|
2020-06-24 11:34:45 +00:00
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2020-11-03 12:29:08 +00:00
|
|
|
#[api()]
|
|
|
|
/// System report
|
|
|
|
async fn report() -> Result<Value, Error> {
|
2020-11-03 16:23:43 +00:00
|
|
|
let report = proxmox_backup::server::generate_report();
|
|
|
|
io::stdout().write_all(report.as_bytes())?;
|
2020-11-03 12:29:08 +00:00
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2020-11-10 13:22:18 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
verbose: {
|
|
|
|
type: Boolean,
|
|
|
|
optional: true,
|
|
|
|
default: false,
|
|
|
|
description: "Output verbose package information. It is ignored if output-format is specified.",
|
|
|
|
},
|
|
|
|
"output-format": {
|
|
|
|
schema: OUTPUT_FORMAT,
|
|
|
|
optional: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)]
|
|
|
|
/// List package versions for important Proxmox Backup Server packages.
|
|
|
|
async fn get_versions(verbose: bool, param: Value) -> Result<Value, Error> {
|
2020-11-11 17:30:19 +00:00
|
|
|
let output_format = get_output_format(¶m);
|
2020-11-10 13:22:18 +00:00
|
|
|
|
2020-11-12 09:12:54 +00:00
|
|
|
let packages = crate::api2::node::apt::get_versions()?;
|
2020-11-12 09:28:03 +00:00
|
|
|
let mut packages = json!(if verbose { &packages[..] } else { &packages[1..2] });
|
2020-11-10 13:22:18 +00:00
|
|
|
|
2020-11-11 17:30:19 +00:00
|
|
|
let options = default_table_format_options()
|
|
|
|
.disable_sort()
|
|
|
|
.noborder(true) // just not helpfull for version info which gets copy pasted often
|
|
|
|
.column(ColumnConfig::new("Package"))
|
|
|
|
.column(ColumnConfig::new("Version"))
|
|
|
|
.column(ColumnConfig::new("ExtraInfo").header("Extra Info"))
|
|
|
|
;
|
2020-12-18 11:26:07 +00:00
|
|
|
let return_type = &crate::api2::node::apt::API_METHOD_GET_VERSIONS.returns;
|
2020-11-11 17:30:19 +00:00
|
|
|
|
2020-12-18 11:26:07 +00:00
|
|
|
format_and_print_result_full(&mut packages, return_type, &output_format, &options);
|
2020-11-10 13:22:18 +00:00
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2018-12-10 12:28:38 +00:00
|
|
|
fn main() {
|
|
|
|
|
2020-06-15 08:38:30 +00:00
|
|
|
proxmox_backup::tools::setup_safe_path_env();
|
|
|
|
|
2018-12-11 10:31:36 +00:00
|
|
|
let cmd_def = CliCommandMap::new()
|
2020-04-13 09:09:44 +00:00
|
|
|
.insert("acl", acl_commands())
|
2019-12-09 16:40:34 +00:00
|
|
|
.insert("datastore", datastore_commands())
|
2020-06-06 13:40:28 +00:00
|
|
|
.insert("disk", disk_commands())
|
2020-04-26 06:23:23 +00:00
|
|
|
.insert("dns", dns_commands())
|
2020-04-21 12:28:26 +00:00
|
|
|
.insert("network", network_commands())
|
2020-04-06 10:09:27 +00:00
|
|
|
.insert("user", user_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-10-30 11:51:19 +00:00
|
|
|
.insert("subscription", subscription_commands())
|
2020-05-21 09:44:45 +00:00
|
|
|
.insert("sync-job", sync_job_commands())
|
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-06-24 11:34:45 +00:00
|
|
|
)
|
|
|
|
.insert(
|
|
|
|
"verify",
|
|
|
|
CliCommand::new(&API_METHOD_VERIFY)
|
|
|
|
.arg_param(&["store"])
|
|
|
|
.completion_cb("store", config::datastore::complete_datastore_name)
|
2020-11-03 12:29:08 +00:00
|
|
|
)
|
|
|
|
.insert("report",
|
|
|
|
CliCommand::new(&API_METHOD_REPORT)
|
2020-11-10 13:22:18 +00:00
|
|
|
)
|
|
|
|
.insert("versions",
|
|
|
|
CliCommand::new(&API_METHOD_GET_VERSIONS)
|
2020-01-09 13:52:29 +00:00
|
|
|
);
|
2018-12-09 15:52:32 +00:00
|
|
|
|
2020-06-24 11:34:45 +00:00
|
|
|
|
|
|
|
|
2020-05-06 05:51:05 +00:00
|
|
|
let mut rpcenv = CliEnvironment::new();
|
2020-10-23 11:33:21 +00:00
|
|
|
rpcenv.set_auth_id(Some(String::from("root@pam")));
|
2020-05-08 14:07:23 +00:00
|
|
|
|
2020-05-06 05:51:05 +00:00
|
|
|
proxmox_backup::tools::runtime::main(run_async_cli_command(cmd_def, rpcenv));
|
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-11-05 11:12:23 +00:00
|
|
|
let data = crate::tools::runtime::block_on(async move {
|
|
|
|
crate::api2::config::remote::scan_remote_datastores(remote.clone()).await
|
|
|
|
})?;
|
2020-01-10 10:58:31 +00:00
|
|
|
|
2020-11-05 11:12:23 +00:00
|
|
|
for item in data {
|
|
|
|
list.push(item.store);
|
2020-01-10 10:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}).map_err(|_err: Error| { /* ignore */ });
|
|
|
|
|
|
|
|
list
|
|
|
|
}
|