From 5ff6ac004e6be77f5ffbfcfb46878230b0373fd7 Mon Sep 17 00:00:00 2001
From: Anders Blomdell <anders.blomdell@control.lth.se>
Date: Mon, 4 Feb 2013 10:54:48 +0100
Subject: [PATCH] Version 2013-02-04 10:54

M  src/hostinfo/ifconfig.py
M  src/hostinfo/named.py
---
 src/hostinfo/ifconfig.py | 186 +++++++++------------------------------
 src/hostinfo/named.py    |  37 +++++---
 2 files changed, 68 insertions(+), 155 deletions(-)

diff --git a/src/hostinfo/ifconfig.py b/src/hostinfo/ifconfig.py
index 23972a0..fa53948 100755
--- a/src/hostinfo/ifconfig.py
+++ b/src/hostinfo/ifconfig.py
@@ -1,12 +1,13 @@
 from hostinfo.util import aton
-import os
+import subprocess
 import re
 
 def generate(tree, host):
     result = []
     for h in filter(lambda h: h.name[0] == host, tree._host_):
-        for i, d in map(lambda i: (i, device(i.ethernet[0])),
-                        filter(lambda i: i.ethernet[0], h._interface_)):
+        for i, d in filter(lambda (i,d): i and d,
+                           map(lambda i: (i, device(i.ethernet[0])),
+                               h._interface_)):
             static = True
             if not i._dhcpserver_ and not i._static_:
                 for ip in i._ip_:
@@ -41,15 +42,36 @@ def generate(tree, host):
                     search.extend(ip.search[0].split())
                     pass
                 config += "%sIPADDR%s=%s\n" % (pre, index, ip.address[0])
-                if not s:
-                    config += "%sNETMASK%s=255.255.255.255\n" % (pre, index)
+                netmask = (ip.netmask[0] or 
+                           s and s.netmask[0] or 
+                           '255.255.255.255')
+                gateway = ip.gateway[0] or s and s.gateway[0]
+                network = ip.network[0] or s and s.network[0]
+                broadcast = ip.broadcast[0] or s and s.broadcast[0]
+                name_servers = ip.name_servers[0] or s and s.name_servers[0]
+                if netmask:
+                    config += "%sNETMASK%s=%s\n" % (pre, index, netmask)
                     pass
-                else:
-                    config += "%sGATEWAY%s=%s\n" % (pre, index, s.gateway[0])
-                    config += "%sNETMASK%s=%s\n" % (pre, index, s.netmask[0])
-                    config += "%sNETWORK%s=%s\n" % (pre, index, s.network[0])
-                    config += "%sBROADCAST%s=%s\n" % (pre, index, 
-                                                      s.broadcast[0])
+                if gateway:
+                    config += "%sGATEWAY%s=%s\n" % (pre, index, gateway)
+                    pass
+                if network:
+                    config += "%sNETWORK%s=%s\n" % (pre, index,network)
+                    pass
+                if broadcast:
+                    config += "%sBROADCAST%s=%s\n" % (pre, index, broadcast)
+                    pass
+                #if not s:
+                #    config += "%sNETMASK%s=255.255.255.255\n" % (pre, index)
+                #    pass
+                #else:
+                #    config += "%sGATEWAY%s=%s\n" % (pre, index, s.gateway[0])
+                #    config += "%sNETMASK%s=%s\n" % (pre, index, s.netmask[0])
+                #    config += "%sNETWORK%s=%s\n" % (pre, index, s.network[0])
+                #    config += "%sBROADCAST%s=%s\n" % (pre, index, 
+                #  s.broadcast[0])
+                    
+                if s:
                     for n in re.split("[, ]+", s.name_servers[0]):
                         # Domain nameservers
                         if not n in nameservers:
@@ -87,130 +109,6 @@ def generate(tree, host):
     return result
                                 
 
-def generate_old(tree, host):
-    result = []
-    for h in tree._host_:
-        if h.name[0] == host:
-            for ip in h._interface_._ip_:
-                d = device(ip._parent.ethernet[0])
-                if not d:
-                    continue
-                
-                if ip.alias[0]:
-                    # Alias
-                    name = "%s:%s" % (d, ip.alias[0])
-                    config = "DEVICE=%s\n" % name
-                    config += "HWADDR=%s\n" % ip._parent.ethernet[0].upper()
-                    config += "ONPARENT=yes\n"
-                    if not ip._parent._dhcpserver_:
-                        config += "BOOTPROTO=dhcp\n"
-                        pass
-                    else:
-                        s = subnet(tree, ip.address[0])
-                        config += "IPADDR=%s\n" % ip.address[0]
-                        if s and s.netmask[0]:
-                            config += "NETMASK=%s\n" % s.netmask[0]
-                            pass
-                        if s and s.network[0]:
-                            config += "NETWORK=%s\n" % s.network[0]
-                            pass
-                        if s and s.broadcast[0]:
-                            config += "BROADCAST=%s\n" % s.broadcast[0]
-                            pass
-                        pass
-                    result.append(("ifcfg-%s" % name , config))
-                    pass
-                elif ip.vlan[0]:
-                    # VLAN
-                    name = "%s.%s" % (d, ip.vlan[0])
-                    config = "DEVICE=%s\n" % name
-                    config += "HWADDR=%s\n" % ip._parent.ethernet[0].upper()
-                    config += "ONBOOT=yes\n"
-                    config += "VLAN=yes\n"
-                    if not ip._parent._dhcpserver_:
-                        config += "BOOTPROTO=dhcp\n"
-                        pass
-                    else:
-                        s = subnet(tree, ip.address[0])
-                        config += "IPADDR=%s\n" % ip.address[0]
-                        if s and s.netmask[0]:
-                            config += "NETMASK=%s\n" % s.netmask[0]
-                            pass
-                        if s and s.network[0]:
-                            config += "NETWORK=%s\n" % s.network[0]
-                            pass
-                        if s and s.broadcast[0]:
-                            config += "BROADCAST=%s\n" % s.broadcast[0]
-                            pass
-                        pass
-                    result.append(("ifcfg-%s" % name , config))
-                    pass
-                elif ip.address[0]:
-                    # Ordinary address
-                    config = "DEVICE=%s\n" % d
-                    config += "HWADDR=%s\n" % ip._parent.ethernet[0].upper()
-                    config += "ONBOOT=yes\n"
-                    config += "TYPE=Ethernet\n"
-                    if not ip._parent._dhcpserver_ and not ip._parent._static_:
-                        config += "BOOTPROTO=dhcp\n"
-                        pass
-                    else:
-                        if ip._parent._dhcpserver_:
-                            s = subnet(tree, ip.address[0])
-                            pass
-                        else:
-                            s = ip._parent._static_[0]
-                            pass
-                        if s and s.name_servers[0]:
-                            name_servers = re.split("[, ]+",
-                                                    s.name_servers[0])
-                            pass
-                        else:
-                            name_servers = []
-                            pass
-                        if s:
-                            for n in ip._parent._nameserver_:
-                                if n.domain[0] == s.domain[0]:
-                                    name_servers.insert(0, ip.address[0])
-                                    pass
-                                pass
-                            pass
-                        config += "BOOTPROTO=none\n"
-                        config += "IPADDR=%s\n" % ip.address[0]
-                        if s and s.gateway[0] and s.gateway[0] != ip.address[0]:
-                            config += "GATEWAY=%s\n" % s.gateway[0]
-                            pass
-                        if s and s.netmask[0]:
-                            config += "NETMASK=%s\n" % s.netmask[0]
-                            pass
-                        if s and s.network[0]:
-                            config += "NETWORK=%s\n" % s.network[0]
-                            pass
-                        if s and s.broadcast[0]:
-                            config += "BROADCAST=%s\n" % s.broadcast[0]
-                            pass
-                        if s and name_servers:
-                            ni = 0
-                            for n in name_servers:
-                                ni += 1
-                                config += "DNS%d=%s\n" % (ni, n)
-                                pass
-                            pass
-                        if s and s.domain[0]:
-                            config += "DOMAIN=%s\n" % s.domain[0]
-                            pass
-                        pass
-                    if ip.defroute[0]:
-                        config += "DEFROUTE=%s\n" % ip.defroute[0]
-                        pass    
-                    result.append(("ifcfg-%s" % d, config))
-                    pass
-                pass
-            pass
-        pass
-    return result
-                                
-
 def subnet(tree, ip):
     for s in tree._subnet_:
         if not s.netmask[0] or not s.network[0]:
@@ -224,16 +122,14 @@ def device(ethernet):
     """ Map ethernet to device name"""
     if not ethernet:
         return None
-    dev = open("/proc/net/dev")
-    for d in dev:
-        m = re.match("\s+(\S+):", d)
+    p = subprocess.Popen(['/sbin/ip', '-o', 'link', 'show'], 
+                         stdout=subprocess.PIPE)
+    for l in p.stdout:
+        m = re.match('^[0-9]+:\s+([^:]+):.*link/ether\s+([0-9a-f:]+)\s+.*', l)
         if m:
-            name = m.group(1)
-            ifconfig = os.popen("/sbin/ifconfig %s" % name)
-            for i in ifconfig:
-                m = re.match(".*HWaddr\s+(\S+)", i)
-                if m:
-                    if m.group(1).lower() == ethernet.lower():
-                        return name
+            if m.group(2) == ethernet:
+                return m.group(1)
+            pass
+        pass
     return None
 
diff --git a/src/hostinfo/named.py b/src/hostinfo/named.py
index 68b499d..c96b6e3 100755
--- a/src/hostinfo/named.py
+++ b/src/hostinfo/named.py
@@ -58,23 +58,30 @@ def generate(tree, host):
     for s in tree._subnet_:
         fzone = s.domain[0]
         if not done.has_key(fzone):
-            conf += "zone \"%s\" { \n" % fzone
-            conf += "  type master; file \"hosts-%s\"; \n" % fzone
-            conf += "};\n"
-            result.append(("named/hosts-%s" % fzone, forward(tree, s)))
+            if forward(tree, s):
+                conf += "zone \"%s\" { \n" % fzone
+                conf += "  type master; file \"hosts-%s\"; \n" % fzone
+                conf += "};\n"
+                result.append(("named/hosts-%s" % fzone, forward(tree, s)))
+                pass
             done[fzone] = 1
+            pass
             
         if s.network[0]:
             t = s.network[0].split(".")[0:3]
             t.reverse()
             rzone = ".".join(t) + ".in-addr.arpa"
             if not done.has_key(rzone):
-                conf += "zone \"%s\" { \n" % rzone
-                conf += "  type master; file \"%s\"; \n" % rzone
-                conf += "};\n"
-                result.append(("named/%s" % rzone, reverse(tree, s)))
+                if reverse(tree, s):
+                    conf += "zone \"%s\" { \n" % rzone
+                    conf += "  type master; file \"%s\"; \n" % rzone
+                    conf += "};\n"
+                    result.append(("named/%s" % rzone, reverse(tree, s)))
+                    pass
                 done[rzone] = 1
-
+                pass
+            pass
+        pass
     result.append(("named.conf", conf))
     
     return result
@@ -93,11 +100,16 @@ def header(tree, domain, origin=None):
     if not soa:
         raise Exception("No SOA defined for %s" % domain)
 
+    if not filter(lambda ns: ns.domain[0] == domain and ns.primary[0] == 'yes',
+                  tree._host_._interface_._nameserver_):
+        return None
     result = ""
     if origin:
         result = "$ORIGIN %s.\n" % origin
+        pass
     if soa.ttl[0]:
         result += "$TTL %s\n" % soa.ttl[0]
+        pass
     result += "@ IN SOA %s %s ( \n" % (soa.nameserver[0], soa.email[0])
     result += "                %-15s ; Serial\n" % tree._mtime
     result += "                %-15s ; Refresh\n" % soa.refresh[0]
@@ -111,12 +123,15 @@ def header(tree, domain, origin=None):
         if ns.domain[0] == domain and ns.primary[0] == 'yes':
             result += "                IN      NS      %s\n" % (
                 fqn(tree, ns._parent))
-
+            pass
+        pass
     result += ";\n"
     return result
 
 def forward(tree, domain):
     result = header(tree, domain.domain[0], domain.domain[0])
+    if not result:
+        return None
     net = []
     for s in tree._subnet_:
         if s.network[0] and s.domain[0] == domain.domain[0]:
@@ -203,6 +218,8 @@ def reverse(tree, net):
     t.reverse()
     origin = ".".join(t) + ".in-addr.arpa"
     result = header(tree, net.domain[0], origin)
+    if not result:
+        return None
     host = {}
     m = aton(net.netmask[0])
     n = aton(net.network[0])
-- 
GitLab