Skip to content

Commit

Permalink
update for actual _lib code
Browse files Browse the repository at this point in the history
  • Loading branch information
LeKovr committed Jul 8, 2023
1 parent 0424928 commit 07d873e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 39 deletions.
11 changes: 6 additions & 5 deletions acme.sql.sample
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@
-- This var must be set in psql args
SET vars.domain TO :'ACME_DOMAIN';

-- This zone copy hostname
SET vars.ns TO :'NSERVER';

DO $_$
DECLARE
v_domain text := 'acme-' || current_setting('vars.domain'); -- domain name
v_ns text := 'ns.' || current_setting('vars.domain'); -- master DNS host
v_ns_admin text := 'admin.'|| current_setting('vars.domain'); -- master DNS admin email
v_ns text := current_setting('vars.ns'); -- master DNS host

v_refresh int := 10800;
v_retry int := 3600;
v_expire int := 604800;
v_ttl int := 1800;

v_domain_id integer; -- internal domain id
v_stamp text; -- zone timestamp
v_stamp_old text; -- previous zone SOA timestamp
v_soa text; -- zone SOA

BEGIN
Expand All @@ -31,12 +32,12 @@ BEGIN
RETURN;
END IF;

RAISE NOTICE 'Setup acme zone % for nameserver %',v_domain,v_ns;
RAISE NOTICE 'Setup acme zone % for nameserver %', v_domain, v_ns;

SELECT INTO v_domain_id id FROM domains WHERE name = v_domain;
IF FOUND THEN
-- no any changes needed after creation
RAISE NOTICE 'Zone already exists. Skipping';
RAISE NOTICE 'Zone % already exists. Skipping', v_domain;
RETURN;
END IF;

Expand Down
57 changes: 23 additions & 34 deletions domain.sql.sample
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,40 @@
Complete PowerDNS zone records
*/

SET vars.ns TO :'NSERVER';
-- This file control sum
SET vars.csum TO :'csum';
-- This zone copy hostname
SET vars.ns TO :'NSERVER';

DO $_$
DECLARE
v_domain text := 'dev.lan'; -- domain name
v_ip text := '127.0.0.1'; -- base ip
v_ip1 text := '127.0.1.1'; -- some another ip
v_ns text := current_setting('vars.ns'); -- 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 SOA timestamp
v_stamp_old text; -- previous zone SOA timestamp
v_soa text; -- zone SOA

v_refresh int := 10800; -- 3 hours
v_retry int := 3600; -- 1 hour
v_expire int := 604800; -- 7 days
v_ttl int := 1800; -- 30 min

/*
refresh -- time lag until the slave again asks the master for a current version of the zone file
retry -- Should this request go unanswered, the “Retry” field regulates when a new attempt is to be carried out (< refresh)
expire -- determines how long the zone file may still be used before the server refuses DNS information delivery
ttl -- how long a client may hold the requested information in the cache before a new request must be sent

Each value in seconds
*/

v_domain text := 'dev.lan'; -- domain name
v_ns_admin text := 'admin.ns.dev.lan'; -- master DNS admin email
v_ip text := '127.0.0.1'; -- base ip
v_ip1 text := '127.0.1.1'; -- some another ip
v_ttl INTEGER := 60; -- 1 min
v_domain_id integer; -- internal domain id
v_soa text; -- zone SOA
BEGIN
v_domain_id := domain_id(v_domain);

-- calculate SOA with next serial
SELECT INTO v_stamp_old split_part(content, ' ', 3) FROM records WHERE domain_id = v_domain_id AND type = 'SOA';
v_stamp := soa_upd(v_stamp_old);
v_soa := concat_ws(' ', v_ns, v_ns_admin, v_stamp, v_refresh, v_retry, v_expire, v_ttl);
-- check csum, do not run twice
IF csum_exists(v_domain_id) THEN
RAISE NOTICE 'Domain % does not changed. Skipping', v_domain;
RETURN;
END IF;

-- clear zone
DELETE FROM records WHERE domain_id = v_domain_id;

v_soa := soa_mk(v_domain_id, v_ns_admin);

-- all zone records are following here
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, v_ttl, 'NS', 0, 'ns.' || v_domain)
, (v_domain_id, v_domain, v_ttl, 'MX', 5, 'mail.' || v_domain)
, (v_domain_id, v_domain, v_ttl,'TXT', 0, 'v=spf1 mx ~all')
INSERT INTO records (domain_id, name, ttl, type, prio, content) VALUES
(v_domain_id, v_domain, v_ttl, 'SOA', 0, v_soa)
, (v_domain_id, v_domain, v_ttl, 'NS', 0, 'ns.' || v_domain)
, (v_domain_id, v_domain, v_ttl, 'MX', 5, 'mail.' || v_domain)
, (v_domain_id, v_domain, v_ttl, 'TXT', 0, '"v=spf1 mx ~all"')

, (v_domain_id, v_domain, v_ttl, 'A', 0, v_ip)
, (v_domain_id, 'www.' || v_domain, v_ttl, 'A', 0, v_ip)
Expand Down

0 comments on commit 07d873e

Please sign in to comment.