diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..478e957 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +*.done diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7f2a0cc --- /dev/null +++ b/Makefile @@ -0,0 +1,32 @@ +SHELL = /bin/bash +CFG=.env +SOURCES=$(wildcard *.sql) +OBJECTS=$(SOURCES:.sql=.done) + +all: $(CFG) $(OBJECTS) + +up: all + +%.done: %.sql + @echo "*** $< ***" + @cat $< | docker exec -i $$PG_CONTAINER psql -U $$PG_USER > $@ + +clean: + rm -rf *.done + +# шаблон файла .env + +define CONFIG_DEF +# dockin-app-dns config file, generated by make $(CFG) + +# Postgresql container name +PG_CONTAINER=dcape_db_1 + +# PowerDNS DB user (and database) name +PG_USER=dns +endef +export CONFIG_DEF + +$(CFG): + @echo "*** $@ ***" + @[ -f $@ ] || { echo "$$CONFIG_DEF" > $@ ; echo >&2 "Warning: Created default $@" ; } diff --git a/README.md b/README.md index 2acc61d..7302702 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,22 @@ # dcape-dns-config Config for dcape based powerdns server + +This project contains Makefile and sample sql zone definition for loading zones into PowerDNS server. + +## Requirements + +* [dcape](https://github.com/TenderPro/dcape) installed on remote host with pdns and gitea running + +## Usage + +* fork this project into your dcape gitea +* in gitea project settings setup hook for server where powerdns service running +* clone project locally from gitea +* add your zones .sql by example from domain.sql.sample +* make `git push` and change project env in dcape cis config frontend +* make `git push` again and see updated dns zones + + +## TODO + +* [ ] Zone rectification SQL diff --git a/domain.sql.sample b/domain.sql.sample new file mode 100644 index 0000000..42902d1 --- /dev/null +++ b/domain.sql.sample @@ -0,0 +1,38 @@ +DO $$ + +-- Reload PowerDNS zone data + +DECLARE + v_domain text := 'dev.lan'; -- domain name + v_ip text := '127.0.0.1'; -- base ip + v_ns text := 'ns.dev.lan'; -- master DNS host + v_ns_admin text := 'admin.ns.dev.lan'; -- master DNS admin email + v_domain_id integer; -- internal domain id + v_stamp text; -- zone timestamp + v_key text := '1'; -- zone serial suffix + v_soa text; -- zone SOA + +BEGIN + + SELECT INTO v_domain_id id FROM domains WHERE name = v_domain; + IF NOT FOUND THEN + INSERT INTO domains (name, type) VALUES + (v_domain, 'MASTER') + RETURNING id INTO v_domain_id + ; + END IF; + + v_stamp := to_char(current_timestamp, 'YYYYMMDD') || v_key; + v_soa := concat_ws(' ', v_ns, v_ns_admin, v_stamp, '10800 3600 604800 1800'); + + DELETE FROM records WHERE domain_id = v_domain_id; + INSERT INTO records (domain_id, name, ttl, type, prio, content) VALUES + (v_domain_id, v_domain, 60, 'SOA', 0, v_soa) + , (v_domain_id, v_domain, 1800, 'NS', 0, 'ns.' || v_domain) + , (v_domain_id, v_domain, 1800, 'MX', 5, 'mail.' || v_domain) + , (v_domain_id, v_domain, 1800, 'A', 0, v_ip) + , (v_domain_id, v_domain, 1800,'TXT', 0, 'v=spf1 mx ~all') + , (v_domain_id, 'www.' || v_domain, 1800, 'A', 0, v_ip) + ; +END; +$$;