-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmagefile.go
128 lines (93 loc) · 2.68 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
//go:build mage
// +build mage
package main
import (
"fmt"
"os"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
/// ! Ansible helpers
var VaultArgs []string
func init() {
// Set colors
os.Setenv("MAGEFILE_ENABLE_COLOR", "true")
if _, err := os.Stat(".passwd"); err == nil {
VaultArgs = []string{"--vault-password-file", ".passwd"}
} else {
VaultArgs = []string{"--ask-vault-password"}
}
}
type Ansible struct {
Tags string
}
// Run Ansible
func (a Ansible) Run() error {
// extraVarsBytes, _ := json.Marshal(map[string]string{
// "cluster": a.Cluster,
// })
// Create arguments
args := []string{
fmt.Sprintf("ansible/main.yml"),
"--inventory",
fmt.Sprintf("ansible/inventories/vm.py"),
// "--extra-vars",
// string(extraVarsBytes),
}
args = append(args, VaultArgs...)
if a.Tags != "" {
args = append(args, "--tags", a.Tags)
}
return sh.RunV("ansible-playbook", args...)
}
// ! Help
// Default target to run when none is specified
// If not set, running mage will list available targets
var Default = Help
// : Show help.
func Help() error {
return sh.RunV("mage", "-l")
}
// ! VM
type VM mg.Namespace
// : Bootstrap development environment.
func (VM) Init() error { return Ansible{Tags: "init"}.Run() }
// : Starts VM cluster.
func (VM) Up() error { return sh.RunV("multipass", "start", "--all") }
// : Stops VM cluster.
func (VM) Down() error { return sh.RunV("multipass", "stop", "--all") }
// : Prints VM status.
func (VM) Status() error { return sh.RunV("multipass", "list") }
// : Destroy VM cluster.
func (VM) Destroy() error {
if err := sh.RunV("multipass", "stop", "--all"); err != nil {
return err
}
if err := sh.RunV("multipass", "delete", "--all"); err != nil {
return err
}
return sh.RunV("multipass", "purge")
}
// ! Setup
// : Setup cluster.
func (VM) Setup() error { return Ansible{Tags: "setup"}.Run() }
// ! K3s
type K3s mg.Namespace
// : K3S setup.
func (K3s) Up() error { return Ansible{Tags: "k3s-up"}.Run() }
// : K3S teardown.
func (K3s) Down() error { return Ansible{Tags: "k3s-down"}.Run() }
// ! Longhorn
type Longhorn mg.Namespace
// : Longhorn setup.
func (Longhorn) Up() error { return Ansible{Tags: "longhorn-up"}.Run() }
// : Longhorn teardown.
func (Longhorn) Down() error { return Ansible{Tags: "longhorn-down"}.Run() }
// ! Cloudnative PG
type Cloudnative mg.Namespace
// : Cloudnative PG setup.
func (Cloudnative) Up() error { return Ansible{Tags: "cloudnative-pg-up"}.Run() }
// : Cloudnative PG teardown.
func (Cloudnative) Down() error { return Ansible{Tags: "cloudnative-pg-down"}.Run() }
// : Cloudnative PG backup.
func (Cloudnative) Backup() error { return Ansible{Tags: "cloudnative-pg-backup"}.Run() }