Skip to content

Commit

Permalink
add skip lock file flag (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcalhoun committed Aug 7, 2023
1 parent 6eef34e commit cfb94ba
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
2 changes: 1 addition & 1 deletion internal/exec/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func processHelp(componentType string, command string) error {
u.PrintMessage(" - 'atmos terraform apply' and 'atmos terraform deploy' commands commands support '--planfile' flag to specify the path " +
"to a planfile. The '--planfile' flag should be used instead of the planfile argument in the native 'terraform apply <planfile>' command")
u.PrintMessage(" - 'atmos terraform clean' command deletes the '.terraform' folder, '.terraform.lock.hcl' lock file, " +
"and the previously generated 'planfile' and 'varfile' for the specified component and stack")
"and the previously generated 'planfile' and 'varfile' for the specified component and stack. Use --skip-lock-file flag to skip deleting the lock file.")
u.PrintMessage(" - 'atmos terraform workspace' command first runs 'terraform init -reconfigure', then 'terraform workspace select', " +
"and if the workspace was not created before, it then runs 'terraform workspace new'")
u.PrintMessage(" - 'atmos terraform import' command searches for 'region' in the variables for the specified component and stack, " +
Expand Down
18 changes: 11 additions & 7 deletions internal/exec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ package exec

import (
"fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"os"
"path"
"strings"

"github.com/pkg/errors"
"github.com/spf13/cobra"

cfg "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
)

const (
autoApproveFlag = "-auto-approve"
outFlag = "-out"
varFileFlag = "-var-file"
autoApproveFlag = "-auto-approve"
outFlag = "-out"
varFileFlag = "-var-file"
skipTerraformLockFileFlag = "--skip-lock-file"
)

// ExecuteTerraformCmd parses the provided arguments and flags and executes terraform commands
Expand Down Expand Up @@ -81,8 +83,10 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error {
u.LogWarning(cliConfig, err.Error())
}

u.LogTrace(cliConfig, "Deleting '.terraform.lock.hcl' file")
_ = os.Remove(path.Join(componentPath, ".terraform.lock.hcl"))
if !u.SliceContainsString(info.AdditionalArgsAndFlags, skipTerraformLockFileFlag) {
u.LogTrace(cliConfig, "Deleting '.terraform.lock.hcl' file")
_ = os.Remove(path.Join(componentPath, ".terraform.lock.hcl"))
}

u.LogTrace(cliConfig, fmt.Sprintf("Deleting terraform varfile: %s\n", varFile))
_ = os.Remove(path.Join(componentPath, varFile))
Expand Down
14 changes: 8 additions & 6 deletions website/docs/cli/commands/terraform/terraform-clean.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ component in a stack.
Execute the `terraform clean` command like this:

```shell
atmos terraform clean <component> -s <stack>
atmos terraform clean <component> -s <stack> [--skip-lock-file]
```

:::tip
Expand All @@ -28,6 +28,7 @@ Run `atmos terraform clean --help` to see all the available options
```shell
atmos terraform clean top-level-component1 -s tenant1-ue2-dev
atmos terraform clean infra/vpc -s tenant1-ue2-staging
atmos terraform clean infra/vpc -s tenant1-ue2-staging --skip-lock-file
atmos terraform clean test/test-component -s tenant1-ue2-dev
atmos terraform clean test/test-component-override-2 -s tenant2-ue2-prod
atmos terraform clean test/test-component-override-3 -s tenant1-ue2-dev
Expand All @@ -36,12 +37,13 @@ atmos terraform clean test/test-component-override-3 -s tenant1-ue2-dev
## Arguments

| Argument | Description | Required |
|:------------|:--------------------------|:---------|
| :---------- | :------------------------ | :------- |
| `component` | Atmos terraform component | yes |

## Flags

| Flag | Description | Alias | Required |
|:------------|:------------|:------|:---------|
| `--stack` | Atmos stack | `-s` | yes |
| `--dry-run` | Dry run | | no |
| Flag | Description | Alias | Required |
| :----------------- | :-------------------------------- | :---- | :------- |
| `--stack` | Atmos stack | `-s` | yes |
| `--dry-run` | Dry run | | no |
| `--skip-lock-file` | Skip deleting .terraform.lock.hcl | | no |
19 changes: 10 additions & 9 deletions website/docs/cli/commands/terraform/usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ sidebar_label: terraform
sidebar_class_name: command
---

import DocCardList from '@theme/DocCardList';
import DocCardList from "@theme/DocCardList";

:::note Purpose
Use these subcommands to interact with `terraform`.
Expand All @@ -19,15 +19,15 @@ atmos terraform <command> <component> -s <stack> [options]
atmos terraform <command> <component> --stack <stack> [options]
```

<br/>
<br />

:::info
Atmos supports all `terraform` commands and options described in [Terraform CLI reference](https://www.terraform.io/cli/commands).

In addition, the `component` argument and `stack` flag are required to generate variables and backend config for the component in the stack.
:::

<br/>
<br />

**Additions and differences from native `terraform`:**

Expand All @@ -53,7 +53,7 @@ In addition, the `component` argument and `stack` flag are required to generate
planfile

- `atmos terraform clean` command deletes the `.terraform` folder, `.terraform.lock.hcl` lock file, and the previously generated `planfile`
and `varfile` for the specified component and stack
and `varfile` for the specified component and stack. Use the `--skip-lock-file` flag to skip deleting the `.terraform.lock.hcl` file.

- `atmos terraform workspace` command first runs `terraform init -reconfigure`, then `terraform workspace select`, and if the workspace was not
created before, it then runs `terraform workspace new`
Expand All @@ -72,7 +72,7 @@ In addition, the `component` argument and `stack` flag are required to generate
- `atmos terraform shell` command configures an environment for an Atmos component in a stack and starts a new shell allowing executing all native
terraform commands inside the shell

<br/>
<br />

:::tip
Run `atmos terraform --help` to see all the available options
Expand All @@ -82,6 +82,7 @@ Run `atmos terraform --help` to see all the available options

```shell
atmos terraform plan test/test-component-override-3 -s tenant1-ue2-dev
atmos terraform plan test/test-component-override-3 -s tenant1-ue2-dev --skip-lock-file
atmos terraform plan test/test-component-override-2 -s tenant1-ue2-dev --redirect-stderr /dev/stdout
atmos terraform plan test/test-component-override -s tenant1-ue2-dev --redirect-stderr ./errors.txt

Expand All @@ -106,18 +107,18 @@ atmos terraform workspace test/test-component-override-3 -s tenant1-ue2-dev --re
## Arguments

| Argument | Description | Required |
|:------------|:--------------------------|:---------|
| :---------- | :------------------------ | :------- |
| `component` | Atmos terraform component | yes |

## Flags

| Flag | Description | Alias | Required |
|:--------------------|:----------------------------------------------------------------------------------------------------------------------------------------------|:------|:---------|
| :------------------ | :-------------------------------------------------------------------------------------------------------------------------------------------- | :---- | :------- |
| `--stack` | Atmos stack | `-s` | yes |
| `--dry-run` | Dry run | | no |
| `--redirect-stderr` | File descriptor to redirect `stderr` to.<br/>Errors can be redirected to any file or any standard file descriptor<br/>(including `/dev/null`) | | no |

<br/>
<br />

:::note

Expand All @@ -127,4 +128,4 @@ All native `terraform` flags are supported

## Subcommands

<DocCardList/>
<DocCardList />

0 comments on commit cfb94ba

Please sign in to comment.