78 lines
3.1 KiB
Makefile
78 lines
3.1 KiB
Makefile
.PHONY: init plan apply apply-if-changes format pre-commit test
|
|
|
|
VAULT_AUTH_METHOD ?= approle
|
|
VAULT_K8S_ROLE ?= woodpecker_terraform_git
|
|
VAULT_K8S_MOUNT ?= auth/k8s/au/syd1
|
|
VAULT_K8S_JWT_PATH ?= /var/run/secrets/kubernetes.io/serviceaccount/token
|
|
|
|
GITHUB_VAULT_PATH ?= kv/service/github/iuclabs/terraform
|
|
|
|
define vault_env
|
|
export VAULT_ADDR="https://vault.service.consul:8200" && \
|
|
if [ "$(VAULT_AUTH_METHOD)" = "kubernetes" ]; then \
|
|
export VAULT_TOKEN=$$(vault write -field=token $(VAULT_K8S_MOUNT)/login role=$(VAULT_K8S_ROLE) jwt=$$(cat $(VAULT_K8S_JWT_PATH))); \
|
|
else \
|
|
export VAULT_TOKEN=$$(vault write -field=token auth/approle/login role_id=$$VAULT_ROLEID); \
|
|
fi && \
|
|
export CONSUL_HTTP_TOKEN=$$(vault read -field=token consul_root/au/syd1/creds/terraform-git) && \
|
|
export GITEA_TOKEN=$$(vault kv get -field=token kv/service/gitea/gitadmin/tokens/terraform-git) && \
|
|
export WOODPECKER_TOKEN=$$(vault kv get -field=token kv/service/woodpecker/tokens/gitadmin)
|
|
endef
|
|
|
|
define github_env
|
|
GITHUB_APP_ID=$$(vault kv get -field=app_id $(GITHUB_VAULT_PATH)) && \
|
|
GITHUB_APP_INSTALLATION_ID=$$(vault kv get -field=installation_id $(GITHUB_VAULT_PATH)) && \
|
|
GITHUB_APP_PEM_FILE=$$(vault kv get -field=pem $(GITHUB_VAULT_PATH)) && \
|
|
TF_VAR_billing_email=$$(vault kv get -field=billing_email $(GITHUB_VAULT_PATH)) && \
|
|
export GITHUB_APP_ID GITHUB_APP_INSTALLATION_ID GITHUB_APP_PEM_FILE TF_VAR_billing_email
|
|
endef
|
|
|
|
# Runs $(1) in $$dir for the gitea units, then the github units with App creds in a subshell; either failing fails the target without skipping the other.
|
|
define each_host
|
|
$(vault_env) || exit 1; \
|
|
rc=0; \
|
|
dir=environments/au; ( $(1) ) || rc=1; \
|
|
dir=environments/github; ( $(github_env) || exit 1; $(1) ) || rc=1; \
|
|
exit $$rc
|
|
endef
|
|
|
|
define plan_then_apply
|
|
terragrunt run --all --working-dir $$dir --parallelism 4 --non-interactive plan -- -detailed-exitcode -out=tfplan; \
|
|
EXIT_CODE=$$?; \
|
|
if [ $$EXIT_CODE -eq 2 ]; then \
|
|
terragrunt run --all --working-dir $$dir --parallelism 2 --non-interactive apply -- tfplan; \
|
|
elif [ $$EXIT_CODE -eq 0 ]; then \
|
|
echo "No changes detected in $$dir, skipping apply."; \
|
|
else \
|
|
exit $$EXIT_CODE; \
|
|
fi
|
|
endef
|
|
|
|
init:
|
|
@$(vault_env) && \
|
|
terragrunt run --all --non-interactive init -- -upgrade
|
|
|
|
plan: init
|
|
@$(call each_host,terragrunt run --all --working-dir $$dir --parallelism 4 --non-interactive plan -- -lock=false)
|
|
|
|
apply-if-changes: init
|
|
@$(call each_host,$(plan_then_apply))
|
|
|
|
apply: init
|
|
@$(call each_host,terragrunt run --all --working-dir $$dir --parallelism 2 --non-interactive apply)
|
|
|
|
format:
|
|
@echo "Formatting OpenTofu files..."
|
|
@tofu fmt -recursive .
|
|
@echo "Formatting Terragrunt files..."
|
|
@terragrunt hcl fmt
|
|
|
|
pre-commit:
|
|
@uvx pre-commit run --all-files
|
|
|
|
test:
|
|
@tofu -chdir=modules/github_org init -backend=false -input=false >/dev/null && \
|
|
tofu -chdir=modules/github_org test
|
|
@terragrunt render --working-dir environments/au/syd1 --json --out /dev/stdout 2>/dev/null | \
|
|
python3 -c 'import json,sys; i=json.loads(sys.stdin.read().splitlines()[-1])["inputs"]; bad=[k for m in i.values() for k in m if k.startswith("github.com/")]; sys.exit(f"github.com config in gitea inputs: {bad}" if bad else 0)'
|