sn_testnet_deploy/
bootstrap.rs

1// Copyright (c) 2023, MaidSafe.
2// All rights reserved.
3//
4// This SAFE Network Software is licensed under the BSD-3-Clause license.
5// Please see the LICENSE file for more details.
6
7use std::{path::PathBuf, time::Duration};
8
9use crate::{
10    ansible::provisioning::{PrivateNodeProvisionInventory, ProvisionOptions},
11    error::Result,
12    write_environment_details, BinaryOption, DeploymentType, EnvironmentDetails, EnvironmentType,
13    EvmDetails, EvmNetwork, InfraRunOptions, LogFormat, NodeType, TestnetDeployer,
14};
15use colored::Colorize;
16use log::error;
17
18#[derive(Clone)]
19pub struct BootstrapOptions {
20    pub binary_option: BinaryOption,
21    pub chunk_size: Option<u64>,
22    pub environment_type: EnvironmentType,
23    pub evm_data_payments_address: Option<String>,
24    pub evm_network: EvmNetwork,
25    pub evm_payment_token_address: Option<String>,
26    pub evm_rpc_url: Option<String>,
27    pub full_cone_private_node_count: u16,
28    pub full_cone_private_node_vm_count: Option<u16>,
29    pub full_cone_private_node_volume_size: Option<u16>,
30    pub interval: Duration,
31    pub log_format: Option<LogFormat>,
32    pub max_archived_log_files: u16,
33    pub max_log_files: u16,
34    pub name: String,
35    pub network_contacts_url: Option<String>,
36    pub network_id: u8,
37    pub node_count: u16,
38    pub node_env_variables: Option<Vec<(String, String)>>,
39    pub node_vm_count: Option<u16>,
40    pub node_vm_size: Option<String>,
41    pub node_volume_size: Option<u16>,
42    pub output_inventory_dir_path: PathBuf,
43    pub peer: Option<String>,
44    pub rewards_address: String,
45    pub symmetric_private_node_count: u16,
46    pub symmetric_private_node_vm_count: Option<u16>,
47    pub symmetric_private_node_volume_size: Option<u16>,
48}
49
50impl TestnetDeployer {
51    pub async fn bootstrap(&self, options: &BootstrapOptions) -> Result<()> {
52        let build_custom_binaries = {
53            match &options.binary_option {
54                BinaryOption::BuildFromSource { .. } => true,
55                BinaryOption::Versioned { .. } => false,
56            }
57        };
58
59        write_environment_details(
60            &self.s3_repository,
61            &options.name,
62            &EnvironmentDetails {
63                deployment_type: DeploymentType::Bootstrap,
64                environment_type: options.environment_type.clone(),
65                evm_details: EvmDetails {
66                    network: options.evm_network.clone(),
67                    data_payments_address: options.evm_data_payments_address.clone(),
68                    payment_token_address: options.evm_payment_token_address.clone(),
69                    rpc_url: options.evm_rpc_url.clone(),
70                },
71                funding_wallet_address: None,
72                network_id: Some(options.network_id),
73                rewards_address: Some(options.rewards_address.clone()),
74            },
75        )
76        .await?;
77
78        self.create_or_update_infra(&InfraRunOptions {
79            enable_build_vm: build_custom_binaries,
80            evm_node_count: Some(0),
81            evm_node_vm_size: None,
82            evm_node_image_id: None,
83            full_cone_nat_gateway_vm_size: None, // We can take the value from tfvars for bootstrap deployments.
84            full_cone_private_node_vm_count: options.full_cone_private_node_vm_count,
85            full_cone_private_node_volume_size: options.full_cone_private_node_volume_size,
86            genesis_vm_count: Some(0),
87            genesis_node_volume_size: None,
88            name: options.name.clone(),
89            nat_gateway_image_id: None,
90            node_image_id: None,
91            node_vm_count: options.node_vm_count,
92            node_vm_size: options.node_vm_size.clone(),
93            node_volume_size: options.node_volume_size,
94            peer_cache_image_id: None,
95            peer_cache_node_vm_count: Some(0),
96            peer_cache_node_vm_size: None,
97            peer_cache_node_volume_size: None,
98            symmetric_nat_gateway_vm_size: None, // We can take the value from tfvars for bootstrap deployments.
99            symmetric_private_node_vm_count: options.symmetric_private_node_vm_count,
100            symmetric_private_node_volume_size: options.symmetric_private_node_volume_size,
101            tfvars_filename: Some(
102                options
103                    .environment_type
104                    .get_tfvars_filename(&options.name)
105                    .to_string(),
106            ),
107            uploader_image_id: None,
108            uploader_vm_count: Some(0),
109            uploader_vm_size: None,
110        })
111        .map_err(|err| {
112            println!("Failed to create infra {err:?}");
113            err
114        })?;
115
116        let mut provision_options = ProvisionOptions::from(options.clone());
117        if build_custom_binaries {
118            self.ansible_provisioner
119                .print_ansible_run_banner("Build Custom Binaries");
120            self.ansible_provisioner
121                .build_safe_network_binaries(&provision_options, None)
122                .map_err(|err| {
123                    println!("Failed to build safe network binaries {err:?}");
124                    err
125                })?;
126        }
127
128        let mut failed_to_provision = false;
129
130        self.ansible_provisioner
131            .print_ansible_run_banner("Provision Normal Nodes");
132        match self.ansible_provisioner.provision_nodes(
133            &provision_options,
134            options.peer.clone(),
135            options.network_contacts_url.clone(),
136            NodeType::Generic,
137        ) {
138            Ok(()) => {
139                println!("Provisioned normal nodes");
140            }
141            Err(e) => {
142                println!("Failed to provision normal nodes: {e:?}");
143                failed_to_provision = true;
144            }
145        }
146
147        let private_node_inventory = PrivateNodeProvisionInventory::new(
148            &self.ansible_provisioner,
149            options.full_cone_private_node_vm_count,
150            options.symmetric_private_node_vm_count,
151        )?;
152
153        if private_node_inventory.should_provision_full_cone_private_nodes() {
154            match self.ansible_provisioner.provision_full_cone(
155                &provision_options,
156                options.peer.clone(),
157                options.network_contacts_url.clone(),
158                private_node_inventory.clone(),
159                None,
160            ) {
161                Ok(()) => {
162                    println!("Provisioned Full Cone nodes and Gateway");
163                }
164                Err(err) => {
165                    error!("Failed to provision Full Cone nodes and Gateway: {err}");
166                    failed_to_provision = true;
167                }
168            }
169        }
170
171        if private_node_inventory.should_provision_symmetric_private_nodes() {
172            self.ansible_provisioner
173                .print_ansible_run_banner("Provision Symmetric NAT Gateway");
174            self.ansible_provisioner
175                .provision_symmetric_nat_gateway(&provision_options, &private_node_inventory)
176                .map_err(|err| {
177                    println!("Failed to provision Symmetric NAT gateway {err:?}");
178                    err
179                })?;
180
181            self.ansible_provisioner
182                .print_ansible_run_banner("Provision Symmetric Private Nodes");
183            match self.ansible_provisioner.provision_symmetric_private_nodes(
184                &mut provision_options,
185                options.peer.clone(),
186                options.network_contacts_url.clone(),
187                &private_node_inventory,
188            ) {
189                Ok(()) => {
190                    println!("Provisioned Symmetric private nodes");
191                }
192                Err(err) => {
193                    error!("Failed to provision Symmetric Private nodes: {err}");
194                    failed_to_provision = true;
195                }
196            }
197        }
198
199        if failed_to_provision {
200            println!("{}", "WARNING!".yellow());
201            println!("Some nodes failed to provision without error.");
202            println!("This usually means a small number of nodes failed to start on a few VMs.");
203            println!("However, most of the time the deployment will still be usable.");
204            println!("See the output from Ansible to determine which VMs had failures.");
205        }
206
207        Ok(())
208    }
209}