c6626f
diff -rup cloud-init-17.1.orig/cloudinit/net/sysconfig.py cloud-init-17.1/cloudinit/net/sysconfig.py
c6626f
--- cloud-init-17.1.orig/cloudinit/net/sysconfig.py	2018-03-26 19:22:35.693111559 +0200
c6626f
+++ cloud-init-17.1/cloudinit/net/sysconfig.py	2018-03-26 23:47:41.424803588 +0200
c6626f
@@ -586,7 +586,17 @@ class Renderer(renderer.Renderer):
c6626f
 
c6626f
         # always write /etc/sysconfig/network configuration
c6626f
         sysconfig_path = util.target_path(target, "etc/sysconfig/network")
c6626f
-        netcfg = [_make_header(), 'NETWORKING=yes']
c6626f
+        # Make sure that existing lines, other than overriding ones, remain
c6626f
+        netcfg = []
c6626f
+        for line in util.load_file(sysconfig_path, quiet=True).split('\n'):
c6626f
+            if 'cloud-init' in line:
c6626f
+                break
c6626f
+            if not line.startswith(('NETWORKING=',
c6626f
+                                    'IPV6_AUTOCONF=',
c6626f
+                                    'NETWORKING_IPV6=')):
c6626f
+                netcfg.append(line)
c6626f
+        # Now generate the cloud-init portion of sysconfig/network
c6626f
+        netcfg.extend([_make_header(), 'NETWORKING=yes'])
c6626f
         if network_state.use_ipv6:
c6626f
             netcfg.append('NETWORKING_IPV6=yes')
c6626f
             netcfg.append('IPV6_AUTOCONF=no')
c6626f
diff -rup cloud-init-17.1.orig/tests/unittests/test_distros/test_netconfig.py cloud-init-17.1/tests/unittests/test_distros/test_netconfig.py
c6626f
--- cloud-init-17.1.orig/tests/unittests/test_distros/test_netconfig.py	2018-03-26 19:22:35.717111557 +0200
c6626f
+++ cloud-init-17.1/tests/unittests/test_distros/test_netconfig.py	2018-03-26 22:08:25.008717651 +0200
c6626f
@@ -384,6 +384,82 @@ hn0: flags=8843
c6626f
             buf.write(content)
c6626f
             write_bufs[filename] = buf
c6626f
 
c6626f
+        def replace_load_file(filename, *args, **kwargs):
c6626f
+            if filename == '/etc/sysconfig/network':
c6626f
+                return 'TEST=yes\nTEST2=yes'
c6626f
+            else:
c6626f
+                return ''
c6626f
+
c6626f
+        with ExitStack() as mocks:
c6626f
+            mocks.enter_context(
c6626f
+                mock.patch.object(util, 'write_file', replace_write))
c6626f
+            mocks.enter_context(
c6626f
+                mock.patch.object(util, 'load_file', replace_load_file))
c6626f
+            mocks.enter_context(
c6626f
+                mock.patch.object(os.path, 'isfile', return_value=False))
c6626f
+
c6626f
+            rh_distro.apply_network(BASE_NET_CFG, False)
c6626f
+
c6626f
+            self.assertEqual(len(write_bufs), 4)
c6626f
+            self.assertIn('/etc/sysconfig/network-scripts/ifcfg-lo',
c6626f
+                          write_bufs)
c6626f
+            write_buf = write_bufs['/etc/sysconfig/network-scripts/ifcfg-lo']
c6626f
+            expected_buf = '''
c6626f
+DEVICE="lo"
c6626f
+ONBOOT=yes
c6626f
+'''
c6626f
+            self.assertCfgEquals(expected_buf, str(write_buf))
c6626f
+            self.assertEqual(write_buf.mode, 0o644)
c6626f
+
c6626f
+            self.assertIn('/etc/sysconfig/network-scripts/ifcfg-eth0',
c6626f
+                          write_bufs)
c6626f
+            write_buf = write_bufs['/etc/sysconfig/network-scripts/ifcfg-eth0']
c6626f
+            expected_buf = '''
c6626f
+DEVICE="eth0"
c6626f
+BOOTPROTO="static"
c6626f
+NETMASK="255.255.255.0"
c6626f
+IPADDR="192.168.1.5"
c6626f
+ONBOOT=yes
c6626f
+GATEWAY="192.168.1.254"
c6626f
+BROADCAST="192.168.1.0"
c6626f
+'''
c6626f
+            self.assertCfgEquals(expected_buf, str(write_buf))
c6626f
+            self.assertEqual(write_buf.mode, 0o644)
c6626f
+
c6626f
+            self.assertIn('/etc/sysconfig/network-scripts/ifcfg-eth1',
c6626f
+                          write_bufs)
c6626f
+            write_buf = write_bufs['/etc/sysconfig/network-scripts/ifcfg-eth1']
c6626f
+            expected_buf = '''
c6626f
+DEVICE="eth1"
c6626f
+BOOTPROTO="dhcp"
c6626f
+ONBOOT=yes
c6626f
+'''
c6626f
+            self.assertCfgEquals(expected_buf, str(write_buf))
c6626f
+            self.assertEqual(write_buf.mode, 0o644)
c6626f
+
c6626f
+            self.assertIn('/etc/sysconfig/network', write_bufs)
c6626f
+            write_buf = write_bufs['/etc/sysconfig/network']
c6626f
+            expected_buf = '''
c6626f
+# Created by cloud-init v. 0.7
c6626f
+NETWORKING=yes
c6626f
+TEST=yes
c6626f
+TEST2=yes
c6626f
+'''
c6626f
+            self.assertCfgEquals(expected_buf, str(write_buf))
c6626f
+            self.assertEqual(write_buf.mode, 0o644)
c6626f
+
c6626f
+    def test_simple_write_rh_no_extra(self):
c6626f
+        rh_distro = self._get_distro('rhel')
c6626f
+
c6626f
+        write_bufs = {}
c6626f
+
c6626f
+        def replace_write(filename, content, mode=0o644, omode="wb"):
c6626f
+            buf = WriteBuffer()
c6626f
+            buf.mode = mode
c6626f
+            buf.omode = omode
c6626f
+            buf.write(content)
c6626f
+            write_bufs[filename] = buf
c6626f
+
c6626f
         with ExitStack() as mocks:
c6626f
             mocks.enter_context(
c6626f
                 mock.patch.object(util, 'write_file', replace_write))
c6626f
@@ -453,6 +529,12 @@ NETWORKING=yes
c6626f
             buf.write(content)
c6626f
             write_bufs[filename] = buf
c6626f
 
c6626f
+        def replace_load_file(filename, *args, **kwargs):
c6626f
+            if filename == '/etc/sysconfig/network':
c6626f
+                return 'TEST=yes\nTEST2=yes'
c6626f
+            else:
c6626f
+                return ''
c6626f
+
c6626f
         with ExitStack() as mocks:
c6626f
             # sysconfig availability checks
c6626f
             mocks.enter_context(
c6626f
@@ -460,7 +542,7 @@ NETWORKING=yes
c6626f
             mocks.enter_context(
c6626f
                 mock.patch.object(util, 'write_file', replace_write))
c6626f
             mocks.enter_context(
c6626f
-                mock.patch.object(util, 'load_file', return_value=''))
c6626f
+                mock.patch.object(util, 'load_file', replace_load_file))
c6626f
             mocks.enter_context(
c6626f
                 mock.patch.object(os.path, 'isfile', return_value=True))
c6626f
 
c6626f
@@ -509,6 +591,8 @@ USERCTL=no
c6626f
             expected_buf = '''
c6626f
 # Created by cloud-init v. 0.7
c6626f
 NETWORKING=yes
c6626f
+TEST=yes
c6626f
+TEST2=yes
c6626f
 '''
c6626f
             self.assertCfgEquals(expected_buf, str(write_buf))
c6626f
             self.assertEqual(write_buf.mode, 0o644)
c6626f
@@ -525,13 +609,20 @@ NETWORKING=yes
c6626f
             buf.write(content)
c6626f
             write_bufs[filename] = buf
c6626f
 
c6626f
+        def replace_load_file(filename, *args, **kwargs):
c6626f
+            if filename == '/etc/sysconfig/network':
c6626f
+                return 'TEST=yes\nTEST2=yes'
c6626f
+            else:
c6626f
+                return ''
c6626f
+
c6626f
         with ExitStack() as mocks:
c6626f
             mocks.enter_context(
c6626f
                 mock.patch.object(util, 'write_file', replace_write))
c6626f
             mocks.enter_context(
c6626f
-                mock.patch.object(util, 'load_file', return_value=''))
c6626f
+                mock.patch.object(util, 'load_file', replace_load_file))
c6626f
             mocks.enter_context(
c6626f
                 mock.patch.object(os.path, 'isfile', return_value=False))
c6626f
+
c6626f
             rh_distro.apply_network(BASE_NET_CFG_IPV6, False)
c6626f
 
c6626f
             self.assertEqual(len(write_bufs), 4)
c6626f
@@ -587,6 +678,8 @@ IPV6_DEFAULTGW="2607:f0d0:1002:0011::1"
c6626f
 NETWORKING=yes
c6626f
 NETWORKING_IPV6=yes
c6626f
 IPV6_AUTOCONF=no
c6626f
+TEST=yes
c6626f
+TEST2=yes
c6626f
 '''
c6626f
             self.assertCfgEquals(expected_buf, str(write_buf))
c6626f
             self.assertEqual(write_buf.mode, 0o644)
c6626f
@@ -604,13 +697,19 @@ IPV6_AUTOCONF=no
c6626f
             buf.write(content)
c6626f
             write_bufs[filename] = buf
c6626f
 
c6626f
+        def replace_load_file(filename, *args, **kwargs):
c6626f
+            if filename == '/etc/sysconfig/network':
c6626f
+                return 'TEST=yes\nTEST2=yes'
c6626f
+            else:
c6626f
+                return ''
c6626f
+
c6626f
         with ExitStack() as mocks:
c6626f
             mocks.enter_context(
c6626f
                 mock.patch.object(util, 'which', return_value=True))
c6626f
             mocks.enter_context(
c6626f
                 mock.patch.object(util, 'write_file', replace_write))
c6626f
             mocks.enter_context(
c6626f
-                mock.patch.object(util, 'load_file', return_value=''))
c6626f
+                mock.patch.object(util, 'load_file', replace_load_file))
c6626f
             mocks.enter_context(
c6626f
                 mock.patch.object(os.path, 'isfile', return_value=True))
c6626f
 
c6626f
@@ -658,6 +757,8 @@ USERCTL=no
c6626f
 NETWORKING=yes
c6626f
 NETWORKING_IPV6=yes
c6626f
 IPV6_AUTOCONF=no
c6626f
+TEST=yes
c6626f
+TEST2=yes
c6626f
 '''
c6626f
             self.assertCfgEquals(expected_buf, str(write_buf))
c6626f
             self.assertEqual(write_buf.mode, 0o644)