]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - debian/cloud-tools/hv_set_ifconfig
3 # hv_set_ifconfig <config> -- take the hv_kvp_daemon generated configuration
4 # file and apply it to the Ubuntu configuration.
8 # HWADDR=11:22:33:44:55:66
13 # HWADDR=11:22:33:44:55:66
15 # IPADDR=192.168.99.10
16 # GATEWAY=192.168.99.1
18 # IPADDR2=192.168.99.11
19 # IPV6ADDR=2001:DB8:99::10
21 # IPV6_DEFAULTGW=2001:DB8:99::10
23 # set interfaces in hv_kvp_daemon style
32 if_filename
="/etc/network/interfaces"
34 # Drop our output (XXX?)
35 sys
.stdout
= open(os
.devnull
, 'w')
36 sys
.stderr
= open(os
.devnull
, 'w')
38 # Confirm we can open the network configuration.
40 if_file
=open(if_filename
,"r+")
46 # Usage: hv_set_ifconfig <config>
47 if len(sys
.argv
) != 2 :
51 # Here is the format of the ip configuration file:
54 # DEVICE=interface name
55 # BOOTPROTO=<protocol> (where <protocol> is "dhcp" if DHCP is configured
56 # or "none" if no boot-time protocol should be used)
58 kvp
=dict(line
.strip().split("=") for line
in fileinput
.input())
60 # Setting the hwaddress to something azure is not expecting is fatal
62 if not "HWADDR" in kvp
:
65 # Confirm we have a device specified.
66 if not "DEVICE" in kvp
:
70 basename
=kvp
["DEVICE"]
72 if "DHCP" in kvp
and kvp
["DHCP"]=="yes" :
73 output
+= ["auto " + basename
]
74 output
+= ["iface " + basename
+ " inet dhcp"]
77 # Matchup the interface specific lines
79 # DNS entries will go with the first interface
80 # and there can be a max of three
91 # No real max for the number of interface + aliases ...
92 # only required is the address (but mate everything up that comes in.
95 v4names
=[name
for name
in kvp
.keys() if name
.startswith("IPADDR")]
103 ifname
+=":" + str(if_count
)
104 suffix
="_"+str(if_count
)
105 if not ifname
in autolist
:
107 output
+= [ "iface " + ifname
+ " inet static"]
108 output
+= [ "\t" + "address " + kvp
[v4
]]
109 if "NETMASK"+suffix
in kvp
.keys():
110 output
+= ["\tnetmask " + kvp
["NETMASK"+suffix
]]
111 if "GATEWAY"+suffix
in kvp
.keys():
112 output
+= ["\tgateway " + kvp
["GATEWAY"+suffix
]]
114 output
+= ["\tdns-nameservers " + ' '.join(dns
)]
119 # IPv6 requires a netmask
120 # If an ipv6 exists, you'll want to turn off /proc/sys/net/ipv6/conf/all/autoconf with
121 # echo 0 > /proc/sys/net/ipv6/conf/all/autoconf
122 v6names
=[name
for name
in kvp
.keys() if name
.startswith("IPV6ADDR")]
131 ifname
+=":" + str(if6_used
)
133 suffix
="_" + str(if6_count
)
134 if not ifname
in autolist
:
136 if "IPV6NETMASK"+suffix
in kvp
.keys():
137 output
+= [ "iface " + ifname
+ " inet6 static"]
138 output
+= [ "\taddress " + kvp
[v6
]]
139 output
+= [ "\tnetmask " + kvp
["IPV6NETMASK"+suffix
]]
140 if "IPV6_DEFAULTGW"+suffix
in kvp
.keys():
141 output
+= [ "\tgateway " + kvp
["IPV6_DEFAULTGW"+suffix
] ]
143 output
+= ["\tdns-nameservers " + ' '.join(dns
)]
149 # Mark this new interface for automatic up.
150 output
= ["auto "+" ".join(autolist
)] + output
152 print("===================================")
154 print("===================================")
157 ''' Time to clean out the existing interface file'''
160 start_mark
= "# The following stanza(s) added by hv_set_ifconfig"
161 end_mark
= "#End of hv_set_ifconfig stanzas"
163 f
=open(if_filename
,"r")
172 if line
.startswith("auto"):
175 newfile
.extend(stanza
)
179 autoline
=line
.strip().split(" ")
180 for word
in autoline
:
181 if (not word
== basename
) and (not word
.startswith(basename
+":")):
183 newline
= newline
.strip()
184 if not newline
== "auto":
185 newfile
+= [newline
.strip()]
186 elif line
.startswith(("iface","mapping","source")):
188 '''A Stanza can also start with allow- ie allow-hotplug'''
191 newfile
.extend(stanza
)
195 autoline
=line
.strip().split(" ")
196 for word
in autoline
:
197 if (word
== basename
) or (word
.startswith(basename
+":")):
200 stanza
+=[line
.strip()]
201 elif line
.strip() in (start_mark
, end_mark
):
204 newfile
.extend(stanza
)
208 # Deduplicate markers.
209 if line
!= prev_line
:
210 newfile
+= [line
.strip()]
214 stanza
+=[line
.strip()]
217 newfile
+= [line
.strip()]
225 os
.write(fd
, output
.encode('utf-8'))
227 # Insert the new output at the end and inside the existing markers if found.
229 fd
, path
= tempfile
.mkstemp()
232 emit("\n".join(output
))
237 emit("\n".join(output
))
241 shutil
.copy(path
,if_filename
)
242 os
.chmod(if_filename
,0o644)
244 #print("TMPFILE is at: " + path)
245 #print("Copied file is at: " + if_filename)
248 retcode
= subprocess
.call("ifdown "+basename
, shell
=True)
250 print("Child was terminated by signal", -retcode
, file=sys
.stderr
)
252 print("Child returned", retcode
, file=sys
.stderr
)
254 print("Execution failed:", e
, file=sys
.stderr
)
257 retcode
= subprocess
.call("ifup "+basename
, shell
=True)
259 print("Child was terminated by signal", -retcode
, file=sys
.stderr
)
261 print("Child returned", retcode
, file=sys
.stderr
)
263 print("Execution failed:", e
, file=sys
.stderr
)