Skip to content

Commit

Permalink
Always delete tfstate (#360)
Browse files Browse the repository at this point in the history
* Delete tfstate by default

* Always remove tfstate

* Always remove tfstate

* Flag -t as deprecated

---------

Co-authored-by: Yevhen Ivantsov <[email protected]>
  • Loading branch information
bianchi2 and Yevhen Ivantsov authored Jan 8, 2024
1 parent c8beba3 commit 5851bb3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 34 deletions.
24 changes: 4 additions & 20 deletions docs/docs/userguide/CLEANUP.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ The uninstallation script is located in the root folder of the project directory
Usage:

```shell
./uninstall.sh [-t] [-c <config.tfvars>]
./uninstall.sh [-c <config_file>] [-h] [-f] [-s]"
```
The following options are available:
- `-t` - Delete Terraform state files for all installed environment in the same region using the same AWS account.
- `-c <config_file_path>` - Pass a custom configuration file to uninstall the environment provisioned by it.
- `-f` - skip manual confirmation of the environment deletion."
- `-s` - skip refresh when running terraform destroy"
- `-h` - provides help to how executing this script."
!!!info "Uninstallation using default and custom configuration files"
Expand All @@ -37,21 +39,3 @@ The following options are available:
./uninstall.sh -c my-custom-config.tfvars
```
### Removing Terraform state files

We create an AWS S3 bucket and DynamoDB table to store the Terraform state of the environments for each region. Without the state information, Terraform cannot maintain the infrastructure.
All environments installed in the same region share one S3 bucket to store the state files.
By default, the uninstall script does not remove Terraform state files.

!!! warning "Remove Terraform state files only if you confirm there is no other installed environment in the same region."
If you have installed multiple environments using the same AWS account in the same region, you need to make sure all those environments are uninstalled before removing terraform state.

After deleting the state files, **Terraform cannot manage the installed environments**.

If you have **no other environment installed in the same region**, you may want to remove the Terraform state files permanently.
To remove Terraform state files permanently and delete AWS S3 bucket and DynamoDB, run the uninstallation script with the `-t` switch:

```shell
./uninstall.sh -t -c <config_file_path>
```

2 changes: 1 addition & 1 deletion test/e2etest/installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func runInstallScript(configPath string) {
func runUninstallScript(configPath string) {
cmd := &exec.Cmd{
Path: "uninstall.sh",
Args: []string{"uninstall.sh", "-c", configPath, "-f", "-t"},
Args: []string{"uninstall.sh", "-c", configPath, "-f"},
Stdout: os.Stdout,
Stderr: os.Stdout,
Dir: "../../",
Expand Down
23 changes: 10 additions & 13 deletions uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ show_help(){
cat << EOF
** WARNING **
This script destroys the infrastructure for Atlassian Data Center products in AWS environment. You may lose all application data.
The infrastructure will be removed by terraform. Also the terraform state could be removed if you use switch `-t` in uninstall command.
The infrastructure will be removed by terraform. Terraform state will be deleted as well.
EOF

fi
echo
echo "Usage: ./uninstall.sh [-c <config_file>] [-h] [-t]"
echo "Usage: ./uninstall.sh [-c <config_file>] [-h] [-t] [-f] [-s]"
echo " -c <config_file>: Terraform configuration file. The default value is 'config.tfvars' if the argument is not provided."
echo " -t : Cleaning up the terraform state S3 bucket permanently."
echo " Use this option only when there is no other environment installed in the region."
echo " -t : DEPRECATED. Skip cleaning up the terraform state S3 bucket permanently."
echo " -f : skip manual confirmation of the environment deletion."
echo " -s : skip refresh when running terraform destroy"
echo " -h : provides help to how executing this script."
echo
exit 2
Expand All @@ -39,16 +40,16 @@ EOF
# Extract arguments
CONFIG_FILE=
HELP_FLAG=
CLEAN_TFSTATE=
SKIP_CLEAN_TFSTATE=
FORCE_FLAG=
SKIP_REFRESH=
while getopts thfs?c: name ; do
case $name in
t) CLEAN_TFSTATE=1;; # Cleaning terraform state
t) SKIP_CLEAN_TFSTATE=1;; # Skip cleaning terraform state
h) HELP_FLAG=1; show_help;; # Help
c) CONFIG_FILE="${OPTARG}";; # Config file name to install - this overrides the default, 'config.tfvars'
f) FORCE_FLAG="-f";; # Force uninstall - Auto-approve
s) SKIP_REFRESH="-s";;
c) CONFIG_FILE="${OPTARG}";; # Config file name to install - this overrides the default, 'config.tfvars'
f) FORCE_FLAG="-f";; # Force uninstall - Auto-approve
s) SKIP_REFRESH="-s";; # Skip refreshing state when running terraform destroy
?) log "Invalid arguments." "ERROR"; show_help
esac
done
Expand Down Expand Up @@ -139,10 +140,6 @@ destroy_infrastructure() {
}

destroy_tfstate() {
# Check if the user passed '-s' parameter to skip removing tfstate
if [ -z "${CLEAN_TFSTATE}" ]; then
return
fi
echo
log "Attempting to remove terraform backend."
echo
Expand Down

0 comments on commit 5851bb3

Please sign in to comment.