From e909bd85c3545b969e8e05aedf031719350d3cd2 Mon Sep 17 00:00:00 2001 From: Julien Fortin Date: Mon, 23 May 2016 10:48:24 +0200 Subject: [PATCH] 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 --- docs/examples/generate_interfaces.py | 29 +++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/docs/examples/generate_interfaces.py b/docs/examples/generate_interfaces.py index 55a6107..e0b69cc 100755 --- a/docs/examples/generate_interfaces.py +++ b/docs/examples/generate_interfaces.py @@ -3,6 +3,7 @@ import argparse import sys import subprocess +import os """ This script prints to stdout /etc/network/interfaces entries for 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(): porttab_path = '/var/lib/cumulus/porttab' ports = [] @@ -43,9 +63,12 @@ def get_swp_interfaces(): ports.append(line.split()[0]) except ValueError: continue - except Exception as e: - print 'Error: Unsupported script: %s' % str(e) - exit(1) + except: + try: + ports = get_pci_interfaces() + except Exception as e: + print 'Error: Unsupported script: %s' % str(e) + exit(1) if not ports: print 'Error: No ports found in %s' % porttab_path exit(1)