-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmagefile.go
604 lines (500 loc) · 16.4 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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
//go:build mage
package main
import (
"archive/tar"
"compress/gzip"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/Masterminds/semver/v3"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/pterm/pterm"
"github.com/sheldonhull/magetools/ci"
"github.com/sheldonhull/magetools/pkg/magetoolsutils"
)
const (
// AnsibleLatest defines the latest stable version we use and support.
AnsibleLatest = "stable-2.16"
// CacheDir is the directory to keep virtual environments (ignored by git).
CacheDir = ".cache"
// ArtifactDir is the directory to store artifacts (ignored by git).
ArtifactDir = ".artifacts"
// PluginDocPathPattern is the pattern to find plugins for documentation for ansible-doc-extractor.
PluginDocPathPattern = "plugins/lookup/dsv.py"
// DocDirectory is the directory to store documentation.
DocDirectory = "docs"
// PermissionUserReadWriteExecute is the permissions for the artifact directory.
PermissionUserReadWriteExecute = 0o0700
)
// ✨ Init unfolds initial environment for productive work.
func Init() error {
magetoolsutils.CheckPtermDebug()
if ci.IsCI() {
pterm.Error.Println("CI should explicitly call `mage initCI <version_name>`")
return nil
}
if err := ansibleInit(AnsibleLatest); err != nil {
return err
}
pterm.Success.Printfln(
"to activate virtual environment run:\n\tsource ./.cache/venv/bin/activate",
)
return nil
}
// 🎩 InitCI initializes a new Python virtual environment with given version of Ansible installed.
func InitCI(version string) error {
return ansibleInit(version)
}
// 🧹 Clean removes '.artifact/', '.cache/', 'tests/output/' directories from the project.
func Clean() {
magetoolsutils.CheckPtermDebug()
if err := os.RemoveAll(ArtifactDir); err != nil {
pterm.Error.Printfln("🧹 failed to delete %q: %v", ArtifactDir, err)
} else {
pterm.Success.Printfln("🧹 %q", ArtifactDir)
}
if err := os.RemoveAll(CacheDir); err != nil {
pterm.Error.Printfln("🧹 failed to delete %q: %v", CacheDir, err)
} else {
pterm.Success.Printfln("🧹 %q", CacheDir)
}
testsOutput := filepath.Join("tests", "output")
if err := os.RemoveAll(testsOutput); err != nil {
pterm.Error.Printfln("🧹 failed to delete %q: %v", testsOutput, err)
} else {
pterm.Success.Printfln("🧹 %q", testsOutput)
}
pterm.Info.Println("🧹 Clean() completed")
}
// 🧪 Test runs unit and sanity tests.
func Test() error {
magetoolsutils.CheckPtermDebug()
if err := TestUnit(); err != nil {
return err
}
return TestSanity()
}
// 🧪 TestSanity runs sanity tests in containers.
func TestSanity() error {
magetoolsutils.CheckPtermDebug()
pterm.DefaultHeader.Println("ansible-test sanity")
if !venvBinExists("ansible-test") {
pterm.Error.Println("run `mage init` first")
return nil
}
now := time.Now()
if err := venvRunV(
"ansible-test", "sanity", "--docker", "--color", "yes",
"--exclude", "vendor/", "--exclude", ".devcontainer/",
); err != nil {
return err
}
pterm.Success.Printfln("sanity tests (took: %s)", time.Since(now))
return nil
}
// 🧪 TestUnit runs unit tests in containers.
func TestUnit() error {
magetoolsutils.CheckPtermDebug()
pterm.DefaultHeader.Println("ansible-test units")
if !venvBinExists("ansible-test") {
pterm.Error.Println("run `mage init` first")
return nil
}
testsOutput := filepath.Join("tests", "output")
if _, err := os.Stat(testsOutput); err == nil {
pterm.DefaultSection.Println("Cleanup old output:")
if err := os.RemoveAll(testsOutput); err != nil {
pterm.Error.Printfln("🧹 failed to delete %q: %v", testsOutput, err)
return nil
}
pterm.Success.Printfln("🧹 %q", testsOutput)
}
pterm.DefaultSection.Println("Unit Tests:")
now := time.Now()
if err := venvRunV(
"ansible-test", "units", "--docker", "--color", "yes", "--coverage",
); err != nil {
return err
}
pterm.Success.Printfln("unit tests (took: %s)", time.Since(now))
pterm.DefaultSection.Println("Code Coverage Report:")
if err := venvRun(
"ansible-test", "coverage", "xml", "-v", "--requirements",
"--group-by", "command", "--group-by", "version",
); err != nil {
return err
}
return venvRunV("ansible-test", "coverage", "report")
}
// 🔼 Bump increments version in the galaxy file of the collection, using yq.
// Valid types are "major", "minor", "patch"
func Bump(bumpType string) error {
pterm.DefaultHeader.Printfln("Version Bump")
galaxyYaml := "galaxy.yml"
current, err := sh.Output("yq", ".version", galaxyYaml)
if err != nil {
pterm.Error.Printfln("failed to get version from galaxy.yml:\n\t%v", err)
return err
}
current = strings.TrimSpace(current)
version, err := semver.StrictNewVersion(current)
if err != nil {
return err
}
var newVersion semver.Version
switch bumpType {
case "major":
newVersion = version.IncMajor()
case "minor":
newVersion = version.IncMinor()
case "patch":
newVersion = version.IncPatch()
default:
return fmt.Errorf("unknown bump type: %s", bumpType)
}
bumped := newVersion.String()
pterm.Info.Printfln("%q: %q -> %q", bumpType, current, bumped)
err = sh.RunV(
"yq", "--inplace", fmt.Sprintf(".version = \"%s\"", bumped), galaxyYaml,
)
if err != nil {
pterm.Error.Printfln("failed to bump version:\n\t%v", err)
return err
}
return nil
}
// 📜 Changelog helps with creating a release changelog.
func Changelog() error {
magetoolsutils.CheckPtermDebug()
pterm.DefaultHeader.Println("antsibull-changelog")
if !venvExists() {
pterm.Error.Println("run `mage init` first")
return nil
}
if !venvBinExists("antsibull-changelog") {
if err := venvInstall("antsibull-changelog"); err != nil {
return err
}
}
galaxyYaml := "galaxy.yml"
current, err := sh.Output("yq", ".version", galaxyYaml)
if err != nil {
pterm.Error.Printfln("failed to get version from galaxy.yml:\n\t%v", err)
return err
}
changelogFragmentDirectory := filepath.Join("changelogs", "fragments")
if err := os.MkdirAll(changelogFragmentDirectory, PermissionUserReadWriteExecute); err != nil {
pterm.Error.Printfln("directory couldn't be created: %s", changelogFragmentDirectory)
return fmt.Errorf("could not create directory: %s", changelogFragmentDirectory)
}
changeFile := filepath.Join("changelogs", "fragments", current+".yml")
if _, err := os.Stat(changeFile); err == nil {
pterm.Error.Printfln("file %q already exists", changeFile)
return errors.New("already exists")
}
pterm.Info.Printfln("Preparing changelog for version %q", current)
pterm.Info.Println("Enter release summary")
summary, err := pterm.DefaultInteractiveTextInput.WithMultiLine(true).Show()
if err != nil {
return err
}
pterm.Info.Printfln("You answered:\n%s", summary)
summary = strings.ReplaceAll(summary, "\n", "\n ")
summary = strings.TrimSpace(summary)
final := "---\nrelease_summary: "
if !strings.Contains(summary, "\n") {
final += summary + "\n"
} else {
final += "|\n " + summary + "\n"
}
if err := writeFile(changeFile, final); err != nil {
return err
}
return venvRunV("antsibull-changelog", "release", "-v")
}
// 📦 Build packages the collection into a publishable archive.
func Build() error {
magetoolsutils.CheckPtermDebug()
pterm.DefaultHeader.Println("ansible-galaxy collection build")
if !venvBinExists("ansible-galaxy") {
pterm.Error.Println("run `mage init` first")
return nil
}
if err := venvRun(
"ansible-galaxy", "collection", "build", "-v", "--force",
"--output-path", filepath.Join(ArtifactDir, ""),
); err != nil {
return err
}
path, err := archiveFind("delinea-core*.tar.gz")
if err != nil {
return err
}
files, err := archiveContent(path)
if err != nil {
return err
}
pterm.Info.Printfln("%q:\n\t- %s", path, strings.Join(files, "\n\t- "))
return nil
}
// 🚀 Publish sends archived collection to Ansible Galaxy.
func Publish() error {
magetoolsutils.CheckPtermDebug()
pterm.DefaultHeader.Println("ansible-galaxy collection publish")
if !venvBinExists("ansible-galaxy") {
pterm.Error.Println("run `mage init` first")
return nil
}
gxServer, gxKey := os.Getenv("GALAXY_SERVER"), os.Getenv("GALAXY_KEY")
if gxServer == "" {
pterm.Error.Printfln("env variable `GALAXY_SERVER` is required, but not set. Skipping publish.")
return fmt.Errorf("missing required environment variables")
}
if gxKey == "" {
pterm.Error.Printfln("env variable `GALAXY_KEY` is required, but not set. Skipping publish.")
return fmt.Errorf("missing required environment variables")
}
path, err := archiveFind("delinea-core*.tar.gz")
if err != nil {
pterm.Error.Println("run `mage build` first")
return err
}
pterm.DefaultSection.Printfln("Publishing `%s` to %s", path, gxServer)
now := time.Now()
if err := venvRunV(
"ansible-galaxy", "collection", "publish", "-v",
"--server", gxServer, "--api-key", gxKey, path,
); err != nil {
return fmt.Errorf("running `ansible-galaxt collection publish` failed")
}
pterm.Success.Printfln("Published collection (took: %s)", time.Since(now))
return nil
}
// 🔍 Doctor will validate the required tools and environment variables are available.
func Doctor() error {
magetoolsutils.CheckPtermDebug()
pterm.DefaultHeader.Println("Check Environment")
if !venvExists() {
pterm.Error.Println("run `mage init` first")
return nil
}
primary := pterm.NewStyle(pterm.FgLightWhite, pterm.BgGray, pterm.Bold)
printer := pterm.DefaultTable.WithHasHeader().WithBoxed().WithHeaderStyle(primary)
tbl := pterm.TableData{
[]string{"Status", "Check", "Value", "Notes"},
[]string{"✅", "Go version", runtime.Version(), ""},
[]string{"✅", "GOOS", runtime.GOOS, ""},
[]string{"✅", "GOARCH", runtime.GOARCH, ""},
[]string{"✅", "GOROOT", runtime.GOROOT(), ""},
[]string{"✅", "GOPATH", os.Getenv("GOPATH"), ""},
}
defer func() {
if err := printer.WithData(tbl).Render(); err != nil {
pterm.Error.Printf("pterm.TablePrinter: Render() failed. Continuing...\n%v", err)
}
}()
errorCount := 0
_, tbl, err := checkEnvVar(&checkEnv{Name: "GALAXY_SERVER", IsSecret: false, IsRequired: true, Tbl: tbl, Notes: "required for defining target publish location"})
if err != nil {
errorCount++
}
_, tbl, err = checkEnvVar(&checkEnv{Name: "GALAXY_KEY", IsSecret: true, IsRequired: true, Tbl: tbl, Notes: "required for publishing"})
if err != nil {
errorCount++
}
output, err := venvOutput("ansible-galaxy", "--version")
if err != nil {
errorCount++
tbl = append(tbl, []string{"❌", "ansible-galaxy", "ansible-galaxy", "required for building and publishing"})
} else {
output = strings.Split(output, "\n")[0]
tbl = append(tbl, []string{"✅", "ansible-galaxy", output, "required cli tool"})
}
version, err := venvOutput("python3", "--version")
if err != nil {
errorCount++
tbl = append(tbl, []string{"❌", "python3", "python3", "required for building and publishing"})
} else {
tbl = append(tbl, []string{"✅", "python3", version, "required for building and publishing"})
}
if errorCount > 0 {
pterm.Error.Printfln("required checks failed: %d", errorCount)
return fmt.Errorf("failed %d checks", errorCount)
}
return nil
}
// ExportPluginDocs runs ansible-doc-extractor to generate a markdown file of the plugin documentation.
func ExportPluginDocs() error {
pterm.DefaultHeader.Println("Exporting plugin docs")
if !venvBinExists("ansible-doc-extractor") {
if err := venvInstall("ansible-doc-extractor"); err != nil {
return fmt.Errorf("failed to install ansible-doc-extractor: %w", err)
}
} else {
pterm.Info.Println("ansible-doc-extractor already installed")
}
if err := mkdir(DocDirectory); err != nil {
return fmt.Errorf("failed to create doc directory: %w", err)
}
return venvRunV(
"ansible-doc-extractor",
DocDirectory,
PluginDocPathPattern,
"--markdown",
)
}
// ----------------------------------- //
// Helper Functions //
// ----------------------------------- //
func ansibleInit(version string) error {
magetoolsutils.CheckPtermDebug()
pterm.DefaultHeader.Printfln("Ansible %s Init()", AnsibleLatest)
link := fmt.Sprintf("https://github.com/ansible/ansible/archive/%s.tar.gz", version)
mg.SerialDeps(
venvInit,
func() error { return venvInstall("wheel") },
func() error { return venvInstall(link) },
)
return nil
}
func venvInit() error {
if err := mkdir(CacheDir); err != nil {
return err
}
path := filepath.Join(CacheDir, "venv")
err := sh.Run("python3", "-m", "venv", "--clear", path)
if err != nil {
pterm.Error.Printfln("error creating a new virtual environment: %s", err)
return err
}
pterm.Success.Printfln("created a new virtual environment: %s", path)
return nil
}
func venvExists() bool { return venvBinExists("pip3") }
func venvBinExists(name string) bool {
_, err := os.Stat(filepath.Join(CacheDir, "venv", "bin", name))
return err == nil
}
func venvInstall(name string) error {
now := time.Now()
if err := venvRun("pip3", "install", name, "--disable-pip-version-check"); err != nil {
pterm.Error.Printfln("error installing name %q: %s", name, err)
return err
}
pterm.Success.Printfln(" - installed %q (took: %s)", name, time.Since(now))
return nil
}
func venvRun(cmd string, args ...string) error { return venvRunBinary(false, cmd, args...) }
func venvRunV(cmd string, args ...string) error { return venvRunBinary(true, cmd, args...) }
func venvRunBinary(useStdout bool, cmd string, args ...string) error {
magetoolsutils.CheckPtermDebug()
path := filepath.Join(CacheDir, "venv")
venvBin := filepath.Join(path, "bin")
runnable := filepath.Join(venvBin, cmd)
pterm.Debug.Printfln("runnable: %s", runnable)
env := map[string]string{
"PATH": venvBin + ":" + os.Getenv("PATH"),
"VIRTUAL_ENV": path,
}
if useStdout {
return sh.RunWithV(env, runnable, args...)
}
return sh.RunWith(env, runnable, args...)
}
func venvOutput(cmd string, args ...string) (string, error) {
path := filepath.Join(CacheDir, "venv")
venvBin := filepath.Join(path, "bin")
runnable := filepath.Join(venvBin, cmd)
env := map[string]string{
"PATH": venvBin + ":" + os.Getenv("PATH"),
"VIRTUAL_ENV": path,
}
return sh.OutputWith(env, runnable, args...)
}
func writeFile(path string, data string) error {
const permBits = 0o777
return os.WriteFile(path, []byte(data), permBits)
}
func mkdir(path string) error {
const permBits = 0o755
return os.MkdirAll(path, permBits)
}
func archiveFind(pattern string) (string, error) {
archivePattern := filepath.Join(ArtifactDir, pattern)
archives, err := filepath.Glob(archivePattern)
if err != nil {
return "", err
}
switch {
case len(archives) == 0:
return "", fmt.Errorf("no archive found with pattern %q", archivePattern)
case len(archives) > 1:
return "", fmt.Errorf("more than one archive found with pattern %q", archivePattern)
default:
return archives[0], nil
}
}
func archiveContent(path string) ([]string, error) {
if _, err := os.Stat(path); err != nil {
return nil, err
}
r, err := os.Open(path)
if err != nil {
return nil, err
}
defer r.Close()
gzipReader, err := gzip.NewReader(r)
if err != nil {
return nil, err
}
defer gzipReader.Close()
tarReader := tar.NewReader(gzipReader)
files := []string{}
for {
header, err := tarReader.Next()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return nil, err
}
files = append(files, header.Name)
}
return files, nil
}
type checkEnv struct {
Name string
IsSecret bool
IsRequired bool
Tbl pterm.TableData
Notes string
}
// checkEnvVar performs a check on environment variable and helps build a report summary of the failing conditions, missing variables, and bypasses logging if it's a secret.
// Yes this could be replaced by the `env` package but I had this in place and the output is nice for debugging so I left it. - Sheldon 😀
//
//nolint:unparam // ignoring as i'll want to use the values in the future, ok to leave for now.
func checkEnvVar(ckv *checkEnv) (string, pterm.TableData, error) {
value, ok := os.LookupEnv(ckv.Name)
switch {
case ok && ckv.IsSecret:
ckv.Tbl = append(ckv.Tbl, []string{"✅", ckv.Name, "***** secret set, but not logged *****", ckv.Notes})
return value, ckv.Tbl, nil
case ok && !ckv.IsSecret:
ckv.Tbl = append(ckv.Tbl, []string{"✅", ckv.Name, value, ckv.Notes})
return value, ckv.Tbl, nil
case !ok && ckv.IsRequired:
ckv.Tbl = append(ckv.Tbl, []string{"❌", ckv.Name, "", ckv.Notes})
return "", ckv.Tbl, fmt.Errorf("%s is required and not set", ckv.Name)
case !ok && !ckv.IsRequired:
ckv.Tbl = append(ckv.Tbl, []string{"👉", ckv.Name, "", ckv.Notes})
return "", ckv.Tbl, nil
default:
return "", nil, nil // Unreachable.
}
}