From 4bcb784ba17566701de21269e4f5b31e18cbda2a Mon Sep 17 00:00:00 2001 From: Qi Luo Date: Thu, 1 Jul 2021 12:02:01 +0000 Subject: [PATCH 1/4] Replace swsssdk.ConfigDBConnector and SonicDBConfig with swsscommon implementation in sonic-host-services --- src/sonic-host-services/scripts/aaastatsd | 2 +- src/sonic-host-services/scripts/caclmgrd | 7 +++---- src/sonic-host-services/scripts/hostcfgd | 14 +++++++------- src/sonic-host-services/scripts/procdockerstatsd | 4 ++-- .../scripts/process-reboot-cause | 4 ++-- src/sonic-host-services/setup.py | 1 - .../tests/determine-reboot-cause_test.py | 4 ++-- .../tests/hostcfgd/hostcfgd_radius_test.py | 6 +++--- .../tests/hostcfgd/hostcfgd_test.py | 6 +++--- .../tests/procdockerstatsd_test.py | 4 ++-- 10 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/sonic-host-services/scripts/aaastatsd b/src/sonic-host-services/scripts/aaastatsd index 57bf7611531b..2cc8f027e573 100755 --- a/src/sonic-host-services/scripts/aaastatsd +++ b/src/sonic-host-services/scripts/aaastatsd @@ -4,7 +4,7 @@ import os import syslog import threading -from swsssdk import ConfigDBConnector +from swsscommon.swsscommon import ConfigDBConnector from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler diff --git a/src/sonic-host-services/scripts/caclmgrd b/src/sonic-host-services/scripts/caclmgrd index f1d2a41abd90..62783eb98df0 100755 --- a/src/sonic-host-services/scripts/caclmgrd +++ b/src/sonic-host-services/scripts/caclmgrd @@ -20,7 +20,6 @@ try: from sonic_py_common import daemon_base, device_info from swsscommon import swsscommon - from swsssdk import SonicDBConfig, ConfigDBConnector except ImportError as err: raise ImportError("%s - required module not found" % str(err)) @@ -98,10 +97,10 @@ class ControlPlaneAclManager(daemon_base.DaemonBase): self.lock[DEFAULT_NAMESPACE] = threading.Lock() self.num_changes[DEFAULT_NAMESPACE] = 0 - SonicDBConfig.load_sonic_global_db_config() + swsscommon.SonicDBConfig.load_sonic_global_db_config() self.config_db_map = {} self.iptables_cmd_ns_prefix = {} - self.config_db_map[DEFAULT_NAMESPACE] = ConfigDBConnector(use_unix_socket_path=True, namespace=DEFAULT_NAMESPACE) + self.config_db_map[DEFAULT_NAMESPACE] = swsscommon.ConfigDBConnector(use_unix_socket_path=True, namespace=DEFAULT_NAMESPACE) self.config_db_map[DEFAULT_NAMESPACE].connect() self.iptables_cmd_ns_prefix[DEFAULT_NAMESPACE] = "" self.namespace_mgmt_ip = self.get_namespace_mgmt_ip(self.iptables_cmd_ns_prefix[DEFAULT_NAMESPACE], DEFAULT_NAMESPACE) @@ -115,7 +114,7 @@ class ControlPlaneAclManager(daemon_base.DaemonBase): self.lock[front_asic_namespace] = threading.Lock() self.num_changes[front_asic_namespace] = 0 - self.config_db_map[front_asic_namespace] = ConfigDBConnector(use_unix_socket_path=True, namespace=front_asic_namespace) + self.config_db_map[front_asic_namespace] = swsscommon.ConfigDBConnector(use_unix_socket_path=True, namespace=front_asic_namespace) self.config_db_map[front_asic_namespace].connect() self.iptables_cmd_ns_prefix[front_asic_namespace] = "ip netns exec " + front_asic_namespace + " " self.namespace_docker_mgmt_ip[front_asic_namespace] = self.get_namespace_mgmt_ip(self.iptables_cmd_ns_prefix[front_asic_namespace], diff --git a/src/sonic-host-services/scripts/hostcfgd b/src/sonic-host-services/scripts/hostcfgd index 8742e8908f22..a818d4d9fe90 100755 --- a/src/sonic-host-services/scripts/hostcfgd +++ b/src/sonic-host-services/scripts/hostcfgd @@ -9,7 +9,7 @@ import syslog import jinja2 from sonic_py_common import device_info -from swsssdk import ConfigDBConnector +from swsscommon import swsscommon # FILE PAM_AUTH_CONF = "/etc/pam.d/common-auth-sonic" @@ -841,7 +841,7 @@ class NtpCfg(object): class HostConfigDaemon: def __init__(self): - self.config_db = ConfigDBConnector() + self.config_db = swsscommon.ConfigDBConnector() self.config_db.connect(wait_for_init=True, retry_on=True) syslog.syslog(syslog.LOG_INFO, 'ConfigDB connect success') @@ -924,7 +924,7 @@ class HostConfigDaemon: self.aaacfg.handle_radius_nas_ip_chg(key) def lpbk_handler(self, key, data): - key = ConfigDBConnector.deserialize_key(key) + key = swsscommon.ConfigDBConnector.deserialize_key(key) # Check if delete operation by fetch existing keys keys = self.config_db.get_keys('LOOPBACK_INTERFACE') if key in keys: @@ -937,19 +937,19 @@ class HostConfigDaemon: self.aaacfg.handle_radius_source_intf_ip_chg(key) def vlan_intf_handler(self, key, data): - key = ConfigDBConnector.deserialize_key(key) + key = swsscommon.ConfigDBConnector.deserialize_key(key) self.aaacfg.handle_radius_source_intf_ip_chg(key) def vlan_sub_intf_handler(self, key, data): - key = ConfigDBConnector.deserialize_key(key) + key = swsscommon.ConfigDBConnector.deserialize_key(key) self.aaacfg.handle_radius_source_intf_ip_chg(key) def portchannel_intf_handler(self, key, data): - key = ConfigDBConnector.deserialize_key(key) + key = swsscommon.ConfigDBConnector.deserialize_key(key) self.aaacfg.handle_radius_source_intf_ip_chg(key) def phy_intf_handler(self, key, data): - key = ConfigDBConnector.deserialize_key(key) + key = swsscommon.ConfigDBConnector.deserialize_key(key) self.aaacfg.handle_radius_source_intf_ip_chg(key) def ntp_server_handler (self, key, data): diff --git a/src/sonic-host-services/scripts/procdockerstatsd b/src/sonic-host-services/scripts/procdockerstatsd index d0b4c8fbf2ac..da6fa433e325 100755 --- a/src/sonic-host-services/scripts/procdockerstatsd +++ b/src/sonic-host-services/scripts/procdockerstatsd @@ -12,7 +12,7 @@ import time from datetime import datetime from sonic_py_common import daemon_base -import swsssdk +from swsscommon import swsscommon VERSION = '1.0' @@ -25,7 +25,7 @@ class ProcDockerStats(daemon_base.DaemonBase): def __init__(self, log_identifier): super(ProcDockerStats, self).__init__(log_identifier) - self.state_db = swsssdk.SonicV2Connector(host=REDIS_HOSTIP) + self.state_db = swsscommon.SonicV2Connector(host=REDIS_HOSTIP) self.state_db.connect("STATE_DB") def run_command(self, cmd): diff --git a/src/sonic-host-services/scripts/process-reboot-cause b/src/sonic-host-services/scripts/process-reboot-cause index 15d19defd1b7..df43a131faa9 100755 --- a/src/sonic-host-services/scripts/process-reboot-cause +++ b/src/sonic-host-services/scripts/process-reboot-cause @@ -12,7 +12,7 @@ try: import pwd import sys - import swsssdk + from swsscommon import swsscommon from sonic_py_common import logger except ImportError as err: raise ImportError("%s - required module not found" % str(err)) @@ -39,7 +39,7 @@ sonic_logger = logger.Logger(SYSLOG_IDENTIFIER) # ============================= Functions ============================= def read_reboot_cause_files_and_save_state_db(): # Connect State DB - state_db = swsssdk.SonicV2Connector(host=REDIS_HOSTIP) + state_db = swsscommon.SonicV2Connector(host=REDIS_HOSTIP) state_db.connect(state_db.STATE_DB) # Sort the previous reboot cause files by creation time diff --git a/src/sonic-host-services/setup.py b/src/sonic-host-services/setup.py index 5df859133274..3d90a93dbaa5 100644 --- a/src/sonic-host-services/setup.py +++ b/src/sonic-host-services/setup.py @@ -27,7 +27,6 @@ 'Jinja2>=2.10', 'PyGObject', 'sonic-py-common', - 'swsssdk>=2.0.1', 'systemd-python', ], setup_requires = [ diff --git a/src/sonic-host-services/tests/determine-reboot-cause_test.py b/src/sonic-host-services/tests/determine-reboot-cause_test.py index d9e999a5ce27..7d22a512f8ee 100644 --- a/src/sonic-host-services/tests/determine-reboot-cause_test.py +++ b/src/sonic-host-services/tests/determine-reboot-cause_test.py @@ -2,7 +2,7 @@ import os import pytest -import swsssdk +from swsscommon import swsscommon from sonic_py_common.general import load_module_from_source # TODO: Remove this if/else block once we no longer support Python 2 @@ -21,7 +21,7 @@ from .mock_connector import MockConnector -swsssdk.SonicV2Connector = MockConnector +swsscommon.SonicV2Connector = MockConnector test_path = os.path.dirname(os.path.abspath(__file__)) modules_path = os.path.dirname(test_path) diff --git a/src/sonic-host-services/tests/hostcfgd/hostcfgd_radius_test.py b/src/sonic-host-services/tests/hostcfgd/hostcfgd_radius_test.py index e327ab5b0c17..3febfa6c5964 100644 --- a/src/sonic-host-services/tests/hostcfgd/hostcfgd_radius_test.py +++ b/src/sonic-host-services/tests/hostcfgd/hostcfgd_radius_test.py @@ -5,7 +5,7 @@ import os import sys import subprocess -import swsssdk +from swsscommon import swsscommon from parameterized import parameterized from unittest import TestCase, mock @@ -13,7 +13,7 @@ from tests.hostcfgd.mock_configdb import MockConfigDb -swsssdk.ConfigDBConnector = MockConfigDb +swsscommon.ConfigDBConnector = MockConfigDb test_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) modules_path = os.path.dirname(test_path) scripts_path = os.path.join(modules_path, "scripts") @@ -37,7 +37,7 @@ class TestHostcfgdRADIUS(TestCase): Test hostcfd daemon - RADIUS """ def run_diff(self, file1, file2): - return subprocess.check_output('diff -uR {} {} || true'.format(file1, file2), shell=True) + return subprocess.check_output('diff -uR {} {} || true'.format(file1, file2), shell=True, text=True) @parameterized.expand(HOSTCFGD_TEST_RADIUS_VECTOR) diff --git a/src/sonic-host-services/tests/hostcfgd/hostcfgd_test.py b/src/sonic-host-services/tests/hostcfgd/hostcfgd_test.py index 7be9c40ec6db..934a4a9a02a6 100644 --- a/src/sonic-host-services/tests/hostcfgd/hostcfgd_test.py +++ b/src/sonic-host-services/tests/hostcfgd/hostcfgd_test.py @@ -1,6 +1,6 @@ import os import sys -import swsssdk +import swsscommon from parameterized import parameterized from sonic_py_common.general import load_module_from_source @@ -12,7 +12,7 @@ from pyfakefs.fake_filesystem_unittest import patchfs -swsssdk.ConfigDBConnector = MockConfigDb +swsscommon.swsscommon.ConfigDBConnector = MockConfigDb test_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) modules_path = os.path.dirname(test_path) scripts_path = os.path.join(modules_path, "scripts") @@ -93,7 +93,7 @@ def test_hostcfgd(self, test_name, test_data, fs): Returns: None """ - fs.add_real_paths(swsssdk.__path__) # add real path of swsssdk for database_config.json + fs.add_real_paths(swsscommon.__path__) # add real path of swsscommon for database_config.json fs.create_dir(hostcfgd.FeatureHandler.SYSTEMD_SYSTEM_DIR) MockConfigDb.set_config_db(test_data["config_db"]) with mock.patch("hostcfgd.subprocess") as mocked_subprocess: diff --git a/src/sonic-host-services/tests/procdockerstatsd_test.py b/src/sonic-host-services/tests/procdockerstatsd_test.py index 65c5a738ca67..4db198be7d75 100644 --- a/src/sonic-host-services/tests/procdockerstatsd_test.py +++ b/src/sonic-host-services/tests/procdockerstatsd_test.py @@ -2,12 +2,12 @@ import os import pytest -import swsssdk +from swsscommon import swsscommon from sonic_py_common.general import load_module_from_source from .mock_connector import MockConnector -swsssdk.SonicV2Connector = MockConnector +swsscommon.SonicV2Connector = MockConnector test_path = os.path.dirname(os.path.abspath(__file__)) modules_path = os.path.dirname(test_path) From b5560e66c723ace1adba6f0a464f74a643769126 Mon Sep 17 00:00:00 2001 From: Qi Luo Date: Thu, 1 Jul 2021 12:59:31 +0000 Subject: [PATCH 2/4] Add the dependency --- rules/sonic-host-services.mk | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rules/sonic-host-services.mk b/rules/sonic-host-services.mk index 022c237ee950..87463d2ba8cc 100644 --- a/rules/sonic-host-services.mk +++ b/rules/sonic-host-services.mk @@ -5,4 +5,6 @@ $(SONIC_HOST_SERVICES_PY3)_SRC_PATH = $(SRC_PATH)/sonic-host-services $(SONIC_HOST_SERVICES_PY3)_PYTHON_VERSION = 3 $(SONIC_HOST_SERVICES_PY3)_DEPENDS += $(SONIC_PY_COMMON_PY3) \ $(SWSSSDK_PY3) +$(SONIC_HOST_SERVICES_PY3)_DEBS_DEPENDS = $(LIBSWSSCOMMON) \ + $(PYTHON3_SWSSCOMMON) SONIC_PYTHON_WHEELS += $(SONIC_HOST_SERVICES_PY3) From 62865f3671661963319f71fcee9570941552ffc1 Mon Sep 17 00:00:00 2001 From: Qi Luo Date: Thu, 1 Jul 2021 14:47:28 +0000 Subject: [PATCH 3/4] Remove a default parameter --- src/sonic-host-services/tests/hostcfgd/hostcfgd_radius_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sonic-host-services/tests/hostcfgd/hostcfgd_radius_test.py b/src/sonic-host-services/tests/hostcfgd/hostcfgd_radius_test.py index 3febfa6c5964..39c77b5beae7 100644 --- a/src/sonic-host-services/tests/hostcfgd/hostcfgd_radius_test.py +++ b/src/sonic-host-services/tests/hostcfgd/hostcfgd_radius_test.py @@ -37,7 +37,7 @@ class TestHostcfgdRADIUS(TestCase): Test hostcfd daemon - RADIUS """ def run_diff(self, file1, file2): - return subprocess.check_output('diff -uR {} {} || true'.format(file1, file2), shell=True, text=True) + return subprocess.check_output('diff -uR {} {} || true'.format(file1, file2), shell=True) @parameterized.expand(HOSTCFGD_TEST_RADIUS_VECTOR) From 09d4c34a0ba5a5d1513a93ebb1fa268e0058ab38 Mon Sep 17 00:00:00 2001 From: Qi Luo Date: Tue, 6 Jul 2021 04:05:14 +0000 Subject: [PATCH 4/4] Simplify code --- src/sonic-host-services/scripts/hostcfgd | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/sonic-host-services/scripts/hostcfgd b/src/sonic-host-services/scripts/hostcfgd index a818d4d9fe90..d6c161a02416 100755 --- a/src/sonic-host-services/scripts/hostcfgd +++ b/src/sonic-host-services/scripts/hostcfgd @@ -9,7 +9,7 @@ import syslog import jinja2 from sonic_py_common import device_info -from swsscommon import swsscommon +from swsscommon.swsscommon import ConfigDBConnector # FILE PAM_AUTH_CONF = "/etc/pam.d/common-auth-sonic" @@ -841,7 +841,7 @@ class NtpCfg(object): class HostConfigDaemon: def __init__(self): - self.config_db = swsscommon.ConfigDBConnector() + self.config_db = ConfigDBConnector() self.config_db.connect(wait_for_init=True, retry_on=True) syslog.syslog(syslog.LOG_INFO, 'ConfigDB connect success') @@ -924,7 +924,7 @@ class HostConfigDaemon: self.aaacfg.handle_radius_nas_ip_chg(key) def lpbk_handler(self, key, data): - key = swsscommon.ConfigDBConnector.deserialize_key(key) + key = ConfigDBConnector.deserialize_key(key) # Check if delete operation by fetch existing keys keys = self.config_db.get_keys('LOOPBACK_INTERFACE') if key in keys: @@ -937,19 +937,19 @@ class HostConfigDaemon: self.aaacfg.handle_radius_source_intf_ip_chg(key) def vlan_intf_handler(self, key, data): - key = swsscommon.ConfigDBConnector.deserialize_key(key) + key = ConfigDBConnector.deserialize_key(key) self.aaacfg.handle_radius_source_intf_ip_chg(key) def vlan_sub_intf_handler(self, key, data): - key = swsscommon.ConfigDBConnector.deserialize_key(key) + key = ConfigDBConnector.deserialize_key(key) self.aaacfg.handle_radius_source_intf_ip_chg(key) def portchannel_intf_handler(self, key, data): - key = swsscommon.ConfigDBConnector.deserialize_key(key) + key = ConfigDBConnector.deserialize_key(key) self.aaacfg.handle_radius_source_intf_ip_chg(key) def phy_intf_handler(self, key, data): - key = swsscommon.ConfigDBConnector.deserialize_key(key) + key = ConfigDBConnector.deserialize_key(key) self.aaacfg.handle_radius_source_intf_ip_chg(key) def ntp_server_handler (self, key, data):