Commit 6b4d4038 authored by Andy Whitcroft's avatar Andy Whitcroft Committed by Tim Gardner

UBUNTU: [Debian] hv: hv_set_ifconfig -- switch to approved indentation

BugLink: http://bugs.launchpad.net/bugs/1540586Signed-off-by: default avatarAndy Whitcroft <apw@canonical.com>
parent a9183910
...@@ -37,15 +37,15 @@ sys.stderr = open(os.devnull, 'w') ...@@ -37,15 +37,15 @@ sys.stderr = open(os.devnull, 'w')
# Confirm we can open the network configuration. # Confirm we can open the network configuration.
try: try:
if_file=open(if_filename,"r+") if_file=open(if_filename,"r+")
except IOError as e: except IOError as e:
exit(e.errno) exit(e.errno)
else: else:
if_file.close() if_file.close()
# Usage: hv_set_ifconfig <config> # Usage: hv_set_ifconfig <config>
if len(sys.argv) != 2 : if len(sys.argv) != 2 :
exit(errno.EINVAL) exit(errno.EINVAL)
# #
# Here is the format of the ip configuration file: # Here is the format of the ip configuration file:
...@@ -60,94 +60,94 @@ kvp=dict(line.strip().split("=") for line in fileinput.input()) ...@@ -60,94 +60,94 @@ kvp=dict(line.strip().split("=") for line in fileinput.input())
# Setting the hwaddress to something azure is not expecting is fatal # Setting the hwaddress to something azure is not expecting is fatal
# to networking. # to networking.
if not "HWADDR" in kvp : if not "HWADDR" in kvp :
exit(errno.EPROTO) exit(errno.EPROTO)
# Confirm we have a device specified. # Confirm we have a device specified.
if not "DEVICE" in kvp : if not "DEVICE" in kvp :
exit(1) exit(1)
output=[] output=[]
basename=kvp["DEVICE"] basename=kvp["DEVICE"]
if "DHCP" in kvp and kvp["DHCP"]=="yes" : if "DHCP" in kvp and kvp["DHCP"]=="yes" :
output += ["auto " + basename] output += ["auto " + basename]
output += ["iface " + basename + " inet dhcp"] output += ["iface " + basename + " inet dhcp"]
output += [""] output += [""]
else: else:
# Matchup the interface specific lines # Matchup the interface specific lines
# DNS entries will go with the first interface # DNS entries will go with the first interface
# and there can be a max of three # and there can be a max of three
autolist=[] autolist=[]
dns=[] dns=[]
if "DNS1" in kvp : if "DNS1" in kvp :
dns+=[kvp["DNS1"]] dns+=[kvp["DNS1"]]
if "DNS2" in kvp : if "DNS2" in kvp :
dns+=[kvp["DNS2"]] dns+=[kvp["DNS2"]]
if "DNS3" in kvp : if "DNS3" in kvp :
dns+=[kvp["DNS3"]] dns+=[kvp["DNS3"]]
# No real max for the number of interface + aliases ... # No real max for the number of interface + aliases ...
# only required is the address (but mate everything up that comes in. # only required is the address (but mate everything up that comes in.
# IPv4 # IPv4
v4names=[name for name in kvp.keys() if name.startswith("IPADDR")] v4names=[name for name in kvp.keys() if name.startswith("IPADDR")]
v4names.sort() v4names.sort()
if_count=0 if_count=0
for v4 in v4names: for v4 in v4names:
ifname=basename ifname=basename
suffix="" suffix=""
if if_count : if if_count :
ifname+=":" + str(if_count) ifname+=":" + str(if_count)
suffix="_"+str(if_count) suffix="_"+str(if_count)
if not ifname in autolist: if not ifname in autolist:
autolist += [ifname] autolist += [ifname]
output += [ "iface " + ifname + " inet static"] output += [ "iface " + ifname + " inet static"]
output += [ "\t" + "address " + kvp[v4]] output += [ "\t" + "address " + kvp[v4]]
if "NETMASK"+suffix in kvp.keys(): if "NETMASK"+suffix in kvp.keys():
output += ["\tnetmask " + kvp["NETMASK"+suffix]] output += ["\tnetmask " + kvp["NETMASK"+suffix]]
if "GATEWAY"+suffix in kvp.keys(): if "GATEWAY"+suffix in kvp.keys():
output += ["\tgateway " + kvp["GATEWAY"+suffix]] output += ["\tgateway " + kvp["GATEWAY"+suffix]]
if not if_count : if not if_count :
output += ["\tdns-nameservers " + ' '.join(dns)] output += ["\tdns-nameservers " + ' '.join(dns)]
output += [""] output += [""]
if_count+=1 if_count+=1
# IPv6 requires a netmask # IPv6 requires a netmask
# If an ipv6 exists, you'll want to turn off /proc/sys/net/ipv6/conf/all/autoconf with # If an ipv6 exists, you'll want to turn off /proc/sys/net/ipv6/conf/all/autoconf with
# echo 0 > /proc/sys/net/ipv6/conf/all/autoconf # echo 0 > /proc/sys/net/ipv6/conf/all/autoconf
v6names=[name for name in kvp.keys() if name.startswith("IPV6ADDR")] v6names=[name for name in kvp.keys() if name.startswith("IPV6ADDR")]
v6names.sort() v6names.sort()
if6_count=0 if6_count=0
if6_used=0 if6_used=0
for v6 in v6names: for v6 in v6names:
ifname=basename ifname=basename
suffix="" suffix=""
if if6_used : if if6_used :
ifname+=":" + str(if6_used) ifname+=":" + str(if6_used)
if if6_count : if if6_count :
suffix="_" + str(if6_count) suffix="_" + str(if6_count)
if not ifname in autolist: if not ifname in autolist:
autolist += [ifname] autolist += [ifname]
if "IPV6NETMASK"+suffix in kvp.keys(): if "IPV6NETMASK"+suffix in kvp.keys():
output += [ "iface " + ifname + " inet6 static"] output += [ "iface " + ifname + " inet6 static"]
output += [ "\taddress " + kvp[v6]] output += [ "\taddress " + kvp[v6]]
output += [ "\tnetmask " + kvp["IPV6NETMASK"+suffix]] output += [ "\tnetmask " + kvp["IPV6NETMASK"+suffix]]
if "IPV6_DEFAULTGW"+suffix in kvp.keys(): if "IPV6_DEFAULTGW"+suffix in kvp.keys():
output += [ "\tgateway " + kvp["IPV6_DEFAULTGW"+suffix] ] output += [ "\tgateway " + kvp["IPV6_DEFAULTGW"+suffix] ]
if not if_count : if not if_count :
output += ["\tdns-nameservers " + ' '.join(dns)] output += ["\tdns-nameservers " + ' '.join(dns)]
output += [""] output += [""]
if_count += 1 if_count += 1
if6_used += 1 if6_used += 1
if6_count += 1 if6_count += 1
# Mark this new interface for automatic up. # Mark this new interface for automatic up.
output = ["auto "+" ".join(autolist)] + output output = ["auto "+" ".join(autolist)] + output
print("===================================") print("===================================")
print(output) print(output)
...@@ -169,73 +169,73 @@ inastanza=0 ...@@ -169,73 +169,73 @@ inastanza=0
stanza=[] stanza=[]
prev_line=None prev_line=None
for line in flines: for line in flines:
if line.startswith("auto"): if line.startswith("auto"):
if inastanza: if inastanza:
if not pitchstanza: if not pitchstanza:
newfile.extend(stanza) newfile.extend(stanza)
stanza=[] stanza=[]
inastanza=0 inastanza=0
newline="" newline=""
autoline=line.strip().split(" ") autoline=line.strip().split(" ")
for word in autoline: for word in autoline:
if (not word == basename) and (not word.startswith(basename+":")): if (not word == basename) and (not word.startswith(basename+":")):
newline+=word + " " newline+=word + " "
newline = newline.strip() newline = newline.strip()
if not newline == "auto": if not newline == "auto":
newfile += [newline.strip()] newfile += [newline.strip()]
elif line.startswith(("iface","mapping","source")): elif line.startswith(("iface","mapping","source")):
'''Read a stanza''' '''Read a stanza'''
'''A Stanza can also start with allow- ie allow-hotplug''' '''A Stanza can also start with allow- ie allow-hotplug'''
if inastanza: if inastanza:
if not pitchstanza: if not pitchstanza:
newfile.extend(stanza) newfile.extend(stanza)
stanza=[] stanza=[]
inastanza=1 inastanza=1
pitchstanza=0 pitchstanza=0
autoline=line.strip().split(" ") autoline=line.strip().split(" ")
for word in autoline: for word in autoline:
if (word == basename) or (word.startswith(basename+":")): if (word == basename) or (word.startswith(basename+":")):
pitchstanza=1 pitchstanza=1
if not pitchstanza: if not pitchstanza:
stanza+=[line.strip()] stanza+=[line.strip()]
elif line.strip() in (start_mark, end_mark): elif line.strip() in (start_mark, end_mark):
if inastanza: if inastanza:
if not pitchstanza: if not pitchstanza:
newfile.extend(stanza) newfile.extend(stanza)
stanza=[] stanza=[]
inastanza = 0 inastanza = 0
pitchstanza = 0 pitchstanza = 0
# Deduplicate markers. # Deduplicate markers.
if line != prev_line: if line != prev_line:
newfile += [line.strip()] newfile += [line.strip()]
else: else:
if inastanza: if inastanza:
if not pitchstanza: if not pitchstanza:
stanza+=[line.strip()] stanza+=[line.strip()]
else: else:
if not pitchstanza: if not pitchstanza:
newfile += [line.strip()] newfile += [line.strip()]
prev_line=line prev_line=line
def emit(line): def emit(line):
print(line) print(line)
output = line + "\n" output = line + "\n"
os.write(fd, output.encode('utf-8')) os.write(fd, output.encode('utf-8'))
# Insert the new output at the end and inside the existing markers if found. # Insert the new output at the end and inside the existing markers if found.
emitted = False emitted = False
fd, path = tempfile.mkstemp() fd, path = tempfile.mkstemp()
for line in newfile: for line in newfile:
if line == end_mark: if line == end_mark:
emit("\n".join(output)) emit("\n".join(output))
emitted = True emitted = True
emit(line) emit(line)
if not emitted: if not emitted:
emit(start_mark) emit(start_mark)
emit("\n".join(output)) emit("\n".join(output))
emit(end_mark) emit(end_mark)
os.close(fd) os.close(fd)
shutil.copy(path,if_filename) 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