Commit 668021a1 authored by aoh's avatar aoh
Browse files

generator.py: Use database



Make use of sql database in generator.py.
Keys, Zones and Zonefile.
Signed-off-by: aoh's avataraoh <aoh@lysator.liu.se>
parent 447da53a
......@@ -42,3 +42,11 @@ class DB():
return None
else:
return secret.get("secret")
def get_record(self, subid):
with self.connection.cursor() as cursor:
cursor.execute('SELECT subdomain.name, subdomain.ttl, record_types.name '
'FROM subdomain '
'INNER JOIN record_types ON subdomain.rr = record_types.id '
'WHERE subdomain.id = %s LIMIT 1', subid)
return cursor.fetchone()
#!/usr/bin/env python3
from jinja2 import FileSystemLoader, Environment
from db import DB
# Represents the client key for updating a DNS record.
class Key:
......@@ -42,30 +43,60 @@ class Record:
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 ])
# Keys
with DB() as d:
for id in d.get_domain_ids():
domain_name = d.get_domain_name(id)
for sid in d.get_subdomain_ids(id):
key_template = env.get_template('key.template')
subdomain_name = d.get_subdomain_name(sid)
fqdn = subdomain_name + '.' + domain_name
key = Key(fqdn, 'hmac-md5', d.get_key(sid))
rendered_key = key_template.render(keylist = [ key ])
print(rendered_key)
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)
# Zones
zones = list()
with DB() as d:
for id in d.get_domain_ids():
domain_name = d.get_domain_name(id)
zone_template = env.get_template('zone.template')
zone_keys = list()
for sid in d.get_subdomain_ids(id):
subdomain_name = d.get_subdomain_name(sid)
fqdn = subdomain_name + '.' + domain_name
key = Key(fqdn, 'hmac-md5', d.get_key(sid))
zone_key = ZoneKey(key.name, fqdn)
zone_keys.append(zone_key)
zone = Zone(domain_name,
zone_keys,
'/usr/local/etc/namedb/master/' + domain_name)
zones.append(zone)
rendered_zone = zone_template.render(zonelist = zones)
print(rendered_zone)
print(rendered_zonefile)
# Zonefile
ip = '10.0.2.2' # TODO obtain correct ip
with DB() as d:
for id in d.get_domain_ids():
domain_name = d.get_domain_name(id)
zonefile_template = env.get_template('zonefile.template')
records = list()
for sid in d.get_subdomain_ids(id):
rr = d.get_record(sid)
record = Record(rr.get('name'),
rr.get('ttl'),
rr.get('record_types.name'),
ip)
records.append(record)
zonefile = ZoneFile(domain_name,
'dns.lysator.liu.se',
'dns.lysator.liu.se',
'hostmaster.' + domain_name,
records)
rendered_zonefile = zonefile_template.render(domain = zonefile.domain,
primary_nameserver = zonefile.primary_nameserver,
nameserver = zonefile.nameserver,
hostmaster = zonefile.hostmaster,
recordlist = zonefile.recordlist)
print(rendered_zonefile)
......@@ -16,7 +16,7 @@ try:
print(cursor.fetchone())
print("Record types")
cursor.execute('SELECT id, name FROM domain')
cursor.execute('SELECT id, name FROM record_types')
print(cursor.fetchone())
print("Subdomain")
......@@ -40,3 +40,4 @@ with DB() as d:
print("subdomain id: ", sid)
print("domain name: ", d.get_subdomain_name(sid))
print("subdomain secret: ", d.get_key(sid))
print("record: ", d.get_record(sid))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment