#!/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)