Skip to content

Commit

Permalink
Experimental feature to help with terraform state migrations (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardo Lopez authored Oct 18, 2018
1 parent 2ace513 commit eace5c9
Show file tree
Hide file tree
Showing 290 changed files with 144,664 additions and 7 deletions.
45 changes: 39 additions & 6 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions cmd/exp/exp.go
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,
}
20 changes: 20 additions & 0 deletions cmd/exp/migrate.go
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")
},
}
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

"github.com/chanzuckerberg/fogg/cmd/exp"
"github.com/chanzuckerberg/fogg/errs"
"github.com/fatih/color"
"github.com/spf13/cobra"
Expand All @@ -17,6 +18,8 @@ var (
func init() {
rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "enable verbose output")
rootCmd.PersistentFlags().BoolVarP(&quiet, "quiet", "q", false, "do not output to console; use return code to determine success/failure")

rootCmd.AddCommand(exp.ExpCmd)
}

var rootCmd = &cobra.Command{
Expand Down
114 changes: 114 additions & 0 deletions exp/migrate/migrate.go
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)
}
19 changes: 19 additions & 0 deletions vendor/github.com/antzucaro/matchr/COPYING.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions vendor/github.com/antzucaro/matchr/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit eace5c9

Please sign in to comment.