Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle TF_DATA_DIR and Error Logging for !terraform.output #1037

Closed
wants to merge 8 commits into from
Closed
8 changes: 4 additions & 4 deletions internal/exec/terraform_outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func GetTerraformOutput(
p.Quit()
<-spinnerDone
fmt.Printf("\r✗ %s\n", message)
u.LogErrorAndExit(err)
u.LogErrorAndExit(fmt.Errorf("failed to describe the component '%s' in the stack '%s': %w", component, stack, err))
}

// Check if the component in the stack is configured with the 'static' remote state backend, in which case get the
Expand All @@ -269,7 +269,7 @@ func GetTerraformOutput(
p.Quit()
<-spinnerDone
fmt.Printf("\r✗ %s\n", message)
u.LogErrorAndExit(err)
u.LogErrorAndExit(fmt.Errorf("failed to get remote state backend static type outputs: %w", err))
}

var result any
Expand All @@ -284,7 +284,7 @@ func GetTerraformOutput(
p.Quit()
<-spinnerDone
fmt.Printf("\r✗ %s\n", message)
u.LogErrorAndExit(err)
u.LogErrorAndExit(fmt.Errorf("failed to execute terraform output for the component '%s' in the stack '%s': %w", component, stack, err))
}

// Cache the result
Expand Down Expand Up @@ -314,7 +314,7 @@ func getTerraformOutputVariable(

res, err := u.EvaluateYqExpression(atmosConfig, outputs, val)
if err != nil {
u.LogErrorAndExit(fmt.Errorf("error evaluating terrform output '%s' for the component '%s' in the stack '%s':\n%v",
u.LogErrorAndExit(fmt.Errorf("error evaluating terraform output '%s' for the component '%s' in the stack '%s':\n%v",
output,
component,
stack,
Expand Down
45 changes: 42 additions & 3 deletions internal/exec/terraform_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import (
"errors"
"fmt"

Check failure on line 5 in internal/exec/terraform_utils.go

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, linux)

"fmt" imported and not used

Check failure on line 5 in internal/exec/terraform_utils.go

View workflow job for this annotation

GitHub Actions / Build (windows-latest, windows)

"fmt" imported and not used

Check failure on line 5 in internal/exec/terraform_utils.go

View workflow job for this annotation

GitHub Actions / Build (macos-latest, macos)

"fmt" imported and not used
"os"
"path/filepath"

Expand All @@ -25,9 +25,48 @@
// We delete the file to prevent the Terraform prompt asking to select the default or the
// previously used workspace. This happens when different backends are used for the same component.
func cleanTerraformWorkspace(atmosConfig schema.AtmosConfiguration, componentPath string) {
filePath := filepath.Join(componentPath, ".terraform", "environment")
u.LogDebug(fmt.Sprintf("\nDeleting Terraform environment file:\n'%s'", filePath))
_ = os.Remove(filePath)
// Get TF_DATA_DIR, default to .terraform if not set
tfDataDir := os.Getenv("TF_DATA_DIR")
if tfDataDir == "" {
tfDataDir = ".terraform"
}

// Convert relative path to absolute
if !filepath.IsAbs(tfDataDir) {
tfDataDir = filepath.Join(componentPath, tfDataDir)
}

// Ensure the path is cleaned properly
tfDataDir = filepath.Clean(tfDataDir)

// Construct the full file path
filePath := filepath.Join(tfDataDir, "environment")

// Check if the file exists before attempting deletion
if _, err := os.Stat(filePath); err == nil {
l.LogDebug("Terraform environment file found. Proceeding with deletion.",

Check failure on line 47 in internal/exec/terraform_utils.go

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, linux)

undefined: l

Check failure on line 47 in internal/exec/terraform_utils.go

View workflow job for this annotation

GitHub Actions / Build (windows-latest, windows)

undefined: l

Check failure on line 47 in internal/exec/terraform_utils.go

View workflow job for this annotation

GitHub Actions / Build (macos-latest, macos)

undefined: l
"file", filePath,
)
if err := os.Remove(filePath); err != nil {
l.LogDebug("Failed to delete Terraform environment file.",

Check failure on line 51 in internal/exec/terraform_utils.go

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, linux)

undefined: l

Check failure on line 51 in internal/exec/terraform_utils.go

View workflow job for this annotation

GitHub Actions / Build (windows-latest, windows)

undefined: l

Check failure on line 51 in internal/exec/terraform_utils.go

View workflow job for this annotation

GitHub Actions / Build (macos-latest, macos)

undefined: l
"file", filePath,
"error", err,
)
} else {
l.LogDebug("Successfully deleted Terraform environment file.",

Check failure on line 56 in internal/exec/terraform_utils.go

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, linux)

undefined: l

Check failure on line 56 in internal/exec/terraform_utils.go

View workflow job for this annotation

GitHub Actions / Build (windows-latest, windows)

undefined: l

Check failure on line 56 in internal/exec/terraform_utils.go

View workflow job for this annotation

GitHub Actions / Build (macos-latest, macos)

undefined: l
"file", filePath,
)
}
} else if os.IsNotExist(err) {
l.LogDebug("Terraform environment file not found. No action needed.",

Check failure on line 61 in internal/exec/terraform_utils.go

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, linux)

undefined: l

Check failure on line 61 in internal/exec/terraform_utils.go

View workflow job for this annotation

GitHub Actions / Build (windows-latest, windows)

undefined: l

Check failure on line 61 in internal/exec/terraform_utils.go

View workflow job for this annotation

GitHub Actions / Build (macos-latest, macos)

undefined: l
"file", filePath,
)
} else {
l.LogDebug("Error checking Terraform environment file.",

Check failure on line 65 in internal/exec/terraform_utils.go

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, linux)

undefined: l

Check failure on line 65 in internal/exec/terraform_utils.go

View workflow job for this annotation

GitHub Actions / Build (windows-latest, windows)

undefined: l

Check failure on line 65 in internal/exec/terraform_utils.go

View workflow job for this annotation

GitHub Actions / Build (macos-latest, macos)

undefined: l
"file", filePath,
"error", err,
)
}
}

func shouldProcessStacks(info *schema.ConfigAndStacksInfo) (bool, bool) {
Expand Down
Loading