diff mbox

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

Message ID 1422784673-14136-2-git-send-email-apw@canonical.com
State New
Headers show

Commit Message

Andy Whitcroft Feb. 1, 2015, 9:57 a.m. UTC
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@canonical.com>
---
 debian/cloud-tools/hv_set_ifconfig | 40 +++++++++++++++++++++++++++-----------
 1 file changed, 29 insertions(+), 11 deletions(-)

Comments

Stefan Bader Feb. 2, 2015, 9:28 a.m. UTC | #1
I trust you to have had that tested and if things go wrong despite its a limited
target.
Brad Figg Feb. 2, 2015, 6:23 p.m. UTC | #2
On Sun, Feb 01, 2015 at 11:57:53AM +0200, Andy Whitcroft wrote:
> 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@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
> 
> 
> -- 
> kernel-team mailing list
> kernel-team@lists.ubuntu.com
> https://lists.ubuntu.com/mailman/listinfo/kernel-team

Looks fine.
Brad Figg Feb. 2, 2015, 6:35 p.m. UTC | #3
On Sun, Feb 01, 2015 at 11:57:53AM +0200, Andy Whitcroft wrote:
> 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@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
> 
> 
> -- 
> kernel-team mailing list
> kernel-team@lists.ubuntu.com
> https://lists.ubuntu.com/mailman/listinfo/kernel-team

Applied to Trusty and Utopic master-next branches.
diff mbox

Patch

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)