bin/pvl.hosts-reverse
author Tero Marttila <terom@paivola.fi>
Mon, 09 Mar 2015 21:17:06 +0200
changeset 736 75938aa0390b
parent 735 008cfe47b194
permissions -rwxr-xr-x
pvl.hosts.interfaces: remove junos-specifics
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
#!/usr/bin/env python
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
475
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
     3
import logging; log = logging.getLogger('pvl.hosts-reverse')
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
     4
import optparse 
519
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
     5
import os.path
475
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
     6
import pvl.args
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
     7
import pvl.hosts
735
008cfe47b194 pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
Tero Marttila <terom@paivola.fi>
parents: 670
diff changeset
     8
import pvl.hosts.interface
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
     9
import 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
    10
475
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    11
def main (argv):
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    12
    """
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    13
        Generate bind zonefiles from host definitions.
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    14
    """
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    15
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    16
    parser = optparse.OptionParser(main.__doc__)
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    17
    parser.add_option_group(pvl.args.parser(parser))
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    18
    parser.add_option_group(pvl.hosts.config.optparser(parser))
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    19
519
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    20
    parser.add_option('--zone-prefix',          metavar='PREFIX',
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    21
            help="Generated records for given prefix's reverse zone origin")
475
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    22
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    23
    parser.add_option('--unknown-host',         metavar='NAME',
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    24
            help="Generate records for unused IPs")
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    25
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    26
    # input
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    27
    options, args = pvl.args.parse(parser, argv)
519
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    28
 
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    29
    if options.zone_prefix:
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    30
        try:
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    31
            prefix = pvl.dns.parse_prefix(options.zone_prefix)
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    32
        except ValueError as error:
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    33
            log.error("invalid reverse --zone-prefix=%s: %s", options.zone_prefix, error)
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    34
            return 1
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    35
        else:
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    36
            log.info("using given reverse --zone-prefix: %s", prefix)
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    37
   
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    38
    elif len(args) == 1:
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    39
        path, = args
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    40
        zone_prefix = os.path.basename(path.rstrip('/'))
475
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    41
519
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    42
        try:
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    43
            prefix = pvl.dns.parse_prefix(zone_prefix)
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    44
        except ValueError as error:
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    45
            log.error("invalid hosts path %s for reverse --zone-prefix: %s", zone_prefix, error)
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    46
            return 1
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    47
        else:
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    48
            log.info("using given hosts path %s for reverse --zone-prefix: %s", zone_prefix, prefix)
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    49
670
b95ad8c8bb4e pvl.hosts: fail if no hostfiles are given as input
Tero Marttila <terom@paivola.fi>
parents: 519
diff changeset
    50
    elif args:
b95ad8c8bb4e pvl.hosts: fail if no hostfiles are given as input
Tero Marttila <terom@paivola.fi>
parents: 519
diff changeset
    51
        log.fatal("--zone-prefix is required if passing multiple hosts files")
b95ad8c8bb4e pvl.hosts: fail if no hostfiles are given as input
Tero Marttila <terom@paivola.fi>
parents: 519
diff changeset
    52
        return 1
b95ad8c8bb4e pvl.hosts: fail if no hostfiles are given as input
Tero Marttila <terom@paivola.fi>
parents: 519
diff changeset
    53
519
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    54
    else:
670
b95ad8c8bb4e pvl.hosts: fail if no hostfiles are given as input
Tero Marttila <terom@paivola.fi>
parents: 519
diff changeset
    55
        log.fatal("no hosts given as input")
475
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    56
        return 1
519
fa2fd79fe205 pvl.hosts-reverse: use given sole host file path for --zone-prefix
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    57
 
489
7f1bd12e0d54 pvl.hosts-reverse: move options out of pvl.hosts.zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 487
diff changeset
    58
    if options.unknown_host and not options.hosts_domain:
7f1bd12e0d54 pvl.hosts-reverse: move options out of pvl.hosts.zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 487
diff changeset
    59
        log.fatal("--unknown-host requires --hosts-domain=")
7f1bd12e0d54 pvl.hosts-reverse: move options out of pvl.hosts.zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 487
diff changeset
    60
        return 1
7f1bd12e0d54 pvl.hosts-reverse: move options out of pvl.hosts.zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 487
diff changeset
    61
475
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    62
    hosts = pvl.hosts.apply(options, args)
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    63
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    64
    # process
487
920394061b6f pvl.hosts.zone: HostZoneError(HostError), move resolve() to pvl.dns.relative(), and expect HostError's in pvl.hosts-forward/pvl.hosts-reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 475
diff changeset
    65
    try:
489
7f1bd12e0d54 pvl.hosts-reverse: move options out of pvl.hosts.zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 487
diff changeset
    66
        for rr in pvl.hosts.zone.apply_hosts_reverse(hosts, prefix,
7f1bd12e0d54 pvl.hosts-reverse: move options out of pvl.hosts.zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 487
diff changeset
    67
                unknown_host    = options.unknown_host,
7f1bd12e0d54 pvl.hosts-reverse: move options out of pvl.hosts.zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 487
diff changeset
    68
                unknown_domain  = options.hosts_domain,
7f1bd12e0d54 pvl.hosts-reverse: move options out of pvl.hosts.zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 487
diff changeset
    69
        ):
487
920394061b6f pvl.hosts.zone: HostZoneError(HostError), move resolve() to pvl.dns.relative(), and expect HostError's in pvl.hosts-forward/pvl.hosts-reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 475
diff changeset
    70
            print unicode(rr)
489
7f1bd12e0d54 pvl.hosts-reverse: move options out of pvl.hosts.zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 487
diff changeset
    71
487
920394061b6f pvl.hosts.zone: HostZoneError(HostError), move resolve() to pvl.dns.relative(), and expect HostError's in pvl.hosts-forward/pvl.hosts-reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 475
diff changeset
    72
    except pvl.hosts.HostError as error:
920394061b6f pvl.hosts.zone: HostZoneError(HostError), move resolve() to pvl.dns.relative(), and expect HostError's in pvl.hosts-forward/pvl.hosts-reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 475
diff changeset
    73
        log.error("%s", error)
920394061b6f pvl.hosts.zone: HostZoneError(HostError), move resolve() to pvl.dns.relative(), and expect HostError's in pvl.hosts-forward/pvl.hosts-reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 475
diff changeset
    74
        return 3
475
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    75
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    76
    return 0
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    77
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    78
if __name__ == '__main__':
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    79
    pvl.args.main(main)