2020-10-16 15:44:31 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from os import listdir
|
|
|
|
from os.path import isfile, join
|
|
|
|
from subprocess import check_output
|
2024-11-08 05:39:05 +00:00
|
|
|
from bundlewrap.utils.ui import io
|
2020-10-16 15:44:31 +00:00
|
|
|
|
|
|
|
zone_path = join(repo.path, 'data', 'powerdns', 'files', 'bind-zones')
|
|
|
|
|
2023-01-29 10:01:48 +00:00
|
|
|
nameservers = set()
|
2020-10-17 10:56:17 +00:00
|
|
|
for rnode in sorted(repo.nodes_in_group('dns')):
|
2023-01-29 10:01:48 +00:00
|
|
|
nameservers.add(rnode.metadata.get('powerdns/my_hostname', rnode.metadata.get('hostname')))
|
2020-10-16 15:44:31 +00:00
|
|
|
|
2023-05-16 17:23:52 +00:00
|
|
|
my_primary_servers = set()
|
|
|
|
for ips in node.metadata.get('powerdns/my_primary_servers', {}).values():
|
|
|
|
my_primary_servers.update(ips)
|
|
|
|
|
2020-10-16 11:19:44 +00:00
|
|
|
directories = {
|
|
|
|
'/etc/powerdns/pdns.d': {
|
|
|
|
'purge': True,
|
2020-10-16 15:59:46 +00:00
|
|
|
'needs': {
|
|
|
|
'pkg_apt:pdns-server',
|
|
|
|
'pkg_apt:pdns-backend-bind',
|
|
|
|
'pkg_apt:pdns-backend-pgsql',
|
|
|
|
},
|
|
|
|
'triggers': {
|
|
|
|
'svc_systemd:pdns:restart',
|
|
|
|
},
|
2020-10-16 11:19:44 +00:00
|
|
|
},
|
2020-10-16 15:44:31 +00:00
|
|
|
'/var/lib/powerdns/zones': {
|
|
|
|
'purge': True,
|
2020-10-16 15:59:46 +00:00
|
|
|
'needs': {
|
|
|
|
'pkg_apt:pdns-backend-bind',
|
|
|
|
},
|
2020-10-16 15:44:31 +00:00
|
|
|
}
|
2020-10-16 11:19:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
files = {
|
|
|
|
'/etc/powerdns/pdns.conf': {
|
2020-10-16 15:44:31 +00:00
|
|
|
'content_type': 'mako',
|
|
|
|
'context': {
|
2023-01-29 07:35:08 +00:00
|
|
|
'api_key': node.metadata.get('powerdns/api_key'),
|
|
|
|
'my_hostname': node.metadata.get('powerdns/my_hostname', node.metadata.get('hostname')),
|
|
|
|
'is_secondary': node.metadata.get('powerdns/is_secondary', False),
|
2023-05-16 17:23:52 +00:00
|
|
|
'my_primary_servers': my_primary_servers,
|
2023-01-29 07:35:08 +00:00
|
|
|
'my_secondary_servers': node.metadata.get('powerdns/my_secondary_servers', set()),
|
2020-10-16 15:44:31 +00:00
|
|
|
},
|
2020-10-16 15:59:46 +00:00
|
|
|
'needs': {
|
|
|
|
'pkg_apt:pdns-server',
|
|
|
|
},
|
|
|
|
'triggers': {
|
|
|
|
'svc_systemd:pdns:restart',
|
|
|
|
},
|
2020-10-16 15:44:31 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
svc_systemd = {
|
|
|
|
'pdns': {
|
2020-10-16 11:19:44 +00:00
|
|
|
'needs': {
|
2020-10-16 15:59:46 +00:00
|
|
|
'pkg_apt:pdns-server',
|
|
|
|
'pkg_apt:pdns-backend-bind',
|
|
|
|
'pkg_apt:pdns-backend-pgsql',
|
2020-10-16 11:19:44 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-10-16 15:44:31 +00:00
|
|
|
|
2020-10-16 18:40:05 +00:00
|
|
|
actions = {
|
|
|
|
'powerdns_reload_zones': {
|
|
|
|
'triggered': True,
|
2024-06-26 05:11:44 +00:00
|
|
|
'command': r'pdns_control rediscover; pdns_control reload; pdns_control notify \*',
|
2023-11-11 19:38:45 +00:00
|
|
|
'after': {
|
2020-10-16 18:40:05 +00:00
|
|
|
'svc_systemd:pdns',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-02-18 17:12:25 +00:00
|
|
|
if node.metadata.get('powerdns/features/bind', False):
|
2020-10-16 15:44:31 +00:00
|
|
|
primary_zones = set()
|
|
|
|
for zone in listdir(zone_path):
|
|
|
|
if not isfile(join(zone_path, zone)) or zone.startswith(".") or zone.startswith("_"):
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
2024-11-08 05:39:05 +00:00
|
|
|
output = check_output(['git', 'log', '-1', '--pretty=%ci']).decode('utf-8').strip()
|
2020-10-16 15:44:31 +00:00
|
|
|
serial = datetime.strptime(output, '%Y-%m-%d %H:%M:%S %z').strftime('%y%m%d%H%M')
|
2024-11-08 05:39:05 +00:00
|
|
|
except Exception as e:
|
|
|
|
io.stderr(f"Error while parsing commit time for {zone} serial: {e!r}")
|
2020-10-16 15:44:31 +00:00
|
|
|
serial = datetime.now().strftime('%y%m%d0000')
|
|
|
|
|
|
|
|
primary_zones.add(zone)
|
|
|
|
|
2021-09-06 14:42:14 +00:00
|
|
|
files[f'/var/lib/powerdns/zones/{zone}'] = {
|
2020-10-16 15:44:31 +00:00
|
|
|
'content_type': 'mako',
|
|
|
|
'context': {
|
2023-01-29 10:01:48 +00:00
|
|
|
'NAMESERVERS': '\n'.join(sorted({f'@ IN NS {ns}.' for ns in nameservers})),
|
|
|
|
'SERIAL': serial,
|
2021-09-06 14:42:14 +00:00
|
|
|
'metadata_records': node.metadata.get(f'powerdns/bind-zones/{zone}/records', []),
|
2020-10-16 15:44:31 +00:00
|
|
|
},
|
2021-09-06 14:42:14 +00:00
|
|
|
'source': f'bind-zones/{zone}',
|
2021-09-22 14:42:26 +00:00
|
|
|
'test_with': f'named-checkzone {zone} {{}}',
|
2020-10-16 15:59:46 +00:00
|
|
|
'triggers': {
|
2020-10-16 18:40:05 +00:00
|
|
|
'action:powerdns_reload_zones',
|
2020-10-16 15:59:46 +00:00
|
|
|
},
|
2020-12-18 05:41:24 +00:00
|
|
|
'needed_by': {
|
|
|
|
'svc_systemd:pdns',
|
|
|
|
},
|
2020-10-16 15:44:31 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 15:59:46 +00:00
|
|
|
files['/etc/powerdns/pdns.d/bind.conf'] = {
|
|
|
|
'needs': {
|
|
|
|
'pkg_apt:pdns-backend-bind',
|
|
|
|
},
|
2020-12-18 05:41:24 +00:00
|
|
|
'needed_by': {
|
|
|
|
'svc_systemd:pdns',
|
|
|
|
},
|
2020-10-16 15:59:46 +00:00
|
|
|
'triggers': {
|
2020-10-16 18:40:05 +00:00
|
|
|
'action:powerdns_reload_zones',
|
2020-10-16 15:59:46 +00:00
|
|
|
},
|
|
|
|
}
|
2020-10-16 17:23:45 +00:00
|
|
|
|
2020-10-16 15:44:31 +00:00
|
|
|
files['/etc/powerdns/named.conf'] = {
|
|
|
|
'content_type': 'mako',
|
|
|
|
'context': {
|
|
|
|
'zones': primary_zones,
|
|
|
|
},
|
2020-10-16 15:59:46 +00:00
|
|
|
'needs': {
|
|
|
|
'pkg_apt:pdns-backend-bind',
|
|
|
|
},
|
2020-12-18 05:41:24 +00:00
|
|
|
'needed_by': {
|
|
|
|
'svc_systemd:pdns',
|
|
|
|
},
|
2020-10-16 15:59:46 +00:00
|
|
|
'triggers': {
|
2020-10-16 18:40:05 +00:00
|
|
|
'action:powerdns_reload_zones',
|
2020-10-16 15:59:46 +00:00
|
|
|
},
|
2020-10-16 15:44:31 +00:00
|
|
|
}
|
2023-01-29 07:35:08 +00:00
|
|
|
else:
|
|
|
|
files['/etc/powerdns/named.conf'] = {
|
|
|
|
'delete': True,
|
|
|
|
'needed_by': {
|
|
|
|
'svc_systemd:pdns',
|
|
|
|
},
|
|
|
|
'triggers': {
|
|
|
|
'action:powerdns_reload_zones',
|
|
|
|
},
|
|
|
|
}
|
2020-10-16 17:23:45 +00:00
|
|
|
|
2023-01-29 07:35:08 +00:00
|
|
|
if node.metadata.get('powerdns/features/pgsql', node.has_bundle('postgresql')):
|
2020-10-16 17:23:45 +00:00
|
|
|
files['/etc/powerdns/pdns.d/pgsql.conf'] = {
|
|
|
|
'content_type': 'mako',
|
|
|
|
'context': {
|
2023-01-29 07:35:08 +00:00
|
|
|
'password': node.metadata.get('postgresql/roles/powerdns/password'),
|
2020-10-16 17:23:45 +00:00
|
|
|
},
|
|
|
|
'needs': {
|
|
|
|
'pkg_apt:pdns-backend-pgsql',
|
|
|
|
},
|
2020-12-18 05:41:24 +00:00
|
|
|
'needed_by': {
|
|
|
|
'svc_systemd:pdns',
|
|
|
|
},
|
2020-10-16 17:23:45 +00:00
|
|
|
'triggers': {
|
2020-10-16 18:40:05 +00:00
|
|
|
'action:powerdns_reload_zones',
|
2020-10-16 17:23:45 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-10-16 18:40:05 +00:00
|
|
|
actions['powerdns_load_pgsql_schema'] = {
|
2023-11-11 19:38:45 +00:00
|
|
|
'command': node.metadata.get('postgresql/roles/powerdns/password').format_into('PGPASSWORD={} psql -h 127.0.0.1 -d powerdns -U powerdns -w < /usr/share/pdns-backend-pgsql/schema/schema.pgsql.sql'),
|
2024-06-26 05:11:44 +00:00
|
|
|
'unless': r'sudo -u postgres psql -d powerdns -c "\dt" | grep domains 2>&1 >/dev/null',
|
2020-10-16 18:40:05 +00:00
|
|
|
'needs': {
|
|
|
|
'bundle:postgresql',
|
2023-11-11 19:38:45 +00:00
|
|
|
'pkg_apt:pdns-backend-pgsql',
|
2020-10-16 18:40:05 +00:00
|
|
|
},
|
|
|
|
'needed_by': {
|
|
|
|
'svc_systemd:pdns',
|
|
|
|
},
|
2020-10-16 17:23:45 +00:00
|
|
|
}
|
2023-05-16 17:23:52 +00:00
|
|
|
|
|
|
|
for hostname, ips in node.metadata.get('powerdns/my_primary_servers', {}).items():
|
|
|
|
for ip in ips:
|
|
|
|
ip_name = ip.replace(':', '-')
|
2023-05-16 17:56:21 +00:00
|
|
|
|
|
|
|
actions[f'powerdns_ensure_{ip_name}_in_autoprimaries'] = {
|
|
|
|
'command': f'psql -c "INSERT INTO supermasters (ip, nameserver, account) VALUES (\'{ip}\', \'{hostname}\', \'admin\')" powerdns',
|
|
|
|
'unless': f'test -n \"$(psql -tAqc "SELECT nameserver FROM supermasters WHERE ip = \'{ip}\'" powerdns)\"',
|
|
|
|
'triggers': {
|
|
|
|
'action:powerdns_fix_primaries',
|
|
|
|
},
|
|
|
|
'after': {
|
|
|
|
'action:powerdns_load_pgsql_schema',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actions[f'powerdns_ensure_{hostname}_matches_{ip_name}_in_autoprimaries'] = {
|
|
|
|
'command': f'psql -c "UPDATE supermasters SET nameserver = \'{hostname}\' WHERE ip = \'{ip}\'" powerdns',
|
2023-05-16 17:23:52 +00:00
|
|
|
'unless': f'bash -c "[ \"$(psql -tAqc "SELECT nameserver FROM supermasters WHERE ip = \'{ip}\'" powerdns)\" == \"{hostname}\" ]"',
|
|
|
|
'triggers': {
|
|
|
|
'action:powerdns_fix_primaries',
|
|
|
|
},
|
2023-05-16 17:56:21 +00:00
|
|
|
'after': {
|
|
|
|
f'action:powerdns_ensure_{ip_name}_in_autoprimaries',
|
|
|
|
},
|
2023-05-16 17:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
actions['powerdns_fix_primaries'] = {
|
|
|
|
'command': f'psql -c "UPDATE domains SET master = \'{", ".join(sorted(my_primary_servers))}\'" powerdns',
|
|
|
|
'triggered': True,
|
|
|
|
}
|