forked from getporter/porter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
539 lines (439 loc) · 14.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
//go:build mage
// +build mage
// This is a magefile, and is a "makefile for go".
// See https://magefile.org/
package main
import (
"fmt"
"go/build"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"get.porter.sh/magefiles/ci"
"get.porter.sh/magefiles/docker"
"get.porter.sh/magefiles/releases"
"get.porter.sh/magefiles/tools"
"get.porter.sh/porter/mage/setup"
"get.porter.sh/porter/pkg"
"get.porter.sh/porter/tests/tester"
mageci "github.com/carolynvs/magex/ci"
"github.com/carolynvs/magex/mgx"
"github.com/carolynvs/magex/shx"
"github.com/carolynvs/magex/xplat"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"golang.org/x/sync/errgroup"
// mage:import
"get.porter.sh/magefiles/tests"
// mage:import
_ "get.porter.sh/porter/mage/docs"
)
// Default target to run when none is specified
// If not set, running mage will list available targets
// var Default = Build
const (
PKG = "get.porter.sh/porter"
GoVersion = ">=1.17"
)
var must = shx.CommandBuilder{StopOnError: true}
// Check if we have the right version of Go
func CheckGoVersion() {
tools.EnforceGoVersion(GoVersion)
}
// Builds all code artifacts in the repository
func Build() {
mg.SerialDeps(setup.EnsureProtobufTools, BuildPorter, DocsGen, BuildExecMixin, BuildAgent)
mg.Deps(GetMixins)
}
// Build the porter client and runtime
func BuildPorter() {
mg.Deps(Tidy, copySchema)
mgx.Must(releases.BuildAll(PKG, "porter", "bin"))
}
func copySchema() {
// Copy the porter manifest schema into our templates directory with the other schema
// We can't use symbolic links because that doesn't work on windows
mgx.Must(shx.Copy("pkg/schema/manifest.schema.json", "pkg/templates/templates/schema.json"))
}
func Tidy() error {
return shx.Run("go", "mod", "tidy")
}
// Build the exec mixin client and runtime
func BuildExecMixin() {
mgx.Must(releases.BuildAll(PKG, "exec", "bin/mixins/exec"))
}
// Build the porter agent
func BuildAgent() {
// the agent is only used embedded in a docker container, so we only build for linux
releases.XBuild(PKG, "agent", "bin", "linux", "amd64")
}
// Cross-compile porter and the exec mixin
func XBuildAll() {
mg.Deps(XBuildPorter, XBuildMixins, BuildAgent)
}
// Cross-compile porter
func XBuildPorter() {
mg.Deps(copySchema)
releases.XBuildAll(PKG, "porter", "bin")
}
// Cross-compile the exec mixin
func XBuildMixins() {
releases.XBuildAll(PKG, "exec", "bin/mixins/exec")
}
// Generate cli documentation for the website
func DocsGen() {
// Remove the generated cli directory so that it can detect deleted files
os.RemoveAll("docs/content/cli")
os.Mkdir("docs/content/cli", pkg.FileModeDirectory)
must.RunV("go", "run", "--tags=docs", "./cmd/porter", "docs")
}
// Cleanup workspace after building or running tests.
func Clean() {
mg.Deps(tests.DeleteTestCluster)
mgx.Must(os.RemoveAll("bin"))
}
// Ensure EnsureMage is installed and on the PATH.
func EnsureMage() error {
return tools.EnsureMage()
}
func Debug() {
releases.LoadMetadata()
}
// ConfigureAgent sets up an Azure DevOps agent with EnsureMage and ensures
// that GOPATH/bin is in PATH.
func ConfigureAgent() error {
return ci.ConfigureAgent()
}
// Install mixins used by tests and example bundles, if not already installed
func GetMixins() error {
defaultMixinVersion := os.Getenv("MIXIN_TAG")
if defaultMixinVersion == "" {
defaultMixinVersion = "canary"
}
mixins := []struct {
name string
url string
feed string
version string
}{
{name: "docker"},
{name: "docker-compose"},
{name: "arm"},
{name: "terraform"},
{name: "kubernetes"},
{name: "helm3", feed: "https://mchorfa.github.io/porter-helm3/atom.xml", version: "v0.1.16"},
}
var errG errgroup.Group
for _, mixin := range mixins {
mixin := mixin
mixinDir := filepath.Join("bin/mixins/", mixin.name)
if _, err := os.Stat(mixinDir); err == nil {
log.Println("Mixin already installed into bin:", mixin.name)
continue
}
errG.Go(func() error {
log.Println("Installing mixin:", mixin.name)
if mixin.version == "" {
mixin.version = defaultMixinVersion
}
var source string
if mixin.feed != "" {
source = "--feed-url=" + mixin.feed
} else {
source = "--url=" + mixin.url
}
return porter("mixin", "install", mixin.name, "--version", mixin.version, source).Run()
})
}
return errG.Wait()
}
// Run a porter command from the bin
func porter(args ...string) shx.PreparedCommand {
porterPath := filepath.Join("bin", "porter")
p := shx.Command(porterPath, args...)
porterHome, _ := filepath.Abs("bin")
p.Cmd.Env = []string{"PORTER_HOME=" + porterHome}
return p
}
// Update golden test files to match the new test outputs
func UpdateTestfiles() {
must.Command("go", "test", "./...").Env("PORTER_UPDATE_TEST_FILES=true").RunV()
must.RunV("make", "test-unit")
}
// Run all tests known to human-kind
func Test() {
mg.Deps(TestUnit, TestSmoke, TestIntegration)
}
// Run unit tests and verify integration tests compile
func TestUnit() {
mg.Deps(copySchema)
// Only do verbose output of tests when called with `mage -v TestSmoke`
v := ""
if mg.Verbose() {
v = "-v"
}
must.Command("go", "test", v, "./...").CollapseArgs().RunV()
// Verify integration tests compile since we don't run them automatically on pull requests
must.Run("go", "test", "-run=non", "-tags=integration", "./...")
}
// Run smoke tests to quickly check if Porter is broken
func TestSmoke() error {
mg.Deps(copySchema, TryRegisterLocalHostAlias, docker.RestartDockerRegistry, BuildTestMixin)
// Only do verbose output of tests when called with `mage -v TestSmoke`
v := ""
if mg.Verbose() {
v = "-v"
}
// Adding -count to prevent go from caching the test results.
return shx.Command("go", "test", "-count=1", "-timeout=20m", "-tags", "smoke", v, "./tests/smoke/...").CollapseArgs().RunV()
}
func getRegistry() string {
registry := os.Getenv("PORTER_REGISTRY")
if registry == "" {
registry = "localhost:5000"
}
return registry
}
func BuildImages() {
info := releases.LoadMetadata()
registry := getRegistry()
buildImages(registry, info)
}
func buildImages(registry string, info releases.GitMetadata) {
var g errgroup.Group
enableBuildKit := "DOCKER_BUILDKIT=1"
g.Go(func() error {
img := fmt.Sprintf("%s/porter:%s", registry, info.Version)
err := shx.Command("docker", "build", "-t", img, "-f", "build/images/client/Dockerfile", ".").
Env(enableBuildKit).RunV()
if err != nil {
return err
}
err = shx.Run("docker", "tag", img, fmt.Sprintf("%s/porter:%s", registry, info.Permalink))
if err != nil {
return err
}
// porter-agent does a FROM porter so they can't go in parallel
img = fmt.Sprintf("%s/porter-agent:%s", registry, info.Version)
err = shx.Command("docker", "build", "-t", img, "--build-arg", "PORTER_VERSION="+info.Version, "--build-arg", "REGISTRY="+registry, "-f", "build/images/agent/Dockerfile", ".").
Env(enableBuildKit).RunV()
if err != nil {
return err
}
return shx.Run("docker", "tag", img, fmt.Sprintf("%s/porter-agent:%s", registry, info.Permalink))
})
g.Go(func() error {
img := fmt.Sprintf("%s/workshop:%s", registry, info.Version)
err := shx.Command("docker", "build", "-t", img, "-f", "build/images/workshop/Dockerfile", ".").
Env(enableBuildKit).RunV()
if err != nil {
return err
}
return shx.Run("docker", "tag", img, fmt.Sprintf("%s/workshop:%s", registry, info.Permalink))
})
mgx.Must(g.Wait())
}
func PublishImages() {
mg.Deps(BuildImages)
info := releases.LoadMetadata()
pushImagesTo(getRegistry(), info)
}
// Builds the porter-agent image and publishes it to a local test cluster with the Porter Operator.
func LocalPorterAgentBuild() {
// Publish to the local registry/cluster setup by the Porter Operator.
os.Setenv("REGISTRY", "localhost:5000")
// Force the image to be pushed to the registry even though it's a local dev build.
os.Setenv("PORTER_FORCE_PUBLISH", "true")
mg.SerialDeps(XBuildPorter, BuildAgent, PublishImages)
}
// Only push tagged versions, canary and latest
func pushImagesTo(registry string, info releases.GitMetadata) {
if info.IsTaggedRelease {
pushImages(registry, info.Version)
}
force, _ := strconv.ParseBool(os.Getenv("PORTER_FORCE_PUBLISH"))
if info.ShouldPublishPermalink() || force {
pushImages(registry, info.Permalink)
} else {
fmt.Println("Skipping image publish for permalink", info.Permalink)
}
}
func pushImages(registry string, tag string) {
pushImage(fmt.Sprintf("%s/porter:%s", registry, tag))
pushImage(fmt.Sprintf("%s/porter-agent:%s", registry, tag))
pushImage(fmt.Sprintf("%s/workshop:%s", registry, tag))
}
func pushImage(img string) {
must.RunV("docker", "push", img)
}
// Publish the porter binaries and install scripts.
func PublishPorter() {
mg.Deps(tools.EnsureGitHubClient, releases.ConfigureGitBot)
info := releases.LoadMetadata()
// Copy install scripts into version directory
must.Command("./scripts/prep-install-scripts.sh").Env("VERSION=" + info.Version).RunV()
porterVersionDir := filepath.Join("bin", info.Version)
execVersionDir := filepath.Join("bin/mixins/exec", info.Version)
var repo = os.Getenv("PORTER_RELEASE_REPOSITORY")
if repo == "" {
repo = "github.com/getporter/porter"
}
remote := fmt.Sprintf("https://%s.git", repo)
// Create or update GitHub release for the permalink (canary/latest) with the version's assets (porter binaries, exec binaries and install scripts)
if info.ShouldPublishPermalink() {
// Move the permalink tag. The existing release automatically points to the tag.
must.RunV("git", "tag", info.Permalink, info.Version+"^{}", "-f")
must.RunV("git", "push", "-f", remote, info.Permalink)
releases.AddFilesToRelease(repo, info.Permalink, porterVersionDir)
releases.AddFilesToRelease(repo, info.Permalink, execVersionDir)
} else {
fmt.Println("Skipping publish binaries for permalink", info.Permalink)
}
if info.IsTaggedRelease {
// Create GitHub release for the exact version (v1.2.3) and attach assets
releases.AddFilesToRelease(repo, info.Version, porterVersionDir)
releases.AddFilesToRelease(repo, info.Version, execVersionDir)
} else {
fmt.Println("Skipping publish binaries for not tagged release", info.Version)
}
}
// Publish internal porter mixins, like exec.
func PublishMixins() {
releases.PublishMixinFeed("exec")
}
// Copy the cross-compiled binaries from xbuild into bin.
func UseXBuildBinaries() error {
pwd, _ := os.Getwd()
goos := build.Default.GOOS
ext := ""
if runtime.GOOS == "windows" {
ext = ".exe"
}
copies := map[string]string{
"bin/dev/porter-$GOOS-amd64$EXT": "bin/porter$EXT",
"bin/dev/porter-linux-amd64": "bin/runtimes/porter-runtime",
"bin/mixins/exec/dev/exec-$GOOS-amd64$EXT": "bin/mixins/exec/exec$EXT",
"bin/mixins/exec/dev/exec-linux-amd64": "bin/mixins/exec/runtimes/exec-runtime",
}
r := strings.NewReplacer("$GOOS", goos, "$EXT", ext, "$PWD", pwd)
for src, dest := range copies {
src = r.Replace(src)
dest = r.Replace(dest)
log.Printf("Copying %s to %s", src, dest)
destDir := filepath.Dir(dest)
os.MkdirAll(destDir, pkg.FileModeDirectory)
err := sh.Copy(dest, src)
if err != nil {
return err
}
}
return SetBinExecutable()
}
// Run `chmod +x -R bin`.
func SetBinExecutable() error {
err := chmodRecursive("bin", pkg.FileModeExecutable)
if err != nil {
return fmt.Errorf("could not set +x on the test bin: %w", err)
}
return nil
}
func chmodRecursive(name string, mode os.FileMode) error {
return filepath.Walk(name, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
log.Println("chmod +x ", path)
return os.Chmod(path, mode)
})
}
// Run integration tests (slow).
func TestIntegration() {
mg.Deps(tests.EnsureTestCluster, copySchema, TryRegisterLocalHostAlias, BuildTestMixin, BuildTestPlugin)
var run string
runTest := os.Getenv("PORTER_RUN_TEST")
if runTest != "" {
run = "-run=" + runTest
}
verbose := ""
if mg.Verbose() {
verbose = "-v"
}
must.Command("go", "test", verbose, "-timeout=30m", run, "-tags=integration", "./...").CollapseArgs().RunV()
}
// TryRegisterLocalHostAlias edits /etc/hosts to use porter-test-registry hostname alias
// This is not safe to call more than once and is intended for use on the CI server only
func TryRegisterLocalHostAlias() {
if _, isCI := mageci.DetectBuildProvider(); !isCI {
return
}
err := shx.RunV("sudo", "bash", "-c", "echo 127.0.0.1 porter-test-registry >> /etc/hosts")
if err != nil {
fmt.Println("skipping registering the porter-test-registry hostname alias: could not write to /etc/hosts")
return
}
fmt.Println("Added host alias porter-test-registry to /etc/hosts")
os.Setenv(tester.TestRegistryAlias, "porter-test-registry")
}
func BuildTestPlugin() {
must.RunV("go", "build", "-o", "bin/testplugin", "./cmd/testplugin")
}
func BuildTestMixin() {
os.MkdirAll("bin/mixins/testmixin", 0770)
must.RunV("go", "build", "-o", "bin/mixins/testmixin/testmixin"+xplat.FileExt(), "./cmd/testmixin")
}
// Copy the locally built porter and exec binaries to PORTER_HOME
func Install() {
porterHome := getPorterHome()
fmt.Println("installing Porter from bin to", porterHome)
// Copy porter binaries
mgx.Must(os.MkdirAll(porterHome, pkg.FileModeDirectory))
mgx.Must(shx.Copy(filepath.Join("bin", "porter"+xplat.FileExt()), porterHome))
mgx.Must(shx.Copy(filepath.Join("bin", "runtimes"), porterHome, shx.CopyRecursive))
// Copy mixin binaries
mixinsDir := filepath.Join("bin", "mixins")
mixinsDirItems, err := ioutil.ReadDir(mixinsDir)
if err != nil {
mgx.Must(fmt.Errorf("could not list mixins in bin: %w", err))
}
for _, fi := range mixinsDirItems {
// do not install the test mixins
if fi.Name() == "testmixin" {
continue
}
if !fi.IsDir() {
continue
}
mixin := fi.Name()
srcDir := filepath.Join(mixinsDir, mixin)
destDir := filepath.Join(porterHome, "mixins", mixin)
mgx.Must(os.MkdirAll(destDir, pkg.FileModeDirectory))
// Copy the mixin client binary
mgx.Must(shx.Copy(filepath.Join(srcDir, mixin+xplat.FileExt()), destDir))
// Copy the mixin runtimes
mgx.Must(shx.Copy(filepath.Join(srcDir, "runtimes"), destDir, shx.CopyRecursive))
}
}
// Run Go Vet on the project
func Vet() {
must.RunV("go", "vet", "./...")
}
// Run staticcheck on the project
func Lint() {
tools.EnsureStaticCheck()
must.RunV("staticcheck", "./...")
}
func getPorterHome() string {
porterHome := os.Getenv("PORTER_HOME")
if porterHome == "" {
home, err := os.UserHomeDir()
if err != nil {
mgx.Must(fmt.Errorf("could not determine home directory: %w", err))
}
porterHome = filepath.Join(home, ".porter")
}
return porterHome
}