github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/scripts/purge-terraform.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  # Destroy any Terraform resources found in tfstate files within the repository.
     4  
     5  set -euo pipefail
     6  
     7  cd "$(dirname "$0")/.."
     8  
     9  echo -n "Are you sure you want to destroy all Terraform resources (y/n)? "
    10  read ans
    11  [[ "$ans" == y* ]] || exit 0
    12  
    13  failures=()
    14  
    15  for tfstate in $(find . -name '*.tfstate'); do
    16    dir=$(dirname "$tfstate")
    17    base=$(basename "$tfstate")
    18    if (set -x && cd "$dir" && terraform destroy -force -state="$base"); then
    19      rm "$tfstate"
    20    else
    21      failures+=("$tfstate")
    22    fi
    23  done
    24  
    25  # Terraform output is quite spammy, so we wait until all `terraform destroy`s
    26  # are finished before warning about tfstates that failed to destroy.
    27  for tfstate in "${failures[@]}"; do
    28    echo "warning: unable to destroy $tfstate" >&2
    29  done