forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.go
389 lines (346 loc) · 10.4 KB
/
run.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package run
import (
"archive/tar"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
"github.com/golang/glog"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"github.com/openshift/origin/pkg/oc/clusterup/docker/dockerhelper"
"github.com/openshift/origin/pkg/oc/clusterup/docker/errors"
)
type RunHelper struct {
client dockerhelper.Interface
dockerHelper *dockerhelper.Helper
}
func NewRunHelper(dockerHelper *dockerhelper.Helper) *RunHelper {
return &RunHelper{
client: dockerHelper.Client(),
dockerHelper: dockerHelper,
}
}
func (h *RunHelper) New() *Runner {
return &Runner{
client: h.client,
dockerHelper: h.dockerHelper,
config: &container.Config{},
hostConfig: &container.HostConfig{},
}
}
// Runner is a helper to run new containers on Docker
type Runner struct {
name string
client dockerhelper.Interface
dockerHelper *dockerhelper.Helper
config *container.Config
hostConfig *container.HostConfig
removeContainer bool
copies map[string][]byte
logDir string
logName string
}
// Name sets the name of the container to create
func (h *Runner) Name(name string) *Runner {
h.name = name
return h
}
// Image sets the image to run
func (h *Runner) Image(image string) *Runner {
h.config.Image = image
return h
}
func (h *Runner) PortForward(local, remote int) *Runner {
if h.hostConfig.PortBindings == nil {
h.hostConfig.PortBindings = nat.PortMap{}
}
containerPort := nat.Port(fmt.Sprintf("%d/tcp", remote))
binding := nat.PortBinding{
HostPort: fmt.Sprintf("%d", local),
}
h.hostConfig.PortBindings[containerPort] = []nat.PortBinding{binding}
if h.config.ExposedPorts == nil {
h.config.ExposedPorts = map[nat.Port]struct{}{}
}
h.config.ExposedPorts[containerPort] = struct{}{}
return h
}
// Entrypoint sets the entrypoint to use when running
func (h *Runner) Entrypoint(cmd ...string) *Runner {
h.config.Entrypoint = cmd
return h
}
// Command sets the command to run
func (h *Runner) Command(cmd ...string) *Runner {
h.config.Cmd = cmd
return h
}
func (h *Runner) Copy(contents map[string][]byte) *Runner {
h.copies = contents
return h
}
// HostPid tells Docker to run using the host's pid namespace
func (h *Runner) HostPid() *Runner {
h.hostConfig.PidMode = "host"
return h
}
// HostNetwork tells Docker to run using the host's Network namespace
func (h *Runner) HostNetwork() *Runner {
h.hostConfig.NetworkMode = "host"
return h
}
// Bind tells Docker to bind host dirs to container dirs
func (h *Runner) Bind(binds ...string) *Runner {
h.hostConfig.Binds = append(h.hostConfig.Binds, binds...)
return h
}
// Env tells Docker to add environment variables to the container getting started
func (h *Runner) Env(env ...string) *Runner {
h.config.Env = append(h.config.Env, env...)
return h
}
// Privileged tells Docker to run the container as privileged
func (h *Runner) Privileged() *Runner {
h.hostConfig.Privileged = true
return h
}
// SaveContainerLogs causes the helper to store the container stdout and stderr into a file
func (h *Runner) SaveContainerLogs(name, logdir string) *Runner {
h.logDir = logdir
h.logName = name
return h
}
// DiscardContainer if true will cause the container to be removed when done executing.
// Will be ignored in the case of Start
func (h *Runner) DiscardContainer() *Runner {
h.removeContainer = true
return h
}
// User sets the username or UID to use when running the container.
// Will be ignored if empty string
func (h *Runner) User(user string) *Runner {
if strings.TrimSpace(user) != "" {
h.config.User = user
}
return h
}
func (h *Runner) DNS(address ...string) *Runner {
h.hostConfig.DNS = address
return h
}
// Start starts the container as a daemon and returns
func (h *Runner) Start() (string, error) {
id, err := h.Create()
if err != nil {
return "", err
}
if err := h.copy(id); err != nil {
return id, err
}
return id, h.startContainer(id)
}
// Output starts the container, waits for it to finish and returns its output
func (h *Runner) Output() (string, string, string, int, error) {
return h.runWithOutput()
}
// Run executes the container and waits until it completes
func (h *Runner) Run() (string, int, error) {
containerId, _, _, rc, err := h.runWithOutput()
return containerId, rc, err
}
func (h *Runner) Create() (string, error) {
if h.hostConfig.Privileged {
userNsMode, err := h.dockerHelper.UserNamespaceEnabled()
if err != nil {
return "", err
}
if userNsMode {
h.hostConfig.UsernsMode = "host"
}
}
glog.V(4).Infof("Creating container named %q\nconfig:\n%s\nhost config:\n%s\n", h.name, printConfig(h.config), printHostConfig(h.hostConfig))
response, err := h.client.ContainerCreate(h.config, h.hostConfig, nil, h.name)
if err != nil {
return "", errors.NewError("cannot create container using image %s", h.config.Image).WithCause(err)
}
glog.V(5).Infof("Container created with id %q", response.ID)
if len(response.Warnings) > 0 {
glog.V(5).Infof("Warnings from container creation: %v", response.Warnings)
}
return response.ID, nil
}
func (h *Runner) copy(id string) error {
if len(h.copies) == 0 {
return nil
}
archive := streamingArchive(h.copies)
defer archive.Close()
err := h.client.CopyToContainer(id, "/", archive, types.CopyToContainerOptions{})
return err
}
// streamingArchive returns a ReadCloser containing a tar archive with contents serialized as files.
func streamingArchive(contents map[string][]byte) io.ReadCloser {
r, w := io.Pipe()
go func() {
archive := tar.NewWriter(w)
for k, v := range contents {
if err := archive.WriteHeader(&tar.Header{
Name: k,
Mode: 0644,
Size: int64(len(v)),
Typeflag: tar.TypeReg,
}); err != nil {
w.CloseWithError(err)
return
}
if _, err := archive.Write(v); err != nil {
w.CloseWithError(err)
return
}
}
archive.Close()
w.Close()
}()
return r
}
func (h *Runner) logContainerOutput(stdout, stderr string) error {
if err := os.MkdirAll(h.logDir, 0755); err != nil {
return err
}
stdoutFile := ""
stderrFile := ""
for spinNumber := 1; spinNumber < 1000; spinNumber++ {
stdoutFile = fmt.Sprintf("%s-%03d.stdout", strings.Replace(h.logName, "/", "-", -1), spinNumber)
stderrFile = fmt.Sprintf("%s-%03d.stderr", strings.Replace(h.logName, "/", "-", -1), spinNumber)
if _, err := os.Stat(path.Join(h.logDir, stdoutFile)); !os.IsNotExist(err) {
continue
}
if _, err := os.Stat(path.Join(h.logDir, stderrFile)); !os.IsNotExist(err) {
continue
}
break
}
stdoutErr := ioutil.WriteFile(path.Join(h.logDir, stdoutFile), []byte(stdout), 0644)
stderrErr := ioutil.WriteFile(path.Join(h.logDir, stderrFile), []byte(stderr), 0644)
switch {
case stdoutErr == nil && stderrErr == nil:
return nil
case stdoutErr != nil && stderrErr == nil:
return stdoutErr
case stdoutErr == nil && stderrErr != nil:
return stderrErr
default:
return utilerrors.NewAggregate([]error{stdoutErr, stderrErr})
}
}
func (h *Runner) startContainer(id string) error {
err := h.client.ContainerStart(id)
if err != nil {
return errors.NewError("cannot start container %s", id).WithCause(err)
}
return nil
}
func (h *Runner) runWithOutput() (string, string, string, int, error) {
id, err := h.Create()
if err != nil {
return "", "", "", 0, err
}
if h.removeContainer {
defer func() {
glog.V(5).Infof("Deleting container %q", id)
if err = h.client.ContainerRemove(id, types.ContainerRemoveOptions{}); err != nil {
glog.V(2).Infof("Error deleting container %q: %v", id, err)
}
}()
}
if err := h.copy(id); err != nil {
return id, "", "", 0, err
}
glog.V(5).Infof("Starting container %q", id)
err = h.startContainer(id)
if err != nil {
glog.V(2).Infof("Error occurred starting container %q: %v", id, err)
return id, "", "", 0, err
}
glog.V(5).Infof("Waiting for container %q", id)
rc, err := h.client.ContainerWait(id)
if err != nil {
glog.V(2).Infof("Error occurred waiting for container %q: %v", id, err)
return id, "", "", 0, err
}
glog.V(5).Infof("Done waiting for container %q, rc=%d", id, rc)
// changed to only reading logs after execution instead of streaming
// stdout/stderr to avoid race condition in (at least) docker 1.10-1.14-dev:
// https://github.com/docker/docker/issues/29285
glog.V(5).Infof("Reading logs from container %q", id)
stdOut := &bytes.Buffer{}
stdErr := &bytes.Buffer{}
err = h.client.ContainerLogs(id, types.ContainerLogsOptions{ShowStdout: true, ShowStderr: true}, stdOut, stdErr)
if err != nil {
glog.V(2).Infof("Error occurred while reading logs: %v", err)
return id, "", "", 0, err
}
glog.V(5).Infof("Done reading logs from container %q", id)
if len(h.logDir) > 0 {
if err := h.logContainerOutput(stdOut.String(), stdErr.String()); err != nil {
// This is an convenience problem, not fatal error.
glog.Warningf("Unable to preserve container %q logs: %v", h.logName, err)
}
}
glog.V(5).Infof("Stdout:\n%s", stdOut.String())
glog.V(5).Infof("Stderr:\n%s", stdErr.String())
if rc != 0 || err != nil {
return id, stdOut.String(), stdErr.String(), rc, newRunError(rc, err, stdOut.String(), stdErr.String(), h.config)
}
glog.V(4).Infof("Container run successful\n")
return id, stdOut.String(), stdErr.String(), rc, nil
}
// printConfig prints out the relevant parts of a container's Docker config
func printConfig(c *container.Config) string {
out := &bytes.Buffer{}
fmt.Fprintf(out, " image: %s\n", c.Image)
if len(c.Entrypoint) > 0 {
fmt.Fprintln(out, " entry point:")
for _, e := range c.Entrypoint {
fmt.Fprintf(out, " %s\n", e)
}
}
if len(c.Cmd) > 0 {
fmt.Fprintln(out, " command:")
for _, c := range c.Cmd {
fmt.Fprintf(out, " %s\n", c)
}
}
if len(c.Env) > 0 {
fmt.Fprintln(out, " environment:")
for _, e := range c.Env {
fmt.Fprintf(out, " %s\n", e)
}
}
return out.String()
}
func printHostConfig(c *container.HostConfig) string {
out := &bytes.Buffer{}
fmt.Fprintf(out, " pid mode: %s\n", c.PidMode)
fmt.Fprintf(out, " user mode: %s\n", c.UsernsMode)
fmt.Fprintf(out, " network mode: %s\n", c.NetworkMode)
if len(c.DNS) > 0 {
fmt.Fprintln(out, " DNS:")
for _, h := range c.DNS {
fmt.Fprintf(out, " %s\n", h)
}
}
if len(c.Binds) > 0 {
fmt.Fprintln(out, " volume binds:")
for _, b := range c.Binds {
fmt.Fprintf(out, " %s\n", b)
}
}
return out.String()
}