1
0
mirror of https://github.com/dkmstr/openuds.git synced 2024-12-22 13:34:04 +03:00

Refactor ProxmoxClient to improve IP address retrieval logic

This commit is contained in:
Adolfo Gómez García 2024-10-03 17:34:30 +02:00
parent 1c388f62c1
commit b473160400
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23

View File

@ -499,16 +499,26 @@ class ProxmoxClient:
node=node, node=node,
)['data']['result'] )['data']['result']
# look for first non-localhost interface with an ip address # look for first non-localhost interface with an ip address
found_ips: list[str] = []
for iface in ifaces_list: for iface in ifaces_list:
if iface['name'] != 'lo' and 'ip-addresses' in iface: if iface['name'] != 'lo' and 'ip-addresses' in iface:
for ip in iface['ip-addresses']: 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 continue
if ip_version == '4' and ip.get('ip-address-type') != 'ipv4': if (ip_version == '4') and ip.get('ip-address-type') != 'ipv4':
continue continue
elif ip_version == '6' and ip.get('ip-address-type') != 'ipv6': elif ip_version == '6' and ip.get('ip-address-type') != 'ipv6':
continue 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: except Exception as e:
logger.info('Error getting guest ip address for machine %s: %s', vmid, 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}') raise exceptions.ProxmoxError(f'No ip address for vm {vmid}: {e}')