citadel-core/karen

84 lines
2.0 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2020 Umbrel. https://getumbrel.com
# SPDX-FileCopyrightText: 2021 Citadel and contributors.
#
2022-01-21 20:37:48 +00:00
# SPDX-License-Identifier: GPL-3.0-or-later
# karen watches for signals and executes triggers in the events dir
# karen gets triggered a lot
check_root () {
if [[ $UID != 0 ]]; then
echo "Error: This script must be run as root."
echo "Can I speak to a manager please?"
exit 1
fi
}
check_if_not_already_running() {
if ps ax | grep $0 | grep -v $$ | grep bash | grep -v grep
then
echo "karen is already running"
exit 1
fi
}
check_dependencies () {
for cmd in "$@"; do
if ! command -v $cmd >/dev/null 2>&1; then
echo "This script requires \"${cmd}\" to be installed"
exit 1
fi
done
}
check_root
check_if_not_already_running
check_dependencies fswatch readlink dirname
if [[ -n "$1" ]]; then
root_dir="$(readlink -f $1)"
else
root_dir="$(dirname $(readlink -f ${BASH_SOURCE[0]}))/events"
fi
signal_dir="$root_dir/signals"
trigger_dir="$root_dir/triggers"
if [[ ! -d "$root_dir" ]]; then
echo "Root dir does not exist '$root_dir'"
exit 1
fi
echo "karen will start in 30 seconds"
sleep 30
echo "karen is running in $root_dir"
2021-11-27 11:50:34 +00:00
fswatch -0 --event=PlatformSpecific $signal_dir | while read -d "" event; do
signal="${event#"$signal_dir"}"
signal="${signal#"/"}"
trigger="$trigger_dir/$signal"
args=""
2021-11-27 11:50:34 +00:00
# If signal isn't an empty string, then it's a signal
if [[ -n "$signal" ]]; then
echo "Got signal: $signal"
2021-11-27 11:50:34 +00:00
# If the signal starts wih app-, the trigger is "$trigger_dir/app"
# and the args are the rest of the signal
2022-01-09 16:32:29 +00:00
if [[ "$signal" =~ ^app- ]] && [[ "$signal" != "app-update" ]]; then
2021-11-27 11:50:34 +00:00
trigger="$trigger_dir/app"
args="${signal#"app-"}"
fi
if test -x "$trigger"; then
echo "karen is getting triggered!"
if [[ ! -f "statuses/update-in-progress" ]]; then
"$trigger" $args &
fi
else
echo "No trigger found"
fi
fi
done