-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdocker.go
391 lines (359 loc) · 10.8 KB
/
docker.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
390
391
package main
import (
"archive/tar"
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strings"
"sync"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/term"
"github.com/pkg/errors"
"github.com/spf13/afero"
billy "gopkg.in/src-d/go-billy.v4"
"gopkg.in/src-d/go-billy.v4/memfs"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/storage/memory"
)
const (
Dockerfile = "Dockerfile"
dockerAPIVersion = "1.24"
// Errors
ErrAddDockerfile = "Error adding Dockerfile"
ErrMappingFilesystem = "Error mapping filesystem"
ErrClosingContext = "Error adding context"
ErrGitWorktree = "Error Git worktreen"
ErrGitCheckout = "Error checkout Git"
ErrGetRepository = "Error getting repository at reference"
ErrFileSystemEncodingToDockerContext = "Error encoding file system into Docker context"
ErrReadFile = "Error reading file %s"
// ErrBuild are triggered when Docker can't build an image
ErrBuild = "Error building Docker image"
// ErrListImage is triggered if we can't list images
ErrListImage = "Error listing images"
ErrTagImage = "Error tagging image"
ErrRemoveImage = "Error removing image"
// ErrCreateClient are triggered when we can't connect to the Docker deamon
ErrCreateClient = "Error creating a new Docker client"
// ErrTarHeaderWrite are triggered when we can't write context header
ErrTarHeaderWrite = "Error writing file tar header: %v"
// ErrTarFileWrite are triggered when we can't write file content to context
ErrTarFileWrite = "Error writing tar file"
// ErrTarClose are triggered when we can't close the context
ErrTarClose = "Error closing tar buffer"
// ErrOutputStream are triggered when we can't display docker output stream
ErrOutputStream = "Error displaying Docker output stream"
ErrFileStat = "Error file stat %s"
ErrReadDir = "Error reading director %s"
ErrAddFile = "Error adding file"
ErrUserDir = "Error getting user directory"
)
// GitStore handles git file system and reference storage
type GitStore struct {
fs billy.Filesystem
storer *memory.Storage
}
// DockerBuilder is a docker container builder
type DockerBuilder struct {
tags []string
sync.RWMutex
gitStore map[string]*GitStore
context *Context
}
// NewDockerBuilder returns a build ready DockerBuilder
// It will clone then checkout the repositories specified on the Dockerfile
// FrontMatter then transform the produced Git file systems into Docker contexts
func NewDockerBuilder(matrix *BuildMatrix, dockerfile []byte, tag, branchOverride string) (*DockerBuilder, error) {
dockerBuilder := &DockerBuilder{
tags: []string{tag},
gitStore: make(map[string]*GitStore),
}
var wg sync.WaitGroup
for repository, reference := range matrix.Git {
wg.Add(1)
dockerBuilder.Lock()
dockerBuilder.gitStore[repository] = &GitStore{
fs: memfs.New(),
storer: memory.NewStorage(),
}
dockerBuilder.Unlock()
var ref = reference
if branchOverride != "" {
ref = branchOverride
}
go func(repository, reference string) error {
err := dockerBuilder.getRepositoryAtReference(repository, reference)
if err != nil {
wg.Done()
fmt.Println("Error", err)
return errors.Wrap(err, ErrGetRepository)
}
wg.Done()
return nil
}(repository, ref)
}
wg.Wait()
err := dockerBuilder.getDockerContextFromFilesystem(dockerfile, matrix.Files)
if err != nil {
return nil, errors.Wrap(err, ErrFileSystemEncodingToDockerContext)
}
return dockerBuilder, nil
}
// Build run the docker build described in the DockerBuilder
func (db *DockerBuilder) Build() error {
fmt.Println("Sending Docker context")
dockerClient, err := client.NewClient(client.DefaultDockerHost, dockerAPIVersion, nil, nil)
if err != nil {
return errors.Wrap(err, ErrCreateClient)
}
resp, err := dockerClient.ImageBuild(context.Background(), bytes.NewReader(db.context.Bytes()), types.ImageBuildOptions{
ForceRemove: true,
})
if err != nil {
return errors.Wrap(err, ErrBuild)
}
defer resp.Body.Close()
err = jsonmessage.DisplayJSONMessagesToStream(
resp.Body,
NewOutStream(os.Stdout),
nil,
)
if err != nil {
return errors.Wrap(err, ErrOutputStream)
}
filterArgs := filters.NewArgs()
filterArgs.Add("label", "build_tag")
filterArgs.Add("dangling", "true")
images, err := dockerClient.ImageList(context.Background(), types.ImageListOptions{
Filters: filterArgs,
})
if err != nil {
return errors.Wrap(err, ErrListImage)
}
for _, image := range images {
tag := image.Labels["build_tag"]
if tag != "" {
fmt.Println("Tagging", image.ID, image.Labels["build_tag"])
err := dockerClient.ImageTag(context.Background(), image.ID, image.Labels["build_tag"])
if err != nil {
return errors.Wrap(err, ErrTagImage)
}
}
}
// Once tagged we need to make sure we remove the old dangling images so they are not picked up on next run
deletableImages, err := dockerClient.ImageList(context.Background(), types.ImageListOptions{
Filters: filterArgs,
})
if err != nil {
return errors.Wrap(err, ErrListImage)
}
for _, image := range deletableImages {
resp, err := dockerClient.ImageRemove(context.Background(), image.ID, types.ImageRemoveOptions{
Force: true,
PruneChildren: true,
})
if err != nil {
return errors.Wrap(err, ErrRemoveImage)
}
fmt.Printf("Deleting: %v\n", resp)
}
return nil
}
func folderFromGitURL(url string) string {
path := strings.Split(url, "/")
return strings.ToLower(strings.TrimSuffix(path[len(path)-1], ".git"))
}
func (db *DockerBuilder) addFile(dir, filePath string) error {
content, err := afero.ReadFile(fs, filePath)
if err != nil {
return errors.Wrapf(err, ErrReadFile, filePath)
}
err = db.context.AddFile(filepath.Join(dir, filepath.Base(filePath)), content)
if err != nil {
return errors.Wrap(err, ErrAddDockerfile)
}
return nil
}
func (db *DockerBuilder) getDockerContextFromFilesystem(dockerfile []byte, files []string) error {
fmt.Println("Building Docker context")
db.context = NewContext()
if err := db.context.AddFile(Dockerfile, dockerfile); err != nil {
return errors.Wrap(err, ErrAddDockerfile)
}
for _, filePath := range files {
usr, err := user.Current()
if filePath[:2] == "~/" {
filePath = filepath.Join(usr.HomeDir, filePath[2:])
}
fileInfo, err := os.Stat(filePath)
if err != nil {
return errors.Wrapf(err, ErrFileStat, filePath)
}
if fileInfo.IsDir() {
filesInfo, err := ioutil.ReadDir(filePath)
if err != nil {
return errors.Wrapf(err, ErrReadDir, filePath)
}
if err != nil {
return errors.Wrapf(err, ErrUserDir)
}
for _, f := range filesInfo {
err := db.addFile(filepath.Base(filePath), filepath.Join(filePath, f.Name()))
if err != nil {
return errors.Wrap(err, ErrAddFile)
}
}
} else {
if err := db.addFile("", filePath); err != nil {
return errors.Wrap(err, ErrAddFile)
}
}
}
for key, store := range db.gitStore {
if err := db.context.AddFilesystem(folderFromGitURL(key), ".", store.fs); err != nil {
return errors.Wrap(err, ErrMappingFilesystem)
}
}
if err := db.context.Close(); err != nil {
return errors.Wrap(err, ErrClosingContext)
}
return nil
}
func (db *DockerBuilder) getRepositoryAtReference(repository, reference string) error {
// TODO: Cache the repository to avoid refetching every time
fmt.Printf("Cloning %s at %s.\n", repository, reference)
db.RLock()
storer := db.gitStore[repository].storer
fs := db.gitStore[repository].fs
db.RUnlock()
_, err := git.Clone(
storer,
fs,
&git.CloneOptions{
URL: repository,
Progress: os.Stdout,
ReferenceName: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", reference)),
SingleBranch: true,
Depth: 1,
},
)
if err != nil {
return errors.Wrap(err, "Error cloning Git repository")
}
return nil
}
// Context is a Docker context as a tar archive
type Context struct {
buf *bytes.Buffer
tw *tar.Writer
}
// NewContext returns a new context
func NewContext() *Context {
buf := new(bytes.Buffer)
return &Context{
buf: buf,
tw: tar.NewWriter(buf),
}
}
// AddFile adds a file the Docker context
func (c *Context) AddFile(name string, content []byte) error {
header := &tar.Header{
Name: name,
Mode: 0600,
Size: int64(len(content)),
}
if err := c.tw.WriteHeader(header); err != nil {
return errors.Wrapf(err, ErrTarHeaderWrite, header)
}
if _, err := c.tw.Write(content); err != nil {
return errors.Wrap(err, ErrTarFileWrite)
}
return nil
}
// AddFilesystem map a file system to a tar archive
func (c *Context) AddFilesystem(prefix, path string, fs billy.Filesystem) error {
info, err := fs.Stat(path)
if err != nil {
return errors.Wrap(err, "Can't stat")
}
header, err := tar.FileInfoHeader(info, path)
if err != nil {
return errors.Wrap(err, "Can't get header from fileinfo")
}
header.Name = filepath.Join(prefix, path)
err = c.tw.WriteHeader(header)
if err != nil {
return errors.Wrap(err, "Can't write header")
}
if info.IsDir() {
files, err := fs.ReadDir(path)
if err != nil {
return errors.Wrap(err, "Can't read dir")
}
for _, f := range files {
if err = c.AddFilesystem(prefix, filepath.Join(path, f.Name()), fs); err != nil {
return errors.Wrapf(err, "Can't tar %s", filepath.Join(path, f.Name()))
}
}
return nil
}
if header.Typeflag == tar.TypeReg {
file, err := fs.Open(path)
if err != nil {
return errors.Wrap(err, "Can't open file")
}
written, err := io.CopyN(c.tw, file, info.Size())
if err != nil && err != io.EOF && written != info.Size() {
return errors.Wrap(err, "Can't copy file content")
}
if err := file.Close(); err != nil {
return errors.Wrap(err, "Can't close file")
}
}
return nil
}
// Close closes the Docker context
func (c *Context) Close() error {
if err := c.tw.Close(); err != nil {
return errors.Wrap(err, ErrTarClose)
}
return nil
}
// Bytes returns the representation of the Docker context
func (c *Context) Bytes() []byte {
return c.buf.Bytes()
}
// OutStream is a Docker output stream
type OutStream struct {
out io.Writer
fd uintptr
isTerminal bool
state *term.State
}
// NewOutStream returns a new
// OutStream object from a Writer
func NewOutStream(out io.Writer) *OutStream {
fd, isTerminal := term.GetFdInfo(out)
return &OutStream{out: out, fd: fd, isTerminal: isTerminal}
}
func (o *OutStream) Write(p []byte) (int, error) {
return o.out.Write(p)
}
// FD returns the file descriptor number for this stream
func (o *OutStream) FD() uintptr {
return o.fd
}
// IsTerminal returns true if this stream is connected to a
// terminal
func (o *OutStream) IsTerminal() bool {
return o.isTerminal
}