-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
config.go
87 lines (74 loc) · 2.55 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package plugins
import (
"fmt"
"os"
"os/exec"
"strconv"
"github.com/smartcontractkit/chainlink-relay/pkg/loop"
)
// RegistrarConfig generates contains static configuration inher
type RegistrarConfig interface {
RegisterLOOP(loopId string, cmdName string) (func() *exec.Cmd, loop.GRPCOpts, error)
}
type registarConfig struct {
grpcOpts loop.GRPCOpts
loopRegistrationFn func(loopId string) (*RegisteredLoop, error)
}
// NewRegistrarConfig creates a RegistarConfig
// loopRegistrationFn must act as a global registry function of LOOPs and must be idempotent.
// The [func() *exec.Cmd] for a LOOP should be generated by calling [RegistrarConfig.RegisterLOOP]
func NewRegistrarConfig(grpcOpts loop.GRPCOpts, loopRegistrationFn func(loopId string) (*RegisteredLoop, error)) RegistrarConfig {
return ®istarConfig{
grpcOpts: grpcOpts,
loopRegistrationFn: loopRegistrationFn,
}
}
// RegisterLOOP calls the configured loopRegistrationFn. The loopRegistrationFn must act as a global registry for LOOPs and must be idempotent.
func (pc *registarConfig) RegisterLOOP(loopID string, cmdName string) (func() *exec.Cmd, loop.GRPCOpts, error) {
cmdFn, err := NewCmdFactory(pc.loopRegistrationFn, CmdConfig{
ID: loopID,
Cmd: cmdName,
})
if err != nil {
return nil, loop.GRPCOpts{}, err
}
return cmdFn, pc.grpcOpts, nil
}
// EnvConfig is the configuration interface between the application and the LOOP executable. The values
// are fully resolved and static and passed via the environment.
type EnvConfig interface {
PrometheusPort() int
}
// SetCmdEnvFromConfig sets LOOP-specific vars in the env of the given cmd.
func SetCmdEnvFromConfig(cmd *exec.Cmd, cfg EnvConfig) {
forward := func(name string) {
if v, ok := os.LookupEnv(name); ok {
cmd.Env = append(cmd.Env, name+"="+v)
}
}
forward("CL_LOG_SQL_MIGRATIONS")
cmd.Env = append(cmd.Env,
"CL_PROMETHEUS_PORT="+strconv.FormatInt(int64(cfg.PrometheusPort()), 10),
)
}
// GetEnvConfig deserializes LOOP-specific environment variables to an EnvConfig
func GetEnvConfig() (EnvConfig, error) {
promPortStr := os.Getenv("CL_PROMETHEUS_PORT")
promPort, err := strconv.Atoi(promPortStr)
if err != nil {
return nil, fmt.Errorf("failed to parse CL_PROMETHEUS_PORT = %q: %w", promPortStr, err)
}
return NewEnvConfig(promPort), nil
}
// envConfig is an implementation of EnvConfig.
type envConfig struct {
prometheusPort int
}
func NewEnvConfig(prometheusPort int) EnvConfig {
return &envConfig{
prometheusPort: prometheusPort,
}
}
func (e *envConfig) PrometheusPort() int {
return e.prometheusPort
}