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