This repository has been archived by the owner on Feb 27, 2020. It is now read-only.
/
sandbox.go
281 lines (251 loc) · 8.21 KB
/
sandbox.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// +build linux
package dockerengine
import (
"context"
"fmt"
"time"
docker "github.com/fsouza/go-dockerclient"
"github.com/pkg/errors"
"github.com/taskcluster/taskcluster-worker/engines"
"github.com/taskcluster/taskcluster-worker/engines/docker/imagecache"
"github.com/taskcluster/taskcluster-worker/engines/docker/network"
"github.com/taskcluster/taskcluster-worker/runtime"
"github.com/taskcluster/taskcluster-worker/runtime/atomics"
"github.com/taskcluster/taskcluster-worker/runtime/ioext"
)
const dockerEngineKillTimeout = 5 * time.Second
type sandbox struct {
engines.SandboxBase
monitor runtime.Monitor
containerID string
resultSet engines.ResultSet
resultErr error
abortErr error
storage runtime.TemporaryFolder
resolve atomics.Once
docker *docker.Client
taskCtx *runtime.TaskContext
networkHandle *network.Handle
imageHandle *imagecache.ImageHandle
}
func newSandbox(sb *sandboxBuilder) (*sandbox, error) {
monitor := sb.monitor.WithTag("struct", "sandbox")
// Take the imageHandle, remember to release it, if we return an error
imageHandle := sb.imageHandle
sb.imageHandle = nil
// Get an isolated network, forwarding requests to gateway to proxyMux
networkHandle, err := sb.e.networks.GetNetwork(&proxyMux{
Proxies: sb.proxies,
TaskContext: sb.taskCtx,
})
if err != nil {
imageHandle.Release()
// Any error here is a fatal error
return nil, errors.Wrap(err, "docker.CreateNetwork failed")
}
// Create the container
container, err := sb.e.docker.CreateContainer(docker.CreateContainerOptions{
Config: &docker.Config{
Cmd: sb.payload.Command,
Image: imageHandle.ImageName,
Env: *sb.env,
AttachStdout: true,
AttachStderr: true,
Labels: map[string]string{
"taskId": sb.taskCtx.TaskID,
},
},
HostConfig: &docker.HostConfig{
Privileged: sb.payload.Privileged,
// gateway IP is also the host machine that we're listening for requests
// to the proxies added to proxyMux above..
ExtraHosts: []string{fmt.Sprintf("taskcluster:%s", networkHandle.Gateway())},
Mounts: sb.mounts,
},
NetworkingConfig: &docker.NetworkingConfig{
EndpointsConfig: map[string]*docker.EndpointConfig{
networkHandle.NetworkID(): {},
},
},
})
if err != nil {
imageHandle.Release()
return nil, runtime.NewMalformedPayloadError(
"could not create container: " + err.Error())
}
// create a temporary storage for use by resultSet
storage, err := sb.e.Environment.TemporaryStorage.NewFolder()
if err != nil {
imageHandle.Release()
monitor.ReportError(err, "failed to create temporary folder")
return nil, runtime.ErrFatalInternalError
}
s := &sandbox{
containerID: container.ID,
storage: storage,
docker: sb.e.docker,
taskCtx: sb.taskCtx,
networkHandle: networkHandle,
imageHandle: imageHandle,
monitor: monitor.WithTags(map[string]string{
"containerId": container.ID,
"networkId": networkHandle.NetworkID(),
}),
}
// attach to the container before starting so that we get all the logs
_, err = s.docker.AttachToContainerNonBlocking(docker.AttachToContainerOptions{
Container: container.ID,
OutputStream: ioext.WriteNopCloser(s.taskCtx.LogDrain()), // TODO: wait for close() before resolving task in s.wait()
Logs: true,
Stdout: true,
Stderr: true,
Stream: true,
})
if err != nil {
imageHandle.Release()
return nil, errors.Wrap(err, "docker.AttachToContainerNonBlocking() failed")
}
// HostConfig is ignored by the remote API and is only kept for
// backward compatibility.
err = s.docker.StartContainer(s.containerID, &docker.HostConfig{})
if err != nil {
imageHandle.Release()
return nil, errors.Wrap(err, "docker.StartContainer failed")
}
go s.wait()
return s, nil
}
func (s *sandbox) WaitForResult() (engines.ResultSet, error) {
s.resolve.Wait()
return s.resultSet, s.resultErr
}
func (s *sandbox) wait() {
exitCode, err := s.docker.WaitContainer(s.containerID)
s.resolve.Do(func() {
if err != nil {
incidentID := s.monitor.ReportError(err, "docker.WaitContainer failed")
s.taskCtx.LogError("internal error waiting for container, incidentId:", incidentID)
s.resultErr = runtime.ErrNonFatalInternalError
s.abortErr = engines.ErrSandboxTerminated
return
}
s.resultSet = &resultSet{
success: exitCode == 0,
containerID: s.containerID,
docker: s.docker,
monitor: s.monitor.WithTag("struct", "resultSet"),
storage: s.storage,
context: s.taskCtx,
networkHandle: s.networkHandle,
imageHandle: s.imageHandle,
}
s.abortErr = engines.ErrSandboxTerminated
})
}
func (s *sandbox) Kill() error {
s.resolve.Do(func() {
debug("Sandbox.Kill() for containerId: %s", s.containerID)
s.resultErr = s.attemptGracefulTermination()
// Create resultSet
if s.resultErr == nil {
s.resultSet = &resultSet{
success: false,
containerID: s.containerID,
docker: s.docker,
monitor: s.monitor.WithTag("struct", "resultSet"),
storage: s.storage,
context: s.taskCtx,
networkHandle: s.networkHandle,
imageHandle: s.imageHandle,
}
s.abortErr = engines.ErrSandboxTerminated
} else {
s.dispose()
}
})
s.resolve.Wait()
return s.resultErr
}
func (s *sandbox) Abort() error {
s.resolve.Do(func() {
debug("Sandbox.Abort() for containerId: %s", s.containerID)
s.attemptGracefulTermination()
s.abortErr = s.dispose()
s.resultErr = engines.ErrSandboxAborted
})
s.resolve.Wait()
return s.abortErr
}
// attemptGracefulTermination will attempt a graceful termination of the
// container and ignore ContainerNotRunning errors.
func (s *sandbox) attemptGracefulTermination() error {
hasErr := false
// Send SIGTERM and give the container 30s to exit
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel() // always free the context
err := s.docker.KillContainer(docker.KillContainerOptions{
ID: s.containerID,
Signal: docker.SIGTERM,
Context: ctx,
})
// Report error if not ContainerNotRunning and ctx was not timed out
if _, ok := err.(*docker.ContainerNotRunning); err != nil && !ok && ctx.Err() == nil {
s.monitor.ReportError(err, "KillContainer with SIGTERM failed")
// signal up the stack that something went wrong, this is not a successful kill
hasErr = true
}
// Send SIGTERM and give docker 5 minutes to kill the container
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel() // always free the context
err = s.docker.KillContainer(docker.KillContainerOptions{
ID: s.containerID,
Signal: docker.SIGKILL,
Context: ctx,
})
// Report errors other than ContainerNotRunning
if _, ok := err.(*docker.ContainerNotRunning); err != nil && !ok {
s.monitor.ReportError(err, "KillContainer with SIGTERM failed")
hasErr = true
}
// Wait for container to exit
_, err = s.docker.WaitContainer(s.containerID)
// Report errors other than ContainerNotRunning
if _, ok := err.(*docker.ContainerNotRunning); err != nil && !ok {
s.monitor.ReportError(err, "WaitContainer failed")
hasErr = true
}
// If ErrNonFatalInternalError if there was an error of any kind, since all
// errors here are not really fatal.
if hasErr {
return runtime.ErrNonFatalInternalError
}
return nil
}
// free all resources held by this sandbox
func (s *sandbox) dispose() error {
hasErr := false
// Remove the container
err := s.docker.RemoveContainer(docker.RemoveContainerOptions{
ID: s.containerID,
Force: true, // Kill anything still running in the container
RemoveVolumes: true, // Remove any volumes automatically created with the container (VOLUME in docker image)
})
if err != nil {
s.monitor.ReportError(err, "failed to remove container in disposal of sandbox")
hasErr = true
}
// Remove temporary storage
if err = s.storage.Remove(); err != nil {
s.monitor.ReportError(err, "failed to remove temporary storage in disposal of sandbox")
hasErr = true
}
// Release the network
s.networkHandle.Release()
// Release image handle
s.imageHandle.Release()
// If ErrNonFatalInternalError if there was an error of any kind
if hasErr {
return runtime.ErrNonFatalInternalError
}
return nil
}