Skip to content

Commit bb57cce

Browse files
authored
[sonic-host-service]: Add SONiC Host Services infrastructure (#4840)
- Why I did it When SONiC is configured with the management framework and/or telemetry services, the applications running inside those containers need to access some functionality on the host system. The following is a non-exhaustive list of such functionality: Image management Configuration save and load ZTP enable/disable and status Show tech support - How I did it The host service is a Python process that listens for requests via D-Bus. It will then service those requests and send a response back to the requestor. This PR only introduces the host service infrastructure. Applications that need access to the host services must add applets that will register on D-Bus endpoints to service the appropriate functionality. - How to verify it - Description for the changelog Add SONiC Host Service for container to execute select commands in host Signed-off-by: Nirenjan Krishnan <Nirenjan.Krishnan@dell.com>
1 parent a1335aa commit bb57cce

17 files changed

+257
-1
lines changed

files/build_templates/sonic_debian_extension.j2

+6
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ sudo dpkg --root=$FILESYSTEM_ROOT -i $debs_path/sonic-ztp_*.deb || \
196196
sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y install -f
197197
{% endif %}
198198

199+
{% if include_host_service == "y" %}
200+
# Install SONiC Host Service (and its dependencies via 'apt-get -y install -f')
201+
sudo dpkg --root=$FILESYSTEM_ROOT -i $debs_path/sonic-host-service_*.deb || \
202+
sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y install -f
203+
{% endif %}
204+
199205
# SONiC utilities installs bash-completion as a dependency. However, it is disabled by default
200206
# in bash.bashrc, so we copy a version of the file with it enabled here.
201207
sudo cp -f $IMAGE_CONFIGS/bash/bash.bashrc $FILESYSTEM_ROOT/etc/

rules/config

+4
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ INCLUDE_SFLOW = y
125125
# INCLUDE_MGMT_FRAMEWORK - build docker-sonic-mgmt-framework for CLI and REST server support
126126
INCLUDE_MGMT_FRAMEWORK = y
127127

128+
# INCLUDE_HOST_SERVICE - build sonic-host-services for mgmt-framework and/or
129+
# telemetry containers to access host functionality
130+
INCLUDE_HOST_SERVICE = n
131+
128132
# INCLUDE_RESTAPI - build docker-sonic-restapi for configuring the switch using REST APIs
129133
INCLUDE_RESTAPI = n
130134

rules/docker-sonic-mgmt-framework.mk

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ $(DOCKER_MGMT_FRAMEWORK)_CONTAINER_NAME = mgmt-framework
2828
$(DOCKER_MGMT_FRAMEWORK)_RUN_OPT += --privileged -t
2929
$(DOCKER_MGMT_FRAMEWORK)_RUN_OPT += -v /etc/sonic:/etc/sonic:ro
3030
$(DOCKER_MGMT_FRAMEWORK)_RUN_OPT += -v /etc:/host_etc:ro
31+
$(DOCKER_MGMT_FRAMEWORK)_RUN_OPT += -v /var/run/dbus:/var/run/dbus:rw
3132
$(DOCKER_MGMT_FRAMEWORK)_RUN_OPT += --mount type=bind,source="/var/platform/",target="/mnt/platform/"
3233

3334
$(DOCKER_MGMT_FRAMEWORK)_BASE_IMAGE_FILES += sonic-cli:/usr/bin/sonic-cli

rules/docker-telemetry.mk

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ endif
2626
$(DOCKER_TELEMETRY)_CONTAINER_NAME = telemetry
2727
$(DOCKER_TELEMETRY)_RUN_OPT += --privileged -t
2828
$(DOCKER_TELEMETRY)_RUN_OPT += -v /etc/sonic:/etc/sonic:ro
29+
$(DOCKER_TELEMETRY)_RUN_OPT += -v /var/run/dbus:/var/run/dbus:rw
2930
$(DOCKER_TELEMETRY)_RUN_OPT += --mount type=bind,source="/var/platform/",target="/mnt/platform/"
3031

3132
$(DOCKER_TELEMETRY)_FILES += $(SUPERVISOR_PROC_EXIT_LISTENER_SCRIPT)

rules/sonic-host-service.dep

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
SPATH := $($(SONIC_HOST_SERVICE)_SRC_PATH)
3+
DEP_FILES := $(SONIC_COMMON_FILES_LIST) rules/sonic-host-service.mk rules/sonic-host-service.dep
4+
SMDEP_FILES := $(addprefix $(SPATH)/,$(shell cd $(SPATH) && git ls-files))
5+
6+
$(SONIC_HOST_SERVICE)_CACHE_MODE := GIT_CONTENT_SHA
7+
$(SONIC_HOST_SERVICE)_DEP_FLAGS := $(SONIC_COMMON_FLAGS_LIST)
8+
$(SONIC_HOST_SERVICE)_DEP_FILES := $(DEP_FILES)
9+
$(SONIC_HOST_SERVICE)_SMDEP_FILES := $(SMDEP_FILES)
10+
$(SONIC_HOST_SERVICE)_SMDEP_PATHS := $(SPATH)
11+

rules/sonic-host-service.mk

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SONiC host service package
2+
3+
ifeq ($(INCLUDE_HOST_SERVICE), y)
4+
5+
SONIC_HOST_SERVICE = sonic-host-service_1.0.0_all.deb
6+
$(SONIC_HOST_SERVICE)_SRC_PATH = $(SRC_PATH)/sonic-host-service
7+
SONIC_MAKE_DEBS += $(SONIC_HOST_SERVICE)
8+
9+
endif

slave.mk

+4-1
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ $(info "VS_PREPARE_MEM" : "$(VS_PREPARE_MEM)")
224224
$(info "INCLUDE_MGMT_FRAMEWORK" : "$(INCLUDE_MGMT_FRAMEWORK)")
225225
$(info "INCLUDE_ICCPD" : "$(INCLUDE_ICCPD)")
226226
$(info "INCLUDE_SYSTEM_TELEMETRY" : "$(INCLUDE_SYSTEM_TELEMETRY)")
227+
$(info "INCLUDE_HOST_SERVICE" : "$(INCLUDE_HOST_SERVICE)")
227228
$(info "INCLUDE_RESTAPI" : "$(INCLUDE_RESTAPI)")
228229
$(info "INCLUDE_SFLOW" : "$(INCLUDE_SFLOW)")
229230
$(info "INCLUDE_NAT" : "$(INCLUDE_NAT)")
@@ -794,7 +795,8 @@ $(addprefix $(TARGET_PATH)/, $(SONIC_INSTALLERS)) : $(TARGET_PATH)/% : \
794795
$(PYTHON_SWSSCOMMON)) \
795796
$$(addprefix $(TARGET_PATH)/,$$($$*_DOCKERS)) \
796797
$$(addprefix $(FILES_PATH)/,$$($$*_FILES)) \
797-
$(if $(findstring y,$(ENABLE_ZTP)),$(addprefix $(IMAGE_DISTRO_DEBS_PATH)/,$(SONIC_ZTP))) \
798+
$(if $(findstring y,$(ENABLE_ZTP)),$(addprefix $(IMAGE_DISTRO_DEBS_PATH)/,$(SONIC_ZTP))) \
799+
$(if $(findstring y,$(INCLUDE_HOST_SERVICE)),$(addprefix $(IMAGE_DISTRO_DEBS_PATH)/,$(SONIC_HOST_SERVICE))) \
798800
$(addprefix $(PYTHON_DEBS_PATH)/,$(SONIC_UTILS)) \
799801
$(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_PY_COMMON_PY2)) \
800802
$(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_PY_COMMON_PY3)) \
@@ -821,6 +823,7 @@ $(addprefix $(TARGET_PATH)/, $(SONIC_INSTALLERS)) : $(TARGET_PATH)/% : \
821823
export enable_ztp="$(ENABLE_ZTP)"
822824
export include_system_telemetry="$(INCLUDE_SYSTEM_TELEMETRY)"
823825
export include_restapi="$(INCLUDE_RESTAPI)"
826+
export include_host_service="$(INCLUDE_HOST_SERVICE)"
824827
export include_nat="$(INCLUDE_NAT)"
825828
export include_sflow="$(INCLUDE_SFLOW)"
826829
export include_mgmt_framework="$(INCLUDE_MGMT_FRAMEWORK)"

src/sonic-host-service/Makefile

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
################################################################################
2+
# #
3+
# Copyright 2020 Dell Inc. #
4+
# #
5+
# Licensed under the Apache License, Version 2.0 (the "License"); #
6+
# you may not use this file except in compliance with the License. #
7+
# You may obtain a copy of the License at #
8+
# #
9+
# http://www.apache.org/licenses/LICENSE-2.0 #
10+
# #
11+
# Unless required by applicable law or agreed to in writing, software #
12+
# distributed under the License is distributed on an "AS IS" BASIS, #
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
14+
# See the License for the specific language governing permissions and #
15+
# limitations under the License. #
16+
# #
17+
################################################################################
18+
19+
TOPDIR := $(abspath .)
20+
MAIN_TARGET = sonic-host-service_1.0.0_all.deb
21+
INSTALL := /usr/bin/install
22+
23+
$(addprefix $(DEST)/, $(MAIN_TARGET)): $(DEST)/% :
24+
dpkg-buildpackage -us -uc -b
25+
mv ../$(MAIN_TARGET) $(DEST)/
26+
27+
SOURCES := $(wildcard '*.*') $(wildcard 'host_modules/*.py')
28+
install: $(SOURCES)
29+
# Scripts for host service
30+
$(INSTALL) -d $(DESTDIR)/usr/lib/sonic_host_service/host_modules
31+
$(INSTALL) -D $(TOPDIR)/sonic_host_server.py $(DESTDIR)/usr/lib/sonic_host_service
32+
$(INSTALL) -D $(TOPDIR)/host_modules/*.py $(DESTDIR)/usr/lib/sonic_host_service/host_modules
33+
34+
# D-Bus permissions configuration
35+
$(INSTALL) -d $(DESTDIR)/etc/dbus-1/system.d
36+
$(INSTALL) -D $(TOPDIR)/org.sonic.hostservice.conf $(DESTDIR)/etc/dbus-1/system.d
37+
38+
# systemd unit
39+
$(INSTALL) -d $(DESTDIR)/lib/systemd/system
40+
$(INSTALL) -D $(TOPDIR)/sonic-hostservice.service $(DESTDIR)/lib/systemd/system
41+
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.debhelper
2+
sonic-host-service.*
3+
sonic-host-service/
4+
files
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
sonic-host-service (1.0.0) UNRELEASED; urgency=low
2+
3+
* Initial release.
4+
5+
-- Nirenjan Krishnan <Nirenjan.Krishnan@dell.com> Mon, 22 Jun 2020 00:00:00 +0000

src/sonic-host-service/debian/compat

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
9

src/sonic-host-service/debian/control

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Source: sonic-host-service
2+
Maintainer: Nirenjan Krishnan <Nirenjan.Krishnan@dell.com>
3+
Build-Depends: debhelper (>= 8.0.0),
4+
dh-systemd
5+
Vcs-Git: https://github.com/Azure/sonic-buildimage
6+
Homepage: https://github.com/Azure/SONiC/
7+
Standards-Version: 3.9.3
8+
Section: net
9+
10+
Package: sonic-host-service
11+
Priority: extra
12+
Architecture: all
13+
Depends: python3-systemd, python3-dbus, python3-gi, ${misc:Depends}
14+
Description: SONiC Host Service
15+

src/sonic-host-service/debian/rules

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/make -f
2+
%:
3+
dh $@ --with systemd --parallel
4+
5+
override_dh_auto_clean:
6+
override_dh_auto_test:
7+
override_dh_auto_build:
8+
override_dh_auto_install:
9+
make install DESTDIR=debian/sonic-host-service
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""Base class for host modules"""
2+
3+
import dbus.service
4+
import dbus
5+
6+
BUS_NAME_BASE = 'org.SONiC.HostService'
7+
BUS_PATH = '/org/SONiC/HostService'
8+
9+
def bus_name(mod_name):
10+
"""Return the bus name for the service"""
11+
return BUS_NAME_BASE + '.' + mod_name
12+
13+
def bus_path(mod_name):
14+
"""Return the bus path for the service"""
15+
return BUS_PATH + '/' + mod_name
16+
17+
method = dbus.service.method
18+
19+
class HostService(dbus.service.Object):
20+
"""Service class for top level DBus endpoint"""
21+
def __init__(self, mod_name):
22+
self.bus = dbus.SystemBus()
23+
self.bus_name = dbus.service.BusName(BUS_NAME_BASE, self.bus)
24+
super(HostService, self).__init__(self.bus_name, BUS_PATH)
25+
26+
class HostModule(dbus.service.Object):
27+
"""Base class for all host modules"""
28+
def __init__(self, mod_name):
29+
self.bus = dbus.SystemBus()
30+
self.bus_name = dbus.service.BusName(bus_name(mod_name), self.bus)
31+
super(HostModule, self).__init__(self.bus_name, bus_path(mod_name))
32+
33+
def register():
34+
return HostService, "host_service"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE busconfig PUBLIC
2+
"-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
3+
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
4+
<busconfig>
5+
6+
<!-- Only root can own the bus -->
7+
8+
<policy user="root">
9+
<allow own_prefix="org.SONiC.HostService"/>
10+
</policy>
11+
12+
<!-- Allow user "root" to invoke methods on the bus -->
13+
<policy user="root">
14+
<allow send_destination="org.SONiC.HostService"/>
15+
<allow receive_sender="org.SONiC.HostService"/>
16+
</policy>
17+
18+
</busconfig>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[Unit]
2+
Description=SONiC Host Service
3+
4+
[Service]
5+
Type=dbus
6+
BusName=org.SONiC.HostService
7+
8+
ExecStart=/usr/bin/python3 -u /usr/lib/sonic_host_service/sonic_host_server.py
9+
10+
Restart=on-failure
11+
RestartSec=10
12+
TimeoutStopSec=3
13+
14+
[Install]
15+
WantedBy=multi-user.target
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env python3
2+
"""Host Service to handle docker-to-host communication"""
3+
4+
import os
5+
import os.path
6+
import glob
7+
import importlib
8+
import sys
9+
10+
import dbus
11+
import dbus.service
12+
import dbus.mainloop.glib
13+
14+
from gi.repository import GObject
15+
16+
def register_modules():
17+
"""Register all host modules"""
18+
mod_path = os.path.join(os.path.dirname(__file__), 'host_modules')
19+
sys.path.append(mod_path)
20+
for mod_file in glob.glob(os.path.join(mod_path, '*.py')):
21+
if os.path.isfile(mod_file) and not mod_file.endswith('__init__.py'):
22+
mod_name = os.path.basename(mod_file)[:-3]
23+
module = importlib.import_module(mod_name)
24+
25+
register_cb = getattr(module, 'register', None)
26+
if not register_cb:
27+
raise Exception('Missing register function for ' + mod_name)
28+
29+
register_dbus(register_cb)
30+
31+
def register_dbus(register_cb):
32+
"""Register DBus handlers for individual modules"""
33+
handler_class, mod_name = register_cb()
34+
handlers[mod_name] = handler_class(mod_name)
35+
36+
# Create a main loop reactor
37+
GObject.threads_init()
38+
dbus.mainloop.glib.threads_init()
39+
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
40+
loop = GObject.MainLoop()
41+
handlers = {}
42+
43+
class SignalManager(object):
44+
''' This is used to manage signals received (e.g. SIGINT).
45+
When stopping a process (systemctl stop [service]), systemd sends
46+
a SIGTERM signal.
47+
'''
48+
shutdown = False
49+
def __init__(self):
50+
''' Install signal handlers.
51+
52+
SIGTERM is invoked when systemd wants to stop the daemon.
53+
For example, "systemctl stop mydaemon.service"
54+
or, "systemctl restart mydaemon.service"
55+
56+
'''
57+
import signal
58+
signal.signal(signal.SIGTERM, self.sigterm_hdlr)
59+
60+
def sigterm_hdlr(self, _signum, _frame):
61+
self.shutdown = True
62+
loop.quit()
63+
64+
sigmgr = SignalManager()
65+
register_modules()
66+
67+
# Only run if we actually have some handlers
68+
if handlers:
69+
import systemd.daemon
70+
systemd.daemon.notify("READY=1")
71+
72+
while not sigmgr.shutdown:
73+
loop.run()
74+
if sigmgr.shutdown:
75+
break
76+
77+
systemd.daemon.notify("STOPPING=1")
78+
else:
79+
print("No handlers to register, quitting...")

0 commit comments

Comments
 (0)