commit c70b736000213b6390ea37c0b1f3d1e471cdafe8
parent 50664dd785a7bb9b34b13aa196e60be14a45cec6
Author: Morel BĂ©renger <berengermorel76@gmail.com>
Date: Wed, 29 Jul 2020 08:16:17 +0200
add runit_help.sh as new tool
Diffstat:
1 file changed, 59 insertions(+), 0 deletions(-)
diff --git a/tools/runit_helper.sh b/tools/runit_helper.sh
@@ -0,0 +1,59 @@
+#!/bin/sh
+
+# This script aims to offer a friendly interface to manage runit services
+# Requires:
+# * dialog
+# * find
+# * $SVDIR which indicates the supervision folder
+# * $SVKNOWN which indicates a colon-separated list of places to search for
+# existing services (duplicated services are *not* supported... yet?)
+# * $ACTION indicates how to manipulate services
+#
+# Currently supported $ACTION values:
+# * install: allows to register/unregister known services;
+#
+# Planned to support $ACTION values:
+# * enable: allows to enable/disable known services;
+# * toggle: same, via down file;
+
+printf "SVDIR=%s\nSVKNOWN=%s\n" ${SVDIR:="/etc/service"} ${SVKNOWN:="/etc/sv/"} >&2
+ACTION="enable"
+SV_LIST=""
+
+FINDOPT="-mindepth 1 -maxdepth 2 -name run "
+for SOURCE in $(printf ${SVKNOWN} | tr ':' '\t')
+do
+ SV_LIST="$SV_LIST $(find -H $SOURCE $FINDOPT -exec sh -c \
+ 'printf "%s %s off\n" $(dirname {}) $(dirname {})' \; | sort)"
+done
+
+SV_ENABLED=$(find -L ${SVDIR} $FINDOPT -exec sh -c \
+ 'printf " -e '\''/%s off$/ s/off$/on/g'\'' " $(basename $(dirname {}))' \;)
+
+SV_CURRENT=$(printf "%s %s %s\n" $SV_LIST | eval sed $SV_ENABLED)
+
+DIALOG=$(mktemp -d)
+dialog --no-tags --checklist "List of service to $ACTION:" 0 0 0 $SV_CURRENT 2> $DIALOG/out
+SV_NEW_ENABLED=$(tr ' ' '\n' < $DIALOG/out | awk -F/ '{printf " -e '\''/%s off$/ s/off$/on/g'\'' \n", $NF}END{printf "\n"}')
+SV_NEXT=$(printf "%s %s %s\n" $SV_LIST | eval sed $SV_NEW_ENABLED)
+
+printf "%s\n" "$SV_CURRENT" > $DIALOG/curr
+printf "%s\n" "$SV_NEXT" > $DIALOG/next
+AWKPROG='
+{
+ if( $2 != $3 )
+ {
+ if( $2 == "on" )
+ {
+ last = split( $1, elem, "/" );
+ printf "rm %s/%s\n", svdir, elem[last];
+ }
+ else
+ {
+ printf "ln -s %s %s/\n", $1, svdir;
+ }
+ }
+}
+'
+paste -d ' ' $DIALOG/curr $DIALOG/next | cut -f1,3,6 -d ' ' | awk -F' ' -v svdir="$SVDIR" "$AWKPROG"
+rm -r $DIALOG