WIP: Automatically create node layout, keys and buckets #883
8 changed files with 214 additions and 175 deletions
|
@ -279,7 +279,8 @@ impl DataLayout {
|
||||||
u16::from_be_bytes([
|
u16::from_be_bytes([
|
||||||
hash.as_slice()[HASH_DRIVE_BYTES.0],
|
hash.as_slice()[HASH_DRIVE_BYTES.0],
|
||||||
hash.as_slice()[HASH_DRIVE_BYTES.1],
|
hash.as_slice()[HASH_DRIVE_BYTES.1],
|
||||||
]) as usize % DRIVE_NPART
|
]) as usize
|
||||||
|
% DRIVE_NPART
|
||||||
}
|
}
|
||||||
|
|
||||||
fn block_dir_from(&self, hash: &Hash, dir: &PathBuf) -> PathBuf {
|
fn block_dir_from(&self, hash: &Hash, dir: &PathBuf) -> PathBuf {
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
use crate::admin::AdminRpc;
|
use crate::admin::AdminRpc;
|
||||||
use crate::cli::{cmd_apply_layout, cmd_assign_role, fetch_layout, fetch_status, ApplyLayoutOpt, AssignRoleOpt, BucketOperation, BucketOpt, KeyImportOpt, KeyInfoOpt, KeyOperation, PermBucketOpt};
|
use crate::cli::{
|
||||||
|
cmd_apply_layout, cmd_assign_role, fetch_layout, fetch_status, ApplyLayoutOpt, AssignRoleOpt,
|
||||||
|
BucketOperation, BucketOpt, KeyImportOpt, KeyInfoOpt, KeyOperation, PermBucketOpt,
|
||||||
|
};
|
||||||
use bytesize::ByteSize;
|
use bytesize::ByteSize;
|
||||||
use garage_model::helper::error::Error as HelperError;
|
use garage_model::helper::error::Error as HelperError;
|
||||||
use garage_net::endpoint::Endpoint;
|
use garage_net::endpoint::Endpoint;
|
||||||
|
@ -17,11 +20,14 @@ pub async fn key_exists(
|
||||||
key_pattern: String,
|
key_pattern: String,
|
||||||
) -> Result<bool, Error> {
|
) -> Result<bool, Error> {
|
||||||
match rpc_cli
|
match rpc_cli
|
||||||
.call(&rpc_host, AdminRpc::KeyOperation(
|
.call(
|
||||||
KeyOperation::Info(KeyInfoOpt{
|
&rpc_host,
|
||||||
|
AdminRpc::KeyOperation(KeyOperation::Info(KeyInfoOpt {
|
||||||
key_pattern,
|
key_pattern,
|
||||||
show_secret: false,
|
show_secret: false,
|
||||||
})), PRIO_NORMAL)
|
})),
|
||||||
|
PRIO_NORMAL,
|
||||||
|
)
|
||||||
.await?
|
.await?
|
||||||
{
|
{
|
||||||
Ok(_) => Ok(true),
|
Ok(_) => Ok(true),
|
||||||
|
@ -36,9 +42,11 @@ pub async fn bucket_exists(
|
||||||
name: String,
|
name: String,
|
||||||
) -> Result<bool, Error> {
|
) -> Result<bool, Error> {
|
||||||
match rpc_cli
|
match rpc_cli
|
||||||
.call(&rpc_host, AdminRpc::BucketOperation(
|
.call(
|
||||||
BucketOperation::Info(BucketOpt{name})
|
&rpc_host,
|
||||||
), PRIO_NORMAL)
|
AdminRpc::BucketOperation(BucketOperation::Info(BucketOpt { name })),
|
||||||
|
PRIO_NORMAL,
|
||||||
|
)
|
||||||
.await?
|
.await?
|
||||||
{
|
{
|
||||||
Ok(_) => Ok(true),
|
Ok(_) => Ok(true),
|
||||||
|
@ -53,18 +61,21 @@ pub async fn key_create(
|
||||||
params: &AutoKey,
|
params: &AutoKey,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
match rpc_cli
|
match rpc_cli
|
||||||
.call(&rpc_host, AdminRpc::KeyOperation(
|
.call(
|
||||||
KeyOperation::Import(KeyImportOpt{
|
&rpc_host,
|
||||||
|
AdminRpc::KeyOperation(KeyOperation::Import(KeyImportOpt {
|
||||||
name: params.name.clone(),
|
name: params.name.clone(),
|
||||||
secret_key: params.secret.clone(),
|
secret_key: params.secret.clone(),
|
||||||
key_id: params.id.clone(),
|
key_id: params.id.clone(),
|
||||||
yes: true,
|
yes: true,
|
||||||
})
|
})),
|
||||||
), PRIO_NORMAL).await?
|
PRIO_NORMAL,
|
||||||
|
)
|
||||||
|
.await?
|
||||||
{
|
{
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(HelperError::BadRequest(msg)) => Err(Error::Message(msg)),
|
Err(HelperError::BadRequest(msg)) => Err(Error::Message(msg)),
|
||||||
resp => Err(Error::unexpected_rpc_message(resp))
|
resp => Err(Error::unexpected_rpc_message(resp)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,14 +85,18 @@ pub async fn bucket_create(
|
||||||
params: &AutoBucket,
|
params: &AutoBucket,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
match rpc_cli
|
match rpc_cli
|
||||||
.call(&rpc_host, AdminRpc::BucketOperation(
|
.call(
|
||||||
BucketOperation::Create(BucketOpt{name: params.name.clone()})
|
&rpc_host,
|
||||||
), PRIO_NORMAL)
|
AdminRpc::BucketOperation(BucketOperation::Create(BucketOpt {
|
||||||
|
name: params.name.clone(),
|
||||||
|
})),
|
||||||
|
PRIO_NORMAL,
|
||||||
|
)
|
||||||
.await?
|
.await?
|
||||||
{
|
{
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(HelperError::BadRequest(msg)) => Err(Error::Message(msg)),
|
Err(HelperError::BadRequest(msg)) => Err(Error::Message(msg)),
|
||||||
resp => Err(Error::unexpected_rpc_message(resp))
|
resp => Err(Error::unexpected_rpc_message(resp)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,20 +107,22 @@ pub async fn grant_permission(
|
||||||
perm: &AutoPermission,
|
perm: &AutoPermission,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
match rpc_cli
|
match rpc_cli
|
||||||
.call(&rpc_host, AdminRpc::BucketOperation(
|
.call(
|
||||||
BucketOperation::Allow(PermBucketOpt{
|
&rpc_host,
|
||||||
|
AdminRpc::BucketOperation(BucketOperation::Allow(PermBucketOpt {
|
||||||
key_pattern: perm.key.clone(),
|
key_pattern: perm.key.clone(),
|
||||||
read: perm.read,
|
read: perm.read,
|
||||||
write: perm.write,
|
write: perm.write,
|
||||||
owner: perm.owner,
|
owner: perm.owner,
|
||||||
bucket: bucket_name,
|
bucket: bucket_name,
|
||||||
})
|
})),
|
||||||
), PRIO_NORMAL)
|
PRIO_NORMAL,
|
||||||
|
)
|
||||||
.await?
|
.await?
|
||||||
{
|
{
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(HelperError::BadRequest(msg)) => Err(Error::Message(msg)),
|
Err(HelperError::BadRequest(msg)) => Err(Error::Message(msg)),
|
||||||
resp => Err(Error::unexpected_rpc_message(resp))
|
resp => Err(Error::unexpected_rpc_message(resp)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,26 +170,31 @@ pub async fn assign_node_layout(
|
||||||
auto_nodes: &Vec<AutoNode>,
|
auto_nodes: &Vec<AutoNode>,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
if unassigned_nodes.len() != auto_nodes.len() {
|
if unassigned_nodes.len() != auto_nodes.len() {
|
||||||
return Err(Error::Message("Cannot apply auto layout: configured nodes do not match actual nodes".to_string()));
|
return Err(Error::Message(
|
||||||
|
"Cannot apply auto layout: configured nodes do not match actual nodes".to_string(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i, node_id) in unassigned_nodes.iter().enumerate() {
|
for (i, node_id) in unassigned_nodes.iter().enumerate() {
|
||||||
if let Some(auto) = auto_nodes.get(i) {
|
if let Some(auto) = auto_nodes.get(i) {
|
||||||
let capacity = auto.capacity.parse::<ByteSize>()?;
|
let capacity = auto.capacity.parse::<ByteSize>()?;
|
||||||
cmd_assign_role(rpc_cli, rpc_host, AssignRoleOpt{
|
cmd_assign_role(
|
||||||
node_ids: vec![format!("{id:?}", id=node_id)],
|
rpc_cli,
|
||||||
|
rpc_host,
|
||||||
|
AssignRoleOpt {
|
||||||
|
node_ids: vec![format!("{id:?}", id = node_id)],
|
||||||
zone: Some(auto.zone.clone()),
|
zone: Some(auto.zone.clone()),
|
||||||
capacity: Some(capacity),
|
capacity: Some(capacity),
|
||||||
gateway: false,
|
gateway: false,
|
||||||
tags: vec![],
|
tags: vec![],
|
||||||
replace: vec![],
|
replace: vec![],
|
||||||
}).await?;
|
},
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd_apply_layout(rpc_cli, rpc_host, ApplyLayoutOpt{
|
cmd_apply_layout(rpc_cli, rpc_host, ApplyLayoutOpt { version: Some(1) }).await?;
|
||||||
version: Some(1),
|
|
||||||
}).await?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
|
@ -47,7 +47,13 @@ pub async fn cli_command_dispatch(
|
||||||
cmd_admin(admin_rpc_endpoint, rpc_host, AdminRpc::MetaOperation(mo)).await
|
cmd_admin(admin_rpc_endpoint, rpc_host, AdminRpc::MetaOperation(mo)).await
|
||||||
}
|
}
|
||||||
Command::Auto => {
|
Command::Auto => {
|
||||||
cmd_auto(admin_rpc_endpoint, system_rpc_endpoint, rpc_host, config.auto.as_ref()).await
|
cmd_auto(
|
||||||
|
admin_rpc_endpoint,
|
||||||
|
system_rpc_endpoint,
|
||||||
|
rpc_host,
|
||||||
|
config.auto.as_ref(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
|
@ -277,7 +283,6 @@ pub async fn cmd_auto(
|
||||||
) -> Result<(), HelperError> {
|
) -> Result<(), HelperError> {
|
||||||
match config {
|
match config {
|
||||||
Some(auto) => {
|
Some(auto) => {
|
||||||
|
|
||||||
// Assign cluster layout if all nodes are unassigned.
|
// Assign cluster layout if all nodes are unassigned.
|
||||||
// This is to ensure a newly created cluster is readily available.
|
// This is to ensure a newly created cluster is readily available.
|
||||||
// Further changes to the cluster layout must be done manually.
|
// Further changes to the cluster layout must be done manually.
|
||||||
|
@ -307,7 +312,9 @@ pub async fn cmd_auto(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
return Err(HelperError::BadRequest("Auto configuration is missing".to_string()))
|
return Err(HelperError::BadRequest(
|
||||||
|
"Auto configuration is missing".to_string(),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -1,15 +1,15 @@
|
||||||
|
pub(crate) mod auto;
|
||||||
pub(crate) mod cmd;
|
pub(crate) mod cmd;
|
||||||
pub(crate) mod init;
|
pub(crate) mod init;
|
||||||
pub(crate) mod layout;
|
pub(crate) mod layout;
|
||||||
pub(crate) mod structs;
|
pub(crate) mod structs;
|
||||||
pub(crate) mod util;
|
pub(crate) mod util;
|
||||||
pub(crate) mod auto;
|
|
||||||
|
|
||||||
pub(crate) mod convert_db;
|
pub(crate) mod convert_db;
|
||||||
|
|
||||||
|
pub(crate) use auto::*;
|
||||||
pub(crate) use cmd::*;
|
pub(crate) use cmd::*;
|
||||||
pub(crate) use init::*;
|
pub(crate) use init::*;
|
||||||
pub(crate) use layout::*;
|
pub(crate) use layout::*;
|
||||||
pub(crate) use structs::*;
|
pub(crate) use structs::*;
|
||||||
pub(crate) use util::*;
|
pub(crate) use util::*;
|
||||||
pub(crate) use auto::*;
|
|
|
@ -284,7 +284,15 @@ async fn cli_command(opt: Opt) -> Result<(), Error> {
|
||||||
let system_rpc_endpoint = netapp.endpoint::<SystemRpc, ()>(SYSTEM_RPC_PATH.into());
|
let system_rpc_endpoint = netapp.endpoint::<SystemRpc, ()>(SYSTEM_RPC_PATH.into());
|
||||||
let admin_rpc_endpoint = netapp.endpoint::<AdminRpc, ()>(ADMIN_RPC_PATH.into());
|
let admin_rpc_endpoint = netapp.endpoint::<AdminRpc, ()>(ADMIN_RPC_PATH.into());
|
||||||
|
|
||||||
match cli_command_dispatch(opt.cmd, &system_rpc_endpoint, &admin_rpc_endpoint, id, config.as_ref().unwrap()).await {
|
match cli_command_dispatch(
|
||||||
|
opt.cmd,
|
||||||
|
&system_rpc_endpoint,
|
||||||
|
&admin_rpc_endpoint,
|
||||||
|
id,
|
||||||
|
config.as_ref().unwrap(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
Err(HelperError::Internal(i)) => Err(Error::Message(format!("Internal error: {}", i))),
|
Err(HelperError::Internal(i)) => Err(Error::Message(format!("Internal error: {}", i))),
|
||||||
Err(HelperError::BadRequest(b)) => Err(Error::Message(b)),
|
Err(HelperError::BadRequest(b)) => Err(Error::Message(b)),
|
||||||
Err(e) => Err(Error::Message(format!("{}", e))),
|
Err(e) => Err(Error::Message(format!("{}", e))),
|
||||||
|
|
|
@ -279,7 +279,8 @@ impl<'a> LockedHelper<'a> {
|
||||||
.local_aliases
|
.local_aliases
|
||||||
.get(alias_name)
|
.get(alias_name)
|
||||||
.cloned()
|
.cloned()
|
||||||
.flatten() != Some(bucket_id)
|
.flatten()
|
||||||
|
!= Some(bucket_id)
|
||||||
{
|
{
|
||||||
return Err(GarageError::Message(format!(
|
return Err(GarageError::Message(format!(
|
||||||
"Bucket {:?} does not have alias {} in namespace of key {}",
|
"Bucket {:?} does not have alias {} in namespace of key {}",
|
||||||
|
|
Loading…
Reference in a new issue