Skip to content

Commit 74516e6

Browse files
authored
Clean Terraform workspace before executing terraform init in the atmos.Component template function (#775)
* updates * updates * updates * updates * updates * updates * updates
1 parent d502eb0 commit 74516e6

File tree

7 files changed

+26
-22
lines changed

7 files changed

+26
-22
lines changed

examples/quick-start-advanced/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ARG GEODESIC_OS=debian
66
# https://atmos.tools/
77
# https://github.com/cloudposse/atmos
88
# https://github.com/cloudposse/atmos/releases
9-
ARG ATMOS_VERSION=1.100.0
9+
ARG ATMOS_VERSION=1.105.0
1010

1111
# Terraform: https://github.com/hashicorp/terraform/releases
1212
ARG TF_VERSION=1.9.8

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/arsham/figurine v1.3.0
1010
github.com/bmatcuk/doublestar/v4 v4.7.1
1111
github.com/charmbracelet/bubbles v0.20.0
12-
github.com/charmbracelet/bubbletea v1.1.2
12+
github.com/charmbracelet/bubbletea v1.2.1
1313
github.com/charmbracelet/glamour v0.8.0
1414
github.com/charmbracelet/lipgloss v1.0.0
1515
github.com/elewis787/boa v0.1.2
@@ -22,15 +22,15 @@ require (
2222
github.com/hashicorp/go-getter v1.7.6
2323
github.com/hashicorp/hcl v1.0.0
2424
github.com/hashicorp/hcl/v2 v2.22.0
25-
github.com/hashicorp/terraform-config-inspect v0.0.0-20240801114854-6714b46f5fe4
25+
github.com/hashicorp/terraform-config-inspect v0.0.0-20241107133921-3adb156ecfe2
2626
github.com/hashicorp/terraform-exec v0.21.0
2727
github.com/ivanpirog/coloredcobra v1.0.1
2828
github.com/json-iterator/go v1.1.12
2929
github.com/jwalton/go-supportscolor v1.2.0
3030
github.com/kubescape/go-git-url v0.0.30
3131
github.com/lrstanley/bubblezone v0.0.0-20240914071701-b48c55a5e78e
3232
github.com/mitchellh/go-homedir v1.1.0
33-
github.com/mitchellh/go-wordwrap v1.0.0
33+
github.com/mitchellh/go-wordwrap v1.0.1
3434
github.com/mitchellh/mapstructure v1.5.0
3535
github.com/open-policy-agent/opa v0.70.0
3636
github.com/otiai10/copy v1.14.0
@@ -95,7 +95,7 @@ require (
9595
github.com/cenkalti/backoff/v3 v3.2.2 // indirect
9696
github.com/cespare/xxhash/v2 v2.3.0 // indirect
9797
github.com/chainguard-dev/git-urls v1.0.2 // indirect
98-
github.com/charmbracelet/x/ansi v0.4.2 // indirect
98+
github.com/charmbracelet/x/ansi v0.4.5 // indirect
9999
github.com/charmbracelet/x/term v0.2.0 // indirect
100100
github.com/cloudflare/circl v1.3.7 // indirect
101101
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be // indirect

go.sum

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -395,14 +395,14 @@ github.com/chainguard-dev/git-urls v1.0.2 h1:pSpT7ifrpc5X55n4aTTm7FFUE+ZQHKiqpiw
395395
github.com/chainguard-dev/git-urls v1.0.2/go.mod h1:rbGgj10OS7UgZlbzdUQIQpT0k/D4+An04HJY7Ol+Y/o=
396396
github.com/charmbracelet/bubbles v0.20.0 h1:jSZu6qD8cRQ6k9OMfR1WlM+ruM8fkPWkHvQWD9LIutE=
397397
github.com/charmbracelet/bubbles v0.20.0/go.mod h1:39slydyswPy+uVOHZ5x/GjwVAFkCsV8IIVy+4MhzwwU=
398-
github.com/charmbracelet/bubbletea v1.1.2 h1:naQXF2laRxyLyil/i7fxdpiz1/k06IKquhm4vBfHsIc=
399-
github.com/charmbracelet/bubbletea v1.1.2/go.mod h1:9HIU/hBV24qKjlehyj8z1r/tR9TYTQEag+cWZnuXo8E=
398+
github.com/charmbracelet/bubbletea v1.2.1 h1:J041h57zculJKEKf/O2pS4edXGIz+V0YvojvfGXePIk=
399+
github.com/charmbracelet/bubbletea v1.2.1/go.mod h1:viLoDL7hG4njLJSKU2gw7kB3LSEmWsrM80rO1dBJWBI=
400400
github.com/charmbracelet/glamour v0.8.0 h1:tPrjL3aRcQbn++7t18wOpgLyl8wrOHUEDS7IZ68QtZs=
401401
github.com/charmbracelet/glamour v0.8.0/go.mod h1:ViRgmKkf3u5S7uakt2czJ272WSg2ZenlYEZXT2x7Bjw=
402402
github.com/charmbracelet/lipgloss v1.0.0 h1:O7VkGDvqEdGi93X+DeqsQ7PKHDgtQfF8j8/O2qFMQNg=
403403
github.com/charmbracelet/lipgloss v1.0.0/go.mod h1:U5fy9Z+C38obMs+T+tJqst9VGzlOYGj4ri9reL3qUlo=
404-
github.com/charmbracelet/x/ansi v0.4.2 h1:0JM6Aj/g/KC154/gOP4vfxun0ff6itogDYk41kof+qk=
405-
github.com/charmbracelet/x/ansi v0.4.2/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
404+
github.com/charmbracelet/x/ansi v0.4.5 h1:LqK4vwBNaXw2AyGIICa5/29Sbdq58GbGdFngSexTdRM=
405+
github.com/charmbracelet/x/ansi v0.4.5/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
406406
github.com/charmbracelet/x/exp/golden v0.0.0-20240815200342-61de596daa2b h1:MnAMdlwSltxJyULnrYbkZpp4k58Co7Tah3ciKhSNo0Q=
407407
github.com/charmbracelet/x/exp/golden v0.0.0-20240815200342-61de596daa2b/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=
408408
github.com/charmbracelet/x/term v0.2.0 h1:cNB9Ot9q8I711MyZ7myUR5HFWL/lc3OpU8jZ4hwm0x0=
@@ -813,8 +813,8 @@ github.com/hashicorp/memberlist v0.5.0 h1:EtYPN8DpAURiapus508I4n9CzHs2W+8NZGbmmR
813813
github.com/hashicorp/memberlist v0.5.0/go.mod h1:yvyXLpo0QaGE59Y7hDTsTzDD25JYBZ4mHgHUZ8lrOI0=
814814
github.com/hashicorp/serf v0.10.1 h1:Z1H2J60yRKvfDYAOZLd2MU0ND4AH/WDz7xYHDWQsIPY=
815815
github.com/hashicorp/serf v0.10.1/go.mod h1:yL2t6BqATOLGc5HF7qbFkTfXoPIY0WZdWHfEvMqbG+4=
816-
github.com/hashicorp/terraform-config-inspect v0.0.0-20240801114854-6714b46f5fe4 h1:RwY5HBgtBZ997UtKJAO2Rx+94ETyevwWEVXWx1SL5YY=
817-
github.com/hashicorp/terraform-config-inspect v0.0.0-20240801114854-6714b46f5fe4/go.mod h1:Gz/z9Hbn+4KSp8A2FBtNszfLSdT2Tn/uAKGuVqqWmDI=
816+
github.com/hashicorp/terraform-config-inspect v0.0.0-20241107133921-3adb156ecfe2 h1:h+wcDC5wTiM+5YKjuLtUSWJtdtMVJNCRCPHoWieLpM4=
817+
github.com/hashicorp/terraform-config-inspect v0.0.0-20241107133921-3adb156ecfe2/go.mod h1:Gz/z9Hbn+4KSp8A2FBtNszfLSdT2Tn/uAKGuVqqWmDI=
818818
github.com/hashicorp/terraform-exec v0.21.0 h1:uNkLAe95ey5Uux6KJdua6+cv8asgILFVWkd/RG0D2XQ=
819819
github.com/hashicorp/terraform-exec v0.21.0/go.mod h1:1PPeMYou+KDUSSeRE9szMZ/oHf4fYUmB923Wzbq1ICg=
820820
github.com/hashicorp/terraform-json v0.22.1 h1:xft84GZR0QzjPVWs4lRUwvTcPnegqlyS7orfb5Ltvec=
@@ -986,8 +986,9 @@ github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77/go.
986986
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
987987
github.com/mitchellh/go-testing-interface v1.14.1 h1:jrgshOhYAUVNMAJiKbEu7EqAwgJJ2JqpQmpLJOu07cU=
988988
github.com/mitchellh/go-testing-interface v1.14.1/go.mod h1:gfgS7OtZj6MA4U1UrDRp04twqAjfvlZyCfX3sDjEym8=
989-
github.com/mitchellh/go-wordwrap v1.0.0 h1:6GlHJ/LTGMrIJbwgdqdl2eEH8o+Exx/0m8ir9Gns0u4=
990989
github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
990+
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
991+
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
991992
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
992993
github.com/mitchellh/mapstructure v1.3.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
993994
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
@@ -1531,17 +1532,13 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
15311532
golang.org/x/sys v0.0.0-20220906165534-d0df966e6959/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
15321533
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
15331534
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1534-
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
1535-
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
15361535
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
15371536
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
15381537
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
15391538
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
15401539
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
15411540
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
15421541
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
1543-
golang.org/x/term v0.25.0 h1:WtHI/ltw4NvSUig5KARz9h521QvRC8RmF/cuYqifU24=
1544-
golang.org/x/term v0.25.0/go.mod h1:RPyXicDX+6vLxogjjRxjgD2TKtmAO6NZBsBRfrOLu7M=
15451542
golang.org/x/term v0.26.0 h1:WEQa6V3Gja/BhNxg540hBip/kkaYtRg3cxg4oXSw4AU=
15461543
golang.org/x/term v0.26.0/go.mod h1:Si5m1o57C5nBNQo5z1iq+XDijt21BDBDp2bK0QI8e3E=
15471544
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

internal/exec/template_funcs_component.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ func componentFunc(cliConfig schema.CliConfiguration, component string, stack st
154154
ctx := context.Background()
155155

156156
// 'terraform init'
157+
// Before executing `terraform init`, delete the `.terraform/environment` file from the component directory
158+
cleanTerraformWorkspace(cliConfig, componentPath)
159+
157160
u.LogTrace(cliConfig, fmt.Sprintf("\nExecuting 'terraform init %s -s %s'", component, stack))
158161
err = tf.Init(ctx, tfexec.Upgrade(false))
159162
if err != nil {

internal/exec/terraform.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error {
285285
}
286286

287287
// Before executing `terraform init`, delete the `.terraform/environment` file from the component directory
288-
cleanTerraformWorkspace(componentPath)
288+
cleanTerraformWorkspace(cliConfig, componentPath)
289289

290290
err = ExecuteShellCommand(
291291
cliConfig,
@@ -369,7 +369,7 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error {
369369
}
370370
case "init":
371371
// Before executing `terraform init`, delete the `.terraform/environment` file from the component directory
372-
cleanTerraformWorkspace(componentPath)
372+
cleanTerraformWorkspace(cliConfig, componentPath)
373373

374374
if cliConfig.Components.Terraform.InitRunReconfigure {
375375
allArgsAndFlags = append(allArgsAndFlags, []string{"-reconfigure"}...)

internal/exec/terraform_utils.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package exec
22

33
import (
4+
"fmt"
45
"os"
5-
"path"
6+
"path/filepath"
67

78
"github.com/pkg/errors"
89

910
"github.com/cloudposse/atmos/pkg/schema"
11+
u "github.com/cloudposse/atmos/pkg/utils"
1012
)
1113

1214
func checkTerraformConfig(cliConfig schema.CliConfiguration) error {
@@ -23,6 +25,8 @@ func checkTerraformConfig(cliConfig schema.CliConfiguration) error {
2325
// helping Terraform identify the active workspace context for managing your infrastructure.
2426
// We delete the file to prevent the Terraform prompt asking to select the default or the
2527
// previously used workspace. This happens when different backends are used for the same component.
26-
func cleanTerraformWorkspace(componentPath string) {
27-
_ = os.Remove(path.Join(componentPath, ".terraform", "environment"))
28+
func cleanTerraformWorkspace(cliConfig schema.CliConfiguration, componentPath string) {
29+
filePath := filepath.Join(componentPath, ".terraform", "environment")
30+
u.LogDebug(cliConfig, fmt.Sprintf("\nDeleting Terraform environment file:\n'%s'", filePath))
31+
_ = os.Remove(filePath)
2832
}

website/docs/integrations/atlantis.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ on:
673673
branches: [ main ]
674674
675675
env:
676-
ATMOS_VERSION: 1.100.0
676+
ATMOS_VERSION: 1.105.0
677677
ATMOS_CLI_CONFIG_PATH: ./
678678
679679
jobs:

0 commit comments

Comments
 (0)