dags/narwhal/acme_renew/Makefile

169 lines
4.5 KiB
Makefile
Raw Normal View History

2022-09-16 10:34:57 +02:00
WD=/var/lib/dags/acme_renew
2023-05-30 17:40:37 +02:00
.PHONY: all refresh_pg renew_certs lb_sync
2022-09-16 10:34:57 +02:00
NGINX_RELOAD=$(WD)/nginx_reload
2023-05-09 15:42:51 +02:00
CALADAN_SYNC=$(WD)/caladan_sync
FUGU_SYNC=$(WD)/fugu_sync
PIKVM_SYNC=$(WD)/pikvm_sync
2023-05-30 17:40:37 +02:00
LB_SYNC=$(WD)/lb_sync
2023-05-09 15:42:51 +02:00
CALADAN_TRIGGER=$(WD)/caladan_trigger
FUGU_TRIGGER=$(WD)/fugu_trigger
PIKVM_TRIGGER=$(WD)/pikvm_trigger
2023-05-30 17:40:37 +02:00
LB_TRIGGER=$(WD)/lb_trigger
2023-05-09 15:42:51 +02:00
2023-05-30 17:40:37 +02:00
all: renew_certs $(CALADAN_TRIGGER) $(FUGU_TRIGGER) $(PIKVM_TRIGGER) $(LB_TRIGGER) $(NGINX_RELOAD) refresh_pg
2022-09-16 10:34:57 +02:00
###############################################################################
2023-05-08 18:18:41 +02:00
ACME_DIR=/srv/certs/acme
2022-09-16 18:02:53 +02:00
DOMAIN=monotremata.xyz
2023-05-08 18:18:41 +02:00
CERT_PATH=$(ACME_DIR)/$(DOMAIN)
2022-09-16 10:34:57 +02:00
2023-05-09 15:42:51 +02:00
FULLCHAIN=$(CERT_PATH)/fullchain.cer
CERT=$(CERT_PATH)/$(DOMAIN).cer
2022-09-16 18:02:53 +02:00
KEY=$(CERT_PATH)/$(DOMAIN).key
2022-09-16 10:34:57 +02:00
2023-05-08 18:18:41 +02:00
###############################################################################
SSH_KEY=/srv/certs/ssh/users/dags/id_ed25519
2022-09-16 10:34:57 +02:00
###############################################################################
# Renew the certificates using acme.sh. Because `renew_certs` is a phony
# target, it will be run each time, but the certificate files will only be
# updated if a renewal happens
CERT_DOMAINS=-d $(DOMAIN) -d '*.$(DOMAIN)' -d '*.narwhal.$(DOMAIN)' -d '*.caladan.$(DOMAIN)' -d '*.xmpp.$(DOMAIN)'
2023-05-09 15:42:51 +02:00
$(FULLCHAIN): renew_certs
2022-09-16 18:02:53 +02:00
$(CERT): renew_certs
$(KEY): renew_certs
2022-09-16 10:34:57 +02:00
HETZNER_TOKEN=/srv/secrets/hetzner_token
DOCKER_IMAGE=neilpang/acme.sh
ACME_DATA_DIR=/mnt/docker_volumes/acmesh/data
2022-09-16 10:34:57 +02:00
ACMESH=docker run --rm -it \
-v $(ACME_DATA_DIR):/acme.sh \
-v $(CERT_PATH):/acme.sh/$(DOMAIN) \
-e "HETZNER_Token=$$(cat $(HETZNER_TOKEN))" \
$(DOCKER_IMAGE) \
/root/.acme.sh/acme.sh
RENEW_CMD=--cron --home /root/.acme.sh --config-home /acme.sh
2022-09-16 10:34:57 +02:00
# DNS_ARGS=--dns dns_linode_v4 --dnssleep 900
DNS_ARGS=--dns dns_hetzner
ISSUE_CMD=--issue $(DNS_ARGS) $(CERT_DOMAINS) --server letsencrypt
renew_certs: $(HETZNER_TOKEN) $(ACME_DATA_DIR)/account.conf
2022-09-23 10:21:15 +02:00
@echo "renewing certs"
$(ACMESH) $(RENEW_CMD)
$(ACME_DATA_DIR)/account.conf: $(HETZNER_TOKEN)
@echo "issuing certificate and saving acme.sh account configuration"
@mkdir -p $(@D)
$(ACMESH) $(ISSUE_CMD)
2022-09-16 10:34:57 +02:00
2023-05-09 15:42:51 +02:00
###############################################################################
# Sync the certs to remote hosts and trigger DAGs there
RSYNC_ARGS=--archive --delete --compress --verbose --human-readable --rsh "ssh -i $(SSH_KEY)"
$(CALADAN_SYNC): $(FULLCHAIN) $(CERT) $(KEY)
mkdir -p $(@D)
2023-05-08 18:18:41 +02:00
rsync \
2023-05-09 15:42:51 +02:00
$(RSYNC_ARGS) \
2023-05-08 18:18:41 +02:00
--rsync-path="doas rsync" \
2023-05-09 15:42:51 +02:00
$^ \
2023-05-10 12:56:15 +02:00
dags@caladan:$(CERT_PATH)
2023-05-09 15:42:51 +02:00
touch $@
2023-05-08 18:18:41 +02:00
2023-05-09 15:42:51 +02:00
$(FUGU_SYNC): $(FULLCHAIN) $(CERT) $(KEY)
mkdir -p $(@D)
2023-05-08 18:18:41 +02:00
rsync \
2023-05-09 15:42:51 +02:00
$(RSYNC_ARGS) \
2023-05-08 18:18:41 +02:00
--rsync-path="doas rsync" \
2023-05-09 15:42:51 +02:00
$^ \
2023-05-10 12:56:15 +02:00
dags@fugu:$(CERT_PATH)
2023-05-09 15:42:51 +02:00
touch $@
2023-05-08 18:18:41 +02:00
2023-05-30 17:40:37 +02:00
$(LB_SYNC): $(FULLCHAIN) $(CERT) $(KEY)
mkdir -p $(@D)
rsync \
$(RSYNC_ARGS) \
--rsync-path="doas rsync" \
$^ \
dags@lb:$(CERT_PATH)
touch $@
KVMD_PST_DATA=/var/lib/kvmd/pst/data
2023-05-09 10:15:54 +02:00
2023-05-09 15:42:51 +02:00
$(PIKVM_SYNC): $(FULLCHAIN) $(CERT) $(KEY)
mkdir -p $(@D)
2023-05-09 10:15:54 +02:00
rsync \
2023-05-09 15:42:51 +02:00
$(RSYNC_ARGS) \
2023-05-09 10:15:54 +02:00
--rsync-path="doas kvmd-pstrun -- rsync" \
2023-05-09 15:42:51 +02:00
$^ \
2023-05-10 12:56:15 +02:00
dags@pikvm:$(KVMD_PST_DATA)/acme/$(DOMAIN)
2023-05-09 15:42:51 +02:00
touch $@
define remote_dag_trigger
mkdir -p $(@D)
ssh -i $(SSH_KEY) dags@$(1) "doas /srv/dags/$(1)/$(2)/run.sh"
touch $@
endef
2023-05-09 10:15:54 +02:00
2023-05-09 15:42:51 +02:00
$(CALADAN_TRIGGER): $(CALADAN_SYNC)
2023-05-10 12:48:52 +02:00
$(call remote_dag_trigger,caladan,acme_refresh)
2023-05-09 15:42:51 +02:00
$(FUGU_TRIGGER): $(FUGU_SYNC)
2023-05-10 12:48:52 +02:00
$(call remote_dag_trigger,fugu,acme_refresh)
2023-05-09 15:42:51 +02:00
$(PIKVM_TRIGGER): $(PIKVM_SYNC)
2023-05-10 12:48:52 +02:00
$(call remote_dag_trigger,pikvm,acme_refresh)
2023-05-09 10:23:49 +02:00
2023-05-30 17:40:37 +02:00
$(LB_TRIGGER): $(LB_SYNC)
$(call remote_dag_trigger,lb,acme_refresh)
2022-09-16 10:34:57 +02:00
###############################################################################
# Reload the nginx instance running on my reverse proxy docker-compose service
# so that it uses the new certificates.
# The target is just an empty sentinel target with no meaningful data other
# than its modification date.
# So far, the nginx instance running on `narwhal` only uses the `monotremata`
# and `narwhal` certificates, so it only needs to be reloaded if those are
# updated
NGINX_COMPOSE_FILE=/srv/services/reverse_proxy/docker-compose.yml
2023-05-09 15:42:51 +02:00
$(NGINX_RELOAD): $(FULLCHAIN) $(KEY)
2022-09-16 10:34:57 +02:00
mkdir -p $(@D)
docker-compose \
--file $(NGINX_COMPOSE_FILE) \
exec nginx \
nginx -s reload
touch $@
###############################################################################
# Copy the certificate for the postgresql domain to the folder where postgre
# service expects it
PG_SSL_PATH=/mnt/docker_volumes/postgres/ssl
PG_CERT=$(PG_SSL_PATH)/server.crt
PG_KEY=$(PG_SSL_PATH)/server.key
2023-05-09 15:42:51 +02:00
$(PG_CERT): $(FULLCHAIN)
2022-09-16 10:34:57 +02:00
mkdir -p $(@D)
rsync --copy-links $< $@
2022-09-16 18:02:53 +02:00
$(PG_KEY): $(KEY)
2022-09-16 10:34:57 +02:00
mkdir -p $(@D)
rsync --copy-links $< $@
refresh_pg: $(PG_CERT) $(PG_KEY)