[trusty/utopic 1/1] UBUNTU: hyper-v -- fix comment handing in /etc/network/interfaces

Andy Whitcroft apw at canonical.com
Sun Feb 1 09:57:53 UTC 2015


We are duplicating the opening comment marker every time we rebuild the
file, such that we end up with multiple of those comments:

    # This file describes the network interfaces available on your system
    # and how to activate them. For more information, see interfaces(5).

    # The loopback network interface
    auto lo
    iface lo inet loopback

    # The primary network interface
    # The following stanza(s) added by hv_set_ifconfig
    # The following stanza(s) added by hv_set_ifconfig
    # The following stanza(s) added by hv_set_ifconfig
    auto eth0
    iface eth0 inet static
	    address 10.100.20.108
	    gateway 10.100.20.1
	    dns-nameservers 8.8.4.4

    #End of hv_set_ifconfig stanzas

Fix handling of these such that we only insert new markers if they do
not already exist.  Where they do, simply inject the new stanzas at the
end of the block before the end marker.  At the same time deduplicate
sequential begin and end markers to clean up previously dammaged files.

BugLink: http://bugs.launchpad.net/bugs/1413020
Signed-off-by: Andy Whitcroft <apw at canonical.com>
---
 debian/cloud-tools/hv_set_ifconfig | 40 +++++++++++++++++++++++++++-----------
 1 file changed, 29 insertions(+), 11 deletions(-)

diff --git a/debian/cloud-tools/hv_set_ifconfig b/debian/cloud-tools/hv_set_ifconfig
index b351776..783a150 100755
--- a/debian/cloud-tools/hv_set_ifconfig
+++ b/debian/cloud-tools/hv_set_ifconfig
@@ -127,8 +127,6 @@ else:
 		if6_count += 1
 
 	output = ["auto "+" ".join(autolist)] + output
-output=["# The following stanza(s) added by hv_set_ifconfig"] + output
-output+=["#End of hv_set_ifconfig stanzas"]
 print "==================================="
 print output
 print "==================================="
@@ -136,6 +134,10 @@ print "==================================="
 
 ''' Time to clean out the existing interface file'''
 
+# Markers.
+start_mark = "# The following stanza(s) added by hv_set_ifconfig"
+end_mark = "#End of hv_set_ifconfig stanzas"
+
 f=open(if_filename,"r")
 flines=f.readlines()
 f.close()
@@ -143,6 +145,7 @@ newfile=[]
 pitchstanza=0
 inastanza=0
 stanza=[]
+prev_line=None
 for line in flines:
 	if line.startswith("auto"):
 		if inastanza:
@@ -173,6 +176,16 @@ for line in flines:
 				pitchstanza=1
 		if not pitchstanza:
 			stanza+=[line.strip()]
+        elif line.strip() in (start_mark, end_mark):
+		if inastanza:
+			if not pitchstanza:
+				newfile.extend(stanza)
+			stanza=[]
+                inastanza = 0
+                pitchstanza = 0
+                # Deduplicate markers.
+                if line != prev_line:
+                    newfile += [line.strip()]
 	else:
 		if inastanza:
 			if not pitchstanza:
@@ -180,21 +193,26 @@ for line in flines:
 		else:
 			if not pitchstanza:
 				newfile += [line.strip()]
+        prev_line=line
 
 
-for line in newfile:
-	print line
-for line in output:
-	print line
 
+def emit(line):
+        print(line)
+	os.write(fd, line + "\n")
 
+# Insert the new output at the end and inside the existing markers if found.
+emitted = False
 fd, path = tempfile.mkstemp()
 for line in newfile:
-	os.write(fd,line)
-	os.write(fd,"\n")
-for line in output:
-	os.write(fd,line)
-	os.write(fd,"\n")
+        if line == end_mark:
+                emit("\n".join(output))
+                emitted = True
+        emit(line)
+if not emitted:
+        emit(start_mark)
+        emit("\n".join(output))
+        emit(end_mark)
 os.close(fd)
 
 shutil.copy(path,if_filename)
-- 
2.1.4





More information about the kernel-team mailing list