2020-04-05 21:33:42 +00:00
|
|
|
mod data;
|
2020-04-10 20:01:48 +00:00
|
|
|
mod error;
|
2020-04-05 21:33:42 +00:00
|
|
|
mod proto;
|
2020-04-08 20:00:41 +00:00
|
|
|
|
2020-04-06 17:55:39 +00:00
|
|
|
mod membership;
|
2020-04-08 20:00:41 +00:00
|
|
|
mod table;
|
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
mod block;
|
2020-04-09 15:32:28 +00:00
|
|
|
mod object_table;
|
2020-04-09 21:45:07 +00:00
|
|
|
mod version_table;
|
2020-04-08 20:00:41 +00:00
|
|
|
|
2020-04-07 14:26:22 +00:00
|
|
|
mod api_server;
|
2020-04-10 20:26:48 +00:00
|
|
|
mod http_util;
|
2020-04-10 20:01:48 +00:00
|
|
|
mod rpc_client;
|
|
|
|
mod rpc_server;
|
|
|
|
mod server;
|
2020-04-05 21:33:42 +00:00
|
|
|
|
2020-04-07 14:26:22 +00:00
|
|
|
use std::collections::HashSet;
|
2020-04-06 17:55:39 +00:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
use std::path::PathBuf;
|
2020-04-05 21:33:42 +00:00
|
|
|
use structopt::StructOpt;
|
|
|
|
|
2020-04-07 14:26:22 +00:00
|
|
|
use data::*;
|
2020-04-10 20:01:48 +00:00
|
|
|
use error::Error;
|
2020-04-07 14:26:22 +00:00
|
|
|
use proto::*;
|
2020-04-10 20:01:48 +00:00
|
|
|
use rpc_client::RpcClient;
|
2020-04-05 21:33:42 +00:00
|
|
|
|
|
|
|
#[derive(StructOpt, Debug)]
|
|
|
|
#[structopt(name = "garage")]
|
|
|
|
pub struct Opt {
|
2020-04-07 14:26:22 +00:00
|
|
|
/// RPC connect to this host to execute client operations
|
|
|
|
#[structopt(short = "h", long = "rpc-host", default_value = "127.0.0.1:3901")]
|
|
|
|
rpc_host: SocketAddr,
|
2020-04-06 17:55:39 +00:00
|
|
|
|
2020-04-07 14:26:22 +00:00
|
|
|
#[structopt(subcommand)]
|
|
|
|
cmd: Command,
|
2020-04-05 21:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 14:26:22 +00:00
|
|
|
#[derive(StructOpt, Debug)]
|
|
|
|
pub enum Command {
|
|
|
|
/// Run Garage server
|
|
|
|
#[structopt(name = "server")]
|
|
|
|
Server(ServerOpt),
|
|
|
|
|
|
|
|
/// Get network status
|
|
|
|
#[structopt(name = "status")]
|
|
|
|
Status,
|
|
|
|
|
|
|
|
/// Configure Garage node
|
|
|
|
#[structopt(name = "configure")]
|
|
|
|
Configure(ConfigureOpt),
|
2020-04-06 17:55:39 +00:00
|
|
|
}
|
2020-04-05 21:33:42 +00:00
|
|
|
|
2020-04-07 14:26:22 +00:00
|
|
|
#[derive(StructOpt, Debug)]
|
|
|
|
pub struct ServerOpt {
|
|
|
|
/// Configuration file
|
|
|
|
#[structopt(short = "c", long = "config", default_value = "./config.toml")]
|
|
|
|
config_file: PathBuf,
|
2020-04-05 21:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 14:26:22 +00:00
|
|
|
#[derive(StructOpt, Debug)]
|
|
|
|
pub struct ConfigureOpt {
|
|
|
|
/// Node to configure (prefix of hexadecimal node id)
|
|
|
|
node_id: String,
|
2020-04-05 21:33:42 +00:00
|
|
|
|
2020-04-07 15:00:48 +00:00
|
|
|
/// Location (datacenter) of the node
|
|
|
|
datacenter: String,
|
|
|
|
|
2020-04-07 14:26:22 +00:00
|
|
|
/// Number of tokens
|
|
|
|
n_tokens: u32,
|
2020-04-05 21:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
let opt = Opt::from_args();
|
|
|
|
|
2020-04-07 14:26:22 +00:00
|
|
|
let rpc_cli = RpcClient::new();
|
2020-04-05 21:33:42 +00:00
|
|
|
|
2020-04-07 14:26:22 +00:00
|
|
|
let resp = match opt.cmd {
|
2020-04-10 20:01:48 +00:00
|
|
|
Command::Server(server_opt) => server::run_server(server_opt.config_file).await,
|
|
|
|
Command::Status => cmd_status(rpc_cli, opt.rpc_host).await,
|
2020-04-07 14:26:22 +00:00
|
|
|
Command::Configure(configure_opt) => {
|
|
|
|
cmd_configure(rpc_cli, opt.rpc_host, configure_opt).await
|
|
|
|
}
|
|
|
|
};
|
2020-04-05 21:33:42 +00:00
|
|
|
|
2020-04-07 14:26:22 +00:00
|
|
|
if let Err(e) = resp {
|
|
|
|
eprintln!("Error: {}", e);
|
|
|
|
}
|
|
|
|
}
|
2020-04-05 21:33:42 +00:00
|
|
|
|
2020-04-07 14:26:22 +00:00
|
|
|
async fn cmd_status(rpc_cli: RpcClient, rpc_host: SocketAddr) -> Result<(), Error> {
|
2020-04-10 20:01:48 +00:00
|
|
|
let status = match rpc_cli
|
|
|
|
.call(&rpc_host, &Message::PullStatus, DEFAULT_TIMEOUT)
|
|
|
|
.await?
|
|
|
|
{
|
2020-04-07 14:26:22 +00:00
|
|
|
Message::AdvertiseNodesUp(nodes) => nodes,
|
2020-04-10 20:01:48 +00:00
|
|
|
resp => return Err(Error::Message(format!("Invalid RPC response: {:?}", resp))),
|
2020-04-07 14:26:22 +00:00
|
|
|
};
|
2020-04-10 20:01:48 +00:00
|
|
|
let config = match rpc_cli
|
|
|
|
.call(&rpc_host, &Message::PullConfig, DEFAULT_TIMEOUT)
|
|
|
|
.await?
|
|
|
|
{
|
2020-04-07 14:26:22 +00:00
|
|
|
Message::AdvertiseConfig(cfg) => cfg,
|
2020-04-10 20:01:48 +00:00
|
|
|
resp => return Err(Error::Message(format!("Invalid RPC response: {:?}", resp))),
|
2020-04-07 14:26:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
println!("Healthy nodes:");
|
|
|
|
for adv in status.iter() {
|
|
|
|
if let Some(cfg) = config.members.get(&adv.id) {
|
2020-04-10 20:01:48 +00:00
|
|
|
println!(
|
|
|
|
"{}\t{}\t{}\t{}",
|
|
|
|
hex::encode(&adv.id),
|
|
|
|
cfg.datacenter,
|
|
|
|
cfg.n_tokens,
|
|
|
|
adv.addr
|
|
|
|
);
|
2020-04-07 14:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-05 21:33:42 +00:00
|
|
|
|
2020-04-07 14:26:22 +00:00
|
|
|
let status_keys = status.iter().map(|x| x.id.clone()).collect::<HashSet<_>>();
|
2020-04-10 20:01:48 +00:00
|
|
|
if config
|
|
|
|
.members
|
|
|
|
.iter()
|
|
|
|
.any(|(id, _)| !status_keys.contains(id))
|
|
|
|
{
|
2020-04-07 14:26:22 +00:00
|
|
|
println!("\nFailed nodes:");
|
|
|
|
for (id, cfg) in config.members.iter() {
|
|
|
|
if !status.iter().any(|x| x.id == *id) {
|
2020-04-07 16:10:20 +00:00
|
|
|
println!("{}\t{}\t{}", hex::encode(&id), cfg.datacenter, cfg.n_tokens);
|
2020-04-07 14:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-05 21:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
if status
|
|
|
|
.iter()
|
|
|
|
.any(|adv| !config.members.contains_key(&adv.id))
|
|
|
|
{
|
2020-04-07 14:26:22 +00:00
|
|
|
println!("\nUnconfigured nodes:");
|
|
|
|
for adv in status.iter() {
|
|
|
|
if !config.members.contains_key(&adv.id) {
|
2020-04-07 16:10:20 +00:00
|
|
|
println!("{}\t{}", hex::encode(&adv.id), adv.addr);
|
2020-04-07 14:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-05 21:33:42 +00:00
|
|
|
}
|
2020-04-07 14:26:22 +00:00
|
|
|
|
|
|
|
Ok(())
|
2020-04-05 21:33:42 +00:00
|
|
|
}
|
2020-04-06 17:55:39 +00:00
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
async fn cmd_configure(
|
|
|
|
rpc_cli: RpcClient,
|
|
|
|
rpc_host: SocketAddr,
|
|
|
|
args: ConfigureOpt,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let status = match rpc_cli
|
|
|
|
.call(&rpc_host, &Message::PullStatus, DEFAULT_TIMEOUT)
|
|
|
|
.await?
|
|
|
|
{
|
2020-04-07 14:26:22 +00:00
|
|
|
Message::AdvertiseNodesUp(nodes) => nodes,
|
2020-04-10 20:01:48 +00:00
|
|
|
resp => return Err(Error::Message(format!("Invalid RPC response: {:?}", resp))),
|
2020-04-07 14:26:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut candidates = vec![];
|
|
|
|
for adv in status.iter() {
|
2020-04-07 16:10:20 +00:00
|
|
|
if hex::encode(&adv.id).starts_with(&args.node_id) {
|
2020-04-07 14:26:22 +00:00
|
|
|
candidates.push(adv.id.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if candidates.len() != 1 {
|
2020-04-10 20:01:48 +00:00
|
|
|
return Err(Error::Message(format!(
|
|
|
|
"{} matching nodes",
|
|
|
|
candidates.len()
|
|
|
|
)));
|
2020-04-07 14:26:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
let mut config = match rpc_cli
|
|
|
|
.call(&rpc_host, &Message::PullConfig, DEFAULT_TIMEOUT)
|
|
|
|
.await?
|
|
|
|
{
|
2020-04-07 14:26:22 +00:00
|
|
|
Message::AdvertiseConfig(cfg) => cfg,
|
2020-04-10 20:01:48 +00:00
|
|
|
resp => return Err(Error::Message(format!("Invalid RPC response: {:?}", resp))),
|
2020-04-07 14:26:22 +00:00
|
|
|
};
|
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
config.members.insert(
|
|
|
|
candidates[0].clone(),
|
|
|
|
NetworkConfigEntry {
|
|
|
|
datacenter: args.datacenter,
|
|
|
|
n_tokens: args.n_tokens,
|
|
|
|
},
|
|
|
|
);
|
2020-04-07 14:26:22 +00:00
|
|
|
config.version += 1;
|
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
rpc_cli
|
|
|
|
.call(
|
|
|
|
&rpc_host,
|
|
|
|
&Message::AdvertiseConfig(config),
|
|
|
|
DEFAULT_TIMEOUT,
|
|
|
|
)
|
|
|
|
.await?;
|
2020-04-07 14:26:22 +00:00
|
|
|
Ok(())
|
|
|
|
}
|