forked from turnerlabs/fargate-create
/
upgrade.go
242 lines (208 loc) · 5.78 KB
/
upgrade.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
package cmd
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
)
var upgradeYes bool
var upgradeCmd = &cobra.Command{
Use: "upgrade",
Short: "Keep a terraform template up to date",
Run: doUpgrade,
Example: `
fargate-create upgrade
fargate-create upgrade -t git@github.com:turnerlabs/terraform-ecs-fargate-scheduled-task
fargate-create upgrade -d infrastructure
`,
}
func init() {
rootCmd.AddCommand(upgradeCmd)
}
func doUpgrade(cmd *cobra.Command, args []string) {
//has the user done a previous install?
notFound := false
if _, err := os.Stat(filepath.Join(targetDir, baseDir)); err != nil {
notFound = true
}
if _, err := os.Stat(filepath.Join(targetDir, envDir)); err != nil {
notFound = true
}
if notFound {
check(errors.New("no existing template found"))
}
//fetch the template from the source
templateDir := downloadTerraformTemplate()
debug("downloaded to:", templateDir)
//process /base first, then iterate over /env
srcDir := filepath.Join(templateDir, baseDir)
destDir := filepath.Join(targetDir, baseDir)
adds, updates := upgradeDirectory(srcDir, destDir)
//process each installed environment
srcDir = filepath.Join(templateDir, envDir, devDir)
objects, err := ioutil.ReadDir(filepath.Join(targetDir, envDir))
check(err)
for _, o := range objects {
if o.IsDir() {
destDir = filepath.Join(targetDir, envDir, o.Name())
debug(destDir)
//look for default terraform.tfvars file for this environment
tfVarsFile := filepath.Join(destDir, "terraform.tfvars")
debug(tfVarsFile)
if _, err = os.Stat(tfVarsFile); os.IsNotExist(err) {
check(errors.New(tfVarsFile + " not found"))
}
fileBits, err := ioutil.ReadFile(tfVarsFile)
check(err)
varFormat := strings.ToLower(filepath.Ext(tfVarsFile))
app, env, profile, region, _, err := parseInputVars(varFormat, string(fileBits))
check(err)
//apply env transformation in src before upgrading
transformMainTFToContext(srcDir, profile, app, env, region)
//upgrade env directory
a, u := upgradeDirectory(srcDir, destDir)
adds = append(adds, a...)
updates = append(updates, u...)
}
}
//delete download dir
os.RemoveAll(templateDir)
fmt.Println()
fmt.Println("---------------------------------------")
fmt.Printf("upgrade complete: %v add(s), %v update(s)\n", len(adds), len(updates))
if len(adds) > 0 || len(updates) > 0 {
fmt.Println()
fmt.Println("updated files:")
fmt.Println()
for _, s := range updates {
fmt.Printf("\t%s\n", s)
}
fmt.Println()
fmt.Println("added files:")
fmt.Println()
for _, s := range adds {
fmt.Printf("\t%s\n", s)
}
fmt.Println()
fmt.Println(`run the following commands to apply these changes:
terraform init -upgrade=true
terraform apply`)
}
}
func upgradeDirectory(srcDir string, destDir string) ([]string, []string) {
//prompt for updates to existing local files
//add new required files
//prompt to add new optional files (using fargate-create.yml)
fmt.Println()
fmt.Println("---------------------------------------")
fmt.Println("upgrading", destDir)
fmt.Println("---------------------------------------")
updates := []string{}
adds := []string{}
//iterate src files
files, err := ioutil.ReadDir(srcDir)
check(err)
for _, f := range files {
file := f.Name()
debug(file)
//only process .tf or .md files
if !(strings.HasSuffix(file, ".tf") || strings.HasSuffix(file, ".md") || strings.HasSuffix(file, ".tpl")) {
continue
}
//does matching file in template exist?
source := filepath.Join(srcDir, file)
dest := filepath.Join(destDir, file)
debug(fmt.Sprintf("source: %s | dest: %s", source, dest))
//can we access the source file?
if _, err := os.Stat(source); err == nil {
//does corresponding dest file exist?
if _, err = os.Stat(dest); os.IsNotExist(err) {
debug("new source file")
//is this file required or optional?
//load template config file
templateConfig := loadTemplateConfig(srcDir)
if templateConfig != nil {
prompt := getFilePrompt(templateConfig, file)
if prompt != nil {
//prompt to install new optional file
fmt.Println()
q := fmt.Sprintf("%s (%s) ", prompt.Question, prompt.Default)
response := promptAndGetResponse(q, prompt.Default)
if containsString(okayResponses, response) {
err = copyFile(source, dest)
check(err)
adds = append(adds, dest)
}
} else {
//write new required file
fmt.Println("writing", dest)
copyFile(source, dest)
adds = append(adds, dest)
}
} else {
debug("no template config file found")
}
} else {
//does dest file need updating?
debug("diffing")
if !deepCompare(source, dest) {
fmt.Println()
response := promptAndGetResponse(dest+" is out of date. Replace? (yes) ", "yes")
if containsString(okayResponses, response) {
err = copyFile(source, dest)
check(err)
updates = append(updates, dest)
}
} else {
debug("files match")
}
}
}
}
return adds, updates
}
func getFilePrompt(config *templateConfig, file string) *prompt {
for _, p := range config.Prompts {
for _, f := range p.FilesToDeleteIfNo {
if f == file {
return p
}
}
}
return nil
}
func deepCompare(file1, file2 string) bool {
chunkSize := 4000
f1, err := os.Open(file1)
if err != nil {
log.Fatal(err)
}
f2, err := os.Open(file2)
if err != nil {
log.Fatal(err)
}
for {
b1 := make([]byte, chunkSize)
_, err1 := f1.Read(b1)
b2 := make([]byte, chunkSize)
_, err2 := f2.Read(b2)
if err1 != nil || err2 != nil {
if err1 == io.EOF && err2 == io.EOF {
return true
} else if err1 == io.EOF || err2 == io.EOF {
return false
} else {
log.Fatal(err1, err2)
}
}
if !bytes.Equal(b1, b2) {
return false
}
}
}