tero@458: #!/usr/bin/env python tero@458: tero@475: import logging; log = logging.getLogger('pvl.hosts-forward') tero@475: import optparse tero@516: import os.path tero@475: import pvl.args tero@475: import pvl.hosts terom@735: import pvl.hosts.interface tero@458: import pvl.hosts.zone tero@458: tero@475: def main (argv): tero@475: """ tero@475: Generate bind zonefiles from host definitions. tero@475: """ tero@475: tero@475: parser = optparse.OptionParser(main.__doc__) tero@475: parser.add_option_group(pvl.args.parser(parser)) tero@475: parser.add_option_group(pvl.hosts.config.optparser(parser)) tero@475: tero@516: parser.add_option('--zone-origin', metavar='DOMAIN', tero@516: help="Generated records for given zone origin") tero@516: tero@516: parser.add_option('--root-zone', action='store_const', dest='zone_origin', const='.', tero@516: help="Generate root zone") tero@475: tero@475: parser.add_option('--add-origin', action='store_true', tero@475: help="Include $ORIGIN directive in zone") tero@475: tero@687: parser.add_option('--check-conflicts', action='store_true', tero@687: help="Check for dupliate forward records, e.g. multiple A records for the same name") tero@687: tero@475: # input tero@475: options, args = pvl.args.parse(parser, argv) tero@516: tero@516: if options.zone_origin: tero@516: origin = options.zone_origin tero@516: tero@516: log.info("using given zone origin: %s", origin) tero@516: tero@516: elif len(args) == 1: tero@516: path, = args tero@516: origin = os.path.basename(path.rstrip('/')) tero@516: tero@516: log.info("using given hostpath for zone origin: %s", origin) tero@516: terom@670: elif args: terom@670: log.fatal("--zone-origin is required if passing multiple hostfiles") terom@670: return 1 terom@670: tero@516: else: terom@670: log.fatal("no hosts given as input") tero@475: return 1 tero@475: tero@475: hosts = pvl.hosts.apply(options, args) tero@475: tero@475: # process tero@487: try: tero@516: for rr in pvl.hosts.zone.apply_hosts_forward(hosts, origin, tero@687: add_origin = options.add_origin, tero@687: check_conflicts = options.check_conflicts, tero@489: ): tero@487: print unicode(rr) tero@487: except pvl.hosts.HostError as error: tero@487: log.error("%s", error) tero@487: return 3 tero@487: tero@475: return 0 tero@475: tero@475: if __name__ == '__main__': tero@475: pvl.args.main(main)