terraform-app.sh

Wrapper for terraform that lets you specify an environment and calls an $ENV.tfvars file

0

Votes

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

Vote Here

You must earn at least 1 vote on your snippets to be allowed to vote

Terms Of Use

Privacy Policy

Featured snippets are MIT license

Gears & Masters

Advertise

DevOpsnipp.com © 2020

medium.png