← Back to team overview

cloud-init-dev team mailing list archive

[Merge] ~rjschwei/cloud-init:netV1ToTranslate into cloud-init:master

 

Robert Schweikert has proposed merging ~rjschwei/cloud-init:netV1ToTranslate into cloud-init:master.

Requested reviews:
  cloud-init commiters (cloud-init-dev)

For more details, see:
https://code.launchpad.net/~rjschwei/cloud-init/+git/cloud-init/+merge/333904

Handle network configuration translation for the legacy path, do not drop gateway information
-- 
Your team cloud-init commiters is requested to review the proposed merge of ~rjschwei/cloud-init:netV1ToTranslate into cloud-init:master.
diff --git a/cloudinit/distros/net_util.py b/cloudinit/distros/net_util.py
index 1ce1aa7..c513034 100644
--- a/cloudinit/distros/net_util.py
+++ b/cloudinit/distros/net_util.py
@@ -67,6 +67,10 @@
 #     }
 # }
 
+import re
+
+ipv4 = re.compile("\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}")
+
 def translate_network(settings):
     # Get the standard cmd, args from the ubuntu format
     entries = []
@@ -88,7 +92,14 @@ def translate_network(settings):
                 consume = {}
             consume[cmd] = args
         else:
-            consume[cmd] = args
+            if consume.get(cmd):
+                val = consume[cmd]
+                if isinstance(val, list):
+                    consume[cmd].append(args)
+                else:
+                    consume[cmd] = [val, args]
+            else:
+                consume[cmd] = args
     # Check if anything left over to consume
     absorb = False
     for (cmd, args) in consume.items():
@@ -148,6 +159,16 @@ def translate_network(settings):
                     hw_addr = hw_split[1]
                     if hw_addr:
                         iface_info['hwaddress'] = hw_addr
+            if 'post-up' in info:
+                routes = info['post-up']
+                if isinstance(routes, list):
+                    for route_info in routes:
+                        if 'default gw' in route_info:
+                            iface_info['gateway'] = ipv4.search(
+                                route_info).group(0)
+                elif 'default gw' in routes:
+                    iface_info['gateway'] = ipv4.search(routes).group(0)
+                
         # If ipv6 is enabled, device will have multiple IPs, so we need to
         # update the dictionary instead of overwriting it...
         if dev_name in real_ifaces:
diff --git a/tests/unittests/test_distros/text_net_util.py b/tests/unittests/test_distros/text_net_util.py
new file mode 100644
index 0000000..7b58d81
--- /dev/null
+++ b/tests/unittests/test_distros/text_net_util.py
@@ -0,0 +1,104 @@
+# This file is part of cloud-init. See LICENSE file for license information.
+
+from cloudinit.distro import net_util
+
+from cloudinit.tests.helpers import CiTestCase
+
+class TestNetworkConfigTransform(CiTestCase):
+
+    def test_basic_config_ipv4(self):
+        basic_conf = '''
+auto lo
+iface lo inet loopback
+
+auto eth0
+iface eth0 inet static
+    address 192.168.1.5
+    broadcast 192.168.1.0
+    gateway 192.168.1.254
+    netmask 255.255.255.0
+    network 192.168.0.0
+
+auto eth1
+iface eth1 inet dhcp
+'''
+        sysconfig = net_util.translate_network(basic_conf)
+        expected = {
+            'lo': {'auto': True, 'ipv6': {}},
+            'eth0': {
+                'auto': True,
+                'ipv6': {},
+                'broadcast': '192.168.1.0',
+                'netmask': '255.255.255.0',
+                'bootproto': 'static',
+                'address': '192.168.1.5',
+                'gateway': '192.168.1.254'
+            },
+            'eth1': {'auto': True, 'bootproto': 'dhcp', 'ipv6': {}}
+        }
+        assertEqual(sysconfig, expected)
+
+    def test_v1_confi_single_route_ipv4(self):
+        v1_conf = '''
+auto lo
+iface lo inet loopback
+
+auto eth0
+iface eth0 inet static
+    hwaddress fa:16:3e:ee:2b:97
+    address 192.168.168.30/24
+    mtu 1500
+    post-up route add default gw 192.168.168.1 || true
+    pre-down route del default gw 192.168.168.1 || true
+'''
+        sysconfig = net_util.translate_network(v1_conf)
+        expected = {
+            'lo': {'auto': True, 'ipv6': {}},
+            'eth0': {
+                'auto': True,
+                'bootproto': 'static',
+                'gateway': '192.168.168.1',
+                'address': '192.168.168.30/24',
+                'ipv6': {}
+            }
+        }
+        assertEqual(sysconfig, expected)
+
+    def test_v1_confi_multi_route_multi_nic_ipv4(self):
+        v1_conf = '''
+auto lo
+iface lo inet loopback
+
+auto eth0
+iface eth0 inet static
+    hwaddress fa:16:3e:ee:2b:97
+    address 192.168.168.30/24
+    mtu 1500
+    post-up route add default gw 192.168.168.1 || true
+    pre-down route del default gw 192.168.168.1 || true
+
+auto eth1
+iface eth1 inet dhcp
+    post-up route add 192.168.168.1 || true
+'''
+        sysconfig = net_util.translate_network(v1_conf)
+        expected = {
+            'lo': {'auto': True, 'ipv6': {}},
+            'eth0': {
+                'auto': True,
+                'bootproto': 'static',
+                'gateway': '192.168.168.1',
+                'address': '192.168.168.30/24',
+                'ipv6': {}
+            }
+            'eth1': {
+                'auto': True,
+                'bootproto': 'dhcp',
+                 'ipv6': {}
+            }
+        }
+        assertEqual(sysconfig, expected)
+
+
+
+# vi: ts=4 expandtab

Follow ups