-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
121 lines (110 loc) · 3.03 KB
/
config.go
File metadata and controls
121 lines (110 loc) · 3.03 KB
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
package main
import (
"github.com/chalupaul/viper"
log "github.com/Sirupsen/logrus"
"time"
"reflect"
"errors"
"fmt"
)
const DefaultCfgUrl string = "/julep/config"
const DefaultTreeUrl string = "/julep/hostgroups.json"
const CfgProviderType string = "etcd"
const DefaultEtcdUrl string = "http://localhost:4001/"
const DefaultKeyFile string = "/etc/julep/.secring.gpg"
type ConfigOption struct {
Url string
Key string
CfgUrl string
ProviderType string
ContentType string
}
// CfgOpt is a wrapper function to set options for LoadCfg()
type CfgOpt func(*ConfigOption) error
// LoadCfg builds a config object from etcd values. It takes a url to an etcd server
// and a keyfile to decrypt the contents. It takes the rest of the values from
// constants declared in this file. It will return any error that is bubbled up.
func LoadCfg(options ...CfgOpt) (*viper.Viper, error) {
// Set defaults
co := &ConfigOption{}
co.Url = DefaultEtcdUrl
co.Key = DefaultKeyFile
co.CfgUrl = DefaultCfgUrl
co.ProviderType = "etcd"
co.ContentType = "json"
// Override defaults
for _, op := range options {
err := op(co)
if err != nil {
return nil, err
}
}
// Make the connections
cfg := viper.New()
log.WithFields(log.Fields{
"url": co.Url,
"key": co.Key,
"function": "LoadCfg",
}).Debug("Loading config")
if err := cfg.AddSecureRemoteProvider(co.ProviderType, co.Url, co.CfgUrl, co.Key); err != nil {
log.WithFields(log.Fields{
"function": "cfg.AddSecureRemoteProvider",
"remote_provider": co.ProviderType,
"url": co.Url,
"path": co.CfgUrl,
"key": co.Key,
}).Fatal(err)
return nil, err
}
// Read config from etcd
cfg.SetConfigType(co.ContentType)
if err := cfg.ReadRemoteConfig(); err != nil {
log.WithFields(log.Fields{
"function": "cfg.ReadRemoteConfig",
"etcd_url": co.Url,
"path": co.CfgUrl,
"key": co.Key,
}).Fatal(err)
return nil, err
}
// poll for changes
go func() {
for {
time.Sleep(time.Second * 5)
if err := cfg.WatchRemoteConfig(); err != nil {
log.Warn(err)
} else {
log.Warn("Config changed. Reloading.")
}
}
}()
return cfg, nil
}
// OptionGeneric takes a string for value and name, then reflects the corresponding
// attribute from the ConfigOption struct and sets the variable. This should not be
// used directly.
func OptionGeneric(o, n string) func (c *ConfigOption) error {
return func(c *ConfigOption) error {
f := reflect.ValueOf(c).Elem().FieldByName(n)
if f.IsValid() && f.CanSet() {
f.SetString(o)
return nil
} else {
return errors.New(fmt.Sprintf("Failed to set config field %s: Invalid Option.", n))
}
reflect.ValueOf(c).Elem().FieldByName(n).SetString(o)
return nil
}
}
func OptionUrl(o string) func(c *ConfigOption) error {
return OptionGeneric(o, "Url")
}
func OptionKey(o string) func(c *ConfigOption) error {
return OptionGeneric(o, "Key")
}
func OptionCfgUrl(o string) func(c *ConfigOption) error {
return OptionGeneric(o, "CfgUrl")
}
func OptionProviderType(o string) func(c *ConfigOption) error {
return OptionGeneric(o, "ProviderType")
}