Files
dcape-dns-config/Makefile
2023-07-08 21:39:43 +03:00

110 lines
2.9 KiB
Makefile

# app custom Makefile
SHELL = /bin/sh
CFG = .env
SOURCES ?= $(wildcard *.sql)
OBJECTS = $(SOURCES:.sql=.done)
OBJECTSDIRECT = $(SOURCES:.sql=.direct)
# ------------------------------------------------------------------------------
# app custom config
# comments prefixed with '#- ' will be copied to $(CFG).sample
#- Postgresql container name (access via docker)
PG_CONTAINER ?= dcape_db_1
#- PowerDNS DB user name
PGUSER ?= pdns
#- PowerDNS DB name
PGDATABASE ?= pdns
#- Used ONLY for direct DB access without docker (update-direct)
PGPASSWORD ?=
#- ACME zone suffix
ACME_DOMAIN ?=
#- This NS hostname for use in all SOA
NSERVER ?=
# ------------------------------------------------------------------------------
all: help
-include $(CFG)
export
# ------------------------------------------------------------------------------
# dcape v1 comparibility
start-hook: update
stop:
# ------------------------------------------------------------------------------
## Usage
#:
## Load updated zone files via running PG container
update: $(OBJECTS)
%.done: %.sql
@echo "*** $< ***"
@csum=$$(md5sum $< | sed 's/ .*//') ; \
cat $< | docker exec -i $$PG_CONTAINER psql -U $$PGUSER -d $$PGDATABASE -vcsum=$$csum -vACME_DOMAIN=$(ACME_DOMAIN) -vNSERVER=$(NSERVER) > $@
## Load updated zone files via psql connection
update-direct: $(CFG) $(OBJECTSDIRECT)
%.direct: %.sql
@echo "*** $< ***"
@source $(CFG) && cat $< | PGPASSWORD=$$PGPASSWORD psql -h localhost -U $$PGUSER > $@
## Run psql via running PG container
psql:
@docker exec -it $$PG_CONTAINER psql -U $$PGUSER $$PGDATABASE
# ------------------------------------------------------------------------------
## Other
#:
# This code generates $(CFG).sample from Makefile vars with previous comment line(s)
# See https://gist.github.com/LeKovr/2697fe02504d7c081b0bf79427c93db6
# Internal: generate config sample data
.env.temp.mk:
@echo "define CFVAR" > $@
@grep -A 1 -h "^#- " $(MAKEFILE_LIST) | grep -vE "^--" \
| sed -E 's/^([^\n ]+)\ *\??=([^\n]*)$$/\1=$$(\1)\n/ ; s/^(#)-/\1/' >> $@
@echo "endef" >> $@
ifneq ($(findstring $(MAKECMDGOALS),config $(CFG).sample),)
include .env.temp.mk
endif
# Internal: generate config sample
$(CFG).sample: .env.temp.mk
@echo "# dcape-dns config file, generated by 'make config'\n" > $@
@echo "$$CFVAR" >> $@
@rm -f $<
## generate sample config
config: $(CFG).sample
# ------------------------------------------------------------------------------
## Remove .done files
clean:
rm -rf $(OBJECTS)
# This code handles group header and target comment with one or two lines only
## list Makefile targets
## (this is default target)
help:
@grep -A 1 -h "^## " $(MAKEFILE_LIST) \
| sed -E 's/^--$$// ; /./{H;$$!d} ; x ; s/^\n## ([^\n]+)\n(## (.+)\n)*(.+):(.*)$$/" " "\4" "\1" "\3"/' \
| sed -E 's/^" " "#" "(.+)" "(.*)"$$/"" "" "" ""\n"\1 \2" "" "" ""/' \
| xargs printf "%s\033[36m%-15s\033[0m %s %s\n"