From 64aa1b839bedb48634433a5a01a3ab4411cc23ab Mon Sep 17 00:00:00 2001 From: maksymbelei95 <75987222+maksymbelei95@users.noreply.github.com> Date: Tue, 26 Jan 2021 04:06:45 +0200 Subject: [PATCH] [show] Fix warnings, related to gearbox, while show commands execution (#1343) * Performing getting from APPL_DB of all the keys while checking readiness status of the gearbox to prevent warnings sending to syslog if there no records of PHYs of a gearbox in APPL_DB. * Moving of the gearbox readiness status checking procedure to separated function. Signed-off-by: Maksym Belei --- show/main.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) 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)