Skip to content

Commit 2e9e0e4

Browse files
CP-53745: Use xapi sdk instead of xe command
Signed-off-by: Stephen Cheng <stephen.cheng@cloud.com>
1 parent 0c4c6c3 commit 2e9e0e4

File tree

2 files changed

+31
-43
lines changed

2 files changed

+31
-43
lines changed

autocertkit/utils.py

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,7 +2286,7 @@ def get_dom0_device_name(session, vm_ref, net_ref):
22862286
"Instead, '%s' were returned." %
22872287
(pifs, device_names))
22882288
device_name = device_names.pop()
2289-
return get_bridge_by_eth(device_name)
2289+
return get_bridge_by_eth(session, device_name)
22902290

22912291

22922292

@@ -2371,55 +2371,43 @@ def set_nic_device_status(session, interface, status):
23712371
wait_for_linkstate(session, interface, status)
23722372
time.sleep(5)
23732373

2374-
def get_eth_by_bridge(bridge: str) -> str:
2374+
@log_exceptions
2375+
def get_eth_by_bridge(session, bridge: str) -> str:
23752376
"""
2376-
Get the device name given a bridge name
2377+
Get the NIC device name given a bridge name.
23772378
"""
2378-
eth = ""
2379-
call = f"xe network-list bridge={bridge} params=uuid --minimal"
2380-
res = make_local_call(call, shell=True)
2381-
network_uuid = res["stdout"].strip().split(',')[0]
2382-
if network_uuid:
2383-
call = f"xe pif-list network-uuid={network_uuid} params=device --minimal"
2384-
res = make_local_call(call, shell=True)
2385-
eth = res["stdout"].strip().split(',')[0]
2386-
2387-
if not eth:
2388-
log.debug(f"Device not found for bridge: {bridge}, use bridge name as device name")
2389-
eth = bridge
2390-
2391-
return eth
2392-
2393-
def get_bridge_by_eth(eth: str) -> str:
2379+
host = session.xenapi.session.get_this_host(session.handle)
2380+
pifs = session.xenapi.host.get_PIFs(host)
2381+
for network in session.xenapi.network.get_all():
2382+
if session.xenapi.network.get_bridge(network) == bridge:
2383+
for pif in pifs:
2384+
if not session.xenapi.PIF.get_physical(pif) and \
2385+
session.xenapi.PIF.get_sriov_logical_PIF_of(pif):
2386+
continue
2387+
if session.xenapi.PIF.get_network(pif) == network:
2388+
return session.xenapi.PIF.get_device(pif)
2389+
2390+
@log_exceptions
2391+
def get_bridge_by_eth(session, eth: str) -> str:
23942392
"""
2395-
Get the bridge name given a device name
2393+
Get the bridge name given a NIC device name
23962394
"""
2397-
bridge = ""
2398-
call = f"xe pif-list device={eth} params=network-uuid --minimal"
2399-
res = make_local_call(call, shell=True)
2400-
network_uuid = res["stdout"].strip().split(',')[0]
2401-
if network_uuid:
2402-
call = f"xe network-list uuid={network_uuid} params=bridge --minimal"
2403-
res = make_local_call(call, shell=True)
2404-
bridge = res["stdout"].strip().split(',')[0]
2405-
2406-
if not bridge:
2407-
log.debug(f"Bridge not found for device: {eth}, use device name as bridge name")
2408-
bridge = eth
2409-
2410-
return bridge
2395+
host = session.xenapi.session.get_this_host(session.handle)
2396+
for pif in session.xenapi.host.get_PIFs(host):
2397+
if not session.xenapi.PIF.get_physical(pif) and \
2398+
session.xenapi.PIF.get_sriov_logical_PIF_of(pif):
2399+
continue
2400+
if eth == session.xenapi.PIF.get_device(pif):
2401+
network = session.xenapi.PIF.get_network(pif)
2402+
return session.xenapi.network.get_bridge(network)
24112403

24122404

24132405
# Plase refer to
24142406
# https://www.thomas-krenn.com/en/wiki/Predictable_Network_Interface_Names
24152407
# https://systemd.io/PREDICTABLE_INTERFACE_NAMES/
2408+
# The nic interface names can start with: eth, eno, enp, ens, enx, end
24162409
def is_nic_device_name(name):
2417-
return name.startswith('eth') \
2418-
or name.startswith('eno') \
2419-
or name.startswith('enp') \
2420-
or name.startswith('ens') \
2421-
or name.startswith('enx') \
2422-
or name.startswith('end') \
2410+
return name[0] == 'e'
24232411

24242412
class TestThread(threading.Thread):
24252413
"""Threading class that runs a function"""

plugins/autocertkit

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ def get_hw_offloads_from_core(session, args):
10431043
checking /sys/class/net/ethX """
10441044
eth_dev = validate_exists(args, 'eth_dev')
10451045
if eth_dev.startswith('xenbr'):
1046-
eth_dev = utils.get_eth_by_bridge(eth_dev)
1046+
eth_dev = utils.get_eth_by_bridge(session, eth_dev)
10471047

10481048
rec = {}
10491049
flag = int(make_local_call(
@@ -1337,7 +1337,7 @@ def reset_arp(session, args):
13371337
set_kernel_parameter("net.ipv4.conf.default.rp_filter", rp_filter_value, vm_m_ip)
13381338

13391339
# Change current existing interface
1340-
cmd = """ip -o link | awk '{if($2 ~ "en[o|s|p|x][^:]*:" || $2 ~ "eth[^:]*:") print $2}' | sed 's/:$//'"""
1340+
cmd = """ip -o link | awk '{if($2 ~ "^e.+:") print $2}' | sed 's/:$//'"""
13411341
call = [SSH, vm_m_ip, cmd]
13421342
iface_list = make_local_call(call)["stdout"].split()
13431343
for iface in iface_list:
@@ -1803,7 +1803,7 @@ def mark_local_pif_for_cleanup(session, device):
18031803
# Undo any transformations between bridges/devices that may
18041804
# have taken place already
18051805
if 'xenbr' in device:
1806-
device = utils.get_eth_by_bridge(device)
1806+
device = utils.get_eth_by_bridge(session, device)
18071807

18081808
this_host_uuid = get_from_xensource_inventory('INSTALLATION_UUID')
18091809

0 commit comments

Comments
 (0)