generator.py 2.56 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
from jinja2 import FileSystemLoader, Environment

# Represents the client key for updating a DNS record.
class Key:
    def __init__(self, name, algorithm, secret):
        self.name = name
        self.algorithm = algorithm
        self.secret = secret

# Represents a zone configuration in named.conf.
class Zone:
    def __init__(self, name, keys, zonefile):
        self.name = name
        self.keys = keys
        self.zonefile = zonefile

# Represents an update policy. Key is allowed to update domain.
class ZoneKey:
    def __init__(self, key, domain):
        self.key = key
        self.domain = domain

# Represents a master zone file.
class ZoneFile:
    def __init__(self, domain, primary_nameserver,
                 nameserver, hostmaster, recordlist):
        self.domain = domain
        self.primary_nameserver = primary_nameserver
        self.nameserver = nameserver
        self.hostmaster = hostmaster
        self.recordlist = recordlist

# Represents DNS records to put in the master zone file.
class Record:
    def __init__(self, subdomain, ttl, type, address):
        self.subdomain = subdomain
        self.ttl = ttl
        self.type = type
        self.address = address

if __name__ == '__main__':
    env = Environment(loader = FileSystemLoader('.'))

    key_template = env.get_template('key.template')
    key = Key('kempe.se', 'hmac-md5',
              'L+AwXRn7e+gVXDfsK9EelBrcKUCeygeKE3hywXHQzqvjh8zpXSVja+gp6X9iYgqODXgNYCvu2XExy4WZFlYzjw==')
    rendered_key = key_template.render(keylist = [ key ])
    
    zone_template = env.get_template('zone.template')
    zone_key = ZoneKey(key.name, 'test.kempe.se')
    zone_key1 = ZoneKey(key.name, 'test1.kempe.se')
    zone = Zone('kempe.se', [ zone_key, zone_key1 ], '/usr/local/etc/namedb/master/kempe.se')
    rendered_zone = zone_template.render(zonelist = [ zone ])

    zonefile_template = env.get_template('zonefile.template')
    record = Record('test', '300', 'A', '10.0.2.2')
    record1 = Record('test1', '3000', 'A', '10.2.2.2')
    zonefile = ZoneFile('kempe.se', 'dns.lysator.liu.se', 'dns.lysator.liu.se',
                        'hostmaster.kempe.se', [ record, record1 ])
    rendered_zonefile = zonefile_template.render(domain = zonefile.domain,
                            primary_nameserver = zonefile.primary_nameserver,
                            nameserver = zonefile.nameserver,
                            hostmaster = zonefile.hostmaster,
                            recordlist = zonefile.recordlist)

    print(rendered_key)
    print(rendered_zone)
    print(rendered_zonefile)