[FIX] hw_drivers: Block when getting network interface

get_ip and get_mac_address respectively try to get the IP and MAC from
the IoT box's active network interface. If the interface isn't fully up
yet (due to waiting on a slow DHCP server response for example), both
functions can end up raising a KeyError exception since neither
interface has a value.

This can be bad if it's uncaught and happens in something like the
Manager thread, potentially killing it as a result. This can then
disrupt the rest of the application. The network being gone for a bit or
slow is something that can happen during everyday use, so we should be
robust against it.

Since the box can't do any useful work without an active network
connection, this fix keeps retrying until one of the interfaces comes
back up.

opw-2357144

closes odoo/odoo#77052

X-original-commit: d0359858b17fb84c04c267bdf5cc1626569f5cce
Signed-off-by: Quentin Lejeune (qle) <qle@odoo.com>
This commit is contained in:
Raf Geens
2021-09-23 14:24:58 +00:00
parent dbdac91c4b
commit 60864de16b
+26 -8
View File
@@ -134,16 +134,34 @@ def get_img_name():
return 'iotboxv%s_%s.zip' % (major, minor)
def get_ip():
try:
return netifaces.ifaddresses('eth0')[netifaces.AF_INET][0]['addr']
except:
return netifaces.ifaddresses('wlan0')[netifaces.AF_INET][0]['addr']
while True:
try:
return netifaces.ifaddresses('eth0')[netifaces.AF_INET][0]['addr']
except KeyError:
pass
try:
return netifaces.ifaddresses('wlan0')[netifaces.AF_INET][0]['addr']
except KeyError:
pass
_logger.warning("Couldn't get IP, sleeping and retrying.")
time.sleep(5)
def get_mac_address():
try:
return netifaces.ifaddresses('eth0')[netifaces.AF_LINK][0]['addr']
except:
return netifaces.ifaddresses('wlan0')[netifaces.AF_LINK][0]['addr']
while True:
try:
return netifaces.ifaddresses('eth0')[netifaces.AF_LINK][0]['addr']
except KeyError:
pass
try:
return netifaces.ifaddresses('wlan0')[netifaces.AF_LINK][0]['addr']
except KeyError:
pass
_logger.warning("Couldn't get MAC address, sleeping and retrying.")
time.sleep(5)
def get_ssid():
ap = subprocess.call(['systemctl', 'is-active', '--quiet', 'hostapd']) # if service is active return 0 else inactive