examples: generate interfaces: will fetch pci interfaces when porttab is missing

Ticket: CM-10751
Reviewed By: Roopa, Nikhil
Testing Done: exec script without porttab

Signed-off-by: Julien Fortin <julien@cumulusnetworks.com>
This commit is contained in:
Julien Fortin 2016-05-23 10:48:24 +02:00
parent 55d9fae146
commit e909bd85c3

View File

@ -3,6 +3,7 @@
import argparse import argparse
import sys import sys
import subprocess import subprocess
import os
""" This script prints to stdout /etc/network/interfaces entries for """ This script prints to stdout /etc/network/interfaces entries for
requested interfaces. requested interfaces.
@ -30,6 +31,25 @@ import subprocess
""" """
def get_pci_interfaces():
ports = []
FNULL = open(os.devnull, 'w')
try:
cmd = '(ip -o link show | grep -v "@" | cut -d" " -f2 | sed \'s/:$//\')'
output = subprocess.check_output(cmd, shell=True).split()
for interface in output:
cmd = 'udevadm info -a -p /sys/class/net/%s | grep \'SUBSYSTEMS=="pci"\'' % interface
try:
subprocess.check_call(cmd, shell=True, stdout=FNULL)
ports.append(interface)
except:
pass
except:
pass
finally:
FNULL.close()
return ports
def get_swp_interfaces(): def get_swp_interfaces():
porttab_path = '/var/lib/cumulus/porttab' porttab_path = '/var/lib/cumulus/porttab'
ports = [] ports = []
@ -43,9 +63,12 @@ def get_swp_interfaces():
ports.append(line.split()[0]) ports.append(line.split()[0])
except ValueError: except ValueError:
continue continue
except Exception as e: except:
print 'Error: Unsupported script: %s' % str(e) try:
exit(1) ports = get_pci_interfaces()
except Exception as e:
print 'Error: Unsupported script: %s' % str(e)
exit(1)
if not ports: if not ports:
print 'Error: No ports found in %s' % porttab_path print 'Error: No ports found in %s' % porttab_path
exit(1) exit(1)