~johannes.erdfelt/openstack-guest-agents/gentoo-dns

« back to all changes in this revision

Viewing changes to unix/commands/redhat/network.py

  • Committer: Johannes Erdfelt
  • Date: 2011-10-21 16:50:41 UTC
  • mfrom: (88.3.17 networking-refactor)
  • Revision ID: johannes.erdfelt@rackspace.com-20111021165041-smmnytu2xezh2751
MergeĀ lp:~johannes.erdfelt/openstack-guest-agents/networking-refactor

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
redhat/centos network helper module
21
21
"""
22
22
 
 
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
 
29
 
23
30
import os
24
31
import time
25
32
import glob
34
41
INTERFACE_FILE = "ifcfg-%s"
35
42
ROUTE_FILE = "route-%s"
36
43
 
37
 
INTERFACE_LABELS = {"public": "eth0",
38
 
                    "private": "eth1"}
39
 
 
40
 
 
41
 
def configure_network(network_config, *args, **kwargs):
42
 
 
43
 
    # Generate new interface files
44
 
    interfaces = network_config.get('interfaces', [])
45
 
 
 
44
 
 
45
def configure_network(hostname, interfaces):
46
46
    if os.path.exists(NETWORK_FILE):
47
47
        infile = open(NETWORK_FILE)
48
48
    else:
51
51
    update_files, remove_files = process_interface_files(infile, interfaces)
52
52
 
53
53
    # Generate new hostname file
54
 
    hostname = network_config.get('hostname')
55
 
 
56
54
    infile = StringIO(update_files.get(NETWORK_FILE, infile))
57
55
 
58
56
    data = get_hostname_file(infile, hostname)
122
120
    return _update_key_value(infile, 'HOSTNAME', hostname)
123
121
 
124
122
 
125
 
def _get_file_data(interface):
 
123
def _get_file_data(ifname_prefix, interface):
126
124
    """
127
125
    Return data for (sub-)interfaces and routes
128
126
    """
129
127
 
130
 
    try:
131
 
        label = interface['label']
132
 
    except KeyError:
133
 
        raise SystemError("No interface label found")
134
 
 
135
 
    try:
136
 
        ifname_prefix = INTERFACE_LABELS[label]
137
 
    except KeyError:
138
 
        raise SystemError("Invalid label '%s'" % label)
139
 
 
140
 
    ip4s = interface.get('ips', [])
141
 
    ip6s = interface.get('ip6s', [])
142
 
 
143
 
    if not ip4s and not ip6s:
144
 
        raise SystemError("No IPs found for interface")
145
 
 
146
 
    try:
147
 
        mac = interface['mac']
148
 
    except KeyError:
149
 
        raise SystemError("No mac address found for interface")
150
 
 
151
 
    if label == "public":
152
 
        gateway4 = interface.get('gateway')
153
 
        gateway6 = interface.get('gateway6')
154
 
 
155
 
        if not gateway4 and not gateway6:
156
 
            raise SystemError("No gateway found for public interface")
157
 
 
158
 
        try:
159
 
            dns = interface['dns']
160
 
        except KeyError:
161
 
            raise SystemError("No DNS found for public interface")
162
 
    else:
163
 
        gateway4 = gateway6 = None
 
128
    ip4s = interface['ip4s']
 
129
    ip6s = interface['ip6s']
 
130
 
 
131
    gateway4 = interface['gateway4']
 
132
    gateway6 = interface['gateway6']
 
133
 
 
134
    dns = interface['dns']
164
135
 
165
136
    ifaces = []
166
137
 
167
138
    ifname_suffix_num = 0
168
 
    ipv6 = False
169
139
 
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)
173
143
        else:
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
180
 
 
181
 
        if i < len(ip4s):
182
 
            ip_info = ip4s[i]
183
 
 
184
 
            enabled = ip_info.get('enabled', '0')
185
 
            if enabled != '0':
186
 
                try:
187
 
                    ip = ip_info['ip']
188
 
                    netmask = ip_info['netmask']
189
 
                except KeyError:
190
 
                    raise SystemError(
191
 
                            "Missing IP or netmask in interface's IP list")
192
 
 
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
198
 
 
199
 
        if i < len(ip6s):
200
 
            ip_info = ip6s[i]
201
 
 
202
 
            enabled = ip_info.get('enabled', '0')
203
 
            if enabled != '0':
204
 
                ip = ip_info.get('address', ip_info.get('ip'))
205
 
                if not ip:
206
 
                    raise SystemError(
207
 
                            "Missing IP in interface's IP list")
208
 
                netmask = ip_info.get('netmask')
209
 
                if not netmask:
210
 
                    raise SystemError(
211
 
                            "Missing netmask in interface's IP list")
212
 
 
213
 
                gateway6 = ip_info.get('gateway', gateway6)
214
 
 
215
 
                iface_data += "IPV6INIT=yes\n"
216
 
                iface_data += "IPV6_AUTOCONF=no\n"
217
 
                iface_data += "IPV6ADDR=%s/%s\n" % (ip, netmask)
218
 
 
219
 
                if gateway6:
220
 
                    iface_data += "IPV6_DEFAULTGW=%s%%%s\n" % (gateway6,
221
 
                            ifname)
222
 
 
223
 
                ipv6 = True
224
 
 
225
 
        if gateway4 or gateway6:
 
149
        iface_data += "HWADDR=%s\n" % interface['mac']
 
150
 
 
151
        if ip4:
 
152
            iface_data += "IPADDR=%(address)s\n" % ip4
 
153
            iface_data += "NETMASK=%(netmask)s\n" % ip4
 
154
            if gateway4:
 
155
                iface_data += "DEFROUTE=yes\n"
 
156
                iface_data += "GATEWAY=%s\n" % gateway4
 
157
                gateway4 = None
 
158
 
 
159
        if ip6:
 
160
            iface_data += "IPV6INIT=yes\n"
 
161
            iface_data += "IPV6_AUTOCONF=no\n"
 
162
            iface_data += "IPV6ADDR=%(address)s/%(prefixlen)s\n" % ip6
 
163
 
 
164
            if gateway6:
 
165
                iface_data += "IPV6_DEFAULTGW=%s%%%s\n" % (gateway6, ifname)
 
166
                gateway6 = None
 
167
 
 
168
        if dns:
226
169
            for j, nameserver in enumerate(dns):
227
170
                iface_data += "DNS%d=%s\n" % (j + 1, nameserver)
 
171
            dns = None
228
172
 
229
173
        iface_data += "ONBOOT=yes\n"
230
174
        iface_data += "NM_CONTROLLED=no\n"
233
177
        ifaces.append((ifname, iface_data))
234
178
 
235
179
    route_data = ''
236
 
    for i, route in enumerate(interface.get('routes', [])):
237
 
        network = route['route']
238
 
        netmask = route['netmask']
239
 
        gateway = route['gateway']
240
 
 
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)
244
 
 
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'])
 
184
 
 
185
    return (ifaces, route_data)
246
186
 
247
187
 
248
188
def get_interface_files(interfaces):
249
189
    update_files = {}
250
190
 
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)
253
193
 
254
194
        for ifname, data in ifaces:
255
195
            update_files[INTERFACE_FILE % ifname] = data
281
221
    update_files = {}
282
222
 
283
223
    ipv6 = False
284
 
    for interface in interfaces:
285
 
        ifname, ifaces, ifaceipv6, route_data = _get_file_data(interface)
286
 
        if ifaceipv6:
 
224
    for ifname, interface in interfaces.iteritems():
 
225
        ifaces, route_data = _get_file_data(ifname, interface)
 
226
        if interface['ip6s']:
287
227
            ipv6 = True
288
228
 
289
229
        for ifname, data in ifaces: