]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - debian/cloud-tools/hv_set_ifconfig
3 # set interfaces in hv_kvp_daemon style
12 if_filename
="/etc/network/interfaces"
15 sys
.stdout
= open(os
.devnull
, 'w')
16 sys
.stderr
= open(os
.devnull
, 'w')
19 if_file
=open(if_filename
,"r+")
27 return dict(line
.strip().split("=") for line
in file)
30 #setting the hwaddress to something azure is not expecting is fatal networking
32 if len(sys
.argv
) != 2 :
35 kvp
=dict(line
.strip().split("=") for line
in fileinput
.input())
37 if not "HWADDR" in kvp
:
40 if not "DEVICE" in kvp
:
44 basename
=kvp
["DEVICE"]
46 if "DHCP" in kvp
and kvp
["DHCP"]=="yes" :
47 output
+= ["auto " + basename
]
48 output
+= ["iface " + basename
+ " inet dhcp"]
51 ''' Matchup the interface specific lines '''
53 '''DNS entries will go with the first interface
54 and there can be a max of three'''
66 No real max for the number of interface + aliases ...
67 only required is the address (but mate everything up that comes in. '''
71 v4names
=[name
for name
in kvp
.keys() if name
.startswith("IPADDR")]
74 v6names
=[name
for name
in kvp
.keys() if name
.startswith("IPV6ADDR")]
77 '''IPV6 requires a netmask'''
78 '''If an ipv6 exists, you'll want to turn off /proc/sys/net/ipv6/conf/all/autoconf with
79 up echo 0 > /proc/sys/net/ipv6/conf/all/autoconf'''
81 '''Counter needs to increment as soon as any interface is set.'''
90 ifname
+=":" + str(if_count
)
91 suffix
="_"+str(if_count
)
92 if not ifname
in autolist
:
94 output
+= [ "iface " + ifname
+ " inet static"]
95 output
+= [ "\t" + "address " + kvp
[v4
]]
96 if "NETMASK"+suffix
in kvp
.keys():
97 output
+= ["\tnetmask " + kvp
["NETMASK"+suffix
]]
98 if "GATEWAY"+suffix
in kvp
.keys():
99 output
+= ["\tgateway " + kvp
["GATEWAY"+suffix
]]
101 output
+= ["\tdns-nameservers " + ' '.join(dns
)]
111 ifname
+=":" + str(if6_used
)
113 suffix
="_" + str(if6_count
)
114 if not ifname
in autolist
:
116 if "IPV6NETMASK"+suffix
in kvp
.keys():
117 output
+= [ "iface " + ifname
+ " inet6 static"]
118 output
+= [ "\taddress " + kvp
[v6
]]
119 output
+= [ "\tnetmask " + kvp
["IPV6NETMASK"+suffix
]]
120 if "IPV6_DEFAULTGW"+suffix
in kvp
.keys():
121 output
+= [ "\tgateway " + kvp
["IPV6_DEFAULTGW"+suffix
] ]
123 output
+= ["\tdns-nameservers " + ' '.join(dns
)]
129 output
= ["auto "+" ".join(autolist
)] + output
130 output
=["# The following stanza(s) added by hv_set_ifconfig"] + output
131 output
+=["#End of hv_set_ifconfig stanzas"]
132 print "==================================="
134 print "==================================="
137 ''' Time to clean out the existing interface file'''
139 f
=open(if_filename
,"r")
147 if line
.startswith("auto"):
150 newfile
.extend(stanza
)
154 autoline
=line
.strip().split(" ")
155 for word
in autoline
:
156 if (not word
== basename
) and (not word
.startswith(basename
+":")):
158 newline
= newline
.strip()
159 if not newline
== "auto":
160 newfile
+= [newline
.strip()]
161 elif line
.startswith(("iface","mapping","source")):
163 '''A Stanza can also start with allow- ie allow-hotplug'''
166 newfile
.extend(stanza
)
170 autoline
=line
.strip().split(" ")
171 for word
in autoline
:
172 if (word
== basename
) or (word
.startswith(basename
+":")):
175 stanza
+=[line
.strip()]
179 stanza
+=[line
.strip()]
182 newfile
+= [line
.strip()]
191 fd
, path
= tempfile
.mkstemp()
200 shutil
.copy(path
,if_filename
)
201 os
.chmod(if_filename
,0644)
202 #print "TMPFILE is at: " + path
203 #print "Copied file is at: " + if_filename
207 retcode
= subprocess
.call("ifdown "+basename
, shell
=True)
209 print >>sys
.stderr
, "Child was terminated by signal", -retcode
211 print >>sys
.stderr
, "Child returned", retcode
213 print >>sys
.stderr
, "Execution failed:", e
216 retcode
= subprocess
.call("ifup "+basename
, shell
=True)
218 print >>sys
.stderr
, "Child was terminated by signal", -retcode
220 print >>sys
.stderr
, "Child returned", retcode
222 print >>sys
.stderr
, "Execution failed:", e