diff --git a/internal/cli/command/controlplane/exec.go b/internal/cli/command/controlplane/exec.go index d610990f..acc3e51c 100644 --- a/internal/cli/command/controlplane/exec.go +++ b/internal/cli/command/controlplane/exec.go @@ -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) @@ -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") } diff --git a/internal/cli/command/controlplane/init.go b/internal/cli/command/controlplane/init.go index 7e042394..54f21d24 100644 --- a/internal/cli/command/controlplane/init.go +++ b/internal/cli/command/controlplane/init.go @@ -47,7 +47,8 @@ const ( "terraform": { "backend": { "local": { - "path": "/workspace/%s" + "path": "/workspace/%s", + "workspace_dir": "/workspace/" } } } diff --git a/internal/cli/command/controlplane/installer_options.go b/internal/cli/command/controlplane/installer_options.go index c9aa4ac4..572cd842 100644 --- a/internal/cli/command/controlplane/installer_options.go +++ b/internal/cli/command/controlplane/installer_options.go @@ -51,6 +51,7 @@ type cpContext struct { refreshState bool pullInstaller bool autoApprove bool + explicitState bool workspace string banzaiCli cli.Cli installerPulled *sync.Once @@ -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) { diff --git a/internal/cli/command/controlplane/up.go b/internal/cli/command/controlplane/up.go index 2e0b6c42..d17b52bd 100644 --- a/internal/cli/command/controlplane/up.go +++ b/internal/cli/command/controlplane/up.go @@ -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)) }