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;
|
2020-04-23 16:05:43 +00:00
|
|
|
|
2020-04-05 21:33:42 +00:00
|
|
|
use structopt::StructOpt;
|
|
|
|
|
2021-10-14 09:50:12 +00:00
|
|
|
use netapp::util::parse_peer_addr;
|
|
|
|
use netapp::NetworkKey;
|
|
|
|
|
2021-03-12 17:16:03 +00:00
|
|
|
use garage_util::error::Error;
|
2020-04-23 17:05:46 +00:00
|
|
|
|
2021-10-14 09:50:12 +00:00
|
|
|
use garage_rpc::system::*;
|
|
|
|
use garage_rpc::*;
|
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 {
|
2021-10-14 09:50:12 +00:00
|
|
|
/// Host to connect to for admin operations, in the format:
|
|
|
|
/// <public-key>@<ip>:<port>
|
|
|
|
#[structopt(short = "h", long = "rpc-host")]
|
|
|
|
pub rpc_host: Option<String>,
|
2020-04-06 17:55:39 +00:00
|
|
|
|
2021-10-14 09:50:12 +00:00
|
|
|
/// RPC secret network key for admin operations
|
|
|
|
#[structopt(short = "s", long = "rpc-secret")]
|
|
|
|
pub rpc_secret: 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> {
|
2021-10-14 09:50:12 +00:00
|
|
|
let net_key_hex_str = &opt.rpc_secret.expect("No RPC secret provided");
|
|
|
|
let network_key = NetworkKey::from_slice(
|
|
|
|
&hex::decode(net_key_hex_str).expect("Invalid RPC secret key (bad hex)")[..],
|
|
|
|
)
|
|
|
|
.expect("Invalid RPC secret provided (wrong length)");
|
|
|
|
let (_pk, sk) = sodiumoxide::crypto::sign::ed25519::gen_keypair();
|
|
|
|
|
|
|
|
let netapp = NetApp::new(network_key, sk);
|
|
|
|
let (id, addr) =
|
|
|
|
parse_peer_addr(&opt.rpc_host.expect("No RPC host provided")).expect("Invalid RPC host");
|
|
|
|
netapp.clone().try_connect(addr, id).await?;
|
|
|
|
|
|
|
|
let system_rpc_endpoint = netapp.endpoint::<SystemRpc, ()>(SYSTEM_RPC_PATH.into());
|
|
|
|
let admin_rpc_endpoint = netapp.endpoint::<AdminRpc, ()>(ADMIN_RPC_PATH.into());
|
|
|
|
|
|
|
|
cli_cmd(opt.cmd, &system_rpc_endpoint, &admin_rpc_endpoint, id).await
|
2021-06-01 17:05:15 +00:00
|
|
|
}
|