UpdateController/UpdateController.sh

248 lines
6.1 KiB
Bash
Raw Normal View History

2023-01-20 11:54:49 +01:00
#!/bin/bash
2023-01-24 16:44:18 +01:00
update_controller_pipe="/tmp/update_controller_pipe"
init () {
trap "rm -f $update_controller_pipe" EXIT
if [[ ! -p $update_controller_pipe ]]; then
mkfifo $update_controller_pipe
fi
}
2023-01-20 11:54:49 +01:00
read_config() {
2023-01-24 16:44:18 +01:00
# name der fifo laesst sich ueberschreiben
2023-01-20 11:54:49 +01:00
return 0
}
wait_for_trigger() {
2023-01-24 16:44:18 +01:00
if [[ "$1" -eq -1 ]]; then
# wait for external impulse
if read line <$update_controller_pipe; then
if [ "$line" == "quit" ] || [ "$line" == "update" ]; then
echo "$line"
fi
fi
else
sleep ${1}
echo "sleep"
fi
2023-01-20 11:54:49 +01:00
}
check_for_apism() {
return 0
}
check_for_updates() {
return 0
}
backup_previous_version() {
return 0
}
fetch_updates() {
return 0
}
check_updates() {
return 0
}
revert_updates() {
return 0
}
lock_before_installing_updates() {
return 0
}
do_update_dry_run() {
return 0
}
do_update() {
return 0
}
fallback_to_previous_version() {
return 0;
}
unlock_after_installing_updates() {
return 0;
}
cleanup_previous_version() {
return 0
}
send_reboot_message_to_system_controller() {
return 0
}
###############################################################################
#
# UPDATE CONTROLLER
#
###############################################################################
UpdateController() {
local trigger_reboot=false
local check_apism_count=0
local try_update_count=0
local try_lock_count=0
local try_unlock_count=0
local trigger_timeout=-1
# read config parameters
read_config
# By default (trigger_timeout==-1), UpdateController can only be triggered
# from some external source to perform an update.
2023-01-24 16:44:18 +01:00
while :
do
request=$(wait_for_trigger $trigger_timeout)
if [ ${request} = "quit" ]; then
$trigger_reboot = true
elif [ ${request} != "update" ]; then
continue
fi
2023-01-20 11:54:49 +01:00
if $trigger_reboot; then
echo "CRITICAL send message to reboot the PSA"
send_reboot_message_to_system_controller
fi
# Is APISM running, listening on the correct ports etc. ?
while :
do
if ! check_for_apism; then
var=$((var+1))
check_apism_count=$((check_apism_count+1))
if [[ "$check_apism_count" -eq 5 ]]; then
trigger_reboot=true
echo "ERROR APISM not working"
continue 2
fi
sleep 60s
else
# APISM up and working
check_apism_count=0
break
fi
done
# Are there new updates available ?
if ! check_for_updates; then
echo "DEBUG no updates available"
continue
fi
# Fetch new updates (using git)
while :
do
if ! fetch_updates; then
try_updates_count=$((try_updates_count+1))
if [[ "$try_updates_count" -eq 5 ]]; then
trigger_reboot=true
echo "ERROR fetching updates"
continue 2
fi
sleep 60s
else
# Fetched updates successfully
try_updates_count=0
break
fi
done
# Check the updates for:
# * correct MD5
# * compatibility with PSA
# ...
if ! check_updates; then
echo "ERROR check_updates"
revert_updates
trigger_reboot=true
continue
fi
# perform a dry-run and check if everything might work as expected.
if ! do_update_dry_run; then
echo "ERROR do_update_dry_run"
revert_updates
trigger_reboot=true
continue
fi
# Backup before any updates in case some previous test was wrong
if ! backup_previous_version; then
echo "ERROR backup_previous_version"
revert_updates
trigger_reboot=true
continue
fi
# Lock the PSA so all other tasks on the PSA are in sync with the update
while :
do
if ! lock_before_installing_updates; then
try_lock_count=$((try_lock_count+1))
if [[ "$try_lock_count" -eq 5 ]]; then
revert_updates
fallback_to_previous_version
trigger_reboot=true
echo "ERROR locking PSA"
continue 2
fi
sleep 60s
else
# Locked PSA successfully
try_lock_count=0
break
fi
done
# Actually do the update
if ! do_update; then
echo "ERROR do_update"
revert_updates
fallback_to_previous_version
trigger_reboot=true
continue
fi
while :
do
# If all went well, the PSA can return to normal operation
if ! unlock_after_installing_updates; then
try_unlock_count=$((try_unlock_count+1))
if [[ "$try_unlock_count" -eq 5 ]]; then
revert_updates
fallback_to_previous_version
trigger_reboot=true
echo "ERROR unlocking PSA"
continue 2
fi
sleep 60s
else
# Unlocked PSA successfully
try_unlock_count=0
break
fi
done
# Cleanup.
if ! cleanup_previous_version; then
echo "ERROR cleanup_previous_version"
fi
done
}
###############################################################################
# start the UpdateController
# UpdateController