|
1 | 1 | # This makefile is intended to enable Terraform repositories. |
2 | 2 | # We've had some success with using it manually and with Jenkins. |
3 | 3 | # |
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 |
30 | 4 |
|
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 |
33 | 21 | endif |
34 | 22 |
|
35 | 23 | all: help |
36 | 24 |
|
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 |
50 | 35 |
|
| 36 | +.PHONY: help |
51 | 37 | help: ## Show this help, includes list of all actions. |
52 | 38 | @awk 'BEGIN {FS = ":.*?## "}; /^.+: .*?## / && !/awk/ {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' ${MAKEFILE_LIST} |
53 | 39 |
|
| 40 | +.PHONY: clean |
54 | 41 | clean: ## Cleanup the local checkout |
55 | | - -rm -f *.zip terraform *.backup |
| 42 | + -rm -f *.zip *.backup |
56 | 43 |
|
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 |
59 | 46 |
|
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 |
62 | 49 |
|
| 50 | +.PHONY: list-workspaces |
63 | 51 | list-workspaces: init ## Displays list of workspaces |
64 | | - ${TF_PATH} workspace list |
| 52 | + $(TERRAFORM_COMMAND) workspace list |
65 | 53 |
|
| 54 | +.PHONY: new-workspace-% |
66 | 55 | new-workspace-%: init ## Creates and selects a new workspace |
67 | | - ${TF_PATH} workspace new $* |
| 56 | + $(TERRAFORM_COMMAND) workspace new $* |
68 | 57 |
|
| 58 | +.PHONY: select-% |
69 | 59 | select-%: init ## Change to the provided workspace |
70 | | - ${TF_PATH} workspace select $* |
| 60 | + $(TERRAFORM_COMMAND) workspace select $* |
71 | 61 |
|
72 | 62 | 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 |
74 | 64 |
|
75 | 65 | 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) |
77 | 67 |
|
78 | 68 | test: plan ## Standard entry point for running tests. Calls plan |
79 | 69 |
|
80 | 70 | apply-%: select-% apply ## Run terraform apply against the defined workspace |
81 | 71 | : # This is because make doesnt like this target to not have any actions |
82 | 72 |
|
83 | 73 | 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) |
85 | 75 |
|
86 | 76 | show: init ## Run terraform show against the current workspace |
87 | | - ${TF_PATH} show |
| 77 | + $(TERRAFORM_COMMAND) show |
88 | 78 |
|
89 | 79 | 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}" |
91 | 81 |
|
92 | 82 | 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