diff mbox series

[8/8] thingsboard-gateway: Update systemd service

Message ID 20251023-wip-thingsboard-gateway-master-v1-8-570de0ecafb2@phytec.de
State New
Headers show
Series thingsboard-gateway: Make build and run succeed | expand

Commit Message

Martin Schwan Oct. 23, 2025, 9:46 a.m. UTC
Update the systemd service file to reflect the changes of the source
repository.

Add a user and group "thingsboard_gateway" which the service runs under,
instead of root.

Store temporary files, like logs, in /var and make use of the newly
created user and group.

Additionally, the service should start after and wants a network
connection to be online, not just be activated.

Signed-off-by: Martin Schwan <m.schwan@phytec.de>
---
 .../thingsboard-gateway.service                    | 18 +++++++++-------
 .../thingsboard-gateway_3.7.4.bb                   | 24 +++++++++++++++++++---
 2 files changed, 32 insertions(+), 10 deletions(-)
diff mbox series

Patch

diff --git a/meta-oe/dynamic-layers/meta-python/recipes-connectivity/thingsboard-gateway/thingsboard-gateway/thingsboard-gateway.service b/meta-oe/dynamic-layers/meta-python/recipes-connectivity/thingsboard-gateway/thingsboard-gateway/thingsboard-gateway.service
index 5dd352a553fc71562f444561e6424bee94814691..0b7601d494b2b10e2e5cd39d25db7be4ba82b437 100644
--- a/meta-oe/dynamic-layers/meta-python/recipes-connectivity/thingsboard-gateway/thingsboard-gateway/thingsboard-gateway.service
+++ b/meta-oe/dynamic-layers/meta-python/recipes-connectivity/thingsboard-gateway/thingsboard-gateway/thingsboard-gateway.service
@@ -1,13 +1,17 @@ 
 [Unit]
-Description = Systemd service for Thingsboard Gateway
-After       = network.target
+Description=ThingsBoard Gateway
+After=network-online.target
+Wants=network-online.target
+ConditionPathExists=/etc/thingsboard-gateway/config/tb_gateway.json
 
 [Service]
-ExecStart   = /usr/bin/python3 /usr/bin/thingsboard-gateway
-ExecStop    = /bin/kill -INT $MAINPID
-ExecReload  = /bin/kill -TERM $MAINPID
-Restart     = always
-Type        = simple
+Type=simple
+User=thingsboard_gateway
+Group=thingsboard_gateway
+ExecStart=/usr/bin/python3 /usr/bin/thingsboard-gateway
+ExecStop=/bin/kill -INT $MAINPID
+ExecReload=/bin/kill -TERM $MAINPID
+Restart=on-failure
 
 [Install]
 WantedBy=multi-user.target
diff --git a/meta-oe/dynamic-layers/meta-python/recipes-connectivity/thingsboard-gateway/thingsboard-gateway_3.7.4.bb b/meta-oe/dynamic-layers/meta-python/recipes-connectivity/thingsboard-gateway/thingsboard-gateway_3.7.4.bb
index 8ff06285f9be60a09c1d49c2f07140e8212d22fc..7a82414937da8bdf3094c825ec5dc2ce4b429c1d 100644
--- a/meta-oe/dynamic-layers/meta-python/recipes-connectivity/thingsboard-gateway/thingsboard-gateway_3.7.4.bb
+++ b/meta-oe/dynamic-layers/meta-python/recipes-connectivity/thingsboard-gateway/thingsboard-gateway_3.7.4.bb
@@ -43,21 +43,39 @@  RDEPENDS:${PN} += " python3-jsonpath-rw \
 
 SRC_URI += "file://thingsboard-gateway.service"
 
-
-inherit systemd
+inherit systemd useradd
 
 SYSTEMD_PACKAGES = "${PN}"
 SYSTEMD_SERVICE:${PN} = "thingsboard-gateway.service"
 
+USERADD_PACKAGES = "${PN}"
+USERADD_PARAM:${PN} = " \
+    --system --no-create-home \
+    --comment 'ThingsBoard-Gateway Service' \
+    --home-dir ${localstatedir}/lib/${BPN} \
+    --shell ${base_sbindir}/nologin \
+    --gid thingsboard_gateway thingsboard_gateway"
+GROUPADD_PARAM:${PN} = "--system thingsboard_gateway"
+
 FILES:${PN} += "/etc \
                 /lib \
                 /usr \
+                ${localstatedir} \
 "
 
 do_install:append(){
     install -d ${D}${sysconfdir}/${BPN}/config
     install -m 0644 ${S}/thingsboard_gateway/config/*.json ${D}${sysconfdir}/${BPN}/config
+    chown -R thingsboard_gateway:thingsboard_gateway ${D}${sysconfdir}/${BPN}
 
-    install -d ${D}${systemd_unitdir}/system/
+    install -d ${D}${systemd_system_unitdir}/
     install -m 0644 ${UNPACKDIR}/thingsboard-gateway.service ${D}${systemd_system_unitdir}/thingsboard-gateway.service
+
+    if ${@bb.utils.contains('DISTRO_FEATURES', 'systemd', 'true', 'false', d)}; then
+        install -d ${D}${sysconfdir}/tmpfiles.d
+        echo "d ${localstatedir}/log/${BPN} 0755 thingsboard_gateway thingsboard_gateway -" \
+            > ${D}${sysconfdir}/tmpfiles.d/${BPN}.conf
+        echo "d ${localstatedir}/lib/${BPN} 0755 thingsboard_gateway thingsboard_gateway -" \
+            >> ${D}${sysconfdir}/tmpfiles.d/${BPN}.conf
+    fi
 }