diff --git a/scripts/testvps b/scripts/testvps new file mode 100755 index 0000000..4382b7d --- /dev/null +++ b/scripts/testvps @@ -0,0 +1,82 @@ +#!/usr/bin/env bash +# Requires: systemctl + +set -u + +function match { + [[ "$1" =~ $2 ]] +} + +function padlen { + local MAX=0 + for KEY in "$@"; do + local LEN=${#KEY} + ((LEN > MAX)) && MAX=$LEN + done + echo $MAX +} + +function pad { + local PAD="$1" + if [ -z "$PAD" ]; then + exit 1 + fi + + for ((i=0; i/dev/null) + if [ -z "$STAT" ]; then + fmt_print "$SERVICE" "$LPAD" "NOTFOUND" + exit 1 + fi + + local STAT_LOADED=$(head -n2 <<< "$STAT" | tail -n1) + # NOTE: "active " intentionally contains right padding + local ENABLED=$(match "$STAT_LOADED" "^\s*Loaded: loaded \(.*; enabled; .*\)" && echo "enabled" || echo "disabled") + local ACTIVE=$(match "$STAT" "\s*Active: active \(running\)" && echo "active " || echo "inactive") + fmt_print "$SERVICE" "$LPAD" "$ACTIVE" "$ENABLED" +) + +function test_services { + local PAD_ALIGN=$(padlen $@) + # fmt_test_service + for SERVICE in "$@"; do + local LPAD=$((PAD_ALIGN - ${#SERVICE})) + fmt_test_service "$SERVICE" $LPAD + done +} + +test_services nginx forgejo vaultwarden