Skip to content

Instantly share code, notes, and snippets.

@kzap
Created June 28, 2016 15:54
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save kzap/2f9bf64df81d85a1c4475828b05c8f67 to your computer and use it in GitHub Desktop.
Save kzap/2f9bf64df81d85a1c4475828b05c8f67 to your computer and use it in GitHub Desktop.
Wrapper for terraform that lets you specify an environment and calls an $ENV.tfvars file, uses a $ENV.tfstate file and sets the env variable
#!/bin/bash
#
# terraform-app
#
# This to terraform the servers for the Galleon App
# By storing the date now, we can calculate the duration of provisioning at the
# end of this script.
start_seconds="$(date +%s)"
# get location of current dir
CWD="$(pwd)"
CONFIG="./"
TERRAFORM="terraform"
VERSION="0.1.0"
LOG=/tmp/terraform-app.log
ENV=
# change to script dir
cd "${0%/*}"
### FUNCTIONS
#
# Output usage information.
#
usage() {
cat <<-EOF
Usage: terraform-app [options] <env> [command]
Options:
-C, --chdir <path> change the working directory to <path>
-c, --config <path> set config path. defaults to ../opt/terraform/dreamcompute/
-V, --version output program version
-h, --help output help information
Commands:
[command] any terraform command and options
EOF
run_terraform --help
}
#
# Abort with <msg>
#
abort() {
echo
echo " $@" 1>&2
echo
exit 1
}
#
# Log <msg>.
#
log() {
echo " ○ $@"
}
#
# Set configuration file <path>.
#
set_config_path() {
test -f $1 || abort invalid --config path
CONFIG=$1
}
#
# Output version.
#
version() {
echo $VERSION
run_terraform --version
}
#
# Require environment arg.
#
require_env() {
test -z "$ENV" && abort "<env> required"
}
#
# Return the terraform command to run.
#
terraform_command() {
#local key="`config_get key`"
#test -n "$key" && local identity="-i $key"
echo "$TERRAFORM"
}
#
# Run the given terraform <cmd>.
#
run_terraform() {
local terraform="`terraform_command`"
echo $terraform "\"$@\"" >> $LOG
log $terraform $@
$terraform $@
}
#
# Plan
#
run_plan() {
run_terraform get $CONFIG
run_terraform plan --state=$CONFIG/$ENV.tfstate --var-file=$CONFIG/$ENV.tfvars --var="env=$ENV" $@ $CONFIG
}
#
# Apply
#
run_apply() {
run_terraform get $CONFIG
run_terraform apply --state=$CONFIG/$ENV.tfstate --var-file=$CONFIG/$ENV.tfvars --var="env=$ENV" $@ $CONFIG
}
#
# Show
#
run_show() {
run_terraform show $@ $CONFIG/$ENV.tfstate
}
### SCRIPT
#set -xv
# parse argv
while test $# -ne 0; do
arg=$1; shift
case $arg in
-h|--help) usage; exit ;;
-V|--version) version; exit ;;
-c|--config) set_config_path $1; shift ;;
-C|--chdir) log cd $1; cd $1; shift ;;
plan) require_env; run_plan $@; exit ;;
apply) require_env; run_apply $@; exit ;;
show) require_env; run_show $@; exit ;;
*)
if test -z "$ENV"; then
ENV=$arg;
else
run_terraform $ENV $arg $@
exit;
fi
;;
esac
done
run_terraform $ENV
#set +xv
# And it's done
end_seconds="$(date +%s)"
log Terraform complete in "$((${end_seconds} - ${start_seconds}))" seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment