moved implementation to update_psa_impl

This commit is contained in:
Gerhard Hoffmann 2022-06-03 21:48:41 +02:00
parent 43110e0cd7
commit 03eba24bbe

View File

@ -1,181 +1,38 @@
#!/bin/bash #! /bin/bash -
# set -x # Implementing update functionality for an PSA.
source ./general_utils
source ./log_helpers
source ./git_helpers
# source ./update_helpers.sh
# source ./opkg_helpers.sh
source ./update_psa_helpers
source ./read_config
###############################################################################
# update_psa
#
# Implementing the UpdateController (see below). UpdateController is waiting
# for an external trigger by the DeviceController (DC). The trigger is sent via
# an UNIX-pipe (=fifo). When DC triggers UpdateController, it has to check if
# an update is possible, i.e. is has to lock the PSA for the update procedure.
# UpdateController will send back the update-result to DeviceController
# which unlocks the PSA to return to nomal operation.
#
# If UpdateController has been triggered, it checks out a predefined git-
# repository. As a sanity check it makes sure that there are some changes in
# the repository. In case there are no changes, it issues an error message
# to DC and returns to its wait-state.
#
# Otherwise it checks if the changed files are correct (using md5) and if
# the new files are valid for the PSA. If there is some problem, it issues an
# error message to DC, reverts the git-repository to its previous state and
# returns to its wait-state.
#
# Otherwise it makes a backup of the current state of the PSA, and if there
# are opkg-packages to install it runs a dry installation process using opkg.
# In case of error, it issues an error message to DC, deletes the backup,
# reverts # the git-repository to its previous state and returns to its
# wait-state.
#
# Otherwise, it copies all new files to their target locations and in case
# of opkg-packages, it installs the packages.
#
# In case of error it restores the previous state using the backup and
# reinstalls the previous opkg-package(s). It issues an error message to
# DC and returns to its wait-state.
#
# Otherwise the update went well, and it does some cleanup, sends a success
# message to DC and returns to its wait-state.
#
############################################################################### ###############################################################################
# #
# UPDATE PROCEDURE # UPDATE PSA
# Usage:
# update_psa [ --? ]
# [ --help ]
# [ --wdir "working_directory" ]
# [ --file "config_file" ]
# [ --dbg ]
# [ --version ]
#
# ./update_psa --file $PWD/update_psa.conf --wdir $PWD
# #
############################################################################### ###############################################################################
update() { if [ $# -eq 0 ]; then
local try_update_count=0 # no parameters given -> nothing to do
local func="${FUNCNAME[0]}"
# read config parameters
# read_config
log_debug "$func:${LINENO}: fetch/merge updates..."
# Fetch new updates (using git)
while :
do
local repository_is_already_up_to_date=""
if ! fetch_customer_updates repository_is_already_up_to_date; then
if [ "$repository_is_already_up_to_date" = "yes" ]; then
log_error "$func:${LINENO}: $customer_id is up-to-date"\
"-> no files to update -> no psa update"
exit 1
fi
try_updates_count=$((try_updates_count+1))
if [[ "$try_updates_count" -eq 5 ]]; then
log_error "$func:${LINENO}: fetch/merging failed" ; exit 1
fi
sleep 60s
else
# Fetched updates successfully
try_updates_count=0
break
fi
done
# TODO: backup implementieren
# Backup before any updates in case some previous test was wrong
if ! backup_previous_version; then
log_error "$func:${LINENO}: backup failed"
revert_customer_repository ; exit 1
fi
local changed_files=$(changed_file_names)
local conf_ini_files=$(filter_conf_ini_files "$changed_files")
# check if *.conf and/or *.ini-files have to md5-sum as denoted
# in update.conf
if ! check_md5_for_changed_conf_and_ini_files "$conf_ini_files" ; then
log_error "$func:${LINENO}: new customer files wrong"
revert_customer_repository ; exit 1
fi
# copy *.conf and/or *.ini-files to their corresponding system-directories
if ! copy $conf_ini_files; then
log_error "$func:${LINENO}: copy operation failed"
revert_customer_repository ; exit 1
fi
# check if the opkg-command-file has been changed during 'git pull'
if grep -qE ".*opkg_commands.*?" <<< $changed_files; then
# read opkg_cmds: each line respresents an opkg-command
readarray opkg_commands < <(cat $opkg_cmds_file)
for opkg_c in "${opkg_commands[@]}"; do
if grep -qE "^\s*[#]+.*$" <<< $opkg_c; then
continue # found comment line
fi
# package manipulation commands without package:
local cwp="update|upgrade|clean"
# informational commands without package:
cwp="${cwp}|list|list-installed|list-upgradable"
if grep -qE "^.*\s+($cwp)\s+.*$" <<< $opkg_c; then
local p=$(printf '%s' "$opkg_c" | awk '{ print $NF }')
local opkg_output=()
if ! exec_opkg_info "$p" opkg_output; then
log_error "$func:${LINENO}: opkg info $opkg_c failed"
revert_customer_repository ; exit 1
fi
if ! check_md5_for_opkg_packages opkg_output; then
log_error "$func:${LINENO}: "\
"wrong md5sum for opkg packages"
revert_customer_repository ; exit 1
fi
fi
# perform a dry-run and check if everything might work as expected.
if ! exec_opkg_noaction $opkg_c; then
log_error "$func:${LINENO}: "\
"opkg --noaction $opkg_c failed"
fi
# Actually execute the opkg command
if ! exec_opkg $opkg_c; then
log_error "$func:${LINENO}: exec_opkg $opkg_c failed"
fallback_to_previous_version
revert_customer_repository ; exit 1
fi
done
else
log_info "$func:${LINENO}: no opkg commnds to execute"
fi
# Cleanup.
if ! cleanup_previous_version; then
log_error "$func:${LINENO}: cleanup_previous_version failed"
fi
log_info "$func:${LINENO}: success"
exit 0 exit 0
} fi
###############################################################################
if [ -z $IFS ]; then if [ -z $IFS ]; then
IFS=$'\n' IFS=$'\n'
fi fi
readonly PROGRAM=`basename $0` # parse commandline parameters
readonly VERSION="0.8.0"
config_filename=""
while test $# -gt 0; do while test $# -gt 0; do
case $1 in case $1 in
--file | --fil | --fi | --f | -file | -fil | -fi | -f ) --file | --fil | --fi | --f | -file | -fil | -fi | -f )
__conf_file=yes shift
CONFIGFILENAME="$1"
;;
--wdir | --wdi | --wd | --w | -wdir | -wdi | -wd | -w )
shift
WORKING_DIRECTORY="$1"
;; ;;
--help | -hel | --he | --h | '--?' | -help | -hel | -he | -h | '-?' ) --help | -hel | --he | --h | '--?' | -help | -hel | -he | -h | '-?' )
usage_and_exit 0 usage_and_exit 0
@ -183,6 +40,7 @@ while test $# -gt 0; do
--version | --versio | --versi | --vers | --ver | --ve | --v | \ --version | --versio | --versi | --vers | --ver | --ve | --v | \
-version | -versio | -versi | -vers | -ver | -ve | -v ) -version | -versio | -versi | -vers | -ver | -ve | -v )
version version
exit 0
;; ;;
--dbg | --db | --d | -dbg | -db | -d ) --dbg | --db | --d | -dbg | -db | -d )
set_dbg_level $DEBUG set_dbg_level $DEBUG
@ -191,26 +49,33 @@ while test $# -gt 0; do
error "Unrecognized option: $1" error "Unrecognized option: $1"
;; ;;
*) *)
if [ "$__conf_file" = "yes" ]; then
config_filename="$1"
__conf_file=""
else
error "Unrecognized parameter string: $1"
fi
break break
;; ;;
esac esac
shift shift
done done
if ! [ -z "$config_filename" ]; then source ./update_psa_impl
if read_config "$config_filename" ; then
# set -x if read_config
if clone_customer_repository $customer_repository_path ; then then
check_sanity_of_repository if clone_customer_repository $CUSTOMER_REPOSITORY_PATH
update then
check_sanity_of_repository
reset_update_trigger
set_updates_available
sleep 10
if updates_available
then
update_psa_started
sleep 5
update_psa_in_progress
sleep 5
update_status
fi fi
# update_psa
fi fi
else
error "config-file missing"
fi fi
exit $EXIT_CODE
###############################################################################