no generation if no ptr record

This commit is contained in:
Charlie Jacomme 2018-08-07 10:04:39 +02:00
parent a0b93b94e0
commit fee0f35f45

166
main.py
View file

@ -148,7 +148,7 @@ def write_dns_files(api_client):
aaaa_records=aaaa_records, aaaa_records=aaaa_records,
cname_records=cname_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: with open(filename, 'w+') as f:
f.write(zone_file_content) 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 # For the ipv4, we need to agregate the subnets together, because
# we can only have reverse for /24, /16 and /8. # we can only have reverse for /24, /16 and /8.
subnets = [] if zone['ptr_records']:
for net in zone['cidrs']: subnets = []
net = netaddr.IPNetwork(net) for net in zone['cidrs']:
# on fragmente les subnets net = netaddr.IPNetwork(net)
# dans les tailles qui vont bien. # on fragmente les subnets
if net.prefixlen > 24: # dans les tailles qui vont bien.
subnets.extend(net.subnet(32)) if net.prefixlen > 24:
elif net.prefixlen > 16: subnets.extend(net.subnet(32))
subnets.extend(net.subnet(24)) elif net.prefixlen > 16:
elif net.prefixlen > 8: subnets.extend(net.subnet(24))
subnets.extend(net.subnet(16)) elif net.prefixlen > 8:
else: subnets.extend(net.subnet(16))
subnets.extend(net.subnet(8)) else:
subnets.extend(net.subnet(8))
for subnet in subnets: for subnet in subnets:
# Then, using the first ip address of the subnet and the # Then, using the first ip address of the subnet and the
# prefix length, we can obtain the name of the reverse zone # prefix length, we can obtain the name of the reverse zone
_address = netaddr.IPAddress(subnet.first) _address = netaddr.IPAddress(subnet.first)
rev_dns_a = _address.reverse_dns.split('.')[:-1] rev_dns_a = _address.reverse_dns.split('.')[:-1]
if subnet.prefixlen == 8: if subnet.prefixlen == 8:
zone_name,prefix_length = ('.'.join(rev_dns_a[3:]), 3) zone_name,prefix_length = ('.'.join(rev_dns_a[3:]), 3)
elif subnet.prefixlen == 16: elif subnet.prefixlen == 16:
zone_name,prefix_length = ('.'.join(rev_dns_a[2:]), 2) zone_name,prefix_length = ('.'.join(rev_dns_a[2:]), 2)
elif subnet.prefixlen == 24: elif subnet.prefixlen == 24:
zone_name,prefix_length = ('.'.join(rev_dns_a[1:]), 1) 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, mail=soa_mail,
serial=serial, serial=serial,
refresh=zone['soa']['refresh'], refresh=zone['soa']['refresh'],
retry=zone['soa']['retry'], retry=zone['soa']['retry'],
expire=zone['soa']['expire'], expire=zone['soa']['expire'],
ttl=zone['soa']['ttl']) ttl=zone['soa']['ttl'])
prefix_length = int((128 - net.prefixlen)/4)
ptr_records = "\n".join( ptr_records = "\n".join(
template_ptr.format(hostname=host['hostname']+extension, template_ptr.format(hostname=host['hostname']+extension,
target=get_ip_reverse(host['ipv4'],prefix_length)) target=get_ip_reverse(ip['ipv6'],prefix_length))
for host in zone['ptr_records'] if host['ipv4'] in subnet for host in zone['ptr_v6_records'] for ip in host['ipv6']
) )
zone_file_content = template_reverse.format(soa=soa, if zone6_name in zone_v6:
ns_records=ns_records, # we already created the file, we ignore the soa
mx_records=mx_records, zone_file_content = template_reverse.format(soa="",
ptr_records = ptr_records) ns_records=ns_records,
mx_records=mx_records,
filename = 'dns.{zone}.zone'.format(zone=zone_name) ptr_records = ptr_records)
with open(filename, 'w+') as f:
f.write(zone_file_content)
### Continue with the ipv6 reverse filename = 'dns.{zone}zone'.format(zone=zone6_name)
net = netaddr.IPNetwork(zone['prefix_v6']+"/"+str(zone['prefix_v6_length'])) with open(filename, 'a') as f:
net_class = max(((net.prefixlen - 1) // 4) + 1, 1) f.write(zone_file_content)
zone6_name = ".".join( else:
netaddr.IPAddress(net.first).reverse_dns.split('.')[32 - net_class:] # 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)
soa = template_soa.format(zone=zone6_name, filename = 'dns.{zone}zone'.format(zone=zone6_name)
mail=soa_mail, with open(filename, 'w+') as f:
serial=serial, f.write(zone_file_content)
refresh=zone['soa']['refresh'], zone_v6.append(zone6_name)
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, 'w+') as f:
f.write(zone_file_content)
zone_v6.append(zone6_name)
api_client = Re2oAPIClient(api_hostname, api_username, api_password) api_client = Re2oAPIClient(api_hostname, api_username, api_password)
client_hostname = socket.gethostname().split('.', 1)[0] 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 \ # if service['hostname'] == client_hostname and \
# service['service_name'] == 'dns' and \ # service['service_name'] == 'dns' and \
# service['need_regen']: # service['need_regen']:
#write_dns_files(api_client) write_dns_files(api_client)
write_dns_reverse_file(api_client) write_dns_reverse_file(api_client)
# api_client.patch(service['api_url'], data={'need_regen': False}) # api_client.patch(service['api_url'], data={'need_regen': False})