bin/pvl.hosts-dns
changeset 331 9dda6a0e9826
parent 325 9be2b216e87c
child 335 77fb4019d4e0
equal deleted inserted replaced
330:3954e7b5ade1 331:9dda6a0e9826
    40             label = str(host)
    40             label = str(host)
    41         elif host.domain.endswith('.' + origin) :
    41         elif host.domain.endswith('.' + origin) :
    42             fqdn = pvl.dns.join(host, host.domain)
    42             fqdn = pvl.dns.join(host, host.domain)
    43             label = fqdn[:(len(fqdn) - len(origin) - 1)]
    43             label = fqdn[:(len(fqdn) - len(origin) - 1)]
    44         else :
    44         else :
    45             log.debug("%s: %s out of zone: %s", host, host.domain, origin)
    45             log.debug("%s: domain %s out of zone: %s", host, host.domain, origin)
       
    46             continue
       
    47         
       
    48         if host.forward is None  :
       
    49             pass
       
    50         elif host.forward :
       
    51             forward = pvl.dns.join(host, host.forward, host.domain)
       
    52 
       
    53             log.info("%s: forward: %s", host, forward)
       
    54 
       
    55             yield pvl.dns.zone.ZoneRecord.CNAME(label, forward)
       
    56             continue
       
    57         else :
       
    58             log.info("%s: skip forward", host)
    46             continue
    59             continue
    47 
    60 
    48         if host.ip :
    61         if host.ip :
    49             yield pvl.dns.zone.ZoneRecord.A(label, host.ip)
    62             yield pvl.dns.zone.ZoneRecord.A(label, host.ip)
    50         
    63         
   171 
   184 
   172         if ip not in prefix :
   185         if ip not in prefix :
   173             log.debug("%s: %s out of prefix: %s", host, ip, prefix)
   186             log.debug("%s: %s out of prefix: %s", host, ip, prefix)
   174             continue
   187             continue
   175         
   188         
   176         fqdn = host.fqdn()
   189         label = pvl.dns.zone.reverse_label(prefix, ip)
   177 
   190        
   178         log.debug("%s: ip=%s domain=%s: %s", host, ip, host.domain, fqdn)
   191         if host.reverse is None :
   179 
   192             fqdn = host.fqdn()
   180         yield ip, fqdn
   193 
       
   194             log.info("%s %s[%s]: PTR %s", host, prefix, ip, fqdn)
       
   195 
       
   196             yield host, ip, pvl.dns.zone.ZoneRecord.PTR(label, fqdn)
       
   197 
       
   198         elif host.reverse :
       
   199             alias = pvl.dns.join(label, host.reverse, host.domain)
       
   200             
       
   201             log.info("%s %s[%s]: CNAME %s", host, prefix, ip, alias)
       
   202 
       
   203             yield host, ip, pvl.dns.zone.ZoneRecord.CNAME(label, alias)
       
   204 
       
   205         else :
       
   206             log.info("%s %s[%s]: omit", host, prefix, ip)
       
   207             continue
       
   208 
   181  
   209  
   182 def process_hosts_reverse (options, hosts, prefix) :
   210 def process_hosts_reverse (options, hosts, prefix) :
   183     """
   211     """
   184         Generate DNS ZoneRecords within the given prefix's reverse-dns zone for hosts.
   212         Generate DNS ZoneRecords within the given prefix's reverse-dns zone for hosts.
   185     """
   213     """
   186     
   214     
   187     # collect data for records
   215     # collect data for records
   188     by_ip = dict()
   216     by_ip = dict()
   189     for ip, fqdn in process_hosts_ips(options, hosts, prefix) :
   217     for host, ip, rr in process_hosts_ips(options, hosts, prefix) :
   190         if ip in by_ip :
   218         if ip in by_ip :
   191             raise ValueError("%s: duplicate ip: %s: %s" % (fqdn, ip, by_ip[ip]))
   219             raise ValueError("%s: duplicate ip: %s: %s" % (host, ip, by_ip[ip]))
   192         else :
   220         else :
   193             by_ip[ip] = fqdn
   221             by_ip[ip] = rr
   194 
   222 
   195     if options.unknown_host :
   223     if options.unknown_host :
   196         # enumerate all of them
   224         # enumerate all of them
   197         iter_ips = prefix.iterhosts()
   225         iter_ips = prefix.iterhosts()
   198     else :
   226     else :
   199         iter_ips = sorted(by_ip)
   227         iter_ips = sorted(by_ip)
   200 
   228 
   201     for ip in iter_ips :
   229     for ip in iter_ips :
   202         if ip in by_ip :
   230         if ip in by_ip :
   203             fqdn = by_ip[ip]
   231             yield by_ip[ip]
   204         elif options.unknown_host :
   232         elif options.unknown_host :
       
   233             label = pvl.dns.zone.reverse_label(prefix, ip)
   205             fqdn = pvl.dns.zone.fqdn(options.unknown_host, options.hosts_domain)
   234             fqdn = pvl.dns.zone.fqdn(options.unknown_host, options.hosts_domain)
   206         else :
   235 
   207             fqdn = None
   236             log.info("%s %s[%s]: unused PTR %s", options.unknown_host, ip, prefix, fqdn)
   208         
   237 
   209         log.info("%s: %s", ip, fqdn)
   238             yield pvl.dns.zone.ZoneRecord.PTR(label, fqdn)
   210 
   239         else :
   211         if fqdn :
   240             continue
   212             # reverse against the reverse-dns zone origin
       
   213             yield pvl.dns.zone.ZoneRecord.PTR(pvl.dns.zone.reverse_label(prefix, ip), fqdn)
       
   214 
   241 
   215 def apply_zone (options, zone) :
   242 def apply_zone (options, zone) :
   216     """
   243     """
   217         Output given ZoneRecord's
   244         Output given ZoneRecord's
   218     """
   245     """