GENICLOUD-31 Fix gateway variable not being set properly.
[pyplnet.git] / plnet.py
index 001bbee..549daf2 100755 (executable)
--- a/plnet.py
+++ b/plnet.py
@@ -79,6 +79,9 @@ def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeMa
 
         details = prepDetails(interface, hostname)
 
+        if interface['is_primary']:
+            gateway = interface['gateway']
+
         if 'interface_tag_ids' in interface:
             version = 4.3
             interface_tag_ids = "interface_tag_ids"
@@ -157,9 +160,8 @@ def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeMa
             elif orig_ifname:
                 ifname = orig_ifname
                 device_id -= 1
-            if 'PRIMARY' in details: del details['PRIMARY']
             logger.log('net:InitInterfaces: Bridge detected. Adding %s to devices_map' % ifname)
-            devices_map[ifname] = details
+            devices_map[ifname] = removeBridgedIfaceDetails(details)
             bridgeName = details['BRIDGE']
 
             logger.log('net:InitInterfaces: Adding bridge %s' % bridgeName)
@@ -404,7 +406,6 @@ def prepDetails(interface, hostname=''):
         details['NETMASK']   = interface['netmask']
         details['GATEWAY']   = interface['gateway']
         if interface['is_primary']:
-            gateway = interface['gateway']
             if interface['dns1']:
                 details['DNS1'] = interface['dns1']
             if interface['dns2']:
@@ -422,6 +423,16 @@ def prepDetails(interface, hostname=''):
 
     return details
 
+##
+# Remove duplicate entry from the bridged interface's configuration file.
+#
+def removeBridgedIfaceDetails(details):
+    for key in [ 'PRIMARY', 'PERSISTENT_DHCLIENT', 'DHCLIENTARGS', 'DHCP_HOSTNAME',
+                 'BOOTPROTO', 'IPADDR', 'NETMASK', 'GATEWAY', 'DNS1', 'DNS2' ]:
+        if key in details:
+            del details[key]
+    return details
+
 if __name__ == "__main__":
     import optparse
     import sys