Skip to content

Instantly share code, notes, and snippets.

@buck4roo
Last active October 5, 2016 21:45
Show Gist options
  • Save buck4roo/250a03d5a321701d1fb945b7cc1c5c86 to your computer and use it in GitHub Desktop.
Save buck4roo/250a03d5a321701d1fb945b7cc1c5c86 to your computer and use it in GitHub Desktop.
version management checks for terraform environments
#!/bin/bash
# Script assumes a recent enough version of bash (=~ perfomrs REGEX matching)
[[ "${DEBUG}" ]] && { env|sort ; set -x ; }
if [[ -z ${TF_MIN_VERSION_REGEX} ]] ; then
echo minimum version check for terraform binary is NOT enforced.
exit 0
fi
function get_tf_version () {
local _TF_VERSION=$(${TF} version | grep ^Terraform | cut -f2 -dv)
[[ "${DEBUG}" ]] && echo tf version found: ${_TF_VERSION} >>/dev/stderr
echo ${_TF_VERSION}
}
function main () {
# if auto_switch is true, always unlink and link:
if [[ -n "${TF_VERSION_AUTO_SWITCH}" &&
! ${INSTALLED_TF_VERSION} =~ ${TF_MIN_VERSION_REGEX} ]] ; then
brew switch terraform ${TF_VERSION_DESIRED} >/dev/null &&
echo installed version of terraform was switched to ${TF_VERSION_DESIRED}
else
# Assume OSX for now:
# warn if switch needed
[[ ${INSTALLED_TF_VERSION} =~ ${TF_MIN_VERSION_REGEX} ]] ||
{
echo "This repo requires a different version of terraform be installed."
echo ;
echo " use: 'brew switch terraform <version>'" ;
echo ;
echo "to switch the installed version of terraform to one that" ;
echo "matches the per-environment defined string check" ;
echo "regex: ${TF_MIN_VERSION_REGEX}" ;
echo ;
echo "your current version of terraform appears to be ${INSTALLED_TF_VERSION}" ;
exit 1 ;
}
exit 0
fi
}
# init variables
INSTALLED_TF_VERSION=$(get_tf_version)
main
Raw
@@ -10,6 +10,7 @@ TF_ROOT := $(shell dirname $$(dirname $(realpath $(lastword $(MAKEFILE_LIST)))))
TF := $(TF_ROOT)/terraform.sh
# This file declares our useful Makefile targets:
+include ./variables.makefile
include ../lib/check.makefile
include ../lib/environment-toplevel-Makefile
.
one-of-many-checks:
# Check that our terraform script is being used:
@$(TF) check >/dev/null || { echo "terraform helper script missing" ; exit 1 ; }
@if $$(type ./check-env.sh 2>/dev/null >/dev/null) ; then \
[[ "${DEBUG}" ]] && echo executing ./check-env.sh ; \
TF=$(TF) ./check-env.sh ; \
elif $$(type ../check-env.sh 2>/dev/null >/dev/null) ; then \
[[ "${DEBUG}" ]] && echo executing ../check-env.sh ; \
TF=$(TF) ../check-env.sh ; \
elif $$(type ../../check-env.sh 2>/dev/null >/dev/null) ; then \
[[ "${DEBUG}" ]] && echo executing ../../check-env.sh ; \
TF=$(TF) ../../check-env.sh ; \
else \
echo unable to find check-env in . .. or ../.. ; \
exit 1 ; \
fi
...
export TF_MIN_VERSION_REGEX := ^0.6
export TF_VERSION_AUTO_SWITCH := true
export TF_VERSION_DESIRED := 0.6.16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment