Skip to content

Commit

Permalink
fix: make uploader upscale work
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin authored and jacderida committed Oct 25, 2024
1 parent 1eeeae2 commit e4565e0
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 85 deletions.
2 changes: 1 addition & 1 deletion src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ impl TestnetDeployer {
deployment_type: DeploymentType::New,
environment_type: options.environment_type.clone(),
evm_network: options.evm_network.clone(),
rewards_address: options.rewards_address.clone(),
evm_testnet_data: evm_testnet_data.clone(),
rewards_address: options.rewards_address.clone(),
},
)
.await?;
Expand Down
23 changes: 14 additions & 9 deletions src/funding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
};
use alloy::{network::EthereumWallet, signers::local::PrivateKeySigner};
use evmlib::{common::U256, wallet::Wallet, Network};
use log::{debug, error};
use log::{debug, error, warn};
use std::collections::HashMap;
use std::str::FromStr;

Expand Down Expand Up @@ -48,11 +48,12 @@ impl AnsibleProvisioner {

for (vm, count) in uploaders_count {
if count == 0 {
error!("No uploader instances found for {:?}", vm.name);
warn!("No uploader instances found for {:?}, ", vm.name);
uploader_secret_keys.insert(vm.clone(), Vec::new());
} else {
let sks = self.get_uploader_secret_key_per_vm(&vm, count)?;
uploader_secret_keys.insert(vm.clone(), sks);
}

let sks = self.get_uploader_secret_key_per_vm(&vm, count)?;
uploader_secret_keys.insert(vm.clone(), sks);
}

Ok(uploader_secret_keys)
Expand Down Expand Up @@ -86,7 +87,9 @@ impl AnsibleProvisioner {
vm.name
);
for _ in 0..missing_keys_count {
keys.push(PrivateKeySigner::random());
let sk = PrivateKeySigner::random();
debug!("Generated key with address: {}", sk.address());
keys.push(sk);
}
}
}
Expand Down Expand Up @@ -124,7 +127,7 @@ impl AnsibleProvisioner {
let cmd = "systemctl list-units --type=service | grep autonomi_uploader_ | wc -l";
let result = self
.ssh_client
.run_command(&vm.public_ip_addr, "root", cmd, false);
.run_command(&vm.public_ip_addr, "root", cmd, true);
match result {
Ok(count) => {
debug!("Count found to be {count:?}, parsing");
Expand Down Expand Up @@ -172,17 +175,19 @@ impl AnsibleProvisioner {
let mut sks_per_vm = Vec::new();

debug!(
"Fetching uploader count for {} @ {}",
"Fetching uploader secret key for {} @ {}",
vm.name, vm.public_ip_addr
);
// Note: if this is gonna be parallelized, we need to make sure the secret keys are in order.
// the playbook expects them in order
for count in 1..=instance_count {
let cmd = format!(
"systemctl show autonomi_uploader_{count}.service --property=Environment | grep SECRET_KEY | cut -d= -f3 | awk '{{print $1}}'"
);
debug!("Fetching secret key for {} instance {count}", vm.name);
let result = self
.ssh_client
.run_command(&vm.public_ip_addr, "root", &cmd, false);
.run_command(&vm.public_ip_addr, "root", &cmd, true);
match result {
Ok(secret_keys) => {
let sk_str = secret_keys
Expand Down
2 changes: 1 addition & 1 deletion src/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl DeploymentInventoryService {
wallet_public_key: sks
.iter()
.enumerate()
.map(|(user, sk)| (format!("safe{user}"), sk.address().encode_hex()))
.map(|(user, sk)| (format!("safe{}", user + 1), sk.address().encode_hex()))
.collect(),
})
.collect::<Vec<_>>();
Expand Down
129 changes: 58 additions & 71 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use crate::{
};
use flate2::read::GzDecoder;
use indicatif::{ProgressBar, ProgressStyle};
use log::debug;
use log::{debug, trace};
use semver::Version;
use serde::{Deserialize, Serialize};
use serde_json::json;
Expand Down Expand Up @@ -1145,87 +1145,74 @@ pub fn get_progress_bar(length: u64) -> Result<ProgressBar> {
progress_bar.enable_steady_tick(Duration::from_millis(100));
Ok(progress_bar)
}

pub async fn get_environment_details(
environment_name: &str,
s3_repository: &S3Repository,
) -> Result<EnvironmentDetails> {
let temp_file = tempfile::NamedTempFile::new()?;
match s3_repository
.download_object("sn-environment-type", environment_name, temp_file.path())
.await
{
Ok(_) => {
debug!("Downloaded the environment details file for {environment_name} from S3");
let content = std::fs::read_to_string(temp_file.path())?;
match serde_json::from_str(&content) {
Ok(environment_details) => Ok(environment_details),
Err(_) => {
let lines: Vec<&str> = content.lines().collect();

let environment_type = if lines.is_empty() {
None
} else {
Some(lines[0].trim())
};

let deployment_type = lines.get(1).map(|s| s.trim());

let environment_type = match environment_type.and_then(|s| s.parse().ok()) {
Some(e) => {
debug!("Parsed the environment type from the S3 file");
e
}
None => {
debug!("Could not parse the environment type from the S3 file");
debug!(
"Falling back to using the environment name ({environment_name})"
);
if environment_name.starts_with("DEV") {
debug!("Using Development as the environment type");
EnvironmentType::Development
} else if environment_name.starts_with("STG") {
debug!("Using Staging as the environment type");
EnvironmentType::Staging
} else if environment_name.starts_with("PROD") {
debug!("Using Production as the environment type");
EnvironmentType::Production
} else {
return Err(Error::EnvironmentNameFromStringError(
environment_name.to_string(),
));
}
let max_retries = 3;
let mut retries = 0;
let env_details = loop {
debug!("Downloading the environment details file for {environment_name} from S3");
match s3_repository
.download_object("sn-environment-type", environment_name, temp_file.path())
.await
{
Ok(_) => {
debug!("Downloaded the environment details file for {environment_name} from S3");
let content = match std::fs::read_to_string(temp_file.path()) {
Ok(content) => content,
Err(err) => {
log::error!("Could not read the environment details file: {err:?}");
if retries < max_retries {
debug!("Retrying to read the environment details file");
retries += 1;
continue;
} else {
return Err(Error::EnvironmentDetailsNotFound(
environment_name.to_string(),
));
}
};

let deployment_type = match deployment_type.and_then(|s| s.parse().ok()) {
Some(d) => d,
None => {
debug!("Could not parse the deployment type from the S3 file");
debug!("Using New as the default");
DeploymentType::New
}
};
trace!("Content of the environment details file: {}", content);

match serde_json::from_str(&content) {
Ok(environment_details) => break environment_details,
Err(err) => {
log::error!("Could not parse the environment details file: {err:?}");
if retries < max_retries {
debug!("Retrying to parse the environment details file");
retries += 1;
continue;
} else {
return Err(Error::EnvironmentDetailsNotFound(
environment_name.to_string(),
));
}
};

Ok(EnvironmentDetails {
environment_type,
deployment_type,
evm_network: EvmNetwork::ArbitrumOne,
rewards_address: "".to_string(),
evm_testnet_data: None,
})
}
}
}
Err(err) => {
log::error!(
"Could not download the environment details file for {environment_name} from S3: {err:?}"
);
if retries < max_retries {
retries += 1;
continue;
} else {
return Err(Error::EnvironmentDetailsNotFound(
environment_name.to_string(),
));
}
}
}
Err(err) => {
log::error!(
"Could not download the environment details file for {environment_name} from S3: {err:?}"
);
Err(Error::EnvironmentDetailsNotFound(
environment_name.to_string(),
))
}
}
};

debug!("Fetched environment details: {env_details:?}");

Ok(env_details)
}

pub async fn write_environment_details(
Expand Down
12 changes: 9 additions & 3 deletions src/upscale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ impl TestnetDeployer {
};
debug!("Retrieved initial peer {initial_multiaddr}");

let evm_testnet_data = options
.current_inventory
.environment_details
.evm_testnet_data
.clone();

let should_provision_private_nodes = desired_private_node_vm_count > 0;
let mut n = 1;
let mut total = if is_bootstrap_deploy { 3 } else { 4 };
Expand All @@ -262,7 +268,7 @@ impl TestnetDeployer {
&provision_options,
&initial_multiaddr,
NodeType::Bootstrap,
None,
evm_testnet_data.clone(),
) {
Ok(()) => {
println!("Provisioned bootstrap nodes");
Expand All @@ -285,7 +291,7 @@ impl TestnetDeployer {
&provision_options,
&initial_multiaddr,
NodeType::Normal,
None,
evm_testnet_data.clone(),
) {
Ok(()) => {
println!("Provisioned normal nodes");
Expand Down Expand Up @@ -352,7 +358,7 @@ impl TestnetDeployer {
self.ansible_provisioner
.print_ansible_run_banner(n, total, "Provision Uploaders");
self.ansible_provisioner
.provision_uploaders(&provision_options, &initial_multiaddr, None)
.provision_uploaders(&provision_options, &initial_multiaddr, evm_testnet_data)
.await
.map_err(|err| {
println!("Failed to provision uploaders {err:?}");
Expand Down

0 comments on commit e4565e0

Please sign in to comment.