This repository has been archived by the owner on Jan 31, 2019. It is now read-only.
forked from josegonzalez/replicator
/
config.go
172 lines (142 loc) · 3.75 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package base
import (
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
"github.com/elsevier-core-engineering/replicator/client"
"github.com/elsevier-core-engineering/replicator/replicator/structs"
)
// Define default local addresses for Consul and Nomad
const (
LocalConsulAddress = "localhost:8500"
LocalNomadAddress = "http://localhost:4646"
)
// DefaultConfig returns a default configuration struct with sane defaults.
func DefaultConfig() *structs.Config {
return &structs.Config{
Consul: LocalConsulAddress,
ConsulKeyRoot: "replicator/config",
Nomad: LocalNomadAddress,
LogLevel: "INFO",
ClusterScalingInterval: 10,
JobScalingInterval: 10,
Telemetry: &structs.Telemetry{},
Notification: &structs.Notification{},
}
}
// DevConfig returns a configuration struct with sane defaults for development
// and testing purposes.
func DevConfig() *structs.Config {
return &structs.Config{
Consul: LocalConsulAddress,
ConsulKeyRoot: "replicator/config",
Nomad: LocalNomadAddress,
LogLevel: "DEBUG",
ClusterScalingInterval: 10,
JobScalingInterval: 10,
Telemetry: &structs.Telemetry{},
Notification: &structs.Notification{},
}
}
// InitializeClients completes the setup process for the Nomad and Consul
// clients. Must be called after configuration merging is complete.
func InitializeClients(config *structs.Config) (err error) {
// Setup the Nomad Client
nClient, err := client.NewNomadClient(config.Nomad)
if err != nil {
return
}
// Setup the Consul Client
cClient, err := client.NewConsulClient(config.Consul, config.ConsulToken)
if err != nil {
return
}
config.ConsulClient = cClient
config.NomadClient = nClient
return
}
// LoadConfig loads the configuration at the given path whether the specified
// path is an individual file or a directory of numerous configuration files.
func LoadConfig(path string) (*structs.Config, error) {
fi, err := os.Stat(path)
if err != nil {
return nil, err
}
if fi.IsDir() {
return LoadConfigDir(path)
}
cleaned := filepath.Clean(path)
config, err := ParseConfigFile(cleaned)
if err != nil {
return nil, fmt.Errorf("Error loading %s: %s", cleaned, err)
}
return config, nil
}
// LoadConfigDir loads all the configurations in the given directory
// in lexicographic order.
func LoadConfigDir(dir string) (*structs.Config, error) {
f, err := os.Open(dir)
if err != nil {
return nil, err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return nil, err
}
if !fi.IsDir() {
return nil, fmt.Errorf(
"configuration path must be a directory: %s", dir)
}
var files []string
err = nil
for err != io.EOF {
var fis []os.FileInfo
fis, err = f.Readdir(128)
if err != nil && err != io.EOF {
return nil, err
}
for _, fi := range fis {
// We do not wish tot navigate directories.
if fi.IsDir() {
continue
}
// Replicator can only parse HCL, and therefore json files, and so we
// ignore all other file extensions.
name := fi.Name()
skip := true
if strings.HasSuffix(name, ".hcl") {
skip = false
} else if strings.HasSuffix(name, ".json") {
skip = false
}
if skip {
continue
}
path := filepath.Join(dir, name)
files = append(files, path)
}
}
// If there are no files, there is no need to continue and therefore we exit
// quickly.
if len(files) == 0 {
return &structs.Config{}, nil
}
sort.Strings(files)
var result *structs.Config
for _, f := range files {
config, err := ParseConfigFile(f)
if err != nil {
return nil, fmt.Errorf("Error loading %s: %s", f, err)
}
if result == nil {
result = config
} else {
result = result.Merge(config)
}
}
return result, nil
}