runit_helper.sh (1828B)
1 #!/bin/sh 2 3 # This script aims to offer a friendly interface to manage runit services 4 # Requires: 5 # * dialog 6 # * find 7 # * $SVDIR which indicates the supervision folder 8 # * $SVKNOWN which indicates a colon-separated list of places to search for 9 # existing services (duplicated services are *not* supported... yet?) 10 # * $ACTION indicates how to manipulate services 11 # 12 # Currently supported $ACTION values: 13 # * install: allows to register/unregister known services; 14 # 15 # Planned to support $ACTION values: 16 # * enable: allows to enable/disable known services; 17 # * toggle: same, via down file; 18 19 printf "SVDIR=%s\nSVKNOWN=%s\n" ${SVDIR:="/etc/service"} ${SVKNOWN:="/etc/sv/"} >&2 20 ACTION="enable" 21 SV_LIST="" 22 23 FINDOPT="-mindepth 1 -maxdepth 2 -name run " 24 for SOURCE in $(printf ${SVKNOWN} | tr ':' '\t') 25 do 26 SV_LIST="$SV_LIST $(find -H $SOURCE $FINDOPT -exec sh -c \ 27 'printf "%s %s off\n" $(dirname {}) $(dirname {})' \; | sort)" 28 done 29 30 SV_ENABLED=$(find -L ${SVDIR} $FINDOPT -exec sh -c \ 31 'printf " -e '\''/%s off$/ s/off$/on/g'\'' " $(basename $(dirname {}))' \;) 32 33 SV_CURRENT=$(printf "%s %s %s\n" $SV_LIST | eval sed $SV_ENABLED) 34 35 DIALOG=$(mktemp -d) 36 dialog --no-tags --checklist "List of service to $ACTION:" 0 0 0 $SV_CURRENT 2> $DIALOG/out 37 SV_NEW_ENABLED=$(tr ' ' '\n' < $DIALOG/out | awk -F/ '{printf " -e '\''/%s off$/ s/off$/on/g'\'' \n", $NF}END{printf "\n"}') 38 SV_NEXT=$(printf "%s %s %s\n" $SV_LIST | eval sed $SV_NEW_ENABLED) 39 40 printf "%s\n" "$SV_CURRENT" > $DIALOG/curr 41 printf "%s\n" "$SV_NEXT" > $DIALOG/next 42 AWKPROG=' 43 { 44 if( $2 != $3 ) 45 { 46 if( $2 == "on" ) 47 { 48 last = split( $1, elem, "/" ); 49 printf "rm %s/%s\n", svdir, elem[last]; 50 } 51 else 52 { 53 printf "ln -s %s %s/\n", $1, svdir; 54 } 55 } 56 } 57 ' 58 paste -d ' ' $DIALOG/curr $DIALOG/next | cut -f1,3,6 -d ' ' | awk -F' ' -v svdir="$SVDIR" "$AWKPROG" 59 rm -r $DIALOG