--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/pvl/hosts/dhcp.py Thu Feb 26 14:40:37 2015 +0200
@@ -0,0 +1,80 @@
+import pvl.dhcp.config
+import pvl.hosts.host
+
+class HostDHCPError(pvl.hosts.host.HostError):
+ pass
+
+def dhcp_host_options (host, ethernet):
+ """
+ Yield specific dhcp.conf host { ... } parameters for build_block()
+ """
+
+ yield 'option', 'host-name', host.name
+ yield 'hardware', 'ethernet', ethernet
+
+ if host.ip:
+ yield 'fixed-address', str(host.ip)
+
+ if not host.boot:
+ next_server = filename = None
+ elif ':' in host.boot :
+ next_server, filename = host.boot.split(':', 1)
+ elif host.boot.startswith('/') :
+ next_server = None
+ filename = host.boot
+ elif host.boot.endswith(':') :
+ next_server = host.boot
+ filename = None
+ else :
+ raise HostError(host, "invalid boot={host.boot}".format(host=host))
+
+ if next_server:
+ yield 'next-server', next_server
+
+ if filename:
+ yield 'filename', filename
+
+def dhcp_host (host):
+ """
+ Yield (block, items, **opts) tuples for pvl.dhcp.config.build_block().
+ """
+
+ 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)
+
+ yield ('host', name), list(dhcp_host_options(host, ethernet)), dict(comment=comment)
+
+def apply_hosts_dhcp (options, hosts):
+ """
+ Generate dhcp.conf output lines for the set of hosts.
+
+ Verifies that there are no dupliate hosts.
+ """
+
+ blocks = { }
+
+ for host in hosts:
+ for block, items, opts in dhcp_host(host):
+ if block in blocks:
+ raise HostDHCPError(host, "dupliate host block {block}: {other}".format(block=block, other=blocks[block]))
+
+ blocks[block] = block
+
+ for line in pvl.dhcp.config.build_block(block, items, **opts):
+ yield line
+
+ yield ''
+