refactor: simplify service_control script
This commit is contained in:
parent
f98cc12e81
commit
69cec6bf2c
|
|
@ -1,126 +1,95 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
validate_arguments ()
|
|
||||||
|
die ()
|
||||||
{
|
{
|
||||||
if [ "$1" != "0" ]; then
|
echo "$MY_NAME: $1"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
parse_show ()
|
MY_NAME="${0##*/}"
|
||||||
{
|
|
||||||
PARSED_ARGUMENTS=$(getopt -a -n systemctl -o p: --long properties:,values -- "$@")
|
|
||||||
|
|
||||||
validate_arguments $?
|
PARSED_ARGUMENTS="$(getopt -n "$MY_NAME" -o t:p:a --long type:,property:,all,value,no-legend,plain -- "$@")" || exit $?
|
||||||
|
|
||||||
eval set -- "$PARSED_ARGUMENTS"
|
eval set -- "$PARSED_ARGUMENTS"
|
||||||
while :
|
while :
|
||||||
do
|
do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
-p | --properties)
|
-t | --type)
|
||||||
IFS="," read -a PROPERTIES <<< $2
|
shift 2
|
||||||
shift 2
|
;;
|
||||||
;;
|
-p | --property)
|
||||||
--values)
|
IFS="," read -a PROPERTIES <<< $2
|
||||||
shift
|
shift 2
|
||||||
;;
|
;;
|
||||||
--)
|
-a | --all)
|
||||||
shift;
|
shift
|
||||||
UNITS=($@)
|
;;
|
||||||
break
|
--value)
|
||||||
;;
|
shift
|
||||||
*)
|
;;
|
||||||
echo "systemctl: unexpected option: $1 - this should not happen."
|
--no-legend)
|
||||||
exit 2
|
shift
|
||||||
;;
|
;;
|
||||||
esac
|
--plain)
|
||||||
done
|
shift
|
||||||
|
;;
|
||||||
for UNIT in "${UNITS[@]}"
|
--)
|
||||||
do
|
shift;
|
||||||
for PROPERTY in "${PROPERTIES[@]}"
|
break
|
||||||
do
|
;;
|
||||||
case "$PROPERTY" in
|
*)
|
||||||
LoadState)
|
die "unexpected option: $1 - this should not happen."
|
||||||
echo "loaded"
|
;;
|
||||||
;;
|
esac
|
||||||
ActiveState)
|
done
|
||||||
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"
|
COMMAND="$1"
|
||||||
|
|
||||||
shift
|
shift
|
||||||
|
|
||||||
|
UNITS=($@)
|
||||||
|
|
||||||
case "$COMMAND" in
|
case "$COMMAND" in
|
||||||
show)
|
show)
|
||||||
parse_show "$@"
|
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
|
||||||
;;
|
;;
|
||||||
list-units)
|
|
||||||
parse_list_units "$@"
|
|
||||||
;;
|
|
||||||
start | stop | restart)
|
|
||||||
UNIT="$1"
|
|
||||||
|
|
||||||
if [ "$UNIT" = "klipper" ]; then
|
list-units)
|
||||||
sudo /usr/bin/supervisorctl "$COMMAND" simulavr klipper
|
sudo /usr/bin/supervisorctl status | awk '{print $1".service\tloaded\tactive\t"tolower($2)"\t"$1" service"}'
|
||||||
else
|
|
||||||
sudo /usr/bin/supervisorctl "$COMMAND" "$UNIT"
|
|
||||||
fi
|
|
||||||
;;
|
;;
|
||||||
|
|
||||||
|
start | stop | restart)
|
||||||
|
for UNIT in "${UNITS[@]}"
|
||||||
|
do
|
||||||
|
if [ "$UNIT" = "klipper" ]; then
|
||||||
|
sudo /usr/bin/supervisorctl "$COMMAND" simulavr klipper
|
||||||
|
else
|
||||||
|
sudo /usr/bin/supervisorctl "$COMMAND" "$UNIT"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
;;
|
||||||
|
|
||||||
*)
|
*)
|
||||||
echo "systemctl: unknown command '$COMMAND'"
|
die "unknown command '$COMMAND'"
|
||||||
exit 1
|
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue