main.py 5.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
from configparser import ConfigParser
import socket
import datetime

from re2oapi import Re2oAPIClient

config = ConfigParser()
config.read('config.ini')

api_hostname = config.get('Re2o', 'hostname')
api_password = config.get('Re2o', 'password')
api_username = config.get('Re2o', 'username')

template_soa = ("{zone} IN SOA ns.{zone}. {mail} (\n"
                "    {serial} ; serial\n"
                "    {refresh} ; refresh\n"
                "    {retry} ; retry\n"
                "    {expire} ; expire\n"
                "    {ttl} ; ttl\n"
                ")")
template_originv4 = "@ IN A {ipv4}"
template_originv6 = "@ IN AAAA {ipv6}"
template_ns = "@ IN NS {target}"
template_mx = "@ IN MX {priority} {target}"
template_txt = "{field1} IN TXT {field2}"
26
template_srv = "_{service}._{protocole}.{zone} {ttl} IN SRV {priority} {weight} {port} {target}"
27 28
template_a = "{hostname} IN A {ipv4}"
template_aaaa = "{hostname} IN AAAA {ipv6}"
Maël Kervella's avatar
Maël Kervella committed
29 30
template_cname = "{hostname} IN CNAME {alias}"
template_ptr = "{target} IN PTR {hostname}"
31

Maël Kervella's avatar
Maël Kervella committed
32 33
template_zone = ("$TTL 2D\n"
                 "{soa}\n"
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
                 "\n"
                 "{originv4}\n"
                 "{originv6}\n"
                 "\n"
                 "{ns_records}\n"
                 "\n"
                 "{mx_records}\n"
                 "\n"
                 "{txt_records}\n"
                 "\n"
                 "{srv_records}\n"
                 "\n"
                 "{a_records}\n"
                 "\n"
                 "{aaaa_records}\n"
                 "\n"
                 "{cname_records}")

Maël Kervella's avatar
Maël Kervella committed
52 53 54 55 56 57
template_reverse = ("$TTL 2D\n"
                    "{soa}\n"
                    "\n"
                    "{ns_records}\n"
                    "\n"
                    "{ptr_records}\n")
58

Maël Kervella's avatar
Maël Kervella committed
59
def write_dns_files(api_client):
60 61 62 63 64
    for zone in api_client.list_dnszones():
        zone_name = zone['name'][1:]

        now = datetime.datetime.now(datetime.timezone.utc)
        serial = now.strftime("%Y%m%d") + str(int(100*(now.hour*3600 + now.minute*60 + now.second)/86400))
65

66 67 68
        soa_mail_fields = zone['soa']['mail'].split('@')
        soa_mail = "{}.{}.".format(soa_mail_fields[0].replace('.', '\\.'),
                                   soa_mail_fields[1])
69

70 71 72 73 74 75 76
        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'])
77

78 79 80 81 82
        originv4 = template_originv4.format(ipv4=zone['originv4']['ipv4'])
        if zone['originv6'] is not None:
            originv6 = template_originv6.format(ipv6=zone['originv6'])
        else:
            originv6 = ""
83

84 85 86 87
        ns_records = "\n".join(
            template_ns.format(target=x['target'])
            for x in zone['ns_records']
        )
88

89 90 91 92 93
        mx_records = "\n".join(
            template_mx.format(priority=x['priority'],
                               target=x['target'])
            for x in zone['mx_records']
        )
94

95 96 97 98 99
        txt_records = "\n".join(
            template_txt.format(field1=x['field1'],
                                field2=x['field2'])
            for x in zone['txt_records']
        )
100

101 102
        srv_records = "\n".join(
            template_srv.format(service=x['service'],
103
                                protocole=x['protocole'],
104 105 106 107 108 109 110 111
                                zone=zone_name,
                                ttl=x['ttl'],
                                priority=x['priority'],
                                weight=x['weight'],
                                port=x['port'],
                                target=x['target'])
            for x in zone['srv_records']
        )
Maël Kervella's avatar
Maël Kervella committed
112

113 114 115 116 117
        a_records = "\n".join(
            template_a.format(hostname=x['hostname'],
                              ipv4=x['ipv4'])
            for x in zone['a_records']
        )
Maël Kervella's avatar
Maël Kervella committed
118

119 120 121 122 123
        aaaa_records = "\n".join(
            template_aaaa.format(hostname=x['hostname'],
                                 ipv6=x['ipv6'])
            for x in zone['aaaa_records'] if x['ipv6'] is not None
        )
124

125 126
        cname_records = "\n".join(
            template_cname.format(hostname=x['hostname'],
Maël Kervella's avatar
Maël Kervella committed
127
                                  alias=x['alias']+extension=x['extension'])
128 129
            for x in zone['cname_records']
        )
130

131 132 133 134 135 136 137 138 139 140 141 142 143 144
        zone_file_content = template_zone.format(soa=soa,
                                                 originv4=originv4,
                                                 originv6=originv6,
                                                 ns_records=ns_records,
                                                 mx_records=mx_records,
                                                 txt_records=txt_records,
                                                 srv_records=srv_records,
                                                 a_records=a_records,
                                                 aaaa_records=aaaa_records,
                                                 cname_records=cname_records)

        filename = 'dns.{zone}.zone'.format(zone=zone_name)
        with open(filename, 'w+') as f:
            f.write(zone_file_content)
Maël Kervella's avatar
Maël Kervella committed
145 146 147 148


def write_dns_reverse_file(api_client):
    pass
149

150 151 152 153 154 155 156 157 158 159


api_client = Re2oAPIClient(api_hostname, api_username, api_password)

client_hostname = socket.gethostname().split('.', 1)[0]

for service in api_client.list_servicesregen():
#    if service['hostname'] == client_hostname and \
#            service['service_name'] == 'dns' and \
#            service['need_regen']:
Maël Kervella's avatar
Maël Kervella committed
160 161
    write_dns_zone_file(api_client)
    write_dns_reverse_file(api_client)
162
#        api_client.patch(service['api_url'], data={'need_regen': False})