dags/narwhal/acme_renew/Makefile

127 lines
3.3 KiB
Makefile
Raw Normal View History

2022-09-16 10:34:57 +02:00
WD=/var/lib/dags/acme_renew
2023-05-09 10:15:54 +02:00
.PHONY: all refresh_pg renew_certs caladan_sync fugu_sync pikvm_sync caladan_trigger fugu_trigger
2022-09-16 10:34:57 +02:00
NGINX_RELOAD=$(WD)/nginx_reload
all: renew_certs caladan_trigger fugu_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
2022-09-16 18:02:53 +02:00
CERT=$(CERT_PATH)/fullchain.cer
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
2022-09-16 18:02:53 +02:00
$(CERT): renew_certs
$(KEY): renew_certs
2022-09-16 10:34:57 +02:00
GOPASS=doas -u gopass gopass
LINODE_TOKEN = $(shell $(GOPASS) linode.com/token)
DOCKER_IMAGE=neilpang/acme.sh
ACME_DATA_DIR=/mnt/docker_volumes/acmesh/data
2022-09-16 10:34:57 +02:00
RENEW_CMD="/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" --config-home "/acme.sh"
renew_certs:
2022-09-23 10:21:15 +02:00
@echo "renewing certs"
@docker run --rm -it \
-v $(ACME_DATA_DIR):/acme.sh \
-v $(CERT_PATH):/acme.sh/$(DOMAIN) \
-e "LINODE_V4_API_KEY=$(LINODE_TOKEN)" \
$(DOCKER_IMAGE) \
2022-09-16 10:34:57 +02:00
$(RENEW_CMD)
2023-05-08 18:18:41 +02:00
caladan_sync: renew_certs
rsync \
--archive \
--delete \
--compress \
--verbose \
--human-readable \
--rsh "ssh -i $(SSH_KEY)" \
--rsync-path="doas rsync" \
2023-05-08 18:22:22 +02:00
$(ACME_DIR)/ \
2023-05-08 18:18:41 +02:00
dags@caladan:$(ACME_DIR)
fugu_sync: renew_certs
rsync \
--archive \
--delete \
--compress \
--verbose \
--human-readable \
--rsh "ssh -i $(SSH_KEY)" \
--rsync-path="doas rsync" \
2023-05-08 18:22:22 +02:00
$(ACME_DIR)/ \
2023-05-08 18:18:41 +02:00
dags@fugu:$(ACME_DIR)
2023-05-09 10:15:54 +02:00
KVMD_PST_DATA = /var/lib/kvmd/pst/data
pikvm_sync: renew_certs
rsync \
--archive \
--delete \
--compress \
--verbose \
--human-readable \
--rsh "ssh -i $(SSH_KEY)" \
--rsync-path="doas kvmd-pstrun -- rsync" \
$(ACME_DIR)/ \
dags@pikvm:$(KVMD_PST_DATA)/acme
caladan_trigger: caladan_sync
ssh -i $(SSH_KEY) dags@caladan "doas make -C /srv/dags/caladan/acme_refresh"
fugu_trigger: fugu_sync
ssh -i $(SSH_KEY) dags@fugu "doas gmake -C /srv/dags/fugu/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
2022-09-16 18:02:53 +02:00
$(NGINX_RELOAD): $(CERT) $(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
2022-09-16 18:02:53 +02:00
$(PG_CERT): $(CERT)
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)