bin/pvl.dns-process
author Tero Marttila <terom@paivola.fi>
Mon, 09 Mar 2015 23:31:13 +0200
changeset 738 3104fdf7ea26
parent 717 e37b0a857a5d
permissions -rw-r--r--
pvl.hosts.hosts: drop support for instanced ip.* in favor of improved interface:ip.* =
#!/usr/bin/env python

"""
    Process bind zonefiles, without altering their structure.

    Takes a zonefile as input, and gives a semantically identical zonefile as output, with the given changes.
"""

import logging; log = logging.getLogger('pvl.dns-process')
import optparse
import pvl.args
import pvl.dns.process
import pvl.dns.zone

def main (argv):
    parser = optparse.OptionParser(main.__doc__)
    parser.add_option_group(pvl.args.parser(parser))
    parser.add_option_group(pvl.dns.process.optparser(parser))

    parser.add_option('--serial',               metavar='YYMMDDXX',
            help="Set serial for SOA record")

    parser.add_option('--include-path',         metavar='PATH',
            help="Rewrite includes to given absolute path")
    
    parser.add_option('--include-trace',         metavar='FILE',
            help="Write out included files to given file")

    # input
    options, args = pvl.args.parse(parser, argv)
    
    # process
    if options.include_trace:
        include_trace = [ ]
    else:
        include_trace = None

    zone = list(pvl.dns.process.apply_zone(options, args,
            include_trace   = include_trace,
    ))

    if options.serial:
        log.info("Set zone serial: %s", options.serial)

        zone = list(pvl.dns.process.zone_serial(zone, options.serial))

    if options.include_path:
        log.info("Set zone include path: %s", options.include_path)

        zone = list(pvl.dns.process.zone_includes(zone, options.include_path,
                include_trace   = include_trace,
        ))

    if options.include_trace:
        with pvl.args.apply_file(options.include_trace, 'w') as file:
            for include in include_trace:
                print >>file, include
    
    pvl.dns.process.apply_zone_output(options, zone)

    return 0

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