-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocker.go
More file actions
255 lines (219 loc) · 6.76 KB
/
docker.go
File metadata and controls
255 lines (219 loc) · 6.76 KB
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
package containerapi
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
"time"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
log "github.com/sirupsen/logrus"
)
type DockerProvider struct {
client *client.Client
}
func (d DockerProvider) Create(ctx context.Context, cfg *ContainerConfig) (string, error) {
dockerEnv := make([]string, 0, len(cfg.env))
for k, v := range cfg.env {
dockerEnv = append(dockerEnv, fmt.Sprintf("%s=%s", k, v))
}
_, _, err := d.client.ImageInspectWithRaw(ctx, cfg.image)
if cfg.pullConfig != nil && cfg.pullConfig(cfg.image, err == nil) {
log.WithField("image", cfg.image).Debug("checking image registry")
reader, err := d.client.ImagePull(ctx, cfg.image, image.PullOptions{})
if err != nil {
return "", fmt.Errorf("failed to pull image: %w", err)
}
defer reader.Close()
downloaded, err := consumePullStream(reader)
if err != nil {
return "", fmt.Errorf("failed to pull image: %w", err)
}
if downloaded {
log.WithField("image", cfg.image).Info("pulled image")
} else {
log.WithField("image", cfg.image).Debug("image already up to date")
}
}
mounts := make([]mount.Mount, len(cfg.mounts))
for i, b := range cfg.mounts {
mounts[i] = mount.Mount{
Type: mount.TypeBind,
Source: b.HostPath,
Target: b.ContainerPath,
ReadOnly: b.ReadOnly,
}
}
timeout := 0
config := &container.Config{
Image: cfg.image,
Env: dockerEnv,
Cmd: cfg.command,
User: cfg.user,
// In case we pass a 0 value to the stop API, this timeout will be used.
// Setting this to 0 means: send SIGKILL immediately
StopTimeout: &timeout,
}
servers := make([]string, len(cfg.dnsServers))
for i, server := range cfg.dnsServers {
servers[i] = server.String()
}
extraHosts := make([]string, len(cfg.extraHosts))
for i := range extraHosts {
extraHosts[i] = fmt.Sprintf("%s:%s", cfg.extraHosts[i].HostName, cfg.extraHosts[i].IP)
}
// Disables SELinux label confinement
// Otherwise, systems using it might have permission issues with bind mounts
securityOpt := []string{"label=disable"}
hostConfig := &container.HostConfig{
NetworkMode: "host",
Mounts: mounts,
DNS: servers,
DNSSearch: cfg.dnsSearchDomains,
ExtraHosts: extraHosts,
SecurityOpt: securityOpt,
}
resp, err := d.client.ContainerCreate(ctx, config, hostConfig, nil, nil, cfg.name)
if err != nil {
return "", fmt.Errorf("failed to create docker container: %w", err)
}
return resp.ID, nil
}
// consumePullStream drains the JSON event stream returned by ImagePull and reports
// whether new layers were actually downloaded. The Docker engine emits a terminal
// "Status:" message of the form "Downloaded newer image for X" when layers were
// fetched, or "Image is up to date for X" when the local cache was already current.
func consumePullStream(reader io.Reader) (bool, error) {
decoder := json.NewDecoder(reader)
downloaded := false
for {
var msg struct {
Status string `json:"status"`
Error string `json:"error"`
ErrorDetail struct {
Message string `json:"message"`
} `json:"errorDetail"`
}
if err := decoder.Decode(&msg); err != nil {
if errors.Is(err, io.EOF) {
return downloaded, nil
}
return downloaded, err
}
if msg.Error != "" {
return downloaded, errors.New(msg.Error)
}
if msg.ErrorDetail.Message != "" {
return downloaded, errors.New(msg.ErrorDetail.Message)
}
if strings.HasPrefix(msg.Status, "Status: Downloaded ") {
downloaded = true
}
}
}
func (d DockerProvider) Remove(ctx context.Context, containerID string) error {
return d.client.ContainerRemove(ctx, containerID, container.RemoveOptions{})
}
func (d DockerProvider) Start(ctx context.Context, containerID string) error {
return d.client.ContainerStart(ctx, containerID, container.StartOptions{})
}
func (d DockerProvider) Stop(ctx context.Context, containerID string, timeout *time.Duration) error {
var t *int
if timeout != nil {
intTimeout := int(timeout.Seconds())
t = &intTimeout
}
return d.client.ContainerStop(ctx, containerID, container.StopOptions{Timeout: t})
}
func (d DockerProvider) Wait(ctx context.Context, containerID string) (<-chan int64, <-chan error) {
msgChan := make(chan int64)
errChan := make(chan error)
message, err := d.client.ContainerWait(ctx, containerID, container.WaitConditionNextExit)
go func() {
defer close(msgChan)
defer close(errChan)
select {
case <-ctx.Done():
errChan <- ctx.Err()
case e := <-err:
errChan <- e
case msg := <-message:
if msg.Error != nil {
errChan <- fmt.Errorf("error waiting on container end: %s", msg.Error.Message)
return
}
msgChan <- msg.StatusCode
}
}()
return msgChan, errChan
}
func (d DockerProvider) Logs(ctx context.Context, containerID string) (io.ReadCloser, io.ReadCloser, error) {
options := container.LogsOptions{
Follow: true,
ShowStdout: true,
ShowStderr: true,
}
combined, err := d.client.ContainerLogs(ctx, containerID, options)
if err != nil {
return nil, nil, fmt.Errorf("failed to get logs from docker: %w", err)
}
readOut, writeOut, err := os.Pipe()
if err != nil {
return nil, nil, fmt.Errorf("failed to create stdout pipe: %w", err)
}
readErr, writeErr, err := os.Pipe()
if err != nil {
return nil, nil, fmt.Errorf("failed to create stderr pipe: %w", err)
}
go func() {
defer writeOut.Close()
defer writeErr.Close()
defer combined.Close()
_, err := stdcopy.StdCopy(writeOut, writeErr, combined)
if err != nil {
log.WithField("err", err).Warn("failed to copy logs content to pipes")
}
}()
return readOut, readErr, nil
}
func (d DockerProvider) CopyFrom(ctx context.Context, container, source, dest string) error {
readTar, _, err := d.client.CopyFromContainer(ctx, container, source)
if err != nil {
return fmt.Errorf("failed to copy files from container: %w", err)
}
tar := exec.CommandContext(ctx, "tar", "-x", "-C", dest, "-f", "-")
tar.Stdin = readTar
tar.Stdout = os.Stdout
tar.Stderr = os.Stderr
err = tar.Run()
if err != nil {
return fmt.Errorf("failed to extract tar archive: %w", err)
}
return nil
}
func (d DockerProvider) Command() string {
return "docker"
}
func (d DockerProvider) Close() error {
return d.client.Close()
}
func NewDockerProvider(ctx context.Context) (ContainerProvider, error) {
apiclient, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return nil, fmt.Errorf("could not connect to Docker: %w", err)
}
_, err = apiclient.ServerVersion(ctx)
if err != nil {
return nil, fmt.Errorf("connection check failed: %w", err)
}
return &DockerProvider{
client: apiclient,
}, nil
}