From 418ae4fce45731c34e9f53648f8a4b068833ff6d Mon Sep 17 00:00:00 2001 From: th33xitus Date: Tue, 15 Feb 2022 19:47:56 +0100 Subject: [PATCH] fix: improve service control (thx pedro) --- docker/service_control.sh | 134 +++++++++++++++++++++++++++++++++++--- 1 file changed, 124 insertions(+), 10 deletions(-) diff --git a/docker/service_control.sh b/docker/service_control.sh index 4e3b23f..f7ce190 100644 --- a/docker/service_control.sh +++ b/docker/service_control.sh @@ -1,13 +1,127 @@ #!/bin/bash +validate_arguments () +{ + if [ "$1" != "0" ]; then + exit 1 + fi +} -if [ "$1" = "list-units" ]; then - echo "klipper.service loaded active running Klipper Dummy Service" - echo "moonraker.service loaded active running Moonraker Dummy Service" - exit 0 -fi +parse_show () +{ + PARSED_ARGUMENTS=$(getopt -a -n systemctl -o p: --long properties:,values -- "$@") -if [ "$2" = "klipper" ]; then - sudo /usr/bin/supervisorctl "$1" simulavr "$2" -else - sudo /usr/bin/supervisorctl "$1" "$2" -fi + validate_arguments $? + + eval set -- "$PARSED_ARGUMENTS" + while : + do + case "$1" in + -p | --properties) + IFS="," read -a PROPERTIES <<< $2 + shift 2 + ;; + --values) + shift + ;; + --) + shift; + UNITS=($@) + break + ;; + *) + echo "systemctl: unexpected option: $1 - this should not happen." + exit 2 + ;; + esac + done + + for UNIT in "${UNITS[@]}" + do + for PROPERTY in "${PROPERTIES[@]}" + do + case "$PROPERTY" in + LoadState) + echo "loaded" + ;; + ActiveState) + echo "active" + ;; + SubState) + sudo /usr/bin/supervisorctl status "$UNIT" | awk '{print tolower($2)}' + ;; + *) + echo "unknown" + ;; + esac + done + echo "" + done + + exit 0 +} + +parse_list_units () +{ + PARSED_ARGUMENTS=$(getopt -a -n systemctl -o at --long all,type:,plain,no-legend -- "$@") + + validate_arguments $? + + eval set -- "$PARSED_ARGUMENTS" + while : + do + case "$1" in + -a | --all) + shift + ;; + -t | --type) + shift 2 + ;; + --plain) + shift + ;; + --no-legend) + shift + ;; + --) + shift; + break + ;; + *) + echo "systemctl: unexpected option: $1 - this should not happen." + exit 2 + ;; + esac + done + + sudo /usr/bin/supervisorctl status | awk '{print $1".service\tloaded\tactive\t"tolower($2)"\t"$1" service"}' + + exit 0 +} + +COMMAND="$1" + +shift + +case "$COMMAND" in + show) + parse_show "$@" + ;; + list-units) + parse_list_units "$@" + ;; + start | stop | restart) + UNIT="$1" + + if [ "$UNIT" = "klipper" ]; then + sudo /usr/bin/supervisorctl "$COMMAND" simulavr klipper + else + sudo /usr/bin/supervisorctl "$COMMAND" "$UNIT" + fi + ;; + *) + echo "systemctl: unknown command '$COMMAND'" + exit 1 + ;; +esac + +exit 0 \ No newline at end of file