sn_testnet_deploy/
inventory.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 crate::{
8    ansible::{
9        inventory::{
10            generate_environment_inventory,
11            generate_full_cone_private_node_static_environment_inventory,
12            generate_symmetric_private_node_static_environment_inventory, AnsibleInventoryType,
13        },
14        provisioning::{AnsibleProvisioner, PrivateNodeProvisionInventory},
15        AnsibleRunner,
16    },
17    clients::ClientsDeployer,
18    get_bootstrap_cache_url, get_environment_details, get_genesis_multiaddr,
19    s3::S3Repository,
20    ssh::SshClient,
21    terraform::TerraformRunner,
22    BinaryOption, CloudProvider, DeploymentType, EnvironmentDetails, EnvironmentType, Error,
23    EvmDetails, TestnetDeployer,
24};
25use alloy::hex::ToHexExt;
26use ant_service_management::{NodeRegistry, ServiceStatus};
27use color_eyre::{eyre::eyre, Result};
28use log::debug;
29use rand::seq::{IteratorRandom, SliceRandom};
30use semver::Version;
31use serde::{Deserialize, Serialize};
32use std::{
33    collections::{HashMap, HashSet},
34    convert::From,
35    fs::File,
36    io::Write,
37    net::{IpAddr, SocketAddr},
38    path::PathBuf,
39};
40
41const DEFAULT_CONTACTS_COUNT: usize = 100;
42const UNAVAILABLE_NODE: &str = "-";
43const TESTNET_BUCKET_NAME: &str = "sn-testnet";
44
45pub struct DeploymentInventoryService {
46    pub ansible_runner: AnsibleRunner,
47    // It may seem strange to have both the runner and the provisioner, because the provisioner is
48    // a wrapper around the runner, but it's for the purpose of sharing some code. More things
49    // could go into the provisioner later, which may eliminate the need to have the runner.
50    pub ansible_provisioner: AnsibleProvisioner,
51    pub cloud_provider: CloudProvider,
52    pub inventory_file_path: PathBuf,
53    pub s3_repository: S3Repository,
54    pub ssh_client: SshClient,
55    pub terraform_runner: TerraformRunner,
56    pub working_directory_path: PathBuf,
57}
58
59impl From<&TestnetDeployer> for DeploymentInventoryService {
60    fn from(item: &TestnetDeployer) -> Self {
61        let provider = match item.cloud_provider {
62            CloudProvider::Aws => "aws",
63            CloudProvider::DigitalOcean => "digital_ocean",
64        };
65        DeploymentInventoryService {
66            ansible_runner: item.ansible_provisioner.ansible_runner.clone(),
67            ansible_provisioner: item.ansible_provisioner.clone(),
68            cloud_provider: item.cloud_provider,
69            inventory_file_path: item
70                .working_directory_path
71                .join("ansible")
72                .join("inventory")
73                .join(format!("dev_inventory_{}.yml", provider)),
74            s3_repository: item.s3_repository.clone(),
75            ssh_client: item.ssh_client.clone(),
76            terraform_runner: item.terraform_runner.clone(),
77            working_directory_path: item.working_directory_path.clone(),
78        }
79    }
80}
81
82impl From<&ClientsDeployer> for DeploymentInventoryService {
83    fn from(item: &ClientsDeployer) -> Self {
84        let provider = match item.cloud_provider {
85            CloudProvider::Aws => "aws",
86            CloudProvider::DigitalOcean => "digital_ocean",
87        };
88        DeploymentInventoryService {
89            ansible_runner: item.ansible_provisioner.ansible_runner.clone(),
90            ansible_provisioner: item.ansible_provisioner.clone(),
91            cloud_provider: item.cloud_provider,
92            inventory_file_path: item
93                .working_directory_path
94                .join("ansible")
95                .join("inventory")
96                .join(format!("dev_inventory_{}.yml", provider)),
97            s3_repository: item.s3_repository.clone(),
98            ssh_client: item.ssh_client.clone(),
99            terraform_runner: item.terraform_runner.clone(),
100            working_directory_path: item.working_directory_path.clone(),
101        }
102    }
103}
104
105impl DeploymentInventoryService {
106    /// Generate or retrieve the inventory for the deployment.
107    ///
108    /// If we're creating a new environment and there is no inventory yet, a empty inventory will
109    /// be returned; otherwise the inventory will represent what is deployed currently.
110    ///
111    /// The `force` flag is used when the `deploy` command runs, to make sure that a new inventory
112    /// is generated, because it's possible that an old one with the same environment name has been
113    /// cached.
114    ///
115    /// The binary option will only be present on the first generation of the inventory, when the
116    /// testnet is initially deployed. On any subsequent runs, we don't have access to the initial
117    /// launch arguments. This means any branch specification is lost. In this case, we'll just
118    /// retrieve the version numbers from the genesis node in the node registry. Most of the time
119    /// it is the version numbers that will be of interest.
120    pub async fn generate_or_retrieve_inventory(
121        &self,
122        name: &str,
123        force: bool,
124        binary_option: Option<BinaryOption>,
125    ) -> Result<DeploymentInventory> {
126        println!("======================================");
127        println!("  Generating or Retrieving Inventory  ");
128        println!("======================================");
129        let inventory_path = get_data_directory()?.join(format!("{name}-inventory.json"));
130        if inventory_path.exists() && !force {
131            let inventory = DeploymentInventory::read(&inventory_path)?;
132            return Ok(inventory);
133        }
134
135        // This allows for the inventory to be generated without a Terraform workspace to be
136        // initialised, which is the case in the workflow for printing an inventory.
137        if !force {
138            let environments = self.terraform_runner.workspace_list()?;
139            if !environments.contains(&name.to_string()) {
140                return Err(eyre!("The '{}' environment does not exist", name));
141            }
142        }
143
144        // For new environments, whether it's a new or bootstrap deploy, the inventory files need
145        // to be generated for the Ansible run to work correctly.
146        //
147        // It is an idempotent operation; the files won't be generated if they already exist.
148        let output_inventory_dir_path = self
149            .working_directory_path
150            .join("ansible")
151            .join("inventory");
152        generate_environment_inventory(
153            name,
154            &self.inventory_file_path,
155            &output_inventory_dir_path,
156        )?;
157
158        let environment_details = match get_environment_details(name, &self.s3_repository).await {
159            Ok(details) => details,
160            Err(Error::EnvironmentDetailsNotFound(_)) => {
161                println!("Environment details not found: treating this as a new deployment");
162                return Ok(DeploymentInventory::empty(
163                    name,
164                    binary_option.ok_or_else(|| {
165                        eyre!("For a new deployment the binary option must be set")
166                    })?,
167                ));
168            }
169            Err(e) => return Err(e.into()),
170        };
171
172        let ansible_runner = self.ansible_runner.clone();
173        let genesis_handle = std::thread::spawn(move || {
174            ansible_runner.get_inventory(AnsibleInventoryType::Genesis, false)
175        });
176        let ansible_runner = self.ansible_runner.clone();
177        let build_handle = std::thread::spawn(move || {
178            ansible_runner.get_inventory(AnsibleInventoryType::Build, false)
179        });
180        let ansible_runner = self.ansible_runner.clone();
181        let full_cone_nat_gateway_handle = std::thread::spawn(move || {
182            ansible_runner.get_inventory(AnsibleInventoryType::FullConeNatGateway, false)
183        });
184        let ansible_runner = self.ansible_runner.clone();
185        let full_cone_private_node_handle = std::thread::spawn(move || {
186            ansible_runner.get_inventory(AnsibleInventoryType::FullConePrivateNodes, false)
187        });
188        let ansible_runner = self.ansible_runner.clone();
189        let symmetric_nat_gateway_handle = std::thread::spawn(move || {
190            ansible_runner.get_inventory(AnsibleInventoryType::SymmetricNatGateway, false)
191        });
192        let ansible_runner = self.ansible_runner.clone();
193        let symmetric_private_node_handle = std::thread::spawn(move || {
194            ansible_runner.get_inventory(AnsibleInventoryType::SymmetricPrivateNodes, false)
195        });
196        let ansible_runner = self.ansible_runner.clone();
197        let generic_node_handle = std::thread::spawn(move || {
198            ansible_runner.get_inventory(AnsibleInventoryType::Nodes, false)
199        });
200        let ansible_runner = self.ansible_runner.clone();
201        let peer_cache_node_handle = std::thread::spawn(move || {
202            ansible_runner.get_inventory(AnsibleInventoryType::PeerCacheNodes, false)
203        });
204        let ansible_runner = self.ansible_runner.clone();
205        let client_handle = std::thread::spawn(move || {
206            ansible_runner.get_inventory(AnsibleInventoryType::Clients, true)
207        });
208
209        let genesis_vm = genesis_handle.join().expect("Thread panicked")?;
210        let mut misc_vms = Vec::new();
211        misc_vms.extend(build_handle.join().expect("Thread panicked")?);
212        let full_cone_nat_gateway_vms = full_cone_nat_gateway_handle
213            .join()
214            .expect("Thread panicked")?;
215        let full_cone_private_node_vms = full_cone_private_node_handle
216            .join()
217            .expect("Thread panicked")?;
218        let symmetric_nat_gateway_vms = symmetric_nat_gateway_handle
219            .join()
220            .expect("Thread panicked")?;
221        let symmetric_private_node_vms = symmetric_private_node_handle
222            .join()
223            .expect("Thread panicked")?;
224        let generic_node_vms = generic_node_handle.join().expect("Thread panicked")?;
225        let peer_cache_node_vms = peer_cache_node_handle.join().expect("Thread panicked")?;
226        let client_vms = if !client_handle.join().expect("Thread panicked")?.is_empty()
227            && environment_details.deployment_type != DeploymentType::Bootstrap
228        {
229            let client_and_sks = self.ansible_provisioner.get_client_secret_keys()?;
230            client_and_sks
231                .iter()
232                .map(|(vm, sks)| ClientVirtualMachine {
233                    vm: vm.clone(),
234                    wallet_public_key: sks
235                        .iter()
236                        .enumerate()
237                        .map(|(user, sk)| (format!("safe{}", user + 1), sk.address().encode_hex()))
238                        .collect(),
239                })
240                .collect()
241        } else {
242            Vec::new()
243        };
244
245        debug!("full_cone_private_node_vms: {full_cone_private_node_vms:?}");
246        debug!("full_cone_nat_gateway_vms: {full_cone_nat_gateway_vms:?}");
247        debug!("symmetric_private_node_vms: {symmetric_private_node_vms:?}");
248        debug!("symmetric_nat_gateway_vms: {symmetric_nat_gateway_vms:?}");
249
250        // Create static inventory for private nodes. Will be used during ansible-playbook run.
251        generate_full_cone_private_node_static_environment_inventory(
252            name,
253            &output_inventory_dir_path,
254            &full_cone_private_node_vms,
255            &full_cone_nat_gateway_vms,
256            &self.ssh_client.private_key_path,
257        )?;
258        generate_symmetric_private_node_static_environment_inventory(
259            name,
260            &output_inventory_dir_path,
261            &symmetric_private_node_vms,
262            &symmetric_nat_gateway_vms,
263            &self.ssh_client.private_key_path,
264        )?;
265
266        // Set up the SSH client to route through the NAT gateway if it exists. This updates all the client clones.
267        if !symmetric_nat_gateway_vms.is_empty() {
268            self.ssh_client.set_symmetric_nat_routed_vms(
269                &symmetric_private_node_vms,
270                &symmetric_nat_gateway_vms,
271            )?;
272        }
273        if !full_cone_nat_gateway_vms.is_empty() {
274            self.ssh_client.set_full_cone_nat_routed_vms(
275                &full_cone_private_node_vms,
276                &full_cone_nat_gateway_vms,
277            )?;
278        }
279
280        println!("Retrieving node registries from all VMs...");
281        let ansible_provisioner = self.ansible_provisioner.clone();
282        let peer_cache_node_registries_handle = std::thread::spawn(move || {
283            ansible_provisioner.get_node_registries(&AnsibleInventoryType::PeerCacheNodes)
284        });
285        let ansible_provisioner = self.ansible_provisioner.clone();
286        let generic_node_registries_handle = std::thread::spawn(move || {
287            ansible_provisioner.get_node_registries(&AnsibleInventoryType::Nodes)
288        });
289        let ansible_provisioner = self.ansible_provisioner.clone();
290        let symmetric_private_node_registries_handle = std::thread::spawn(move || {
291            ansible_provisioner.get_node_registries(&AnsibleInventoryType::SymmetricPrivateNodes)
292        });
293        let ansible_provisioner = self.ansible_provisioner.clone();
294        let full_cone_private_node_registries_handle = std::thread::spawn(move || {
295            ansible_provisioner.get_node_registries(&AnsibleInventoryType::FullConePrivateNodes)
296        });
297        let ansible_provisioner = self.ansible_provisioner.clone();
298        let genesis_node_registry_handle = std::thread::spawn(move || {
299            ansible_provisioner.get_node_registries(&AnsibleInventoryType::Genesis)
300        });
301
302        let peer_cache_node_registries = peer_cache_node_registries_handle
303            .join()
304            .expect("Thread panicked")?;
305        let generic_node_registries = generic_node_registries_handle
306            .join()
307            .expect("Thread panicked")?;
308        let symmetric_private_node_registries = symmetric_private_node_registries_handle
309            .join()
310            .expect("Thread panicked")?;
311        let full_cone_private_node_registries = full_cone_private_node_registries_handle
312            .join()
313            .expect("Thread panicked")?;
314        let genesis_node_registry = genesis_node_registry_handle
315            .join()
316            .expect("Thread panicked")?;
317
318        let peer_cache_node_vms =
319            NodeVirtualMachine::from_list(&peer_cache_node_vms, &peer_cache_node_registries);
320
321        let generic_node_vms =
322            NodeVirtualMachine::from_list(&generic_node_vms, &generic_node_registries);
323
324        let symmetric_private_node_vms = NodeVirtualMachine::from_list(
325            &symmetric_private_node_vms,
326            &symmetric_private_node_registries,
327        );
328        debug!("symmetric_private_node_vms after conversion: {symmetric_private_node_vms:?}");
329
330        debug!("full_cone_private_node_vms: {full_cone_private_node_vms:?}");
331        let full_cone_private_node_gateway_vm_map =
332            PrivateNodeProvisionInventory::match_private_node_vm_and_gateway_vm(
333                &full_cone_private_node_vms,
334                &full_cone_nat_gateway_vms,
335            )?;
336        debug!("full_cone_private_node_gateway_vm_map: {full_cone_private_node_gateway_vm_map:?}");
337        let full_cone_private_node_vms = NodeVirtualMachine::from_list(
338            &full_cone_private_node_vms,
339            &full_cone_private_node_registries,
340        );
341        debug!("full_cone_private_node_vms after conversion: {full_cone_private_node_vms:?}");
342
343        let genesis_vm = NodeVirtualMachine::from_list(&genesis_vm, &genesis_node_registry);
344        let genesis_vm = if !genesis_vm.is_empty() {
345            Some(genesis_vm[0].clone())
346        } else {
347            None
348        };
349
350        let mut failed_node_registry_vms = Vec::new();
351        failed_node_registry_vms.extend(peer_cache_node_registries.failed_vms);
352        failed_node_registry_vms.extend(generic_node_registries.failed_vms);
353        failed_node_registry_vms.extend(full_cone_private_node_registries.failed_vms);
354        failed_node_registry_vms.extend(symmetric_private_node_registries.failed_vms);
355        failed_node_registry_vms.extend(genesis_node_registry.failed_vms);
356
357        let binary_option = if let Some(binary_option) = binary_option {
358            binary_option
359        } else {
360            let (antnode_version, antctl_version) = {
361                let mut random_vm = None;
362                if !generic_node_vms.is_empty() {
363                    random_vm = generic_node_vms.first().cloned();
364                } else if !peer_cache_node_vms.is_empty() {
365                    random_vm = peer_cache_node_vms.first().cloned();
366                } else if genesis_vm.is_some() {
367                    random_vm = genesis_vm.clone()
368                };
369
370                let Some(random_vm) = random_vm else {
371                    return Err(eyre!("Unable to obtain a VM to retrieve versions"));
372                };
373
374                // It's reasonable to make the assumption that one antnode service is running.
375                let antnode_version = self.get_bin_version(
376                    &random_vm.vm,
377                    "/mnt/antnode-storage/data/antnode1/antnode --version",
378                    "Autonomi Node v",
379                )?;
380                let antctl_version = self.get_bin_version(
381                    &random_vm.vm,
382                    "antctl --version",
383                    "Autonomi Node Manager v",
384                )?;
385                (Some(antnode_version), Some(antctl_version))
386            };
387
388            let ant_version = if !client_vms.is_empty()
389                && environment_details.deployment_type != DeploymentType::Bootstrap
390            {
391                let random_client_vm = client_vms
392                    .choose(&mut rand::thread_rng())
393                    .ok_or_else(|| eyre!("No Client VMs available to retrieve ant version"))?;
394                self.get_bin_version(&random_client_vm.vm, "ant --version", "Autonomi Client v")
395                    .ok()
396            } else {
397                None
398            };
399
400            println!("Retrieved binary versions from previous deployment:");
401            if let Some(version) = &antnode_version {
402                println!("  antnode: {}", version);
403            }
404            if let Some(version) = &antctl_version {
405                println!("  antctl: {}", version);
406            }
407            if let Some(version) = &ant_version {
408                println!("  ant: {}", version);
409            }
410
411            BinaryOption::Versioned {
412                ant_version,
413                antnode_version,
414                antctl_version,
415            }
416        };
417
418        let (genesis_multiaddr, genesis_ip) =
419            if environment_details.deployment_type == DeploymentType::New {
420                match get_genesis_multiaddr(&self.ansible_runner, &self.ssh_client) {
421                    Ok((multiaddr, ip)) => (Some(multiaddr), Some(ip)),
422                    Err(_) => (None, None),
423                }
424            } else {
425                (None, None)
426            };
427        let inventory = DeploymentInventory {
428            binary_option,
429            client_vms,
430            environment_details,
431            failed_node_registry_vms,
432            faucet_address: genesis_ip.map(|ip| format!("{ip}:8000")),
433            full_cone_nat_gateway_vms,
434            full_cone_private_node_vms,
435            genesis_multiaddr,
436            genesis_vm,
437            name: name.to_string(),
438            misc_vms,
439            node_vms: generic_node_vms,
440            peer_cache_node_vms,
441            ssh_user: self.cloud_provider.get_ssh_user(),
442            ssh_private_key_path: self.ssh_client.private_key_path.clone(),
443            symmetric_nat_gateway_vms,
444            symmetric_private_node_vms,
445            uploaded_files: Vec::new(),
446        };
447        debug!("Inventory: {inventory:?}");
448        Ok(inventory)
449    }
450
451    /// Create all the environment inventory files. This also updates the SSH client to route the private nodes
452    /// the NAT gateway if it exists.
453    ///
454    /// This is used when 'generate_or_retrieve_inventory' is not used, but you still need to set up the inventory files.
455    pub fn setup_environment_inventory(&self, name: &str) -> Result<()> {
456        let output_inventory_dir_path = self
457            .working_directory_path
458            .join("ansible")
459            .join("inventory");
460        generate_environment_inventory(
461            name,
462            &self.inventory_file_path,
463            &output_inventory_dir_path,
464        )?;
465
466        let full_cone_nat_gateway_vms = self
467            .ansible_runner
468            .get_inventory(AnsibleInventoryType::FullConeNatGateway, false)?;
469        let full_cone_private_node_vms = self
470            .ansible_runner
471            .get_inventory(AnsibleInventoryType::FullConePrivateNodes, false)?;
472
473        let symmetric_nat_gateway_vms = self
474            .ansible_runner
475            .get_inventory(AnsibleInventoryType::SymmetricNatGateway, false)?;
476        let symmetric_private_node_vms = self
477            .ansible_runner
478            .get_inventory(AnsibleInventoryType::SymmetricPrivateNodes, false)?;
479
480        // Create static inventory for private nodes. Will be used during ansible-playbook run.
481        generate_symmetric_private_node_static_environment_inventory(
482            name,
483            &output_inventory_dir_path,
484            &symmetric_private_node_vms,
485            &symmetric_nat_gateway_vms,
486            &self.ssh_client.private_key_path,
487        )?;
488
489        generate_full_cone_private_node_static_environment_inventory(
490            name,
491            &output_inventory_dir_path,
492            &full_cone_private_node_vms,
493            &full_cone_nat_gateway_vms,
494            &self.ssh_client.private_key_path,
495        )?;
496
497        // Set up the SSH client to route through the NAT gateway if it exists. This updates all the client clones.
498        if !full_cone_nat_gateway_vms.is_empty() {
499            self.ssh_client.set_full_cone_nat_routed_vms(
500                &full_cone_private_node_vms,
501                &full_cone_nat_gateway_vms,
502            )?;
503        }
504
505        if !symmetric_nat_gateway_vms.is_empty() {
506            self.ssh_client.set_symmetric_nat_routed_vms(
507                &symmetric_private_node_vms,
508                &symmetric_nat_gateway_vms,
509            )?;
510        }
511
512        Ok(())
513    }
514
515    pub async fn upload_network_contacts(
516        &self,
517        inventory: &DeploymentInventory,
518        contacts_file_name: Option<String>,
519    ) -> Result<()> {
520        let temp_dir_path = tempfile::tempdir()?.into_path();
521        let temp_file_path = if let Some(file_name) = contacts_file_name {
522            temp_dir_path.join(file_name)
523        } else {
524            temp_dir_path.join(inventory.name.clone())
525        };
526
527        let mut file = std::fs::File::create(&temp_file_path)?;
528        let mut rng = rand::thread_rng();
529
530        let peer_cache_peers = inventory
531            .peer_cache_node_vms
532            .iter()
533            .flat_map(|vm| vm.get_quic_addresses())
534            .collect::<Vec<_>>();
535        let peer_cache_peers_len = peer_cache_peers.len();
536        for peer in peer_cache_peers
537            .iter()
538            .filter(|&peer| peer != UNAVAILABLE_NODE)
539            .cloned()
540            .choose_multiple(&mut rng, DEFAULT_CONTACTS_COUNT)
541        {
542            writeln!(file, "{peer}",)?;
543        }
544
545        if DEFAULT_CONTACTS_COUNT > peer_cache_peers_len {
546            let node_peers = inventory
547                .node_vms
548                .iter()
549                .flat_map(|vm| vm.get_quic_addresses())
550                .collect::<Vec<_>>();
551            for peer in node_peers
552                .iter()
553                .filter(|&peer| peer != UNAVAILABLE_NODE)
554                .cloned()
555                .choose_multiple(&mut rng, DEFAULT_CONTACTS_COUNT - peer_cache_peers_len)
556            {
557                writeln!(file, "{peer}",)?;
558            }
559        }
560
561        self.s3_repository
562            .upload_file(TESTNET_BUCKET_NAME, &temp_file_path, true)
563            .await?;
564
565        Ok(())
566    }
567
568    /// Connects to a VM with SSH and runs a command to retrieve the version of a binary.
569    fn get_bin_version(&self, vm: &VirtualMachine, command: &str, prefix: &str) -> Result<Version> {
570        let output = self.ssh_client.run_command(
571            &vm.public_ip_addr,
572            &self.cloud_provider.get_ssh_user(),
573            command,
574            true,
575        )?;
576        let version_line = output
577            .first()
578            .ok_or_else(|| eyre!("No output from {} command", command))?;
579        let version_str = version_line
580            .strip_prefix(prefix)
581            .ok_or_else(|| eyre!("Unexpected output format from {} command", command))?;
582        Version::parse(version_str).map_err(|e| eyre!("Failed to parse {} version: {}", command, e))
583    }
584
585    /// Generate or retrieve the Client inventory for the deployment.
586    ///
587    /// If we're creating a new environment and there is no inventory yet, an empty inventory will
588    /// be returned; otherwise the inventory will represent what is deployed currently.
589    ///
590    /// The `force` flag is used when the `deploy` command runs, to make sure that a new inventory
591    /// is generated, because it's possible that an old one with the same environment name has been
592    /// cached.
593    pub async fn generate_or_retrieve_client_inventory(
594        &self,
595        name: &str,
596        region: &str,
597        force: bool,
598        binary_option: Option<BinaryOption>,
599    ) -> Result<ClientsDeploymentInventory> {
600        println!("===============================================");
601        println!("  Generating or Retrieving Client Inventory  ");
602        println!("===============================================");
603        let inventory_path = get_data_directory()?.join(format!("{name}-clients-inventory.json"));
604        if inventory_path.exists() && !force {
605            let inventory = ClientsDeploymentInventory::read(&inventory_path)?;
606            return Ok(inventory);
607        }
608
609        // This allows for the inventory to be generated without a Terraform workspace to be
610        // initialised, which is the case in the workflow for printing an inventory.
611        if !force {
612            let environments = self.terraform_runner.workspace_list()?;
613            if !environments.contains(&name.to_string()) {
614                return Err(eyre!("The '{}' environment does not exist", name));
615            }
616        }
617
618        // For new environments, whether it's a new or bootstrap deploy, the inventory files need
619        // to be generated for the Ansible run to work correctly.
620        //
621        // It is an idempotent operation; the files won't be generated if they already exist.
622        let output_inventory_dir_path = self
623            .working_directory_path
624            .join("ansible")
625            .join("inventory");
626        generate_environment_inventory(
627            name,
628            &self.inventory_file_path,
629            &output_inventory_dir_path,
630        )?;
631
632        let environment_details = match get_environment_details(name, &self.s3_repository).await {
633            Ok(details) => details,
634            Err(Error::EnvironmentDetailsNotFound(_)) => {
635                println!("Environment details not found: treating this as a new deployment");
636                return Ok(ClientsDeploymentInventory::empty(
637                    name,
638                    binary_option.ok_or_else(|| {
639                        eyre!("For a new deployment the binary option must be set")
640                    })?,
641                    region,
642                ));
643            }
644            Err(e) => return Err(e.into()),
645        };
646
647        let client_and_sks = self.ansible_provisioner.get_client_secret_keys()?;
648        let client_vms: Vec<ClientVirtualMachine> = client_and_sks
649            .iter()
650            .map(|(vm, sks)| ClientVirtualMachine {
651                vm: vm.clone(),
652                wallet_public_key: sks
653                    .iter()
654                    .enumerate()
655                    .map(|(user, sk)| (format!("safe{}", user + 1), sk.address().encode_hex()))
656                    .collect(),
657            })
658            .collect();
659
660        let binary_option = if let Some(binary_option) = binary_option {
661            binary_option
662        } else {
663            let ant_version = if !client_vms.is_empty() {
664                let random_client_vm = client_vms
665                    .choose(&mut rand::thread_rng())
666                    .ok_or_else(|| eyre!("No Client VMs available to retrieve ant version"))?;
667                self.get_bin_version(&random_client_vm.vm, "ant --version", "Autonomi Client v")
668                    .ok()
669            } else {
670                None
671            };
672
673            println!("Retrieved binary versions from previous deployment:");
674            if let Some(version) = &ant_version {
675                println!("  ant: {}", version);
676            }
677
678            BinaryOption::Versioned {
679                ant_version,
680                antnode_version: None,
681                antctl_version: None,
682            }
683        };
684
685        let inventory = ClientsDeploymentInventory {
686            binary_option,
687            client_vms,
688            environment_type: environment_details.environment_type,
689            evm_details: environment_details.evm_details,
690            funding_wallet_address: None, // This would need to be populated from somewhere
691            network_id: environment_details.network_id,
692            failed_node_registry_vms: Vec::new(),
693            name: name.to_string(),
694            region: environment_details.region,
695            ssh_user: self.cloud_provider.get_ssh_user(),
696            ssh_private_key_path: self.ssh_client.private_key_path.clone(),
697            uploaded_files: Vec::new(),
698        };
699
700        debug!("Client Inventory: {inventory:?}");
701        Ok(inventory)
702    }
703}
704
705impl NodeVirtualMachine {
706    pub fn from_list(
707        vms: &[VirtualMachine],
708        node_registries: &DeploymentNodeRegistries,
709    ) -> Vec<Self> {
710        let mut node_vms = Vec::new();
711        for vm in vms {
712            let node_registry = node_registries
713                .retrieved_registries
714                .iter()
715                .find(|(name, _)| {
716                    if vm.name.contains("private") {
717                        let result = name == &vm.private_ip_addr.to_string();
718                        debug!(
719                            "Vm name: {name} is a private node with result {result}. Vm: {vm:?}"
720                        );
721                        result
722                    } else {
723                        name == &vm.name
724                    }
725                })
726                .map(|(_, reg)| reg);
727
728            // We want to accommodate cases where the node registry is empty because the machine
729            // may not have been provisioned yet.
730            let node_vm = Self {
731                node_count: node_registry.map_or(0, |reg| reg.nodes.len()),
732                node_listen_addresses: node_registry.map_or_else(Vec::new, |reg| {
733                    if reg.nodes.is_empty() {
734                        Vec::new()
735                    } else {
736                        reg.nodes
737                            .iter()
738                            .map(|node| {
739                                node.listen_addr
740                                    .as_ref()
741                                    .map(|addrs| {
742                                        addrs.iter().map(|addr| addr.to_string()).collect()
743                                    })
744                                    .unwrap_or_default()
745                            })
746                            .collect()
747                    }
748                }),
749                rpc_endpoint: node_registry.map_or_else(HashMap::new, |reg| {
750                    reg.nodes
751                        .iter()
752                        .filter_map(|node| {
753                            node.peer_id
754                                .map(|peer_id| (peer_id.to_string(), node.rpc_socket_addr))
755                        })
756                        .collect()
757                }),
758                safenodemand_endpoint: node_registry
759                    .and_then(|reg| reg.daemon.as_ref())
760                    .and_then(|daemon| daemon.endpoint),
761                vm: vm.clone(),
762            };
763            node_vms.push(node_vm.clone());
764            debug!("Added node VM: {node_vm:?}");
765        }
766        debug!("Node VMs generated from NodeRegistries: {node_vms:?}");
767        node_vms
768    }
769
770    pub fn get_quic_addresses(&self) -> Vec<String> {
771        self.node_listen_addresses
772            .iter()
773            .map(|addresses| {
774                addresses
775                    .iter()
776                    .find(|addr| {
777                        addr.contains("/quic-v1")
778                            && !addr.starts_with("/ip4/127.0.0.1")
779                            && !addr.starts_with("/ip4/10.")
780                    })
781                    .map(|s| s.to_string())
782                    .unwrap_or_else(|| UNAVAILABLE_NODE.to_string())
783            })
784            .collect()
785    }
786}
787
788/// The name of the OS user.
789pub type OsUser = String;
790
791#[derive(Clone, Debug, Serialize, Deserialize)]
792pub struct ClientVirtualMachine {
793    pub vm: VirtualMachine,
794    /// The public key of the wallet for each OS user (1 ant uploader instance per OS user).
795    pub wallet_public_key: HashMap<OsUser, String>,
796}
797
798#[derive(Clone, Debug, Serialize, Deserialize)]
799pub struct NodeVirtualMachine {
800    pub vm: VirtualMachine,
801    pub node_count: usize,
802    pub node_listen_addresses: Vec<Vec<String>>,
803    pub rpc_endpoint: HashMap<String, SocketAddr>,
804    pub safenodemand_endpoint: Option<SocketAddr>,
805}
806
807#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
808pub struct VirtualMachine {
809    pub id: u64,
810    pub name: String,
811    pub public_ip_addr: IpAddr,
812    pub private_ip_addr: IpAddr,
813}
814
815#[derive(Clone)]
816pub struct DeploymentNodeRegistries {
817    pub inventory_type: AnsibleInventoryType,
818    /// The (name, NodeRegistry) pairs for each VM that was successfully retrieved.
819    /// Note: for private nodes, the name is set to the private address of the VM.
820    pub retrieved_registries: Vec<(String, NodeRegistry)>,
821    pub failed_vms: Vec<String>,
822}
823
824impl DeploymentNodeRegistries {
825    pub fn print(&self) {
826        if self.retrieved_registries.is_empty() {
827            return;
828        }
829
830        Self::print_banner(&self.inventory_type.to_string());
831        for (vm_name, registry) in self.retrieved_registries.iter() {
832            println!("{vm_name}:");
833            for node in registry.nodes.iter() {
834                println!(
835                    "  {}: {} {}",
836                    node.service_name,
837                    node.version,
838                    Self::format_status(&node.status)
839                );
840            }
841        }
842        if !self.failed_vms.is_empty() {
843            println!(
844                "Failed to retrieve node registries for {}:",
845                self.inventory_type
846            );
847            for vm_name in self.failed_vms.iter() {
848                println!("- {}", vm_name);
849            }
850        }
851    }
852
853    fn format_status(status: &ServiceStatus) -> String {
854        match status {
855            ServiceStatus::Running => "RUNNING".to_string(),
856            ServiceStatus::Stopped => "STOPPED".to_string(),
857            ServiceStatus::Added => "ADDED".to_string(),
858            ServiceStatus::Removed => "REMOVED".to_string(),
859        }
860    }
861
862    fn print_banner(text: &str) {
863        let padding = 2;
864        let text_width = text.len() + padding * 2;
865        let border_chars = 2;
866        let total_width = text_width + border_chars;
867        let top_bottom = "═".repeat(total_width);
868
869        println!("╔{}╗", top_bottom);
870        println!("║ {:^width$} ║", text, width = text_width);
871        println!("╚{}╝", top_bottom);
872    }
873}
874
875#[derive(Clone, Debug, Serialize, Deserialize)]
876pub struct DeploymentInventory {
877    pub binary_option: BinaryOption,
878    pub client_vms: Vec<ClientVirtualMachine>,
879    pub environment_details: EnvironmentDetails,
880    pub failed_node_registry_vms: Vec<String>,
881    pub faucet_address: Option<String>,
882    pub full_cone_nat_gateway_vms: Vec<VirtualMachine>,
883    pub full_cone_private_node_vms: Vec<NodeVirtualMachine>,
884    pub genesis_vm: Option<NodeVirtualMachine>,
885    pub genesis_multiaddr: Option<String>,
886    pub misc_vms: Vec<VirtualMachine>,
887    pub name: String,
888    pub node_vms: Vec<NodeVirtualMachine>,
889    pub peer_cache_node_vms: Vec<NodeVirtualMachine>,
890    pub ssh_user: String,
891    pub ssh_private_key_path: PathBuf,
892    pub symmetric_nat_gateway_vms: Vec<VirtualMachine>,
893    pub symmetric_private_node_vms: Vec<NodeVirtualMachine>,
894    pub uploaded_files: Vec<(String, String)>,
895}
896
897impl DeploymentInventory {
898    /// Create an inventory for a new deployment which is initially empty, other than the name and
899    /// binary option, which will have been selected.
900    pub fn empty(name: &str, binary_option: BinaryOption) -> DeploymentInventory {
901        Self {
902            binary_option,
903            client_vms: Default::default(),
904            environment_details: EnvironmentDetails::default(),
905            genesis_vm: Default::default(),
906            genesis_multiaddr: Default::default(),
907            failed_node_registry_vms: Default::default(),
908            faucet_address: Default::default(),
909            full_cone_nat_gateway_vms: Default::default(),
910            full_cone_private_node_vms: Default::default(),
911            misc_vms: Default::default(),
912            name: name.to_string(),
913            node_vms: Default::default(),
914            peer_cache_node_vms: Default::default(),
915            ssh_user: "root".to_string(),
916            ssh_private_key_path: Default::default(),
917            symmetric_nat_gateway_vms: Default::default(),
918            symmetric_private_node_vms: Default::default(),
919            uploaded_files: Default::default(),
920        }
921    }
922
923    pub fn get_tfvars_filenames(&self) -> Vec<String> {
924        let filenames = self
925            .environment_details
926            .environment_type
927            .get_tfvars_filenames(&self.name, &self.environment_details.region);
928        debug!("Using tfvars files {filenames:?}");
929        filenames
930    }
931
932    pub fn is_empty(&self) -> bool {
933        self.peer_cache_node_vms.is_empty() && self.node_vms.is_empty()
934    }
935
936    pub fn vm_list(&self) -> Vec<VirtualMachine> {
937        let mut list = Vec::new();
938        list.extend(self.symmetric_nat_gateway_vms.clone());
939        list.extend(self.full_cone_nat_gateway_vms.clone());
940        list.extend(
941            self.peer_cache_node_vms
942                .iter()
943                .map(|node_vm| node_vm.vm.clone()),
944        );
945        list.extend(self.genesis_vm.iter().map(|node_vm| node_vm.vm.clone()));
946        list.extend(self.node_vms.iter().map(|node_vm| node_vm.vm.clone()));
947        list.extend(self.misc_vms.clone());
948        list.extend(
949            self.symmetric_private_node_vms
950                .iter()
951                .map(|node_vm| node_vm.vm.clone()),
952        );
953        list.extend(
954            self.full_cone_private_node_vms
955                .iter()
956                .map(|node_vm| node_vm.vm.clone()),
957        );
958        list.extend(self.client_vms.iter().map(|client_vm| client_vm.vm.clone()));
959        list
960    }
961
962    pub fn node_vm_list(&self) -> Vec<NodeVirtualMachine> {
963        let mut list = Vec::new();
964        list.extend(self.peer_cache_node_vms.iter().cloned());
965        list.extend(self.genesis_vm.iter().cloned());
966        list.extend(self.node_vms.iter().cloned());
967        list.extend(self.full_cone_private_node_vms.iter().cloned());
968        list.extend(self.symmetric_private_node_vms.iter().cloned());
969
970        list
971    }
972
973    pub fn peers(&self) -> HashSet<String> {
974        let mut list = HashSet::new();
975        list.extend(
976            self.peer_cache_node_vms
977                .iter()
978                .flat_map(|node_vm| node_vm.get_quic_addresses()),
979        );
980        list.extend(
981            self.genesis_vm
982                .iter()
983                .flat_map(|node_vm| node_vm.get_quic_addresses()),
984        );
985        list.extend(
986            self.node_vms
987                .iter()
988                .flat_map(|node_vm| node_vm.get_quic_addresses()),
989        );
990        list.extend(
991            self.full_cone_private_node_vms
992                .iter()
993                .flat_map(|node_vm| node_vm.get_quic_addresses()),
994        );
995        list.extend(
996            self.symmetric_private_node_vms
997                .iter()
998                .flat_map(|node_vm| node_vm.get_quic_addresses()),
999        );
1000        list
1001    }
1002
1003    pub fn save(&self) -> Result<()> {
1004        let path = get_data_directory()?.join(format!("{}-inventory.json", self.name));
1005        let serialized_data = serde_json::to_string_pretty(self)?;
1006        let mut file = File::create(path)?;
1007        file.write_all(serialized_data.as_bytes())?;
1008        Ok(())
1009    }
1010
1011    pub fn read(file_path: &PathBuf) -> Result<Self> {
1012        let data = std::fs::read_to_string(file_path)?;
1013        let deserialized_data: DeploymentInventory = serde_json::from_str(&data)?;
1014        Ok(deserialized_data)
1015    }
1016
1017    pub fn add_uploaded_files(&mut self, uploaded_files: Vec<(String, String)>) {
1018        self.uploaded_files.extend_from_slice(&uploaded_files);
1019    }
1020
1021    pub fn get_random_peer(&self) -> Option<String> {
1022        let mut rng = rand::thread_rng();
1023        self.peers().into_iter().choose(&mut rng)
1024    }
1025
1026    pub fn peer_cache_node_count(&self) -> usize {
1027        if let Some(first_vm) = self.peer_cache_node_vms.first() {
1028            first_vm.node_count
1029        } else {
1030            0
1031        }
1032    }
1033
1034    pub fn genesis_node_count(&self) -> usize {
1035        if let Some(genesis_vm) = &self.genesis_vm {
1036            genesis_vm.node_count
1037        } else {
1038            0
1039        }
1040    }
1041
1042    pub fn node_count(&self) -> usize {
1043        if let Some(first_vm) = self.node_vms.first() {
1044            first_vm.node_count
1045        } else {
1046            0
1047        }
1048    }
1049
1050    pub fn full_cone_private_node_count(&self) -> usize {
1051        if let Some(first_vm) = self.full_cone_private_node_vms.first() {
1052            first_vm.node_count
1053        } else {
1054            0
1055        }
1056    }
1057
1058    pub fn symmetric_private_node_count(&self) -> usize {
1059        if let Some(first_vm) = self.symmetric_private_node_vms.first() {
1060            first_vm.node_count
1061        } else {
1062            0
1063        }
1064    }
1065
1066    pub fn print_report(&self, full: bool) -> Result<()> {
1067        println!("**************************************");
1068        println!("*                                    *");
1069        println!("*          Inventory Report          *");
1070        println!("*                                    *");
1071        println!("**************************************");
1072
1073        println!("Environment Name: {}", self.name);
1074        println!();
1075        match &self.binary_option {
1076            BinaryOption::BuildFromSource {
1077                repo_owner, branch, ..
1078            } => {
1079                println!("==============");
1080                println!("Branch Details");
1081                println!("==============");
1082                println!("Repo owner: {repo_owner}");
1083                println!("Branch name: {branch}");
1084                println!();
1085            }
1086            BinaryOption::Versioned {
1087                ant_version,
1088                antnode_version,
1089                antctl_version,
1090            } => {
1091                println!("===============");
1092                println!("Version Details");
1093                println!("===============");
1094                println!(
1095                    "ant version: {}",
1096                    ant_version
1097                        .as_ref()
1098                        .map_or("N/A".to_string(), |v| v.to_string())
1099                );
1100                println!(
1101                    "antnode version: {}",
1102                    antnode_version
1103                        .as_ref()
1104                        .map_or("N/A".to_string(), |v| v.to_string())
1105                );
1106                println!(
1107                    "antctl version: {}",
1108                    antctl_version
1109                        .as_ref()
1110                        .map_or("N/A".to_string(), |v| v.to_string())
1111                );
1112                println!();
1113            }
1114        }
1115
1116        if !self.peer_cache_node_vms.is_empty() {
1117            println!("==============");
1118            println!("Peer Cache VMs");
1119            println!("==============");
1120            for node_vm in self.peer_cache_node_vms.iter() {
1121                println!("{}: {}", node_vm.vm.name, node_vm.vm.public_ip_addr);
1122            }
1123            println!("Nodes per VM: {}", self.peer_cache_node_count());
1124            println!("SSH user: {}", self.ssh_user);
1125            println!();
1126
1127            self.print_peer_cache_webserver();
1128        }
1129
1130        println!("========");
1131        println!("Node VMs");
1132        println!("========");
1133        if let Some(genesis_vm) = &self.genesis_vm {
1134            println!("{}: {}", genesis_vm.vm.name, genesis_vm.vm.public_ip_addr);
1135        }
1136        for node_vm in self.node_vms.iter() {
1137            println!("{}: {}", node_vm.vm.name, node_vm.vm.public_ip_addr);
1138        }
1139        println!("Nodes per VM: {}", self.node_count());
1140        println!("SSH user: {}", self.ssh_user);
1141        println!();
1142
1143        if !self.full_cone_private_node_vms.is_empty() {
1144            println!("=================");
1145            println!("Full Cone Private Node VMs");
1146            println!("=================");
1147            let full_cone_private_node_nat_gateway_map =
1148                PrivateNodeProvisionInventory::match_private_node_vm_and_gateway_vm(
1149                    self.full_cone_private_node_vms
1150                        .iter()
1151                        .map(|node_vm| node_vm.vm.clone())
1152                        .collect::<Vec<_>>()
1153                        .as_slice(),
1154                    &self.full_cone_nat_gateway_vms,
1155                )?;
1156
1157            for (node_vm, nat_gateway_vm) in full_cone_private_node_nat_gateway_map.iter() {
1158                println!(
1159                    "{}: {} ==routed through==> {}: {}",
1160                    node_vm.name,
1161                    node_vm.public_ip_addr,
1162                    nat_gateway_vm.name,
1163                    nat_gateway_vm.public_ip_addr
1164                );
1165                let ssh = if let Some(ssh_key_path) = self.ssh_private_key_path.to_str() {
1166                    format!(
1167                        "ssh -i {ssh_key_path} root@{}",
1168                        nat_gateway_vm.public_ip_addr,
1169                    )
1170                } else {
1171                    format!("ssh root@{}", nat_gateway_vm.public_ip_addr,)
1172                };
1173                println!("SSH using NAT gateway: {ssh}");
1174            }
1175            println!("Nodes per VM: {}", self.full_cone_private_node_count());
1176            println!("SSH user: {}", self.ssh_user);
1177            println!();
1178        }
1179
1180        if !self.symmetric_private_node_vms.is_empty() {
1181            println!("=================");
1182            println!("Symmetric Private Node VMs");
1183            println!("=================");
1184            let symmetric_private_node_nat_gateway_map =
1185                PrivateNodeProvisionInventory::match_private_node_vm_and_gateway_vm(
1186                    self.symmetric_private_node_vms
1187                        .iter()
1188                        .map(|node_vm| node_vm.vm.clone())
1189                        .collect::<Vec<_>>()
1190                        .as_slice(),
1191                    &self.symmetric_nat_gateway_vms,
1192                )?;
1193
1194            for (node_vm, nat_gateway_vm) in symmetric_private_node_nat_gateway_map.iter() {
1195                println!(
1196                    "{}: {} ==routed through==> {}: {}",
1197                    node_vm.name,
1198                    node_vm.public_ip_addr,
1199                    nat_gateway_vm.name,
1200                    nat_gateway_vm.public_ip_addr
1201                );
1202                let ssh = if let Some(ssh_key_path) = self.ssh_private_key_path.to_str() {
1203                    format!(
1204                        "ssh -i {ssh_key_path} -o ProxyCommand=\"ssh -W %h:%p root@{} -i {ssh_key_path}\" root@{}",
1205                        nat_gateway_vm.public_ip_addr, node_vm.private_ip_addr
1206                    )
1207                } else {
1208                    format!(
1209                        "ssh -o ProxyCommand=\"ssh -W %h:%p root@{}\" root@{}",
1210                        nat_gateway_vm.public_ip_addr, node_vm.private_ip_addr
1211                    )
1212                };
1213                println!("SSH using NAT gateway: {ssh}");
1214            }
1215            println!("Nodes per VM: {}", self.symmetric_private_node_count());
1216            println!("SSH user: {}", self.ssh_user);
1217            println!();
1218        }
1219
1220        if !self.client_vms.is_empty() {
1221            println!("==========");
1222            println!("Client VMs");
1223            println!("==========");
1224            for client_vm in self.client_vms.iter() {
1225                println!("{}: {}", client_vm.vm.name, client_vm.vm.public_ip_addr);
1226            }
1227            println!();
1228
1229            println!("=============================");
1230            println!("Ant Client Wallet Public Keys");
1231            println!("=============================");
1232            for client_vm in self.client_vms.iter() {
1233                for (user, key) in client_vm.wallet_public_key.iter() {
1234                    println!("{}@{}: {}", client_vm.vm.name, user, key);
1235                }
1236            }
1237        }
1238
1239        if !self.misc_vms.is_empty() {
1240            println!("=========");
1241            println!("Other VMs");
1242            println!("=========");
1243        }
1244        if !self.misc_vms.is_empty() {
1245            for vm in self.misc_vms.iter() {
1246                println!("{}: {}", vm.name, vm.public_ip_addr);
1247            }
1248        }
1249
1250        for nat_gateway_vm in self.full_cone_nat_gateway_vms.iter() {
1251            println!("{}: {}", nat_gateway_vm.name, nat_gateway_vm.public_ip_addr);
1252        }
1253
1254        for nat_gateway_vm in self.symmetric_nat_gateway_vms.iter() {
1255            println!("{}: {}", nat_gateway_vm.name, nat_gateway_vm.public_ip_addr);
1256        }
1257
1258        println!("SSH user: {}", self.ssh_user);
1259        println!();
1260
1261        if full {
1262            println!("===============");
1263            println!("Full Peer List");
1264            println!("===============");
1265            let mut quic_listeners = Vec::new();
1266            let mut ws_listeners = Vec::new();
1267
1268            for node_vm in self.peer_cache_node_vms.iter().chain(self.node_vms.iter()) {
1269                for addresses in &node_vm.node_listen_addresses {
1270                    for addr in addresses {
1271                        if !addr.starts_with("/ip4/127.0.0.1") && !addr.starts_with("/ip4/10.") {
1272                            if addr.contains("/quic") {
1273                                quic_listeners.push(addr.clone());
1274                            } else if addr.contains("/ws") {
1275                                ws_listeners.push(addr.clone());
1276                            }
1277                        }
1278                    }
1279                }
1280            }
1281
1282            if !quic_listeners.is_empty() {
1283                println!("QUIC:");
1284                for addr in quic_listeners {
1285                    println!("  {addr}");
1286                }
1287                println!();
1288            }
1289
1290            if !ws_listeners.is_empty() {
1291                println!("Websocket:");
1292                for addr in ws_listeners {
1293                    println!("  {addr}");
1294                }
1295                println!();
1296            }
1297        } else {
1298            println!("============");
1299            println!("Sample Peers");
1300            println!("============");
1301            self.peer_cache_node_vms
1302                .iter()
1303                .chain(self.node_vms.iter())
1304                .map(|node_vm| node_vm.vm.public_ip_addr.to_string())
1305                .for_each(|ip| {
1306                    if let Some(peer) = self.peers().iter().find(|p| p.contains(&ip)) {
1307                        println!("{peer}");
1308                    }
1309                });
1310        }
1311        println!();
1312
1313        println!(
1314            "Genesis: {}",
1315            self.genesis_multiaddr
1316                .as_ref()
1317                .map_or("N/A", |genesis| genesis)
1318        );
1319        let inventory_file_path =
1320            get_data_directory()?.join(format!("{}-inventory.json", self.name));
1321        println!(
1322            "The full inventory is at {}",
1323            inventory_file_path.to_string_lossy()
1324        );
1325        println!();
1326
1327        if !self.uploaded_files.is_empty() {
1328            println!("Uploaded files:");
1329            for file in self.uploaded_files.iter() {
1330                println!("{}: {}", file.0, file.1);
1331            }
1332        }
1333
1334        if self
1335            .environment_details
1336            .evm_details
1337            .data_payments_address
1338            .is_some()
1339            || self
1340                .environment_details
1341                .evm_details
1342                .payment_token_address
1343                .is_some()
1344            || self.environment_details.evm_details.rpc_url.is_some()
1345        {
1346            println!("===========");
1347            println!("EVM Details");
1348            println!("===========");
1349            println!(
1350                "EVM data payments address: {}",
1351                self.environment_details
1352                    .evm_details
1353                    .data_payments_address
1354                    .as_ref()
1355                    .map_or("N/A", |addr| addr)
1356            );
1357            println!(
1358                "EVM payment token address: {}",
1359                self.environment_details
1360                    .evm_details
1361                    .payment_token_address
1362                    .as_ref()
1363                    .map_or("N/A", |addr| addr)
1364            );
1365            println!(
1366                "EVM RPC URL: {}",
1367                self.environment_details
1368                    .evm_details
1369                    .rpc_url
1370                    .as_ref()
1371                    .map_or("N/A", |addr| addr)
1372            );
1373        }
1374
1375        Ok(())
1376    }
1377
1378    pub fn get_genesis_ip(&self) -> Option<IpAddr> {
1379        self.misc_vms
1380            .iter()
1381            .find(|vm| vm.name.contains("genesis"))
1382            .map(|vm| vm.public_ip_addr)
1383    }
1384
1385    pub fn print_peer_cache_webserver(&self) {
1386        println!("=====================");
1387        println!("Peer Cache Webservers");
1388        println!("=====================");
1389
1390        for node_vm in &self.peer_cache_node_vms {
1391            let webserver = get_bootstrap_cache_url(&node_vm.vm.public_ip_addr);
1392            println!("{}: {webserver}", node_vm.vm.name);
1393        }
1394    }
1395}
1396
1397#[derive(Clone, Debug, Serialize, Deserialize)]
1398pub struct ClientsDeploymentInventory {
1399    pub binary_option: BinaryOption,
1400    pub client_vms: Vec<ClientVirtualMachine>,
1401    pub environment_type: EnvironmentType,
1402    pub evm_details: EvmDetails,
1403    pub funding_wallet_address: Option<String>,
1404    pub network_id: Option<u8>,
1405    pub failed_node_registry_vms: Vec<String>,
1406    pub name: String,
1407    pub region: String,
1408    pub ssh_user: String,
1409    pub ssh_private_key_path: PathBuf,
1410    pub uploaded_files: Vec<(String, String)>,
1411}
1412
1413impl ClientsDeploymentInventory {
1414    /// Create an inventory for a new Client deployment which is initially empty, other than the name and
1415    /// binary option, which will have been selected.
1416    pub fn empty(
1417        name: &str,
1418        binary_option: BinaryOption,
1419        region: &str,
1420    ) -> ClientsDeploymentInventory {
1421        Self {
1422            binary_option,
1423            client_vms: Default::default(),
1424            environment_type: EnvironmentType::default(),
1425            evm_details: EvmDetails::default(),
1426            funding_wallet_address: None,
1427            network_id: None,
1428            failed_node_registry_vms: Default::default(),
1429            name: name.to_string(),
1430            region: region.to_string(),
1431            ssh_user: "root".to_string(),
1432            ssh_private_key_path: Default::default(),
1433            uploaded_files: Default::default(),
1434        }
1435    }
1436
1437    pub fn get_tfvars_filenames(&self) -> Vec<String> {
1438        debug!("Environment type: {:?}", self.environment_type);
1439        let filenames = self
1440            .environment_type
1441            .get_tfvars_filenames(&self.name, &self.region);
1442        debug!("Using tfvars files {filenames:?}");
1443        filenames
1444    }
1445
1446    pub fn is_empty(&self) -> bool {
1447        self.client_vms.is_empty()
1448    }
1449
1450    pub fn vm_list(&self) -> Vec<VirtualMachine> {
1451        self.client_vms
1452            .iter()
1453            .map(|client_vm| client_vm.vm.clone())
1454            .collect()
1455    }
1456
1457    pub fn save(&self) -> Result<()> {
1458        let path = get_data_directory()?.join(format!("{}-clients-inventory.json", self.name));
1459        let serialized_data = serde_json::to_string_pretty(self)?;
1460        let mut file = File::create(path)?;
1461        file.write_all(serialized_data.as_bytes())?;
1462        Ok(())
1463    }
1464
1465    pub fn read(file_path: &PathBuf) -> Result<Self> {
1466        let data = std::fs::read_to_string(file_path)?;
1467        let deserialized_data: ClientsDeploymentInventory = serde_json::from_str(&data)?;
1468        Ok(deserialized_data)
1469    }
1470
1471    pub fn add_uploaded_files(&mut self, uploaded_files: Vec<(String, String)>) {
1472        self.uploaded_files.extend_from_slice(&uploaded_files);
1473    }
1474
1475    pub fn print_report(&self) -> Result<()> {
1476        println!("*************************************");
1477        println!("*                                   *");
1478        println!("*     Clients Inventory Report      *");
1479        println!("*                                   *");
1480        println!("*************************************");
1481
1482        println!("Environment Name: {}", self.name);
1483        println!();
1484        match &self.binary_option {
1485            BinaryOption::BuildFromSource {
1486                repo_owner, branch, ..
1487            } => {
1488                println!("==============");
1489                println!("Branch Details");
1490                println!("==============");
1491                println!("Repo owner: {repo_owner}");
1492                println!("Branch name: {branch}");
1493                println!();
1494            }
1495            BinaryOption::Versioned { ant_version, .. } => {
1496                println!("===============");
1497                println!("Version Details");
1498                println!("===============");
1499                println!(
1500                    "ant version: {}",
1501                    ant_version
1502                        .as_ref()
1503                        .map_or("N/A".to_string(), |v| v.to_string())
1504                );
1505                println!();
1506            }
1507        }
1508
1509        if !self.client_vms.is_empty() {
1510            println!("==========");
1511            println!("Client VMs");
1512            println!("==========");
1513            for client_vm in self.client_vms.iter() {
1514                println!("{}: {}", client_vm.vm.name, client_vm.vm.public_ip_addr);
1515            }
1516            println!("SSH user: {}", self.ssh_user);
1517            println!();
1518
1519            println!("=============================");
1520            println!("Ant Client Wallet Public Keys");
1521            println!("=============================");
1522            for client_vm in self.client_vms.iter() {
1523                for (user, key) in client_vm.wallet_public_key.iter() {
1524                    println!("{}@{}: {}", client_vm.vm.name, user, key);
1525                }
1526            }
1527            println!();
1528        }
1529
1530        if !self.uploaded_files.is_empty() {
1531            println!("==============");
1532            println!("Uploaded files");
1533            println!("==============");
1534            for file in self.uploaded_files.iter() {
1535                println!("{}: {}", file.0, file.1);
1536            }
1537            println!();
1538        }
1539
1540        if self.evm_details.data_payments_address.is_some()
1541            || self.evm_details.payment_token_address.is_some()
1542            || self.evm_details.rpc_url.is_some()
1543        {
1544            println!("===========");
1545            println!("EVM Details");
1546            println!("===========");
1547            println!(
1548                "EVM data payments address: {}",
1549                self.evm_details
1550                    .data_payments_address
1551                    .as_ref()
1552                    .map_or("N/A", |addr| addr)
1553            );
1554            println!(
1555                "EVM payment token address: {}",
1556                self.evm_details
1557                    .payment_token_address
1558                    .as_ref()
1559                    .map_or("N/A", |addr| addr)
1560            );
1561            println!(
1562                "EVM RPC URL: {}",
1563                self.evm_details.rpc_url.as_ref().map_or("N/A", |addr| addr)
1564            );
1565            println!();
1566        }
1567
1568        if let Some(funding_wallet_address) = &self.funding_wallet_address {
1569            println!("======================");
1570            println!("Funding Wallet Address");
1571            println!("======================");
1572            println!("{}", funding_wallet_address);
1573            println!();
1574        }
1575
1576        if let Some(network_id) = &self.network_id {
1577            println!("==========");
1578            println!("Network ID");
1579            println!("==========");
1580            println!("{}", network_id);
1581            println!();
1582        }
1583
1584        let inventory_file_path =
1585            get_data_directory()?.join(format!("{}-clients-inventory.json", self.name));
1586        println!(
1587            "The full Clients inventory is at {}",
1588            inventory_file_path.to_string_lossy()
1589        );
1590        println!();
1591
1592        Ok(())
1593    }
1594}
1595
1596pub fn get_data_directory() -> Result<PathBuf> {
1597    let path = dirs_next::data_dir()
1598        .ok_or_else(|| eyre!("Could not retrieve data directory"))?
1599        .join("autonomi")
1600        .join("testnet-deploy");
1601    if !path.exists() {
1602        std::fs::create_dir_all(path.clone())?;
1603    }
1604    Ok(path)
1605}