Formatage
This commit is contained in:
parent
90f9d15ced
commit
4bba92b930
1 changed files with 183 additions and 153 deletions
170
main.py
170
main.py
|
@ -10,7 +10,7 @@ import sys
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
path =(os.path.dirname(os.path.abspath(__file__)))
|
path = os.path.dirname(os.path.abspath(__file__))
|
||||||
config = ConfigParser()
|
config = ConfigParser()
|
||||||
config.read(path+'/config.ini')
|
config.read(path+'/config.ini')
|
||||||
|
|
||||||
|
@ -18,14 +18,17 @@ api_hostname = config.get('Re2o', 'hostname')
|
||||||
api_password = config.get('Re2o', 'password')
|
api_password = config.get('Re2o', 'password')
|
||||||
api_username = config.get('Re2o', 'username')
|
api_username = config.get('Re2o', 'username')
|
||||||
|
|
||||||
template_soa = ("$ORIGIN {zone}.\n"
|
template_soa = (
|
||||||
|
"$ORIGIN {zone}.\n"
|
||||||
"@ IN SOA {ns}. {mail} (\n"
|
"@ IN SOA {ns}. {mail} (\n"
|
||||||
" {serial} ; serial\n"
|
" {serial} ; serial\n"
|
||||||
" {refresh} ; refresh\n"
|
" {refresh} ; refresh\n"
|
||||||
" {retry} ; retry\n"
|
" {retry} ; retry\n"
|
||||||
" {expire} ; expire\n"
|
" {expire} ; expire\n"
|
||||||
" {ttl} ; ttl\n"
|
" {ttl} ; ttl\n"
|
||||||
")")
|
")"
|
||||||
|
)
|
||||||
|
|
||||||
template_originv4 = "@ IN A {ipv4}"
|
template_originv4 = "@ IN A {ipv4}"
|
||||||
template_originv6 = "@ IN AAAA {ipv6}"
|
template_originv6 = "@ IN AAAA {ipv6}"
|
||||||
template_ns = "@ IN NS {target}."
|
template_ns = "@ IN NS {target}."
|
||||||
|
@ -38,7 +41,8 @@ template_cname = "{hostname} IN CNAME {alias}."
|
||||||
template_ptr = "{target} IN PTR {hostname}."
|
template_ptr = "{target} IN PTR {hostname}."
|
||||||
template_sshfp = "{hostname} SSHFP {algo} {type} {fp}"
|
template_sshfp = "{hostname} SSHFP {algo} {type} {fp}"
|
||||||
|
|
||||||
template_zone = ("$TTL 2D\n"
|
template_zone = (
|
||||||
|
"$TTL 2D\n"
|
||||||
"{soa}\n"
|
"{soa}\n"
|
||||||
"\n"
|
"\n"
|
||||||
"{originv4}\n"
|
"{originv4}\n"
|
||||||
|
@ -59,30 +63,22 @@ template_zone = ("$TTL 2D\n"
|
||||||
"\n"
|
"\n"
|
||||||
"{aaaa_records}\n"
|
"{aaaa_records}\n"
|
||||||
"\n"
|
"\n"
|
||||||
"{cname_records}")
|
"{cname_records}"
|
||||||
|
)
|
||||||
|
|
||||||
template_reverse = ("$TTL 2D\n"
|
template_reverse = (
|
||||||
|
"$TTL 2D\n"
|
||||||
"{soa}\n"
|
"{soa}\n"
|
||||||
"\n"
|
"\n"
|
||||||
"{ns_records}\n"
|
"{ns_records}\n"
|
||||||
"\n"
|
"\n"
|
||||||
"{mx_records}\n"
|
"{mx_records}\n"
|
||||||
"\n"
|
"\n"
|
||||||
"{ptr_records}\n")
|
"{ptr_records}\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
def write_dns_files(api_client, processes):
|
||||||
|
for zone in api_client.list("dns/zones"):
|
||||||
def write_dns_files(api_client):
|
|
||||||
dns_zones = api_client.list("dns/zones")
|
|
||||||
zone_names = {zone['name'][1:] for zone in dns_zones}
|
|
||||||
records_to_add = {name: {'a': [], 'aaaa': [], 'cname': []} for name in zone_names}
|
|
||||||
|
|
||||||
def add_record(zone, record, type):
|
|
||||||
nonlocal records_to_add
|
|
||||||
records_to_add[zone][type].append(record)
|
|
||||||
return False
|
|
||||||
|
|
||||||
for zone in sorted(dns_zones, key=lambda zone:zone['name'].count('.')):
|
|
||||||
zone_name = zone['name'][1:]
|
zone_name = zone['name'][1:]
|
||||||
|
|
||||||
now = datetime.datetime.now(datetime.timezone.utc)
|
now = datetime.datetime.now(datetime.timezone.utc)
|
||||||
|
@ -95,14 +91,17 @@ def write_dns_files(api_client):
|
||||||
ns = zone['ns_records'][0]['target']
|
ns = zone['ns_records'][0]['target']
|
||||||
else:
|
else:
|
||||||
ns = "ns."+zone_name+"."
|
ns = "ns."+zone_name+"."
|
||||||
soa = template_soa.format(zone=zone_name,
|
|
||||||
|
soa = template_soa.format(
|
||||||
|
zone=zone_name,
|
||||||
mail=soa_mail,
|
mail=soa_mail,
|
||||||
serial=serial,
|
serial=serial,
|
||||||
ns=ns,
|
ns=ns,
|
||||||
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']
|
||||||
|
)
|
||||||
|
|
||||||
if zone['originv4'] is not None:
|
if zone['originv4'] is not None:
|
||||||
originv4 = template_originv4.format(ipv4=zone['originv4']['ipv4'])
|
originv4 = template_originv4.format(ipv4=zone['originv4']['ipv4'])
|
||||||
|
@ -119,71 +118,91 @@ def write_dns_files(api_client):
|
||||||
)
|
)
|
||||||
|
|
||||||
fp_records = "\n".join(
|
fp_records = "\n".join(
|
||||||
template_sshfp.format(hostname=host['hostname'], algo=fp['algo_id'], type="1", fp=fp['hash']['1'] )
|
template_sshfp.format(
|
||||||
|
hostname=host['hostname'],
|
||||||
|
algo=fp['algo_id'],
|
||||||
|
type="1",
|
||||||
|
fp=fp['hash']['1']
|
||||||
|
)
|
||||||
+ "\n" +
|
+ "\n" +
|
||||||
template_sshfp.format(hostname=host['hostname'], algo=fp['algo_id'], type="2", fp=fp['hash']['2'] )
|
template_sshfp.format(
|
||||||
|
hostname=host['hostname'],
|
||||||
|
algo=fp['algo_id'],
|
||||||
|
type="2",
|
||||||
|
fp=fp['hash']['2']
|
||||||
|
)
|
||||||
for host in zone['sshfp_records']
|
for host in zone['sshfp_records']
|
||||||
for fp in host['sshfp']
|
for fp in host['sshfp']
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
mx_records = "\n".join(
|
mx_records = "\n".join(
|
||||||
template_mx.format(priority=x['priority'],
|
template_mx.format(
|
||||||
target=x['target'])
|
priority=x['priority'],
|
||||||
|
target=x['target']
|
||||||
|
)
|
||||||
for x in zone['mx_records']
|
for x in zone['mx_records']
|
||||||
)
|
)
|
||||||
|
|
||||||
txt_records = "\n".join(
|
txt_records = "\n".join(
|
||||||
template_txt.format(field1=x['field1'],
|
template_txt.format(
|
||||||
field2=x['field2'])
|
field1=x['field1'],
|
||||||
|
field2=x['field2']
|
||||||
|
)
|
||||||
for x in zone['txt_records']
|
for x in zone['txt_records']
|
||||||
)
|
)
|
||||||
|
|
||||||
srv_records = "\n".join(
|
srv_records = "\n".join(
|
||||||
template_srv.format(service=x['service'],
|
template_srv.format(
|
||||||
|
service=x['service'],
|
||||||
protocol=x['protocol'],
|
protocol=x['protocol'],
|
||||||
zone=zone_name,
|
zone=zone_name,
|
||||||
ttl=x['ttl'],
|
ttl=x['ttl'],
|
||||||
priority=x['priority'],
|
priority=x['priority'],
|
||||||
weight=x['weight'],
|
weight=x['weight'],
|
||||||
port=x['port'],
|
port=x['port'],
|
||||||
target=x['target'])
|
target=x['target']
|
||||||
|
)
|
||||||
for x in zone['srv_records']
|
for x in zone['srv_records']
|
||||||
)
|
)
|
||||||
|
|
||||||
a_records = "\n".join(
|
a_records = "\n".join(
|
||||||
template_a.format(hostname=x['hostname'],
|
template_a.format(
|
||||||
ipv4=x['ipv4'])
|
hostname=x['hostname'],
|
||||||
|
ipv4=x['ipv4']
|
||||||
|
)
|
||||||
for x in zone['a_records']
|
for x in zone['a_records']
|
||||||
if (True if x['hostname'] + '.' + zone_name not in zone_names else add_record(x['hostname'] + '.' + zone_name, template_a.format(hostname='@', ipv4=ip['ipv4']), 'a'))
|
|
||||||
)
|
)
|
||||||
|
|
||||||
aaaa_records = "\n".join(
|
aaaa_records = "\n".join(
|
||||||
template_aaaa.format(hostname=x['hostname'],
|
template_aaaa.format(
|
||||||
ipv6=ip['ipv6'])
|
hostname=x['hostname'],
|
||||||
|
ipv6=ip['ipv6']
|
||||||
|
)
|
||||||
|
for x in zone['aaaa_records']
|
||||||
|
for ip in x['ipv6']
|
||||||
|
if x['ipv6'] is not None
|
||||||
|
)
|
||||||
|
|
||||||
|
aaaa_records = "\n".join(
|
||||||
|
template_aaaa.format(
|
||||||
|
hostname=x['hostname'],
|
||||||
|
ipv6=ip['ipv6']
|
||||||
|
)
|
||||||
for x in zone['aaaa_records']
|
for x in zone['aaaa_records']
|
||||||
for ip in x['ipv6']
|
for ip in x['ipv6']
|
||||||
if x['ipv6'] is not None
|
if x['ipv6'] is not None
|
||||||
and (True if x['hostname'] + '.' + zone_name not in zone_names else add_record(x['hostname'] + '.' + zone_name, template_aaaa.format(hostname='@', ipv6=ip['ipv6']), 'aaaa'))
|
|
||||||
)
|
)
|
||||||
|
|
||||||
cname_records = "\n".join(
|
cname_records = "\n".join(
|
||||||
template_cname.format(hostname=x['hostname'],
|
template_cname.format(
|
||||||
alias=x['alias'])
|
hostname=x['hostname'],
|
||||||
|
alias=x['alias']
|
||||||
|
)
|
||||||
for x in zone['cname_records']
|
for x in zone['cname_records']
|
||||||
if (True if x['hostname'] + '.' + zone_name not in zone_names else add_record(x['hostname'] + '.' + zone_name, template_cname.format(hostname='@', alias=x['alias']), 'cname'))
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if records_to_add[zone_name]['a']:
|
zone_file_content = template_zone.format(
|
||||||
a_records += "\n" + "\n".join(records_to_add[zone_name]['a'])
|
soa=soa,
|
||||||
|
|
||||||
if records_to_add[zone_name]['aaaa']:
|
|
||||||
aaaa_records += "\n" + "\n".join(records_to_add[zone_name]['aaaa'])
|
|
||||||
|
|
||||||
if records_to_add[zone_name]['cname']:
|
|
||||||
cname_records += "\n" + "\n".join(records_to_add[zone_name]['cname'])
|
|
||||||
|
|
||||||
zone_file_content = template_zone.format(soa=soa,
|
|
||||||
originv4=originv4,
|
originv4=originv4,
|
||||||
originv6=originv6,
|
originv6=originv6,
|
||||||
ns_records=ns_records,
|
ns_records=ns_records,
|
||||||
|
@ -193,7 +212,8 @@ def write_dns_files(api_client):
|
||||||
srv_records=srv_records,
|
srv_records=srv_records,
|
||||||
a_records=a_records,
|
a_records=a_records,
|
||||||
aaaa_records=aaaa_records,
|
aaaa_records=aaaa_records,
|
||||||
cname_records=cname_records)
|
cname_records=cname_records
|
||||||
|
)
|
||||||
|
|
||||||
filename = path+'/generated/dns.{zone}.zone'.format(zone=zone_name)
|
filename = path+'/generated/dns.{zone}.zone'.format(zone=zone_name)
|
||||||
with open(filename, 'w+') as f:
|
with open(filename, 'w+') as f:
|
||||||
|
@ -206,7 +226,6 @@ def get_ip_reverse(ip, prefix_length):
|
||||||
return '.'.join(ip.reverse_dns.split('.')[:prefix_length])
|
return '.'.join(ip.reverse_dns.split('.')[:prefix_length])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def write_dns_reverse_file(api_client):
|
def write_dns_reverse_file(api_client):
|
||||||
""" Generate the reverve file for each reverse zone (= IpType)
|
""" Generate the reverve file for each reverse zone (= IpType)
|
||||||
For each IpType, we generate both an Ipv4 reverse and a v6.
|
For each IpType, we generate both an Ipv4 reverse and a v6.
|
||||||
|
@ -237,12 +256,13 @@ def write_dns_reverse_file(api_client):
|
||||||
)
|
)
|
||||||
|
|
||||||
mx_records = "\n".join(
|
mx_records = "\n".join(
|
||||||
template_mx.format(priority=x['priority'],
|
template_mx.format(
|
||||||
target=x['target'])
|
priority=x['priority'],
|
||||||
|
target=x['target']
|
||||||
|
)
|
||||||
for x in zone['mx_records']
|
for x in zone['mx_records']
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
### We start with the v4
|
### We start with the v4
|
||||||
# We setup the network from the cidrs of the IpType
|
# We setup the network from the cidrs of the IpType
|
||||||
|
|
||||||
|
@ -275,29 +295,35 @@ def write_dns_reverse_file(api_client):
|
||||||
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,
|
mail=soa_mail,
|
||||||
serial=serial,
|
serial=serial,
|
||||||
ns=ns,
|
ns=ns,
|
||||||
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']
|
||||||
|
)
|
||||||
|
|
||||||
ptr_records = "\n".join(
|
ptr_records = "\n".join(
|
||||||
template_ptr.format(hostname=host['hostname']+extension,
|
template_ptr.format(
|
||||||
target=get_ip_reverse(host['ipv4'],prefix_length))
|
hostname=host['hostname']+extension,
|
||||||
|
target=get_ip_reverse(host['ipv4'],prefix_length)
|
||||||
|
)
|
||||||
for host in zone['ptr_records'] if host['ipv4'] in subnet
|
for host in zone['ptr_records'] if host['ipv4'] in subnet
|
||||||
)
|
)
|
||||||
zone_file_content = template_reverse.format(soa=soa,
|
zone_file_content = template_reverse.format(
|
||||||
|
soa=soa,
|
||||||
ns_records=ns_records,
|
ns_records=ns_records,
|
||||||
mx_records=mx_records,
|
mx_records=mx_records,
|
||||||
ptr_records = ptr_records)
|
ptr_records = ptr_records
|
||||||
|
)
|
||||||
|
|
||||||
filename = path+'/generated/dns.{zone}.zone'.format(zone=zone_name)
|
filename = path+'/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)
|
||||||
|
|
||||||
|
|
||||||
### Continue with the ipv6 reverse
|
### Continue with the ipv6 reverse
|
||||||
if zone['ptr_v6_records']:
|
if zone['ptr_v6_records']:
|
||||||
net = netaddr.IPNetwork(zone['prefix_v6']+"/"+str(zone['prefix_v6_length']))
|
net = netaddr.IPNetwork(zone['prefix_v6']+"/"+str(zone['prefix_v6_length']))
|
||||||
|
@ -307,14 +333,16 @@ def write_dns_reverse_file(api_client):
|
||||||
)[:-1]
|
)[:-1]
|
||||||
|
|
||||||
|
|
||||||
soa = template_soa.format(zone=zone6_name,
|
soa = template_soa.format(
|
||||||
|
zone=zone6_name,
|
||||||
mail=soa_mail,
|
mail=soa_mail,
|
||||||
serial=serial,
|
serial=serial,
|
||||||
ns=ns,
|
ns=ns,
|
||||||
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)
|
prefix_length = int((128 - net.prefixlen)/4)
|
||||||
ptr_records = "\n".join(
|
ptr_records = "\n".join(
|
||||||
|
@ -324,22 +352,24 @@ def write_dns_reverse_file(api_client):
|
||||||
)
|
)
|
||||||
if zone6_name in zone_v6:
|
if zone6_name in zone_v6:
|
||||||
# we already created the file, we ignore the soa
|
# we already created the file, we ignore the soa
|
||||||
zone_file_content = template_reverse.format(soa="",
|
zone_file_content = template_reverse.format(
|
||||||
|
soa="",
|
||||||
ns_records=ns_records,
|
ns_records=ns_records,
|
||||||
mx_records=mx_records,
|
mx_records=mx_records,
|
||||||
ptr_records = ptr_records)
|
ptr_records = ptr_records
|
||||||
|
)
|
||||||
|
|
||||||
filename = path+'/generated/dns.{zone}.zone'.format(zone=zone6_name)
|
filename = path+'/generated/dns.{zone}.zone'.format(zone=zone6_name)
|
||||||
with open(filename, 'a') as f:
|
with open(filename, 'a') as f:
|
||||||
f.write(zone_file_content)
|
f.write(zone_file_content)
|
||||||
else:
|
else:
|
||||||
# we create the file from scratch
|
# we create the file from scratch
|
||||||
zone_file_content = template_reverse.format(soa=soa,
|
zone_file_content = template_reverse.format(
|
||||||
|
soa=soa,
|
||||||
ns_records=ns_records,
|
ns_records=ns_records,
|
||||||
mx_records=mx_records,
|
mx_records=mx_records,
|
||||||
ptr_records = ptr_records)
|
ptr_records = ptr_records
|
||||||
|
)
|
||||||
|
|
||||||
filename = path+'/generated/dns.{zone}.zone'.format(zone=zone6_name)
|
filename = path+'/generated/dns.{zone}.zone'.format(zone=zone6_name)
|
||||||
with open(filename, 'w+') as f:
|
with open(filename, 'w+') as f:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue