2020-04-19 17:59:59 +00:00
|
|
|
#![recursion_limit = "1024"]
|
2021-04-06 03:25:28 +00:00
|
|
|
//! Garage CLI, used to interact with a running Garage instance, and to launch a Garage instance
|
2020-04-19 17:59:59 +00:00
|
|
|
|
2020-04-21 12:54:55 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
|
2020-04-19 15:15:48 +00:00
|
|
|
mod admin_rpc;
|
2021-03-12 17:16:03 +00:00
|
|
|
mod cli;
|
2020-04-24 10:10:01 +00:00
|
|
|
mod repair;
|
2020-04-10 20:01:48 +00:00
|
|
|
mod server;
|
2020-04-05 21:33:42 +00:00
|
|
|
|
2020-04-06 17:55:39 +00:00
|
|
|
use std::net::SocketAddr;
|
2021-03-12 17:12:31 +00:00
|
|
|
use std::sync::Arc;
|
2021-03-12 17:16:03 +00:00
|
|
|
use std::time::Duration;
|
2020-04-23 16:05:43 +00:00
|
|
|
|
2020-04-05 21:33:42 +00:00
|
|
|
use structopt::StructOpt;
|
|
|
|
|
2021-03-12 17:12:31 +00:00
|
|
|
use garage_util::config::TlsConfig;
|
2021-03-12 17:16:03 +00:00
|
|
|
use garage_util::error::Error;
|
2020-04-23 17:05:46 +00:00
|
|
|
|
2021-03-12 17:12:31 +00:00
|
|
|
use garage_rpc::membership::*;
|
2021-03-12 17:16:03 +00:00
|
|
|
use garage_rpc::rpc_client::*;
|
2020-04-19 11:22:28 +00:00
|
|
|
|
2020-04-19 15:15:48 +00:00
|
|
|
use admin_rpc::*;
|
2021-03-12 17:16:03 +00:00
|
|
|
use cli::*;
|
2020-04-19 15:15:48 +00:00
|
|
|
|
2020-04-05 21:33:42 +00:00
|
|
|
#[derive(StructOpt, Debug)]
|
|
|
|
#[structopt(name = "garage")]
|
2021-03-26 21:36:23 +00:00
|
|
|
struct Opt {
|
2020-04-07 14:26:22 +00:00
|
|
|
/// RPC connect to this host to execute client operations
|
2021-06-01 17:05:15 +00:00
|
|
|
#[structopt(short = "h", long = "rpc-host", default_value = "127.0.0.1:3901", parse(try_from_str = parse_address))]
|
2021-03-12 17:12:31 +00:00
|
|
|
pub rpc_host: SocketAddr,
|
2020-04-06 17:55:39 +00:00
|
|
|
|
2020-04-16 12:50:49 +00:00
|
|
|
#[structopt(long = "ca-cert")]
|
2021-03-12 17:12:31 +00:00
|
|
|
pub ca_cert: Option<String>,
|
2020-04-16 12:50:49 +00:00
|
|
|
#[structopt(long = "client-cert")]
|
2021-03-12 17:12:31 +00:00
|
|
|
pub client_cert: Option<String>,
|
2020-04-16 12:50:49 +00:00
|
|
|
#[structopt(long = "client-key")]
|
2021-03-12 17:12:31 +00:00
|
|
|
pub client_key: Option<String>,
|
2020-04-12 17:41:19 +00:00
|
|
|
|
2020-04-07 14:26:22 +00:00
|
|
|
#[structopt(subcommand)]
|
|
|
|
cmd: Command,
|
2020-04-05 21:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2020-04-21 12:54:55 +00:00
|
|
|
pretty_env_logger::init();
|
|
|
|
|
2020-04-05 21:33:42 +00:00
|
|
|
let opt = Opt::from_args();
|
|
|
|
|
2021-03-12 17:12:31 +00:00
|
|
|
let res = if let Command::Server(server_opt) = opt.cmd {
|
|
|
|
// Abort on panic (same behavior as in Go)
|
|
|
|
std::panic::set_hook(Box::new(|panic_info| {
|
|
|
|
error!("{}", panic_info.to_string());
|
|
|
|
std::process::abort();
|
|
|
|
}));
|
|
|
|
|
|
|
|
server::run_server(server_opt.config_file).await
|
|
|
|
} else {
|
|
|
|
cli_command(opt).await
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(e) = res {
|
|
|
|
error!("{}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn cli_command(opt: Opt) -> Result<(), Error> {
|
2020-04-12 17:41:19 +00:00
|
|
|
let tls_config = match (opt.ca_cert, opt.client_cert, opt.client_key) {
|
2020-04-16 12:50:49 +00:00
|
|
|
(Some(ca_cert), Some(client_cert), Some(client_key)) => Some(TlsConfig {
|
|
|
|
ca_cert,
|
|
|
|
node_cert: client_cert,
|
|
|
|
node_key: client_key,
|
|
|
|
}),
|
2020-04-12 17:41:19 +00:00
|
|
|
(None, None, None) => None,
|
|
|
|
_ => {
|
2020-04-21 12:54:55 +00:00
|
|
|
warn!("Missing one of: --ca-cert, --node-cert, --node-key. Not using TLS.");
|
2020-04-12 17:41:19 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-18 17:21:34 +00:00
|
|
|
let rpc_http_cli =
|
2020-04-22 16:51:52 +00:00
|
|
|
Arc::new(RpcHttpClient::new(8, &tls_config).expect("Could not create RPC client"));
|
2020-04-19 15:15:48 +00:00
|
|
|
let membership_rpc_cli =
|
|
|
|
RpcAddrClient::new(rpc_http_cli.clone(), MEMBERSHIP_RPC_PATH.to_string());
|
|
|
|
let admin_rpc_cli = RpcAddrClient::new(rpc_http_cli.clone(), ADMIN_RPC_PATH.to_string());
|
2020-04-05 21:33:42 +00:00
|
|
|
|
2021-03-12 17:12:31 +00:00
|
|
|
cli_cmd(opt.cmd, membership_rpc_cli, admin_rpc_cli, opt.rpc_host).await
|
2020-04-19 15:15:48 +00:00
|
|
|
}
|
2021-06-01 17:05:15 +00:00
|
|
|
|
|
|
|
fn parse_address(address: &str) -> Result<SocketAddr, String> {
|
|
|
|
use std::net::ToSocketAddrs;
|
|
|
|
address
|
|
|
|
.to_socket_addrs()
|
|
|
|
.map_err(|_| format!("Could not resolve {}", address))?
|
|
|
|
.next()
|
|
|
|
.ok_or_else(|| format!("Could not resolve {}", address))
|
|
|
|
}
|