bin/pvl.hosts-dns
changeset 272 5755f9c54135
parent 270 bafcda3a3c0d
child 273 c8deaa9a2746
equal deleted inserted replaced
271:4dfa1a939153 272:5755f9c54135
    21             yield pvl.dns.zone.ZoneRecord.A(host, host.ip)
    21             yield pvl.dns.zone.ZoneRecord.A(host, host.ip)
    22         
    22         
    23         for alias in host.alias :
    23         for alias in host.alias :
    24             yield pvl.dns.zone.ZoneRecord.CNAME(alias, host)
    24             yield pvl.dns.zone.ZoneRecord.CNAME(alias, host)
    25 
    25 
    26 def process_hosts_reverse (options, hosts, prefix) :
    26 def process_hosts_ips (options, hosts, prefix) :
    27     """
    27     """
    28         Generate DNS ZoneRecords within the given prefix's reverse-dns zone for hosts.
    28         Yield (ip, fqnd) for hosts within given prefix.
    29     """
    29     """
    30 
    30 
    31     for host in hosts :
    31     for host in hosts :
    32         if not host.ip :
    32         if not host.ip :
    33             continue
    33             continue
    34 
    34 
    35         if host.ip not in prefix :
    35         if host.ip not in prefix :
    36             continue
    36             continue
       
    37 
       
    38         yield host.ip, pvl.dns.zone.fqdn(host, host.domain)
       
    39  
       
    40 def process_hosts_reverse (options, hosts, prefix) :
       
    41     """
       
    42         Generate DNS ZoneRecords within the given prefix's reverse-dns zone for hosts.
       
    43     """
       
    44     
       
    45     # collect data for records
       
    46     by_ip = dict()
       
    47     for ip, fqdn in process_hosts_ips(options, hosts, prefix) :
       
    48         if ip in by_ip :
       
    49             raise ValueError("%s: duplicate ip: %s: %s" % (fqdn, ip, by_ip[ip]))
       
    50         else :
       
    51             by_ip[ip] = fqdn
       
    52 
       
    53     for ip in prefix.iterhosts() :
       
    54         if ip in by_ip :
       
    55             fqdn = by_ip[ip]
       
    56         elif options.unknown_host :
       
    57             fqdn = pvl.dns.zone.fqdn(options.unknown_host, options.hosts_domain)
       
    58         else :
       
    59             fqdn = None
    37         
    60         
    38         # reverse against the reverse-dns zone origin
    61         if fqdn :
    39         yield pvl.dns.zone.ZoneRecord.PTR(
    62             # reverse against the reverse-dns zone origin
    40                 pvl.dns.zone.reverse_label(prefix, host.ip),
    63             yield pvl.dns.zone.ZoneRecord.PTR(pvl.dns.zone.reverse_label(prefix, ip), fqdn)
    41                 pvl.dns.zone.fqdn(host, host.domain)
       
    42         )
       
    43 
    64 
    44 def apply_zone (options, zone) :
    65 def apply_zone (options, zone) :
    45     """
    66     """
    46         Output given ZoneRecord's
    67         Output given ZoneRecord's
    47     """
    68     """
    62             help="Generate forward zone for domain")
    83             help="Generate forward zone for domain")
    63 
    84 
    64     parser.add_option('--reverse-zone',         metavar='PREFIX',
    85     parser.add_option('--reverse-zone',         metavar='PREFIX',
    65             help="Generate reverse zone for prefx")
    86             help="Generate reverse zone for prefx")
    66 
    87 
       
    88     parser.add_option('--unknown-host',         metavar='NAME',
       
    89             help="Generate records for unused IPs")
       
    90 
    67     options, args = parser.parse_args(argv[1:])
    91     options, args = parser.parse_args(argv[1:])
    68     pvl.args.apply(options)
    92     pvl.args.apply(options)
    69 
    93 
    70     # input
    94     # input
    71     hosts = pvl.hosts.apply(options, args)
    95     hosts = pvl.hosts.apply(options, args)