Created
November 26, 2020 10:56
-
-
Save karstenmueller/fb319c9d798a6fd2557c30a2ffd5490b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -o nounset -o errexit -o pipefail | |
SCRIPT_DIR=${SCRIPT_DIR:="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"} | |
TF_CONTEXT="${TF_CONTEXT:-}" # name of the game | |
TF_IMAGE_VERSION="${TF_IMAGE_VERSION:-$(cat .terraform-version)}" | |
usage="usage: $(basename "$0") <plan|apply|destroy|validate|fmt> <module>" | |
subcommand="${1:-plan}" | |
module="${2:-}" | |
case "$subcommand" in | |
-h | --help) | |
echo "$usage" | |
exit 2 | |
;; | |
*) ;; | |
esac | |
if [ "X$module" == "X" ]; then | |
echo "ERROR: module is missing" | |
echo "$usage" | |
exit 2 | |
fi | |
echo() { | |
local string="$1" | |
export beg='\033[0;36m' # Cyan | |
export end='\033[0m' # No Color | |
builtin echo -e "$beg--> $string$end" | |
} | |
export -f echo | |
checkenv() { | |
local env_vars=("$@") | |
local error | |
msg="ERROR required environment variables are unset: " | |
for var in "${env_vars[@]}"; do | |
if [[ -z ${!var+x} ]]; then # indirect expansion here | |
error=1 | |
msg+=" $var " | |
fi | |
done | |
if [ "$error" ]; then | |
echo "$msg" | |
return 1 | |
fi | |
} | |
setup() { | |
echo "Preparing dockerized terraform..." | |
docker pull "hashicorp/terraform:$TF_IMAGE_VERSION" | |
function terraform() { | |
echo "Executing: terraform $@" | |
TF_IN_AUTOMATION=${TF_IN_AUTOMATION:=true} | |
docker run --rm \ | |
-v "$SCRIPT_DIR:/repo" \ | |
-w /repo \ | |
-e TF_IN_AUTOMATION="$TF_IN_AUTOMATION" \ | |
-e AWS_PROFILE="$AWS_PROFILE" \ | |
-e AWS_ACCESS_KEY_ID="$AWS_ACCESS_KEY_ID" \ | |
-e AWS_SECRET_ACCESS_KEY="$AWS_SECRET_ACCESS_KEY" \ | |
-e AWS_SESSION_TOKEN="$AWS_SESSION_TOKEN" \ | |
hashicorp/terraform:"$TF_IMAGE_VERSION" "$@" | |
} | |
export -f terraform | |
} | |
cleanup() { | |
echo "Cleanup..." | |
find . -type d -name .terraform -exec rm -rf \{\} + || : | |
unset terraform || : | |
} | |
trap cleanup EXIT ERR | |
# ----------------------------------------------------------------------- | |
# Check and setup environment | |
# ----------------------------------------------------------------------- | |
environment=( | |
AWS_PROFILE AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN | |
) | |
checkenv "${environment[@]}" | |
setup | |
# ----------------------------------------------------------------------- | |
# Run terraform | |
# ----------------------------------------------------------------------- | |
declare -a terraform_options | |
if [ -f "$module/$TF_CONTEXT.tfvars" ]; then | |
terraform_options+=("-var-file=$module/$TF_CONTEXT.tfvars") | |
fi | |
declare -a terraform_init_options | |
terraform_init_options+=("-lock=false") | |
terraform_init_options+=("$module") | |
terraform init "${terraform_init_options[@]}" | |
case "$subcommand" in | |
plan) | |
terraform_options+=("-detailed-exitcode" "$module") | |
terraform "$subcommand" "${terraform_options[@]}" | |
;; | |
apply | destroy) | |
terraform_options+=("-auto-approve" "$module") | |
terraform "$subcommand" "${terraform_options[@]}" | |
;; | |
validate | fmt) | |
terraform "$subcommand" | |
;; | |
-h | --help) | |
echo "$usage" | |
exit 2 | |
;; | |
*) | |
[ -n "$subcommand" ] && echo "Unknown option '$subcommand'!" | |
echo "$usage" | |
exit 2 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment