2018-02-05 06:03:08 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-02-05 06:17:16 +00:00
|
|
|
import difflib
|
|
|
|
import hashlib
|
2018-02-05 06:03:08 +00:00
|
|
|
import os
|
2018-02-05 06:17:16 +00:00
|
|
|
import os.path
|
2019-01-12 16:09:07 +00:00
|
|
|
import subprocess
|
2018-02-05 06:17:16 +00:00
|
|
|
import sys
|
2018-02-05 07:37:55 +00:00
|
|
|
import time
|
2018-02-05 06:03:08 +00:00
|
|
|
|
2018-12-14 18:18:10 +00:00
|
|
|
# Custom modules
|
|
|
|
import consul
|
|
|
|
|
2018-02-05 06:03:08 +00:00
|
|
|
# Third-party imports
|
|
|
|
import jinja2
|
2018-12-15 00:47:22 +00:00
|
|
|
import requests
|
2018-02-05 06:03:08 +00:00
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
2019-01-12 16:09:07 +00:00
|
|
|
rndc_queue = []
|
|
|
|
|
|
|
|
|
|
|
|
def call_rndc(params):
|
|
|
|
command = ["rndc"]
|
|
|
|
command.extend(params)
|
|
|
|
|
|
|
|
if 'DEBUG' in os.environ and os.environ['DEBUG'] == 'true':
|
|
|
|
print('DBG: Call "{}"'.format(" ".join(command)))
|
|
|
|
else:
|
|
|
|
subprocess.check_call(command, stdout=sys.stdout, stderr=sys.stderr)
|
|
|
|
|
2018-02-05 06:03:08 +00:00
|
|
|
|
2018-02-05 06:17:16 +00:00
|
|
|
def diff_files(file1, file2):
|
|
|
|
fromlines = []
|
|
|
|
tolines = []
|
|
|
|
if os.path.isfile(file1):
|
|
|
|
with open(file1) as ff:
|
|
|
|
fromlines = ff.readlines()
|
|
|
|
if os.path.isfile(file2):
|
|
|
|
with open(file2) as tf:
|
|
|
|
tolines = tf.readlines()
|
|
|
|
|
|
|
|
print(''.join(difflib.unified_diff(
|
|
|
|
fromlines, tolines, file1, file2)))
|
|
|
|
|
|
|
|
|
2019-01-12 16:09:07 +00:00
|
|
|
def exec_rndc_queue():
|
|
|
|
global rndc_queue
|
|
|
|
|
|
|
|
for params in rndc_queue:
|
|
|
|
call_rndc(params)
|
|
|
|
|
|
|
|
rndc_queue = []
|
|
|
|
|
|
|
|
|
2018-02-05 06:17:16 +00:00
|
|
|
def hash_file(filename):
|
|
|
|
if not os.path.isfile(filename):
|
|
|
|
return ""
|
|
|
|
|
|
|
|
hasher = hashlib.sha1()
|
2018-02-05 07:37:55 +00:00
|
|
|
with open(filename, 'r') as afile:
|
|
|
|
lines = afile.readlines()
|
|
|
|
|
|
|
|
hasher.update(''.join(lines).encode('utf-8'))
|
2018-02-05 06:17:16 +00:00
|
|
|
return hasher.hexdigest()
|
|
|
|
|
|
|
|
|
2019-01-12 16:09:07 +00:00
|
|
|
def queue_rndc_call(params):
|
|
|
|
global rndc_queue
|
|
|
|
|
|
|
|
rndc_queue.append(params)
|
|
|
|
|
|
|
|
|
|
|
|
def write_named_conf(zones):
|
|
|
|
tpl = jinja2.Template(open("named.conf").read())
|
|
|
|
zone_content = tpl.render({
|
|
|
|
"zones": zones,
|
|
|
|
})
|
|
|
|
|
|
|
|
with open("zones/named.conf.new", "w") as f:
|
|
|
|
f.write(zone_content)
|
|
|
|
|
|
|
|
if hash_file("zones/named.conf.new") != hash_file("zones/named.conf"):
|
|
|
|
print("Generated and replaced named.conf")
|
|
|
|
diff_files("zones/named.conf", "zones/named.conf.new")
|
|
|
|
os.rename("zones/named.conf.new", "zones/named.conf")
|
|
|
|
|
|
|
|
queue_rndc_call(['reconfig'])
|
|
|
|
else:
|
|
|
|
os.unlink("zones/named.conf.new")
|
|
|
|
|
|
|
|
|
2018-12-15 00:47:22 +00:00
|
|
|
def healthcheck():
|
2018-12-17 17:04:10 +00:00
|
|
|
if os.getenv('HC_PING') is not None:
|
2018-12-15 00:47:22 +00:00
|
|
|
requests.get(os.getenv('HC_PING'))
|
|
|
|
|
|
|
|
|
2018-02-05 06:03:08 +00:00
|
|
|
def main():
|
2018-12-15 00:30:22 +00:00
|
|
|
consul_zones = consul.get_zones()
|
2019-09-12 07:39:58 +00:00
|
|
|
write_named_conf(consul_zones)
|
2018-02-05 06:03:08 +00:00
|
|
|
|
2019-01-12 16:09:07 +00:00
|
|
|
exec_rndc_queue()
|
|
|
|
|
2018-12-15 00:47:22 +00:00
|
|
|
healthcheck()
|
|
|
|
|
2018-02-05 06:03:08 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|