ソースを参照

Add tools/install-service.sh: systemd, sysvinit or OpenRC service for radio-ui

k4be 2 週間 前
親
コミット
e5ed82c2fa
2 ファイル変更、275 行追加、0 行削除
  1. 19 0
      README.md
  2. 256 0
      tools/install-service.sh

+ 19 - 0
README.md

@@ -18,6 +18,7 @@ submodule `panel/`. The keys' wiring and device tree are in
 | `radio/libpanel.c` | The panel core and simulator as a library for the application |
 | `radio-ui` | Starts the application |
 | `tests/` | Application tests, a fake MPD server and `hw_test.sh` |
+| `tools/install-service.sh` | Installs radio-ui as a systemd, sysvinit or OpenRC service |
 | `panel/` | Submodule: s1d13700-tca (panel core, simulator, DRM driver, device tree overlay) |
 | `keyboard/` | Submodule: radio-keyboard-tca (key expander wiring, device tree overlay) |
 | `docs/REPORT.md` | The port from the LPC1114 firmware: decisions, verification, history |
@@ -203,6 +204,24 @@ On a kernel without the driver (Armbian 4.19), use `radio-ui i2c --bus 1`;
 set the bus to 400 kHz (`clock-frequency` on i2c1), the mv64xxx default of
 100 kHz makes a playlist redraw take about half a second.
 
+### As a service
+
+`tools/install-service.sh` (run with sudo) writes a service that starts
+`radio-ui` from this checkout at boot, after MPD, as the user who ran sudo,
+then enables and starts it. The init system is detected (`-i systemd`,
+`sysvinit` or `openrc` overrides); the arguments after `--` go to radio-ui,
+`drm` by default:
+
+    make lib                                   # as yourself, first
+    sudo tools/install-service.sh              # radio-ui drm
+    sudo tools/install-service.sh -- i2c --bus 1 --host radio.lo
+    tools/install-service.sh -p -i openrc      # print the file only
+    sudo tools/install-service.sh -r           # stop, disable and remove
+
+`-u USER` names another user; it needs the `video`, `input` and `i2c` groups
+(the script warns if not). systemd logs to the journal, sysvinit and OpenRC to
+`/var/log/radio-ui.log`. `-N` writes the file without enabling or starting it.
+
 ## Not settled
 
 - TC/R (`--tcr`) for the frame rate; 162 comes from the AVR project's 2 bpp setup

+ 256 - 0
tools/install-service.sh

@@ -0,0 +1,256 @@
+#!/bin/sh
+# Install radio-ui as a system service (systemd, sysvinit or OpenRC).
+#
+#   sudo tools/install-service.sh [OPTIONS] [-- RADIO-UI ARGS...]
+#
+# The service runs ./radio-ui from this checkout (not a copy), as USER, with
+# the given arguments (default: drm). Build the library first, as yourself:
+# make lib.
+#
+# OPTIONS:
+#   -i INIT   systemd, sysvinit or openrc (default: the running one)
+#   -u USER   user the service runs as (default: $SUDO_USER; root refused
+#             unless named here). Needs the video, input and i2c groups.
+#   -n NAME   service name (default: radio-ui)
+#   -N        do not enable or start the service, only write its file
+#   -p        print the service file and exit; nothing is installed
+#   -r        remove the service: stop, disable, delete its file
+#   -h        this help
+#
+# Examples:
+#   sudo tools/install-service.sh
+#   sudo tools/install-service.sh -- drm --host radio.lo
+#   sudo tools/install-service.sh -- i2c --bus 1
+#   sudo tools/install-service.sh -r
+set -eu
+
+dir=$(cd "$(dirname "$0")/.." && pwd -P)
+prog=$dir/radio-ui
+
+init= user=${SUDO_USER:-} name=radio-ui activate=1 print=0 remove=0
+while getopts i:u:n:Nprh opt; do
+	case $opt in
+	i) init=$OPTARG ;;
+	u) user=$OPTARG ;;
+	n) name=$OPTARG ;;
+	N) activate=0 ;;
+	p) print=1 ;;
+	r) remove=1 ;;
+	*) sed -n '2,24s/^# \{0,1\}//p' "$0"; [ "$opt" = h ] && exit 0; exit 2 ;;
+	esac
+done
+shift $((OPTIND - 1))
+[ $# -gt 0 ] || set -- drm
+
+die() { echo "$0: $*" >&2; exit 1; }
+
+# The arguments go into ExecStart= or a shell script unquoted: keep them plain.
+safe() {
+	case $1 in
+	''|*[!A-Za-z0-9._/:=,@+-]*) return 1 ;;
+	esac
+}
+for a in "$name" "$dir" "$@"; do
+	safe "$a" || die "'$a': only letters, digits and ._/:=,@+- are allowed"
+done
+args=$*
+
+if [ -z "$init" ]; then
+	if [ -d /run/systemd/system ]; then init=systemd
+	elif [ -d /run/openrc ] || command -v openrc-run >/dev/null 2>&1; then init=openrc
+	else init=sysvinit
+	fi
+fi
+case $init in
+systemd) file=/etc/systemd/system/$name.service ;;
+sysvinit|openrc) file=/etc/init.d/$name ;;
+*) die "unknown init system '$init' (systemd, sysvinit, openrc)" ;;
+esac
+log=/var/log/$name.log
+
+# ---- the service files -------------------------------------------------------
+
+unit_systemd() {
+	cat <<EOF
+[Unit]
+Description=Radio front panel (radio-ui)
+After=network.target sound.target mpd.service
+Wants=mpd.service
+
+[Service]
+Type=simple
+User=$user
+WorkingDirectory=$dir
+Environment=PYTHONUNBUFFERED=1
+ExecStart=$prog $args
+Restart=on-failure
+RestartSec=5
+
+[Install]
+WantedBy=multi-user.target
+EOF
+}
+
+unit_openrc() {
+	cat <<EOF
+#!/sbin/openrc-run
+# Radio front panel (radio-ui); written by tools/install-service.sh
+
+description="Radio front panel (radio-ui)"
+command="$prog"
+command_args="$args"
+command_user="$user"
+command_background=yes
+pidfile="/run/$name.pid"
+directory="$dir"
+output_log="$log"
+error_log="$log"
+export PYTHONUNBUFFERED=1
+
+depend() {
+	need localmount
+	after net mpd
+	use mpd
+}
+
+start_pre() {
+	checkpath -f -m 0644 -o "$user" "$log"
+}
+EOF
+}
+
+unit_sysvinit() {
+	cat <<EOF
+#!/bin/sh
+### BEGIN INIT INFO
+# Provides:          $name
+# Required-Start:    \$local_fs \$remote_fs \$network
+# Required-Stop:     \$local_fs \$remote_fs \$network
+# Should-Start:      mpd
+# Should-Stop:       mpd
+# Default-Start:     2 3 4 5
+# Default-Stop:      0 1 6
+# Short-Description: Radio front panel (radio-ui)
+### END INIT INFO
+# Written by tools/install-service.sh
+
+NAME=$name
+DAEMON=$prog
+ARGS="$args"
+USER=$user
+DIR=$dir
+PIDFILE=/run/\$NAME.pid
+LOG=$log
+export PYTHONUNBUFFERED=1
+
+running() {
+	[ -f "\$PIDFILE" ] && kill -0 "\$(cat "\$PIDFILE")" 2>/dev/null
+}
+
+do_start() {
+	if running; then echo "\$NAME already running"; return 0; fi
+	touch "\$LOG" && chown "\$USER" "\$LOG"
+	start-stop-daemon --start --background --make-pidfile --pidfile "\$PIDFILE" \\
+		--chuid "\$USER" --chdir "\$DIR" --startas /bin/sh -- \\
+		-c "exec \$DAEMON \$ARGS >>\$LOG 2>&1"
+}
+
+do_stop() {
+	start-stop-daemon --stop --retry TERM/10/KILL/5 --pidfile "\$PIDFILE" --oknodo
+	rm -f "\$PIDFILE"
+}
+
+case "\$1" in
+start)   echo "Starting \$NAME"; do_start ;;
+stop)    echo "Stopping \$NAME"; do_stop ;;
+restart|force-reload) do_stop; do_start ;;
+status)
+	if running; then echo "\$NAME is running"; exit 0
+	else echo "\$NAME is not running"; exit 3
+	fi ;;
+*) echo "Usage: \$0 {start|stop|restart|force-reload|status}" >&2; exit 2 ;;
+esac
+EOF
+}
+
+# ---- enable, start, stop, disable -------------------------------------------
+
+svc() {	# start, stop, restart, enable, disable
+	case $init in
+	systemd)
+		case $1 in
+		enable)  systemctl daemon-reload; systemctl enable "$name" ;;
+		disable) systemctl disable "$name" || true; systemctl daemon-reload ;;
+		*)       systemctl "$1" "$name" ;;
+		esac ;;
+	openrc)
+		case $1 in
+		enable)  rc-update add "$name" default ;;
+		disable) rc-update del "$name" default || true ;;
+		*)       rc-service "$name" "$1" ;;
+		esac ;;
+	sysvinit)
+		case $1 in
+		enable|disable)
+			if command -v update-rc.d >/dev/null 2>&1; then
+				[ "$1" = enable ] && update-rc.d "$name" defaults ||
+					update-rc.d -f "$name" remove
+			elif command -v chkconfig >/dev/null 2>&1; then
+				[ "$1" = enable ] && chkconfig --add "$name" ||
+					chkconfig --del "$name"
+			else
+				echo "no update-rc.d or chkconfig: $1 $file in the runlevels yourself" >&2
+			fi ;;
+		*) "$file" "$1" ;;
+		esac ;;
+	esac
+}
+
+# ---- main --------------------------------------------------------------------
+
+[ $print = 1 ] || [ "$(id -u)" = 0 ] || die "needs root: run it with sudo"
+[ -n "$user" ] || [ $print = 0 ] || user=$(id -un)
+if [ $remove = 0 ]; then
+	[ -n "$user" ] || die "no user: pass -u USER (root is not used by default)"
+	safe "$user" || die "'$user': not a plain user name"
+fi
+
+if [ $print = 1 ]; then
+	"unit_$init"
+	exit 0
+fi
+
+if [ $remove = 1 ]; then
+	[ -e "$file" ] || die "$file does not exist"
+	svc stop || true
+	svc disable
+	rm -f "$file"
+	[ "$init" != systemd ] || systemctl daemon-reload
+	echo "removed $file ($log, if any, is kept)"
+	exit 0
+fi
+
+id "$user" >/dev/null 2>&1 || die "no such user: $user"
+[ -x "$prog" ] || die "$prog is not executable"
+[ -n "${RADIO_PANEL_LIB:-}" ] || [ -e "$dir/build/libradiopanel.so" ] ||
+	echo "warning: $dir/build/libradiopanel.so missing; run 'make lib' as $user" >&2
+if [ "$user" != root ]; then
+	for g in video input i2c; do
+		getent group "$g" >/dev/null 2>&1 || continue
+		id -nG "$user" | tr ' ' '\n' | grep -qx "$g" ||
+			echo "warning: $user is not in group $g (usermod -aG $g $user)" >&2
+	done
+fi
+
+tmp=$(mktemp "$file.XXXXXX")
+trap 'rm -f "$tmp"' EXIT
+"unit_$init" >"$tmp"
+if [ "$init" = systemd ]; then chmod 0644 "$tmp"; else chmod 0755 "$tmp"; fi
+mv "$tmp" "$file"
+trap - EXIT
+echo "wrote $file ($init): $prog $args, as $user"
+
+if [ $activate = 1 ]; then
+	svc enable
+	svc restart
+fi