Skip to content

Commit

Permalink
Merge pull request #39 from thin-edge/add-missing-sysvinit-yocto-defi…
Browse files Browse the repository at this point in the history
…nitions

fix(sysvinit-yocto): add missing configuration and log service definitions
  • Loading branch information
reubenmiller authored Nov 18, 2023
2 parents 77275a5 + 934acbc commit bbef79f
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 3 deletions.
2 changes: 1 addition & 1 deletion services/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
159 changes: 159 additions & 0 deletions services/sysvinit-yocto/init.d/tedge-configuration-plugin
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions services/sysvinit-yocto/init.d/tedge-log-plugin
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit bbef79f

Please sign in to comment.