|
| 1 | +package contract |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/smartcontractkit/chainlink-deployments-framework/datastore" |
| 8 | + cldcatalog "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/catalog" |
| 9 | + "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/config" |
| 10 | + cfgdomain "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/config/domain" |
| 11 | + cfgnet "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/config/network" |
| 12 | + "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/domain" |
| 13 | + "github.com/smartcontractkit/chainlink-deployments-framework/pkg/logger" |
| 14 | +) |
| 15 | + |
| 16 | +// NetworkLoaderFunc loads network configuration for an environment. |
| 17 | +type NetworkLoaderFunc func(env string, dom domain.Domain) (*cfgnet.Config, error) |
| 18 | + |
| 19 | +// DataStoreLoadOptions configures how the datastore is loaded. |
| 20 | +type DataStoreLoadOptions struct { |
| 21 | + // FromLocal, when true, always use local files (envdir.DataStore) and ignore domain config. |
| 22 | + // Use for local runs when you want to verify against local datastore only. |
| 23 | + FromLocal bool |
| 24 | +} |
| 25 | + |
| 26 | +// DataStoreLoaderFunc returns a datastore for the given env directory. |
| 27 | +// When opts.FromLocal is false and domain uses catalog datastore, loads from the remote catalog (CI-friendly). |
| 28 | +type DataStoreLoaderFunc func(ctx context.Context, envdir domain.EnvDir, lggr logger.Logger, opts DataStoreLoadOptions) (datastore.DataStore, error) |
| 29 | + |
| 30 | +// Deps holds injectable dependencies. |
| 31 | +type Deps struct { |
| 32 | + NetworkLoader NetworkLoaderFunc |
| 33 | + DataStoreLoader DataStoreLoaderFunc |
| 34 | +} |
| 35 | + |
| 36 | +func (d *Deps) applyDefaults() { |
| 37 | + if d.NetworkLoader == nil { |
| 38 | + d.NetworkLoader = defaultNetworkLoader |
| 39 | + } |
| 40 | + if d.DataStoreLoader == nil { |
| 41 | + d.DataStoreLoader = defaultDataStoreLoader |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func defaultNetworkLoader(env string, dom domain.Domain) (*cfgnet.Config, error) { |
| 46 | + return config.LoadNetworks(env, dom, logger.Nop()) |
| 47 | +} |
| 48 | + |
| 49 | +func defaultDataStoreLoader(ctx context.Context, envdir domain.EnvDir, lggr logger.Logger, opts DataStoreLoadOptions) (datastore.DataStore, error) { |
| 50 | + dom := domain.NewDomain(envdir.RootPath(), envdir.DomainKey()) |
| 51 | + envKey := envdir.Key() |
| 52 | + |
| 53 | + cfg, err := config.Load(dom, envKey, lggr) |
| 54 | + if err != nil { |
| 55 | + if opts.FromLocal { |
| 56 | + lggr.Infow("Loading datastore from local files") |
| 57 | + return envdir.DataStore() |
| 58 | + } |
| 59 | + |
| 60 | + return nil, fmt.Errorf("failed to load config: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + if opts.FromLocal || cfg.DatastoreType == cfgdomain.DatastoreTypeFile { |
| 64 | + lggr.Infow("Loading datastore from local files") |
| 65 | + return envdir.DataStore() |
| 66 | + } |
| 67 | + |
| 68 | + if cfg.Env.Catalog.GRPC == "" { |
| 69 | + return nil, fmt.Errorf("catalog GRPC endpoint is required when datastore is set to %q", cfg.DatastoreType) |
| 70 | + } |
| 71 | + lggr.Infow("Loading datastore from catalog", "url", cfg.Env.Catalog.GRPC) |
| 72 | + catalogStore, err := cldcatalog.LoadCatalog(ctx, envKey, cfg, dom) |
| 73 | + if err != nil { |
| 74 | + return nil, fmt.Errorf("failed to load catalog: %w", err) |
| 75 | + } |
| 76 | + ds, err := datastore.LoadDataStoreFromCatalog(ctx, catalogStore) |
| 77 | + if err != nil { |
| 78 | + return nil, fmt.Errorf("failed to load data from catalog: %w", err) |
| 79 | + } |
| 80 | + lggr.Infow("Loaded datastore from catalog") |
| 81 | + |
| 82 | + return ds, nil |
| 83 | +} |
0 commit comments