diff options
author | 2017-04-04 13:58:43 -0400 | |
---|---|---|
committer | 2017-04-10 10:05:42 -0400 | |
commit | b905e0d803e55deee9620a24de4b87c3429b43ab (patch) | |
tree | 74a799122f66f3e072bb9a2c934e60718b58aecd /os_net_config/objects.py | |
parent | 1b83afb07ddec59845ea3ca47fb9b20eefba1c5d (diff) |
os_net_config should map nics that are down if nic is in mapping file
Currently os-net_config will map nics from a user-supplied mapping file
only if the nic is active (operstate = up). This can cause problems
if a nic is in a bond and one of the bond's nics has no carrier.
This fix will map the nic from the mapping file if the nic is defined
on the system, regardless of the operstate status.
The fix implements a new function to return a list of available nics
(no check of operstate) for use if a mapping file is supplied. The
list of active nics must still be used in the default case when
numbering nics (no mapping file supplied). There is also some
cleanup to check if a user-supplied mac is in the mapping file before
attempting to convert the mac to a nic.
Change-Id: Ia5d8c8b49b7ac0b51ee42a754f06e5e53587a5f6
Closes-Bug: 1679787
Diffstat (limited to 'os_net_config/objects.py')
-rw-r--r-- | os_net_config/objects.py | 66 |
1 files changed, 39 insertions, 27 deletions
diff --git a/os_net_config/objects.py b/os_net_config/objects.py index 944aecd..5d43777 100644 --- a/os_net_config/objects.py +++ b/os_net_config/objects.py @@ -85,37 +85,49 @@ def _mapped_nics(nic_mapping=None): if _MAPPED_NICS: return _MAPPED_NICS _MAPPED_NICS = {} - active_nics = utils.ordered_active_nics() - for nic_alias, nic_mapped in mapping.items(): - if nic_mapped not in active_nics: - # The mapping is either invalid, or specifies a mac - is_mapping_valid = False - for active in active_nics: - try: - active_mac = utils.interface_mac(active) - except IOError: + + if mapping: + # If mapping file provided, nics need not be active + available_nics = utils.ordered_available_nics() + for nic_alias, nic_mapped in mapping.items(): + + if netaddr.valid_mac(nic_mapped): + # If 'nic' is actually a mac address, retrieve actual nic name + for nic in available_nics: + try: + mac = utils.interface_mac(nic) + except IOError: + continue + if nic_mapped == mac: + logger.debug("%s matches device %s" % + (nic_mapped, nic)) + nic_mapped = nic + break + else: + # The mac could not be found on this system + logger.error('mac %s not found in available nics (%s)' + % (nic_mapped, ', '.join(available_nics))) continue - if nic_mapped == active_mac: - logger.debug("%s matches device %s" % (nic_mapped, active)) - nic_mapped = active - is_mapping_valid = True - break - - if not is_mapping_valid: - # The mapping can't specify a non-active or non-existent nic - logger.warning('interface %s is not an active nic (%s)' - % (nic_mapped, ', '.join(active_nics))) + + elif nic_mapped not in available_nics: + # nic doesn't exist on this system + logger.error('nic %s not found in available nics (%s)' + % (nic_mapped, ', '.join(available_nics))) continue - # Duplicate mappings are not allowed - if nic_mapped in _MAPPED_NICS.values(): - msg = ('interface %s already mapped, ' - 'check mapping file for duplicates' - % nic_mapped) - raise InvalidConfigException(msg) + # Duplicate mappings are not allowed + if nic_mapped in _MAPPED_NICS.values(): + msg = ('interface %s already mapped, ' + 'check mapping file for duplicates' + % nic_mapped) + raise InvalidConfigException(msg) + + _MAPPED_NICS[nic_alias] = nic_mapped + logger.info("%s in mapping file mapped to: %s" + % (nic_alias, nic_mapped)) - _MAPPED_NICS[nic_alias] = nic_mapped - logger.info("%s mapped to: %s" % (nic_alias, nic_mapped)) + # nics not in mapping file must be active in order to be mapped + active_nics = utils.ordered_active_nics() # Add default numbered mappings, but do not overwrite existing entries for nic_mapped in set(active_nics).difference(set(_MAPPED_NICS.values())): |