30
37
import commands.network
32
RESOLV_FILE = "/etc/resolv.conf"
33
39
RCCONF_FILE = "/etc/rc.conf"
35
INTERFACE_LABELS = {"public": "xn0",
39
def configure_network(network_config, *args, **kwargs):
42
def configure_network(hostname, interfaces):
43
# Generate new interface files
44
interfaces = network_config.get('interfaces', [])
46
# Generate new hostname file
47
hostname = network_config.get('hostname')
49
45
# Generate new /etc/rc.conf
50
46
data = _get_file_data(interfaces, hostname)
51
47
update_files[RCCONF_FILE] = data
53
49
# Generate new /etc/resolv.conf file
54
data = _get_resolv_conf(interfaces)
55
update_files[RESOLV_FILE] = data
50
filepath, data = commands.network.get_resolv_conf(interfaces)
52
update_files[filepath] = data
57
54
# Generate new /etc/hosts file
58
55
filepath, data = commands.network.get_etc_hosts(interfaces, hostname)
139
119
print >> outfile, 'dhcpd_enable="NO"'
140
120
print >> outfile, 'hostname=%s' % hostname
142
for interface in interfaces:
144
label = interface['label']
146
raise SystemError("No interface label found")
149
ifname_prefix = INTERFACE_LABELS[label]
151
raise SystemError("Invalid label '%s'" % label)
154
ips = interface['ips']
156
raise SystemError("No IPs found for interface")
158
ip6s = interface.get('ip6s', [])
161
mac = interface['mac']
163
raise SystemError("No mac address found for interface")
166
routes = interface['routes']
170
if label == "public":
171
gateway4 = interface.get('gateway')
172
gateway6 = interface.get('gateway6')
173
if not gateway4 and not gateway6:
174
raise SystemError("No gateway found for public interface")
175
if gateway4 and not len(defaultrouter):
176
defaultrouter = gateway4
177
if gateway6 and not len(ipv6_defaultrouter):
178
ipv6_defaultrouter = gateway6
180
if len(ipv6_interfaces):
181
ipv6_interfaces += ','
182
ipv6_interfaces += ifname_prefix
184
gateway4 = gateway6 = None
122
gateway4, gateway6 = commands.network.get_gateways(interfaces)
124
ifnames = interfaces.keys()
126
for ifname_prefix in ifnames:
127
interface = interfaces[ifname_prefix]
129
ip4s = interface['ip4s']
130
ip6s = interface['ip6s']
133
ipv6_interfaces.append(ifname_prefix)
186
135
ifname_suffix_num = 0
188
for i in xrange(max(len(ips), len(ip6s))):
137
for ip4, ip6 in map(None, ip4s, ip6s):
189
138
if ifname_suffix_num:
190
139
ifname = "%s_alias%d" % (ifname_prefix, ifname_suffix_num - 1)
192
141
ifname = ifname_prefix
204
if not ip_info and not ip6_info:
207
if ip_info and ip_info.get('enabled', '0') != '0':
210
netmask = ip_info['netmask']
213
"Missing IP or netmask in interface's IP list")
215
144
if ifname_suffix_num:
216
145
# XXX -- Known bug here. If we're adding an alias
217
146
# that is on the same network as another address already
218
147
# configured, the netmask here should be 255.255.255.255
219
148
print >> outfile, 'ifconfig_%s="%s netmask %s"' % \
220
(ifname, ip, netmask)
149
(ifname, ip4['address'], ip4['netmask'])
222
151
print >> outfile, 'ifconfig_%s="%s netmask %s up"' % \
223
(ifname, ip, netmask)
225
if ip6_info and ip6_info.get('enabled', '0') != '0':
226
ip = ip6_info.get('address', ip6_info.get('ip'))
229
"Missing IP in interface's IPv6 list")
230
netmask = ip6_info.get('netmask')
233
"Missing netmask in interface's IPv6 list")
152
(ifname, ip4['address'], ip4['netmask'])
235
155
print >> outfile, 'ipv6_ifconfig_%s="%s/%s"' % \
236
(ifname, ip, netmask)
238
gateway = ip6_info.get('gateway', gateway6)
156
(ifname, ip6['address'], ip6['prefixlen'])
240
158
ifname_suffix_num += 1
242
for x in xrange(len(routes)):
244
network = route['route']
245
netmask = route['netmask']
246
gateway = route['gateway']
160
for route in interface['routes']:
161
if ':' in route['network']:
250
fmt = '-net %s/%s %s'
163
fmt = '-net %(network)s/%(netmask)s %(gateway)s'
252
fmt = '-net %s -netmask %s %s'
254
static_route_entries.append(fmt % (network, netmask, gateway))
256
if len(static_route_entries):
165
fmt = '-net %(network)s -netmask %(netmask)s %(gateway)s'
167
static_route_entries.append(fmt % route)
169
if static_route_entries:
258
for x in xrange(len(static_route_entries)):
171
for i, line in enumerate(static_route_entries):
260
173
names.append(name)
261
print >> outfile, 'route_lan%d="%s"' % \
262
(x, static_route_entries[x])
174
print >> outfile, 'route_%s="%s"' % (name, line)
263
176
print >> outfile, 'static_routes="%s"' % ','.join(names)
264
if len(ipv6_interfaces):
265
179
print >> outfile, 'ipv6_enable="YES"'
266
print >> outfile, 'ipv6_network_interfaces="%s"' % ipv6_interfaces
267
if len(defaultrouter):
268
print >> outfile, 'defaultrouter="%s"' % defaultrouter
269
if len(ipv6_defaultrouter):
270
print >> outfile, 'ipv6_defaultrouter="%s"' % ipv6_defaultrouter
180
print >> outfile, 'ipv6_network_interfaces="%s"' % \
181
','.join(ipv6_interfaces)
184
print >> outfile, 'defaultrouter="%s"' % gateway4
187
print >> outfile, 'ipv6_defaultrouter="%s"' % gateway6
273
190
return outfile.read()