main.py
author Tero Marttila <terom@fixme.fi>
Sun, 12 Jul 2009 00:43:36 +0300
changeset 6 57e8168ba8c4
parent 5 86b05c0ab5cd
permissions -rwxr-xr-x
use FQDN for zone hosts
#!/usr/bin/env python2.5

import data, dhcp, bind_conf, bind

import optparse, itertools

def parse_args (argv) :
    """
        Parse the command-line arguments from the given argv list, returning a (options_struct, args_list) tuple,
        as per optparse.
    """

    usage = "Usage: %prog [options] data-file"

    # define our options
    parser = optparse.OptionParser(usage=usage)
    parser.add_option('--dhcpd-conf',   dest='dhcpd_conf',      metavar="PATH", help="path to dhcpd.conf", default='/etc/dhcp3/dhcpd.conf')
    parser.add_option('--bind-zone',    dest='bind_zone',       metavar="PATH", help="path to bind zone file", default=None)
    parser.add_option('--autoserial',   dest='autoserial_path', metavar="PATH", help="path to autoserial file", default='autoserial')
    
    # parse them
    options, args = parser.parse_args(args=argv[1:])

    # parse the positional arguments
    data_file, = args

    # ok
    return options, (data_file, )

def write_dhcp (options, settings) :
    """
        Write the DHCP config module using the data loaded from the given module
    """
    
    # build the config file
    config = dhcp.Config(path=options.dhcpd_conf,
        settings        = settings.dhcp_settings,
        options         = settings.dhcp_options,
        shared_network  = settings.shared_network,
        subnets         = settings.subnets,
        hosts           = itertools.chain(*(host.build_dhcp_hosts() for host in settings.hosts)),
    )

    # write it out
    config.write()

def write_bind (options, settings) :
    """
        Write a BIND config for a forward zone
    """

    assert options.bind_zone

    # load the serial
    autoserial = bind.AutoSerial(options.autoserial_path)

    # build the zone file
    zone = bind.Domain(domain=settings.domain, path=options.bind_zone,
            nameservers     = settings.nameservers,
            mailservers     = [((i+1)*10, label) for i, label in enumerate(settings.mailservers)],
            serial          = autoserial.serial(),
            settings        = settings.bind_settings,
            objs            = itertools.chain(*[host.build_bind_domain_records(settings.domain) for host in settings.hosts]),
    )

    # write it out
    zone.write()

def main (argv) :
    """
        Our app entry point, parse args, load data, write out the config files
    """
    
    # parse args
    options, (data_file, ) = parse_args(argv)

    # load the data
    data_module = data.load_py('pvl_hosts_data', data_file)
    
    # write out the config files
    write_dhcp(options, data_module)
    write_bind(options, data_module)

if __name__ == '__main__' :
    from sys import argv

    main(argv)