Skip to content

Commit

Permalink
feat: introduce node type to start,stop nodes and telegraph
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin committed Nov 5, 2024
1 parent c99557d commit 1a4fc97
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 22 deletions.
60 changes: 55 additions & 5 deletions src/ansible/provisioning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ impl AnsibleProvisioner {
Some(extra_vars::build_node_extra_vars_doc(
&self.cloud_provider.to_string(),
options,
NodeType::Bootstrap,
NodeType::Genesis,
None,
1,
options.evm_network.clone(),
Expand Down Expand Up @@ -344,16 +344,18 @@ impl AnsibleProvisioner {
evm_testnet_data: Option<EvmCustomTestnetData>,
) -> Result<()> {
let start = Instant::now();
let (inventory_type, node_count) = match node_type {
let (inventory_type, node_count) = match &node_type {
NodeType::Bootstrap => (
AnsibleInventoryType::BootstrapNodes,
node_type.to_ansible_inventory_type(),
options.bootstrap_node_count,
),
NodeType::Normal => (AnsibleInventoryType::Nodes, options.node_count),
NodeType::Normal => (node_type.to_ansible_inventory_type(), options.node_count),
NodeType::Private => (
AnsibleInventoryType::PrivateNodes,
node_type.to_ansible_inventory_type(),
options.private_node_count,
),
// use provision_genesis_node fn
NodeType::Genesis => return Err(Error::InvalidNodeType(node_type)),
};

// For a new deployment, it's quite probable that SSH is available, because this part occurs
Expand Down Expand Up @@ -498,11 +500,22 @@ impl AnsibleProvisioner {
&self,
environment_name: &str,
interval: Duration,
node_type: Option<NodeType>,
custom_inventory: Option<Vec<VirtualMachine>>,
) -> Result<()> {
let mut extra_vars = ExtraVarsDocBuilder::default();
extra_vars.add_variable("interval", &interval.as_millis().to_string());

if let Some(node_type) = node_type {
println!("Running the start nodes playbook for {node_type:?} nodes");
self.ansible_runner.run_playbook(
AnsiblePlaybook::StartNodes,
node_type.to_ansible_inventory_type(),
Some(extra_vars.build()),
)?;
return Ok(());
}

if let Some(custom_inventory) = custom_inventory {
println!("Running the start nodes playbook with a custom inventory");
generate_custom_environment_inventory(
Expand All @@ -518,6 +531,7 @@ impl AnsibleProvisioner {
return Ok(());
}

println!("Running the start nodes playbook for all node types");
for node_inv_type in AnsibleInventoryType::iter_node_type() {
self.ansible_runner.run_playbook(
AnsiblePlaybook::StartNodes,
Expand All @@ -539,8 +553,19 @@ impl AnsibleProvisioner {
pub fn start_telegraf(
&self,
environment_name: &str,
node_type: Option<NodeType>,
custom_inventory: Option<Vec<VirtualMachine>>,
) -> Result<()> {
if let Some(node_type) = node_type {
println!("Running the start telegraf playbook for {node_type:?} nodes");
self.ansible_runner.run_playbook(
AnsiblePlaybook::StartTelegraf,
node_type.to_ansible_inventory_type(),
None,
)?;
return Ok(());
}

if let Some(custom_inventory) = custom_inventory {
println!("Running the start telegraf playbook with a custom inventory");
generate_custom_environment_inventory(
Expand All @@ -556,6 +581,7 @@ impl AnsibleProvisioner {
return Ok(());
}

println!("Running the start telegraf playbook for all node types");
for node_inv_type in AnsibleInventoryType::iter_node_type() {
self.ansible_runner.run_playbook(
AnsiblePlaybook::StartTelegraf,
Expand All @@ -571,11 +597,22 @@ impl AnsibleProvisioner {
&self,
environment_name: &str,
interval: Duration,
node_type: Option<NodeType>,
custom_inventory: Option<Vec<VirtualMachine>>,
) -> Result<()> {
let mut extra_vars = ExtraVarsDocBuilder::default();
extra_vars.add_variable("interval", &interval.as_millis().to_string());

if let Some(node_type) = node_type {
println!("Running the stop nodes playbook for {node_type:?} nodes");
self.ansible_runner.run_playbook(
AnsiblePlaybook::StopNodes,
node_type.to_ansible_inventory_type(),
Some(extra_vars.build()),
)?;
return Ok(());
}

if let Some(custom_inventory) = custom_inventory {
println!("Running the stop nodes playbook with a custom inventory");
generate_custom_environment_inventory(
Expand All @@ -591,6 +628,7 @@ impl AnsibleProvisioner {
return Ok(());
}

println!("Running the stop nodes playbook for all node types");
for node_inv_type in AnsibleInventoryType::iter_node_type() {
self.ansible_runner.run_playbook(
AnsiblePlaybook::StopNodes,
Expand All @@ -605,8 +643,19 @@ impl AnsibleProvisioner {
pub fn stop_telegraf(
&self,
environment_name: &str,
node_type: Option<NodeType>,
custom_inventory: Option<Vec<VirtualMachine>>,
) -> Result<()> {
if let Some(node_type) = node_type {
println!("Running the stop telegraf playbook for {node_type:?} nodes");
self.ansible_runner.run_playbook(
AnsiblePlaybook::StopTelegraf,
node_type.to_ansible_inventory_type(),
None,
)?;
return Ok(());
}

if let Some(custom_inventory) = custom_inventory {
println!("Running the stop telegraf playbook with a custom inventory");
generate_custom_environment_inventory(
Expand All @@ -622,6 +671,7 @@ impl AnsibleProvisioner {
return Ok(());
}

println!("Running the stop telegraf playbook for all node types");
for node_inv_type in AnsibleInventoryType::iter_node_type() {
self.ansible_runner
.run_playbook(AnsiblePlaybook::StopTelegraf, node_inv_type, None)?;
Expand Down
4 changes: 3 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// This SAFE Network Software is licensed under the BSD-3-Clause license.
// Please see the LICENSE file for more details.

use crate::ansible::inventory::AnsibleInventoryType;
use crate::{ansible::inventory::AnsibleInventoryType, NodeType};
use evmlib::contract::network_token;
use thiserror::Error;
use tokio::task::JoinError;
Expand Down Expand Up @@ -69,6 +69,8 @@ pub enum Error {
GetS3ObjectError(String, String),
#[error(transparent)]
InquireError(#[from] inquire::InquireError),
#[error("The node type '{0:?}' is not supported")]
InvalidNodeType(NodeType),
#[error(
"The '{0}' deployment type for the environment is not supported for upscaling uploaders"
)]
Expand Down
72 changes: 61 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,47 @@ impl std::str::FromStr for DeploymentType {
}
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum NodeType {
Bootstrap,
Genesis,
Normal,
Private,
}

impl std::str::FromStr for NodeType {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"bootstrap" => Ok(NodeType::Bootstrap),
"genesis" => Ok(NodeType::Genesis),
"normal" => Ok(NodeType::Normal),
"private" => Ok(NodeType::Private),
_ => Err(format!("Invalid node type: {}", s)),
}
}
}

impl NodeType {
pub fn telegraph_role(&self) -> &'static str {
match self {
NodeType::Bootstrap => "BOOTSTRAP_NODE",
// Genesis node should be marked as a bootstrap node for telegraf
NodeType::Genesis => "BOOTSTRAP_NODE",
NodeType::Normal => "GENERIC_NODE",
NodeType::Private => "NAT_RANDOMIZED_NODE",
}
}

pub fn to_ansible_inventory_type(&self) -> AnsibleInventoryType {
match self {
NodeType::Bootstrap => AnsibleInventoryType::BootstrapNodes,
NodeType::Genesis => AnsibleInventoryType::Genesis,
NodeType::Normal => AnsibleInventoryType::Nodes,
NodeType::Private => AnsibleInventoryType::PrivateNodes,
}
}
}

#[derive(Clone, Debug, Default, Eq, Serialize, Deserialize, PartialEq)]
Expand Down Expand Up @@ -635,10 +661,15 @@ impl TestnetDeployer {
pub fn start(
&self,
interval: Duration,
node_type: Option<NodeType>,
custom_inventory: Option<Vec<VirtualMachine>>,
) -> Result<()> {
self.ansible_provisioner
.start_nodes(&self.environment_name, interval, custom_inventory)?;
self.ansible_provisioner.start_nodes(
&self.environment_name,
interval,
node_type,
custom_inventory,
)?;
Ok(())
}

Expand Down Expand Up @@ -677,25 +708,44 @@ impl TestnetDeployer {
Ok(())
}

pub fn start_telegraf(&self, custom_inventory: Option<Vec<VirtualMachine>>) -> Result<()> {
self.ansible_provisioner
.start_telegraf(&self.environment_name, custom_inventory)?;
pub fn start_telegraf(
&self,
node_type: Option<NodeType>,
custom_inventory: Option<Vec<VirtualMachine>>,
) -> Result<()> {
self.ansible_provisioner.start_telegraf(
&self.environment_name,
node_type,
custom_inventory,
)?;
Ok(())
}

pub fn stop(
&self,
interval: Duration,
node_type: Option<NodeType>,
custom_inventory: Option<Vec<VirtualMachine>>,
) -> Result<()> {
self.ansible_provisioner
.stop_nodes(&self.environment_name, interval, custom_inventory)?;
self.ansible_provisioner.stop_nodes(
&self.environment_name,
interval,
node_type,
custom_inventory,
)?;
Ok(())
}

pub fn stop_telegraf(&self, custom_inventory: Option<Vec<VirtualMachine>>) -> Result<()> {
self.ansible_provisioner
.stop_telegraf(&self.environment_name, custom_inventory)?;
pub fn stop_telegraf(
&self,
node_type: Option<NodeType>,
custom_inventory: Option<Vec<VirtualMachine>>,
) -> Result<()> {
self.ansible_provisioner.stop_telegraf(
&self.environment_name,
node_type,
custom_inventory,
)?;
Ok(())
}

Expand Down
Loading

0 comments on commit 1a4fc97

Please sign in to comment.