pvl/hosts/zone.py
author Tero Marttila <tero.marttila@aalto.fi>
Wed, 25 Feb 2015 15:55:23 +0200
changeset 475 a76571e27c6f
parent 474 51983fcda6b1
child 487 920394061b6f
permissions -rw-r--r--
pvl.dns.zone: move script main()'s back out to bin/
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     1
"""
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     2
    Generate zonefile records from hosts
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     3
"""
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     4
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     5
import ipaddr
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     6
import logging; log = logging.getLogger('pvl.hosts.zone')
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     7
import pvl.dns
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     8
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     9
class HostZoneError(Exception):
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    10
    pass
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    11
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    12
def resolve (origin, domain, name) :
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    13
    """
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    14
        Resolve relative CNAME for label@origin -> alias@domain
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    15
    """
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    16
462
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    17
    if origin:
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    18
        origin = pvl.dns.fqdn(origin)
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    19
    
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    20
    if domain:
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    21
        fqdn = pvl.dns.fqdn(name, domain)
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    22
    else:
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    23
        fqdn = pvl.dns.fqdn(name)
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    24
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    25
    if not origin:
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    26
        return fqdn
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    27
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    28
    elif domain == origin:
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    29
         return name
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    30
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    31
    elif fqdn.endswith('.' + origin):
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    32
        return pvl.dns.relative(origin, fqdn)
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    33
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    34
    elif domain:
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    35
        raise HostZoneError("{name}: domain {domain} out of zone {origin}".format(name=name, domain=domain, origin=origin))
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    36
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    37
    else:
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    38
        raise HostZoneError("{name}: fqdn {fqdn} out of zone {origin}".format(name=name, fqdn=fqdn, origin=origin))
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    39
471
e4b4458d8061 pvl.hosts: test location domains
Tero Marttila <tero.marttila@aalto.fi>
parents: 470
diff changeset
    40
# TODO: generate location alias CNAMEs even if host itself is outside origin?
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    41
def host_forward (host, origin) :
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    42
    """
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    43
        Yield ZoneRecords for hosts within the given zone origin
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    44
    """
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    45
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    46
    try:
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    47
        label = resolve(origin, host.domain, host.name)
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    48
    except HostZoneError as error:
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    49
        log.info("%s: skip: %s", host, error)
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    50
        return
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    51
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    52
    if host.forward:
463
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
    53
        forward = pvl.dns.fqdn(host.forward)
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    54
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    55
        log.info("%s: forward: %s", host, forward)
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    56
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    57
        yield pvl.dns.ZoneRecord.CNAME(label, forward)
467
3bb00e5e79d3 pvl.hosts.zone: support combining forward= with alias=
Tero Marttila <tero.marttila@aalto.fi>
parents: 466
diff changeset
    58
    
3bb00e5e79d3 pvl.hosts.zone: support combining forward= with alias=
Tero Marttila <tero.marttila@aalto.fi>
parents: 466
diff changeset
    59
    elif host.forward is None:
3bb00e5e79d3 pvl.hosts.zone: support combining forward= with alias=
Tero Marttila <tero.marttila@aalto.fi>
parents: 466
diff changeset
    60
        # forward
3bb00e5e79d3 pvl.hosts.zone: support combining forward= with alias=
Tero Marttila <tero.marttila@aalto.fi>
parents: 466
diff changeset
    61
        if host.ip :
3bb00e5e79d3 pvl.hosts.zone: support combining forward= with alias=
Tero Marttila <tero.marttila@aalto.fi>
parents: 466
diff changeset
    62
            log.info("%s: forward %s[%s]: A %s", host, origin, label, host.ip)
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    63
467
3bb00e5e79d3 pvl.hosts.zone: support combining forward= with alias=
Tero Marttila <tero.marttila@aalto.fi>
parents: 466
diff changeset
    64
            yield pvl.dns.ZoneRecord.A(label, host.ip)
3bb00e5e79d3 pvl.hosts.zone: support combining forward= with alias=
Tero Marttila <tero.marttila@aalto.fi>
parents: 466
diff changeset
    65
3bb00e5e79d3 pvl.hosts.zone: support combining forward= with alias=
Tero Marttila <tero.marttila@aalto.fi>
parents: 466
diff changeset
    66
        if host.ip6 :
3bb00e5e79d3 pvl.hosts.zone: support combining forward= with alias=
Tero Marttila <tero.marttila@aalto.fi>
parents: 466
diff changeset
    67
            log.info("%s: forward %s[%s]: AAAA %s", host, origin, label, host.ip6)
3bb00e5e79d3 pvl.hosts.zone: support combining forward= with alias=
Tero Marttila <tero.marttila@aalto.fi>
parents: 466
diff changeset
    68
3bb00e5e79d3 pvl.hosts.zone: support combining forward= with alias=
Tero Marttila <tero.marttila@aalto.fi>
parents: 466
diff changeset
    69
            yield pvl.dns.ZoneRecord.AAAA(label, host.ip6)
3bb00e5e79d3 pvl.hosts.zone: support combining forward= with alias=
Tero Marttila <tero.marttila@aalto.fi>
parents: 466
diff changeset
    70
3bb00e5e79d3 pvl.hosts.zone: support combining forward= with alias=
Tero Marttila <tero.marttila@aalto.fi>
parents: 466
diff changeset
    71
    else:
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    72
        log.info("%s: skip forward", host)
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    73
        return
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    74
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    75
    if host.location:
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    76
        location_alias, location_domain = host.location
471
e4b4458d8061 pvl.hosts: test location domains
Tero Marttila <tero.marttila@aalto.fi>
parents: 470
diff changeset
    77
        
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    78
        yield pvl.dns.ZoneRecord.CNAME(resolve(origin, location_domain, location_alias), label)
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    79
466
ad9d512ec1e7 pvl.hosts.zone: test and fix alias=
Tero Marttila <tero.marttila@aalto.fi>
parents: 464
diff changeset
    80
    for alias in host.alias:
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    81
        yield pvl.dns.ZoneRecord.CNAME(resolve(origin, host.domain, alias), label)
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    82
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    83
    for alias in host.alias4:
468
3e7cb8dd5708 pvl.hosts.zone: raise HostZoneError on alias4/alias6 without ip/ip6
Tero Marttila <tero.marttila@aalto.fi>
parents: 467
diff changeset
    84
        if not host.ip:
3e7cb8dd5708 pvl.hosts.zone: raise HostZoneError on alias4/alias6 without ip/ip6
Tero Marttila <tero.marttila@aalto.fi>
parents: 467
diff changeset
    85
            raise HostZoneError(host, "alias4={host.alias4} without ip=".format(host=host))
3e7cb8dd5708 pvl.hosts.zone: raise HostZoneError on alias4/alias6 without ip/ip6
Tero Marttila <tero.marttila@aalto.fi>
parents: 467
diff changeset
    86
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    87
        yield pvl.dns.ZoneRecord.A(resolve(origin, host.domain, alias), host.ip)
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    88
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    89
    for alias in host.alias6:
468
3e7cb8dd5708 pvl.hosts.zone: raise HostZoneError on alias4/alias6 without ip/ip6
Tero Marttila <tero.marttila@aalto.fi>
parents: 467
diff changeset
    90
        if not host.ip6:
3e7cb8dd5708 pvl.hosts.zone: raise HostZoneError on alias4/alias6 without ip/ip6
Tero Marttila <tero.marttila@aalto.fi>
parents: 467
diff changeset
    91
            raise HostZoneError(host, "alias6={host.alias6} without ip6=".format(host=host))
3e7cb8dd5708 pvl.hosts.zone: raise HostZoneError on alias4/alias6 without ip/ip6
Tero Marttila <tero.marttila@aalto.fi>
parents: 467
diff changeset
    92
3e7cb8dd5708 pvl.hosts.zone: raise HostZoneError on alias4/alias6 without ip/ip6
Tero Marttila <tero.marttila@aalto.fi>
parents: 467
diff changeset
    93
        yield pvl.dns.ZoneRecord.AAAA(resolve(origin, host.domain, alias), host.ip6)
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    94
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    95
def host_reverse (host, prefix) :
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    96
    """
464
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
    97
        Yield (ipaddr.IPAddress, ZoneRecord) tuples for host within given prefix's reverse-dns zone.
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    98
    """
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    99
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   100
    if prefix.version == 4 :
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   101
        ip = host.ip
464
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   102
        
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   103
        # reverse= is IPv4-only
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   104
        reverse = host.reverse
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   105
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   106
    elif prefix.version == 6 :
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   107
        ip = host.ip6
464
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   108
        
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   109
        # if reverse= is set, always omit, for lack of reverse6=
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   110
        reverse = None if host.reverse is None else False
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   111
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   112
    else :
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   113
        raise ValueError("%s: unknown ip version: %s" % (prefix, prefix.version))
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   114
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   115
    if not ip :
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   116
        log.debug("%s: no ip%d", host, prefix.version)
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   117
        return
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   118
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   119
    if ip not in prefix :
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   120
        log.debug("%s: %s out of prefix: %s", host, ip, prefix)
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   121
        return
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   122
    
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   123
    # relative label
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   124
    label = pvl.dns.reverse_label(prefix, ip)
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   125
   
464
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   126
    if reverse:
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   127
        alias = pvl.dns.fqdn(reverse)
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   128
        
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   129
        log.info("%s %s[%s]: CNAME %s", host, prefix, ip, alias)
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   130
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   131
        yield ip, pvl.dns.zone.ZoneRecord.CNAME(label, alias)
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   132
464
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   133
    elif reverse is None :
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   134
        fqdn = host.fqdn()
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   135
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   136
        log.info("%s %s[%s]: PTR %s", host, prefix, ip, fqdn)
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   137
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   138
        yield ip, pvl.dns.zone.ZoneRecord.PTR(label, fqdn)
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   139
464
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   140
    else:
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   141
        log.info("%s %s[%s]: omit", host, prefix, ip)
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   142
 
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   143
def apply_hosts_forward (options, hosts, origin) :
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   144
    """
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   145
        Generate DNS ZoneRecords for for hosts within the given zone origin.
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   146
472
814cc88c531b pvl.hosts.zone: stricter name/type + name/CNAME -conflict logic
Tero Marttila <tero.marttila@aalto.fi>
parents: 471
diff changeset
   147
        Verifies that there are no overlapping name/type records, or CNAME records from aliases.
814cc88c531b pvl.hosts.zone: stricter name/type + name/CNAME -conflict logic
Tero Marttila <tero.marttila@aalto.fi>
parents: 471
diff changeset
   148
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   149
        Yields ZoneRecords in Host-order
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   150
    """
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   151
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   152
    if options.add_origin :
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   153
        yield pvl.dns.ZoneDirective.build(None, 'ORIGIN', origin)
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   154
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   155
    by_name = dict()
472
814cc88c531b pvl.hosts.zone: stricter name/type + name/CNAME -conflict logic
Tero Marttila <tero.marttila@aalto.fi>
parents: 471
diff changeset
   156
    by_name_type = dict()
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   157
    
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   158
    for host in hosts:
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   159
        if not host.domain:
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   160
            log.debug("%s: skip without domain", host)
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   161
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   162
        for rr in host_forward(host, origin) :
472
814cc88c531b pvl.hosts.zone: stricter name/type + name/CNAME -conflict logic
Tero Marttila <tero.marttila@aalto.fi>
parents: 471
diff changeset
   163
            if (rr.name, 'CNAME') in by_name_type:
814cc88c531b pvl.hosts.zone: stricter name/type + name/CNAME -conflict logic
Tero Marttila <tero.marttila@aalto.fi>
parents: 471
diff changeset
   164
                raise HostZoneError(host, "{host}: CNAME {cname} conflict: {rr}".format(host=host, cname=by_name_type[rr.name, 'CNAME'].name, rr=rr))
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   165
            elif rr.type == 'CNAME' and rr.name in by_name:
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   166
                raise HostZoneError(host, "{host}: CNAME {cname} conflict: {rr}".format(host=host, cname=rr.name, rr=by_name[rr.name]))
472
814cc88c531b pvl.hosts.zone: stricter name/type + name/CNAME -conflict logic
Tero Marttila <tero.marttila@aalto.fi>
parents: 471
diff changeset
   167
            elif (rr.name, rr.type) in by_name_type:
814cc88c531b pvl.hosts.zone: stricter name/type + name/CNAME -conflict logic
Tero Marttila <tero.marttila@aalto.fi>
parents: 471
diff changeset
   168
                raise HostZoneError(host, "{host}: {type} {name} conflict: {rr}".format(host=host, type=rr.type, name=rr.name, rr=by_name_type[rr.name, rr.type]))
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   169
            
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   170
            by_name[rr.name] = rr
472
814cc88c531b pvl.hosts.zone: stricter name/type + name/CNAME -conflict logic
Tero Marttila <tero.marttila@aalto.fi>
parents: 471
diff changeset
   171
            by_name_type[rr.name, rr.type] = rr
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   172
            
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   173
            # preserve ordering
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   174
            yield rr
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   175
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   176
def apply_hosts_reverse (options, hosts, prefix) :
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   177
    """
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   178
        Generate DNS ZoneRecords within the given prefix's reverse-dns zone for hosts.
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   179
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   180
        Yields ZoneRecords in IPAddress-order
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   181
    """
474
51983fcda6b1 pvl.hosts.zone: fix and test --unknown-host
Tero Marttila <tero.marttila@aalto.fi>
parents: 472
diff changeset
   182
        
51983fcda6b1 pvl.hosts.zone: fix and test --unknown-host
Tero Marttila <tero.marttila@aalto.fi>
parents: 472
diff changeset
   183
    if options.unknown_host and not options.hosts_domain:
51983fcda6b1 pvl.hosts.zone: fix and test --unknown-host
Tero Marttila <tero.marttila@aalto.fi>
parents: 472
diff changeset
   184
        raise Exception("--unknown-host requires --hosts-domain")
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   185
    
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   186
    # collect data for records
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   187
    by_ip = dict()
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   188
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   189
    for host in hosts:
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   190
        for ip, rr in host_reverse(host, prefix) :
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   191
            if ip in by_ip :
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   192
                raise HostZoneError(host, "{host}: IP {ip} conflict: {other}".format(host=host, ip=ip, other=by_ip[ip]))
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   193
            
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   194
            # do not retain order
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   195
            by_ip[ip] = rr
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   196
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   197
    if options.unknown_host :
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   198
        # enumerate all of them
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   199
        iter_ips = prefix.iterhosts()
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   200
    else :
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   201
        iter_ips = sorted(by_ip)
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   202
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   203
    for ip in iter_ips :
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   204
        if ip in by_ip :
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   205
            yield by_ip[ip]
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   206
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   207
        elif options.unknown_host:
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   208
            # synthesize a record
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   209
            label = pvl.dns.reverse_label(prefix, ip)
474
51983fcda6b1 pvl.hosts.zone: fix and test --unknown-host
Tero Marttila <tero.marttila@aalto.fi>
parents: 472
diff changeset
   210
            fqdn = pvl.dns.fqdn(options.unknown_host, options.hosts_domain)
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   211
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   212
            log.info("%s %s[%s]: unused PTR %s", options.unknown_host, ip, prefix, fqdn)
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   213
474
51983fcda6b1 pvl.hosts.zone: fix and test --unknown-host
Tero Marttila <tero.marttila@aalto.fi>
parents: 472
diff changeset
   214
            yield pvl.dns.ZoneRecord.PTR(label, fqdn)
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   215
474
51983fcda6b1 pvl.hosts.zone: fix and test --unknown-host
Tero Marttila <tero.marttila@aalto.fi>
parents: 472
diff changeset
   216
        else:
458
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   217
            continue
600ad9eb6f25 pvl.hosts.zone: cleanup and split pvl.hosts-forward and pvl.hosts-reverse from pvl.hosts-dns
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   218