1
0
Fork 0
distrust-stack/Makefile

182 lines
5.3 KiB
Makefile
Raw Permalink Normal View History

2023-03-10 04:43:38 +00:00
include $(PWD)/src/toolchain/Makefile
include $(PWD)/src/make/tools.mk
2023-03-10 04:43:38 +00:00
2023-02-17 06:09:13 +00:00
BACKEND_TF := $(wildcard infra/backend/*.tf)
2023-04-14 03:22:35 +00:00
MAIN_TF := $(wildcard infra/main/*.tf)
2023-02-17 06:09:13 +00:00
ENVIRONMENT := production
2023-03-10 04:43:38 +00:00
REGION := sfo3
ROOT_DIR := $(shell pwd)
2023-12-15 03:31:21 +00:00
# TODO: automatically determine
2024-03-29 04:28:02 +00:00
TERRAFORM := $(ROOT_DIR)/out/tofu.linux-x86_64
2023-12-15 03:31:21 +00:00
SOPS := $(ROOT_DIR)/out/sops.linux-x86_64
KEYS := \
6B61ECD76088748C70590D55E90A401336C8AAA9 \
88823A75ECAA786B0FF38B148E401478A3FBEF72 \
2023-05-26 02:36:41 +00:00
3D7C8D39E8C4DF771583D3F0A8A091FD346001CA \
F4BF5C81EC78A5DD341C91EEDC4B7D1F52E0BA4D
2023-02-17 06:09:13 +00:00
2024-03-29 04:28:02 +00:00
EXTRA_ARGS :=
2023-03-10 07:43:21 +00:00
.DEFAULT_GOAL :=
.PHONY: default
default: \
toolchain \
tools \
2023-03-10 07:43:21 +00:00
apply
2023-02-17 06:09:13 +00:00
.PHONY:
clean:
rm -rf $(CACHE_DIR)
.PHONY:
credentials: \
$(CACHE_DIR)/secrets/credentials.tfvars
.PHONY:
shell: toolchain tools
$(call toolchain," \
HOST_OS=linux \
HOST_ARCH=x86_64 \
PREFIX=.local \
XDG_CONFIG_HOME=/home/build/.config \
make -f src/make/tools.mk tools-install \
&& PS1='build@distrust-stack\\$$ ' bash --norc \
",--interactive)
$(KEY_DIR)/%.asc:
$(call fetch_pgp_key,$(basename $(notdir $@)))
$(OUT_DIR)/website/.well-known/matrix/client \
2023-07-26 07:15:02 +00:00
$(OUT_DIR)/website/.well-known/matrix/server:
mkdir -p $(OUT_DIR)/website/.well-known/matrix
cp -R \
$(SRC_DIR)/well-known/matrix/* \
$(OUT_DIR)/website/.well-known/matrix/
2023-03-17 04:14:39 +00:00
$(OUT_DIR)/website/.well-known/openpgpkey:
2023-03-17 03:37:07 +00:00
$(call toolchain," \
sq wkd \
2023-03-17 04:14:39 +00:00
generate $(OUT_DIR)/website distrust.co \
2023-03-17 03:37:07 +00:00
<(cat $(patsubst %,$(KEY_DIR)/%.asc,$(KEYS))) \
")
$(CACHE_DIR)/website/index.html: \
$(CACHE_DIR)/website/.well-known/openpgpkey \
$(CACHE_DIR)/website/.well-known/matrix/server \
$(CACHE_DIR)/website/.well-known/matrix/client
2023-04-14 04:19:08 +00:00
$(call toolchain," \
cd $(SRC_DIR)/website \
&& jekyll build \
&& cp -R _site/* /home/build/out/website/ \
")
2023-03-10 04:43:38 +00:00
infra/backend/.terraform: \
2023-05-13 05:03:35 +00:00
$(TERRAFORM) \
2023-03-10 04:43:38 +00:00
$(BACKEND_TF)
$(SOPS) exec-env secrets/$(ENVIRONMENT).enc.env '\
env -C infra/backend $(TERRAFORM) init -upgrade \
'
2024-03-29 04:28:02 +00:00
$(SOPS) exec-env secrets/$(ENVIRONMENT).enc.env '\
env -C infra/backend $(TERRAFORM) refresh \
-var environment=$(ENVIRONMENT) \
-var namespace=$(ENVIRONMENT) \
-var region=$(REGION) \
-state $(ENVIRONMENT).tfstate \
'
2023-02-17 06:09:13 +00:00
infra/main/.terraform: | \
2023-05-13 05:03:35 +00:00
$(TERRAFORM) \
2023-04-14 03:22:35 +00:00
config/$(ENVIRONMENT).tfbackend \
$(MAIN_TF)
$(SOPS) exec-env secrets/$(ENVIRONMENT).enc.env '\
env -C infra/main $(TERRAFORM) init -upgrade \
-backend-config="../../config/$(ENVIRONMENT).tfbackend" \
'
2024-03-29 04:28:02 +00:00
$(SOPS) exec-env secrets/$(ENVIRONMENT).enc.env '\
env -C infra/main $(TERRAFORM) refresh \
-var environment=$(ENVIRONMENT) \
-var namespace=$(ENVIRONMENT) \
-var region=$(REGION) \
-state $(ENVIRONMENT).tfstate \
'
2023-03-10 07:43:21 +00:00
2023-03-10 04:43:38 +00:00
infra/backend/$(ENVIRONMENT).tfstate: \
2023-05-13 05:03:35 +00:00
$(TERRAFORM) \
$(SOPS) \
2023-03-10 04:43:38 +00:00
infra/backend/.terraform
2023-05-07 07:37:07 +00:00
$(SOPS) exec-env secrets/$(ENVIRONMENT).enc.env '\
2023-05-06 20:35:46 +00:00
env -C infra/backend \
$(TERRAFORM) apply \
2023-03-10 04:43:38 +00:00
-var environment=$(ENVIRONMENT) \
-var namespace=$(ENVIRONMENT) \
-var region=$(REGION) \
2024-03-29 04:28:02 +00:00
-state $@ \
2023-05-06 20:35:46 +00:00
'
2023-02-17 06:09:13 +00:00
config/$(ENVIRONMENT).tfbackend: | \
2023-05-13 05:03:35 +00:00
$(TERRAFORM) \
$(SOPS) \
2023-04-14 03:47:41 +00:00
# File is not committed and this has no shared state
$(MAKE) infra/backend/$(ENVIRONMENT).tfstate
2023-05-07 07:37:07 +00:00
$(SOPS) exec-env secrets/$(ENVIRONMENT).enc.env '\
2023-05-06 20:35:46 +00:00
env -C infra/backend \
$(TERRAFORM) \
2024-03-29 04:28:02 +00:00
output -state $(ENVIRONMENT).tfstate \
2023-05-06 20:35:46 +00:00
> $@ \
'
2024-03-29 04:28:02 +00:00
$(SOPS) exec-env secrets/$(ENVIRONMENT).enc.env '\
env -C infra/backend \
$(TERRAFORM) refresh \
-var environment=$(ENVIRONMENT) \
-var namespace=$(ENVIRONMENT) \
-var region=$(REGION) \
-state $(ENVIRONMENT).tfstate \
'
2023-02-17 06:09:13 +00:00
.PHONY:
2023-03-10 07:43:21 +00:00
apply: \
2023-05-13 05:03:35 +00:00
$(TERRAFORM) \
$(SOPS) \
2023-03-10 07:43:21 +00:00
infra/main/.terraform
$(call maybe_decrypt_secret,secrets/$(ENVIRONMENT).talosconfig,infra/main/talos/talosconfig)
$(call maybe_decrypt_secret,secrets/$(ENVIRONMENT).kubeconfig,infra/main/talos/kubeconfig)
$(call maybe_decrypt_secret,secrets/$(ENVIRONMENT).controlplane.yaml,infra/main/talos/controlplane.yaml)
$(call maybe_decrypt_secret,secrets/$(ENVIRONMENT).worker.yaml,infra/main/talos/worker.yaml)
2023-05-07 07:37:07 +00:00
$(SOPS) exec-env secrets/$(ENVIRONMENT).enc.env '\
2023-05-06 20:35:46 +00:00
env -C infra/main \
$(TERRAFORM) apply \
2023-03-17 04:14:39 +00:00
-var environment=$(ENVIRONMENT) \
-var namespace=$(ENVIRONMENT) \
2023-05-06 20:35:46 +00:00
-var region=$(REGION) \
2024-03-29 04:28:02 +00:00
$(EXTRA_ARGS) '
$(call maybe_encrypt_secret,infra/main/talos/talosconfig,secrets/$(ENVIRONMENT).talosconfig)
$(call maybe_encrypt_secret,infra/main/talos/kubeconfig,secrets/$(ENVIRONMENT).kubeconfig)
$(call maybe_encrypt_secret,infra/main/talos/controlplane.yaml,secrets/$(ENVIRONMENT).controlplane.yaml)
$(call maybe_encrypt_secret,infra/main/talos/worker.yaml,secrets/$(ENVIRONMENT).worker.yaml)
2023-02-17 06:09:13 +00:00
$(CACHE_DIR)/secrets:
mkdir -p $@
# Note: Decryption MUST reset the mod time to avoid encryption/decryption loops
# Encrypt if:
# - Both files exist, local is newer than remote
# - Only local exists
define maybe_encrypt_secret
test \( -f $(1) -a -f $(2) -a $(1) -nt $(2) \) -o \
\( -f $(1) -a ! -f $(2) \) && \
$(SOPS) --encrypt $(1) > $(2) || true
endef
# Only decrypt when local files don't exist
# Unfortunately, this means we can't decrypt if the secrets update. We can't
# do that because otherwise it creates a loop. The secrets update, therefore we
# decrypt secrets, but because the modtime of the decrypted secrets is newer
# than the encrypted secrets, we want to reencrypt encrypted secrets.
define maybe_decrypt_secret
test -f $(1) -a ! -f $(2) && \
2023-12-15 03:31:21 +00:00
mkdir -p `dirname $(2)` && \
$(SOPS) --decrypt $(1) > $(2) && \
touch -d 1970-01-01 $(2) || \
true
endef