From 9e310e534eda6048b75ac8011bdfab4de916940e Mon Sep 17 00:00:00 2001 From: Hua Liu <58683130+liuh-80@users.noreply.github.com> Date: Mon, 30 May 2022 12:04:29 +0800 Subject: [PATCH] Fix sonic-installer and 'show version' command crash when database docker not running issue. (#2183) Fix sonic-installer and 'show version' command crash when database docker not running issue. #### Description Global variable utilies_common.cli.iface_alias_converter will connect to config DB when initialize itself. If database docker not running, the initialize code will crash. To fix this issue, change the variable to lazy initialize variable with lazy_object_proxy. #### Motivation and Context Fix this issue: https://github.com/Azure/sonic-buildimage/issues/10434 #### How Has This Been Tested? Pass all UT and sonic-buildimage E2E test. #### Additional Information (Optional) --- setup.py | 1 + show/main.py | 5 +++-- utilities_common/cli.py | 5 +++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/setup.py b/setup.py index c954f03825..8cb4a7515f 100644 --- a/setup.py +++ b/setup.py @@ -216,6 +216,7 @@ 'toposort==1.6', 'www-authenticate==0.9.2', 'xmltodict==0.12.0', + 'lazy-object-proxy', ], setup_requires= [ 'pytest-runner', diff --git a/show/main.py b/show/main.py index 173c84bd7e..84f6cee981 100755 --- a/show/main.py +++ b/show/main.py @@ -5,6 +5,7 @@ import re import click +import lazy_object_proxy import utilities_common.cli as clicommon from sonic_py_common import multi_asic import utilities_common.multi_asic as multi_asic_util @@ -126,8 +127,8 @@ def run_command(command, display_cmd=False, return_cmd=False): if rc != 0: sys.exit(rc) -# Global class instance for SONiC interface name to alias conversion -iface_alias_converter = clicommon.InterfaceAliasConverter() +# Lazy global class instance for SONiC interface name to alias conversion +iface_alias_converter = lazy_object_proxy.Proxy(lambda: clicommon.InterfaceAliasConverter()) # # Display all storm-control data diff --git a/utilities_common/cli.py b/utilities_common/cli.py index c05069adcf..771bb9121e 100644 --- a/utilities_common/cli.py +++ b/utilities_common/cli.py @@ -7,6 +7,7 @@ import click import json +import lazy_object_proxy import netaddr from natsort import natsorted @@ -183,8 +184,8 @@ def alias_to_name(self, interface_alias): # interface_alias not in port_dict. Just return interface_alias return interface_alias if sub_intf_sep_idx == -1 else interface_alias + VLAN_SUB_INTERFACE_SEPARATOR + vlan_id -# Global class instance for SONiC interface name to alias conversion -iface_alias_converter = InterfaceAliasConverter() +# Lazy global class instance for SONiC interface name to alias conversion +iface_alias_converter = lazy_object_proxy.Proxy(lambda: InterfaceAliasConverter()) def get_interface_naming_mode(): mode = os.getenv('SONIC_CLI_IFACE_MODE')