]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - debian/cloud-tools/hv_set_ifconfig
UBUNTU: [Debian] Cloud tools
[mirror_ubuntu-zesty-kernel.git] / debian / cloud-tools / hv_set_ifconfig
1 #! /usr/bin/env python
2
3 # set interfaces in hv_kvp_daemon style
4 import fileinput
5 import sys
6 import errno
7 import os
8 import shutil
9 import tempfile
10 import subprocess
11
12 if_filename="/etc/network/interfaces"
13
14 '''Get quiet'''
15 sys.stdout = open(os.devnull, 'w')
16 sys.stderr = open(os.devnull, 'w')
17
18 try:
19 if_file=open(if_filename,"r+")
20 except IOError as e:
21 exit(e.errno)
22 else:
23 if_file.close()
24
25
26 def kvp_dict(file):
27 return dict(line.strip().split("=") for line in file)
28
29
30 #setting the hwaddress to something azure is not expecting is fatal networking
31
32 if len(sys.argv) != 2 :
33 exit(errno.EINVAL)
34
35 kvp=dict(line.strip().split("=") for line in fileinput.input())
36
37 if not "HWADDR" in kvp :
38 exit(errno.EPROTO)
39
40 if not "DEVICE" in kvp :
41 exit(1)
42
43 output=[]
44 basename=kvp["DEVICE"]
45
46 if "DHCP" in kvp and kvp["DHCP"]=="yes" :
47 output += ["auto " + basename]
48 output += ["iface " + basename + " inet dhcp"]
49 output += [""]
50 else:
51 ''' Matchup the interface specific lines '''
52
53 '''DNS entries will go with the first interface
54 and there can be a max of three'''
55 autolist=[]
56 dns=[]
57 if "DNS1" in kvp :
58 dns+=[kvp["DNS1"]]
59 if "DNS2" in kvp :
60 dns+=[kvp["DNS2"]]
61 if "DNS3" in kvp :
62 dns+=[kvp["DNS3"]]
63
64
65 '''
66 No real max for the number of interface + aliases ...
67 only required is the address (but mate everything up that comes in. '''
68
69 '''ipv4 first'''
70
71 v4names=[name for name in kvp.keys() if name.startswith("IPADDR")]
72 v4names.sort()
73
74 v6names=[name for name in kvp.keys() if name.startswith("IPV6ADDR")]
75 v6names.sort()
76
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'''
80
81 '''Counter needs to increment as soon as any interface is set.'''
82
83
84 if_count=0
85
86 for v4 in v4names:
87 ifname=basename
88 suffix=""
89 if if_count :
90 ifname+=":" + str(if_count)
91 suffix="_"+str(if_count)
92 if not ifname in autolist:
93 autolist += [ifname]
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]]
100 if not if_count :
101 output += ["\tdns-nameservers " + ' '.join(dns)]
102 output += [""]
103 if_count+=1
104
105 if6_count=0
106 if6_used=0
107 for v6 in v6names:
108 ifname=basename
109 suffix=""
110 if if6_used :
111 ifname+=":" + str(if6_used)
112 if if6_count :
113 suffix="_" + str(if6_count)
114 if not ifname in autolist:
115 autolist += [ifname]
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] ]
122 if not if_count :
123 output += ["\tdns-nameservers " + ' '.join(dns)]
124 output += [""]
125 if_count += 1
126 if6_used += 1
127 if6_count += 1
128
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 "==================================="
133 print output
134 print "==================================="
135
136
137 ''' Time to clean out the existing interface file'''
138
139 f=open(if_filename,"r")
140 flines=f.readlines()
141 f.close()
142 newfile=[]
143 pitchstanza=0
144 inastanza=0
145 stanza=[]
146 for line in flines:
147 if line.startswith("auto"):
148 if inastanza:
149 if not pitchstanza:
150 newfile.extend(stanza)
151 stanza=[]
152 inastanza=0
153 newline=""
154 autoline=line.strip().split(" ")
155 for word in autoline:
156 if (not word == basename) and (not word.startswith(basename+":")):
157 newline+=word + " "
158 newline = newline.strip()
159 if not newline == "auto":
160 newfile += [newline.strip()]
161 elif line.startswith(("iface","mapping","source")):
162 '''Read a stanza'''
163 '''A Stanza can also start with allow- ie allow-hotplug'''
164 if inastanza:
165 if not pitchstanza:
166 newfile.extend(stanza)
167 stanza=[]
168 inastanza=1
169 pitchstanza=0
170 autoline=line.strip().split(" ")
171 for word in autoline:
172 if (word == basename) or (word.startswith(basename+":")):
173 pitchstanza=1
174 if not pitchstanza:
175 stanza+=[line.strip()]
176 else:
177 if inastanza:
178 if not pitchstanza:
179 stanza+=[line.strip()]
180 else:
181 if not pitchstanza:
182 newfile += [line.strip()]
183
184
185 for line in newfile:
186 print line
187 for line in output:
188 print line
189
190
191 fd, path = tempfile.mkstemp()
192 for line in newfile:
193 os.write(fd,line)
194 os.write(fd,"\n")
195 for line in output:
196 os.write(fd,line)
197 os.write(fd,"\n")
198 os.close(fd)
199
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
204
205
206 try:
207 retcode = subprocess.call("ifdown "+basename , shell=True)
208 if retcode < 0:
209 print >>sys.stderr, "Child was terminated by signal", -retcode
210 else:
211 print >>sys.stderr, "Child returned", retcode
212 except OSError as e:
213 print >>sys.stderr, "Execution failed:", e
214
215 try:
216 retcode = subprocess.call("ifup "+basename , shell=True)
217 if retcode < 0:
218 print >>sys.stderr, "Child was terminated by signal", -retcode
219 else:
220 print >>sys.stderr, "Child returned", retcode
221 except OSError as e:
222 print >>sys.stderr, "Execution failed:", e
223
224