core-system-scripts

Scripts submodule for the core-system repository
git clone https://ccx.te2000.cz/git/core-system-scripts
Log | Files | Refs

commit c6b56f2f05e429ca3d3e23f6a38337016f1a4d2d
parent 830c15d5ecf475fd10b98bc1515aa22b4f0e3086
Author: Jan Pobrislo <ccx@webprojekty.cz>
Date:   Fri, 29 Jul 2022 01:48:23 +0200

Determine bundle state from it's content

Diffstat:
Mcommand/s | 66+++++++++++++++++++++++++++++++++++++++++++++++-------------------
1 file changed, 47 insertions(+), 19 deletions(-)

diff --git a/command/s b/command/s @@ -35,24 +35,58 @@ load_s6_rc() { done service_bundles[$s]=${service_bundles[$s]%$'\n'} done + + process_bundles +} + +process_bundles() { + typeset -gA bundle_stat + + local b s + local -a count bundle_content + + for b in ${(k)bundle_services}; do + bundle_content=( ${(f)bundle_services[$b]} ) + count=( 0 0 ) # Active, inactive + for s in $bundle_content; do + : $(( count[${service_active[$s]:-2}]++ )) + done + if (($count[1] == 0)); then + bundle_stat[$b]=1 # inactive + elif (($count[2] == 0)); then + bundle_stat[$b]=3 # active + else + bundle_stat[$b]=2 # partial + fi + done +} + +load_svstat() { + typeset -gA service_stat + + local -a svstat + local s svcdir + + for svcdir in /run/service/*(-/); do + s=$svcdir:t + if svstat=( $(s6-svstat $svcdir) ); then + $service_stat[$s]=$svstat[1] + [[ $$service_stat[$s] == up && $svstat[3] == ? ]] && $service_stat[$s]=starting + else + $service_stat[$s]=err + fi + done } print_service_bundles() { local b pre post s=$1 - local -a fmt_active=( '%F{yellow}%BA%b%f' '%F{cyan}I%f' '%F{magenta}-%f' ) + local -a fmt_active=( '%F{cyan}' '%F{magenta}' '%F{yellow}%B' ) if ! (($+service_bundles[$s])); then printf '\t-\n' return fi for b in ${(f)service_bundles[$s]}; do - if (($+service_active[$b])); then - pre='%F{yellow}%B' - post='%b%f' - else - pre='%F{cyan}' - post='%f' - fi - printf '\t%s' "${(%)pre}$b${(%)post}" + printf '\t%s' "${(%)fmt_active[${bundle_stat[$b]}]}$b${(%)post}" done print } @@ -69,21 +103,14 @@ show_oneshots() { 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' err '%K{red}X%k' ) - local s svcdir state active - for svcdir in /run/service/*(-/); do - s=$svcdir:t - if sv_stat=( $(s6-svstat $svcdir) ); then - state=$sv_stat[1] - [[ $state == up && $sv_stat[3] == ? ]] && state=starting - else - state=err - fi + local s state active + for s state in "${(@kv)service_stat}"; do if (($+service_type[$s])); then active=${service_active[$s]:-2} else active=3 fi - printf "%s %s\t%s\n" ${(%)fmt_up[$state]}${(%)fmt_active[$active]} $s + printf "%s %s" ${(%)fmt_up[$state]}${(%)fmt_active[$active]} $s print_service_bundles $s done } @@ -91,6 +118,7 @@ show_longruns() { main() { load_s6_rc_db load_s6_rc + load_svstat show_oneshots show_longruns