-
Notifications
You must be signed in to change notification settings - Fork 86
/
config.go
151 lines (123 loc) · 4.16 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package config
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/bacalhau-project/bacalhau/pkg/config/types"
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
)
const (
environmentVariablePrefix = "BACALHAU"
inferConfigTypes = true
automaticEnvVar = true
// user key files
Libp2pPrivateKeyFileName = "libp2p_private_key"
UserPrivateKeyFileName = "user_id.pem"
// compute paths
ComputeStoragesPath = "executor_storages"
ComputeStorePath = "compute_store"
PluginsPath = "plugins"
// orchestrator paths
OrchestratorStorePath = "orchestrator_store"
AutoCertCachePath = "autocert-cache"
NetworkTransportStore = "nats-store"
// update check paths
UpdateCheckStatePath = "update.json"
// auth paths
TokensPath = "tokens.json"
)
var (
ComputeExecutionsStorePath = filepath.Join(ComputeStorePath, "executions.db")
OrchestratorJobStorePath = filepath.Join(OrchestratorStorePath, "jobs.db")
)
var (
environmentVariableReplace = strings.NewReplacer(".", "_")
DecoderHook = viper.DecodeHook(mapstructure.TextUnmarshallerHookFunc())
)
const (
ConfigFileName = "config.yaml"
ConfigFileMode = 0666
)
func Init(path string) (types.BacalhauConfig, error) {
// initialize the configuration with default values.
return initConfig(path,
WithDefaultConfig(getDefaultConfig(path)),
)
}
func Load(path string) (types.BacalhauConfig, error) {
return initConfig(path,
WithDefaultConfig(getDefaultConfig(path)),
WithFileHandler(ReadConfigHandler),
)
}
func getDefaultConfig(path string) types.BacalhauConfig {
// derive the default config for the specified environment.
defaultConfig := ForEnvironment()
// set default values for path dependent config.
defaultConfig.User.KeyPath = filepath.Join(path, UserPrivateKeyFileName)
defaultConfig.User.Libp2pKeyPath = filepath.Join(path, Libp2pPrivateKeyFileName)
defaultConfig.Node.ExecutorPluginPath = filepath.Join(path, PluginsPath)
defaultConfig.Node.ComputeStoragePath = filepath.Join(path, ComputeStoragesPath)
defaultConfig.Node.Compute.ExecutionStore.Path = filepath.Join(path, ComputeExecutionsStorePath)
defaultConfig.Node.Requester.JobStore.Path = filepath.Join(path, OrchestratorJobStorePath)
defaultConfig.Update.CheckStatePath = filepath.Join(path, UpdateCheckStatePath)
defaultConfig.Auth.TokensPath = filepath.Join(path, TokensPath)
// We default to the folder which contains the job store, and add
// a subfolder for the network store.
defaultConfig.Node.Network.StoreDir = filepath.Join(
filepath.Dir(defaultConfig.Node.Requester.JobStore.Path),
NetworkTransportStore,
)
return defaultConfig
}
type Params struct {
FileName string
FileHandler func(fileName string) error
DefaultConfig types.BacalhauConfig
}
func initConfig(path string, opts ...Option) (types.BacalhauConfig, error) {
params := &Params{
FileName: ConfigFileName,
FileHandler: NoopConfigHandler,
DefaultConfig: ForEnvironment(),
}
for _, opt := range opts {
opt(params)
}
configFile := filepath.Join(path, params.FileName)
viper.SetConfigFile(configFile)
viper.SetEnvPrefix(environmentVariablePrefix)
viper.SetTypeByDefaultValue(inferConfigTypes)
viper.SetEnvKeyReplacer(environmentVariableReplace)
if err := SetDefault(params.DefaultConfig); err != nil {
return types.BacalhauConfig{}, nil
}
if err := params.FileHandler(configFile); err != nil {
return types.BacalhauConfig{}, err
}
if automaticEnvVar {
viper.AutomaticEnv()
}
var out types.BacalhauConfig
if err := viper.Unmarshal(&out, DecoderHook); err != nil {
return types.BacalhauConfig{}, err
}
return out, nil
}
// Reset clears all configuration, useful for testing.
func Reset() {
viper.Reset()
}
// Getenv wraps os.Getenv and retrieves the value of the environment variable named by the config key.
// It returns the value, which will be empty if the variable is not present.
func Getenv(key string) string {
return os.Getenv(KeyAsEnvVar(key))
}
// KeyAsEnvVar returns the environment variable corresponding to a config key
func KeyAsEnvVar(key string) string {
return strings.ToUpper(
fmt.Sprintf("%s_%s", environmentVariablePrefix, environmentVariableReplace.Replace(key)),
)
}