install-service.sh 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #!/bin/sh
  2. # Install radio-ui as a system service (systemd, sysvinit or OpenRC).
  3. #
  4. # sudo tools/install-service.sh [OPTIONS] [-- RADIO-UI ARGS...]
  5. #
  6. # The service runs ./radio-ui from this checkout (not a copy), as USER, with
  7. # the given arguments (default: drm). Build the library first, as yourself:
  8. # make lib.
  9. #
  10. # OPTIONS:
  11. # -i INIT systemd, sysvinit or openrc (default: the running one)
  12. # -u USER user the service runs as (default: $SUDO_USER; root refused
  13. # unless named here). Needs the video, input and i2c groups.
  14. # -n NAME service name (default: radio-ui)
  15. # -N do not enable or start the service, only write its file
  16. # -p print the service file and exit; nothing is installed
  17. # -r remove the service: stop, disable, delete its file
  18. # -h this help
  19. #
  20. # Examples:
  21. # sudo tools/install-service.sh
  22. # sudo tools/install-service.sh -- drm --host radio.lo
  23. # sudo tools/install-service.sh -- i2c --bus 1
  24. # sudo tools/install-service.sh -r
  25. set -eu
  26. dir=$(cd "$(dirname "$0")/.." && pwd -P)
  27. prog=$dir/radio-ui
  28. init= user=${SUDO_USER:-} name=radio-ui activate=1 print=0 remove=0
  29. while getopts i:u:n:Nprh opt; do
  30. case $opt in
  31. i) init=$OPTARG ;;
  32. u) user=$OPTARG ;;
  33. n) name=$OPTARG ;;
  34. N) activate=0 ;;
  35. p) print=1 ;;
  36. r) remove=1 ;;
  37. *) sed -n '2,24s/^# \{0,1\}//p' "$0"; [ "$opt" = h ] && exit 0; exit 2 ;;
  38. esac
  39. done
  40. shift $((OPTIND - 1))
  41. [ $# -gt 0 ] || set -- drm
  42. die() { echo "$0: $*" >&2; exit 1; }
  43. # The arguments go into ExecStart= or a shell script unquoted: keep them plain.
  44. safe() {
  45. case $1 in
  46. ''|*[!A-Za-z0-9._/:=,@+-]*) return 1 ;;
  47. esac
  48. }
  49. for a in "$name" "$dir" "$@"; do
  50. safe "$a" || die "'$a': only letters, digits and ._/:=,@+- are allowed"
  51. done
  52. args=$*
  53. if [ -z "$init" ]; then
  54. if [ -d /run/systemd/system ]; then init=systemd
  55. elif [ -d /run/openrc ] || command -v openrc-run >/dev/null 2>&1; then init=openrc
  56. else init=sysvinit
  57. fi
  58. fi
  59. case $init in
  60. systemd) file=/etc/systemd/system/$name.service ;;
  61. sysvinit|openrc) file=/etc/init.d/$name ;;
  62. *) die "unknown init system '$init' (systemd, sysvinit, openrc)" ;;
  63. esac
  64. log=/var/log/$name.log
  65. # ---- the service files -------------------------------------------------------
  66. unit_systemd() {
  67. cat <<EOF
  68. [Unit]
  69. Description=Radio front panel (radio-ui)
  70. After=network.target sound.target mpd.service
  71. Wants=mpd.service
  72. [Service]
  73. Type=simple
  74. User=$user
  75. WorkingDirectory=$dir
  76. Environment=PYTHONUNBUFFERED=1
  77. ExecStart=$prog $args
  78. Restart=on-failure
  79. RestartSec=5
  80. [Install]
  81. WantedBy=multi-user.target
  82. EOF
  83. }
  84. unit_openrc() {
  85. cat <<EOF
  86. #!/sbin/openrc-run
  87. # Radio front panel (radio-ui); written by tools/install-service.sh
  88. description="Radio front panel (radio-ui)"
  89. command="$prog"
  90. command_args="$args"
  91. command_user="$user"
  92. command_background=yes
  93. pidfile="/run/$name.pid"
  94. directory="$dir"
  95. output_log="$log"
  96. error_log="$log"
  97. export PYTHONUNBUFFERED=1
  98. depend() {
  99. need localmount
  100. after net mpd
  101. use mpd
  102. }
  103. start_pre() {
  104. checkpath -f -m 0644 -o "$user" "$log"
  105. }
  106. EOF
  107. }
  108. unit_sysvinit() {
  109. cat <<EOF
  110. #!/bin/sh
  111. ### BEGIN INIT INFO
  112. # Provides: $name
  113. # Required-Start: \$local_fs \$remote_fs \$network
  114. # Required-Stop: \$local_fs \$remote_fs \$network
  115. # Should-Start: mpd
  116. # Should-Stop: mpd
  117. # Default-Start: 2 3 4 5
  118. # Default-Stop: 0 1 6
  119. # Short-Description: Radio front panel (radio-ui)
  120. ### END INIT INFO
  121. # Written by tools/install-service.sh
  122. NAME=$name
  123. DAEMON=$prog
  124. ARGS="$args"
  125. USER=$user
  126. DIR=$dir
  127. PIDFILE=/run/\$NAME.pid
  128. LOG=$log
  129. export PYTHONUNBUFFERED=1
  130. running() {
  131. [ -f "\$PIDFILE" ] && kill -0 "\$(cat "\$PIDFILE")" 2>/dev/null
  132. }
  133. do_start() {
  134. if running; then echo "\$NAME already running"; return 0; fi
  135. touch "\$LOG" && chown "\$USER" "\$LOG"
  136. start-stop-daemon --start --background --make-pidfile --pidfile "\$PIDFILE" \\
  137. --chuid "\$USER" --chdir "\$DIR" --startas /bin/sh -- \\
  138. -c "exec \$DAEMON \$ARGS >>\$LOG 2>&1"
  139. }
  140. do_stop() {
  141. start-stop-daemon --stop --retry TERM/10/KILL/5 --pidfile "\$PIDFILE" --oknodo
  142. rm -f "\$PIDFILE"
  143. }
  144. case "\$1" in
  145. start) echo "Starting \$NAME"; do_start ;;
  146. stop) echo "Stopping \$NAME"; do_stop ;;
  147. restart|force-reload) do_stop; do_start ;;
  148. status)
  149. if running; then echo "\$NAME is running"; exit 0
  150. else echo "\$NAME is not running"; exit 3
  151. fi ;;
  152. *) echo "Usage: \$0 {start|stop|restart|force-reload|status}" >&2; exit 2 ;;
  153. esac
  154. EOF
  155. }
  156. # ---- enable, start, stop, disable -------------------------------------------
  157. svc() { # start, stop, restart, enable, disable
  158. case $init in
  159. systemd)
  160. case $1 in
  161. enable) systemctl daemon-reload; systemctl enable "$name" ;;
  162. disable) systemctl disable "$name" || true; systemctl daemon-reload ;;
  163. *) systemctl "$1" "$name" ;;
  164. esac ;;
  165. openrc)
  166. case $1 in
  167. enable) rc-update add "$name" default ;;
  168. disable) rc-update del "$name" default || true ;;
  169. *) rc-service "$name" "$1" ;;
  170. esac ;;
  171. sysvinit)
  172. case $1 in
  173. enable|disable)
  174. if command -v update-rc.d >/dev/null 2>&1; then
  175. [ "$1" = enable ] && update-rc.d "$name" defaults ||
  176. update-rc.d -f "$name" remove
  177. elif command -v chkconfig >/dev/null 2>&1; then
  178. [ "$1" = enable ] && chkconfig --add "$name" ||
  179. chkconfig --del "$name"
  180. else
  181. echo "no update-rc.d or chkconfig: $1 $file in the runlevels yourself" >&2
  182. fi ;;
  183. *) "$file" "$1" ;;
  184. esac ;;
  185. esac
  186. }
  187. # ---- main --------------------------------------------------------------------
  188. [ $print = 1 ] || [ "$(id -u)" = 0 ] || die "needs root: run it with sudo"
  189. [ -n "$user" ] || [ $print = 0 ] || user=$(id -un)
  190. if [ $remove = 0 ]; then
  191. [ -n "$user" ] || die "no user: pass -u USER (root is not used by default)"
  192. safe "$user" || die "'$user': not a plain user name"
  193. fi
  194. if [ $print = 1 ]; then
  195. "unit_$init"
  196. exit 0
  197. fi
  198. if [ $remove = 1 ]; then
  199. [ -e "$file" ] || die "$file does not exist"
  200. svc stop || true
  201. svc disable
  202. rm -f "$file"
  203. [ "$init" != systemd ] || systemctl daemon-reload
  204. echo "removed $file ($log, if any, is kept)"
  205. exit 0
  206. fi
  207. id "$user" >/dev/null 2>&1 || die "no such user: $user"
  208. [ -x "$prog" ] || die "$prog is not executable"
  209. [ -n "${RADIO_PANEL_LIB:-}" ] || [ -e "$dir/build/libradiopanel.so" ] ||
  210. echo "warning: $dir/build/libradiopanel.so missing; run 'make lib' as $user" >&2
  211. if [ "$user" != root ]; then
  212. for g in video input i2c; do
  213. getent group "$g" >/dev/null 2>&1 || continue
  214. id -nG "$user" | tr ' ' '\n' | grep -qx "$g" ||
  215. echo "warning: $user is not in group $g (usermod -aG $g $user)" >&2
  216. done
  217. fi
  218. tmp=$(mktemp "$file.XXXXXX")
  219. trap 'rm -f "$tmp"' EXIT
  220. "unit_$init" >"$tmp"
  221. if [ "$init" = systemd ]; then chmod 0644 "$tmp"; else chmod 0755 "$tmp"; fi
  222. mv "$tmp" "$file"
  223. trap - EXIT
  224. echo "wrote $file ($init): $prog $args, as $user"
  225. if [ $activate = 1 ]; then
  226. svc enable
  227. svc restart
  228. fi