-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathscript.go
96 lines (86 loc) · 2.35 KB
/
script.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
package collector
import (
"errors"
"strconv"
except "github.com/banyanops/collector/except"
blog "github.com/ccpaging/log4go"
)
// Script is the common interface to run sripts inside a container
type Script interface {
//We expect YAML output from scripts that needs parsing of output by Banyan service
Run(imageID ImageIDType) ([]byte, error)
Name() string
}
// Script info for all types (e.g., bash, python, etc.)
type ScriptInfo struct {
name string
dirPath string
params []string
staticBinary string
}
// Create a new bash script
func newBashScript(scriptName string, path string, params []string) Script {
return &ScriptInfo{
name: scriptName,
dirPath: path,
params: params,
staticBinary: "bash-static",
}
}
// Create a new python script
func newPythonScript(scriptName string, path string, params []string) Script {
return &ScriptInfo{
name: scriptName,
dirPath: path,
params: params,
staticBinary: "python-static",
}
}
// Run handles running of a script inside an image
func (sh ScriptInfo) Run(imageID ImageIDType) (b []byte, err error) {
jsonString, err := createCmd(imageID, sh.name, sh.staticBinary, sh.dirPath)
if err != nil {
except.Error(err, ": Error in creating command")
return
}
blog.Debug("Container spec: %s", string(jsonString))
containerID, err := CreateContainer(jsonString)
if err != nil {
except.Error(err, ": Error in creating container")
return
}
blog.Debug("New container ID: %s", containerID)
defer RemoveContainer(containerID)
jsonString, err = StartContainer(containerID)
if err != nil {
except.Error(err, ": Error in starting container")
return
}
blog.Debug("Response from StartContainer: %s", string(jsonString))
statusCode, err := WaitContainer(containerID)
if err != nil {
except.Error(err, ": Error in waiting for container to stop")
return
}
if statusCode != 0 {
err = errors.New("Bash script exit status: " + strconv.Itoa(statusCode))
return
}
b, err = LogsContainer(containerID)
if err != nil {
except.Error(err, ":Error in extracting output from container")
return
}
/*
_, err = removeContainer(containerID)
if err != nil {
except.Error(err, ":Error in removing container for image", containerID)
return
}
*/
return
}
// Name gives the name of the script
func (sh ScriptInfo) Name() string {
return sh.name
}