-
Notifications
You must be signed in to change notification settings - Fork 6
/
executor.go
123 lines (110 loc) · 3.08 KB
/
executor.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
package main
import (
"os/exec"
"strings"
)
// An Executor is responsible for executed to perform some action when a
// watched key is changed.
type Executor interface {
// Return the unique name of the executor.
Name() string
// Called to run the executor's actions.
Execute(client Client) error
}
// A Executor performs template rendering. It will optionally execute a command
// when one or more changes are made by the templating system. If no templates
// are provided the command will always be executed.
type TemplateExecutor struct {
name string
prefix string
context []string
Templates []Template
Command []string
}
// Render the templates. Return true if any templates changed.
func (ex *TemplateExecutor) render(context interface{}) (changed bool, err error) {
var oneChanged bool
if ex.Templates == nil || len(ex.Templates) == 0 {
logger.Debugf("%s: no templates to render", ex.name)
changed = true
return
}
logger.Debugf("%s: context %+v", ex.name, context)
for _, tpl := range ex.Templates {
if oneChanged, err = tpl.Render(context); err != nil {
return
}
if oneChanged {
logger.Debugf("%s: rendered '%s' -> '%s'", ex.name, tpl.Src, tpl.Dest)
} else {
logger.Debugf("%s: no change to '%s'", ex.name, tpl.Dest)
}
changed = changed || oneChanged
}
return
}
// Run the command.
func (ex *TemplateExecutor) run() error {
if len(ex.Command) == 0 {
logger.Debugf("%s: no command to call", ex.name)
return nil
}
cmdName := ex.Command[0]
cmdArgs := ex.Command[1:]
command := exec.Command(cmdName, cmdArgs...)
out, err := command.CombinedOutput()
if err == nil {
logger.Debugf("%s: command %v ran", ex.name, ex.Command)
} else {
logger.Errorf("%s: command %v failed: %s", ex.name, ex.Command, err)
}
outStr := string(out)
if outStr != "" {
lines := strings.Split(outStr, "\n")
for _, line := range lines {
if err == nil {
logger.Debugf("%s> %s", ex.name, line)
} else {
logger.Errorf("%s> %s", ex.name, line)
}
}
}
return err
}
// Return the unique name of the executor.
func (ex *TemplateExecutor) Name() string {
return ex.name
}
// Render the templates using the context retrieved from the provided `client`
// and execute the command. The command will be executed if one of the template
// destinations changes or no templates are present in the Watcher.
func (ex *TemplateExecutor) Execute(client Client) error {
var err error
var context interface{}
logger.Debugf("%s: executing", ex.name)
if ex.context == nil || len(ex.context) == 0 {
context = map[string]interface{}{}
} else if context, err = client.Get(ex.context); err != nil {
logger.Errorf("%s: context get failed: %s", ex.name, err)
return err
} else {
for _, key := range strings.Split(ex.prefix, "/") {
if contextMap, ok := context.(map[string]interface{}); ok {
context, ok = contextMap[getKeyName(key)]
if !ok {
context = map[string]interface{}{}
break
}
} else {
context = map[string]interface{}{}
break
}
}
}
run := true
run, err = ex.render(context)
if run && err == nil {
err = ex.run()
}
return err
}