UpdateController/UpdateController.sh

339 lines
8.2 KiB
Bash
Raw Normal View History

2023-01-20 11:54:49 +01:00
#!/bin/bash
2023-01-25 11:11:17 +01:00
set -x
2023-01-25 09:54:00 +01:00
fifo=""
2022-06-02 15:59:11 +02:00
source ./git_helpers.sh
source ./opkg_helpers.sh
source ./update_helpers.sh
2023-01-25 09:54:00 +01:00
init_fifo () {
2023-01-25 11:11:17 +01:00
trap "rm -f $fifo" EXIT
2023-01-25 09:54:00 +01:00
if [ $? -eq 0 ]; then
# set trap
2023-01-25 11:11:17 +01:00
if [[ ! -p "$fifo" ]]; then
mkfifo "$fifo"
2023-01-25 09:54:00 +01:00
if [ $? -eq 0 ]; then
# fifo created
2023-01-25 11:11:17 +01:00
echo "created fifo=$fifo"
2023-01-25 09:54:00 +01:00
return 0
fi
fi
2023-01-24 16:44:18 +01:00
fi
2023-01-25 09:54:00 +01:00
return 1
2023-01-24 16:44:18 +01:00
}
2023-01-20 11:54:49 +01:00
read_config() {
2023-01-25 11:11:17 +01:00
fifo_dir=$(cat "$1" | jq -r .fifo_dir)
if [ -z "$fifo_dir" ]; then
return 1
fi
fifo=$(cat "$1" | jq -r .fifo)
if [ -z "$fifo" ]; then
return 1
fi
fifo="${fifo_dir}/$fifo"
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
2023-01-25 09:54:00 +01:00
if read line <$fifo; then
2023-01-25 11:11:17 +01:00
if [ "$line" = "quit" ] || [ "$line" = "update" ]; then
2023-01-24 16:44:18 +01:00
echo "$line"
fi
fi
else
2023-01-25 11:11:17 +01:00
sleep $1
2023-01-24 16:44:18 +01:00
echo "sleep"
fi
2023-01-20 11:54:49 +01:00
}
2023-01-30 08:27:30 +01:00
# APISM has al least two TCP-ports open: 7778 and 7779. To check for a running
# APISM it is therefore enough to check for the port 7778. netcat will return 0
# if the port 7778 is open.
2023-01-20 11:54:49 +01:00
check_for_apism() {
2023-01-30 08:27:30 +01:00
return 0
2022-06-11 18:16:19 +02:00
nc localhost 7778
if [ $? -eq 0 ]; then
return 0
fi
return 1
2023-01-20 11:54:49 +01:00
}
2023-01-30 08:27:30 +01:00
######################### send_to_ismas.txt ###################################
# #M=GH_APP
# #C=REQ_ISMASParameter
# #J={}
# {
# "DEV_ID": {
# "Device_Type": "2020",
# "Custom_ID": 999,
# "Device_ID": 1
# },
# "REQ_ISMASParameter_#1": {}
# }
#
# APISM should respond with (something like this):
#
# {
# "REQ_ISMASPARAMETER#69553_Response": {
# "Aknoledge": "OK"
# },
# "Dev_ID": {
# "Device_Type": "ISMAS",
# "Custom_ID": 999,
# "Device_ID": 2
# },
# "Fileupload": [
# { "IPK": "BladiBlupp.Zip",
# "MD5": "01234567890123456789012345678901"}
# ],
# "Parameter": {
# "Location": "An der Bahn 11",
# "Group": "G1",
# "Zone": "Z1",
# "Name": "Bruno",
# "SecNumber": "0",
# "LastAcc": "1",
# "GPSLat": "49,6019",
# "GPSLon": "12,1258"
# }
# }
#
# where the interesting part is contained in "FileUpload".
#
2023-01-20 11:54:49 +01:00
check_for_updates() {
2023-01-30 08:27:30 +01:00
# ismas_response=$(cat send_to_ismas.txt; sleep 1) | nc localhost 7778)
2023-01-20 11:54:49 +01:00
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
2023-01-25 09:54:00 +01:00
# read_config
2023-01-20 11:54:49 +01:00
# 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
2023-01-25 11:11:17 +01:00
trigger=$(wait_for_trigger $trigger_timeout)
echo "trigger=$trigger"
if [ "$trigger" = "quit" ]; then
2023-01-24 16:44:18 +01:00
$trigger_reboot = true
2023-01-25 11:11:17 +01:00
elif [ "$trigger" != "update" ]; then
2023-01-24 16:44:18 +01:00
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
2023-01-25 11:11:17 +01:00
continue
2023-01-20 11:54:49 +01:00
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
2023-01-25 11:11:17 +01:00
check_apism_count=0
2023-01-20 11:54:49 +01:00
echo "ERROR APISM not working"
continue 2
fi
2023-01-25 11:11:17 +01:00
echo "[$check_apism_count]: $(date +'%Y-%m-%d %T') check APISM"
2023-01-20 11:54:49 +01:00
sleep 60s
else
# APISM up and working
check_apism_count=0
break
fi
done
2023-01-25 11:11:17 +01:00
echo "$(date +'%Y-%m-%d %T') checking for updates..."
exit 1
2023-01-20 11:54:49 +01:00
# Are there new updates available ?
if ! check_for_updates; then
echo "DEBUG no updates available"
continue
fi
2023-01-30 08:27:30 +01:00
echo "$(date +'%Y-%m-%d %T') fetching updates..."
exit 1
2023-01-20 11:54:49 +01:00
# 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
}
###############################################################################
2023-01-25 11:11:17 +01:00
if [ $# -ne 1 ] ; then
echo "Usage: $0 filename"
exit 1
else
if read_config "$1" ; then
2022-06-02 15:59:11 +02:00
2023-01-25 11:11:17 +01:00
init_fifo
UpdateController
fi
fi