-
Notifications
You must be signed in to change notification settings - Fork 2
/
magefile.go
340 lines (300 loc) · 8.29 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
// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
//go:build mage
// +build mage
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"sort"
"strconv"
"strings"
"time"
"github.com/magefile/mage/sh"
"github.com/zeebo/errs"
)
// Test executes all unit and integration tests.
//nolint:deadcode
func Test() error {
err := sh.RunV("go", "test", "./...")
return err
}
// Coverage executes all unit test with coverage measurement.
//nolint:deadcode
func Coverage() error {
fmt.Println("Executing tests and generate coverage 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", "./tmp/coverage.html")
}
// Lint executes all the linters with golangci-lint.
//nolint:deadcode
func Lint() error {
return sh.RunV("bash", "scripts/lint.sh")
}
// Format reformats code automatically.
//nolint:deadcode
func Format() error {
err := sh.RunV("gofmt", "-w", ".")
if err != nil {
return err
}
return sh.RunV("goimports", "-w", "-local=storj", ".")
}
// 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")
}
// DockerBuild builds storjscan image.
//nolint:deadcode
func DockerBuild() error {
tag, err := getNextDockerTag("storjscan.last")
if err != nil {
return err
}
err = sh.RunV("docker", "build", "-t", "img.dev.storj.io/storjup/storjscan:"+tag, ".")
if err != nil {
return err
}
return nil
}
// DockerPublish pushes storjscan image.
//nolint:deadcode
func DockerPublish() error {
return dockerPushWithNextTag("storjscan")
}
// Integration executes integration tests.
//nolint:deadcode
func Integration() error {
return sh.RunV("bash", "test/test.sh")
}
// ListImages prints all the existing storjscan images in the repo.
//nolint:deadcode
func ListImages() error {
versions, err := listContainerVersions("storjscan")
if err != nil {
return err
}
for _, v := range versions {
fmt.Printf("storjscan:%s\n", v)
}
return nil
}
func dockerPushWithNextTag(image string) error {
tagFile := fmt.Sprintf("%s.last", image)
tag, err := getNextDockerTag(tagFile)
if err != nil {
return err
}
err = sh.RunV("docker", "push", fmt.Sprintf("img.dev.storj.io/storjup/%s:%s", image, tag))
if err != nil {
return err
}
return writeDockerTag(tagFile, tag)
}
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
}
// 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 := ioutil.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 ioutil.WriteFile(tagFile, []byte(tag), 0o644)
}
// ListVersions prints out the available container / release versions.
//nolint:deadcode
func ListVersions() error {
fmt.Println("container: storjscan")
versions, err := listContainerVersions("storjscan")
if err != nil {
return err
}
for _, v := range versions {
fmt.Println(" " + v)
}
return nil
}
func listReleaseVersions(name string) ([]string, error) {
url := fmt.Sprintf("https://api.github.com/repos/storjscan/%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:%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 := ioutil.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"`
}