-
Notifications
You must be signed in to change notification settings - Fork 3
/
runfunc.go
77 lines (62 loc) · 1.91 KB
/
runfunc.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
package linuxgen
import (
"strings"
"github.com/blueprint-uservices/blueprint/blueprint/pkg/blueprint"
"github.com/blueprint-uservices/blueprint/blueprint/pkg/blueprint/stringutil"
"github.com/blueprint-uservices/blueprint/blueprint/pkg/ir"
)
/*
Used to generate bash run funcs that get gathered together into a single run.sh
Process nodes provide the bash command needed to execute the process; but that's
not enough. We need to make sure dependencies are correctly instantiated when needed.
This code provides a wrapper function implementation around the commands provided
by the process nodes
*/
func GenerateRunFunc(name string, runfunc string, deps ...ir.IRNode) (string, error) {
runfunc = getFuncBody(runfunc)
if runfunc == "" {
return "", blueprint.Errorf("invalid runfunc for process %v %v", name, runfunc)
}
templateArgs := runFuncTemplateArgs{
Name: name,
Dependencies: deps,
RunFuncBody: stringutil.Reindent(runfunc, 8),
}
return ExecuteTemplate("runfunc", runFuncTemplate, templateArgs)
}
func getFuncBody(runcmd string) string {
from := strings.Index(runcmd, "{") + 1
to := strings.LastIndex(runcmd, "}")
if from < to {
return runcmd[from:to]
} else {
return ""
}
}
type runFuncTemplateArgs struct {
Name string
Dependencies []ir.IRNode
RunFuncBody string
}
var runFuncTemplate = `{{RunFuncName .Name}}() {
cd $WORKSPACE_DIR
{{ range $i, $dep := .Dependencies -}}
{{Get $dep.Name}}
{{end -}}
run_{{RunFuncName .Name}}() {
{{.RunFuncBody}}
}
if run_{{RunFuncName .Name}}; then
if [ -z "${ {{- EnvVarName .Name}}+x}" ]; then
echo "${WORKSPACE_NAME} error starting {{.Name}}: function {{RunFuncName .Name}} did not set {{EnvVarName .Name}}"
return 1
else
echo "${WORKSPACE_NAME} started {{.Name}}"
return 0
fi
else
exitcode=$?
echo "${WORKSPACE_NAME} aborting {{.Name}} due to exitcode ${exitcode} from {{RunFuncName .Name}}"
return $exitcode
fi
}`