-
Notifications
You must be signed in to change notification settings - Fork 20
/
apply.go
353 lines (314 loc) · 10 KB
/
apply.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
package apply
import (
"bytes"
"fmt"
"io"
"net/url"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/chanzuckerberg/fogg/config"
"github.com/chanzuckerberg/fogg/plan"
"github.com/chanzuckerberg/fogg/templates"
"github.com/chanzuckerberg/fogg/util"
"github.com/gobuffalo/packr"
getter "github.com/hashicorp/go-getter"
"github.com/hashicorp/hcl/hcl/printer"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
)
const rootPath = "terraform"
func Apply(fs afero.Fs, conf *config.Config, tmp *templates.T, siccMode bool) error {
p, err := plan.Eval(conf, siccMode, false)
if err != nil {
return errors.Wrap(err, "unable to evaluate plan")
}
e := applyRepo(fs, p, &tmp.Repo)
if e != nil {
return errors.Wrap(e, "unable to apply repo")
}
e = applyAccounts(fs, p, &tmp.Account)
if e != nil {
return errors.Wrap(e, "unable to apply accounts")
}
e = applyEnvs(fs, p, &tmp.Env, &tmp.Component)
if e != nil {
return errors.Wrap(e, "unable to apply envs")
}
e = applyGlobal(fs, p.Global, &tmp.Global)
if e != nil {
return errors.Wrap(e, "unable to apply global")
}
e = applyModules(fs, p.Modules, &tmp.Module)
return errors.Wrap(e, "unable to apply modules")
}
func applyRepo(fs afero.Fs, p *plan.Plan, repoTemplates *packr.Box) error {
e := applyTree(repoTemplates, fs, "", p.SiccMode, p)
if e != nil {
return e
}
// Remove after migration.
if !p.SiccMode {
e := fs.Remove(".sicc-version")
if e != nil {
log.Debug("error removing .sicc-version. ignoring")
}
}
return nil
}
func applyGlobal(fs afero.Fs, p plan.Component, repoBox *packr.Box) error {
path := fmt.Sprintf("%s/global", rootPath)
e := fs.MkdirAll(path, 0755)
if e != nil {
return errors.Wrapf(e, "unable to make directory %s", path)
}
return applyTree(repoBox, fs, path, p.SiccMode, p)
}
func applyAccounts(fs afero.Fs, p *plan.Plan, accountBox *packr.Box) (e error) {
for account, accountPlan := range p.Accounts {
path := fmt.Sprintf("%s/accounts/%s", rootPath, account)
e = fs.MkdirAll(path, 0755)
if e != nil {
return errors.Wrap(e, "unable to make directories for accounts")
}
e = applyTree(accountBox, fs, path, accountPlan.SiccMode, accountPlan)
if e != nil {
return errors.Wrap(e, "unable to apply templates to account")
}
}
return nil
}
func applyModules(fs afero.Fs, p map[string]plan.Module, moduleBox *packr.Box) error {
var e error
for module, modulePlan := range p {
path := fmt.Sprintf("%s/modules/%s", rootPath, module)
e = fs.MkdirAll(path, 0755)
if e != nil {
return errors.Wrapf(e, "unable to make path %s", path)
}
e = applyTree(moduleBox, fs, path, modulePlan.SiccMode, modulePlan)
if e != nil {
return errors.Wrap(e, "unable to apply tree")
}
}
return nil
}
func applyEnvs(fs afero.Fs, p *plan.Plan, envBox *packr.Box, componentBox *packr.Box) (e error) {
for env, envPlan := range p.Envs {
path := fmt.Sprintf("%s/envs/%s", rootPath, env)
e = fs.MkdirAll(path, 0755)
if e != nil {
return errors.Wrapf(e, "unable to make directory %s", path)
}
e := applyTree(envBox, fs, path, envPlan.SiccMode, envPlan)
if e != nil {
return errors.Wrap(e, "unable to apply templates to env")
}
for component, componentPlan := range envPlan.Components {
path = fmt.Sprintf("%s/envs/%s/%s", rootPath, env, component)
e = fs.MkdirAll(path, 0755)
if e != nil {
return errors.Wrap(e, "unable to make directories for component")
}
e := applyTree(componentBox, fs, path, componentPlan.SiccMode, componentPlan)
if e != nil {
return errors.Wrap(e, "unable to apply templates for component")
}
if componentPlan.ModuleSource != nil {
e := applyModuleInvocation(fs, path, *componentPlan.ModuleSource, templates.Templates.ModuleInvocation)
if e != nil {
return errors.Wrap(e, "unable to apply module invocation")
}
}
}
}
return nil
}
func applyTree(source *packr.Box, dest afero.Fs, targetBasePath string, siccMode bool, subst interface{}) (e error) {
return source.Walk(func(path string, sourceFile packr.File) error {
extension := filepath.Ext(path)
target := getTargetPath(targetBasePath, path, siccMode)
targetExtension := filepath.Ext(target)
if extension == ".tmpl" {
e = applyTemplate(sourceFile, dest, target, subst)
if e != nil {
return errors.Wrap(e, "unable to apply template")
}
} else if extension == ".touch" {
e = touchFile(dest, target)
if e != nil {
return errors.Wrapf(e, "unable to touch file %s", target)
}
} else if extension == ".create" {
e = createFile(dest, target, sourceFile)
if e != nil {
return errors.Wrapf(e, "unable to create file %s", target)
}
} else {
log.Infof("%s copied", path)
e = afero.WriteReader(dest, path, sourceFile)
if e != nil {
return errors.Wrap(e, "unable to copy file")
}
}
if !siccMode && strings.HasSuffix(target, "fogg.tf") {
r := filepath.Join(targetBasePath, "sicc.tf")
log.Infof("%s removed", r)
e = dest.Remove(r)
if e != nil {
log.Debugf("error removing %s. ignoring", r)
}
}
if targetExtension == ".tf" {
e = fmtHcl(dest, target)
if e != nil {
return errors.Wrap(e, "unable to format HCL")
}
}
return nil
})
}
func fmtHcl(fs afero.Fs, path string) error {
in, e := afero.ReadFile(fs, path)
if e != nil {
return errors.Wrapf(e, "unable to read file %s", path)
}
out, e := printer.Format(in)
if e != nil {
return errors.Wrapf(e, "fmt hcl failed for %s", path)
}
return afero.WriteReader(fs, path, bytes.NewReader(out))
}
func touchFile(dest afero.Fs, path string) error {
_, err := dest.Stat(path)
if err != nil { // TODO we might not want to do this for all errors
log.Infof("%s touched", path)
_, err = dest.Create(path)
if err != nil {
return errors.Wrap(err, "unable to touch file")
}
} else {
log.Infof("%s skipped touch", path)
}
return nil
}
func createFile(dest afero.Fs, path string, sourceFile io.Reader) error {
_, err := dest.Stat(path)
if err != nil { // TODO we might not want to do this for all errors
log.Infof("%s created", path)
err = afero.WriteReader(dest, path, sourceFile)
if err != nil {
return errors.Wrap(err, "unable to create file")
}
} else {
log.Infof("%s skipped", path)
}
return nil
}
func removeExtension(path string) string {
return strings.TrimSuffix(path, filepath.Ext(path))
}
func applyTemplate(sourceFile io.Reader, dest afero.Fs, path string, overrides interface{}) error {
log.Infof("%s templated", path)
writer, err := dest.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return errors.Wrap(err, "unable to open file")
}
t := util.OpenTemplate(sourceFile)
return t.Execute(writer, overrides)
}
// This should really be part of the plan stage, not apply. But going to
// leave it here for now and re-think it when we make this mechanism
// general purpose.
type moduleData struct {
ModuleName string
ModuleSource string
Variables []string
Outputs []string
}
func applyModuleInvocation(fs afero.Fs, path, moduleAddress string, box packr.Box) error {
e := fs.MkdirAll(path, 0755)
if e != nil {
return errors.Wrapf(e, "couldn't create %s directory", path)
}
moduleConfig, e := util.DownloadAndParseModule(moduleAddress)
if e != nil {
return errors.Wrap(e, "could not download or parse module")
}
// This should really be part of the plan stage, not apply. But going to
// leave it here for now and re-think it when we make this mechanism
// general purpose.
variables := make([]string, 0)
for _, v := range moduleConfig.Variables {
variables = append(variables, v.Name)
}
sort.Strings(variables)
outputs := make([]string, 0)
for _, o := range moduleConfig.Outputs {
outputs = append(outputs, o.Name)
}
sort.Strings(outputs)
moduleName := filepath.Base(moduleAddress)
re := regexp.MustCompile(`\?ref=.*`)
moduleName = re.ReplaceAllString(moduleName, "")
moduleAddressForSource, _ := calculateModuleAddressForSource(path, moduleAddress)
// MAIN
f, e := box.Open("main.tf.tmpl")
if e != nil {
return errors.Wrap(e, "could not open template file")
}
e = applyTemplate(f, fs, filepath.Join(path, "main.tf"), &moduleData{moduleName, moduleAddressForSource, variables, outputs})
if e != nil {
return errors.Wrap(e, "unable to apply template for main.tf")
}
e = fmtHcl(fs, filepath.Join(path, "main.tf"))
if e != nil {
return errors.Wrap(e, "unable to format main.tf")
}
// OUTPUTS
f, e = box.Open("outputs.tf.tmpl")
if e != nil {
return errors.Wrap(e, "could not open template file")
}
e = applyTemplate(f, fs, filepath.Join(path, "outputs.tf"), &moduleData{moduleName, moduleAddressForSource, variables, outputs})
if e != nil {
return errors.Wrap(e, "unable to apply template for outputs.tf")
}
e = fmtHcl(fs, filepath.Join(path, "outputs.tf"))
if e != nil {
return errors.Wrap(e, "unable to format outputs.tf")
}
return nil
}
func calculateModuleAddressForSource(path, moduleAddress string) (string, error) {
// For cases where the module is a local path, we need to calculate the
// relative path from the component to the module.
// The module_source path in the fogg.json is relative to the repo root.
var moduleAddressForSource string
// getter will kinda normalize the module address, but it will actually be
// wrong for local file paths, so we need to calculate that ourselves below
s, e := getter.Detect(moduleAddress, path, getter.Detectors)
u, e := url.Parse(s)
if e != nil || u.Scheme == "file" {
// This indicates that we have a local path to the module.
// It is possible that this test is unreliable.
moduleAddressForSource, _ = filepath.Rel(path, moduleAddress)
} else {
moduleAddressForSource = moduleAddress
}
return moduleAddressForSource, nil
}
func getTargetPath(basePath, path string, siccMode bool) string {
target := filepath.Join(basePath, path)
extension := filepath.Ext(path)
if extension == ".tmpl" || extension == ".touch" || extension == ".create" {
target = removeExtension(target)
}
if siccMode && filepath.Base(target) == "fogg.tf" {
target = filepath.Join(filepath.Dir(target), "sicc.tf")
}
return target
}