-
Notifications
You must be signed in to change notification settings - Fork 379
Expand file tree
/
Copy pathsa.go
More file actions
420 lines (370 loc) · 9.6 KB
/
sa.go
File metadata and controls
420 lines (370 loc) · 9.6 KB
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
package buildscript
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/craiggwilson/goke/pkg/sh"
"github.com/craiggwilson/goke/task"
)
const (
goimportsVersion = "v0.29.0"
goimportsPkg = "golang.org/x/tools/cmd/goimports@" + goimportsVersion
// For JS tools like eslint and prettier, these versions need to match the ones in the
// `package.json` file. To update to a new version, run a command like this:
//
// npm install --save-dev --save-exact prettier@3.3.1
//
// Then update the version in this file as well.
// This is the latest version to support a YAML config file. Updating to
// the new config file syntax did not seem trivial.
eslintVersion = "8.57.0"
gitHubCodeownersVersion = "0.2.1"
golangCILintVersion = "2.2.2"
golinesVersion = "0.12.2"
gosecVersion = "2.22.5"
preciousVersion = "0.7.3"
ubiVersion = "0.4.2"
prettierVersion = "3.4.2"
)
func SAInstallDevTools(ctx *task.Context) error {
if err := installUBI(ctx); err != nil {
return err
}
if err := installGoimports(ctx); err != nil {
return err
}
if err := installGolangCILint(ctx); err != nil {
return err
}
if err := installGolines(ctx); err != nil {
return err
}
if err := installGosec(ctx); err != nil {
return err
}
if err := installPrecious(ctx); err != nil {
return err
}
return installJSTools(ctx)
}
// Install goimports.
func installGoimports(ctx *task.Context) error {
return goInstall(ctx, goimportsPkg)
}
// Install UBI.
func installUBI(ctx *task.Context) error {
var err error
devBin, err := devBinDir()
if err != nil {
return err
}
ubi, err := devBinFile("ubi")
if err != nil {
return err
}
exists, err := executableExistsWithVersion(ctx, ubi, ubiVersion)
if err != nil {
return err
}
if exists {
return nil
}
var ubiBootstrapURL string
switch runtime.GOOS {
case "windows":
ubiBootstrapURL = "https://raw.githubusercontent.com/houseabsolute/ubi/ci-for-bootstrap/bootstrap/bootstrap-ubi.ps1"
default:
ubiBootstrapURL = fmt.Sprintf(
"https://raw.githubusercontent.com/houseabsolute/ubi/v%s/bootstrap/bootstrap-ubi.sh",
ubiVersion,
)
}
s := strings.Split(ubiBootstrapURL, "/")
bootstrapPath := filepath.Join(os.TempDir(), s[len(s)-1])
out, err := os.Create(bootstrapPath)
if err != nil {
return err
}
defer out.Close()
resp, err := httpGetWithRetries(ubiBootstrapURL, 5)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
var cmd []string
if strings.HasSuffix(ubiBootstrapURL, ".ps1") {
cmd = []string{"powershell", bootstrapPath}
} else {
cmd = []string{"sh", bootstrapPath}
}
// On Windows the bootstrapper always installs into the current directory,
// so chdir there first.
return doInDir(devBin, func() error {
c := exec.CommandContext(ctx, cmd[0], cmd[1:]...)
c.Env = []string{"TARGET=" + devBin, "TAG=v" + ubiVersion}
return sh.RunCmd(ctx, c)
})
}
// Install golangci-lint.
func installGolangCILint(ctx *task.Context) error {
return installBinaryTool(
ctx,
"golangci-lint",
golangCILintVersion,
"golangci/golangci-lint",
fmt.Sprintf(
"https://github.com/golangci/golangci-lint/releases/download/v%s/golangci-lint-%s-linux-amd64.tar.gz",
golangCILintVersion,
golangCILintVersion,
),
)
}
// Install golines.
func installGolines(ctx *task.Context) error {
return installBinaryTool(
ctx,
"golines",
golinesVersion,
"segmentio/golines",
fmt.Sprintf(
"https://github.com/segmentio/golines/releases/download/v%s/golines_%s_linux_amd64.tar.gz",
golinesVersion,
golinesVersion,
),
)
}
// Install gosec.
func installGosec(ctx *task.Context) error {
return installBinaryTool(
ctx,
"gosec",
gosecVersion,
"securego/gosec",
fmt.Sprintf(
"https://github.com/securego/gosec/releases/download/v%s/gosec_%s_linux_amd64.tar.gz",
gosecVersion,
gosecVersion,
),
)
}
func installPrecious(ctx *task.Context) error {
return installBinaryTool(
ctx,
"precious",
preciousVersion,
"houseabsolute/precious",
fmt.Sprintf(
"https://github.com/houseabsolute/precious/releases/download/v%s/precious-Linux-x86_64-musl.tar.gz",
preciousVersion,
),
)
}
// Install a Golang package as an executable with "go install".
func goInstall(ctx *task.Context, link string) error {
root, err := repoRoot()
if err != nil {
return err
}
if err = os.Setenv("GOBIN", filepath.Join(root, "dev-bin")); err != nil {
return err
}
return withRetries(
5,
fmt.Sprintf("go install %s", link),
func() error {
return sh.Run(ctx, "go", "install", link)
},
)
}
func installBinaryTool(
ctx *task.Context,
exeName, toolVersion, githubProject, downloadURLForCI string,
) error {
devBin, err := devBinDir()
if err != nil {
return err
}
devBinExe, err := devBinFile(exeName)
if err != nil {
return err
}
exists, err := executableExistsWithVersion(ctx, devBinExe, toolVersion)
if err != nil {
return err
}
if exists {
return nil
}
cmd := []string{
filepath.Join(devBin, "ubi"),
"--in", devBin,
}
if inCI() {
// Using the `--url` arg avoids hitting the GitHub API, but it skips
// all the platform detection ubi provides. We do it this way because
// even with authentication, the limits on the GitHub API are
// something like 5,000 requests an hour. Without it, the limit is way
// lower.
//
// This seemed simpler than adding a GitHub token to Evergreen. If we
// ever switch to GH Actions we can reconsider, since in that case
// we'd have a token automatically available in the `GITHUB_TOKEN` env
// var.
cmd = append(cmd, "--url", downloadURLForCI)
} else {
cmd = append(
cmd,
"--project", githubProject,
"--tag", "v"+toolVersion,
)
}
return withRetries(
5,
fmt.Sprintf("installing %s", exeName),
func() error {
return sh.Run(ctx, cmd[0], cmd[1:]...)
},
)
}
// We have to install all the JS tools at once. If we run `npm install <tool>` multiple times, each
// execution wipes the entire `node_modules` directory, so we only end up with one tool (the last
// one) installed.
func installJSTools(ctx *task.Context) error {
eslint, err := npmPath("eslint")
if err != nil {
return err
}
prettier, err := npmPath("prettier")
if err != nil {
return err
}
for _, tool := range [][]string{
{eslint, eslintVersion},
{prettier, prettierVersion},
} {
exists, err := executableExistsWithVersion(ctx, tool[0], tool[1])
if err != nil {
return err
}
if !exists {
return runNPMInstall(ctx)
}
}
gitHubCodeowners, err := npmPath("github-codeowners")
if err != nil {
return err
}
// This program doesn't have any way to print its version. :(
exists, err := fileExists(gitHubCodeowners)
if err != nil {
return err
}
if !exists {
return runNPMInstall(ctx)
}
return nil
}
func runNPMInstall(ctx *task.Context) error {
return sh.Run(
ctx,
"npm", "install",
)
}
func npmPath(exe string) (string, error) {
root, err := repoRoot()
if err != nil {
return "", err
}
return filepath.Join(root, "node_modules", ".bin", exe), nil
}
func SAPreciousLint(ctx *task.Context) error {
return runPrecious(ctx, "lint", "--all")
}
func runPrecious(ctx *task.Context, args ...string) error {
devBin, err := devBinDir()
if err != nil {
return err
}
cmd := append(
[]string{filepath.Join(devBin, "precious")},
args...,
)
c := exec.CommandContext(ctx, cmd[0], cmd[1:]...)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
return sh.RunCmd(ctx, c)
}
// SAModTidy runs go mod tidy and ensure no changes were made.
// Copied from mongohouse: https://github.com/10gen/mongohouse/blob/333308814f96a0909c8125f71af7748b263e3263/buildscript/sa.go#L72
func SAModTidy(ctx *task.Context) error {
// Save original contents in case they get modified. When
// https://github.com/golang/go/issues/27005 is done, we
// shouldn't need this anymore.
origGoMod, err := os.ReadFile("go.mod")
if err != nil {
return fmt.Errorf("error reading go.mod: %w", err)
}
origGoSum, err := os.ReadFile("go.sum")
if err != nil {
return fmt.Errorf("error reading go.sum: %w", err)
}
err = sh.Run(ctx, "go", "mod", "tidy")
if err != nil {
return err
}
newGoMod, err := os.ReadFile("go.mod")
if err != nil {
return fmt.Errorf("error reading go.mod: %w", err)
}
newGoSum, err := os.ReadFile("go.sum")
if err != nil {
return fmt.Errorf("error reading go.sum: %w", err)
}
if !bytes.Equal(origGoMod, newGoMod) || !bytes.Equal(origGoSum, newGoSum) {
// Restore originals, ignoring errors since they need tidying anyway.
_ = os.WriteFile("go.mod", origGoMod, 0600)
_ = os.WriteFile("go.sum", origGoSum, 0600)
return errors.New(
"go.mod and/or go.sum needs changes: run `go mod tidy` and commit the changes",
)
}
return nil
}
// SAEvergreenValidate runs `evergreen validate` on common.yml and ensures the file is valid.
func SAEvergreenValidate(ctx *task.Context) error {
output, err := sh.RunOutput(
ctx,
"evergreen",
"validate",
"--file",
"common.yml",
"-p",
"mongo-tools",
)
if err != nil {
return fmt.Errorf("error from `evergreen validate`: %s: %w", output, err)
}
// TODO: change this if-block in TOOLS-2840.
// This check ignores any YAML warnings related to duplicate keys in YAML maps.
// See ticket for more details.
if strings.HasSuffix(output, "is valid with warnings") {
for _, line := range strings.Split(output, "\n") {
if !strings.HasSuffix(line, "unmarshal errors:") &&
!strings.HasSuffix(line, "already set in map") &&
!strings.HasSuffix(line, "is valid with warnings") {
return fmt.Errorf("error from `evergreen validate`: %s", output)
}
}
}
return nil
}