terom@7: #!/usr/bin/env python2.5 terom@7: terom@7: import data, dhcp, bind_conf, bind terom@7: terom@7: import optparse, itertools terom@7: terom@7: def parse_args (argv) : terom@7: """ terom@7: Parse the command-line arguments from the given argv list, returning a (options_struct, args_list) tuple, terom@7: as per optparse. terom@7: """ terom@7: terom@7: usage = "Usage: %prog [options] data-file" terom@7: terom@7: # define our options terom@7: parser = optparse.OptionParser(usage=usage) terom@7: parser.add_option('--dhcpd-conf', dest='dhcpd_conf', metavar="PATH", help="path to dhcpd.conf", default='/etc/dhcp3/dhcpd.conf') terom@7: parser.add_option('--bind-zone', dest='bind_zone', metavar="PATH", help="path to bind zone file", default=None) terom@7: parser.add_option('--autoserial', dest='autoserial_path', metavar="PATH", help="path to autoserial file", default='autoserial') terom@7: terom@7: # parse them terom@7: options, args = parser.parse_args(args=argv[1:]) terom@7: terom@7: # parse the positional arguments terom@7: data_file, = args terom@7: terom@7: # ok terom@7: return options, (data_file, ) terom@7: terom@7: def write_dhcp (options, settings) : terom@7: """ terom@7: Write the DHCP config module using the data loaded from the given module terom@7: """ terom@7: terom@7: # build the config file terom@7: config = dhcp.Config(path=options.dhcpd_conf, terom@7: settings = settings.dhcp_settings, terom@7: options = settings.dhcp_options, terom@7: shared_network = settings.shared_network, terom@7: subnets = settings.subnets, terom@7: hosts = itertools.chain(*(host.build_dhcp_hosts() for host in settings.hosts)), terom@7: ) terom@7: terom@7: # write it out terom@7: config.write() terom@7: terom@7: def write_bind (options, settings) : terom@7: """ terom@7: Write a BIND config for a forward zone terom@7: """ terom@7: terom@7: assert options.bind_zone terom@7: terom@7: # load the serial terom@7: autoserial = bind.AutoSerial(options.autoserial_path) terom@7: terom@7: # build the zone file terom@7: zone = bind.Domain(domain=settings.domain, path=options.bind_zone, terom@7: nameservers = settings.nameservers, terom@7: mailservers = [((i+1)*10, label) for i, label in enumerate(settings.mailservers)], terom@7: serial = autoserial.serial(), terom@7: settings = settings.bind_settings, terom@7: objs = itertools.chain(*[host.build_bind_domain_records(settings.domain) for host in settings.hosts]), terom@7: ) terom@7: terom@7: # write it out terom@7: zone.write() terom@7: terom@7: def main (argv) : terom@7: """ terom@7: Our app entry point, parse args, load data, write out the config files terom@7: """ terom@7: terom@7: # parse args terom@7: options, (data_file, ) = parse_args(argv) terom@7: terom@7: # load the data terom@7: data_module = data.load_py('pvl_hosts_data', data_file) terom@7: terom@7: # write out the config files terom@7: write_dhcp(options, data_module) terom@7: write_bind(options, data_module) terom@7: terom@7: if __name__ == '__main__' : terom@7: from sys import argv terom@7: terom@7: main(argv) terom@7: