Created
September 20, 2013 20:23
-
-
Save brimston3/6643335 to your computer and use it in GitHub Desktop.
PowerDNS. Whenever the SOA is updated, queue the zone to have it rectified.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import subprocess | |
import psycopg2 | |
conn_string = "host='localhost' dbname='pdns' user='pdns' password=''" | |
def call_rectifyzone(zone): | |
proc = 0 | |
try: | |
proc = subprocess.check_output(['/usr/bin/pdnssec','rectify-zone',zone]) | |
except e: | |
print "An error occurred trying to call pdnssec rectify-zone:",str(e) | |
raise | |
conn = psycopg2.connect(conn_string) | |
cursor = conn.cursor() | |
try: | |
cursor.execute('select d.id,d.name from soa_update_queue soaq, domains d where soaq.domain_id = d.id') | |
except e: | |
print "Could not execute soa_update_queue selection query:", str(e) | |
raise | |
rows = cursor.fetchall() | |
for r in rows: | |
call_rectifyzone(r[1]) | |
try: | |
cursor.execute('delete from soa_update_queue where domain_id = %s',(r[0],)) | |
except e: | |
print 'Could not delete soa_update_queue entry after rectify:',str(e) | |
raise | |
conn.commit() | |
cursor.close() | |
conn.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE soa_update_queue (domain_id integer primary key); | |
CREATE OR REPLACE FUNCTION fcn_queue_soa_updates() RETURNS TRIGGER | |
LANGUAGE plpgsql | |
AS | |
$$ | |
DECLARE | |
insertquery text; | |
BEGIN | |
IF (NEW.type = 'SOA') THEN | |
insertquery := 'INSERT INTO soa_update_queue (domain_id) SELECT '|| NEW.domain_id || ' WHERE NOT EXISTS ( SELECT domain_id FROM soa_update_queue WHERE domain_id = '|| NEW.domain_id || ')'; | |
EXECUTE insertquery; | |
END IF; | |
RETURN NEW; | |
END; | |
$$; | |
CREATE TRIGGER records_upd | |
AFTER INSERT OR UPDATE | |
ON records | |
FOR EACH ROW | |
EXECUTE PROCEDURE fcn_queue_soa_updates(); | |
GRANT SELECT,INSERT,UPDATE,DELETE ON soa_update_queue TO pdns; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist and associated files are copyright Andrew Domaszek Sept., 2013, all rights reserved.
License is BSD-new.