From 9d7387a18e15427ed11103fc8d1724b01fbdc324 Mon Sep 17 00:00:00 2001 From: ganglv <88995770+ganglyu@users.noreply.github.com> Date: Wed, 27 Apr 2022 07:14:51 +0800 Subject: [PATCH] [sonic-host-services]: Fix import and invalid path (#10660) Why I did it Can not start sonic-hostservice How I did it Install python3-dbus and systemd-python, and replace invalid path How to verify it Start the service with below commands: sudo systemctl start sonic-hostservice sudo systemctl status sonic-hostservice Signed-off-by: Gang Lv ganglv@microsoft.com --- files/build_templates/sonic_debian_extension.j2 | 5 ++++- src/sonic-host-services/scripts/sonic-host-server | 15 ++++++++++++--- src/sonic-host-services/setup.py | 1 + 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/files/build_templates/sonic_debian_extension.j2 b/files/build_templates/sonic_debian_extension.j2 index 924641393c..c51d6d4543 100644 --- a/files/build_templates/sonic_debian_extension.j2 +++ b/files/build_templates/sonic_debian_extension.j2 @@ -233,7 +233,10 @@ sudo cp -f $IMAGE_CONFIGS/bash/bash.bashrc $FILESYSTEM_ROOT/etc/ sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y install libcairo2-dev libdbus-1-dev libgirepository1.0-dev libsystemd-dev pkg-config # Mark runtime dependencies as manually installed to avoid them being auto-removed while uninstalling build dependencies -sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-mark manual gir1.2-glib-2.0 libdbus-1-3 libgirepository-1.0-1 libsystemd0 +sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-mark manual gir1.2-glib-2.0 libdbus-1-3 libgirepository-1.0-1 libsystemd0 python3-dbus + +# Install systemd-python for SONiC host services +sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip3 install systemd-python # Install SONiC host services package SONIC_HOST_SERVICES_PY3_WHEEL_NAME=$(basename {{sonic_host_services_py3_wheel_path}}) diff --git a/src/sonic-host-services/scripts/sonic-host-server b/src/sonic-host-services/scripts/sonic-host-server index a9fdc2eb25..bf4449e34e 100755 --- a/src/sonic-host-services/scripts/sonic-host-server +++ b/src/sonic-host-services/scripts/sonic-host-server @@ -13,9 +13,16 @@ import dbus.mainloop.glib from gi.repository import GObject -def register_modules(): +def find_module_path(): + """Find path for host_moduels""" + try: + from host_modules import host_service + return os.path.dirname(host_service.__file__) + except ImportError as e: + return None + +def register_modules(mod_path): """Register all host modules""" - mod_path = '/usr/local/lib/python3.7/dist-packages/host_modules' sys.path.append(mod_path) for mod_file in glob.glob(os.path.join(mod_path, '*.py')): if os.path.isfile(mod_file) and not mod_file.endswith('__init__.py'): @@ -62,7 +69,9 @@ class SignalManager(object): loop.quit() sigmgr = SignalManager() -register_modules() +mod_path = find_module_path() +if mod_path is not None: + register_modules(mod_path) # Only run if we actually have some handlers if handlers: diff --git a/src/sonic-host-services/setup.py b/src/sonic-host-services/setup.py index 8926e960e3..9ed9e1082a 100644 --- a/src/sonic-host-services/setup.py +++ b/src/sonic-host-services/setup.py @@ -24,6 +24,7 @@ setup( ], install_requires = [ 'dbus-python', + 'systemd-python', 'Jinja2>=2.10', 'PyGObject', 'sonic-py-common'