bin/pvl.hosts-dhcp
author Tero Marttila <terom@paivola.fi>
Mon, 16 Dec 2013 19:11:58 +0200
changeset 271 4dfa1a939153
child 284 eb7e2a4c6880
permissions -rwxr-xr-x
pvl.hosts-dhcp: generate dhcp hosts conf
#!/usr/bin/env python

import pvl.args
import pvl.hosts

import configobj
import logging; log = logging.getLogger('pvl.hosts-dhcp')
import optparse

def process_hosts (options, hosts) :
    for host in hosts :
        if host.owner :
            yield u"# {owner}".format(owner=host.owner)

        for index, ethernet in enumerate(host.ethernet) :
            if len(host.ethernet) > 1 :
                yield "host {host}-{index} {{".format(host=host, index=index)
            else :
                yield "host {host} {{".format(host=host)

            yield '\toption host-name "{host}";'.format(host=host)
            yield "\thardware ethernet {ethernet};".format(ethernet=ethernet)

            if host.ip :
                yield "\tfixed-address {ip};".format(ip=host.ip)
            
            yield "}"

        yield ""

def apply_conf (options, lines) :
    for line in lines :
        print line

def main (argv) :
    """
        Generate DHCP host configs from host definitions.
    """

    parser = optparse.OptionParser(main.__doc__)
    parser.add_option_group(pvl.args.parser(parser))
    parser.add_option_group(pvl.hosts.optparser(parser))

    options, args = parser.parse_args(argv[1:])
    pvl.args.apply(options)

    # input
    hosts = pvl.hosts.apply(options, args)

    # process
    apply_conf(options, process_hosts(options, hosts))

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