-
Notifications
You must be signed in to change notification settings - Fork 1
/
deployable.go
109 lines (92 loc) · 2.47 KB
/
deployable.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
package main
import (
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
varPattern = regexp.MustCompile(`\(\(([a-z]+):([a-zA-Z0-9_\-]+)\)\)`)
allowedEnvvars = map[string]bool{
"HOME": true,
"LANG": true,
"PATH": true,
"SHELL": true,
"USER": true,
"XDG_RUNTIME_DIR": true,
}
)
// https://stackoverflow.com/a/35247204
func replaceAllGroupFunc(re *regexp.Regexp, str string, repl func([]string) string) string {
result := ""
lastIndex := 0
for _, v := range re.FindAllSubmatchIndex([]byte(str), -1) {
groups := []string{}
for i := 0; i < len(v); i += 2 {
groups = append(groups, str[v[i]:v[i+1]])
}
result += str[lastIndex:v[0]] + repl(groups)
lastIndex = v[1]
}
return result + str[lastIndex:]
}
func replaceVars(raw string, data map[string]string, actionEnv map[string]string) string {
return replaceAllGroupFunc(varPattern, raw, func(groups []string) string {
vtype := strings.ToLower(groups[1])
key := groups[2]
switch vtype {
case "data":
if v, ok := data[key]; ok {
return v
}
return ""
case "env":
if v, ok := actionEnv[key]; ok {
return v
}
if _, ok := allowedEnvvars[key]; ok {
return os.Getenv(key)
}
}
return ""
})
}
func processDeployable(deploy Deployable, data map[string]string) (err error) {
for idx, action := range deploy.Actions {
idx := idx
if len(action.Command) == 0 {
err = fmt.Errorf("action %d has empty command array", idx)
return
}
workdir := replaceVars(action.WorkDir, data, action.Env)
argv := []string{}
for _, arg := range action.Command {
argv = append(argv, replaceVars(arg, data, action.Env))
}
args := []string{}
if len(argv) > 1 {
args = argv[1:]
}
cmd := exec.Command(argv[0], args...)
cmd.Dir = workdir
cmd.Env = os.Environ()
for key, value := range action.Env {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", key, replaceVars(value, data, action.Env)))
}
cmd.Stderr = NewZapWriter(zapcore.WarnLevel, "stderr", zap.Int("idx", idx))
cmd.Stdout = NewZapWriter(zapcore.InfoLevel, "stdout", zap.Int("idx", idx))
zap.L().Debug("processing action", zap.Int("idx", idx), zap.Strings("argv", argv))
if err = cmd.Start(); err != nil {
err = fmt.Errorf("action %d command failed to execute: %w", idx, err)
return
}
if err = cmd.Wait(); err != nil {
err = fmt.Errorf("action %d command failed: %w", idx, err)
return
}
}
return
}