From 934acbc2fe6d19c33433a6ec7a9984dfa4a4251b Mon Sep 17 00:00:00 2001 From: reubenmiller Date: Sat, 18 Nov 2023 22:40:02 +0100 Subject: [PATCH] fix(sysvinit-yocto): add missing configuration and log service definitions --- services/generate.sh | 2 +- .../init.d/tedge-configuration-plugin | 159 ++++++++++++++++++ .../sysvinit-yocto/init.d/tedge-log-plugin | 4 +- 3 files changed, 162 insertions(+), 3 deletions(-) create mode 100755 services/sysvinit-yocto/init.d/tedge-configuration-plugin diff --git a/services/generate.sh b/services/generate.sh index e0b5679..9930419 100755 --- a/services/generate.sh +++ b/services/generate.sh @@ -132,7 +132,7 @@ do SHORTNAME="${SHORTNAME:-$COMMAND}" if [ -z "$TEMPLATE_FOR" ]; then - TEMPLATE_FOR="systemd openrc sysvinit sysvinit s6_overlay runit supervisord" + TEMPLATE_FOR="systemd openrc sysvinit sysvinit-yocto s6_overlay runit supervisord" fi # Validate mandatory arguments diff --git a/services/sysvinit-yocto/init.d/tedge-configuration-plugin b/services/sysvinit-yocto/init.d/tedge-configuration-plugin new file mode 100755 index 0000000..c1ae234 --- /dev/null +++ b/services/sysvinit-yocto/init.d/tedge-configuration-plugin @@ -0,0 +1,159 @@ +#!/bin/sh +### BEGIN INIT INFO +# Provides: tedge-configuration-plugin +# Required-Start: $remote_fs $syslog +# Required-Stop: $remote_fs $syslog +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: Thin-edge device configuration management +# Description: Thin-edge device configuration management +### END INIT INFO + +dir="/var" +DAEMON="/usr/bin/tedge-configuration-plugin" +DAEMON_USER="root" +DAEMON_ARGS="" + +name="tedge-configuration-plugin" +PIDFILE=/run/lock/$name.lock +stdout_log="/var/log/$name.log" +stderr_log="/var/log/$name.err" + +user_exists() { + if command -V getent > /dev/null 2>&1; then + getent passwd "$1" >/dev/null + else + # Fallback to plain grep, as busybox does not have getent + grep -q "^${1}:" /etc/passwd + fi +} + +# Check if sudo or su is installed, otherwise don't use it +SUDO="" +if [ -n "$DAEMON_USER" ]; then + if user_exists "$DAEMON_USER"; then + if command -V sudo >/dev/null 2>&1; then + SUDO="sudo" + if [ -n "$DAEMON_USER" ] && user_exists "$DAEMON_USER"; then + SUDO="sudo -u $DAEMON_USER" + fi + elif command -V runuser >/dev/null 2>&1; then + SUDO="runuser" + if [ -n "$DAEMON_USER" ] && user_exists "$DAEMON_USER"; then + SUDO="runuser -u $DAEMON_USER --" + fi + elif command -V su >/dev/null 2>&1; then + # Note: using su requires a user which can login + # su -s /bin/sh -c '$DAEMON $DAEMON_AGS' $DAEMON_USER + SUDO="su" + if [ -n "$DAEMON_USER" ] && user_exists "$DAEMON_USER"; then + SUDO="su - $DAEMON_USER" + fi + fi + else + echo "WARNING: Daemon user does not exist, so starting the service as current user. DAEMON_USER=$DAEMON_USER" + fi +fi + +get_pid() { cat "$PIDFILE"; } + +is_running() { + # shellcheck disable=SC2009 + # Use ps/grep fallback as busybox does not support the "ps -p" option + if command -V pidof >/dev/null 2>&1; then + pidof tedge-configuration-plugin >/dev/null + else + PROCESSES=$(ps -x || ps) + [ -f "$PIDFILE" ] && (echo "$PROCESSES" | grep "^[[:blank:]]*$(get_pid)" >/dev/null 2>&1) + fi +} + +case "$1" in + start) + if is_running; then + echo "Already started" + else + echo "Starting $name (using '$SUDO $DAEMON $DAEMON_ARGS')" + cd "$dir" || (echo "Failed changing directory"; exit 1) + $SUDO $DAEMON $DAEMON_ARGS >> "$stdout_log" 2>> "$stderr_log" & + # TODO: is it ok to let the process create the pidfile itself? + # echo $! > "$PIDFILE" + + i=10 + printf "Waiting for %s.." "$name" + while [ $i -gt 0 ]; do + if is_running; then + break + fi + printf "." + i=$((i-1)) + sleep 1 + done + + if ! is_running; then + echo "Unable to start, see $stdout_log and $stderr_log" + exit 1 + else + echo "started" + fi + fi + ;; + stop) + if is_running; then + printf "Stopping (pid=%s) %s.." "$(get_pid)" "$name" + kill "$(get_pid)" + i=30 + while [ $i -gt 0 ]; do + if ! is_running; then + break + fi + printf "." + i=$((i-1)) + sleep 1 + done + echo + + if is_running; then + echo "Not stopped; may still be shutting down or shutdown may have failed" + exit 1 + else + echo "stopped" + if [ -f "$PIDFILE" ]; then + rm -f "$PIDFILE" + fi + fi + else + echo "Not running" + fi + ;; + # reload) + # if is_running; then + # echo "Reloading configuration" + # kill -HUP "$(get_pid)" + # else + # echo "Not running" + # fi + # ;; + restart) + "$0" stop + if is_running; then + echo "Unable to stop, will not attempt to start" + exit 1 + fi + "$0" start + ;; + status) + if is_running; then + echo "Running" + else + echo "Stopped" + exit 1 + fi + ;; + *) + echo "Usage: $0 {start|stop|restart|status}" + exit 1 + ;; +esac + +exit 0 \ No newline at end of file diff --git a/services/sysvinit-yocto/init.d/tedge-log-plugin b/services/sysvinit-yocto/init.d/tedge-log-plugin index 9e1b508..0c3c526 100755 --- a/services/sysvinit-yocto/init.d/tedge-log-plugin +++ b/services/sysvinit-yocto/init.d/tedge-log-plugin @@ -5,8 +5,8 @@ # Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 -# Short-Description: Thin-edge logfile retriever for Cumulocity -# Description: Thin-edge logfile retriever for Cumulocity +# Short-Description: thin-edge.io log file retriever +# Description: thin-edge.io log file retriever ### END INIT INFO dir="/var"