|
| 1 | +SHELL := /bin/bash |
| 2 | +TERRAFORM := $(shell which tofu) |
| 3 | +S3_REGION := $(shell sops decrypt secrets/secrets.yaml | grep ^s3_region | cut -d ' ' -f 2) |
| 4 | +S3_BUCKET := $(shell sops decrypt secrets/secrets.yaml | grep ^s3_bucket | cut -d ' ' -f 2) |
| 5 | +S3_KEY := $(shell sops decrypt secrets/secrets.yaml | grep ^s3_key | cut -d ' ' -f 2) |
| 6 | +S3_ACCESS_KEY := $(shell sops decrypt secrets/secrets.yaml | grep ^s3_access_key | cut -d ' ' -f 2) |
| 7 | +S3_SECRET_KEY := $(shell sops decrypt secrets/secrets.yaml | grep ^s3_secret_key | cut -d ' ' -f 2) |
| 8 | + |
| 9 | +.PHONY: help init plan apply migrate test pre-commit-check-deps pre-commit-install-hooks argcd-login |
| 10 | + |
| 11 | +help: |
| 12 | + @echo "General targets" |
| 13 | + @echo "----------------" |
| 14 | + @echo |
| 15 | + @echo "\thelp: show this help text" |
| 16 | + @echo "\tclean: removes all .terraform directories" |
| 17 | + @echo |
| 18 | + @echo "Terraform targets" |
| 19 | + @echo "-----------------" |
| 20 | + @echo |
| 21 | + @echo "\tinit: run 'terraform init'" |
| 22 | + @echo "\ttest: run pre-commmit checks" |
| 23 | + @echo "\tplan: run 'terraform plan'" |
| 24 | + @echo "\tapply: run 'terraform apply'" |
| 25 | + @echo "\tmigrate; run terraform init -migrate-state" |
| 26 | + @echo |
| 27 | + @echo "One-time repo init targets" |
| 28 | + @echo "--------------------------" |
| 29 | + @echo |
| 30 | + @echo "\tpre-commit-install-hooks: install pre-commit hooks" |
| 31 | + @echo "\tpre-commit-check-deps: check pre-commit dependencies" |
| 32 | + @echo |
| 33 | + |
| 34 | +clean: |
| 35 | + @find . -name .terraform -type d | xargs -I {} rm -rf {} |
| 36 | + |
| 37 | +init: clean .terraform/terraform.tfstate |
| 38 | + |
| 39 | +.terraform/terraform.tfstate: |
| 40 | + @${TERRAFORM} init -reconfigure -upgrade -input=false -backend-config="key=${S3_KEY}" -backend-config="bucket=${S3_BUCKET}" -backend-config="region=${S3_REGION}" -backend-config="access_key=${S3_ACCESS_KEY}" -backend-config="secret_key=${S3_SECRET_KEY}" |
| 41 | + |
| 42 | +plan: init .terraform/plan |
| 43 | + |
| 44 | +.terraform/plan: |
| 45 | + @${TERRAFORM} plan -compact-warnings -no-color -out tfplan.bin |
| 46 | + @${TERRAFORM} show -no-color tfplan.bin | tee plan-output.txt |
| 47 | + @rm -f tfplan.bin |
| 48 | + |
| 49 | +apply: init .terraform/apply |
| 50 | + |
| 51 | +.terraform/apply: |
| 52 | + @${TERRAFORM} apply -auto-approve -compact-warnings |
| 53 | + |
| 54 | +migrate: |
| 55 | + @echo "First use -make init- using the old S3 backend, then run -make migrate- to use the new one." |
| 56 | + @${TERRAFORM} init -migrate-state -backend-config="key=${S3_KEY}" -backend-config="bucket=${S3_BUCKET}" -backend-config="region=${S3_REGION}" -backend-config="access_key=${S3_ACCESS_KEY}" -backend-config="secret_key=${S3_SECRET_KEY}" |
| 57 | + |
| 58 | +test: .git/hooks/pre-commit |
| 59 | + @pre-commit run -a |
| 60 | + |
| 61 | +DEPS_PRE_COMMIT=$(shell which pre-commit || echo "pre-commit not found") |
| 62 | +DEPS_TERRAFORM_DOCS=$(shell which terraform-docs || echo "terraform-docs not found") |
| 63 | +DEPS_TFLINT=$(shell which tflint || echo "tflint not found,") |
| 64 | +DEPS_CHECKOV=$(shell which checkov || echo "checkov not found,") |
| 65 | +DEPS_JQ=$(shell which jq || echo "jq not found,") |
| 66 | +pre-commit-check-deps: |
| 67 | + @echo "Checking for pre-commit and its dependencies:" |
| 68 | + @echo " pre-commit: ${DEPS_PRE_COMMIT}" |
| 69 | + @echo " terraform-docs: ${DEPS_TERRAFORM_DOCS}" |
| 70 | + @echo " tflint: ${DEPS_TFLINT}" |
| 71 | + @echo " checkov: ${DEPS_CHECKOV}" |
| 72 | + @echo " jq: ${DEPS_JQ}" |
| 73 | + @echo "" |
| 74 | + |
| 75 | +pre-commit-install-hooks: .git/hooks/pre-commit |
| 76 | + |
| 77 | +.git/hooks/pre-commit: pre-commit-check-deps |
| 78 | + @pre-commit install --install-hooks |
| 79 | + |
0 commit comments