Commit ae271078 authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Add TTL support for individual named/BIND records

parent 8867ff1b
......@@ -139,22 +139,24 @@ class DomainDict:
self.host = {}
pass
def add_host(self, name, kind, value):
def add_host(self, name, ttl, kind, value):
if not re.match('^[0-9a-zA-Z.-]+$', name):
raise Exception('Invalid host name "%s"' % name)
if not name in self.host:
self.host[name] = set()
pass
self.host[name].add((kind, value))
if not ttl:
ttl = ''
self.host[name].add((kind, value, ttl))
pass
def value(self, cmp=None):
result = util.StringArray()
result += self.header
for name in sorted(self.host, cmp):
for kind, value in sorted(self.host[name]):
result += ('%(name)-18s IN %(kind)-7s %(value)s' %
dict(name=name, kind=kind, value=value))
for kind,value,ttl in sorted(self.host[name]):
result += ('%(name)-18s %(ttl)-8s IN %(kind)-7s %(value)s' %
dict(name=name, ttl=ttl, kind=kind, value=value))
pass
pass
return result
......@@ -185,8 +187,9 @@ def generate_forward(tree, hosts):
for mx in [ m for m in tree._host_._interface_._mailhost_
if m.domain[0] == domain]:
pri = int(mx.priority[0] or 0)
result += (' IN MX %d %s' %
(pri, util.fqn(tree, mx._parent)))
ttl = mx.ttl[0] or ''
result += (' %-8s IN MX %d %s' %
(ttl, pri, util.fqn(tree, mx._parent)))
pass
for txt in [ t for t in tree._subnet_._txt_ if t.domain[1] == domain]:
result += (' IN TXT "%s"' % (txt.value[0]))
......@@ -200,31 +203,31 @@ def generate_forward(tree, hosts):
# Add cname hosts
for c in tree._subnet_._cname_:
result[c.domain[1]].add_host(c.alias[0], 'CNAME', c.name[0])
result[c.domain[1]].add_host(c.alias[0], c.ttl[0], 'CNAME', c.name[0])
pass
# Add numbered hosts
def add_host(domain, name, address):
def add_host(domain, name, ttl, address):
if address.version == 4:
result[domain].add_host(name, 'A', str(address.exploded))
result[domain].add_host(name, ttl, 'A', str(address.exploded))
pass
elif address.version == 6:
result[domain].add_host(name, 'AAAA', str(address.exploded))
result[domain].add_host(name, ttl, 'AAAA', str(address.exploded))
pass
for domain,net in [ (s.domain[0],util.network(s))
for s in tree._subnet_
if s.domain[0] and util.network(s)]:
for name,address in hosts:
for name,address,ttl in hosts:
try:
if name.endswith('.'):
d = '.'.join(name.split('.')[1:-1])
n = name.split('.')[0]
add_host(d, n, address)
add_host(d, n, ttl, address)
continue
except Exception,e:
pass
if address in net:
add_host(domain, name,address)
add_host(domain, name, ttl, address)
pass
pass
pass
......@@ -247,14 +250,14 @@ def generate_reverse(tree, hosts):
for net in net_to_origin:
origin = net_to_origin[net]
domain = origin_to_domain[origin]
for name,address in hosts:
for name,address,ttl in hosts:
if address in net:
reverse = reverse_addr(address).replace('.%s' % origin, '')
fqn = name
if fqn[-1] != '.':
fqn += '.' + domain + '.'
pass
result[origin].add_host(reverse, 'PTR', fqn)
result[origin].add_host(reverse, ttl, 'PTR', fqn)
pass
pass
pass
......@@ -267,7 +270,7 @@ def generate_reverse(tree, hosts):
def get_hosts(tree, with_alias=True):
result = []
seen = {}
def add (name, address, check=None):
def add (name, address, ttl, check=None):
if check and address in seen:
old_name = seen[address][0]
old_check = seen[address][1]
......@@ -277,14 +280,14 @@ def get_hosts(tree, with_alias=True):
where=[old_check, check])
pass
seen[address] = (name, check)
result.append((name,address))
result.append((name,address,ttl))
pass
# IPv4 static addresses
for i in filter(util.address, tree._host_._interface_._ip_):
add(i.name[0:], util.address(i), check=i)
add(i.name[0:], util.address(i), i.ttl[0:2], check=i)
if with_alias:
for a in i._alias_:
add(a.name[0:], util.address(i))
add(a.name[0:], util.address(i), a.ttl[0:3])
pass
pass
pass
......@@ -295,17 +298,17 @@ def get_hosts(tree, with_alias=True):
a = util.address(d.first[0])
while a <= last:
name = '-'.join([ 'dynamic' ] + a.exploded.split('.'))
add(name, a, check=d)
add(name, a, d.ttl[0], check=d)
a = a + 1
pass
pass
# IPv6 static addresses
for i in filter(util.address, tree._host_._interface_._ipv6_):
add(i.name[0:], util.address(i), check=i)
add(i.name[0:], util.address(i), i.ttl[0:2], check=i)
if with_alias:
for a in i._alias_:
add(a.name[0:], util.address(i))
add(a.name[0:], util.address(i), a.ttl[0:3])
pass
pass
pass
......@@ -316,7 +319,7 @@ def get_hosts(tree, with_alias=True):
a = util.address(d.first[0])
while a <= last:
name = '-'.join([ 'dynamic' ] + a.exploded.split(':'))
add(name, a, check=d)
add(name, a, d.ttl[0], check=d)
a = a + 1
pass
pass
......
Supports Markdown
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