-
Notifications
You must be signed in to change notification settings - Fork 38
/
docker.go
296 lines (237 loc) · 8.1 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
package image
import (
"bytes"
"crypto/rand"
"encoding/json"
"fmt"
"io"
"os/exec"
"regexp"
"time"
regname "github.com/google/go-containerregistry/pkg/name"
)
var (
tmpRefHint = regexp.MustCompile("[^a-zA-Z0-9\\-]+")
)
type Docker struct {
logger Logger
}
type DockerBuildOpts struct {
// https://docs.docker.com/engine/reference/commandline/build/
Target *string
Pull *bool
NoCache *bool
File *string
RawOptions *[]string
}
type DockerTmpRef struct {
val string
}
func (r DockerTmpRef) AsString() string { return r.val }
type DockerImageDigest struct {
val string
}
func (r DockerImageDigest) AsString() string { return r.val }
func (d Docker) Build(image, directory string, opts DockerBuildOpts) (DockerTmpRef, error) {
randPrefix, err := d.randomStr(5)
if err != nil {
return DockerTmpRef{}, fmt.Errorf("Generating tmp image suffix: %s", err)
}
tmpRef := DockerTmpRef{fmt.Sprintf(
"kbld:%s-%s", randPrefix, tmpRefHint.ReplaceAllString(image, "-"))}
prefixedLogger := d.logger.NewPrefixedWriter(image + " | ")
prefixedLogger.Write([]byte(fmt.Sprintf("starting build (using Docker): %s -> %s\n", directory, tmpRef.AsString())))
defer prefixedLogger.Write([]byte("finished build (using Docker)\n"))
{
var stdoutBuf, stderrBuf bytes.Buffer
cmdArgs := []string{"build"}
if opts.Target != nil {
cmdArgs = append(cmdArgs, "--target", *opts.Target)
}
if opts.Pull != nil && *opts.Pull {
cmdArgs = append(cmdArgs, "--pull")
}
if opts.NoCache != nil && *opts.NoCache {
cmdArgs = append(cmdArgs, "--no-cache")
}
if opts.File != nil {
// Since docker command is executed with cwd of directory,
// Dockerfile path doesnt need to be joined with it
cmdArgs = append(cmdArgs, "--file", *opts.File)
}
if opts.RawOptions != nil {
cmdArgs = append(cmdArgs, *opts.RawOptions...)
}
cmdArgs = append(cmdArgs, "--tag", tmpRef.AsString(), ".")
cmd := exec.Command("docker", cmdArgs...)
cmd.Dir = directory
cmd.Stdout = io.MultiWriter(&stdoutBuf, prefixedLogger)
cmd.Stderr = io.MultiWriter(&stderrBuf, prefixedLogger)
err := cmd.Run()
if err != nil {
prefixedLogger.Write([]byte(fmt.Sprintf("error: %s\n", err)))
return DockerTmpRef{}, err
}
}
inspectData, err := d.inspect(tmpRef.AsString())
if err != nil {
prefixedLogger.Write([]byte(fmt.Sprintf("inspect error: %s\n", err)))
return DockerTmpRef{}, err
}
// Retag image with its sha256 to produce exact image ref if nothing has changed.
// Seems that Docker doesn't like `kbld@sha256:...` format for local images.
// Image hint at the beginning for easier sorting.
stableTmpRef := DockerTmpRef{fmt.Sprintf("kbld:%s-%s",
tmpRefHint.ReplaceAllString(image, "-"),
tmpRefHint.ReplaceAllString(inspectData.Id, "-"))}
{
var stdoutBuf, stderrBuf bytes.Buffer
cmd := exec.Command("docker", "tag", tmpRef.AsString(), stableTmpRef.AsString())
cmd.Stdout = io.MultiWriter(&stdoutBuf, prefixedLogger)
cmd.Stderr = io.MultiWriter(&stderrBuf, prefixedLogger)
err := cmd.Run()
if err != nil {
prefixedLogger.Write([]byte(fmt.Sprintf("tag error: %s\n", err)))
return DockerTmpRef{}, err
}
}
// Remove temporary tag to be nice to `docker images` output.
{
var stdoutBuf, stderrBuf bytes.Buffer
cmd := exec.Command("docker", "rmi", tmpRef.AsString())
cmd.Stdout = io.MultiWriter(&stdoutBuf, prefixedLogger)
cmd.Stderr = io.MultiWriter(&stderrBuf, prefixedLogger)
err := cmd.Run()
if err != nil {
prefixedLogger.Write([]byte(fmt.Sprintf("untag error: %s\n", err)))
return DockerTmpRef{}, err
}
}
return stableTmpRef, nil
}
func (d Docker) Push(tmpRef DockerTmpRef, imageDst string) (DockerImageDigest, error) {
prefixedLogger := d.logger.NewPrefixedWriter(imageDst + " | ")
// Generate random tag for pushed image.
// TODO we are technically polluting registry with new tags.
// Unfortunately we do not know digest upfront so cannot use kbld-sha256-... format.
imageDstTagged, err := regname.NewTag(imageDst, regname.WeakValidation)
if err == nil {
randSuffix, err := d.randomStr(5)
if err != nil {
return DockerImageDigest{}, fmt.Errorf("Generating image dst suffix: %s", err)
}
imageDstTag := fmt.Sprintf("kbld-%s", randSuffix)
imageDstTagged, err = regname.NewTag(imageDst+":"+imageDstTag, regname.WeakValidation)
if err != nil {
return DockerImageDigest{}, fmt.Errorf("Generating image dst tag '%s': %s", imageDst, err)
}
}
imageDst = imageDstTagged.Name()
prefixedLogger.Write([]byte(fmt.Sprintf("starting push (using Docker): %s -> %s\n", tmpRef.AsString(), imageDst)))
defer prefixedLogger.Write([]byte("finished push (using Docker)\n"))
prevInspectData, err := d.inspect(tmpRef.AsString())
if err != nil {
prefixedLogger.Write([]byte(fmt.Sprintf("inspect error: %s\n", err)))
return DockerImageDigest{}, err
}
{
var stdoutBuf, stderrBuf bytes.Buffer
cmd := exec.Command("docker", "tag", tmpRef.AsString(), imageDst)
cmd.Stdout = io.MultiWriter(&stdoutBuf, prefixedLogger)
cmd.Stderr = io.MultiWriter(&stderrBuf, prefixedLogger)
err := cmd.Run()
if err != nil {
prefixedLogger.Write([]byte(fmt.Sprintf("tag error: %s\n", err)))
return DockerImageDigest{}, err
}
}
{
var stdoutBuf, stderrBuf bytes.Buffer
cmd := exec.Command("docker", "push", imageDst)
cmd.Stdout = io.MultiWriter(&stdoutBuf, prefixedLogger)
cmd.Stderr = io.MultiWriter(&stderrBuf, prefixedLogger)
err := cmd.Run()
if err != nil {
prefixedLogger.Write([]byte(fmt.Sprintf("push error: %s\n", err)))
return DockerImageDigest{}, err
}
}
currInspectData, err := d.inspect(imageDst)
if err != nil {
prefixedLogger.Write([]byte(fmt.Sprintf("inspect error: %s\n", err)))
return DockerImageDigest{}, err
}
// Try to detect if image we should be pushing isnt the one we ended up pushing
// given that its theoretically possible concurrent Docker commands
// may have retagged in the middle of the process.
if prevInspectData.Id != currInspectData.Id {
prefixedLogger.Write([]byte(fmt.Sprintf("push race error: %s\n", err)))
return DockerImageDigest{}, err
}
return d.determineRepoDigest(currInspectData, prefixedLogger)
}
func (d Docker) determineRepoDigest(inspectData dockerInspectData, prefixedLogger *LoggerPrefixWriter) (DockerImageDigest, error) {
if len(inspectData.RepoDigests) == 0 {
prefixedLogger.Write([]byte("missing repo digest\n"))
return DockerImageDigest{}, fmt.Errorf("Expected to find at least one repo digest")
}
digestStrs := map[string]struct{}{}
for _, rd := range inspectData.RepoDigests {
nameWithDigest, err := regname.NewDigest(rd, regname.WeakValidation)
if err != nil {
return DockerImageDigest{}, fmt.Errorf("Extracting reference digest from '%s': %s", rd, err)
}
digestStrs[nameWithDigest.DigestStr()] = struct{}{}
}
if len(digestStrs) != 1 {
prefixedLogger.Write([]byte("repo digests mismatch\n"))
return DockerImageDigest{}, fmt.Errorf("Expected to find same repo digest, but found %#v", inspectData.RepoDigests)
}
for digest, _ := range digestStrs {
return DockerImageDigest{digest}, nil
}
panic("unreachable")
}
type dockerInspectData struct {
Id string
RepoDigests []string
}
func (d Docker) inspect(ref string) (dockerInspectData, error) {
var stdoutBuf, stderrBuf bytes.Buffer
cmd := exec.Command("docker", "inspect", ref)
cmd.Stdout = &stdoutBuf
cmd.Stderr = &stderrBuf
err := cmd.Run()
if err != nil {
return dockerInspectData{}, err
}
var data []dockerInspectData
err = json.Unmarshal(stdoutBuf.Bytes(), &data)
if err != nil {
return dockerInspectData{}, err
}
if len(data) != 1 {
return dockerInspectData{}, fmt.Errorf("Expected to find exactly one image, but found %d", len(data))
}
return data[0], nil
}
func (d Docker) randomStr(n int) (string, error) {
bs, err := d.randomBytes(n)
if err != nil {
return "", err
}
result := ""
for _, b := range bs {
result += fmt.Sprintf("%d", b)
}
// Timestamp at the beginning for easier sorting
return fmt.Sprintf("rand-%d-%s", time.Now().UTC().UnixNano(), result), nil
}
func (d Docker) randomBytes(n int) ([]byte, error) {
bs := make([]byte, n)
_, err := rand.Read(bs)
if err != nil {
return nil, err
}
return bs, nil
}