~wesley-wiedenmeier/curtin/vmtest-webhook-delay

« back to all changes in this revision

Viewing changes to curtin/net/__init__.py

  • Committer: Ryan Harper
  • Date: 2016-04-04 18:53:03 UTC
  • mfrom: (366.1.2 merge-a-few)
  • Revision ID: ryan.harper@canonical.com-20160404185303-pjzprjz4qr7eeh9a
network,unittest: add bonding tests and cleanup newline rendering

- Add a unittest to validate bonding network config render, specifically
  when to emit auto $iface for dependent bond slaves.
- Cleanup newline logic so we always have a clean '\n\n' between stanzas
- Add '# Autogenerated by curtin' header to to 70-persistent-net.rules file
- Add Xenial bonding testcase

Show diffs side-by-side

added added

removed removed

Lines of Context:
283
283
    ''' Given state, emit udev rules to map
284
284
        mac to ifname
285
285
    '''
286
 
    content = ""
 
286
    content = "# Autogenerated by curtin\n"
287
287
    interfaces = network_state.get('interfaces')
288
288
    for iface in interfaces.values():
289
 
        # for physical interfaces write out a persist net udev rule
290
 
        if iface['type'] == 'physical' and \
291
 
           'name' in iface and 'mac_address' in iface:
292
 
            content += generate_udev_rule(iface['name'],
293
 
                                          iface['mac_address'])
 
289
        if iface['type'] == 'physical':
 
290
            ifname = iface.get('name', None)
 
291
            mac = iface.get('mac_address', '')
 
292
            # len(macaddr) == 2 * 6 + 5 == 17
 
293
            if ifname and mac and len(mac) == 17:
 
294
                content += generate_udev_rule(ifname, mac)
294
295
 
295
296
    return content
296
297
 
382
383
 
383
384
    for iface in sorted(interfaces.values(),
384
385
                        key=lambda k: (order[k['type']], k['name'])):
385
 
        content += "auto {name}\n".format(**iface)
386
386
 
 
387
        if content[-2:] != "\n\n":
 
388
            content += "\n"
387
389
        subnets = iface.get('subnets', {})
388
390
        if subnets:
389
391
            for index, subnet in zip(range(0, len(subnets)), subnets):
 
392
                if content[-2:] != "\n\n":
 
393
                    content += "\n"
390
394
                iface['index'] = index
391
395
                iface['mode'] = subnet['type']
392
396
                if iface['mode'].endswith('6'):
397
401
                    iface['mode'] = 'dhcp'
398
402
 
399
403
                if index == 0:
 
404
                    if subnet['type'] != 'manual':
 
405
                        content += "auto {name}\n".format(**iface)
400
406
                    content += "iface {name} {inet} {mode}\n".format(**iface)
401
407
                else:
402
 
                    content += "auto {name}:{index}\n".format(**iface)
 
408
                    if subnet['type'] != 'manual':
 
409
                        content += "auto {name}:{index}\n".format(**iface)
403
410
                    content += \
404
411
                        "iface {name}:{index} {inet} {mode}\n".format(**iface)
405
412
 
406
413
                content += iface_add_subnet(iface, subnet)
407
414
                content += iface_add_attrs(iface)
408
 
                content += "\n"
409
415
        else:
 
416
            if 'bond-master' in iface:
 
417
                content += "auto {name}\n".format(**iface)
410
418
            content += "iface {name} {inet} {mode}\n".format(**iface)
411
419
            content += iface_add_attrs(iface)
412
 
            content += "\n"
413
420
 
414
421
    for route in network_state.get('routes'):
415
422
        content += render_route(route)
426
433
    eni = os.path.sep.join((target, eni,))
427
434
    util.ensure_dir(os.path.dirname(eni))
428
435
    with open(eni, 'w+') as f:
 
436
        LOG.info('Writing ' + eni)
429
437
        f.write(render_interfaces(network_state))
430
438
 
431
439
    netrules = os.path.sep.join((target, netrules,))
432
440
    util.ensure_dir(os.path.dirname(netrules))
433
 
    with open(netrules, 'w+') as f:
434
 
        f.write(render_persistent_net(network_state))
 
441
    persistent_net_rules = render_persistent_net(network_state)
 
442
    if len(persistent_net_rules) > 0:
 
443
        with open(netrules, 'w+') as f:
 
444
            LOG.info('Writing ' + netrules)
 
445
            f.write(persistent_net_rules)
435
446
 
436
447
# vi: ts=4 expandtab syntax=python