Skip to content

Commit c46defc

Browse files
committed
Add support for multiple terraform versions
that may be overridden by individual infrastructures, with versions installed to a private cache directory.
1 parent a55eede commit c46defc

File tree

9 files changed

+50
-24
lines changed

9 files changed

+50
-24
lines changed

.example.env

+2
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ export GITHUB_USERNAME=yourname
88
export GITHUB_TOKEN=AFAFAFAFAFAFAFAFAFAFAFAFAFAF
99

1010
export TF_VAR_ssh_user=yourusername
11+
12+
export TF_INSTALLATION_PREFIX=$HOME/.cache/travis-terraform-config

.travis.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ sudo: required
55
cache:
66
directories:
77
- "${HOME}/bin"
8+
- "${HOME}/.cache/travis-terraform-config"
89
env:
910
global:
1011
- PATH="${HOME}/bin:${PATH}"
1112
- SHELLCHECK_URL="https://s3.amazonaws.com/travis-blue-public/binaries/ubuntu/14.04/x86_64/shellcheck-0.4.4.tar.bz2"
1213
- SHFMT_URL="mvdan.cc/sh/cmd/shfmt"
1314
- TMPDIR="${TMPDIR:-/tmp}"
1415
before_install:
15-
- ./bin/ensure-terraform "$(make -f terraform-common.mk .echo-tf-version)"
16+
- git ls-files '*/Makefile' | xargs -n 1 make .echo-tf-version -f 2>/dev/null | sort | uniq |
17+
while read -r tf_version;
18+
do ./bin/ensure-terraform "${tf_version}";
19+
done
1620
- if [[ ! -x "${HOME}/bin/shellcheck" ]]; then
1721
curl -sSL "${SHELLCHECK_URL}" | tar -C "${HOME}/bin" -xjf -;
1822
fi

bin/ensure-terraform

+9-15
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,18 @@ main() {
1111
fi
1212

1313
: "${TF_INSTALL_MISSING:=1}"
14-
: "${TF_INSTALLATION_PREFIX:=${HOME}/bin}"
14+
: "${TF_INSTALLATION_PREFIX:=${HOME}/.cache/travis-terraform-config}"
1515
: "${TF_DOWNLOAD_SERVER:=https://releases.hashicorp.com}"
1616
: "${TMPDIR:=/tmp}"
1717

18-
local tf_current='???'
19-
local tf="${TF_INSTALLATION_PREFIX}/terraform"
18+
mkdir -p "${TF_INSTALLATION_PREFIX}"
19+
local tf="${TF_INSTALLATION_PREFIX}/terraform-${tf_version}"
2020

21-
if [[ -x "${tf}" ]]; then
22-
local tf_current
23-
tf_current="$( ("${tf}" version || true) | head -n1 | cut -d ' ' -f 2)"
24-
fi
25-
26-
if [[ "${tf_current}" == "${tf_version}" ]]; then
27-
exit 0
28-
fi
29-
30-
if [[ "${TF_INSTALL_MISSING}" == 0 ]]; then
31-
echo "Terraform ${tf_version} required (current=${tf_current})" >&2
21+
if [[ ! -x "${tf}" && "${TF_INSTALL_MISSING}" == 0 ]]; then
22+
echo "===> Terraform ${tf_version} required."
23+
echo " Run this:"
24+
echo " ${0} ${tf_version}"
25+
echo
3226
exit 1
3327
fi
3428

@@ -43,7 +37,7 @@ main() {
4337
mv -v terraform "${tf}"
4438
chmod +x "${tf}"
4539
popd &>/dev/null
46-
terraform version
40+
"${tf}" version
4741
}
4842

4943
main "$@"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

macstadium-shared-1/Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
include $(shell git rev-parse --show-toplevel)/terraform.mk
22

3+
PROD_TF_VERSION := v0.9.11
4+
TERRAFORM := $(HOME)/.cache/travis-terraform-config/terraform-$(PROD_TF_VERSION)
5+
36
.PHONY: default
47
default: hello
58

macstadium-shared-2/.terraform-validate-flags

Whitespace-only changes.

macstadium-shared-2/Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
include $(shell git rev-parse --show-toplevel)/terraform.mk
22

3+
PROD_TF_VERSION := v0.9.11
4+
TERRAFORM := $(HOME)/.cache/travis-terraform-config/terraform-$(PROD_TF_VERSION)
5+
36
.PHONY: default
47
default: hello
58

runtests

+15-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ main() {
1010
git diff-index --quiet HEAD -- || (echo "$msg" && exit 1)
1111
fi
1212

13+
local terraform
14+
terraform="$(make -f terraform-common.mk .echo-tf)"
15+
1316
if [[ $# -gt 1 && $1 == --env ]]; then
1417
echo "Running isolated with env ${2}"
1518
exec env -i \
@@ -46,12 +49,22 @@ main() {
4649

4750
for d in $(git ls-files '*.tf' | xargs -n1 dirname | LC_ALL=C sort | uniq); do
4851
echo -en "${d} "
49-
terraform validate "${d}"
52+
if [[ -f "${d}/Makefile" ]]; then
53+
terraform="$(make -f "${d}/Makefile" .echo-tf)"
54+
else
55+
terraform="$(make -f terraform-common.mk .echo-tf)"
56+
fi
57+
if [[ -f "${d}/.terraform-validate-flags" ]]; then
58+
# shellcheck disable=SC2046
59+
"${terraform}" validate $(cat "${d}/.terraform-validate-flags") "${d}"
60+
else
61+
"${terraform}" validate -check-variables=false "${d}"
62+
fi
5063
echo ""
5164
done
5265

5366
echo 'Running terraform fmt'
54-
terraform fmt
67+
"${terraform}" fmt
5568
}
5669

5770
main "$@"

terraform-common.mk

+12-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ TRAVIS_BUILD_ORG_HOST ?= build.travis-ci.org
1010
JOB_BOARD_HOST ?= job-board.travis-ci.com
1111
AMQP_URL_VARNAME ?= AMQP_URL
1212
TOP := $(shell git rev-parse --show-toplevel)
13-
PROD_TF_VERSION := v0.9.11
13+
14+
PROD_TF_VERSION := v0.11.0
15+
TERRAFORM := $(HOME)/.cache/travis-terraform-config/terraform-$(PROD_TF_VERSION)
1416

1517
.PHONY: hello
1618
hello: announce
@@ -27,6 +29,10 @@ hello: announce
2729
.echo-tf-version:
2830
@echo $(PROD_TF_VERSION)
2931

32+
.PHONY: .echo-tf
33+
.echo-tf:
34+
@echo $(TERRAFORM)
35+
3036
.PHONY: .assert-tf-version
3137
.assert-tf-version:
3238
@TF_INSTALL_MISSING=0 $(TOP)/bin/ensure-terraform $(PROD_TF_VERSION)
@@ -37,20 +43,20 @@ announce: .assert-ruby .assert-tf-version
3743

3844
.PHONY: apply
3945
apply: announce .config $(TFVARS) $(TFSTATE)
40-
terraform apply $(TFPLAN)
46+
$(TERRAFORM) apply $(TFPLAN)
4147
$(TOP)/bin/post-flight $(TOP)
4248

4349
.PHONY: plan
4450
plan: announce .config $(TFVARS) $(TFSTATE)
45-
terraform plan \
51+
$(TERRAFORM) plan \
4652
-var-file=$(ENV_NAME).tfvars \
4753
-var-file=$(TFVARS) \
4854
-module-depth=-1 \
4955
-out=$(TFPLAN)
5056

5157
.PHONY: destroy
5258
destroy: announce .config $(TFVARS) $(TFSTATE)
53-
terraform plan \
59+
$(TERRAFORM) plan \
5460
-var-file=$(ENV_NAME).tfvars \
5561
-var-file=$(TFVARS) \
5662
-module-depth=-1 \
@@ -59,7 +65,7 @@ destroy: announce .config $(TFVARS) $(TFSTATE)
5965
$(TOP)/bin/post-flight $(TOP)
6066

6167
$(TFSTATE):
62-
terraform init
68+
$(TERRAFORM) init
6369

6470
.PHONY: clean
6571
clean: announce
@@ -71,7 +77,7 @@ distclean: clean
7177

7278
.PHONY: graph
7379
graph:
74-
terraform graph -draw-cycles | dot -Tpng > graph.png
80+
$(TERRAFORM) graph -draw-cycles | dot -Tpng > graph.png
7581

7682
$(ENV_NAME).tfvars:
7783
$(TOP)/bin/generate-tfvars $@

0 commit comments

Comments
 (0)