Only set DNS1 and DNS2 for the primary network, if they exist.
[pyplnet.git] / plnet.py
index 1c92123..abfeb37 100755 (executable)
--- a/plnet.py
+++ b/plnet.py
@@ -42,17 +42,18 @@ def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeMa
     # returned.  Because 'interface'is decremented as each interface is processed,
     # by the time is_primary=True (primary) interface is reached, the device
     # "eth%s" % interface, is not eth0.  But, something like eth-4, or eth-12.
-    # This code sorts the interfaces, implicitly placing the primary (first created)
-    # interface first.  There is a lot of room for improvement to how this
+    # This code sorts the interfaces, placing is_primary=True interfaces first.  
+    # There is a lot of room for improvement to how this
     # script handles interfaces and how it chooses the primary interface.
     def compare_by (fieldname):
         def compare_two_dicts (a, b):
             return cmp(a[fieldname], b[fieldname])
         return compare_two_dicts
-    if version == 4.3:
-        networks.sort( compare_by('interface_id') )
-    else:
-        networks.sort( compare_by('nodenetwork_id') )
+
+    # NOTE: by sorting on 'is_primary' and then reversing (since False is sorted
+    # before True) all 'is_primary' interfaces are at the beginning of the list.
+    networks.sort( compare_by('is_primary') )
+    networks.reverse()
 
     for network in networks:
        logger.verbose('net:InitInterfaces interface %d: %s'%(interface,network))
@@ -84,10 +85,12 @@ def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeMa
             inter['BOOTPROTO'] = "static"
             inter['IPADDR'] = network['ip']
             inter['NETMASK'] = network['netmask']
-            inter['DNS1'] = network['dns1']
-            inter['DNS2'] = network['dns2']
             if network['is_primary']:
                 gateway = network['gateway']
+                if network['dns1']:
+                    inter['DNS1'] = network['dns1']
+                if network['dns2']:
+                    inter['DNS2'] = network['dns2']
 
         elif network['method'] == "dhcp":
             inter['BOOTPROTO'] = "dhcp"
@@ -155,7 +158,7 @@ def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeMa
                                        (inter['IFNAME'],hwifname))
                         inter['IFNAME'] = hwifname
                 else:
-                    logger.log('net:InitInterfaces WARNING: mac addr %s for alias not found' %(hwaddr,alias))
+                    logger.log('net:InitInterfaces WARNING: mac addr %s for alias not found' %(hwaddr))
 
             if 'IFNAME' in inter:
                 # stupid RH /etc/sysconfig/network-scripts/ifup-aliases:new_interface()