Skip to content

Commit

Permalink
fix remote state migration fix (#226)
Browse files Browse the repository at this point in the history
Signed-off-by: Nandor Kracser <[email protected]>
  • Loading branch information
bonifaido authored Mar 31, 2020
1 parent 9a4c8ac commit f827f52
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
8 changes: 7 additions & 1 deletion internal/cli/command/controlplane/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,13 @@ func runTerraformCommandGeneric(options *cpContext, cmd []string, cmdEnv map[str
"-v", fmt.Sprintf("%s:/terraform/state.tf.json", options.workspace+"/state.tf.json"),
"-v", fmt.Sprintf("%s:/terraform/.terraform/terraform.tfstate", options.workspace+"/.terraform/terraform.tfstate"),
}
if options.explicitState && options.tfstateExists() {
args = append(args, "-v", fmt.Sprintf("%s:/terraform/terraform.tfstate", options.tfstatePath()))
}
if options.banzaiCli.Interactive() {
args = append(args, "-ti")
}
for key, _ := range cmdEnv {
for key := range cmdEnv {
args = append(args, "-e", key)
}
return runDocker(cmd, options, args, cmdOpt)
Expand All @@ -275,6 +278,9 @@ func runTerraformCommandGeneric(options *cpContext, cmd []string, cmdEnv map[str
"--mount", fmt.Sprintf("type=bind,src=%s,dst=/terraform/state.tf.json,options=rbind:rw", options.workspace+"/state.tf.json"),
"--mount", fmt.Sprintf("type=bind,src=%s,dst=/terraform/.terraform/terraform.tfstate,options=rbind:rw", options.workspace+"/.terraform/terraform.tfstate"),
}
if options.explicitState && options.tfstateExists() {
args = append(args, "--mount", fmt.Sprintf("type=bind,src=%s,dst=/terraform/terraform.tfstate,options=rbind:rw", options.tfstatePath()))
}
if options.banzaiCli.Interactive() {
args = append(args, "-t")
}
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/command/controlplane/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ const (
"terraform": {
"backend": {
"local": {
"path": "/workspace/%s"
"path": "/workspace/%s",
"workspace_dir": "/workspace/"
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions internal/cli/command/controlplane/installer_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type cpContext struct {
refreshState bool
pullInstaller bool
autoApprove bool
explicitState bool
workspace string
banzaiCli cli.Cli
installerPulled *sync.Once
Expand Down Expand Up @@ -160,6 +161,11 @@ func (c *cpContext) tfstatePath() string {
return filepath.Join(c.workspace, tfstateFilename)
}

func (c *cpContext) tfstateExists() bool {
_, err := os.Stat(c.tfstatePath())
return err == nil
}

func (c *cpContext) deleteTfstate() error {
_, err := os.Stat(c.tfstatePath())
if os.IsNotExist(err) {
Expand Down
1 change: 1 addition & 0 deletions internal/cli/command/controlplane/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ func initStateBackend(options *cpContext, values map[string]interface{}, env map
if err != nil {
return errors.WrapIf(err, "failed to marshal state backend configuration")
}
options.explicitState = true
} else {
stateData = []byte(fmt.Sprintf(localStateBackend, tfstateFilename))
}
Expand Down

0 comments on commit f827f52

Please sign in to comment.