Commit 5795046f authored by Andy Whitcroft's avatar Andy Whitcroft Committed by Tim Gardner

UBUNTU: [Debian] hyper-v -- fix comment handing in /etc/network/interfaces

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/1413020Signed-off-by: default avatarAndy Whitcroft <apw@canonical.com>
parent 7f4797f4
......@@ -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)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment