Skip to content

Commit ccb43aa

Browse files
committed
feat: dockerize terraform makefile
1 parent acf6f77 commit ccb43aa

File tree

1 file changed

+45
-55
lines changed

1 file changed

+45
-55
lines changed

Makefile.terraform

Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,83 @@
11
# This makefile is intended to enable Terraform repositories.
22
# We've had some success with using it manually and with Jenkins.
33
#
4-
# We mainly run with it from Linux. If you want to see if support
5-
# other OSes send a PR :D
6-
7-
.PHONY: all docs taint-node select plan apply
8-
.SILENT: banner help
9-
10-
## Terraform is very version specific, so know what you need
11-
TF_VERSION = 0.11.11
12-
13-
TF_PATH = ./terraform
14-
15-
ifeq ("${TF_PATH}", "./terraform")
16-
ifeq ("$(wildcard $(TF_PATH))", "")
17-
ifeq ("$(shell uname)", "Linux")
18-
ARCHIVE_FILE = terraform_${TF_VERSION}_linux_amd64.zip
19-
else
20-
ifeq ("$(shell uname)", "Darwin")
21-
ARCHIVE_FILE = terraform_${TF_VERSION}_darwin_amd64.zip
22-
else
23-
$(error This only works on darwin and linux for now... PRs welcome)
24-
endif
25-
endif
26-
BASE_URL = https://releases.hashicorp.com/terraform/${TF_VERSION}/
27-
BOOTSTRAP_CMD := test ! -f ${TF_PATH} && curl -O ${BASE_URL}${ARCHIVE_FILE} && unzip ${ARCHIVE_FILE} && rm -f ${ARCHIVE_FILE}
28-
endif
29-
endif
304

31-
ifeq ($(origin BOOTSTRAP_CMD), undefined)
32-
BOOTSTRAP_CMD := echo "INFO: Using installed ${TF_PATH}"
5+
## Override any of the below ?= variables in .config.mk
6+
-include .config.mk
7+
8+
TERRAFORM_IMAGE ?= docker.io/hashicorp/terraform
9+
TERRAFORM_VERSION ?= 1.0 ## Terraform is very version specific, so know what you need
10+
TERRAFORM_STATE_S3 ?= no ## If using S3 for shared state, override this with a 'yes'
11+
CONTAINER_ENGINE ?= docker ## Commands will be executed via the container engine, expected to be docker cli compatible
12+
13+
# Container based commands to for use handling target steps
14+
BASE_COMMAND := $(CONTAINER_ENGINE) run --rm -it --env-file $(BASE_ENV) -v "$(CURDIR)":$(WORKDIR):Z
15+
TERRAFORM_COMMAND := $(BASE_COMMAND) $(TERRAFORM_IMAGE):$(TERRAFORM_VERSION)
16+
17+
# Determine some runtime values
18+
ifeq (".terraform/environment", "$(wildcard .terraform/environment)")
19+
# if file exists on disk, get the workspace from it
20+
TERRAFORM_VAR_FILE := -var-file=$(shell cat .terraform/environment).tfvars
3321
endif
3422

3523
all: help
3624

37-
check-env:
38-
# Uncomment this block if you use S3 for buckets
39-
#ifeq ($(origin AWS_ACCESS_KEY_ID), undefined)
40-
#$(error Environment variable AWS_ACCESS_KEY_ID needs to be defined)
41-
#endif
42-
#
43-
#ifeq ($(origin AWS_SECRET_ACCESS_KEY), undefined)
44-
#$(error Environment variable AWS_SECRET_ACCESS_KEY needs to be defined)
45-
#endif
46-
#
47-
#ifeq ($(origin AWS_SESSION_TOKEN), undefined)
48-
#$(warn For temporary credentials the environment variable AWS_SESSION_TOKEN needs to be defined)
49-
#endif
25+
.PHONY:.check-env
26+
.check-env:
27+
ifeq ($(origin TERRAFORM_STATE_S3), "yes")
28+
@if [ "${AWS_PROFILE}" == "" ]; then \
29+
if [ "${AWS_SECRET_ACCESS_KEY}" == "" ] || [ "${AWS_ACCESS_KEY_ID}" == "" ]; then \
30+
echo "ERROR: AWS_PROFILE _or_ AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY must be profiled"; \
31+
exit 1; \
32+
fi; \
33+
fi
34+
endif
5035

36+
.PHONY: help
5137
help: ## Show this help, includes list of all actions.
5238
@awk 'BEGIN {FS = ":.*?## "}; /^.+: .*?## / && !/awk/ {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' ${MAKEFILE_LIST}
5339

40+
.PHONY: clean
5441
clean: ## Cleanup the local checkout
55-
-rm -f *.zip terraform *.backup
42+
-rm -f *.zip *.backup
5643

57-
setup: ## Use this to install terraform
58-
-${BOOTSTRAP_CMD}
44+
.terraform:
45+
@if [ ! -d .terraform ]; then $(TERRAFORM_COMMAND) init; else $(TERRAFORM_COMMAND) get --update > /dev/null; fi
5946

60-
init: ## Initalize shared storage bucket for state and ensure modules are loaded
61-
@if [ ! -d .terraform ]; then ${TF_PATH} init; else ${TF_PATH} get --update > /dev/null;fi
47+
.PHONY: init
48+
init: .terraform ## Initalize shared storage bucket for state and ensure modules are loaded
6249

50+
.PHONY: list-workspaces
6351
list-workspaces: init ## Displays list of workspaces
64-
${TF_PATH} workspace list
52+
$(TERRAFORM_COMMAND) workspace list
6553

54+
.PHONY: new-workspace-%
6655
new-workspace-%: init ## Creates and selects a new workspace
67-
${TF_PATH} workspace new $*
56+
$(TERRAFORM_COMMAND) workspace new $*
6857

58+
.PHONY: select-%
6959
select-%: init ## Change to the provided workspace
70-
${TF_PATH} workspace select $*
60+
$(TERRAFORM_COMMAND) workspace select $*
7161

7262
plan-%: select-% plan ## Run terraform plan against the defined workspace
73-
: # This is because make doesnt like this target to not have any actions
63+
: # This is because make doesnt like wildcard targets to not have any actions
7464

7565
plan: init ## Run terraform plan against the current workspace
76-
${TF_PATH} plan -var-file=$(shell cat .terraform/environment).tfvars
66+
$(TERRAFORM_COMMAND) plan $(TERRAFORM_VAR_FILE)
7767

7868
test: plan ## Standard entry point for running tests. Calls plan
7969

8070
apply-%: select-% apply ## Run terraform apply against the defined workspace
8171
: # This is because make doesnt like this target to not have any actions
8272

8373
apply: init ## Run terraform apply against the current workspace
84-
${TF_PATH} apply -var-file=$(shell cat .terraform/environment).tfvars
74+
$(TERRAFORM_COMMAND) apply $(TERRAFORM_VAR_FILE)
8575

8676
show: init ## Run terraform show against the current workspace
87-
${TF_PATH} show
77+
$(TERRAFORM_COMMAND) show
8878

8979
show-node: init ## Run terraform show against the current workspace. NODE_REGEX search pattern
90-
${TF_PATH} show | grep -P '^module| id| ip' | grep -C1 -P "${NODE_REGEX}"
80+
$(TERRAFORM_COMMAND) show | grep -P '^module| id| ip' | grep -C1 -P "${NODE_REGEX}"
9181

9282
taint-node: init ## Run terraform taint against the current workspace. NODE_REGEX search pattern
93-
./scripts/taint.sh ${TF_PATH} ${NODE_REGEX}
83+
./scripts/taint.sh $(TERRAFORM_COMMAND) ${NODE_REGEX}

0 commit comments

Comments
 (0)