Skip to content

Commit

Permalink
feat: obtain volume size from node count
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin authored and jacderida committed Nov 28, 2024
1 parent 132f64e commit a4791a9
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 28 deletions.
4 changes: 0 additions & 4 deletions resources/terraform/testnet/digital-ocean/dev.tfvars
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
bootstrap_droplet_size = "s-2vcpu-4gb"
bootstrap_droplet_image_id = 162461040
bootstrap_node_vm_count = 1
bootstrap_node_volume_size = 5
evm_node_droplet_size = "s-4vcpu-8gb"
evm_node_droplet_image_id = 167317579
evm_node_vm_count = 1
genesis_node_volume_size = 5
nat_gateway_droplet_image_id = 166664184
node_droplet_size = "s-4vcpu-8gb"
node_droplet_image_id = 162460774
node_vm_count = 10
node_volume_size = 10
private_node_vm_count = 1
private_node_volume_size = 10
setup_nat_gateway = true
uploader_droplet_size = "s-2vcpu-4gb"
uploader_droplet_image_id = 162461150
Expand Down
4 changes: 0 additions & 4 deletions resources/terraform/testnet/digital-ocean/staging.tfvars
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
bootstrap_droplet_size = "s-2vcpu-4gb"
bootstrap_droplet_image_id = 162461040
bootstrap_node_vm_count = 2
bootstrap_node_volume_size = 5
evm_node_vm_count = 1
evm_node_droplet_size = "s-4vcpu-8gb"
evm_node_droplet_image_id = 167317579
genesis_node_volume_size = 5
nat_gateway_droplet_image_id = 166664184
node_droplet_size = "s-4vcpu-8gb"
node_droplet_image_id = 162460774
node_vm_count = 39
node_volume_size = 10
private_node_vm_count = 1
private_node_volume_size = 10
setup_nat_gateway = true
uploader_droplet_size = "s-2vcpu-4gb"
uploader_droplet_image_id = 162461150
Expand Down
47 changes: 45 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub mod ssh;
pub mod terraform;
pub mod upscale;

const STORAGE_REQUIRED_PER_NODE: u16 = 7;

use crate::{
ansible::{
extra_vars::ExtraVarsDocBuilder,
Expand Down Expand Up @@ -919,7 +921,7 @@ impl TestnetDeployer {
Ok(())
}

pub async fn clean(&self) -> Result<()> {
pub async fn clean(&self, inventory: &DeploymentInventory) -> Result<()> {
let environment_details =
get_environment_details(&self.environment_name, &self.s3_repository).await?;

Expand Down Expand Up @@ -966,6 +968,7 @@ impl TestnetDeployer {
self.working_directory_path.clone(),
&self.terraform_runner,
None,
Some(inventory),
)?;
self.s3_repository
.delete_object("sn-environment-type", &self.environment_name)
Expand Down Expand Up @@ -1297,6 +1300,7 @@ pub fn do_clean(
working_directory_path: PathBuf,
terraform_runner: &TerraformRunner,
inventory_types: Option<Vec<AnsibleInventoryType>>,
inventory: Option<&DeploymentInventory>,
) -> Result<()> {
terraform_runner.init()?;
let workspaces = terraform_runner.workspace_list()?;
Expand All @@ -1306,7 +1310,36 @@ pub fn do_clean(
terraform_runner.workspace_select(name)?;
println!("Selected {name} workspace");

terraform_runner.destroy(environment_type.map(|et| et.get_tfvars_filename()))?;
let args = if let Some(inventory) = inventory {
let options = InfraRunOptions::generate_from_deployment(inventory, terraform_runner)?;
let mut args = Vec::new();
if let Some(bootstrap_node_volume_size) = options.bootstrap_node_volume_size {
args.push((
"bootstrap_node_volume_size".to_string(),
bootstrap_node_volume_size.to_string(),
));
}
if let Some(genesis_node_volume_size) = options.genesis_node_volume_size {
args.push((
"genesis_node_volume_size".to_string(),
genesis_node_volume_size.to_string(),
));
}
if let Some(node_volume_size) = options.node_volume_size {
args.push(("node_volume_size".to_string(), node_volume_size.to_string()));
}
if let Some(private_node_volume_size) = options.private_node_volume_size {
args.push((
"private_node_volume_size".to_string(),
private_node_volume_size.to_string(),
));
}
Some(args)
} else {
None
};

terraform_runner.destroy(args, environment_type.map(|et| et.get_tfvars_filename()))?;

// The 'dev' workspace is one we always expect to exist, for admin purposes.
// You can't delete a workspace while it is selected, so we select 'dev' before we delete
Expand Down Expand Up @@ -1501,3 +1534,13 @@ pub async fn write_environment_details(
.await?;
Ok(())
}

pub fn calculate_size_per_attached_volume(node_count: u16) -> u16 {
if node_count == 0 {
return 0;
}
let total_volume_required = node_count * STORAGE_REQUIRED_PER_NODE;

// 7 attached volumes per VM
(total_volume_required as f64 / 7.0).ceil() as u16
}
1 change: 1 addition & 0 deletions src/logstash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ impl LogstashDeploy {
self.working_directory_path.clone(),
&self.terraform_runner,
Some(vec![AnsibleInventoryType::Logstash]),
None,
)
}

Expand Down
54 changes: 37 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use sn_testnet_deploy::{
AnsiblePlaybook,
},
bootstrap::BootstrapOptions,
calculate_size_per_attached_volume,
deploy::DeployOptions,
error::Error,
funding::FundingOptions,
Expand Down Expand Up @@ -1553,6 +1554,10 @@ async fn main() -> Result<()> {
}
}

let node_count = node_count.unwrap_or(environment_type.get_default_node_count());
let private_node_count =
private_node_count.unwrap_or(environment_type.get_default_private_node_count());

testnet_deployer
.bootstrap(&BootstrapOptions {
binary_option,
Expand All @@ -1566,20 +1571,21 @@ async fn main() -> Result<()> {
interval,
log_format,
name: name.clone(),
node_count: node_count.unwrap_or(environment_type.get_default_node_count()),
node_volume_size,
node_count,
node_vm_count,
node_vm_size,
node_volume_size: node_volume_size
.or_else(|| Some(calculate_size_per_attached_volume(node_count))),
max_archived_log_files,
max_log_files,
node_vm_size,
output_inventory_dir_path: inventory_service
.working_directory_path
.join("ansible")
.join("inventory"),
private_node_vm_count,
private_node_count: private_node_count
.unwrap_or(environment_type.get_default_private_node_count()),
private_node_volume_size,
node_vm_count,
private_node_count,
private_node_volume_size: private_node_volume_size
.or_else(|| Some(calculate_size_per_attached_volume(private_node_count))),
rewards_address,
chunk_size,
})
Expand All @@ -1598,7 +1604,13 @@ async fn main() -> Result<()> {
.environment_name(&name)
.provider(provider)
.build()?;
testnet_deployer.clean().await?;

let inventory_service = DeploymentInventoryService::from(&testnet_deployer);
let inventory = inventory_service
.generate_or_retrieve_inventory(&name, true, None)
.await?;

testnet_deployer.clean(&inventory).await?;
Ok(())
}
Commands::Deploy {
Expand Down Expand Up @@ -1745,13 +1757,19 @@ async fn main() -> Result<()> {
}
};

let bootstrap_node_count =
bootstrap_node_count.unwrap_or(environment_type.get_default_bootstrap_node_count());
let node_count = node_count.unwrap_or(environment_type.get_default_node_count());
let private_node_count =
private_node_count.unwrap_or(environment_type.get_default_private_node_count());

testnet_deployer
.deploy(&DeployOptions {
binary_option: binary_option.clone(),
bootstrap_node_count: bootstrap_node_count
.unwrap_or(environment_type.get_default_bootstrap_node_count()),
bootstrap_node_count,
bootstrap_node_vm_count,
bootstrap_node_volume_size,
bootstrap_node_volume_size: bootstrap_node_volume_size
.or_else(|| Some(calculate_size_per_attached_volume(bootstrap_node_count))),
chunk_size,
current_inventory: inventory,
downloaders_count,
Expand All @@ -1763,24 +1781,26 @@ async fn main() -> Result<()> {
evm_rpc_url,
evm_node_vm_size,
funding_wallet_secret_key,
genesis_node_volume_size,
genesis_node_volume_size: genesis_node_volume_size
.or_else(|| Some(calculate_size_per_attached_volume(1))),
interval,
log_format,
logstash_details,
name: name.clone(),
node_count: node_count.unwrap_or(environment_type.get_default_node_count()),
node_count,
node_vm_count,
node_volume_size,
node_volume_size: node_volume_size
.or_else(|| Some(calculate_size_per_attached_volume(node_count))),
max_archived_log_files,
max_log_files,
output_inventory_dir_path: inventory_service
.working_directory_path
.join("ansible")
.join("inventory"),
private_node_vm_count,
private_node_count: private_node_count
.unwrap_or(environment_type.get_default_private_node_count()),
private_node_volume_size,
private_node_count,
private_node_volume_size: private_node_volume_size
.or_else(|| Some(calculate_size_per_attached_volume(private_node_count))),
public_rpc,
uploaders_count,
uploader_vm_count,
Expand Down
12 changes: 11 additions & 1 deletion src/terraform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,21 @@ impl TerraformRunner {
Ok(())
}

pub fn destroy(&self, tfvars_filename: Option<String>) -> Result<()> {
pub fn destroy(
&self,
vars: Option<Vec<(String, String)>>,
tfvars_filename: Option<String>,
) -> Result<()> {
let mut args = vec!["destroy".to_string(), "-auto-approve".to_string()];
if let Some(tfvars_filename) = tfvars_filename {
args.push(format!("-var-file={}", tfvars_filename));
}
if let Some(vars) = vars {
for var in vars.iter() {
args.push("-var".to_string());
args.push(format!("{}={}", var.0, var.1));
}
}
run_external_command(
self.binary_path.clone(),
self.working_directory_path.clone(),
Expand Down

0 comments on commit a4791a9

Please sign in to comment.