2020-04-19 17:59:59 +00:00
|
|
|
#![recursion_limit = "1024"]
|
|
|
|
|
2020-04-05 21:33:42 +00:00
|
|
|
mod data;
|
2020-04-10 20:01:48 +00:00
|
|
|
mod error;
|
2020-04-08 20:00:41 +00:00
|
|
|
|
2020-04-11 16:51:11 +00:00
|
|
|
mod background;
|
2020-04-06 17:55:39 +00:00
|
|
|
mod membership;
|
2020-04-08 20:00:41 +00:00
|
|
|
mod table;
|
2020-04-19 13:14:23 +00:00
|
|
|
mod table_fullcopy;
|
2020-04-19 11:22:28 +00:00
|
|
|
mod table_sharded;
|
2020-04-16 12:50:49 +00:00
|
|
|
mod table_sync;
|
2020-04-08 20:00:41 +00:00
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
mod block;
|
2020-04-10 21:11:52 +00:00
|
|
|
mod block_ref_table;
|
2020-04-19 15:15:48 +00:00
|
|
|
mod bucket_table;
|
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-19 15:15:48 +00:00
|
|
|
mod admin_rpc;
|
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-12 13:51:19 +00:00
|
|
|
mod tls_util;
|
2020-04-05 21:33:42 +00:00
|
|
|
|
2020-04-19 15:15:48 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
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-18 17:21:34 +00:00
|
|
|
use std::sync::Arc;
|
2020-04-19 11:22:28 +00:00
|
|
|
use std::time::Duration;
|
2020-04-05 21:33:42 +00:00
|
|
|
use structopt::StructOpt;
|
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
use error::Error;
|
2020-04-18 17:30:05 +00:00
|
|
|
use membership::*;
|
2020-04-18 17:21:34 +00:00
|
|
|
use rpc_client::*;
|
2020-04-19 11:22:28 +00:00
|
|
|
use server::TlsConfig;
|
|
|
|
|
2020-04-19 15:15:48 +00:00
|
|
|
use admin_rpc::*;
|
|
|
|
|
2020-04-19 11:22:28 +00:00
|
|
|
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(10);
|
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-16 12:50:49 +00:00
|
|
|
#[structopt(long = "ca-cert")]
|
2020-04-12 17:41:19 +00:00
|
|
|
ca_cert: Option<String>,
|
2020-04-16 12:50:49 +00:00
|
|
|
#[structopt(long = "client-cert")]
|
2020-04-12 17:41:19 +00:00
|
|
|
client_cert: Option<String>,
|
2020-04-16 12:50:49 +00:00
|
|
|
#[structopt(long = "client-key")]
|
2020-04-12 17:41:19 +00:00
|
|
|
client_key: Option<String>,
|
|
|
|
|
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,
|
|
|
|
|
2020-04-19 15:15:48 +00:00
|
|
|
/// Garage node operations
|
|
|
|
#[structopt(name = "node")]
|
|
|
|
Node(NodeOperation),
|
2020-04-16 15:04:28 +00:00
|
|
|
|
2020-04-19 15:15:48 +00:00
|
|
|
/// Bucket operations
|
|
|
|
#[structopt(name = "bucket")]
|
|
|
|
Bucket(BucketOperation),
|
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)]
|
2020-04-19 15:15:48 +00:00
|
|
|
pub enum NodeOperation {
|
|
|
|
/// Configure Garage node
|
|
|
|
#[structopt(name = "configure")]
|
|
|
|
Configure(ConfigureNodeOpt),
|
|
|
|
|
|
|
|
/// Remove Garage node from cluster
|
|
|
|
#[structopt(name = "remove")]
|
|
|
|
Remove(RemoveNodeOpt),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(StructOpt, Debug)]
|
|
|
|
pub struct ConfigureNodeOpt {
|
2020-04-07 14:26:22 +00:00
|
|
|
/// 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
|
|
|
}
|
|
|
|
|
2020-04-16 15:04:28 +00:00
|
|
|
#[derive(StructOpt, Debug)]
|
2020-04-19 15:15:48 +00:00
|
|
|
pub struct RemoveNodeOpt {
|
2020-04-16 15:04:28 +00:00
|
|
|
/// Node to configure (prefix of hexadecimal node id)
|
|
|
|
node_id: String,
|
|
|
|
|
|
|
|
/// If this flag is not given, the node won't be removed
|
|
|
|
#[structopt(long = "yes")]
|
|
|
|
yes: bool,
|
|
|
|
}
|
|
|
|
|
2020-04-19 15:15:48 +00:00
|
|
|
#[derive(Serialize, Deserialize, StructOpt, Debug)]
|
|
|
|
pub enum BucketOperation {
|
|
|
|
/// List buckets
|
|
|
|
#[structopt(name = "list")]
|
|
|
|
List,
|
|
|
|
|
|
|
|
/// Get bucket info
|
|
|
|
#[structopt(name = "info")]
|
|
|
|
Info(BucketOpt),
|
|
|
|
|
|
|
|
/// Create bucket
|
|
|
|
#[structopt(name = "create")]
|
|
|
|
Create(BucketOpt),
|
|
|
|
|
|
|
|
/// Delete bucket
|
|
|
|
#[structopt(name = "delete")]
|
|
|
|
Delete(DeleteBucketOpt),
|
|
|
|
|
|
|
|
/// Allow key to read or write to bucket
|
|
|
|
#[structopt(name = "allow")]
|
|
|
|
Allow(PermBucketOpt),
|
|
|
|
|
|
|
|
/// Allow key to read or write to bucket
|
|
|
|
#[structopt(name = "deny")]
|
|
|
|
Deny(PermBucketOpt),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, StructOpt, Debug)]
|
|
|
|
pub struct BucketOpt {
|
|
|
|
/// Bucket name
|
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, StructOpt, Debug)]
|
|
|
|
pub struct DeleteBucketOpt {
|
|
|
|
/// Bucket name
|
|
|
|
pub name: String,
|
|
|
|
|
|
|
|
/// If this flag is not given, the bucket won't be deleted
|
|
|
|
#[structopt(long = "yes")]
|
|
|
|
pub yes: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, StructOpt, Debug)]
|
|
|
|
pub struct PermBucketOpt {
|
|
|
|
/// Access key ID
|
|
|
|
#[structopt(long = "key")]
|
|
|
|
pub key: String,
|
|
|
|
|
|
|
|
/// Allow/deny read operations
|
|
|
|
#[structopt(long = "read")]
|
|
|
|
pub read: bool,
|
|
|
|
|
|
|
|
/// Allow/deny write operations
|
|
|
|
#[structopt(long = "write")]
|
|
|
|
pub write: bool,
|
|
|
|
|
|
|
|
/// Bucket name
|
|
|
|
pub bucket: String,
|
|
|
|
}
|
|
|
|
|
2020-04-05 21:33:42 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
let opt = Opt::from_args();
|
|
|
|
|
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,
|
|
|
|
_ => {
|
|
|
|
eprintln!("Missing one of: --ca-cert, --node-cert, --node-key. Not using TLS.");
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-18 17:21:34 +00:00
|
|
|
let rpc_http_cli =
|
|
|
|
Arc::new(RpcHttpClient::new(&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
|
|
|
|
2020-04-07 14:26:22 +00:00
|
|
|
let resp = match opt.cmd {
|
2020-04-16 15:04:28 +00:00
|
|
|
Command::Server(server_opt) => {
|
|
|
|
// Abort on panic (same behavior as in Go)
|
|
|
|
std::panic::set_hook(Box::new(|panic_info| {
|
|
|
|
eprintln!("{}", panic_info.to_string());
|
|
|
|
std::process::abort();
|
|
|
|
}));
|
|
|
|
|
|
|
|
server::run_server(server_opt.config_file).await
|
|
|
|
}
|
2020-04-19 15:15:48 +00:00
|
|
|
Command::Status => cmd_status(membership_rpc_cli, opt.rpc_host).await,
|
|
|
|
Command::Node(NodeOperation::Configure(configure_opt)) => {
|
|
|
|
cmd_configure(membership_rpc_cli, opt.rpc_host, configure_opt).await
|
|
|
|
}
|
|
|
|
Command::Node(NodeOperation::Remove(remove_opt)) => {
|
|
|
|
cmd_remove(membership_rpc_cli, opt.rpc_host, remove_opt).await
|
|
|
|
}
|
|
|
|
Command::Bucket(bo) => {
|
|
|
|
cmd_admin(admin_rpc_cli, opt.rpc_host, AdminRPC::BucketOperation(bo)).await
|
2020-04-07 14:26:22 +00:00
|
|
|
}
|
|
|
|
};
|
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-18 17:21:34 +00:00
|
|
|
async fn cmd_status(rpc_cli: RpcAddrClient<Message>, 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!(
|
2020-04-19 17:08:48 +00:00
|
|
|
"{:?}\t{}\t{}\t{}\t{}",
|
|
|
|
adv.id, adv.state_info.hostname, adv.addr, cfg.datacenter, cfg.n_tokens
|
2020-04-10 20:01:48 +00:00
|
|
|
);
|
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-19 11:22:28 +00:00
|
|
|
println!("{:?}\t{}\t{}", 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-19 17:08:48 +00:00
|
|
|
println!("{:?}\t{}\t{}", adv.id, adv.state_info.hostname, 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(
|
2020-04-18 17:21:34 +00:00
|
|
|
rpc_cli: RpcAddrClient<Message>,
|
2020-04-10 20:01:48 +00:00
|
|
|
rpc_host: SocketAddr,
|
2020-04-19 15:15:48 +00:00
|
|
|
args: ConfigureNodeOpt,
|
2020-04-10 20:01:48 +00:00
|
|
|
) -> 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(())
|
|
|
|
}
|
2020-04-16 15:04:28 +00:00
|
|
|
|
|
|
|
async fn cmd_remove(
|
2020-04-18 17:21:34 +00:00
|
|
|
rpc_cli: RpcAddrClient<Message>,
|
2020-04-16 15:04:28 +00:00
|
|
|
rpc_host: SocketAddr,
|
2020-04-19 15:15:48 +00:00
|
|
|
args: RemoveNodeOpt,
|
2020-04-16 15:04:28 +00:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
let mut config = match rpc_cli
|
|
|
|
.call(&rpc_host, &Message::PullConfig, DEFAULT_TIMEOUT)
|
|
|
|
.await?
|
|
|
|
{
|
|
|
|
Message::AdvertiseConfig(cfg) => cfg,
|
|
|
|
resp => return Err(Error::Message(format!("Invalid RPC response: {:?}", resp))),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut candidates = vec![];
|
|
|
|
for (key, _) in config.members.iter() {
|
|
|
|
if hex::encode(key).starts_with(&args.node_id) {
|
|
|
|
candidates.push(key.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if candidates.len() != 1 {
|
|
|
|
return Err(Error::Message(format!(
|
|
|
|
"{} matching nodes",
|
|
|
|
candidates.len()
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if !args.yes {
|
|
|
|
return Err(Error::Message(format!(
|
|
|
|
"Add the flag --yes to really remove {:?} from the cluster",
|
|
|
|
candidates[0]
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
config.members.remove(&candidates[0]);
|
|
|
|
config.version += 1;
|
|
|
|
|
|
|
|
rpc_cli
|
|
|
|
.call(
|
|
|
|
&rpc_host,
|
|
|
|
&Message::AdvertiseConfig(config),
|
|
|
|
DEFAULT_TIMEOUT,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-19 15:15:48 +00:00
|
|
|
|
|
|
|
async fn cmd_admin(
|
|
|
|
rpc_cli: RpcAddrClient<AdminRPC>,
|
|
|
|
rpc_host: SocketAddr,
|
|
|
|
args: AdminRPC,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
match rpc_cli.call(&rpc_host, args, DEFAULT_TIMEOUT).await? {
|
2020-04-19 17:59:59 +00:00
|
|
|
AdminRPC::Ok(msg) => {
|
|
|
|
println!("{}", msg);
|
2020-04-19 15:15:48 +00:00
|
|
|
}
|
|
|
|
AdminRPC::BucketList(bl) => {
|
|
|
|
println!("List of buckets:");
|
|
|
|
for bucket in bl {
|
|
|
|
println!("{}", bucket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AdminRPC::BucketInfo(bucket) => {
|
|
|
|
println!("{:?}", bucket);
|
|
|
|
}
|
|
|
|
r => {
|
|
|
|
eprintln!("Unexpected response: {:?}", r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|