From cde892b5e6cf96e3233f6cc085648836bd58ce2f Mon Sep 17 00:00:00 2001
From: Matthieu Imbert <matthieu.imbert@inria.fr>
Date: Mon, 13 Nov 2023 11:10:34 +0100
Subject: [PATCH] [execo_g5k] api_utils - canonical_host_name: fix for ifname
 != ethX (eg. fpgaX) - add get_host_interface

---
 src/execo_g5k/api_utils.py | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/src/execo_g5k/api_utils.py b/src/execo_g5k/api_utils.py
index 016fe0b..bfa3e31 100644
--- a/src/execo_g5k/api_utils.py
+++ b/src/execo_g5k/api_utils.py
@@ -559,7 +559,7 @@ def get_host_site(host):
             return m.group(3)
         else:
             return get_cluster_site(m.group(1))
-    else: return None 
+    else: return None
 
 def get_host_network_equipments(host):
     """"""
@@ -611,12 +611,12 @@ def get_network_equipment_attributes(equip):
     site = get_network_equipment_site(equip)
     return get_api_data()['network'][site][equip]
 
-__canonical_host_name_regex = re.compile("^([a-zA-Z]+\d*-\d+)(-eth\d+)?(-kavlan-\d+)?(-ipv6)?(\.([.\w]+))?")
+__canonical_host_name_regex = re.compile("^([a-zA-Z]+\d*-\d+)(-(?!kavlan-\d+)(?!ipv6)(\w+))?(-kavlan-\d+)?(-ipv6)?(\.([.\w]+))?$")
 
 def __canonical_sub_func(matchobj):
     n = matchobj.group(1)
-    if matchobj.lastindex >= 5:
-        n += matchobj.group(5)
+    if matchobj.lastindex >= 6:
+        n += matchobj.group(6)
     return n
 
 def canonical_host_name(host):
@@ -633,6 +633,18 @@ def canonical_host_name(host):
     else:
         return h.address
 
+__host_interface_regex = __canonical_host_name_regex
+
+def get_host_interface(host):
+    eth = None
+    h = execo.Host(host)
+    m = __host_interface_regex.search(h.address)
+    if m:
+        eth = m.group(3)
+        if not eth:
+            eth = 'eth0'
+    return eth
+
 def get_host_shortname(host):
     """Convert, if needed, the host name to its shortname"""
     if isinstance(host, execo.Host):
-- 
GitLab