|
2 | 2 | import os
|
3 | 3 | import subprocess
|
4 | 4 | import sys
|
| 5 | +import re |
5 | 6 |
|
6 | 7 | import click
|
7 | 8 | import netifaces
|
|
45 | 46 |
|
46 | 47 | VLAN_SUB_INTERFACE_SEPARATOR = '.'
|
47 | 48 |
|
| 49 | +GEARBOX_TABLE_PHY_PATTERN = r"_GEARBOX_TABLE:phy:*" |
| 50 | + |
48 | 51 | # To be enhanced. Routing-stack information should be collected from a global
|
49 | 52 | # location (configdb?), so that we prevent the continous execution of this
|
50 | 53 | # bash oneliner. To be revisited once routing-stack info is tracked somewhere.
|
@@ -119,7 +122,20 @@ def connect_config_db():
|
119 | 122 | config_db.connect()
|
120 | 123 | return config_db
|
121 | 124 |
|
| 125 | +def is_gearbox_configured(): |
| 126 | + """ |
| 127 | + Checks whether Gearbox is configured or not |
| 128 | + """ |
| 129 | + app_db = SonicV2Connector() |
| 130 | + app_db.connect(app_db.APPL_DB) |
122 | 131 |
|
| 132 | + keys = app_db.keys(app_db.APPL_DB, '*') |
| 133 | + |
| 134 | + # If any _GEARBOX_TABLE:phy:* records present in APPL_DB, then the gearbox is configured |
| 135 | + if any(re.match(GEARBOX_TABLE_PHY_PATTERN, key) for key in keys): |
| 136 | + return True |
| 137 | + else: |
| 138 | + return False |
123 | 139 |
|
124 | 140 | CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help', '-?'])
|
125 | 141 |
|
@@ -160,10 +176,7 @@ def cli(ctx):
|
160 | 176 | cli.add_command(warm_restart.warm_restart)
|
161 | 177 |
|
162 | 178 | # Add greabox commands only if GEARBOX is configured
|
163 |
| -# TODO: Find a cleaner way to do this |
164 |
| -app_db = SonicV2Connector(host='127.0.0.1') |
165 |
| -app_db.connect(app_db.APPL_DB) |
166 |
| -if app_db.keys(app_db.APPL_DB, '_GEARBOX_TABLE:phy:*'): |
| 179 | +if is_gearbox_configured(): |
167 | 180 | cli.add_command(gearbox.gearbox)
|
168 | 181 |
|
169 | 182 |
|
|
0 commit comments