This repository has been archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
docker_driver.go
265 lines (238 loc) · 7.06 KB
/
docker_driver.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
package driver
import (
"archive/tar"
"context"
"fmt"
"io"
"io/ioutil"
"os"
unix_path "path"
"github.com/docker/cli/cli/command"
cliflags "github.com/docker/cli/cli/flags"
"github.com/docker/distribution/reference"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/strslice"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/stdcopy"
"github.com/docker/docker/registry"
)
// DockerDriver is capable of running Docker invocation images using Docker itself.
type DockerDriver struct {
config map[string]string
// If true, this will not actually run Docker
Simulate bool
dockerCli command.Cli
dockerConfigurationOptions []DockerConfigurationOption
containerOut io.Writer
containerErr io.Writer
}
// Run executes the Docker driver
func (d *DockerDriver) Run(op *Operation) error {
return d.exec(op)
}
// Handles indicates that the Docker driver supports "docker" and "oci"
func (d *DockerDriver) Handles(dt string) bool {
return dt == ImageTypeDocker || dt == ImageTypeOCI
}
// AddConfigurationOptions adds configuration callbacks to the driver
func (d *DockerDriver) AddConfigurationOptions(opts ...DockerConfigurationOption) {
d.dockerConfigurationOptions = append(d.dockerConfigurationOptions, opts...)
}
// Config returns the Docker driver configuration options
func (d *DockerDriver) Config() map[string]string {
return map[string]string{
"VERBOSE": "Increase verbosity. true, false are supported values",
"PULL_ALWAYS": "Always pull image, even if locally available (0|1)",
"DOCKER_DRIVER_QUIET": "Make the Docker driver quiet (only print container stdout/stderr)",
}
}
// SetConfig sets Docker driver configuration
func (d *DockerDriver) SetConfig(settings map[string]string) {
d.config = settings
}
// SetDockerCli makes the driver use an already initialized cli
func (d *DockerDriver) SetDockerCli(dockerCli command.Cli) {
d.dockerCli = dockerCli
}
// SetContainerOut sets the container output stream
func (d *DockerDriver) SetContainerOut(w io.Writer) {
d.containerOut = w
}
// SetContainerErr sets the container error stream
func (d *DockerDriver) SetContainerErr(w io.Writer) {
d.containerErr = w
}
func pullImage(ctx context.Context, cli command.Cli, image string) error {
ref, err := reference.ParseNormalizedNamed(image)
if err != nil {
return err
}
// Resolve the Repository name from fqn to RepositoryInfo
repoInfo, err := registry.ParseRepositoryInfo(ref)
if err != nil {
return err
}
authConfig := command.ResolveAuthConfig(ctx, cli, repoInfo.Index)
encodedAuth, err := command.EncodeAuthToBase64(authConfig)
if err != nil {
return err
}
options := types.ImagePullOptions{
RegistryAuth: encodedAuth,
}
responseBody, err := cli.Client().ImagePull(ctx, image, options)
if err != nil {
return err
}
defer responseBody.Close()
// passing isTerm = false here because of https://github.com/Nvveen/Gotty/pull/1
return jsonmessage.DisplayJSONMessagesStream(responseBody, cli.Out(), cli.Out().FD(), false, nil)
}
func (d *DockerDriver) initializeDockerCli() (command.Cli, error) {
if d.dockerCli != nil {
return d.dockerCli, nil
}
cli, err := command.NewDockerCli()
if err != nil {
return nil, err
}
if d.config["DOCKER_DRIVER_QUIET"] == "1" {
cli.Apply(command.WithCombinedStreams(ioutil.Discard))
}
if err := cli.Initialize(cliflags.NewClientOptions()); err != nil {
return nil, err
}
d.dockerCli = cli
return cli, nil
}
func (d *DockerDriver) exec(op *Operation) error {
ctx := context.Background()
cli, err := d.initializeDockerCli()
if err != nil {
return err
}
if d.Simulate {
return nil
}
if d.config["PULL_ALWAYS"] == "1" {
if err := pullImage(ctx, cli, op.Image); err != nil {
return err
}
}
var env []string
for k, v := range op.Environment {
env = append(env, fmt.Sprintf("%s=%v", k, v))
}
cfg := &container.Config{
Image: op.Image,
Env: env,
Entrypoint: strslice.StrSlice{"/cnab/app/run"},
AttachStderr: true,
AttachStdout: true,
}
hostCfg := &container.HostConfig{AutoRemove: true}
for _, opt := range d.dockerConfigurationOptions {
if err := opt(cfg, hostCfg); err != nil {
return err
}
}
resp, err := cli.Client().ContainerCreate(ctx, cfg, hostCfg, nil, "")
switch {
case client.IsErrNotFound(err):
fmt.Fprintf(cli.Err(), "Unable to find image '%s' locally\n", op.Image)
if err := pullImage(ctx, cli, op.Image); err != nil {
return err
}
if resp, err = cli.Client().ContainerCreate(ctx, cfg, hostCfg, nil, ""); err != nil {
return fmt.Errorf("cannot create container: %v", err)
}
case err != nil:
return fmt.Errorf("cannot create container: %v", err)
}
tarContent, err := generateTar(op.Files)
if err != nil {
return fmt.Errorf("error staging files: %s", err)
}
options := types.CopyToContainerOptions{
AllowOverwriteDirWithFile: false,
}
// This copies the tar to the root of the container. The tar has been assembled using the
// path from the given file, starting at the /.
err = cli.Client().CopyToContainer(ctx, resp.ID, "/", tarContent, options)
if err != nil {
return fmt.Errorf("error copying to / in container: %s", err)
}
attach, err := cli.Client().ContainerAttach(ctx, resp.ID, types.ContainerAttachOptions{
Stream: true,
Stdout: true,
Stderr: true,
Logs: true,
})
if err != nil {
return fmt.Errorf("unable to retrieve logs: %v", err)
}
var (
stdout io.Writer = os.Stdout
stderr io.Writer = os.Stderr
)
if d.containerOut != nil {
stdout = d.containerOut
}
if d.containerErr != nil {
stderr = d.containerErr
}
go func() {
defer attach.Close()
for {
_, err := stdcopy.StdCopy(stdout, stderr, attach.Reader)
if err != nil {
break
}
}
}()
statusc, errc := cli.Client().ContainerWait(ctx, resp.ID, container.WaitConditionRemoved)
if err = cli.Client().ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
return fmt.Errorf("cannot start container: %v", err)
}
select {
case err := <-errc:
if err != nil {
return fmt.Errorf("error in container: %v", err)
}
case s := <-statusc:
if s.StatusCode == 0 {
return nil
}
if s.Error != nil {
return fmt.Errorf("container exit code: %d, message: %v", s.StatusCode, s.Error.Message)
}
return fmt.Errorf("container exit code: %d", s.StatusCode)
}
return err
}
func generateTar(files map[string]string) (io.Reader, error) {
r, w := io.Pipe()
tw := tar.NewWriter(w)
for path := range files {
if !unix_path.IsAbs(path) {
return nil, fmt.Errorf("destination path %s should be an absolute unix path", path)
}
}
go func() {
for path, content := range files {
hdr := &tar.Header{
Name: path,
Mode: 0644,
Size: int64(len(content)),
}
tw.WriteHeader(hdr)
tw.Write([]byte(content))
}
w.Close()
}()
return r, nil
}
// DockerConfigurationOption is an option used to customize docker driver container and host config
type DockerConfigurationOption func(*container.Config, *container.HostConfig) error