forked from hashicorp/consul-template
/
template_functions.go
635 lines (526 loc) · 14.7 KB
/
template_functions.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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
dep "github.com/hashicorp/consul-template/dependency"
yaml "gopkg.in/yaml.v2"
)
// now is function that represents the current time in UTC. This is here
// primarily for the tests to override times.
var now = func() time.Time { return time.Now().UTC() }
// datacentersFunc returns or accumulates datacenter dependencies.
func datacentersFunc(brain *Brain,
used, missing map[string]dep.Dependency) func(...string) ([]string, error) {
return func(s ...string) ([]string, error) {
result := make([]string, 0)
d, err := dep.ParseDatacenters(s...)
if err != nil {
return result, err
}
addDependency(used, d)
if value, ok := brain.Recall(d); ok {
return value.([]string), nil
}
addDependency(missing, d)
return result, nil
}
}
// fileFunc returns or accumulates file dependencies.
func fileFunc(brain *Brain,
used, missing map[string]dep.Dependency) func(string) (string, error) {
return func(s string) (string, error) {
if len(s) == 0 {
return "", nil
}
d, err := dep.ParseFile(s)
if err != nil {
return "", err
}
addDependency(used, d)
if value, ok := brain.Recall(d); ok {
if value == nil {
return "", nil
} else {
return value.(string), nil
}
}
addDependency(missing, d)
return "", nil
}
}
// keyFunc returns or accumulates key dependencies.
func keyFunc(brain *Brain,
used, missing map[string]dep.Dependency) func(string) (string, error) {
return func(s string) (string, error) {
if len(s) == 0 {
return "", nil
}
d, err := dep.ParseStoreKey(s)
if err != nil {
return "", err
}
addDependency(used, d)
if value, ok := brain.Recall(d); ok {
if value == nil {
return "", nil
} else {
return value.(string), nil
}
}
addDependency(missing, d)
return "", nil
}
}
// lsFunc returns or accumulates keyPrefix dependencies.
func lsFunc(brain *Brain,
used, missing map[string]dep.Dependency) func(string) ([]*dep.KeyPair, error) {
return func(s string) ([]*dep.KeyPair, error) {
result := make([]*dep.KeyPair, 0)
if len(s) == 0 {
return result, nil
}
d, err := dep.ParseStoreKeyPrefix(s)
if err != nil {
return result, err
}
addDependency(used, d)
// Only return non-empty top-level keys
if value, ok := brain.Recall(d); ok {
for _, pair := range value.([]*dep.KeyPair) {
if pair.Key != "" && !strings.Contains(pair.Key, "/") {
result = append(result, pair)
}
}
return result, nil
}
addDependency(missing, d)
return result, nil
}
}
func nodeFunc(brain *Brain,
used, missing map[string]dep.Dependency) func(...string) (*dep.NodeDetail, error) {
return func(s ...string) (*dep.NodeDetail, error) {
d, err := dep.ParseCatalogNode(s...)
if err != nil {
return nil, err
}
addDependency(used, d)
if value, ok := brain.Recall(d); ok {
return value.(*dep.NodeDetail), nil
}
addDependency(missing, d)
return nil, nil
}
}
// nodesFunc returns or accumulates catalog node dependencies.
func nodesFunc(brain *Brain,
used, missing map[string]dep.Dependency) func(...string) ([]*dep.Node, error) {
return func(s ...string) ([]*dep.Node, error) {
result := make([]*dep.Node, 0)
d, err := dep.ParseCatalogNodes(s...)
if err != nil {
return nil, err
}
addDependency(used, d)
if value, ok := brain.Recall(d); ok {
return value.([]*dep.Node), nil
}
addDependency(missing, d)
return result, nil
}
}
// serviceFunc returns or accumulates health service dependencies.
func serviceFunc(brain *Brain,
used, missing map[string]dep.Dependency) func(...string) ([]*dep.HealthService, error) {
return func(s ...string) ([]*dep.HealthService, error) {
result := make([]*dep.HealthService, 0)
if len(s) == 0 || s[0] == "" {
return result, nil
}
d, err := dep.ParseHealthServices(s...)
if err != nil {
return nil, err
}
addDependency(used, d)
if value, ok := brain.Recall(d); ok {
return value.([]*dep.HealthService), nil
}
addDependency(missing, d)
return result, nil
}
}
// servicesFunc returns or accumulates catalog services dependencies.
func servicesFunc(brain *Brain,
used, missing map[string]dep.Dependency) func(...string) ([]*dep.CatalogService, error) {
return func(s ...string) ([]*dep.CatalogService, error) {
result := make([]*dep.CatalogService, 0)
d, err := dep.ParseCatalogServices(s...)
if err != nil {
return nil, err
}
addDependency(used, d)
if value, ok := brain.Recall(d); ok {
return value.([]*dep.CatalogService), nil
}
addDependency(missing, d)
return result, nil
}
}
// treeFunc returns or accumulates keyPrefix dependencies.
func treeFunc(brain *Brain,
used, missing map[string]dep.Dependency) func(string) ([]*dep.KeyPair, error) {
return func(s string) ([]*dep.KeyPair, error) {
result := make([]*dep.KeyPair, 0)
if len(s) == 0 {
return result, nil
}
d, err := dep.ParseStoreKeyPrefix(s)
if err != nil {
return result, err
}
addDependency(used, d)
// Only return non-empty top-level keys
if value, ok := brain.Recall(d); ok {
for _, pair := range value.([]*dep.KeyPair) {
parts := strings.Split(pair.Key, "/")
if parts[len(parts)-1] != "" {
result = append(result, pair)
}
}
return result, nil
}
addDependency(missing, d)
return result, nil
}
}
// vaultFunc returns or accumulates secret dependencies.
func vaultFunc(brain *Brain,
used, missing map[string]dep.Dependency) func(string) (*dep.Secret, error) {
return func(s string) (*dep.Secret, error) {
result := &dep.Secret{}
if len(s) == 0 {
return result, nil
}
d, err := dep.ParseVaultSecret(s)
if err != nil {
return result, nil
}
addDependency(used, d)
if value, ok := brain.Recall(d); ok {
result = value.(*dep.Secret)
return result, nil
}
addDependency(missing, d)
return result, nil
}
}
// byKey accepts a slice of KV pairs and returns a map of the top-level
// key to all its subkeys. For example:
//
// elasticsearch/a //=> "1"
// elasticsearch/b //=> "2"
// redis/a/b //=> "3"
//
// Passing the result from Consul through byTag would yield:
//
// map[string]map[string]string{
// "elasticsearch": &dep.KeyPair{"a": "1"}, &dep.KeyPair{"b": "2"},
// "redis": &dep.KeyPair{"a/b": "3"}
// }
//
// Note that the top-most key is stripped from the Key value. Keys that have no
// prefix after stripping are removed from the list.
func byKey(pairs []*dep.KeyPair) (map[string]map[string]*dep.KeyPair, error) {
m := make(map[string]map[string]*dep.KeyPair)
for _, pair := range pairs {
parts := strings.Split(pair.Key, "/")
top := parts[0]
key := strings.Join(parts[1:], "/")
if key == "" {
// Do not add a key if it has no prefix after stripping.
continue
}
if _, ok := m[top]; !ok {
m[top] = make(map[string]*dep.KeyPair)
}
newPair := *pair
newPair.Key = key
m[top][key] = &newPair
}
return m, nil
}
// byTag is a template func that takes the provided services and
// produces a map based on Service tags.
//
// The map key is a string representing the service tag. The map value is a
// slice of Services which have the tag assigned.
func byTag(in interface{}) (map[string][]interface{}, error) {
m := make(map[string][]interface{})
switch typed := in.(type) {
case nil:
case []*dep.CatalogService:
for _, s := range typed {
for _, t := range s.Tags {
m[t] = append(m[t], s)
}
}
case []*dep.HealthService:
for _, s := range typed {
for _, t := range s.Tags {
m[t] = append(m[t], s)
}
}
default:
return nil, fmt.Errorf("byTag: wrong argument type %T", in)
}
return m, nil
}
// env returns the value of the environment variable set
func env(s string) (string, error) {
return os.Getenv(s), nil
}
// explode is used to expand a list of keypairs into a deeply-nested hash.
func explode(pairs []*dep.KeyPair) (map[string]interface{}, error) {
m := make(map[string]interface{})
for _, pair := range pairs {
explodeHelper(m, pair.Key, pair.Value)
}
return m, nil
}
// explodeHelper is a recursive helper for explode.
func explodeHelper(m map[string]interface{}, k, v string) {
if strings.Contains(k, "/") {
parts := strings.Split(k, "/")
top := parts[0]
key := strings.Join(parts[1:], "/")
if _, ok := m[top]; !ok {
m[top] = make(map[string]interface{})
}
explodeHelper(m[top].(map[string]interface{}), key, v)
} else {
if k != "" {
m[k] = v
}
}
}
// loop accepts varying parameters and differs its behavior. If given one
// parameter, loop will return a goroutine that begins at 0 and loops until the
// given int, increasing the index by 1 each iteration. If given two parameters,
// loop will return a goroutine that begins at the first parameter and loops
// up to but not including the second parameter.
//
// // Prints 0 1 2 3 4
// for _, i := range loop(5) {
// print(i)
// }
//
// // Prints 5 6 7
// for _, i := range loop(5, 8) {
// print(i)
// }
//
func loop(ints ...int) (<-chan int, error) {
var start, stop int
switch len(ints) {
case 1:
start, stop = 0, ints[0]
case 2:
start, stop = ints[0], ints[1]
default:
return nil, fmt.Errorf("loop: wrong number of arguments, expected 1 or 2"+
", but got %d", len(ints))
}
ch := make(chan int)
go func() {
for i := start; i < stop; i++ {
ch <- i
}
close(ch)
}()
return ch, nil
}
// join is a version of strings.Join that can be piped
func join(sep string, a []string) (string, error) {
return strings.Join(a, sep), nil
}
// parseBool parses a string into a boolean
func parseBool(s string) (bool, error) {
result, err := strconv.ParseBool(s)
if err != nil {
return false, fmt.Errorf("parseBool: %s", err)
}
return result, nil
}
// parseFloat parses a string into a base 10 float
func parseFloat(s string) (float64, error) {
result, err := strconv.ParseFloat(s, 10)
if err != nil {
return 0, fmt.Errorf("parseFloat: %s", err)
}
return result, nil
}
// parseInt parses a string into a base 10 int
func parseInt(s string) (int64, error) {
result, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return 0, fmt.Errorf("parseInt: %s", err)
}
return result, nil
}
// parseJSON returns a structure for valid JSON
func parseJSON(s string) (interface{}, error) {
if s == "" {
return make([]interface{}, 0), nil
}
var data interface{}
if err := json.Unmarshal([]byte(s), &data); err != nil {
return nil, err
}
return data, nil
}
// parseUint parses a string into a base 10 int
func parseUint(s string) (uint64, error) {
result, err := strconv.ParseUint(s, 10, 64)
if err != nil {
return 0, fmt.Errorf("parseUint: %s", err)
}
return result, nil
}
// plugin executes a subprocess as the given command string. It is assumed the
// resulting command returns JSON which is then parsed and returned as the
// value for use in the template.
func plugin(name string, args ...string) (string, error) {
if name == "" {
return "", nil
}
stdout, stderr := new(bytes.Buffer), new(bytes.Buffer)
// Strip and trim each arg or else some plugins get confused with the newline
// characters
jsons := make([]string, 0, len(args))
for _, arg := range args {
if v := strings.TrimSpace(arg); v != "" {
jsons = append(jsons, v)
}
}
cmd := exec.Command(name, jsons...)
cmd.Stdout = stdout
cmd.Stderr = stderr
if err := cmd.Start(); err != nil {
return "", fmt.Errorf("exec %q: %s\n\nstdout:\n\n%s\n\nstderr:\n\n%s",
name, err, stdout.Bytes(), stderr.Bytes())
}
done := make(chan error, 1)
go func() {
done <- cmd.Wait()
}()
select {
case <-time.After(5 * time.Second):
if cmd.Process != nil {
if err := cmd.Process.Kill(); err != nil {
return "", fmt.Errorf("exec %q: failed to kill", name)
}
}
<-done // Allow the goroutine to exit
return "", fmt.Errorf("exec %q: did not finish", name)
case err := <-done:
if err != nil {
return "", fmt.Errorf("exec %q: %s\n\nstdout:\n\n%s\n\nstderr:\n\n%s",
name, err, stdout.Bytes(), stderr.Bytes())
}
}
return strings.TrimSpace(stdout.String()), nil
}
// replaceAll replaces all occurrences of a value in a string with the given
// replacement value.
func replaceAll(f, t, s string) (string, error) {
return strings.Replace(s, f, t, -1), nil
}
// regexReplaceAll replaces all occurrences of a regular expression with
// the given replacement value.
func regexReplaceAll(re, pl, s string) (string, error) {
compiled, err := regexp.Compile(re)
if err != nil {
return "", err
}
return compiled.ReplaceAllString(s, pl), nil
}
// regexMatch returns true or false if the string matches
// the given regular expression
func regexMatch(re, s string) (bool, error) {
compiled, err := regexp.Compile(re)
if err != nil {
return false, err
}
return compiled.MatchString(s), nil
}
// split is a version of strings.Split that can be piped
func split(sep, s string) ([]string, error) {
s = strings.TrimSpace(s)
if s == "" {
return []string{}, nil
}
return strings.Split(s, sep), nil
}
// timestamp returns the current UNIX timestamp in UTC. If an argument is
// specified, it will be used to format the timestamp.
func timestamp(s ...string) (string, error) {
switch len(s) {
case 0:
return now().Format(time.RFC3339), nil
case 1:
return now().Format(s[0]), nil
default:
return "", fmt.Errorf("timestamp: wrong number of arguments, expected 0 or 1"+
", but got %d", len(s))
}
}
// toLower converts the given string (usually by a pipe) to lowercase.
func toLower(s string) (string, error) {
return strings.ToLower(s), nil
}
// toJSON converts the given structure into a deeply nested JSON string.
func toJSON(m map[string]interface{}) (string, error) {
result, err := json.Marshal(m)
if err != nil {
return "", fmt.Errorf("toJSON: %s", err)
}
return string(bytes.TrimSpace(result)), err
}
// toJSONPretty converts the given structure into a deeply nested pretty JSON
// string.
func toJSONPretty(m map[string]interface{}) (string, error) {
result, err := json.MarshalIndent(m, "", " ")
if err != nil {
return "", fmt.Errorf("toJSONPretty: %s", err)
}
return string(bytes.TrimSpace(result)), err
}
// toTitle converts the given string (usually by a pipe) to titlecase.
func toTitle(s string) (string, error) {
return strings.Title(s), nil
}
// toUpper converts the given string (usually by a pipe) to uppercase.
func toUpper(s string) (string, error) {
return strings.ToUpper(s), nil
}
// toYAML converts the given structure into a deeply nested YAML string.
func toYAML(m map[string]interface{}) (string, error) {
result, err := yaml.Marshal(m)
if err != nil {
return "", fmt.Errorf("toYAML: %s", err)
}
return string(bytes.TrimSpace(result)), nil
}
// addDependency adds the given Dependency to the map.
func addDependency(m map[string]dep.Dependency, d dep.Dependency) {
if _, ok := m[d.HashCode()]; !ok {
m[d.HashCode()] = d
}
}