-
Notifications
You must be signed in to change notification settings - Fork 4
/
update-docker-tags.go
415 lines (346 loc) · 10.5 KB
/
update-docker-tags.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/Masterminds/semver/v3"
"github.com/pkg/errors"
)
// TODO: make this not Sourcegraph-specific
var TAG_PATTERN = regexp.MustCompile(`(sourcegraph/.+):(.+)@(sha256:[[:alnum:]]+)`)
var (
constraintArgs rawConstraints
enforceArgs rawConstraints
)
const (
helpConstraint = "perform semver update on given docker image to satisfy semver constraint (repeatable)"
helpEnforce = "override given docker image to enforce a semver constraint (repeatable)"
)
func main() {
flag.Usage = func() {
fmt.Printf(`update-docker-tags
Usage:
update-docker-tags [options] < FILE | FOLDER >...
Options:
--constraint %s
--enforce %s
Examples:
Update all image tags in a directory:
$ update-docker-tags dir/
Update all image tags in the given files and folders, satisfying constraints:
$ update-docker-tags --constraint=ubuntu=<18.04 --constraint=alpine=<3.10 deployment.yaml dir/
Override all tags in the given files and folders to enforce a constraint:
$ update-docker-tags --enforce=sourcegraph/frontend=~3.19
`, helpConstraint, helpEnforce)
os.Exit(2)
}
flag.Var(&constraintArgs, "constraint", helpConstraint)
flag.Var(&enforceArgs, "enforce", helpEnforce)
flag.Parse()
parsedConstraints, err := constraintArgs.parse()
if err != nil {
log.Fatalf("failed to parse raw constraints, err: %s", err)
}
parsedEnforce, err := enforceArgs.parse()
if err != nil {
log.Fatalf("failed to parse raw enforce, err: %s", err)
}
paths := flag.Args()
if len(paths) == 0 {
flag.Usage()
os.Exit(2)
}
o := &options{
constraints: parsedConstraints,
enforce: parsedEnforce,
filePaths: paths,
}
for _, root := range o.filePaths {
if err := updateDockerTags(o, root); err != nil {
log.Fatalf("failed to update docker tags for root %q, err: %s", root, err)
}
}
}
// UpdateDockerTags updates the Docker tags for the entire file tree rooted at
// "root" using the provided constraints.
func updateDockerTags(o *options, root string) error {
return filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if strings.HasPrefix(path, ".git") {
// Highly doubt anyone would ever want us to traverse git directories.
return nil
}
data, err := ioutil.ReadFile(path)
if err != nil {
return errors.Wrap(err, "when reading file contents")
}
printedPath := false
// replaceErr is a workaround for replaceAllSubmatchFunc not propagating errors
var replaceErr error
data = replaceAllSubmatchFunc(TAG_PATTERN, data, func(groups [][]byte) [][]byte {
repositoryName := string(groups[0])
repository, err := newRepository(o, repositoryName)
if err != nil {
replaceErr = errors.Wrapf(err, "when initializing repository %q", repositoryName)
return groups
}
originalTag := string(groups[1])
var newTag string
// if we are not enforcing a constraint, keep non-semver tags
if isNonSemverTag(originalTag) && !repository.enforceConstraint {
newTag = originalTag
} else {
latest, err := repository.findLatestSemverTag()
if err != nil {
replaceErr = errors.Wrapf(err, "when finding tag for '%s:%s'", repository.name, originalTag)
return groups
}
newTag = latest
}
newDigest, err := repository.fetchImageDigest(newTag)
if err != nil {
replaceErr = errors.Wrapf(err, "when fetching image digest for '%s:%s'", repository.name, newTag)
return groups
}
if !printedPath {
printedPath = true
fmt.Println(path)
}
fmt.Println("\t", repository.name, "\t\t", newTag)
groups[1] = []byte(newTag)
groups[2] = []byte(newDigest)
return groups
}, -1)
if replaceErr != nil {
return errors.Wrapf(replaceErr, "when replacing image tags in %q", path)
}
err = ioutil.WriteFile(path, data, info.Mode())
return errors.Wrapf(err, "when writing file contents of %q", path)
})
}
type repository struct {
name string
constraint *semver.Constraints
enforceConstraint bool
authToken string
}
func isNonSemverTag(tag string) bool {
_, err := semver.NewVersion(tag)
// Assume that "tag" isn't a semver one (like "latest")
// if we're unable to parse it
return err != nil
}
func (r *repository) findLatestSemverTag() (string, error) {
var versions []*semver.Version
tags, err := r.fetchAllTags()
if err != nil {
return "", errors.Wrap(err, "when fetching all tags")
}
for _, t := range tags {
v, err := semver.NewVersion(t)
if err != nil {
continue // ignore non-semver tags
}
if r.constraint != nil {
if r.constraint.Check(v) {
versions = append(versions, v)
}
} else {
versions = append(versions, v)
}
}
if len(versions) == 0 {
if r.constraint != nil {
return "", fmt.Errorf("no semver tags found for %q matching constraints %q", r.name, r.constraint.String())
}
return "", fmt.Errorf("no semver tags found for %q", r.name)
}
sort.Sort(sort.Reverse(semver.Collection(versions)))
latestTag := versions[0].Original()
return latestTag, nil
}
// Effectively the same as:
//
// $ curl -s -D - -H "Authorization: Bearer $token" -H "Accept: application/vnd.docker.distribution.manifest.v2+json" https://index.docker.io/v2/sourcegraph/server/manifests/3.12.1 | grep Docker-Content-Digest
//
func (r *repository) fetchImageDigest(tag string) (string, error) {
req, err := http.NewRequest("GET", "https://index.docker.io/v2/"+r.name+"/manifests/"+tag, nil)
if err != nil {
return "", err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", r.authToken))
req.Header.Set("Accept", "application/vnd.docker.distribution.manifest.v2+json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
return resp.Header.Get("Docker-Content-Digest"), nil
}
// Effectively the same as:
//
// $ export token=$(curl -s "https://auth.docker.io/token?service=registry.docker.io&scope=repository:sourcegraph/server:pull" | jq -r .token)
//
func fetchAuthToken(repositoryName string) (string, error) {
resp, err := http.Get(fmt.Sprintf("https://auth.docker.io/token?service=registry.docker.io&scope=repository:%s:pull", repositoryName))
if err != nil {
return "", err
}
defer resp.Body.Close()
result := struct {
Token string
}{}
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return "", err
}
return result.Token, nil
}
// Effectively the same as:
//
// $ curl -H "Authorization: Bearer $token" https://index.docker.io/v2/sourcegraph/server/tags/list
//
func (r *repository) fetchAllTags() ([]string, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("https://index.docker.io/v2/%s/tags/list", r.name), nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+r.authToken)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
result := struct {
Tags []string
}{}
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return nil, err
}
return result.Tags, nil
}
// replaceAllSubmatchFunc is the missing regexp.ReplaceAllSubmatchFunc; to use it:
//
// pattern := regexp.MustCompile(...)
// data = replaceAllSubmatchFunc(pattern, data, func(groups [][]byte) [][]byte {
// // mutate groups here
// return groups
// })
//
// This snippet is MIT licensed. Please cite by leaving this comment in place. Find
// the latest version at:
//
// https://gist.github.com/slimsag/14c66b88633bd52b7fa710349e4c6749
//
func replaceAllSubmatchFunc(re *regexp.Regexp, src []byte, repl func([][]byte) [][]byte, n int) []byte {
var (
result = make([]byte, 0, len(src))
matches = re.FindAllSubmatchIndex(src, n)
last = 0
)
for _, match := range matches {
// Append bytes between our last match and this one (i.e. non-matched bytes).
matchStart := match[0]
matchEnd := match[1]
result = append(result, src[last:matchStart]...)
last = matchEnd
// Determine the groups / submatch bytes and indices.
groups := [][]byte{}
groupIndices := [][2]int{}
for i := 2; i < len(match); i += 2 {
start := match[i]
end := match[i+1]
groups = append(groups, src[start:end])
groupIndices = append(groupIndices, [2]int{start, end})
}
// Replace the groups as desired.
groups = repl(groups)
// Append match data.
lastGroup := matchStart
for i, newValue := range groups {
// Append bytes between our last group match and this one (i.e. non-group-matched bytes)
groupStart := groupIndices[i][0]
groupEnd := groupIndices[i][1]
result = append(result, src[lastGroup:groupStart]...)
lastGroup = groupEnd
// Append the new group value.
result = append(result, newValue...)
}
result = append(result, src[lastGroup:matchEnd]...) // remaining
}
result = append(result, src[last:]...) // remaining
return result
}
type rawConstraint struct {
image string
constraint string
}
func (rc *rawConstraint) String() string {
return fmt.Sprintf("%s=%s", rc.image, rc.constraint)
}
type rawConstraints []*rawConstraint
func (rc *rawConstraints) String() string {
var elems []string
for _, raw := range *rc {
elems = append(elems, raw.String())
}
return strings.Join(elems, ", ")
}
func (rc *rawConstraints) Set(value string) error {
splits := strings.Split(value, "=")
if len(splits) != 2 {
return fmt.Errorf("unable to split constraint %q", value)
}
image, constraint := splits[0], splits[1]
*rc = append(*rc, &rawConstraint{
image: image,
constraint: constraint,
})
return nil
}
func (rc *rawConstraints) parse() (map[string]*semver.Constraints, error) {
out := map[string]*semver.Constraints{}
for _, raw := range *rc {
parsed, err := semver.NewConstraint(raw.constraint)
if err != nil {
return nil, fmt.Errorf("cannot parse constraint %q, err: %w", raw.constraint, err)
}
out[raw.image] = parsed
}
return out, nil
}
type options struct {
constraints map[string]*semver.Constraints
enforce map[string]*semver.Constraints
filePaths []string
}
func newRepository(o *options, repositoryName string) (*repository, error) {
token, err := fetchAuthToken(repositoryName)
if err != nil {
return nil, errors.Wrap(err, "when fetching auth token")
}
if enforce, exists := o.enforce[repositoryName]; exists {
return &repository{
name: repositoryName,
constraint: enforce,
enforceConstraint: true,
authToken: token,
}, nil
}
return &repository{
name: repositoryName,
constraint: o.constraints[repositoryName],
authToken: token,
}, nil
}