write_redhat_interfaces: refactor to walk interfaces first

We currently go through each network an assume that we're writing out
one ifcfg-<interface> file for each entry we see.

This fails when we have an ipv4 and and ipv6 entry for the same
interface.  In this case, we need to combine the details into the same
ifcfg-<interface> config file.

This is a small refactor of write_redhat_interfaces to filter out the
interfaces list before we start writing the config files.  This is the
first step in reworking things so an output function can take multiple
networks to configure.

This is intended to be only a refactor and have no functional change.

Change-Id: I0aac3674fd17ff2bfcf3542e27097e171036c837
This commit is contained in:
Ian Wienand 2022-05-25 12:23:10 +10:00
parent db6d29eb53
commit 035c808c12
1 changed files with 31 additions and 21 deletions

View File

@ -263,11 +263,10 @@ def _write_rh_manual(name, interface, args):
def write_redhat_interfaces(interfaces, sys_interfaces, args):
files_to_write = dict()
# Sort the interfaces by id so that we'll have consistent output order
for iname, interface in sorted(
interfaces.items(), key=lambda x: x[1]['id']):
if interface['type'] == 'ipv6':
continue
# Strip out ignored interfaces
_interfaces = {}
for iname, interface in interfaces.items():
# sys_interfaces is pruned by --interface; if one of the
# raw_macs (or, *the* MAC for single interfaces) does not
# match as one of the interfaces we want configured, skip
@ -275,22 +274,6 @@ def write_redhat_interfaces(interfaces, sys_interfaces, args):
if not set(sys_interfaces).intersection(set(raw_macs)):
continue
if 'vlan_id' in interface:
# raw_macs will have a single entry if the vlan device is a
# phsical device and >1 when it is a bond device.
if len(raw_macs) == 1:
vlan_raw_device = sys_interfaces.get(raw_macs[0])
else:
vlan_raw_device = interface['vlan_link']
interface_name = "{0}.{1}".format(
vlan_raw_device, interface['vlan_id'])
elif 'bond_mode' in interface:
# It is possible our interface does not have a link, so fall back
# to iname which is the link id.
interface_name = interface.get('link', iname)
else:
interface_name = sys_interfaces[interface['mac_address']]
if 'bond_links' in interface:
# We need to keep track of the slave interfaces because
# SUSE configures the slaves on the master ifcfg file
@ -301,6 +284,33 @@ def write_redhat_interfaces(interfaces, sys_interfaces, args):
# Remove the 'bond_links' key
interface.pop('bond_links')
_interfaces[iname] = interface
interfaces = _interfaces
# Sort the interfaces by id so that we'll have consistent output order
for iname, interface in sorted(
interfaces.items(), key=lambda x: x[1]['id']):
if interface['type'] == 'ipv6':
continue
if 'vlan_id' in interface:
# raw_macs will have a single entry if the vlan device is a
# phsical device and >1 when it is a bond device.
raw_macs = interface.get('raw_macs', [interface['mac_address']])
if len(raw_macs) == 1:
vlan_raw_device = sys_interfaces.get(raw_macs[0])
else:
vlan_raw_device = interface['vlan_link']
interface_name = "{0}.{1}".format(
vlan_raw_device, interface['vlan_id'])
elif 'bond_mode' in interface:
# It is possible our interface does not have a link, so fall back
# to iname which is the link id.
interface_name = interface.get('link', iname)
else:
interface_name = sys_interfaces[interface['mac_address']]
if interface['type'] == 'ipv4':
files_to_write.update(
_write_rh_interface(interface_name, interface, args))