pvl.hosts: support new alias/ethernet field format
authorTero Marttila <terom@paivola.fi>
Tue, 17 Dec 2013 12:55:53 +0200
changeset 305 e85c95e757eb
parent 304 9332f21f5aa1
child 306 a1bdb4499c7a
pvl.hosts: support new alias/ethernet field format
bin/pvl.hosts-dhcp
pvl/hosts.py
--- a/bin/pvl.hosts-dhcp	Tue Dec 17 12:40:16 2013 +0200
+++ b/bin/pvl.hosts-dhcp	Tue Dec 17 12:55:53 2013 +0200
@@ -36,12 +36,12 @@
         else :
             next_server = filename = None
 
-        if len(host.ethernet) > 1 :
-            host_fmt = "{host}-{index}"
+        if set(host.ethernet) == set([0]) :
+            host_fmt = "{host}"
         else :
-            host_fmt = "{host}"
+            host_fmt = "{host}-{index}"
 
-        for index, ethernet in enumerate(host.ethernet) :
+        for index, ethernet in host.ethernet.iteritems() :
             for line in build_host(host_fmt.format(host=host, index=index),
                     { 'option host-name':   str(host) },
                     { 'hardware ethernet':  ethernet },
--- a/pvl/hosts.py	Tue Dec 17 12:40:16 2013 +0200
+++ b/pvl/hosts.py	Tue Dec 17 12:55:53 2013 +0200
@@ -51,19 +51,30 @@
             yield cls.build(options, host, ip=ip, **extra)
     
     @classmethod
-    def build (cls, options, host, domain=None, ip=None, ip6=None, owner=None, boot=None, **extra) :
+    def build (cls, options, host, domain=None, ip=None, ip6=None, owner=None, boot=None, alias=None, **extra) :
         """
             Return a Host from a config section's scalars.
         """
 
-        ethernet = []
-        alias = []
+        if alias :
+            alias = alias.split()
+        else :
+            alias = ()
+        
+        ethernet = { }
 
-        for attr, value in extra.iteritems() :
-            if attr.startswith('ethernet') :
-                ethernet.append(value)
-            elif attr.startswith('alias') :
-                alias.append(value)
+        for field, value in extra.iteritems() :
+            if '.' in field :
+                field, instance = field.split('.')
+            else :
+                instance = None
+
+            if field == 'ethernet' :
+                if instance :
+                    ethernet[instance] = value
+                else :
+                    for eth in value.split() :
+                        ethernet[len(ethernet)] = eth
             else :
                 raise ValueError("Unknown host attr: %s=%s" % (attr, value))
         
@@ -80,13 +91,13 @@
                 boot        = boot,
         )
 
-    def __init__ (self, host, domain=None, ip=None, ip6=None, ethernet=(), alias=(), owner=None, boot=None) :
+    def __init__ (self, host, domain=None, ip=None, ip6=None, ethernet={ }, alias=(), owner=None, boot=None) :
         """
             host        - str
             domain      - str
             ip          - ipaddr.IPv4Address
             ip6         - ipaddr.IPv6Address
-            ethernet    - list
+            ethernet    - { index: ethernet }
             alias       - list
             owner       - str: LDAP uid
         """