20
20
redhat/centos network helper module
23
# Red Hat network configuration uses:
24
# - 1 network configuration file per interface
25
# - 1 IP per interface
26
# - routes are per interface
27
# - gateways are per interface
28
# - DNS is configured per interface
34
41
INTERFACE_FILE = "ifcfg-%s"
35
42
ROUTE_FILE = "route-%s"
37
INTERFACE_LABELS = {"public": "eth0",
41
def configure_network(network_config, *args, **kwargs):
43
# Generate new interface files
44
interfaces = network_config.get('interfaces', [])
45
def configure_network(hostname, interfaces):
46
46
if os.path.exists(NETWORK_FILE):
47
47
infile = open(NETWORK_FILE)
51
51
update_files, remove_files = process_interface_files(infile, interfaces)
53
53
# Generate new hostname file
54
hostname = network_config.get('hostname')
56
54
infile = StringIO(update_files.get(NETWORK_FILE, infile))
58
56
data = get_hostname_file(infile, hostname)
122
120
return _update_key_value(infile, 'HOSTNAME', hostname)
125
def _get_file_data(interface):
123
def _get_file_data(ifname_prefix, interface):
127
125
Return data for (sub-)interfaces and routes
131
label = interface['label']
133
raise SystemError("No interface label found")
136
ifname_prefix = INTERFACE_LABELS[label]
138
raise SystemError("Invalid label '%s'" % label)
140
ip4s = interface.get('ips', [])
141
ip6s = interface.get('ip6s', [])
143
if not ip4s and not ip6s:
144
raise SystemError("No IPs found for interface")
147
mac = interface['mac']
149
raise SystemError("No mac address found for interface")
151
if label == "public":
152
gateway4 = interface.get('gateway')
153
gateway6 = interface.get('gateway6')
155
if not gateway4 and not gateway6:
156
raise SystemError("No gateway found for public interface")
159
dns = interface['dns']
161
raise SystemError("No DNS found for public interface")
163
gateway4 = gateway6 = None
128
ip4s = interface['ip4s']
129
ip6s = interface['ip6s']
131
gateway4 = interface['gateway4']
132
gateway6 = interface['gateway6']
134
dns = interface['dns']
167
138
ifname_suffix_num = 0
170
for i in xrange(max(len(ip4s), len(ip6s))):
140
for ip4, ip6 in map(None, ip4s, ip6s):
171
141
if ifname_suffix_num:
172
142
ifname = "%s:%d" % (ifname_prefix, ifname_suffix_num)
176
146
iface_data = "# Automatically generated, do not edit\n"
177
147
iface_data += "DEVICE=%s\n" % ifname
178
148
iface_data += "BOOTPROTO=static\n"
179
iface_data += "HWADDR=%s\n" % mac
184
enabled = ip_info.get('enabled', '0')
188
netmask = ip_info['netmask']
191
"Missing IP or netmask in interface's IP list")
193
iface_data += "IPADDR=%s\n" % ip
194
iface_data += "NETMASK=%s\n" % netmask
195
if label == "public" and gateway4:
196
iface_data += "DEFROUTE=yes\n"
197
iface_data += "GATEWAY=%s\n" % gateway4
202
enabled = ip_info.get('enabled', '0')
204
ip = ip_info.get('address', ip_info.get('ip'))
207
"Missing IP in interface's IP list")
208
netmask = ip_info.get('netmask')
211
"Missing netmask in interface's IP list")
213
gateway6 = ip_info.get('gateway', gateway6)
215
iface_data += "IPV6INIT=yes\n"
216
iface_data += "IPV6_AUTOCONF=no\n"
217
iface_data += "IPV6ADDR=%s/%s\n" % (ip, netmask)
220
iface_data += "IPV6_DEFAULTGW=%s%%%s\n" % (gateway6,
225
if gateway4 or gateway6:
149
iface_data += "HWADDR=%s\n" % interface['mac']
152
iface_data += "IPADDR=%(address)s\n" % ip4
153
iface_data += "NETMASK=%(netmask)s\n" % ip4
155
iface_data += "DEFROUTE=yes\n"
156
iface_data += "GATEWAY=%s\n" % gateway4
160
iface_data += "IPV6INIT=yes\n"
161
iface_data += "IPV6_AUTOCONF=no\n"
162
iface_data += "IPV6ADDR=%(address)s/%(prefixlen)s\n" % ip6
165
iface_data += "IPV6_DEFAULTGW=%s%%%s\n" % (gateway6, ifname)
226
169
for j, nameserver in enumerate(dns):
227
170
iface_data += "DNS%d=%s\n" % (j + 1, nameserver)
229
173
iface_data += "ONBOOT=yes\n"
230
174
iface_data += "NM_CONTROLLED=no\n"
233
177
ifaces.append((ifname, iface_data))
236
for i, route in enumerate(interface.get('routes', [])):
237
network = route['route']
238
netmask = route['netmask']
239
gateway = route['gateway']
241
route_data += "ADDRESS%d=%s\n" % (i, network)
242
route_data += "NETMASK%d=%s\n" % (i, netmask)
243
route_data += "GATEWAY%d=%s\n" % (i, gateway)
245
return (ifname_prefix, ifaces, ipv6, route_data)
180
for i, route in enumerate(interface['routes']):
181
route_data += "ADDRESS%d=%s\n" % (i, route['network'])
182
route_data += "NETMASK%d=%s\n" % (i, route['netmask'])
183
route_data += "GATEWAY%d=%s\n" % (i, route['gateway'])
185
return (ifaces, route_data)
248
188
def get_interface_files(interfaces):
249
189
update_files = {}
251
for interface in interfaces:
252
ifname, ifaces, ipv6, route_data = _get_file_data(interface)
191
for ifname, interface in interfaces.iteritems():
192
ifaces, route_data = _get_file_data(ifname, interface)
254
194
for ifname, data in ifaces:
255
195
update_files[INTERFACE_FILE % ifname] = data
281
221
update_files = {}
284
for interface in interfaces:
285
ifname, ifaces, ifaceipv6, route_data = _get_file_data(interface)
224
for ifname, interface in interfaces.iteritems():
225
ifaces, route_data = _get_file_data(ifname, interface)
226
if interface['ip6s']:
289
229
for ifname, data in ifaces: