From b473160400cd06c8d81a77aaa7c4e4d40876e6dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez=20Garc=C3=ADa?= Date: Thu, 3 Oct 2024 17:34:30 +0200 Subject: [PATCH] Refactor ProxmoxClient to improve IP address retrieval logic --- .../src/uds/services/Proxmox/proxmox/client.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/server/src/uds/services/Proxmox/proxmox/client.py b/server/src/uds/services/Proxmox/proxmox/client.py index 25f46e25f..d9341de1f 100644 --- a/server/src/uds/services/Proxmox/proxmox/client.py +++ b/server/src/uds/services/Proxmox/proxmox/client.py @@ -499,16 +499,26 @@ class ProxmoxClient: node=node, )['data']['result'] # look for first non-localhost interface with an ip address + found_ips: list[str] = [] for iface in ifaces_list: if iface['name'] != 'lo' and 'ip-addresses' in iface: for ip in iface['ip-addresses']: - if ip['ip-address'].startswith('127.'): + ip_address = ip['ip-address'] + if ( + ip_address.startswith('127.') + or ip_address.startswith('fe80:') + or ip_address == '::1' + ): continue - if ip_version == '4' and ip.get('ip-address-type') != 'ipv4': + if (ip_version == '4') and ip.get('ip-address-type') != 'ipv4': continue elif ip_version == '6' and ip.get('ip-address-type') != 'ipv6': continue - return ip['ip-address'] + found_ips.append(ip_address) + if found_ips: + sorted_ips = sorted(found_ips, key=lambda x: ':' in x) + return sorted_ips[0] + except Exception as e: logger.info('Error getting guest ip address for machine %s: %s', vmid, e) raise exceptions.ProxmoxError(f'No ip address for vm {vmid}: {e}')