pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
import pvl.dhcp.config
import pvl.hosts.host
def dhcp_host_subclass (host, subclass, ethernet):
"""
Build a DHCP Item for declaring a subclass for a host.
"""
# hardware-type prefixed hardware-address
hardware = pvl.dhcp.config.Field('1:' + ethernet)
return pvl.dhcp.config.Block(None, [
('subclass', subclass, hardware),
])
class HostDHCPError(pvl.hosts.host.HostError):
pass
def dhcp_host_options (host, ethernet, subclass=None):
"""
Yield specific dhcp.conf host { ... } items.
"""
yield 'option', 'host-name', host.name
yield 'hardware', 'ethernet', pvl.dhcp.config.Field(ethernet)
if host.ip:
yield 'fixed-address', pvl.dhcp.config.Field(str(host.ip))
for bootopt in ('next-server', 'filename'):
if bootopt in host.boot:
yield bootopt, host.boot[bootopt]
def dhcp_host (host,
subclass = None,
):
"""
Yield pvl.dhcp.config.Block's
Takes dhcp:* extensions as keyword arguments
subclass: name - generate a subclass name $ethernet for this host
"""
if set(host.ethernet) == set([None]) :
host_fmt = "{host.name}"
elif host.ethernet :
host_fmt = "{host.name}-{index}"
else :
# nothing to be seen here
return
if host.owner :
comment = u"Owner: {host.owner}".format(host=host)
else:
comment = None
for index, ethernet in host.ethernet.iteritems() :
name = host_fmt.format(host=host, index=index)
items = list(dhcp_host_options(host, ethernet))
yield pvl.dhcp.config.Block(('host', name), items, comment=comment)
if subclass:
yield dhcp_host_subclass(host, subclass, ethernet)
def dhcp_hosts (hosts):
"""
Verifies that there are no dupliate hosts.
"""
blocks = { }
for host in hosts:
extensions = host.extensions.get('dhcp', {})
for block in dhcp_host(host, **extensions):
if block.key in blocks:
raise HostDHCPError(host, "dhcp {block} conflict with {other}; hosts on multiple networks must use unique ethernet.XXX=... naming".format(block=block, other=blocks[block.key]))
blocks[block.key] = host
yield block