pvl.hosts-dns: handle cross-domain aliases, within origin
authorTero Marttila <terom@paivola.fi>
Wed, 18 Dec 2013 23:22:02 +0200
changeset 310 eb1829893317
parent 309 2b8ba955afac
child 311 0e79917de9f7
pvl.hosts-dns: handle cross-domain aliases, within origin
bin/pvl.hosts-dns
--- a/bin/pvl.hosts-dns	Wed Dec 18 23:21:29 2013 +0200
+++ b/bin/pvl.hosts-dns	Wed Dec 18 23:22:02 2013 +0200
@@ -9,44 +9,64 @@
 import logging; log = logging.getLogger('pvl.hosts-dns')
 import optparse
 
-def process_hosts_names (options, hosts, domain) :
+def process_hosts_alias (options, origin, host_domain, alias, host) :
     """
-        Yield ZoneRecords for the given domain's hosts.
+        Resolve alias@domain within given zone.
+    """
+
+    if '.' in alias :
+        pass
+    elif host_domain :
+        alias = pvl.dns.join(alias, host_domain)
+    else :
+        raise ValueError("no domain given for alias: %s / %s" % (alias, ))
+
+    if alias.endswith('.' + origin) :
+        # strip
+        alias = alias[:(len(alias) - len(origin) - 1)]
+    else:
+        raise ValueError("alias domain outside of origin: %s / %s" % (alias, origin))
+
+    return pvl.dns.zone.ZoneRecord.CNAME(alias, host)
+
+def process_hosts_names (options, hosts, origin) :
+    """
+        Yield ZoneRecords for hosts within the given zone.
     """
 
     for host in hosts :
-        if not options.forward_zone :
-            pass
-        elif host.domain == domain :
-            pass
-        elif pvl.dns.zone.join('*', host.domain) == domain :
-            pass
-        elif fnmatch.fnmatch(host.domain, domain) :
-            pass
+        # determine label within zone
+        if not origin :
+            label = pvl.dns.join(host, host.domain)
+        elif host.domain == origin :
+            label = str(host)
+        elif host.domain.endswith('.' + origin) :
+            fqdn = pvl.dns.join(host, host.domain)
+            label = fqdn[:(len(fqdn) - len(origin) - 1)]
         else :
-            log.debug("%s: %s out of domain: %s", host, host.domain, domain)
+            log.debug("%s: %s out of zone: %s", host, host.domain, origin)
             continue
-        
+
         if host.ip :
-            yield pvl.dns.zone.ZoneRecord.A(host, host.ip)
+            yield pvl.dns.zone.ZoneRecord.A(label, host.ip)
         
         if host.alias4 :
-            yield pvl.dns.zone.ZoneRecord.A(host.ALIAS4_FMT.format(host=host), host.ip)
+            yield pvl.dns.zone.ZoneRecord.A(host.ALIAS4_FMT.format(host=label), host.ip)
 
         if host.ip6 :
-            yield pvl.dns.zone.ZoneRecord.AAAA(host, host.ip6)
+            yield pvl.dns.zone.ZoneRecord.AAAA(label, host.ip6)
 
         if host.alias6 :
-            yield pvl.dns.zone.ZoneRecord.AAAA(host.ALIAS6_FMT.format(host=host), host.ip6)
+            yield pvl.dns.zone.ZoneRecord.AAAA(label.ALIAS6_FMT.format(host=host), host.ip6)
 
         for alias in host.alias :
-            yield pvl.dns.zone.ZoneRecord.CNAME(alias, host)
+            yield process_hosts_alias(options, origin, host.domain, alias, label)
 
         for alias4 in host.alias4 :
-            yield pvl.dns.zone.ZoneRecord.CNAME(alias4, host.ALIAS4_FMT.format(host=host))
+            yield process_hosts_alias(options, origin, host.domain, alias4, host.ALIAS4_FMT.format(host=label))
 
         for alias6 in host.alias6 :
-            yield pvl.dns.zone.ZoneRecord.CNAME(alias6, host.ALIAS6_FMT.format(host=host))
+            yield process_hosts_alias(options, origin, host.domain,  alias6, host.ALIAS6_FMT.format(host=label))
 
 def process_hosts_forward (options, hosts, domain) :
     """