forked from vladaionescu/leveros
/
config.go
426 lines (372 loc) · 9.57 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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Package config provides configuration flags. These can be configured via
// command line flags, environment variables or via Consul.
package config
import (
"bytes"
"encoding/json"
"fmt"
"hash/crc32"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"sync"
"time"
consulapi "github.com/hashicorp/consul/api"
)
// PackageName is the name of this package.
const PackageName = "config"
const consulPrefix = "conf/"
// UploadFile uploads provided config file to consul for use as flags.
func UploadFile(filePath string, service string) error {
byteData, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
// Check contents are valid json and re-encode for minimal formatting.
var data map[string]interface{}
err = json.Unmarshal(byteData, &data)
if err != nil {
return err
}
byteData, err = json.Marshal(&data)
if err != nil {
return err
}
client := GetConsulClient()
kv := client.KV()
kv.Put(&consulapi.KVPair{
Key: consulPrefix + service,
Value: byteData,
}, nil)
return nil
}
const (
typeString = iota
typeInt
typeFloat
typeBool
typeDuration
)
type flagEntry struct {
commandLineValue interface{}
consulValue interface{}
defaultValue interface{}
flagType int
}
var (
// Guard the flags map and the entries within.
flagsRWMutex sync.RWMutex
flags = make(map[string]*flagEntry)
// RestArgs represents the list of command line args remaining after all
// the flags.
RestArgs []string
consulConfigChecksum uint32
consulConfigSize int
)
func declare(key string, defaultValue interface{}, flagType int) *flagEntry {
flagsRWMutex.Lock()
defer flagsRWMutex.Unlock()
_, alreadyExists := flags[key]
if alreadyExists {
log.Fatalf("Flag re-declaration of %s\n", key)
}
entry := &flagEntry{
defaultValue: defaultValue,
flagType: flagType,
}
flags[key] = entry
return entry
}
func (entry *flagEntry) get() interface{} {
flagsRWMutex.RLock()
defer flagsRWMutex.RUnlock()
return entry.getInternal()
}
func (entry *flagEntry) getInternal() interface{} {
if entry.commandLineValue != nil {
return entry.commandLineValue
}
if entry.consulValue != nil {
return entry.consulValue
}
return entry.defaultValue
}
// FlagString represents a string configuration.
type FlagString struct {
entry *flagEntry
}
// DeclareString can be used to declare a new string flag with a default value.
func DeclareString(pack, name, defaultValue string) *FlagString {
key := getKey(pack, name)
return &FlagString{entry: declare(key, defaultValue, typeString)}
}
// Get retrieves the value of flag.
func (flag *FlagString) Get() string {
return flag.entry.get().(string)
}
// FlagInt represents an integer configuration.
type FlagInt struct {
entry *flagEntry
}
// DeclareInt can be used to declare a new int flag with a default value.
func DeclareInt(pack, name string, defaultValue int) *FlagInt {
key := getKey(pack, name)
return &FlagInt{entry: declare(key, defaultValue, typeInt)}
}
// Get retrieves the value of flag.
func (flag *FlagInt) Get() int {
return flag.entry.get().(int)
}
// FlagFloat represents a float configuration.
type FlagFloat struct {
entry *flagEntry
}
// DeclareFloat can be used to declare a new float flag with a default value.
func DeclareFloat(pack, name string, defaultValue float64) *FlagFloat {
key := getKey(pack, name)
return &FlagFloat{entry: declare(key, defaultValue, typeFloat)}
}
// Get retrieves the value of flag.
func (flag *FlagFloat) Get() float64 {
return flag.entry.get().(float64)
}
// FlagBool represents a boolean configuration.
type FlagBool struct {
entry *flagEntry
}
// DeclareBool can be used to declare a new bool flag with a default value
// false.
func DeclareBool(pack, name string) *FlagBool {
key := getKey(pack, name)
return &FlagBool{entry: declare(key, false, typeBool)}
}
// Get retrieves the value of flag.
func (flag *FlagBool) Get() bool {
return flag.entry.get().(bool)
}
// FlagDuration represents a duration configuration.
type FlagDuration struct {
entry *flagEntry
}
// DeclareDuration can be used to declare a new duration flag with a default
// value.
func DeclareDuration(
pack, name string, defaultValue time.Duration) *FlagDuration {
key := getKey(pack, name)
return &FlagDuration{entry: declare(key, defaultValue, typeDuration)}
}
// Get retrieves the value of flag.
func (flag *FlagDuration) Get() time.Duration {
return flag.entry.get().(time.Duration)
}
// ConfigFlag is the service identifier used to get/monitor the flags from
// consul.
var ConfigFlag = DeclareString("", "config", "")
// Initialize needs to be called before any call to Get. It parses command line
// flags and begins monitoring of consul key holding the flags for this service.
func Initialize() {
flagsRWMutex.Lock()
parseArgs()
parseEnv()
flagsRWMutex.Unlock()
monitorConsul()
}
func parseArgs() {
var entry *flagEntry
var expectValue bool
var isRestArgs bool
for _, arg := range os.Args[1:] {
if expectValue {
var err error
entry.commandLineValue, err = parseFromString(arg, entry.flagType)
if err != nil {
log.Fatalf("Unable to parse flag value for %s\n", arg)
}
expectValue = false
continue
}
if isRestArgs {
RestArgs = append(RestArgs, arg)
continue
}
if len(arg) < 2 || arg[:2] != "--" {
RestArgs = append(RestArgs, arg)
isRestArgs = true
continue
}
if arg == "--" {
isRestArgs = true
continue
}
flag := arg[2:]
var exists bool
entry, exists = flags[flag]
if !exists {
log.Fatalf("Invalid flag %s\n", arg)
}
expectValue = true
}
}
func parseEnv() {
for key, entry := range flags {
if entry.commandLineValue != nil {
continue
}
envKey := "LEVEROS_" + strings.Replace(key, ".", "_", -1)
envValue := os.Getenv(envKey)
if envValue != "" {
var err error
entry.commandLineValue, err = parseFromString(
envValue, entry.flagType)
if err != nil {
log.Fatalf(
"Unable to parse env var %s as %s\n", envKey, envValue)
}
}
}
}
func monitorConsul() {
service := ConfigFlag.Get()
if service == "" {
return
}
client := GetConsulClient()
kv := client.KV()
var (
pair *consulapi.KVPair
qm *consulapi.QueryMeta
err error
)
for retry := 0; retry < 20; retry++ {
pair, qm, err = kv.Get(
consulPrefix+service, &consulapi.QueryOptions{
RequireConsistent: true,
})
if err != nil {
if consulapi.IsServerError(err) {
// Consul unavailable. Try again.
time.Sleep(1 * time.Second)
continue
} else {
break
}
}
if pair == nil {
// Config does not exist. Maybe not yet uploaded. Try again.
err = fmt.Errorf("Config not found")
time.Sleep(1 * time.Second)
continue
}
break
}
if err != nil {
log.Fatalf(
"Error trying to get config for the first time from consul: %v\n",
err)
}
updateConsulFlags(pair.Value)
go monitorConsulGoroutine(kv, service, qm.LastIndex)
}
func monitorConsulGoroutine(kv *consulapi.KV, service string, lastIndex uint64) {
for {
pair, qm, err := kv.Get(
consulPrefix+service,
&consulapi.QueryOptions{
WaitIndex: lastIndex,
RequireConsistent: true,
})
if err != nil {
if consulapi.IsServerError(err) {
// Consul unavailable. Try again.
time.Sleep(1 * time.Second)
continue
}
if strings.Contains(err.Error(), "read: connection timed out") {
// Try again.
time.Sleep(1 * time.Second)
continue
}
log.Fatalf("Error monitoring config in Consul: %v\n", err)
}
if pair == nil {
log.Fatalf("Config in consul has been deleted\n")
}
updateConsulFlags(pair.Value)
lastIndex = qm.LastIndex
}
}
func updateConsulFlags(byteData []byte) {
checksum := crc32.ChecksumIEEE(byteData)
if len(byteData) == consulConfigSize && checksum == consulConfigChecksum {
// Assume config did not change. Don't do anything.
return
}
consulConfigSize = len(byteData)
consulConfigChecksum = checksum
var data map[string]interface{}
err := json.Unmarshal(byteData, &data)
if err != nil {
log.Fatalf("Consul config is not valid JSON\n")
}
flagsRWMutex.Lock()
defer flagsRWMutex.Unlock()
// Clear existing flags.
for _, entry := range flags {
entry.consulValue = nil
}
// Update with data from consul.
for key, value := range data {
entry, exists := flags[key]
if !exists {
log.Fatalf("Invalid flag %s in consul config\n", key)
}
strValue, isStr := value.(string)
if !isStr {
var strFormatted []byte
strFormatted, err = json.Marshal(value)
if err != nil {
log.Fatalf("Unable to marshal value back to json\n")
}
strValue = string(strFormatted)
}
entry.consulValue, err = parseFromString(strValue, entry.flagType)
if err != nil {
log.Fatalf(
"Unable to parse flag %s from consul: %s\n", key, strValue)
}
}
var buffer bytes.Buffer
buffer.WriteString("Config updated from consul. Current configuration:\n")
for key, entry := range flags {
buffer.WriteString(fmt.Sprintf("\t%s=%v\n", key, entry.getInternal()))
}
// TODO: It might be necessary to put this logging through the usual
// leverutil.logging stuff. Problem is that there's a circular
// dependency issue. Possible solution: Move logging under config.
log.Printf(buffer.String())
}
func getKey(pack, name string) string {
if pack == "" {
return name
}
return pack + "." + name
}
func parseFromString(
value string, flagType int) (parsed interface{}, err error) {
switch flagType {
case typeString:
parsed = value
err = nil
case typeInt:
parsed, err = strconv.Atoi(value)
case typeFloat:
parsed, err = strconv.ParseFloat(value, 64)
case typeBool:
parsed, err = strconv.ParseBool(value)
case typeDuration:
parsed, err = time.ParseDuration(value)
}
return
}