dot

packages and services management
Log | Files | Refs | README

commit 0242b772a161cc31042ef870807661e0752fa41f
parent 31efb95da6fff85636e367b50c503fe5b5eadd1e
Author: Josuah Demangeon <josuah.demangeon@gandi.net>
Date:   Tue, 15 Aug 2017 21:11:00 +0200

pkg/bin/pkg-service: much saner structure

Using recursion to simulate functions, making the script more atomic,
permitting to re-use already implemented parts without status variables
everywhere.

Diffstat:
Mpkg/bin/pkg-service | 125+++++++++++++++++++++++++++++++++++--------------------------------------------
1 file changed, 55 insertions(+), 70 deletions(-)

diff --git a/pkg/bin/pkg-service b/pkg/bin/pkg-service @@ -1,17 +1,6 @@ -cmd=$1 srv=${2%/} error=0 running=0 acted=0 killed=0 -mkdir -p "$ROOT/run/pid" +cmd="$1" srv=${2%/} found=0 -case "$cmd" in -(start | stop | status) - shift 2 - ;; -(update) - shift 1 - ;; -(restart) - shift 1 - "$0" stop "$@" && "$0" start "$@" || exit 1 - ;; +case "$1" in ('') cd "$ROOT/pkg" || exit 1 for service in */run @@ -19,71 +8,67 @@ case "$cmd" in done exit 0 ;; -(*) - printf '%s (start | stop | restart | status | update) <pkg>\n' "${0##*/}" - exit 1 - ;; -esac - +(stop|status) + shift 2 + [ "$srv" ] && [ -d "$ROOT/run/pid/$srv" ] && + for pid in $(ls "$ROOT/run/pid/$srv") + do + for arg in "$@" + do grep -qF "$arg" "$ROOT/run/pid/$srv/$pid" || continue 2 + done -# --- stop / status ------------------------------------------------------------ + found=1 -[ "$srv" ] && [ -d "$ROOT/run/pid/$srv" ] && -for pid in $(ls "$ROOT/run/pid/$srv") -do - for arg in "$@" - do grep -qF "$arg" "$ROOT/run/pid/$srv/$pid" || continue 2 + if cmp "$ROOT/run/pid/$srv/$pid" "/proc/$pid/cmdline" 2> /dev/null + then + cmdline="$(tr '\0' ' ' < "$ROOT/run/pid/$srv/$pid")" + + if [ "$cmd" = stop ] && kill "$pid" + then + printf '%-15s [x] %s\n' "$srv" "$cmdline" + rm -f "$ROOT/run/pid/$srv/$pid" + else + printf '%-15s [r] %s\n' "$srv" "$cmdline" + fi + fi done - - if cmp "$ROOT/run/pid/$srv/$pid" "/proc/$pid/cmdline" &> /dev/null + [ "$found" = 0 ] && printf '%-15s [ ]\n' "$srv" && exit 1 + ;; +(start) + shift 2 + if ! "$0" status "$srv" "$@" then - acted=1 cmdline="$(tr '\0' ' ' < "$ROOT/run/pid/$srv/$pid")" + mkdir -p "$ROOT/run/pid/$srv" + . "$ROOT/pkg/$srv/run" "$@" >> "$ROOT/var/log/$srv" 2>&1 <&- - if [ "$cmd" = stop ] && kill "$pid" - then - printf '%-15s [x] %s\n' "$srv" "$cmdline" - rm -f "$ROOT/run/pid/$srv/$pid" + if cp "/proc/$PID/cmdline" "$ROOT/run/pid/$srv/$PID" 2> /dev/null + then + cmdline=$(tr '\0' ' ' < "$ROOT/run/pid/$srv/$PID") + printf '%-15s [+] %s\n' "$srv" "$cmdline" else - running=1 - printf '%-15s [r] %s\n' "$srv" "$cmdline" + touch "$ROOT/run/pid/$srv/$PID" fi fi -done - - -# --- start -------------------------------------------------------------------- - -if [ "$running" = 0 ] && [ "$cmd" = start ] -then - mkdir -p "$ROOT/run/pid/$srv" - . "$ROOT/pkg/$srv/run" "$@" >> "$ROOT/var/log/$srv" 2>&1 <&- - - if cp "/proc/$PID/cmdline" "$ROOT/run/pid/$srv/$PID" 2> /dev/null - then - cmdline=$(tr '\0' ' ' < "$ROOT/run/pid/$srv/$PID") - printf '%-15s [+] %s\n' "$srv" "$cmdline" - else - touch "$ROOT/run/pid/$srv/$PID" - fi -fi - - -# --- check -------------------------------------------------------------------- - -find "$ROOT/run/pid" -type f | while IFS='' read path -do - if ! cmp "$path" "/proc/${path##*/}/cmdline" 2> /dev/null - then - srv=${path%/*} srv=${srv##*/} - printf '%-15s [!] %s\n' "$srv" "$(tr '\0' ' ' < "$path")" - rm -f "$path" + ;; +(update) + find "$ROOT/run/pid" -type f | while IFS='' read path + do + if ! cmp "$path" "/proc/${path##*/}/cmdline" > /dev/null 2>&1 + then + srv=${path%/*} srv=${srv##*/} + printf '%-15s [!] %s\n' "$srv" "$(tr '\0' ' ' < "$path")" + rm -f "$path" - [ -f "$ROOT/pkg/$srv/crash" ] && sh "$ROOT/pkg/$srv/crash" - fi -done + [ -f "$ROOT/pkg/$srv/crash" ] && + sh "$ROOT/pkg/$srv/crash" + fi + done -if [ "$acted""$cmd" = '0''status' ] || [ "$acted""$cmd" = '0''stop' ] -then printf '%-15s [ ]\n' "$srv" -fi + rmdir "$ROOT/run/pid"/* 2> /dev/null + ;; +(*) + printf '[start <pkg>|stop <pkg>|restart <pkg>|status <pkg>|update]\n' + ;; +esac -rmdir "$ROOT/run/pid"/* 2> /dev/null || true +true