commit b654d8bb14949f8f66b9f0c0802313c75d553f4c parent d4cdcd0cbbf8fca79c6cc179c50a302511665b92 Author: Jan Pobrislo <ccx@webprojekty.cz> Date: Thu, 28 Jul 2022 22:55:35 +0200 Service listing script Diffstat:
M | command/s | | | 75 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- |
1 file changed, 74 insertions(+), 1 deletion(-)
diff --git a/command/s b/command/s @@ -1,2 +1,75 @@ #!/bin/zsh -diff -u <(s6-rc listall ok-all) <(s6-rc -a list) + +typeset -g default_bundle=ok-all + +load_s6_rc_db() { + typeset -gA service_type + typeset -ga oneshots longruns bundles + + local s + + oneshots=( "${(@f)"$( s6-rc-db list oneshots )"}" ) || exit $? + longruns=( "${(@f)"$( s6-rc-db list longruns )"}" ) || exit $? + bundles=( "${(@f)"$( s6-rc-db list bundles )"}" ) || exit $? + for s in $oneshots; do service_type[$s]=oneshot; done + for s in $longruns; do service_type[$s]=longrun; done + for s in $bundles; do service_type[$s]=bundle; done +} + +load_s6_rc() { + typeset -gA service_bundles bundle_services service_in_bundle service_active + typeset -ga active_services + + local b s + + active_services=( "${(@f)"$( s6-rc -a list )"}" ) || exit $? + for s in $active_services; do + service_active[$s]=1 + done + for b in $bundles; do + bundle_services[$b]=$( s6-rc listall $b ) ) || exit $? + for s in "${(@f)bundle_svcs[$b]}"; do + service_in_bundle[$s//$b]=1 + service_bundles[$s]+=$b$'\n' + done + service_bundles[$s]=${service_bundles[$s]%$'\n'} + done +} + +show_oneshots() { + local -a fmt_active=( '%F{yellow}%BA%b%f' '%F{cyan}I%f' ) + local s + for s in $oneshots; do + printf "%s %s\t%s\n" ${(%)fmt_active[${active_services[$s]:-2}]} $s ${(j: :)${(f)service_bundles[$s]}} + done +} + +show_longruns() { + local -a sv_stat fmt_active=( '%F{yellow}%BA%b%f' '%F{cyan}I%f' '%F{magenta}-%f' ) + local -A fmt_up=( up '%F{green}%BU%b%f' down '%F{red}D%f' starting '%F{magenta}%B?%b%f' ) + local s svcdir state active + for svcdir in /run/service/*(/); do + if sv_stat=( s6-svstat $svcdir ); then + state=$sv_stat[1] + [[ $state == up && $sv_stat[3] == ? ]] && state=starting + fi + if (($+service_type[$s])); then + active=${active_services[$s]:-2} + else + active=3 + fi + printf "%s %s\t%s\n" ${(%)fmt_up[$state]}${(%)fmt_active[$active]} $s ${(j: :)${(f)service_bundles[$s]}} + done +} + +main() { + load_s6_rc_db + load_s6_rc + + show_oneshots + show_longruns + + diff -u <(s6-rc listall ok-all) <(s6-rc -a list) +} + +main "$@"