- Why I did it To move ‘sonic-host-service’ which is currently built as a separate package to ‘sonic-host-services' package. - How I did it - Moved 'sonic-host-server' to 'src/sonic-host-services' and included it as part of the python3 wheel. - Other files were moved to 'src/sonic-host-services-data' and included as part of the deb package. - Changed build option ‘INCLUDE_HOST_SERVICE’ to ‘ENABLE_HOST_SERVICE_ON_START’ for enabling sonic-hostservice at boot-up by default.
80 lines
2.2 KiB
Python
Executable File
80 lines
2.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Host Service to handle docker-to-host communication"""
|
|
|
|
import os
|
|
import os.path
|
|
import glob
|
|
import importlib
|
|
import sys
|
|
|
|
import dbus
|
|
import dbus.service
|
|
import dbus.mainloop.glib
|
|
|
|
from gi.repository import GObject
|
|
|
|
def register_modules():
|
|
"""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'):
|
|
mod_name = os.path.basename(mod_file)[:-3]
|
|
module = importlib.import_module(mod_name)
|
|
|
|
register_cb = getattr(module, 'register', None)
|
|
if not register_cb:
|
|
raise Exception('Missing register function for ' + mod_name)
|
|
|
|
register_dbus(register_cb)
|
|
|
|
def register_dbus(register_cb):
|
|
"""Register DBus handlers for individual modules"""
|
|
handler_class, mod_name = register_cb()
|
|
handlers[mod_name] = handler_class(mod_name)
|
|
|
|
# Create a main loop reactor
|
|
GObject.threads_init()
|
|
dbus.mainloop.glib.threads_init()
|
|
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
|
|
loop = GObject.MainLoop()
|
|
handlers = {}
|
|
|
|
class SignalManager(object):
|
|
''' This is used to manage signals received (e.g. SIGINT).
|
|
When stopping a process (systemctl stop [service]), systemd sends
|
|
a SIGTERM signal.
|
|
'''
|
|
shutdown = False
|
|
def __init__(self):
|
|
''' Install signal handlers.
|
|
|
|
SIGTERM is invoked when systemd wants to stop the daemon.
|
|
For example, "systemctl stop mydaemon.service"
|
|
or, "systemctl restart mydaemon.service"
|
|
|
|
'''
|
|
import signal
|
|
signal.signal(signal.SIGTERM, self.sigterm_hdlr)
|
|
|
|
def sigterm_hdlr(self, _signum, _frame):
|
|
self.shutdown = True
|
|
loop.quit()
|
|
|
|
sigmgr = SignalManager()
|
|
register_modules()
|
|
|
|
# Only run if we actually have some handlers
|
|
if handlers:
|
|
import systemd.daemon
|
|
systemd.daemon.notify("READY=1")
|
|
|
|
while not sigmgr.shutdown:
|
|
loop.run()
|
|
if sigmgr.shutdown:
|
|
break
|
|
|
|
systemd.daemon.notify("STOPPING=1")
|
|
else:
|
|
print("No handlers to register, quitting...")
|