This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
/
execute.go
99 lines (83 loc) · 1.99 KB
/
execute.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
package flow
import (
"fmt"
"time"
"github.com/debeando/lightflow/cli/args"
"github.com/debeando/lightflow/common"
"github.com/debeando/lightflow/common/log"
"github.com/debeando/lightflow/flow/execute"
"github.com/debeando/lightflow/flow/template"
)
// Execute is a method to render and execute command and save result into
// variables and then are interpreted to take other decisions.
func (f *Flow) Execute() {
cmd := f.renderCommand()
if f.when() {
f.Retry(func() {
f.unset()
f.wait()
f.execute(cmd)
f.mysql()
f.aws()
f.parse()
f.error()
f.print()
f.debug()
f.skip()
f.slack()
})
}
}
func (f *Flow) renderCommand() string {
var cmd = f.GetProperty("Execute")
var vars = f.Variables.GetItems()
// Find unknown variables:
for _, variable := range template.Variables(cmd) {
if f.Variables.Exist(variable) == false {
log.Warning(fmt.Sprintf("Register empty variable: %s", variable), nil)
f.Variables.Set(map[string]interface{}{variable: ""})
}
}
// Find template variables to render:
for variable, value := range f.Variables.Items {
value_template := template.Variables(common.InterfaceToString(value))
if len(value_template) > 0 {
cmd, err := template.Render(common.InterfaceToString(value), vars)
if err != nil {
log.Warning(err.Error(), nil)
}
vars[variable] = cmd
}
}
// Render template:
cmd, err := template.Render(cmd, vars)
if err != nil {
log.Warning(err.Error(), nil)
}
return common.TrimNewlines(cmd)
}
func (f *Flow) execute(cmd string) {
if len(cmd) == 0 {
return
}
log.Debug(
fmt.Sprintf(
"%s/%s/%s",
f.TaskName(),
f.SubTaskName(),
f.PipeName(),
),
map[string]interface{}{
"Execute": cmd,
},
)
stdout, exitCode := execute.Execute(cmd, args.DryRun())
f.Variables.Set(map[string]interface{}{
"exit_code": exitCode,
"stdout": stdout,
})
}
func (f *Flow) wait() {
wait := f.Config.Tasks[f.Index.Task].Pipes[f.Index.Pipe].Wait
time.Sleep(time.Duration(wait) * time.Second)
}