-
Notifications
You must be signed in to change notification settings - Fork 85
/
getters.go
389 lines (325 loc) · 10.6 KB
/
getters.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package config
import (
"fmt"
"net"
"os"
"strings"
"time"
"github.com/mitchellh/mapstructure"
"github.com/multiformats/go-multiaddr"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"github.com/bacalhau-project/bacalhau/pkg/config/types"
"github.com/bacalhau-project/bacalhau/pkg/lib/network"
"github.com/bacalhau-project/bacalhau/pkg/logger"
)
// GetConfig returns the current resolved configuration from viper as a BacalhauConfig.
// This is the resolved configuration after all configuration sources have been merged,
// including the default configuration, the configuration file, environment variables, and flags.
func GetConfig() (*types.BacalhauConfig, error) {
out := new(types.BacalhauConfig)
if err := viper.Unmarshal(&out, DecoderHook); err != nil {
return nil, err
}
return out, nil
}
func ClientAPIPort() uint16 {
return uint16(viper.GetInt(types.NodeClientAPIPort))
}
func ClientAPIHost() string {
return viper.GetString(types.NodeClientAPIHost)
}
func ClientTLSConfig() types.ClientTLSConfig {
cfg := types.ClientTLSConfig{
UseTLS: viper.GetBool(types.NodeClientAPIClientTLSUseTLS),
Insecure: viper.GetBool(types.NodeClientAPIClientTLSInsecure),
CACert: viper.GetString(types.NodeClientAPIClientTLSCACert),
}
if !cfg.UseTLS {
// If we haven't explicitly turned on TLS, but implied it through
// the other options, then set it to true
if cfg.Insecure || cfg.CACert != "" {
cfg.UseTLS = true
}
}
return cfg
}
func ClientAPIBase() string {
scheme := "http"
if ClientTLSConfig().UseTLS {
scheme = "https"
}
return fmt.Sprintf("%s://%s:%d", scheme, ClientAPIHost(), ClientAPIPort())
}
func ServerAPIPort() uint16 {
return uint16(viper.GetInt(types.NodeServerAPIPort))
}
func configError(e error) {
msg := fmt.Sprintf("config error: %s", e)
fmt.Fprintln(os.Stderr, msg)
os.Exit(1)
}
func ServerAPIHost() string {
host := viper.GetString(types.NodeServerAPIHost)
if net.ParseIP(host) == nil {
// We should check that the value gives us an address type
// we can use to get our IP address. If it doesn't, we should
// panic.
atype, ok := network.AddressTypeFromString(host)
if !ok {
configError(fmt.Errorf("invalid address type in Server API Host config: %s", host))
}
addr, err := network.GetNetworkAddress(atype, network.AllAddresses)
if err != nil {
configError(errors.Wrap(err, fmt.Sprintf("failed to get network address for Server API Host: %s", host)))
}
if len(addr) == 0 {
configError(fmt.Errorf("no %s addresses found for Server API Host", host))
}
// Use the first address
host = addr[0]
}
return host
}
func ServerAutoCertDomain() string {
return viper.GetString(types.NodeServerAPITLSAutoCert)
}
func GetRequesterCertificateSettings() (string, string) {
cert := viper.GetString(types.NodeServerAPITLSServerCertificate)
key := viper.GetString(types.NodeServerAPITLSServerKey)
return cert, key
}
func DevstackGetShouldPrintInfo() bool {
return os.Getenv("DEVSTACK_PRINT_INFO") != ""
}
func DevstackSetShouldPrintInfo() {
os.Setenv("DEVSTACK_PRINT_INFO", "1")
}
func DevstackEnvFile() string {
return os.Getenv("DEVSTACK_ENV_FILE")
}
func ShouldKeepStack() bool {
return os.Getenv("KEEP_STACK") != ""
}
type DockerCredentials struct {
Username string
Password string
}
func (d *DockerCredentials) IsValid() bool {
return d.Username != "" && d.Password != ""
}
func GetDockerCredentials() DockerCredentials {
return DockerCredentials{
Username: os.Getenv("DOCKER_USERNAME"),
Password: os.Getenv("DOCKER_PASSWORD"),
}
}
func GetLibp2pConfig() (types.Libp2pConfig, error) {
var libp2pCfg types.Libp2pConfig
if err := ForKey(types.NodeLibp2p, &libp2pCfg); err != nil {
return types.Libp2pConfig{}, err
}
return libp2pCfg, nil
}
func GetBootstrapPeers() ([]multiaddr.Multiaddr, error) {
bootstrappers := viper.GetStringSlice(types.NodeBootstrapAddresses)
peers := make([]multiaddr.Multiaddr, 0, len(bootstrappers))
for _, peer := range bootstrappers {
parsed, err := multiaddr.NewMultiaddr(peer)
if err != nil {
return nil, err
}
peers = append(peers, parsed)
}
return peers, nil
}
func GetLogMode() logger.LogMode {
mode := viper.Get(types.NodeLoggingMode)
switch v := mode.(type) {
case logger.LogMode:
return v
case string:
out, err := logger.ParseLogMode(v)
if err != nil {
log.Warn().Err(err).Msgf("invalid logging mode specified: %s", v)
}
return out
default:
log.Error().Msgf("unknown logging mode: %v", mode)
return logger.LogModeDefault
}
}
func GetAutoCertCachePath() string {
return viper.GetString(types.NodeServerAPITLSAutoCertCachePath)
}
func GetLibp2pTracerPath() string {
return viper.GetString(types.MetricsLibp2pTracerPath)
}
func GetEventTracerPath() string {
return viper.GetString(types.MetricsEventTracerPath)
}
func GetExecutorPluginsPath() string {
return viper.GetString(types.NodeExecutorPluginPath)
}
// TODO idk where this goes yet these are mostly random
func GetDownloadURLRequestRetries() int {
return viper.GetInt(types.NodeDownloadURLRequestRetries)
}
func GetDownloadURLRequestTimeout() time.Duration {
return viper.GetDuration(types.NodeDownloadURLRequestTimeout)
}
func SetVolumeSizeRequestTimeout(value time.Duration) {
viper.Set(types.NodeVolumeSizeRequestTimeout, value)
}
func GetVolumeSizeRequestTimeout() time.Duration {
return viper.GetDuration(types.NodeVolumeSizeRequestTimeout)
}
func GetUpdateCheckFrequency() time.Duration {
return viper.GetDuration(types.UpdateCheckFrequency)
}
func GetStoragePath() string {
path := viper.GetString(types.NodeComputeStoragePath)
if path == "" {
return os.TempDir()
}
return path
}
func GetDockerManifestCacheSettings() (*types.DockerCacheConfig, error) {
//var cfg types.DockerCacheConfig
if cfg, err := Get[types.DockerCacheConfig](types.NodeComputeManifestCache); err != nil {
return nil, err
} else {
return &cfg, nil
}
}
// PreferredAddress will allow for the specifying of
// the preferred address to listen on for cases where it
// is not clear, or where the address does not appear when
// using 0.0.0.0
func PreferredAddress() string {
return os.Getenv("BACALHAU_PREFERRED_ADDRESS")
}
func GetStringMapString(key string) map[string]string {
return viper.GetStringMapString(key)
}
func Get[T any](key string) (T, error) {
raw := viper.Get(key)
if raw == nil {
return zeroValue[T](), fmt.Errorf("value not found for %s", key)
}
var val T
val, ok := raw.(T)
if !ok {
err := ForKey(key, &val)
if err != nil {
return zeroValue[T](), fmt.Errorf("value not of expected type, got: %T: %w", raw, err)
}
}
return val, nil
}
func zeroValue[T any]() T {
var zero T
return zero
}
// ForKey unmarshals configuration values associated with a given key into the provided cfg structure.
// It uses unmarshalCompositeKey internally to handle composite keys, ensuring values spread across
// nested sub-keys are correctly populated into the cfg structure.
//
// Parameters:
// - key: The configuration key to retrieve values for.
// - cfg: The structure into which the configuration values will be unmarshaled.
//
// Returns:
// - An error if any occurred during unmarshaling; otherwise, nil.
func ForKey(key string, cfg interface{}) error {
return unmarshalCompositeKey(key, cfg)
}
// unmarshalCompositeKey takes a key and an output structure to unmarshal into. It gets the
// composite value associated with the given key and decodes it into the provided output structure.
// It's especially useful when the desired value is not directly associated with the key, but
// instead is spread across various nested sub-keys within the configuration.
func unmarshalCompositeKey(key string, output interface{}) error {
compositeValue, isNested, err := getCompositeValue(key)
if err != nil {
return err
}
decoderConfig := &mapstructure.DecoderConfig{
DecodeHook: mapstructure.TextUnmarshallerHookFunc(),
Result: output,
TagName: "mapstructure", // This is the default struct tag name used by Viper.
}
decoder, err := mapstructure.NewDecoder(decoderConfig)
if err != nil {
return err
}
if isNested {
val, ok := compositeValue[key]
if !ok {
// NB(forrest): this case should never happen as we ensure all configuration values
// have a corresponding key via code gen. If this does occur it represents an error we need to debug.
err := fmt.Errorf("CRITICAL ERROR: invalid configuration detected for key: %s. Config value not found", key)
log.Err(err).Msg("invalid configuration detected")
return err
}
return decoder.Decode(val)
}
return decoder.Decode(compositeValue)
}
// getCompositeValue constructs a composite value for a given key. If the key directly corresponds
// to a set value in Viper, it returns that, and false to indicate the value isn't nested under the key.
// Otherwise, it collects all nested values under that key and returns them as a nested map and true
// indicating the value is nested under the key.
func getCompositeValue(key string) (map[string]interface{}, bool, error) {
var compositeValue map[string]interface{}
// Fetch directly if the exact key exists
if viper.IsSet(key) {
rawValue := viper.Get(key)
switch v := rawValue.(type) {
case map[string]interface{}:
compositeValue = v
default:
return map[string]interface{}{
key: rawValue,
}, true, nil
}
} else {
return nil, false, fmt.Errorf("configuration value not found for key: %s", key)
}
lowerKey := strings.ToLower(key)
// Prepare a map for faster key lookup.
viperKeys := viper.AllKeys()
keyMap := make(map[string]string, len(viperKeys))
for _, k := range viperKeys {
keyMap[strings.ToLower(k)] = k
}
// Build a composite map of values for keys nested under the provided key.
for lowerK, originalK := range keyMap {
if strings.HasPrefix(lowerK, lowerKey+".") {
parts := strings.Split(lowerK[len(lowerKey)+1:], ".")
if err := setNested(compositeValue, parts, viper.Get(originalK)); err != nil {
return nil, false, nil
}
}
}
return compositeValue, false, nil
}
// setNested is a recursive helper function that sets a value in a nested map based on a slice of keys.
// It goes through each key, creating maps for each level as needed, and ultimately sets the value
// in the innermost map.
func setNested(m map[string]interface{}, keys []string, value interface{}) error {
if len(keys) == 1 {
m[keys[0]] = value
return nil
}
// If the next map level doesn't exist, create it.
if m[keys[0]] == nil {
m[keys[0]] = make(map[string]interface{})
}
// Cast the nested level to a map and return an error if the type assertion fails.
nestedMap, ok := m[keys[0]].(map[string]interface{})
if !ok {
return fmt.Errorf("key %s is not of type map[string]interface{}", keys[0])
}
return setNested(nestedMap, keys[1:], value)
}