-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimental feature to help with terraform state migrations (#175)
- Loading branch information
Eduardo Lopez
authored
Oct 18, 2018
1 parent
2ace513
commit eace5c9
Showing
290 changed files
with
144,664 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package exp | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
// ExpCmd is a subcommand for experimental commands | ||
var ExpCmd = &cobra.Command{ | ||
Use: "exp", | ||
Short: "Experimental commands", | ||
Long: "Grouping of experimental commands. These are experimental and prone to change", | ||
SilenceUsage: true, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package exp | ||
|
||
import ( | ||
"github.com/chanzuckerberg/fogg/exp/migrate" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
ExpCmd.AddCommand(migrateCmd) | ||
} | ||
|
||
var migrateCmd = &cobra.Command{ | ||
Use: "migrate", | ||
Short: "Assists with terraform state migrations", | ||
Long: `This command aims to assist with terraform state migrations. | ||
Particularly when there are module renames and such.`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return migrate.Migrate("plan.tfplan") | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package migrate | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/antzucaro/matchr" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/hashicorp/terraform/terraform" | ||
"github.com/pkg/errors" | ||
"github.com/segmentio/go-prompt" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func generatePlan(planPath string) error { | ||
cmd := exec.Command("make", "init") | ||
cmd.Stdin = os.Stdin | ||
cmd.Stdout = os.Stdout | ||
err := cmd.Run() | ||
if err != nil { | ||
errors.Wrap(err, "Could not run make init") | ||
} | ||
|
||
cmd = exec.Command("make", "run") | ||
cmd.Env = append(cmd.Env, fmt.Sprintf("plan -out %s", planPath)) | ||
cmd.Stdin = os.Stdin | ||
cmd.Stdout = os.Stdout | ||
|
||
err = cmd.Run() | ||
return errors.Wrap(err, "Could not run terraform plan") | ||
} | ||
|
||
// parsePlan | ||
func parsePlan(planPath string) error { | ||
f, err := os.Open(planPath) | ||
if err != nil { | ||
return errors.Wrapf(err, "Could not read plan at %s", planPath) | ||
} | ||
defer f.Close() | ||
// TODO: also remove the plan? | ||
|
||
plan, err := terraform.ReadPlan(f) | ||
if err != nil { | ||
return errors.Wrapf(err, "Terraform could not parse plan at %s", planPath) | ||
} | ||
if plan.Diff == nil { | ||
log.Debug("nil diff") | ||
return nil | ||
} | ||
|
||
deletions := map[string]bool{} | ||
additions := map[string]bool{} | ||
|
||
for _, module := range plan.Diff.Modules { | ||
moduleName := strings.TrimPrefix(strings.Join(module.Path, "."), "root.") | ||
for name, instance := range module.Resources { | ||
fullName := fmt.Sprintf("%s.%s", moduleName, name) | ||
if instance.Destroy { | ||
deletions[fullName] = true | ||
} else { | ||
additions[fullName] = true | ||
} | ||
} | ||
} | ||
|
||
for addition := range additions { | ||
currScore := math.MaxInt64 | ||
var replace *string | ||
|
||
for deletion, ok := range deletions { | ||
if !ok { | ||
continue | ||
} | ||
score := matchr.DamerauLevenshtein(addition, deletion) | ||
if score < currScore { | ||
currScore = score | ||
replace = aws.String(deletion) | ||
} | ||
} | ||
|
||
if replace == nil { | ||
continue | ||
} | ||
|
||
if !prompt.Confirm("Would you like us to move %s to %s", *replace, addition) { | ||
continue | ||
} | ||
|
||
deletions[*replace] = false | ||
cmd := exec.Command("make", "run") | ||
cmd.Stdin = os.Stdin | ||
cmd.Stdout = os.Stdout | ||
cmd.Env = append(cmd.Env, fmt.Sprintf("CMD=state mv %s %s", *replace, addition)) | ||
|
||
err = cmd.Run() | ||
if err != nil { | ||
return errors.Wrapf(err, "Could not move %s to %s", *replace, addition) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Migrate migrates | ||
func Migrate(planPath string) error { | ||
defer os.Remove(planPath) | ||
err := generatePlan(planPath) | ||
if err != nil { | ||
return err | ||
} | ||
return parsePlan(planPath) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.