From f56b74c0e5196f4ce7b6bf46e582327324ad81bc Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Fri, 20 Jan 2023 11:54:49 +0100 Subject: [PATCH] Basic skeleton of the UpdateController --- UpdateController.sh | 218 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 218 insertions(+) mode change 100644 => 100755 UpdateController.sh diff --git a/UpdateController.sh b/UpdateController.sh old mode 100644 new mode 100755 index e69de29..c2a37c4 --- a/UpdateController.sh +++ b/UpdateController.sh @@ -0,0 +1,218 @@ +#!/bin/bash + +read_config() { + return 0 +} + +wait_for_trigger() { + return 0 +} + +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. + while wait_for_trigger $trigger_timeout; do + 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 + +