no generation if no ptr record
This commit is contained in:
parent
a0b93b94e0
commit
fee0f35f45
1 changed files with 84 additions and 82 deletions
166
main.py
166
main.py
|
@ -148,7 +148,7 @@ def write_dns_files(api_client):
|
|||
aaaa_records=aaaa_records,
|
||||
cname_records=cname_records)
|
||||
|
||||
filename = 'dns.{zone}.zone'.format(zone=zone_name)
|
||||
filename = 'generated/dns.{zone}.zone'.format(zone=zone_name)
|
||||
with open(filename, 'w+') as f:
|
||||
f.write(zone_file_content)
|
||||
|
||||
|
@ -198,108 +198,110 @@ def write_dns_reverse_file(api_client):
|
|||
|
||||
# For the ipv4, we need to agregate the subnets together, because
|
||||
# we can only have reverse for /24, /16 and /8.
|
||||
subnets = []
|
||||
for net in zone['cidrs']:
|
||||
net = netaddr.IPNetwork(net)
|
||||
# on fragmente les subnets
|
||||
# dans les tailles qui vont bien.
|
||||
if net.prefixlen > 24:
|
||||
subnets.extend(net.subnet(32))
|
||||
elif net.prefixlen > 16:
|
||||
subnets.extend(net.subnet(24))
|
||||
elif net.prefixlen > 8:
|
||||
subnets.extend(net.subnet(16))
|
||||
else:
|
||||
subnets.extend(net.subnet(8))
|
||||
if zone['ptr_records']:
|
||||
subnets = []
|
||||
for net in zone['cidrs']:
|
||||
net = netaddr.IPNetwork(net)
|
||||
# on fragmente les subnets
|
||||
# dans les tailles qui vont bien.
|
||||
if net.prefixlen > 24:
|
||||
subnets.extend(net.subnet(32))
|
||||
elif net.prefixlen > 16:
|
||||
subnets.extend(net.subnet(24))
|
||||
elif net.prefixlen > 8:
|
||||
subnets.extend(net.subnet(16))
|
||||
else:
|
||||
subnets.extend(net.subnet(8))
|
||||
|
||||
for subnet in subnets:
|
||||
# Then, using the first ip address of the subnet and the
|
||||
# prefix length, we can obtain the name of the reverse zone
|
||||
_address = netaddr.IPAddress(subnet.first)
|
||||
rev_dns_a = _address.reverse_dns.split('.')[:-1]
|
||||
if subnet.prefixlen == 8:
|
||||
zone_name,prefix_length = ('.'.join(rev_dns_a[3:]), 3)
|
||||
elif subnet.prefixlen == 16:
|
||||
zone_name,prefix_length = ('.'.join(rev_dns_a[2:]), 2)
|
||||
elif subnet.prefixlen == 24:
|
||||
zone_name,prefix_length = ('.'.join(rev_dns_a[1:]), 1)
|
||||
for subnet in subnets:
|
||||
# Then, using the first ip address of the subnet and the
|
||||
# prefix length, we can obtain the name of the reverse zone
|
||||
_address = netaddr.IPAddress(subnet.first)
|
||||
rev_dns_a = _address.reverse_dns.split('.')[:-1]
|
||||
if subnet.prefixlen == 8:
|
||||
zone_name,prefix_length = ('.'.join(rev_dns_a[3:]), 3)
|
||||
elif subnet.prefixlen == 16:
|
||||
zone_name,prefix_length = ('.'.join(rev_dns_a[2:]), 2)
|
||||
elif subnet.prefixlen == 24:
|
||||
zone_name,prefix_length = ('.'.join(rev_dns_a[1:]), 1)
|
||||
|
||||
soa = template_soa.format(zone=zone_name,
|
||||
soa = template_soa.format(zone=zone_name,
|
||||
mail=soa_mail,
|
||||
serial=serial,
|
||||
refresh=zone['soa']['refresh'],
|
||||
retry=zone['soa']['retry'],
|
||||
expire=zone['soa']['expire'],
|
||||
ttl=zone['soa']['ttl'])
|
||||
ptr_records = "\n".join(
|
||||
template_ptr.format(hostname=host['hostname']+extension,
|
||||
target=get_ip_reverse(host['ipv4'],prefix_length))
|
||||
for host in zone['ptr_records'] if host['ipv4'] in subnet
|
||||
)
|
||||
zone_file_content = template_reverse.format(soa=soa,
|
||||
ns_records=ns_records,
|
||||
mx_records=mx_records,
|
||||
ptr_records = ptr_records)
|
||||
|
||||
filename = 'dns.{zone}.zone'.format(zone=zone_name)
|
||||
with open(filename, 'w+') as f:
|
||||
f.write(zone_file_content)
|
||||
|
||||
|
||||
### Continue with the ipv6 reverse
|
||||
if zone['ptr_v6_records']:
|
||||
net = netaddr.IPNetwork(zone['prefix_v6']+"/"+str(zone['prefix_v6_length']))
|
||||
net_class = max(((net.prefixlen - 1) // 4) + 1, 1)
|
||||
zone6_name = ".".join(
|
||||
netaddr.IPAddress(net.first).reverse_dns.split('.')[32 - net_class:]
|
||||
)
|
||||
|
||||
|
||||
soa = template_soa.format(zone=zone6_name,
|
||||
mail=soa_mail,
|
||||
serial=serial,
|
||||
refresh=zone['soa']['refresh'],
|
||||
retry=zone['soa']['retry'],
|
||||
expire=zone['soa']['expire'],
|
||||
ttl=zone['soa']['ttl'])
|
||||
|
||||
prefix_length = int((128 - net.prefixlen)/4)
|
||||
ptr_records = "\n".join(
|
||||
template_ptr.format(hostname=host['hostname']+extension,
|
||||
target=get_ip_reverse(host['ipv4'],prefix_length))
|
||||
for host in zone['ptr_records'] if host['ipv4'] in subnet
|
||||
target=get_ip_reverse(ip['ipv6'],prefix_length))
|
||||
for host in zone['ptr_v6_records'] for ip in host['ipv6']
|
||||
)
|
||||
zone_file_content = template_reverse.format(soa=soa,
|
||||
ns_records=ns_records,
|
||||
mx_records=mx_records,
|
||||
ptr_records = ptr_records)
|
||||
|
||||
filename = 'dns.{zone}.zone'.format(zone=zone_name)
|
||||
with open(filename, 'w+') as f:
|
||||
f.write(zone_file_content)
|
||||
if zone6_name in zone_v6:
|
||||
# we already created the file, we ignore the soa
|
||||
zone_file_content = template_reverse.format(soa="",
|
||||
ns_records=ns_records,
|
||||
mx_records=mx_records,
|
||||
ptr_records = ptr_records)
|
||||
|
||||
|
||||
### Continue with the ipv6 reverse
|
||||
net = netaddr.IPNetwork(zone['prefix_v6']+"/"+str(zone['prefix_v6_length']))
|
||||
net_class = max(((net.prefixlen - 1) // 4) + 1, 1)
|
||||
zone6_name = ".".join(
|
||||
netaddr.IPAddress(net.first).reverse_dns.split('.')[32 - net_class:]
|
||||
)
|
||||
|
||||
|
||||
soa = template_soa.format(zone=zone6_name,
|
||||
mail=soa_mail,
|
||||
serial=serial,
|
||||
refresh=zone['soa']['refresh'],
|
||||
retry=zone['soa']['retry'],
|
||||
expire=zone['soa']['expire'],
|
||||
ttl=zone['soa']['ttl'])
|
||||
|
||||
prefix_length = int((128 - net.prefixlen)/4)
|
||||
ptr_records = "\n".join(
|
||||
template_ptr.format(hostname=host['hostname']+extension,
|
||||
target=get_ip_reverse(ip['ipv6'],prefix_length))
|
||||
for host in zone['ptr_v6_records'] for ip in host['ipv6']
|
||||
)
|
||||
if zone6_name in zone_v6:
|
||||
# we already created the file, we ignore the soa
|
||||
zone_file_content = template_reverse.format(soa="",
|
||||
ns_records=ns_records,
|
||||
mx_records=mx_records,
|
||||
ptr_records = ptr_records)
|
||||
filename = 'dns.{zone}zone'.format(zone=zone6_name)
|
||||
with open(filename, 'a') as f:
|
||||
f.write(zone_file_content)
|
||||
else:
|
||||
# we create the file from scratch
|
||||
zone_file_content = template_reverse.format(soa=soa,
|
||||
ns_records=ns_records,
|
||||
mx_records=mx_records,
|
||||
ptr_records = ptr_records)
|
||||
|
||||
|
||||
filename = 'dns.{zone}zone'.format(zone=zone6_name)
|
||||
with open(filename, 'a') as f:
|
||||
f.write(zone_file_content)
|
||||
else:
|
||||
# we create the file from scratch
|
||||
zone_file_content = template_reverse.format(soa=soa,
|
||||
ns_records=ns_records,
|
||||
mx_records=mx_records,
|
||||
ptr_records = ptr_records)
|
||||
|
||||
|
||||
filename = 'dns.{zone}zone'.format(zone=zone6_name)
|
||||
with open(filename, 'w+') as f:
|
||||
f.write(zone_file_content)
|
||||
zone_v6.append(zone6_name)
|
||||
filename = 'dns.{zone}zone'.format(zone=zone6_name)
|
||||
with open(filename, 'w+') as f:
|
||||
f.write(zone_file_content)
|
||||
zone_v6.append(zone6_name)
|
||||
|
||||
api_client = Re2oAPIClient(api_hostname, api_username, api_password)
|
||||
|
||||
client_hostname = socket.gethostname().split('.', 1)[0]
|
||||
|
||||
for service in api_client.list("services/regen/"):
|
||||
#for service in api_client.list("services/regen/"):
|
||||
# if service['hostname'] == client_hostname and \
|
||||
# service['service_name'] == 'dns' and \
|
||||
# service['need_regen']:
|
||||
#write_dns_files(api_client)
|
||||
write_dns_reverse_file(api_client)
|
||||
write_dns_files(api_client)
|
||||
write_dns_reverse_file(api_client)
|
||||
# api_client.patch(service['api_url'], data={'need_regen': False})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue