/
etcdconfig.go
578 lines (493 loc) · 18 KB
/
etcdconfig.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
package etcdconfig
import (
"io"
"fmt"
"sort"
"math"
"errors"
// "time"
// "reflect"
"regexp"
// "reflect"
"io/ioutil"
"encoding/json"
"golang.org/x/net/context"
"github.com/luisfurquim/goose"
// etcd "github.com/coreos/etcd/client"
// etcd "github.com/etcd-io/etcd/client/v2"
etcd "go.etcd.io/etcd/client/v2"
)
type watchReq struct {
key string
id string
resp chan bool
}
type EtcdconfigG struct {
Setter goose.Alert
Getter goose.Alert
Updater goose.Alert
}
var Goose EtcdconfigG
var reArrayIndex *regexp.Regexp = regexp.MustCompile("/\\[([0-9]+)\\]$")
var reMapIndex *regexp.Regexp = regexp.MustCompile("/([^/]*)$")
var keysWatched map[string][]string
var wreq chan watchReq
var ErrIndex = errors.New("Error invalid index.")
func rSetConfig(path string, config map[string]interface{}, etcdcli etcd.KeysAPI) error {
var key string
var key2 int
var value, value2 interface{}
var err error
var resp *etcd.Response
var optDir *etcd.SetOptions
var ctx context.Context
optDir = &etcd.SetOptions{Dir:true}
ctx = context.Background()
resp, err = etcdcli.Set(ctx, path, "",optDir)
if err != nil {
Goose.Setter.Logf(1,"Error setting configuration, creating diretory.1 (%s): %s",path,err)
Goose.Setter.Fatalf(5,"path:%s, key:%s Metadata: %q",path, key, resp)
}
for key, value = range config {
switch value.(type) {
case map[string]interface{} :
Goose.Setter.Logf(5,"Found %s/%s=%q => map[string]interface{}", path, key, value.(map[string]interface{}))
err = rSetConfig(path + "/" + key, value.(map[string]interface{}), etcdcli)
if err != nil {
return err
}
case []interface{} :
Goose.Setter.Logf(5,"Found %s/%s=%q => []interface{}", path, key, value.([]interface{}))
resp, err = etcdcli.Set(ctx, fmt.Sprintf("%s/%s",path,key), "", optDir)
if err != nil {
Goose.Setter.Fatalf(1,"Error setting configuration, creating diretory.2 (%s/%s): %s",path,key,err)
}
for key2, value2 = range value.([]interface{}) {
switch value2.(type) {
case map[string]interface{} :
Goose.Setter.Logf(5,"Found %s/%s=%q => []interface{map[string]interface{}}", path, key, value2.(map[string]interface{}))
err = rSetConfig(fmt.Sprintf("%s/%s/[%d]",path,key,key2), value2.(map[string]interface{}), etcdcli)
if err != nil {
return err
}
case string :
Goose.Setter.Logf(5,"Found %s/%s=%q => []interface{string}", path, key, value2.(string))
resp, err = etcdcli.Set(ctx, fmt.Sprintf("%s/%s/[%d]",path,key,key2), value2.(string), nil)
if err != nil {
Goose.Setter.Fatalf(1,"Error setting configuration.1: %s",err)
} else {
// print common key info
Goose.Setter.Logf(5,"Configuration set. Metadata: %q\n", resp)
}
default:
Goose.Setter.Fatalf(1,"Invalid type: key=%s, key2=%d, value=%v",key,key2,value2)
}
}
case string :
resp, err = etcdcli.Set(ctx, path + "/" + key, value.(string), nil)
if err != nil {
Goose.Setter.Logf(1,"Error setting configuration.2: %s",err)
Goose.Setter.Fatalf(5,"path:%s, key:%s Metadata: %q",path, key, resp)
} else {
// print common key info
Goose.Setter.Logf(5,"Configuration.2 %s/%s=%s set. Metadata: %q", path, key, value.(string), resp)
}
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
resp, err = etcdcli.Set(ctx, path + "/" + key, fmt.Sprintf("%d",value), nil)
if err != nil {
Goose.Setter.Logf(1,"Error setting configuration.3: %s",err)
Goose.Setter.Fatalf(5,"path:%s, key:%s Metadata: %q",path, key, resp)
} else {
// print common key info
Goose.Setter.Logf(5,"Configuration.3 %s/%s=%d set. Metadata: %q", path, key, value, resp)
}
case float32:
if math.Floor(float64(value.(float32))) == float64(value.(float32)) {
resp, err = etcdcli.Set(ctx, path + "/" + key, fmt.Sprintf("%.0f",math.Floor(float64(value.(float32)))), nil)
} else {
resp, err = etcdcli.Set(ctx, path + "/" + key, fmt.Sprintf("%f",value.(float32)), nil)
}
if err != nil {
Goose.Setter.Logf(1,"Error setting configuration.4: %s",err)
Goose.Setter.Fatalf(5,"path:%s, key:%s Metadata: %q",path, key, resp)
} else {
// print common key info
Goose.Setter.Logf(5,"Configuration.4 %s/%s=%c set. Metadata: %q", path, key, value, resp)
}
case float64:
if math.Floor(value.(float64)) == value.(float64) {
resp, err = etcdcli.Set(ctx, path + "/" + key, fmt.Sprintf("%.0f",math.Floor(value.(float64))), nil)
} else {
resp, err = etcdcli.Set(ctx, path + "/" + key, fmt.Sprintf("%f",value.(float64)), nil)
}
if err != nil {
Goose.Setter.Logf(1,"Error setting configuration.4: %s",err)
Goose.Setter.Fatalf(5,"path:%s, key:%s Metadata: %q",path, key, resp)
} else {
// print common key info
Goose.Setter.Logf(5,"Configuration.5 %s/%s=%c set. Metadata: %q", path, key, value, resp)
}
default:
resp, err = etcdcli.Set(ctx, path + "/" + key, fmt.Sprintf("%s",value), nil)
if err != nil {
Goose.Setter.Logf(1,"Error setting configuration.5: %s",err)
Goose.Setter.Fatalf(5,"path:%s, key:%s Metadata: %q",path, key, resp)
} else {
// print common key info
Goose.Setter.Logf(5,"Configuration.6 %s/%s=%s set. Metadata: %q", path, key, value, resp)
}
// Goose.Setter.Fatalf(1,"Invalid type: key=%s, value=%#v, valuetype:%#v",key,value,reflect.TypeOf(value))
}
}
return nil
}
func Set(etcdcli etcd.KeysAPI, path, key, typ string) error {
var err error
var resp *etcd.Response
var ctx context.Context
ctx = context.Background()
switch typ {
case "string":
Goose.Setter.Logf(5,"Key to set: %s\n", "/"+ path + "/" + key)
resp, err = etcdcli.Set(ctx, "/" + path + "/" + key, "", nil)
if err != nil {
Goose.Setter.Logf(1,"Error setting configuration to string type: %s",err)
Goose.Setter.Logf(1,"path:%s, key:%s Metadata: %q",path, key, resp)
return err
} else {
// print common key info
Goose.Setter.Logf(2,"Configuration type string %s/%s set. Metadata: %q", path, key, resp)
}
case "number":
Goose.Setter.Logf(5,"Key to set: %s\n", "/"+ path + "/" + key)
resp, err = etcdcli.Set(ctx, "/"+ path + "/" + key, fmt.Sprintf("%d",0), nil)
if err != nil {
Goose.Setter.Logf(1,"Error setting configuration to number type: %s",err)
Goose.Setter.Logf(1,"path:%s, key:%s Metadata: %q",path, key, resp)
} else {
// print common key info
Goose.Setter.Logf(2,"Configuration type number %s/%s set. Metadata: %q", path, key, resp)
}
case "boolean":
Goose.Setter.Logf(5,"Key to set: %s\n", "/"+ path + "/" + key)
resp, err = etcdcli.Set(ctx, "/"+ path + "/" + key, fmt.Sprintf("%t","true"), nil)
if err != nil {
Goose.Setter.Logf(1,"Error setting configuration to boolean type: %s",err)
Goose.Setter.Logf(1,"path:%s, key:%s Metadata: %q",path, key, resp)
return err
} else {
// print common key info
Goose.Setter.Logf(2,"Configuration type boolean %s/%s set. Metadata: %q", path, key, resp)
}
default:
resp, err = etcdcli.Set(ctx, "/"+ path + "/" + key, "", nil)
if err != nil {
Goose.Setter.Logf(1,"Error setting configuration to default: %s",err)
Goose.Setter.Logf(1,"path:%s, key:%s Metadata: %q",path, key, resp)
return err
} else {
// print common key info
Goose.Setter.Logf(2,"Configuration default %s/%s set. Metadata: %q", path, key, resp)
}
}//ends switch
return nil
}
func SetConfig(cfg string, etcdcli etcd.Client, key string) error {
var err error
var configbuf []byte
var config map[string]interface{}
configbuf, err = ioutil.ReadFile(cfg)
if err != nil {
Goose.Setter.Logf(1,"Error reading config file (%s)\n",err)
return err
}
err = json.Unmarshal(configbuf, &config);
if err != nil {
Goose.Setter.Logf(1,"Error parsing config (%s)\n",err)
return err
}
err = rSetConfig("/" + key,config,etcd.NewKeysAPI(etcdcli))
if err != nil {
Goose.Setter.Logf(1,"Error setting config cluster (%s)\n",err)
return err
}
return nil
}
func SetConfigFromReader(cfg io.Reader, etcdcli etcd.Client, key string) error {
var err error
var configbuf []byte
var config map[string]interface{}
configbuf, err = ioutil.ReadAll(cfg)
if err != nil {
Goose.Setter.Logf(1,"Error reading config file (%s)\n",err)
return err
}
err = json.Unmarshal(configbuf, &config);
if err != nil {
Goose.Setter.Logf(1,"Error parsing config (%s)\n",err)
return err
}
err = rSetConfig("/" + key,config,etcd.NewKeysAPI(etcdcli))
if err != nil {
Goose.Setter.Logf(1,"Error setting config cluster (%s)\n",err)
return err
}
return nil
}
func SetConfigFromMap(config map[string]interface{}, etcdcli etcd.Client, key string) error {
var err error
err = rSetConfig("/" + key,config,etcd.NewKeysAPI(etcdcli))
if err != nil {
Goose.Setter.Logf(1,"Error setting config cluster (%s)\n",err)
return err
}
return nil
}
func rShowConfig(node *etcd.Node) error {
var err error
var child *etcd.Node
if !node.Dir {
Goose.Getter.Logf(1,"[%s] => %s",node.Key,node.Value)
return nil
}
Goose.Getter.Logf(1,"[%s]",node.Key)
for _, child = range node.Nodes {
if child != nil {
err = rShowConfig(child)
if err != nil {
Goose.Getter.Logf(1,"Error reading child node: %s",err)
return err
}
}
}
return nil
}
func rGetConfig(node *etcd.Node) (interface{}, interface{}, error) {
var err error
var child *etcd.Node
var i int
var data interface{}
var data2 interface{}
var index interface{}
var index2 interface{}
var array []interface{}
var matched []string
matched = reArrayIndex.FindStringSubmatch(node.Key)
if len(matched) > 0 {
fmt.Sscanf(matched[1],"%d",&i)
index = i
} else {
matched = reMapIndex.FindStringSubmatch(node.Key)
if len(matched) <= 1 {
Goose.Getter.Logf(1,"Error invalid index")
return nil, nil, ErrIndex
}
index = matched[1]
}
if !node.Dir {
Goose.Getter.Logf(4,"[%s] => %s",node.Key,node.Value)
return index, node.Value, nil
}
Goose.Getter.Logf(4,"[%s]",node.Key)
for _, child = range node.Nodes {
if child != nil {
index2, data2, err = rGetConfig(child)
if err != nil {
Goose.Getter.Logf(1,"Error reading child node: %s",err)
return nil, nil, err
}
switch index2.(type) {
case string:
if data == nil {
data = map[string]interface{}{}
}
data.(map[string]interface{})[index2.(string)] = data2
case int:
if array == nil {
array = make([]interface{},index2.(int)+1)
} else if len(array) <= index2.(int) {
array = append(array,make([]interface{},index2.(int)-len(array)+1)...)
}
array[index2.(int)] = data2
data = array
}
}
}
return index, data, nil
}
func GetConfig(etcdcli etcd.Client, key string) (interface{}, interface{}, error) {
var err error
var resp *etcd.Response
resp, err = etcd.NewKeysAPI(etcdcli).Get(context.Background(), "/" + key, &etcd.GetOptions{Recursive:true})
if err != nil {
Goose.Getter.Logf(1,"Error fetching configuration for %s: %s", "/" + key, err)
return nil, nil, err
}
return rGetConfig(resp.Node)
}
func DeleteConfig(etcdcli etcd.Client, key string) error {
var err error
_, err = etcd.NewKeysAPI(etcdcli).Delete(context.Background(), "/" + key, &etcd.DeleteOptions{Recursive:true,Dir:true})
if err != nil {
Goose.Updater.Logf(1,"Error deleting configuration: %s",err)
return err
}
return nil
}
func SetKey(etcdcli etcd.Client, key string, value string) error {
var err error
var resp *etcd.Response
var ctx context.Context
ctx = context.Background()
Goose.Setter.Logf(6,"Entering SetKey /%s <- %#v", key, value)
resp, err = etcd.NewKeysAPI(etcdcli).Set(ctx, "/" + key, value, nil)
if err != nil {
Goose.Setter.Logf(1,"Error setting configuration.2: %s",err)
Goose.Setter.Logf(5,"key:%s Metadata: %q", key, resp)
panic(fmt.Sprintf("Error setting configuration: %s",err))
} else {
// print common key info
Goose.Setter.Logf(5,"Configuration set. Metadata: %q", resp)
}
return nil
}
func watcherChecker(req chan watchReq) {
var reqKey watchReq
var ok bool
var j int
var kw []string
for {
reqKey = <-req
if reqKey.key == "" {
return
}
if kw, ok = keysWatched[reqKey.key]; !ok {
reqKey.resp<- false
keysWatched[reqKey.key] = []string{reqKey.id}
}
j = sort.Search(len(kw), func(i int) bool { return kw[i] >= reqKey.id })
if j < len(kw) && kw[j] == reqKey.id {
// reqKey.fn is present at kw[j]
reqKey.resp <- true
} else {
reqKey.resp <- false
// reqKey.id is not present at kw[j]
// but j is the index where it would be inserted.
if j==len(kw) {
keysWatched[reqKey.key] = append(kw,reqKey.id)
} else {
kw = append(kw,reqKey.id)
copy(kw[j+1:],kw[j:len(kw)-1])
kw[j] = reqKey.id
keysWatched[reqKey.key] = kw
}
}
}
}
func chkWatcher(key string, id string) bool {
var resp chan bool
resp = make(chan bool,1)
wreq<- watchReq{key:key, id:id, resp: resp}
return <-resp
}
func OnUpdate(etcdCli etcd.Client, key string, fn func(val string), id ...interface{}) {
var kapi etcd.KeysAPI
var ctx context.Context
var sid string
if len(id) > 0 {
switch id[0].(type) {
case string:
sid = id[0].(string)
default:
Goose.Updater.Logf(2,"Ignoring wrong parameter type for key %s (%#v)",key,id[0])
return
}
}
if chkWatcher(key, sid) {
return
}
kapi = etcd.NewKeysAPI(etcdCli)
ctx = context.Background()
go func (w etcd.Watcher) {
var err error
var resp *etcd.Response
for {
resp, err = w.Next(ctx)
if err == nil {
Goose.Updater.Logf(3,"Updating config variable %s = %s", key, resp.Node.Value)
fn(resp.Node.Value)
} else {
Goose.Updater.Logf(1,"Error updating config variable %s (%s)",key,err)
}
}
}(kapi.Watcher("/" + key,nil))
}
func OnUpdateIFace(etcdCli etcd.Client, key string, fn func(val interface{}), id ...interface{}) {
var kapi etcd.KeysAPI
var ctx context.Context
var sid string
if len(id) > 0 {
switch id[0].(type) {
case string:
sid = id[0].(string)
default:
Goose.Updater.Logf(2,"Ignoring wrong parameter type for key %s (%#v)",key,id[0])
return
}
}
if chkWatcher(key, sid) {
return
}
kapi = etcd.NewKeysAPI(etcdCli)
ctx = context.Background()
go func (w etcd.Watcher) {
var err error
var resp *etcd.Response
for {
resp, err = w.Next(ctx)
if err == nil {
Goose.Updater.Logf(3,"Updating config variable %s = %s",key,resp.Node.Value)
fn(resp.Node.Value)
} else {
Goose.Updater.Logf(1,"Error updating config variable %s (%s)",key,err)
}
}
}(kapi.Watcher("/" + key,nil))
}
func OnUpdateTree(etcdCli etcd.Client, key string, fn func(key string, val interface{}, action string), id ...interface{}) {
var kapi etcd.KeysAPI
var ctx context.Context
var sid string
if len(id) > 0 {
switch id[0].(type) {
case string:
sid = id[0].(string)
default:
Goose.Updater.Logf(2,"Ignoring wrong parameter type for key %s (%#v)",key,id[0])
return
}
}
if chkWatcher(key, sid) {
return
}
kapi = etcd.NewKeysAPI(etcdCli)
ctx = context.Background()
go func (w etcd.Watcher) {
var err error
var resp *etcd.Response
for {
resp, err = w.Next(ctx)
if err == nil {
Goose.Updater.Logf(3,"Updating (%s) config variable %s = %s", resp.Action, key, resp.Node.Value)
fn(resp.Node.Key, resp.Node.Value, resp.Action)
} else {
Goose.Updater.Logf(1,"Error updating config variable %s (%s)",key,err)
}
}
}(kapi.Watcher("/" + key,&etcd.WatcherOptions{Recursive:true}))
}
func init() {
keysWatched = map[string][]string{}
wreq = make(chan watchReq,1)
go watcherChecker(wreq)
}