bin/pvl.hosts-forward
author Tero Marttila <terom@paivola.fi>
Mon, 09 Mar 2015 19:48:19 +0200
changeset 735 008cfe47b194
parent 687 f99f9e3d02cf
permissions -rwxr-xr-x
pvl.hosts.interface: sub-interfaces for a host, which are not associated with any separate domain/network
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-forward')
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 
516
9615ffc647a0 pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
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: 687
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
516
9615ffc647a0 pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    20
    parser.add_option('--zone-origin',          metavar='DOMAIN',
9615ffc647a0 pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    21
            help="Generated records for given zone origin")
9615ffc647a0 pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    22
9615ffc647a0 pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    23
    parser.add_option('--root-zone',            action='store_const', dest='zone_origin', const='.',
9615ffc647a0 pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    24
            help="Generate root zone")
475
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
    parser.add_option('--add-origin',           action='store_true',
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    27
            help="Include $ORIGIN directive in zone")
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    28
687
f99f9e3d02cf pvl.hosts.zone: make non-CNAME check_conflicts optional
Tero Marttila <tero.marttila@aalto.fi>
parents: 670
diff changeset
    29
    parser.add_option('--check-conflicts',      action='store_true',
f99f9e3d02cf pvl.hosts.zone: make non-CNAME check_conflicts optional
Tero Marttila <tero.marttila@aalto.fi>
parents: 670
diff changeset
    30
            help="Check for dupliate forward records, e.g. multiple A records for the same name")
f99f9e3d02cf pvl.hosts.zone: make non-CNAME check_conflicts optional
Tero Marttila <tero.marttila@aalto.fi>
parents: 670
diff changeset
    31
475
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    32
    # input
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    33
    options, args = pvl.args.parse(parser, argv)
516
9615ffc647a0 pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    34
 
9615ffc647a0 pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    35
    if options.zone_origin:
9615ffc647a0 pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    36
        origin = options.zone_origin
9615ffc647a0 pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    37
        
9615ffc647a0 pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    38
        log.info("using given zone origin: %s", origin)
9615ffc647a0 pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    39
   
9615ffc647a0 pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    40
    elif len(args) == 1:
9615ffc647a0 pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    41
        path, = args
9615ffc647a0 pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    42
        origin = os.path.basename(path.rstrip('/'))
9615ffc647a0 pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    43
9615ffc647a0 pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    44
        log.info("using given hostpath for zone origin: %s", origin)
9615ffc647a0 pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    45
670
b95ad8c8bb4e pvl.hosts: fail if no hostfiles are given as input
Tero Marttila <terom@paivola.fi>
parents: 516
diff changeset
    46
    elif args:
b95ad8c8bb4e pvl.hosts: fail if no hostfiles are given as input
Tero Marttila <terom@paivola.fi>
parents: 516
diff changeset
    47
        log.fatal("--zone-origin is required if passing multiple hostfiles")
b95ad8c8bb4e pvl.hosts: fail if no hostfiles are given as input
Tero Marttila <terom@paivola.fi>
parents: 516
diff changeset
    48
        return 1
b95ad8c8bb4e pvl.hosts: fail if no hostfiles are given as input
Tero Marttila <terom@paivola.fi>
parents: 516
diff changeset
    49
516
9615ffc647a0 pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    50
    else:
670
b95ad8c8bb4e pvl.hosts: fail if no hostfiles are given as input
Tero Marttila <terom@paivola.fi>
parents: 516
diff changeset
    51
        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
    52
        return 1
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    53
    
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    54
    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
    55
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    56
    # 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
    57
    try:
516
9615ffc647a0 pvl.hosts-forward: if only a single hosts file/dir is given, use it as the --zone-origin; support --root-zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 489
diff changeset
    58
        for rr in pvl.hosts.zone.apply_hosts_forward(hosts, origin,
687
f99f9e3d02cf pvl.hosts.zone: make non-CNAME check_conflicts optional
Tero Marttila <tero.marttila@aalto.fi>
parents: 670
diff changeset
    59
                add_origin      = options.add_origin,
f99f9e3d02cf pvl.hosts.zone: make non-CNAME check_conflicts optional
Tero Marttila <tero.marttila@aalto.fi>
parents: 670
diff changeset
    60
                check_conflicts = options.check_conflicts,
489
7f1bd12e0d54 pvl.hosts-reverse: move options out of pvl.hosts.zone
Tero Marttila <tero.marttila@aalto.fi>
parents: 487
diff changeset
    61
        ):
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
    62
            print unicode(rr)
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
    63
    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
    64
        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
    65
        return 3
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
    66
475
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    67
    return 0
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    68
a76571e27c6f pvl.dns.zone: move script main()'s back out to bin/
Tero Marttila <tero.marttila@aalto.fi>
parents: 458
diff changeset
    69
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
    70
    pvl.args.main(main)