Skip to content

Commit d577271

Browse files
authored
refactor(engine): move env network from cld (#332)
Move environment network from cld to cldf. https://smartcontract-it.atlassian.net/browse/CLD-563
1 parent ff82a88 commit d577271

File tree

4 files changed

+469
-0
lines changed

4 files changed

+469
-0
lines changed

.changeset/sweet-frogs-talk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"chainlink-deployments-framework": patch
3+
---
4+
5+
refactor: move environment network from cld

engine/cld/environment/names.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package environment
2+
3+
const (
4+
Local = "local"
5+
StagingTestnet = "staging_testnet"
6+
StagingMainnet = "staging_mainnet"
7+
Staging = "staging" // Note this is currently the equivalent of staging_testnet.
8+
ProdMainnet = "prod_mainnet"
9+
ProdTestnet = "prod_testnet"
10+
Prod = "prod"
11+
12+
// Legacy environments to be cleaned up once the migration to the above environments is completed.
13+
Testnet = "testnet"
14+
Mainnet = "mainnet"
15+
SolStaging = "solana-staging" // Note this is testnet staging for Solana.
16+
)

engine/cld/network/network.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

Comments
 (0)