-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcmdmoverename.go
400 lines (344 loc) · 12 KB
/
cmdmoverename.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
package main
import (
"bufio"
"fmt"
"io"
"os"
"regexp"
"sort"
"strings"
"github.com/dexyk/stringosim"
"github.com/scylladb/go-set"
"github.com/scylladb/go-set/strset"
)
func doRename(upPath, downPath, planPath, localStatePath string, fuzzyMatch bool) error {
planFile, err := os.Open(planPath)
if err != nil {
return fmt.Errorf("opening the terraform plan file: %v", err)
}
defer planFile.Close()
upFile, err := os.Create(upPath)
if err != nil {
return fmt.Errorf("creating the up file: %v", err)
}
defer upFile.Close()
downFile, err := os.Create(downPath)
if err != nil {
return fmt.Errorf("creating the down file: %v", err)
}
defer downFile.Close()
create, destroy, err := parse(planFile)
if err != nil {
return fmt.Errorf("parse: %v", err)
}
upMatches, downMatches := matchExact(create, destroy)
msg := collectErrors(create, destroy)
if msg != "" && !fuzzyMatch {
return fmt.Errorf("matchExact:%v", msg)
}
if fuzzyMatch && create.Size() == 0 && destroy.Size() == 0 {
return fmt.Errorf("required fuzzy-match but there is nothing left to match")
}
if fuzzyMatch {
upMatches, downMatches, err = matchFuzzy(create, destroy)
if err != nil {
return fmt.Errorf("fuzzyMatch: %v", err)
}
msg := collectErrors(create, destroy)
if msg != "" {
return fmt.Errorf("matchFuzzy: %v", msg)
}
}
stateFlags := "-state=" + localStatePath
if err := upDownScript(upMatches, stateFlags, upFile); err != nil {
return fmt.Errorf("writing the up script: %v", err)
}
if err := upDownScript(downMatches, stateFlags, downFile); err != nil {
return fmt.Errorf("writing the down script: %v", err)
}
return nil
}
func doMoveAfter(script, before, after string) error {
beforePlanPath := before + ".tfplan"
beforePlanFile, err := os.Open(beforePlanPath)
if err != nil {
return fmt.Errorf("opening the terraform BEFORE plan file: %v", err)
}
defer beforePlanFile.Close()
afterPlanPath := after + ".tfplan"
afterPlanFile, err := os.Open(afterPlanPath)
if err != nil {
return fmt.Errorf("opening the terraform AFTER plan file: %v", err)
}
defer beforePlanFile.Close()
upPath := script + "_up.sh"
upFile, err := os.Create(upPath)
if err != nil {
return fmt.Errorf("creating the up file: %v", err)
}
defer upFile.Close()
downPath := script + "_down.sh"
downFile, err := os.Create(downPath)
if err != nil {
return fmt.Errorf("creating the down file: %v", err)
}
defer downFile.Close()
beforeCreate, beforeDestroy, err := parse(beforePlanFile)
if err != nil {
return fmt.Errorf("parse BEFORE plan: %v", err)
}
if beforeCreate.Size() > 0 {
return fmt.Errorf("BEFORE plan contains resources to create: %v",
sorted(beforeCreate.List()))
}
afterCreate, afterDestroy, err := parse(afterPlanFile)
if err != nil {
return fmt.Errorf("parse AFTER plan: %v", err)
}
if afterDestroy.Size() > 0 {
return fmt.Errorf("AFTER plan contains resources to destroy: %v",
sorted(afterDestroy.List()))
}
upMatches, downMatches := matchExact(afterCreate, beforeDestroy)
msg := collectErrors(afterCreate, beforeDestroy)
if msg != "" {
return fmt.Errorf("matchExact:%v", msg)
}
beforeStatePath := before + ".tfstate"
afterStatePath := after + ".tfstate"
upStateFlags := fmt.Sprintf("-state=%s -state-out=%s", beforeStatePath, afterStatePath)
downStateFlags := fmt.Sprintf("-state=%s -state-out=%s", afterStatePath, beforeStatePath)
if err := upDownScript(upMatches, upStateFlags, upFile); err != nil {
return fmt.Errorf("writing the up script: %v", err)
}
if err := upDownScript(downMatches, downStateFlags, downFile); err != nil {
return fmt.Errorf("writing the down script: %v", err)
}
return nil
}
func doMoveBefore(script, before, after string) error {
beforePlanPath := before + ".tfplan"
beforePlanFile, err := os.Open(beforePlanPath)
if err != nil {
return fmt.Errorf("opening the terraform BEFORE plan file: %v", err)
}
defer beforePlanFile.Close()
upPath := script + "_up.sh"
upFile, err := os.Create(upPath)
if err != nil {
return fmt.Errorf("creating the up file: %v", err)
}
defer upFile.Close()
downPath := script + "_down.sh"
downFile, err := os.Create(downPath)
if err != nil {
return fmt.Errorf("creating the down file: %v", err)
}
defer downFile.Close()
beforeCreate, beforeDestroy, err := parse(beforePlanFile)
if err != nil {
return fmt.Errorf("parse BEFORE plan: %v", err)
}
if beforeCreate.Size() == 0 {
return fmt.Errorf("BEFORE plan does not contain resources to create")
}
if beforeDestroy.Size() > 0 {
return fmt.Errorf("BEFORE plan contains resources to destroy: %s",
sorted(beforeDestroy.List()))
}
upMatches, downMatches := matchExact(beforeCreate, beforeCreate)
beforeStatePath := before + ".tfstate"
afterStatePath := after + ".tfstate"
upStateFlags := fmt.Sprintf("-state=%s -state-out=%s", afterStatePath, beforeStatePath)
downStateFlags := fmt.Sprintf("-state=%s -state-out=%s", beforeStatePath, afterStatePath)
if err := upDownScript(upMatches, upStateFlags, upFile); err != nil {
return fmt.Errorf("writing the up script: %v", err)
}
if err := upDownScript(downMatches, downStateFlags, downFile); err != nil {
return fmt.Errorf("writing the down script: %v", err)
}
return nil
}
func collectErrors(create *strset.Set, destroy *strset.Set) string {
msg := ""
if create.Size() != 0 {
msg += "\nunmatched create:\n " + strings.Join(sorted(create.List()), "\n ")
}
if destroy.Size() != 0 {
msg += "\nunmatched destroy:\n " + strings.Join(sorted(destroy.List()), "\n ")
}
return msg
}
// Parse the output of "terraform plan" and return two sets, the first a set of elements
// to be created and the second a set of elements to be destroyed. The two sets are
// unordered.
//
// For example:
// " # module.ci.aws_instance.docker will be destroyed"
// " # aws_instance.docker will be created"
// " # module.ci.module.workers["windows-vs2019"].aws_autoscaling_schedule.night_mode will be destroyed"
// " # module.workers["windows-vs2019"].aws_autoscaling_schedule.night_mode will be created"
func parse(rd io.Reader) (*strset.Set, *strset.Set, error) {
var re = regexp.MustCompile(`# (.+) will be (.+)`)
create := set.NewStringSet()
destroy := set.NewStringSet()
scanner := bufio.NewScanner(rd)
for scanner.Scan() {
line := scanner.Text()
if m := re.FindStringSubmatch(line); m != nil {
if len(m) != 3 {
return create, destroy,
fmt.Errorf("could not parse line %q: %q", line, m)
}
switch m[2] {
case "created":
create.Add(m[1])
case "destroyed":
destroy.Add(m[1])
case "read during apply":
// do nothing
default:
return create, destroy,
fmt.Errorf("line %q, unexpected action %q", line, m[2])
}
}
}
if err := scanner.Err(); err != nil {
return create, destroy, err
}
return create, destroy, nil
}
// Given two unordered sets create and destroy, perform an exact match from destroy to create.
//
// Return two maps, the first that exact matches each old element in destroy to the
// corresponding new element in create (up), the second that matches in the opposite
// direction (down).
//
// Modify the two input sets so that they contain only the remaining (if any) unmatched elements.
//
// The criterium used to perform a matchExact is that one of the two elements must be a
// prefix of the other.
// Note that the longest element could be the old or the new one, it depends on the inputs.
func matchExact(create, destroy *strset.Set) (map[string]string, map[string]string) {
// old -> new (or equvalenty: destroy -> create)
upMatches := map[string]string{}
downMatches := map[string]string{}
// 1. Create and destroy give us the direction:
// terraform state mv destroy[i] create[j]
// 2. But, for each resource, we need to know i,j so that we can match which old state
// we want to move to which new state, for example both are theoretically valid:
// terraform state mv module.ci.aws_instance.docker aws_instance.docker
// terraform state mv aws_instance.docker module.ci.aws_instance.docker
for _, d := range destroy.List() {
for _, c := range create.List() {
if strings.HasSuffix(c, d) || strings.HasSuffix(d, c) {
upMatches[d] = c
downMatches[c] = d
// Remove matched elements from the two sets.
destroy.Remove(d)
create.Remove(c)
}
}
}
// Now the two sets create, destroy contain only unmatched elements.
return upMatches, downMatches
}
// Given two unordered sets create and destroy, that have already been processed by
// matchExact(), perform a fuzzy match from destroy to create.
//
// Return two maps, the first that fuzzy matches each old element in destroy to the
// corresponding new element in create (up), the second that matches in the opposite
// direction (down).
//
// Modify the two input sets so that they contain only the remaining (if any) unmatched elements.
//
// The criterium used to perform a matchFuzzy is that one of the two elements must be a
// fuzzy match of the other, according to some definition of fuzzy.
// Note that the longest element could be the old or the new one, it depends on the inputs.
func matchFuzzy(create, destroy *strset.Set) (map[string]string, map[string]string, error) {
// old -> new (or equvalenty: destroy -> create)
upMatches := map[string]string{}
downMatches := map[string]string{}
type candidate struct {
distance int
create string
destroy string
}
candidates := []candidate{}
for _, d := range destroy.List() {
for _, c := range create.List() {
// Here we could also use a custom NGramSizes via
// stringosim.QGramSimilarityOptions
dist := stringosim.QGram([]rune(d), []rune(c))
candidates = append(candidates, candidate{dist, c, d})
}
}
sort.Slice(candidates,
func(i, j int) bool { return candidates[i].distance < candidates[j].distance })
for len(candidates) > 0 {
bestCandidate := candidates[0]
tmpCandidates := []candidate{}
for _, c := range candidates[1:] {
if bestCandidate.distance == c.distance {
if (bestCandidate.create == c.create) || (bestCandidate.destroy == c.destroy) {
return map[string]string{}, map[string]string{},
fmt.Errorf("ambiguous migration: {%s} -> {%s} or {%s} -> {%s}",
bestCandidate.create, bestCandidate.destroy,
c.create, c.destroy,
)
}
}
if (bestCandidate.create != c.create) && (bestCandidate.destroy != c.destroy) {
tmpCandidates = append(tmpCandidates, candidate{c.distance, c.create, c.destroy})
}
}
candidates = tmpCandidates
upMatches[bestCandidate.destroy] = bestCandidate.create
downMatches[bestCandidate.create] = bestCandidate.destroy
destroy.Remove(bestCandidate.destroy)
create.Remove(bestCandidate.create)
}
return upMatches, downMatches, nil
}
// Given a map old->new, create a script that for each element in the map issues the
// command: "terraform state mv old new".
func upDownScript(matches map[string]string, stateFlags string, out io.Writer) error {
fmt.Fprintf(out, "#! /bin/sh\n")
fmt.Fprintf(out, "# DO NOT EDIT. Generated by terravalet.\n")
fmt.Fprintf(out, "# terravalet_output_format=2\n")
fmt.Fprintf(out, "#\n")
fmt.Fprintf(out, "# This script will move %d items.\n\n", len(matches))
fmt.Fprintf(out, "set -e\n\n")
// -lock=false greatly speeds up operations when the state has many elements
// and is safe as long as we use -state=FILE, since this keeps operations
// strictly local, without considering the configured backend.
cmd := fmt.Sprintf("terraform state mv -lock=false %s", stateFlags)
// Go maps are unordered. We want instead a stable iteration order, to make it
// possible to compare scripts.
destroys := make([]string, 0, len(matches))
for d := range matches {
destroys = append(destroys, d)
}
sort.Strings(destroys)
i := 1
for _, d := range destroys {
fmt.Fprintf(out, "%s \\\n '%s' \\\n '%s'\n\n", cmd, d, matches[d])
i++
}
return nil
}
// sorted returns a sorted slice of strings.
// Useful to be able to write
//
// ... sorted(create.List()) ...
//
// instead of
//
// elems := create.List()
// sort.Strings(elems)
// ... elems ...
//
func sorted(in []string) []string {
sort.Strings(in)
return in
}