bin/pvl.hosts-forward
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
#!/usr/bin/env python

import logging; log = logging.getLogger('pvl.hosts-forward')
import optparse 
import os.path
import pvl.args
import pvl.hosts
import pvl.hosts.interface
import pvl.hosts.zone

def main (argv):
    """
        Generate bind zonefiles from host definitions.
    """

    parser = optparse.OptionParser(main.__doc__)
    parser.add_option_group(pvl.args.parser(parser))
    parser.add_option_group(pvl.hosts.config.optparser(parser))

    parser.add_option('--zone-origin',          metavar='DOMAIN',
            help="Generated records for given zone origin")

    parser.add_option('--root-zone',            action='store_const', dest='zone_origin', const='.',
            help="Generate root zone")
    
    parser.add_option('--add-origin',           action='store_true',
            help="Include $ORIGIN directive in zone")

    parser.add_option('--check-conflicts',      action='store_true',
            help="Check for dupliate forward records, e.g. multiple A records for the same name")

    # input
    options, args = pvl.args.parse(parser, argv)
 
    if options.zone_origin:
        origin = options.zone_origin
        
        log.info("using given zone origin: %s", origin)
   
    elif len(args) == 1:
        path, = args
        origin = os.path.basename(path.rstrip('/'))

        log.info("using given hostpath for zone origin: %s", origin)

    elif args:
        log.fatal("--zone-origin is required if passing multiple hostfiles")
        return 1

    else:
        log.fatal("no hosts given as input")
        return 1
    
    hosts = pvl.hosts.apply(options, args)

    # process
    try:
        for rr in pvl.hosts.zone.apply_hosts_forward(hosts, origin,
                add_origin      = options.add_origin,
                check_conflicts = options.check_conflicts,
        ):
            print unicode(rr)
    except pvl.hosts.HostError as error:
        log.error("%s", error)
        return 3

    return 0

if __name__ == '__main__':
    pvl.args.main(main)