-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.acme.sql
50 lines (39 loc) · 1.43 KB
/
setup.acme.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
SET vars.domain TO :'ACME_DOMAIN';
SET vars.ns TO :'ACME_NS';
SET vars.admin TO :'NS_ADMIN';
DO $_$
DECLARE
v_domain text := current_setting('vars.domain'); -- domain name
v_ns text := current_setting('vars.ns'); -- master DNS host
v_ns_admin text := current_setting('vars.admin'); -- master DNS admin email
v_key text := '00'; -- zone serial suffix
v_domain_id integer; -- internal domain id
v_stamp text; -- zone timestamp
v_soa text; -- zone SOA
BEGIN
IF v_domain = '' THEN
RAISE NOTICE 'ACME_DOMAIN is not set. Skipping acme zone setup';
RETURN;
END IF;
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';
RETURN;
END IF;
INSERT INTO domains (name, type) VALUES
(v_domain, 'NATIVE')
RETURNING id INTO v_domain_id
;
INSERT INTO domainmetadata(domain_id, kind, content) VALUES
(v_domain_id, 'SOA-EDIT-API', 'INCREASE')
;
v_stamp := to_char(current_timestamp, 'YYYYMMDD') || v_key;
v_soa := concat_ws(' ', v_ns, v_ns_admin, v_stamp, '10800 3600 604800 1800');
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, v_ns)
;
END;
$_$;