-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmagefile.go
227 lines (189 loc) · 4.36 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
// +build mage
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"github.com/magefile/mage/mg" // mg contains helpful utility functions, like Deps
"github.com/pkg/errors"
)
type buildTarget struct {
name string
path string
}
var (
buildTargets = []buildTarget{
{name: "receiver", path: "./functions/receiver"},
}
)
// Building binaries
func Build() error {
for _, target := range buildTargets {
fmt.Println("Bulding ", target.name)
cmd := exec.Command("go", "build",
"-o", "build/"+target.name, target.path)
cmd.Env = append(os.Environ(), "GOARCH=amd64", "GOOS=linux")
err := cmd.Run()
if err != nil {
return err
}
}
return nil
}
// Run test of each handler
func doTest(path string) error {
cmd := exec.Command("go", "test", path, "-v")
out, err := cmd.CombinedOutput()
fmt.Printf(string(out))
return err
}
func Test() error {
fmt.Println("Testing...")
for _, target := range buildTargets {
err := doTest("./" + target.path)
if err != nil {
return err
}
}
return nil
}
type config struct {
StackName string
CodeS3Bucket string
CodeS3Prefix string
CodeS3Region string
Parameters []string
}
func loadConfigFile(fpath string) (config, error) {
cfg := config{}
cfg.Parameters = []string{}
fp, err := os.Open(fpath)
if err != nil {
return cfg, err
}
defer fp.Close()
scanner := bufio.NewScanner(fp)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if len(line) == 0 {
continue
}
idx := strings.Index(line, "=")
if idx < 0 {
log.Printf("Warning, invalid format of cfg file: '%s'\n", line)
continue
}
key := line[:idx]
value := line[(idx + 1):]
switch key {
case "StackName":
cfg.StackName = value
case "CodeS3Bucket":
cfg.CodeS3Bucket = value
case "CodeS3Prefix":
cfg.CodeS3Prefix = value
case "CodeS3Region":
cfg.CodeS3Region = value
default:
cfg.Parameters = append(cfg.Parameters, line)
}
}
return cfg, nil
}
func deployCFn(paramFile string) error {
cfg, err := loadConfigFile(paramFile)
if err != nil {
return err
}
templateFile := "template.yml"
var tmpPath string
if tf, err := ioutil.TempFile("", "slam_template_"); err != nil {
log.Fatal(err)
} else {
tmpPath = tf.Name()
tf.Close()
}
log.Printf("[%s] Packaging...\n", paramFile)
pkgCmd := exec.Command("aws", "cloudformation", "package",
"--template-file", templateFile,
"--s3-bucket", cfg.CodeS3Bucket,
"--s3-prefix", cfg.CodeS3Prefix,
"--output-template-file", tmpPath)
pkgOut, err := pkgCmd.CombinedOutput()
if err != nil {
log.Printf("[%s] Error: %s, %s", paramFile, string(pkgOut), err)
return err
}
log.Printf("[%s] Generated template file: %s\n", paramFile, tmpPath)
// fmt.Printf("Package > %s", string(pkgOut))
log.Printf("[%s] Deploy...\n", paramFile)
args := []string{
"--region", cfg.CodeS3Region,
"cloudformation", "deploy",
"--template-file", tmpPath,
"--stack-name", cfg.StackName,
"--capabilities", "CAPABILITY_IAM",
"--parameter-overrides",
}
args = append(args, cfg.Parameters...)
deployCmd := exec.Command("aws", args...)
deployOut, err := deployCmd.CombinedOutput()
if err != nil {
log.Println("[%s] Error: %s, %s", paramFile, string(deployOut), err)
return err
}
log.Printf("[%s] Done!", paramFile)
return nil
}
// Deploying CloudFormation stack
func Deploy() error {
mg.Deps(Build)
configFile := os.Getenv("PARAM_FILE")
configDir := os.Getenv("PARAM_DIR")
if configFile != "" {
err := deployCFn(configFile)
if err != nil {
return err
}
} else if configDir != "" {
files, err := ioutil.ReadDir(configDir)
if err != nil {
return errors.Wrap(err, "Fail to retrieve files in PARAM_DIR")
}
var wg sync.WaitGroup
for _, finfo := range files {
fpath := filepath.Join(configDir, finfo.Name())
if !strings.HasSuffix(fpath, ".cfg") || finfo.IsDir() {
continue
}
wg.Add(1)
go func(fname string) {
defer wg.Done()
err := deployCFn(fname)
if err != nil {
log.Printf("[%s] ERROR %s", fname, err)
}
}(fpath)
}
wg.Wait()
} else {
return errors.New("PARAM_FILE is not available. Set PARAM_FILE as environment variable.")
}
return nil
}
// Remove all built binaries
func Clean() error {
for _, target := range buildTargets {
err := os.RemoveAll("config/build/" + target.name)
if err != nil {
return err
}
}
return nil
}