|
| 1 | +package network |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + |
| 8 | + "github.com/smartcontractkit/chainlink-common/pkg/logger" |
| 9 | + |
| 10 | + cldf_config_network "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/config/network" |
| 11 | + cldf_domain "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/domain" |
| 12 | + "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/environment" |
| 13 | +) |
| 14 | + |
| 15 | +// LoadNetworks retrieves the network configuration for the given domain and filters the networks |
| 16 | +// according to the specified environment. This ensures that only networks relevant to the selected |
| 17 | +// environment are accessible, minimizing the risk of accidental operations on unintended networks. |
| 18 | +func LoadNetworks( |
| 19 | + env string, domain cldf_domain.Domain, lggr logger.Logger, |
| 20 | +) (*cldf_config_network.Config, error) { |
| 21 | + cfg, err := loadNetworkConfig(domain) |
| 22 | + if err != nil { |
| 23 | + return nil, fmt.Errorf("failed to load network config: %w", err) |
| 24 | + } |
| 25 | + |
| 26 | + var ( |
| 27 | + typesAll = []cldf_config_network.NetworkType{cldf_config_network.NetworkTypeTestnet, cldf_config_network.NetworkTypeMainnet} |
| 28 | + typesTestnet = []cldf_config_network.NetworkType{cldf_config_network.NetworkTypeTestnet} |
| 29 | + typesMainnet = []cldf_config_network.NetworkType{cldf_config_network.NetworkTypeMainnet} |
| 30 | + ) |
| 31 | + |
| 32 | + // Filter networks based on the environment |
| 33 | + var networkTypes []cldf_config_network.NetworkType |
| 34 | + switch env { |
| 35 | + case environment.Local, environment.StagingTestnet, environment.ProdTestnet: |
| 36 | + networkTypes = typesTestnet |
| 37 | + case environment.StagingMainnet, environment.ProdMainnet: |
| 38 | + networkTypes = typesMainnet |
| 39 | + case environment.Prod: |
| 40 | + networkTypes = typesAll |
| 41 | + // The following environments are legacy environments that are used to support domains which |
| 42 | + // have not transitioned to the new environment structure. |
| 43 | + case environment.Testnet, environment.SolStaging: |
| 44 | + networkTypes = typesTestnet |
| 45 | + case environment.Staging: |
| 46 | + if domain.Key() == "data-streams" { |
| 47 | + networkTypes = typesAll |
| 48 | + } else { |
| 49 | + networkTypes = typesTestnet |
| 50 | + } |
| 51 | + case environment.Mainnet: |
| 52 | + networkTypes = typesMainnet |
| 53 | + default: |
| 54 | + lggr.Errorf("Unknown environment: %s", env) |
| 55 | + |
| 56 | + return nil, fmt.Errorf("unknown env: %s", env) |
| 57 | + } |
| 58 | + |
| 59 | + lggr.Infof("Loaded %s Networks for %s/%s", networkTypes, domain.Key(), env) |
| 60 | + |
| 61 | + return cfg.FilterWith(cldf_config_network.TypesFilter(networkTypes...)), nil |
| 62 | +} |
| 63 | + |
| 64 | +// loadNetworkConfig loads the network config from the .config directory in the given domain. |
| 65 | +func loadNetworkConfig(domain cldf_domain.Domain) (*cldf_config_network.Config, error) { |
| 66 | + // Check if the .config directory exists in the domain |
| 67 | + configDir := filepath.Join(domain.DirPath(), ".config") |
| 68 | + if _, err := os.Stat(configDir); err != nil { |
| 69 | + return nil, fmt.Errorf("cannot find config directory: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + // Find all yaml config files in the .config directory and any subdirectories |
| 73 | + var configFiles []string |
| 74 | + |
| 75 | + yamlFiles, err := filepath.Glob(filepath.Join(configDir, "**", "*.yaml")) |
| 76 | + if err != nil { |
| 77 | + return nil, fmt.Errorf("failed to find config files: %w", err) |
| 78 | + } |
| 79 | + configFiles = append(configFiles, yamlFiles...) |
| 80 | + |
| 81 | + ymlFiles, err := filepath.Glob(filepath.Join(configDir, "**", "*.yml")) |
| 82 | + if err != nil { |
| 83 | + return nil, fmt.Errorf("failed to find config files: %w", err) |
| 84 | + } |
| 85 | + configFiles = append(configFiles, ymlFiles...) |
| 86 | + |
| 87 | + if len(configFiles) == 0 { |
| 88 | + return nil, fmt.Errorf("no config files found in %s", configDir) |
| 89 | + } |
| 90 | + |
| 91 | + cfg, err := cldf_config_network.Load(configFiles) |
| 92 | + if err != nil { |
| 93 | + return nil, fmt.Errorf("failed to load config files: %w", err) |
| 94 | + } |
| 95 | + |
| 96 | + return cfg, nil |
| 97 | +} |
0 commit comments