pvl.hosts-dhcp: generate dhcp hosts conf
authorTero Marttila <terom@paivola.fi>
Mon, 16 Dec 2013 19:11:58 +0200
changeset 271 4dfa1a939153
parent 270 bafcda3a3c0d
child 272 5755f9c54135
pvl.hosts-dhcp: generate dhcp hosts conf
bin/pvl.hosts-dhcp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/pvl.hosts-dhcp	Mon Dec 16 19:11:58 2013 +0200
@@ -0,0 +1,54 @@
+#!/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)