diff --git a/show/main.py b/show/main.py index bce69063ed0f..d7b0fdaac308 100644 --- a/show/main.py +++ b/show/main.py @@ -2,6 +2,7 @@ import os import subprocess import sys +import re import click import netifaces @@ -45,6 +46,8 @@ VLAN_SUB_INTERFACE_SEPARATOR = '.' +GEARBOX_TABLE_PHY_PATTERN = r"_GEARBOX_TABLE:phy:*" + # To be enhanced. Routing-stack information should be collected from a global # location (configdb?), so that we prevent the continous execution of this # bash oneliner. To be revisited once routing-stack info is tracked somewhere. @@ -119,7 +122,20 @@ def connect_config_db(): config_db.connect() return config_db +def is_gearbox_configured(): + """ + Checks whether Gearbox is configured or not + """ + app_db = SonicV2Connector() + app_db.connect(app_db.APPL_DB) + keys = app_db.keys(app_db.APPL_DB, '*') + + # If any _GEARBOX_TABLE:phy:* records present in APPL_DB, then the gearbox is configured + if any(re.match(GEARBOX_TABLE_PHY_PATTERN, key) for key in keys): + return True + else: + return False CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help', '-?']) @@ -160,10 +176,7 @@ def cli(ctx): cli.add_command(warm_restart.warm_restart) # Add greabox commands only if GEARBOX is configured -# TODO: Find a cleaner way to do this -app_db = SonicV2Connector(host='127.0.0.1') -app_db.connect(app_db.APPL_DB) -if app_db.keys(app_db.APPL_DB, '_GEARBOX_TABLE:phy:*'): +if is_gearbox_configured(): cli.add_command(gearbox.gearbox)