forked from getporter/porter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagefile.go
379 lines (313 loc) · 10.1 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
// +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"
// mage:import
"get.porter.sh/porter/mage/releases"
"get.porter.sh/porter/mage"
"get.porter.sh/porter/mage/tests"
"get.porter.sh/porter/mage/tools"
"github.com/carolynvs/magex/mgx"
"github.com/carolynvs/magex/pkg/gopath"
"github.com/carolynvs/magex/shx"
"github.com/carolynvs/magex/xplat"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)
// Default target to run when none is specified
// If not set, running mage will list available targets
// var Default = Build
var must = shx.CommandBuilder{StopOnError: true}
// 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() {
mage.LoadMetadata()
}
// ConfigureAgent sets up an Azure DevOps agent with EnsureMage and ensures
// that GOPATH/bin is in PATH.
func ConfigureAgent() error {
err := EnsureMage()
if err != nil {
return err
}
// Instruct Azure DevOps to add GOPATH/bin to PATH
gobin := gopath.GetGopathBin()
err = os.MkdirAll(gobin, 0700)
if err != nil {
return errors.Wrapf(err, "could not mkdir -p %s", gobin)
}
fmt.Printf("Adding %s to the PATH\n", gobin)
fmt.Printf("##vso[task.prependpath]%s\n", gobin)
return nil
}
// 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
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.14"},
}
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
}
return porter("mixin", "install", mixin.name, "--version", mixin.version, "--feed-url", mixin.feed).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("make", "test-unit").Env("PORTER_UPDATE_TEST_FILES=true").RunV()
must.Command("make", "test-unit").RunV()
}
// Run smoke tests to quickly check if Porter is broken
func TestSmoke() error {
mg.Deps(tests.StartDockerRegistry)
// Only do verbose output of tests when called with `mage -v TestSmoke`
v := ""
if mg.Verbose() {
v = "-v"
}
return shx.Command("go", "test", "-tags", "smoke", v, "./tests/smoke/...").CollapseArgs().RunV()
}
func getRegistry() string {
registry := os.Getenv("REGISTRY")
if registry == "" {
registry = "getporterci"
}
return registry
}
func getDualPublish() bool {
dualPublish, _ := strconv.ParseBool(os.Getenv("DUAL_PUBLISH"))
return dualPublish
}
func BuildImages() {
info := mage.LoadMetadata()
registry := getRegistry()
buildImages(registry, info)
if getDualPublish() {
buildImages("ghcr.io/getporter", info)
}
}
func buildImages(registry string, info mage.GitMetadata) {
var g errgroup.Group
g.Go(func() error {
img := fmt.Sprintf("%s/porter:%s", registry, info.Version)
err := shx.RunV("docker", "build", "-t", img, "-f", "build/images/client/Dockerfile", ".")
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.RunV("docker", "build", "-t", img, "--build-arg", "PORTER_VERSION="+info.Version, "--build-arg", "REGISTRY="+registry, "-f", "build/images/agent/Dockerfile", "build/images/agent")
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.RunV("docker", "build", "-t", img, "-f", "build/images/workshop/Dockerfile", ".")
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 := mage.LoadMetadata()
pushImagesTo(getRegistry(), info)
if getDualPublish() {
pushImagesTo("ghcr.io/getporter", info)
}
}
// Only push tagged versions, canary and latest
func pushImagesTo(registry string, info mage.GitMetadata) {
if info.IsTaggedRelease {
pushImages(registry, info.Version)
}
if info.ShouldPublishPermalink() {
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 := mage.LoadMetadata()
// Copy install scripts into version directory
must.Command("./scripts/prep-install-scripts.sh").Env("VERSION="+info.Version, "PERMALINK="+info.Permalink).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)
}
}
// 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, 0700)
err := sh.Copy(dest, src)
if err != nil {
return err
}
}
return SetBinExecutable()
}
// Run `chmod +x -R bin`.
func SetBinExecutable() error {
err := chmodRecursive("bin", 0700)
return errors.Wrap(err, "could not set +x on the test bin")
}
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)
var run string
runTest := os.Getenv("PORTER_RUN_TEST")
if runTest != "" {
run = "-run=" + runTest
}
os.Setenv("GO111MODULE", "on")
must.RunV("go", "build", "-o", "bin/testplugin", "./cmd/testplugin")
must.Command("go", "test", "-timeout=30m", run, "-tags=integration", "./...").CollapseArgs().RunV()
}
func Install() {
porterHome := getPorterHome()
fmt.Println("installing Porter from bin to", porterHome)
// Copy porter binaries
mgx.Must(os.MkdirAll(porterHome, 0700))
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)
mgx.Must(errors.Wrap(err, "could not list mixins in bin"))
for _, fi := range mixinsDirItems {
if !fi.IsDir() {
continue
}
mixin := fi.Name()
srcDir := filepath.Join(mixinsDir, mixin)
destDir := filepath.Join(porterHome, "mixins", mixin)
mgx.Must(os.MkdirAll(destDir, 0700))
// 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))
}
}
func getPorterHome() string {
porterHome := os.Getenv("PORTER_HOME")
if porterHome == "" {
home, err := os.UserHomeDir()
mgx.Must(errors.Wrap(err, "could not determine home directory"))
porterHome = filepath.Join(home, ".porter")
}
return porterHome
}