pvl.hosts-dhcp: boot support
authorTero Marttila <terom@paivola.fi>
Mon, 16 Dec 2013 20:50:10 +0200
changeset 284 eb7e2a4c6880
parent 283 951e4b70bad4
child 285 e18aaec99e54
pvl.hosts-dhcp: boot support
bin/pvl.hosts-dhcp
--- a/bin/pvl.hosts-dhcp	Mon Dec 16 20:50:04 2013 +0200
+++ b/bin/pvl.hosts-dhcp	Mon Dec 16 20:50:10 2013 +0200
@@ -7,26 +7,56 @@
 import logging; log = logging.getLogger('pvl.hosts-dhcp')
 import optparse
 
-def process_hosts (options, hosts) :
-    for host in hosts :
+def build_host (host, *items) :
+    yield "host {host} {{".format(host=host)
+    for item in items :
+        if isinstance(item, dict) :
+            for setting, value in item.iteritems() :
+                if value :
+                    yield "\t{setting:30} {value};".format(setting=setting, value=value)
+        else :
+            raise ValueError("Unknown item: %r", item)
+    yield "}"
+
+def process_host (options, host) :
         if host.owner :
-            yield u"# {owner}".format(owner=host.owner)
+            yield u"# {host.owner}".format(host=host)
+
+        if host.boot :
+            if ':' 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 :
+                log.error("%s: invalid boot: %s", host, host.boot)
+        else :
+            next_server = filename = None
+
+        if len(host.ethernet) > 1 :
+            host_fmt = "{host}-{index}"
+        else :
+            host_fmt = "{host}"
 
         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 "}"
+            for line in build_host(host_fmt.format(host=host, index=index),
+                    { 'option host-name':   str(host) },
+                    { 'hardware ethernet':  ethernet },
+                    { 'fixed-address':      host.ip },
+                    { 'next-server':        next_server },
+                    { 'filename':           filename },
+            ) :
+                yield line
 
         yield ""
+    
+def process_hosts (options, hosts) :
+    for host in hosts :
+        for line in process_host(options, host) :
+            yield line
 
 def apply_conf (options, lines) :
     for line in lines :