Skip to content

Instantly share code, notes, and snippets.

@fearoffish
Last active March 27, 2020 14:25
Show Gist options
  • Save fearoffish/30517e268d2715f32a6f300f89826121 to your computer and use it in GitHub Desktop.
Save fearoffish/30517e268d2715f32a6f300f89826121 to your computer and use it in GitHub Desktop.
Check terraform state in a set subdirectories `./check_state.sh staging`
#!/usr/bin/env bash
# Stop on ctrl-c
trap 'exit 130' INT
ENV=$1
# Check the remote cluster state against terraform state
function check_state() {
cd $1
printf "Checking ${1}..."
EXIT_CODE=`terraform plan -detailed-exitcode`
if [ $? -eq 1 ]
then
printf "errors found\n"
elif [ $? -eq 2 ]
then
printf "needs applying\n"
else
printf "okay\n"
fi
cd ~-
return 0
}
if [[ -z $ENV ]];
then
printf "No argument for terraform folder given. Checking all.\n\n"
check_state 'global'
check_state 'mgmt'
check_state 'staging/data_storage'
check_state 'staging/repository'
check_state 'staging/services/app'
check_state 'staging/services/bastion'
check_state 'production/data_storage'
check_state 'production/repository'
check_state 'production/services/app'
check_state 'production/services/bastion'
else
check_state $ENV
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment