-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmagefile.go
540 lines (482 loc) · 12.5 KB
/
magefile.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
//go:build mage
// +build mage
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"sort"
"strconv"
"strings"
"time"
"github.com/magefile/mage/sh"
"github.com/zeebo/errs"
)
// Coverage executes all unit test with coverage measurement.
//
//nolint:deadcode
func Coverage() error {
fmt.Println("Executing tests and generate coverate information")
err := sh.RunV("go", "test", "-coverprofile=/tmp/coverage.out", "./...")
if err != nil {
return err
}
return sh.RunV("go", "tool", "cover", "-html=/tmp/coverage.out", "-o", "coverage.html")
}
// Format reformats code automatically.
//
//nolint:deadcode
func Format() error {
err := sh.RunV("gofmt", "-w", ".")
if err != nil {
return err
}
return sh.RunV("goimports", "-w", ".")
}
// GenBuild re-generates `./build` helper binary.
//
//nolint:deadcode
func GenBuild() error {
envs := map[string]string{
"CGO_ENABLED": "0",
"GOOS": "linux",
"GOARCH": "amd64",
}
return sh.RunWithV(envs, "mage", "-compile", "build")
}
func withDockerTag(filename string, publish bool, action func(tag string) error) error {
tag, err := getNextDockerTag(filename)
if err != nil {
return err
}
err = action(tag)
if err != nil {
return err
}
if publish {
return writeDockerTag(filename, tag)
}
return nil
}
// DockerBaseBuild builds storj-base image.
//
//nolint:deadcode
func DockerBaseBuild() error {
return dockerBase(false)
}
// DockerBasePublish pushes storj-base image.
//
//nolint:deadcode
func DockerBasePublish() error {
return dockerBase(true)
}
func dockerBase(publish bool) error {
return withDockerTag("base.last", publish, func(tag string) error {
return buildxRun(publish,
"build",
"--tag", "img.dev.storj.io/storjup/base:"+tag,
"-f", "pkg/files/docker/base.Dockerfile", ".")
})
}
// DockerBuildBuild builds the storj-build docker image.
//
//nolint:deadcode
func DockerBuildBuild() error {
return dockerBuild(false)
}
// DockerBuildPublish pushes the storj-build docker image
//
//nolint:deadcode
func DockerBuildPublish() error {
return errs.Combine(
dockerBuild(true),
)
}
func dockerBuild(publish bool) error {
return withDockerTag("build.last", publish, func(tag string) error {
return buildxRun(publish,
"build",
"--build-arg", "SOURCE=branch",
"--build-arg", "TYPE=github",
"--build-arg", "BRANCH=main",
"--build-arg", "REPO=https://github.com/storj/storj.git",
"--tag", "img.dev.storj.io/storjup/build:"+tag,
"-f", "pkg/files/docker/build.Dockerfile", ".")
})
}
func dockerCore(version string, publish bool) error {
err := buildxRun(
publish,
"build",
"-t", "img.dev.storj.io/storjup/storj:"+version,
"--build-arg", "BRANCH=v"+version,
"--build-arg", "SOURCE=branch",
"--build-arg", "TYPE=github",
"-f", "pkg/files/docker/storj.Dockerfile", ".")
if err != nil {
return err
}
return nil
}
func buildxRun(publish bool, args ...string) error {
if publish {
args = append(args, "--push")
}
hasPlatform := false
for _, arg := range args {
if strings.HasPrefix(arg, "--platform") {
hasPlatform = true
}
}
if !hasPlatform {
args = append(args, "--platform=linux/amd64,linux/arm64")
}
args = append([]string{"docker", "buildx"}, args...)
return sh.RunV(args[0], args[1:]...)
}
func dockerEdge(version string, publish bool) error {
err := buildxRun(publish,
"build",
"-t", "img.dev.storj.io/storjup/edge:"+version,
"--build-arg", "BRANCH=v"+version,
"--build-arg", "SOURCE=branch",
"--build-arg", "TYPE=github",
"-f", "pkg/files/docker/edge.Dockerfile", ".")
if err != nil {
return err
}
return nil
}
// Integration executes integration tests.
//
//nolint:deadcode
func Integration() error {
return sh.RunV("bash", "test/test.sh")
}
// RebuildImages rebuilds all core and edge images.
//
//nolint:deadcode
func RebuildImages() error {
versions, err := listContainerVersions("storj")
if err != nil {
return err
}
for _, v := range versions {
err := dockerCore(v, true)
if err != nil {
return err
}
}
versions, err = listContainerVersions("edge")
if err != nil {
return err
}
for _, v := range versions {
err := dockerEdge(v, true)
if err != nil {
return err
}
}
return nil
}
// DockerEdge builds a Edge docker image for local use.
//
//nolint:deadcode
func DockerEdge(version string, publish bool) error {
if version == "" {
return errs.New("VERSION should be defined with environment variable")
}
return dockerEdge(version, publish)
}
// DockerStorj builds a Core docker image for local use.
//
//nolint:deadcode
func DockerStorj(version string, publish bool) error {
if version == "" {
return errs.New("VERSION should be defined with environment variable")
}
return dockerCore(version, publish)
}
// Images build missing images for existing git tags
//
//nolint:deadcode
func Images() error {
err := doOnMissing("storj", "storj", func(container string, repo string, version string) error {
err := dockerCore(version, true)
if err != nil {
return err
}
return dockerCorePublish(version)
})
if err != nil {
return err
}
err = doOnMissing("edge", "gateway-mt", func(container string, repo string, version string) error {
err := dockerEdge(version, true)
if err != nil {
return err
}
return dockerEdgePublish(version)
})
if err != nil {
return err
}
return nil
}
// ListImages prints all the existing storj and storj-edge images in the repo.
//
//nolint:deadcode
func ListImages() error {
versions, err := listContainerVersions("storj")
if err != nil {
return err
}
for _, v := range versions {
fmt.Printf("storj:%s\n", v)
}
versions, err = listContainerVersions("edge")
if err != nil {
return err
}
for _, v := range versions {
fmt.Printf("edge:%s\n", v)
}
return nil
}
func dockerPush(image string, tag string) error {
err := sh.RunV("docker", "push", fmt.Sprintf("img.dev.storj.io/storjup/%s:%s", image, tag))
if err != nil {
return err
}
return err
}
func dockerCorePublish(version string) error {
return dockerPush("storj", version)
}
func dockerEdgePublish(version string) error {
return dockerPush("storj-edge", version)
}
// getNextDockerTag generates docker tag with the pattern yyyymmdd-n.
// last used tag is saved to the file and supposed to be committed.
func getNextDockerTag(tagFile string) (string, error) {
datePattern := time.Now().Format("20060102")
if _, err := os.Stat(tagFile); os.IsNotExist(err) {
return datePattern + "-1", nil
}
content, err := os.ReadFile(tagFile)
if err != nil {
return "", err
}
parts := strings.Split(string(content), "-")
if parts[0] == datePattern {
i, err := strconv.Atoi(parts[1])
if err != nil {
return "", err
}
return fmt.Sprintf("%s-%d", datePattern, i+1), err
}
return datePattern + "-1", nil
}
func doOnMissing(containerName string, repoName string, action func(string, string, string) error) error {
containerVersions := make(map[string]bool)
versions, err := listContainerVersions(containerName)
if err != nil {
return err
}
for _, v := range versions {
containerVersions[v] = true
}
releases, err := listReleaseVersions(repoName)
if err != nil {
return err
}
for _, v := range releases {
if _, found := containerVersions[v]; !found {
err = action(containerName, repoName, v)
if err != nil {
return err
}
}
}
return nil
}
// writeDockerTag persist the last used docker tag to a file.
func writeDockerTag(tagFile string, tag string) error {
return os.WriteFile(tagFile, []byte(tag), 0o644)
}
// ListVersions prints out the available container / release versions.
//
//nolint:deadcode
func ListVersions() error {
fmt.Println("container: storj")
coreContainers, err := listContainerVersions("storj")
if err != nil {
return err
}
for _, v := range coreContainers {
fmt.Println(" " + v)
}
fmt.Println("container: edge")
edgeContainers, err := listContainerVersions("edge")
if err != nil {
return err
}
for _, v := range edgeContainers {
fmt.Println(" " + v)
}
fmt.Println("repo: storj/storj")
versions, err := listReleaseVersions("storj")
if err != nil {
return err
}
for _, v := range versions {
fmt.Println(" " + v + " container:" + findContainer(coreContainers, v))
}
fmt.Println("repo: storj/gateway-mt")
versions, err = listReleaseVersions("gateway-mt")
if err != nil {
return err
}
for _, v := range versions {
fmt.Println(" " + v + " container:" + findContainer(edgeContainers, v))
}
return nil
}
func findContainer(containers []string, v string) string {
for _, c := range containers {
if c == v {
return c
}
}
return "MISSING"
}
func listReleaseVersions(name string) ([]string, error) {
url := fmt.Sprintf("https://api.github.com/repos/storj/%s/releases?per_page=10", name)
rawVersions, err := callGithubAPIV3(context.Background(), "GET", url, nil)
if err != nil {
return nil, err
}
var releases []release
err = json.Unmarshal(rawVersions, &releases)
if err != nil {
return nil, err
}
var res []string
for _, v := range releases {
name := v.TagName
if strings.Contains(name, "rc") {
continue
}
if name[0] == 'v' {
name = name[1:]
}
res = append(res, name)
}
sort.Strings(res)
return res, nil
}
// listContainerVersions lists the available tags for one specific container.
func listContainerVersions(name string) ([]string, error) {
ctx := context.Background()
url := fmt.Sprintf("https://img.dev.storj.io/auth?service=img.dev.storj.io&scope=repository:storjup/%s:pull", name)
tokenResponse, err := httpCall(ctx, "GET", url, nil)
if err != nil {
return nil, errs.Wrap(err)
}
k := struct {
Token string `json:"token"`
}{}
err = json.Unmarshal(tokenResponse, &k)
if err != nil {
return nil, errs.Wrap(err)
}
url = fmt.Sprintf("https://img.dev.storj.io/v2/storjup/%s/tags/list", name)
tagResponse, err := httpCall(ctx, "GET", url, nil, func(request *http.Request) {
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", k.Token))
})
if err != nil {
return nil, errs.Wrap(err)
}
var versions version
err = json.Unmarshal(tagResponse, &versions)
if err != nil {
return nil, err
}
var res []string
for _, version := range versions.Tags {
if version == "latest" {
continue
}
res = append(res, version)
}
return res, nil
}
// callGithubAPIV3 is a wrapper around the HTTP method call.
func callGithubAPIV3(ctx context.Context, method string, url string, body io.Reader) ([]byte, error) {
token, err := getToken()
if err != nil {
return nil, errs.Wrap(err)
}
return httpCall(ctx, method, url, body, func(req *http.Request) {
req.Header.Add("Authorization", "token "+token)
req.Header.Add("Accept", "application/vnd.github.v3+json")
})
}
type httpRequestOpt func(*http.Request)
func httpCall(ctx context.Context, method string, url string, body io.Reader, opts ...httpRequestOpt) ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
return nil, errs.Wrap(err)
}
for _, o := range opts {
o(req)
}
resp, err := client.Do(req)
if err != nil {
return nil, errs.Wrap(err)
}
if resp.StatusCode > 299 {
return nil, errs.Combine(errs.New("%s url is failed (%s): %s", method, resp.Status, url), resp.Body.Close())
}
responseBody, err := io.ReadAll(resp.Body)
return responseBody, errs.Combine(err, resp.Body.Close())
}
// getToken retrieves the GITHUB_TOKEN for API usage.
func getToken() (string, error) {
token := os.Getenv("GITHUB_TOKEN")
if token != "" {
return token, nil
}
return "", fmt.Errorf("GITHUB_TOKEN environment variable must set")
}
// release is a Github API response object.
type release struct {
URL string `json:"url"`
AssetsURL string `json:"assets_url"`
UploadURL string `json:"upload_url"`
HTMLURL string `json:"html_url"`
ID int `json:"id"`
NodeID string `json:"node_id"`
TagName string `json:"tag_name"`
TargetCommitish string `json:"target_commitish"`
Name string `json:"name"`
Draft bool `json:"draft"`
Prerelease bool `json:"prerelease"`
CreatedAt time.Time `json:"created_at"`
PublishedAt time.Time `json:"published_at"`
TarballURL string `json:"tarball_url"`
ZipballURL string `json:"zipball_url"`
Body string `json:"body"`
MentionsCount int `json:"mentions_count,omitempty"`
}
// version is a Docker v2 REST API response object.
type version struct {
Name string `json:"name"`
Tags []string `json:"tags"`
}