/
execution.go
171 lines (147 loc) · 3.95 KB
/
execution.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package engine
import (
"bytes"
"fmt"
"strings"
"time"
docker "github.com/fsouza/go-dockerclient"
)
var (
endpoint = "unix:///var/run/docker.sock"
tempdir = "/tmp"
)
// ExecutionResult contains the results from one step of code execution --
// either compilation or running.
type ExecutionResult struct {
// The exit status code of the process. 0 is success, anything else is failure.
ExitCode int
// The output streams.
Stdout, Stderr string
// How long this step ran.
RunTime time.Duration
// If an error occured, this error string will be non-empty.
ErrorString string
}
type execution struct {
step string
command []string
srcDir string
dockerImage string
apparmorProfile string
client *docker.Client
container *docker.Container
sentinel chan struct{}
result *ExecutionResult
}
// Initialize a new exeuction object for use.
func newExecution(step string, command []string, srcDir, dockerImage, apparmorProfile string) (exe *execution, err error) {
exe = &execution{
step: step,
command: command,
srcDir: srcDir,
dockerImage: dockerImage,
apparmorProfile: apparmorProfile,
sentinel: make(chan struct{}),
result: &ExecutionResult{},
}
exe.client, err = docker.NewClient(endpoint)
return
}
// Run the execution with the given options.
func (exe *execution) run(opts *RunOptions) (result *ExecutionResult, err error) {
result = exe.result
timeout := false
defer exe.cleanup()
err = exe.createContainer()
if err == nil {
startTime := time.Now()
runResult := exe.attachAndRun(opts.Stdin)
select {
case err = <-runResult:
// pass
case <-time.After(time.Duration(opts.Timeout) * time.Second):
timeout = true
}
result.RunTime = time.Now().Sub(startTime)
}
if timeout {
result.ErrorString = fmt.Sprintf("%s_timelimit", exe.step)
result.ExitCode = -9
} else if result.ExitCode != 0 {
result.ErrorString = fmt.Sprintf("%s_error", exe.step)
}
return
}
func (exe *execution) createContainer() (err error) {
exe.container, err = exe.client.CreateContainer(docker.CreateContainerOptions{
Config: &docker.Config{
Image: exe.dockerImage,
Cmd: exe.command,
OpenStdin: true,
StdinOnce: true,
},
})
return
}
func (exe *execution) attachAndRun(stdin string) chan error {
sentinel := make(chan struct{})
runResult := make(chan error)
attachResult := make(chan error)
finalResult := make(chan error)
// run goroutine
go func() {
_ = <-sentinel
securityOpt := []string{}
if exe.apparmorProfile != "" {
securityOpt = append(securityOpt, fmt.Sprintf("apparmor:%s", exe.apparmorProfile))
}
// when we get the sentinel, we know we've attached in the other goroutine
err := exe.client.StartContainer(exe.container.ID, &docker.HostConfig{
Binds: []string{fmt.Sprintf("%s:/src", exe.srcDir)},
SecurityOpt: securityOpt,
})
sentinel <- struct{}{}
if err == nil {
exe.result.ExitCode, err = exe.client.WaitContainer(exe.container.ID)
}
runResult <- err
}()
// attach goroutine
go func() {
stdinReader := strings.NewReader(stdin)
var stdout, stderr bytes.Buffer
err := exe.client.AttachToContainer(docker.AttachToContainerOptions{
Container: exe.container.ID,
InputStream: stdinReader,
OutputStream: &stdout,
ErrorStream: &stderr,
Stream: true,
Stdin: true,
Stdout: true,
Stderr: true,
Success: sentinel,
})
if err == nil {
exe.result.Stdout = stdout.String()
exe.result.Stderr = stderr.String()
}
attachResult <- err
}()
go func() {
finalResult <- firstError(<-runResult, <-attachResult)
}()
return finalResult
}
func (exe *execution) cleanup() {
if exe.container != nil {
exe.client.RemoveContainer(docker.RemoveContainerOptions{ID: exe.container.ID, Force: true})
}
}
func firstError(errs ...error) error {
for _, err := range errs {
if err != nil {
return err
}
}
return nil
}