Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
aknysh committed Nov 2, 2024
1 parent 16b18db commit 685b71e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 22 deletions.
33 changes: 20 additions & 13 deletions internal/exec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,19 +238,6 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error {
}
}

// Run `terraform init` before running other commands
runTerraformInit := true
if info.SubCommand == "init" ||
info.SubCommand == "clean" ||
(info.SubCommand == "deploy" && !cliConfig.Components.Terraform.DeployRunInit) {
runTerraformInit = false
}

if info.SkipInit {
u.LogDebug(cliConfig, "Skipping over 'terraform init' due to '--skip-init' flag being passed")
runTerraformInit = false
}

// Set `TF_IN_AUTOMATION` ENV var to `true` to suppress verbose instructions after terraform commands
// https://developer.hashicorp.com/terraform/cli/config/environment-variables#tf_in_automation
info.ComponentEnvList = append(info.ComponentEnvList, "TF_IN_AUTOMATION=true")
Expand All @@ -273,11 +260,28 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error {
}
}

// Run `terraform init` before running other commands
runTerraformInit := true
if info.SubCommand == "init" ||
info.SubCommand == "clean" ||
(info.SubCommand == "deploy" && !cliConfig.Components.Terraform.DeployRunInit) {
runTerraformInit = false
}

if info.SkipInit {
u.LogDebug(cliConfig, "Skipping over 'terraform init' due to '--skip-init' flag being passed")
runTerraformInit = false
}

if runTerraformInit {
initCommandWithArguments := []string{"init"}
if info.SubCommand == "workspace" || cliConfig.Components.Terraform.InitRunReconfigure {
initCommandWithArguments = []string{"init", "-reconfigure"}
}

// Before executing `terraform init`, delete the `.terraform/environment` file from the component directory
cleanTerraformWorkspace(componentPath)

err = ExecuteShellCommand(
cliConfig,
info.Command,
Expand Down Expand Up @@ -359,6 +363,9 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error {
allArgsAndFlags = append(allArgsAndFlags, []string{varFileFlag, varFile}...)
}
case "init":
// Before executing `terraform init`, delete the `.terraform/environment` file from the component directory
cleanTerraformWorkspace(componentPath)

if cliConfig.Components.Terraform.InitRunReconfigure {
allArgsAndFlags = append(allArgsAndFlags, []string{"-reconfigure"}...)
}
Expand Down
12 changes: 12 additions & 0 deletions internal/exec/terraform_utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package exec

import (
"os"
"path"

"github.com/pkg/errors"

"github.com/cloudposse/atmos/pkg/schema"
Expand All @@ -14,3 +17,12 @@ func checkTerraformConfig(cliConfig schema.CliConfiguration) error {

return nil
}

// cleanTerraformWorkspace deletes the `.terraform/environment` file from the component directory.
// The `.terraform/environment` file contains the name of the currently selected workspace,
// helping Terraform identify the active workspace context for managing your infrastructure.
// We delete the file to prevent the Terraform prompt asking o select the default or the
// previously used workspace. This happens when different backends are used for the same component.
func cleanTerraformWorkspace(componentPath string) {
_ = os.Remove(path.Join(componentPath, ".terraform", "environment"))
}
12 changes: 8 additions & 4 deletions pkg/utils/file_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,37 +197,41 @@ func SearchConfigFile(path string) (string, bool) {

// IsURL checks if a string is a URL
func IsURL(s string) bool {
url, err := url.Parse(s)
u, err := url.Parse(s)
if err != nil {
return false
}

validSchemes := []string{"http", "https"}
schemeValid := false
for _, scheme := range validSchemes {
if url.Scheme == scheme {
if u.Scheme == scheme {
schemeValid = true
break
}
}
return schemeValid

return schemeValid
}

// GetFileNameFromURL extracts the file name from a URL
func GetFileNameFromURL(rawURL string) (string, error) {
if rawURL == "" {
return "", fmt.Errorf("empty URL provided")
}

parsedURL, err := url.Parse(rawURL)
if err != nil {
return "", err
}

// Extract the path from the URL
urlPath := parsedURL.Path

// Get the base name of the path
fileName := path.Base(urlPath)
if fileName == "/" || fileName == "." {
return "", fmt.Errorf("unable to extract filename from URL: %s", rawURL)
}
// Get the base name of the path
return fileName, nil
}
8 changes: 4 additions & 4 deletions website/package-lock.json

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

2 changes: 1 addition & 1 deletion website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"marked": "^14.1.3",
"node-fetch": "^3.3.2",
"posthog-docusaurus": "^2.0.1",
"posthog-js": "^1.178.0",
"posthog-js": "^1.180.1",
"prism-react-renderer": "^2.4.0",
"raw-loader": "^4.0.2",
"react": "^18.3.1",
Expand Down

0 comments on commit 685b71e

Please sign in to comment.