runtipi/scripts/watcher.sh
Nicolas Meienberger 48c1e2607f
Release/0.7.2 (#249)
* feat: move from cookie base auth to jwt auth

test: mock redis

* test: auth.service & auth.resolver

test: auth.resolver

* test: session middleware

* chore: bump version

fix: merge conflicts

* docs: update readme & trace start script

* fix: start script unbound variables [skip ci]

* fix: kill watcher function [skip ci]

* fix: register store token

* fix: don't delete token immediately after refresh. keep it for 6 sec to account for delays
2022-11-04 07:49:40 +00:00

121 lines
2.8 KiB
Bash
Executable File

#!/usr/bin/env bash
source "${BASH_SOURCE%/*}/common.sh"
ROOT_FOLDER="${PWD}"
WATCH_FILE="${ROOT_FOLDER}/state/events"
function clean_events() {
# Create the file if it doesn't exist
if [[ ! -f "${WATCH_FILE}" ]]; then
touch "${WATCH_FILE}"
fi
echo "" >"$WATCH_FILE"
chmod -R a+rwx "${ROOT_FOLDER}/state/events"
}
function set_status() {
local id=$1
local status=$2
write_log "Setting status for ${id} to ${status}"
# Update the status of the event
if [[ "$(uname)" != "Linux" ]]; then
sed -i '' "s/${id} [a-z]*/${id} ${status}/g" "${WATCH_FILE}"
else
sed -i "s/${id}.*$/$(echo "${id} ${status}" | sed 's/\//\\\//g')/" "$WATCH_FILE"
fi
}
function run_command() {
local command_path="${1}"
local id=$2
shift 2
set_status "$id" "running"
$command_path "$@" >>"${ROOT_FOLDER}/logs/${id}.log" 2>&1
local result=$?
if [[ $result -eq 0 ]]; then
set_status "$id" "success"
else
set_status "$id" "error"
fi
}
function select_command() {
# Example command:
# clone_repo id waiting "args"
local command=$(echo "$1" | cut -d ' ' -f 1)
local id=$(echo "$1" | cut -d ' ' -f 2)
local status=$(echo "$1" | cut -d ' ' -f 3)
local args=$(echo "$1" | cut -d ' ' -f 4-)
if [[ "$status" != "waiting" ]]; then
return 0
fi
write_log "Executing command ${command}"
if [ -z "$command" ]; then
return 0
fi
if [ "$command" = "clone_repo" ]; then
run_command "${ROOT_FOLDER}/scripts/git.sh" "$id" "clone" "$args"
return 0
fi
if [ "$command" = "update_repo" ]; then
run_command "${ROOT_FOLDER}/scripts/git.sh" "$id" "update" "$args"
return 0
fi
if [ "$command" = "app" ]; then
local arg1=$(echo "$args" | cut -d ' ' -f 1)
local arg2=$(echo "$args" | cut -d ' ' -f 2)
# Args example: start filebrowser
run_command "${ROOT_FOLDER}/scripts/app.sh" "$id" "$arg1" "$arg2"
return 0
fi
if [ "$command" = "system_info" ]; then
run_command "${ROOT_FOLDER}/scripts/system-info.sh" "$id"
return 0
fi
if [ "$command" = "update" ]; then
run_command "${ROOT_FOLDER}/scripts/system.sh" "$id" "update"
return 0
fi
if [ "$command" = "restart" ]; then
run_command "${ROOT_FOLDER}/scripts/system.sh" "$id" "restart"
return 0
fi
return 0
}
write_log "Listening for events in ${WATCH_FILE}..."
clean_events
# Listen in for changes in the WATCH_FILE
fswatch -0 "${WATCH_FILE}" | while read -d ""; do
# Read the command from the last line of the file
command=$(tail -n 1 "${WATCH_FILE}")
status=$(echo "$command" | cut -d ' ' -f 3)
if [ -z "$command" ] || [ "$status" != "waiting" ]; then
continue
else
select_command "$command"
fi
done