UpdateController/update_psa

216 lines
7.5 KiB
Bash
Executable File

#!/bin/bash
# set -x
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() {
local try_update_count=0
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 files=$(changed_file_names)
local system_files=$(filter_system_files "$files") # conf/ini-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 "$system_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 $system_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.*?" <<< $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
}
###############################################################################
if [ -z $IFS ]; then
IFS=$'\n'
fi
readonly PROGRAM=`basename $0`
readonly VERSION="0.8.0"
config_filename=""
while test $# -gt 0; do
case $1 in
--file | --fil | --fi | --f | -file | -fil | -fi | -f )
__conf_file=yes
;;
--help | -hel | --he | --h | '--?' | -help | -hel | -he | -h | '-?' )
usage_and_exit 0
;;
--version | --versio | --versi | --vers | --ver | --ve | --v | \
-version | -versio | -versi | -vers | -ver | -ve | -v )
version
;;
--dbg | --db | --d | -dbg | -db | -d )
set_dbg_level $DEBUG
;;
-*)
error "Unrecognized option: $1"
;;
*)
if [ "$__conf_file" = "yes" ]; then
config_filename="$1"
__conf_file=""
else
error "Unrecognized parameter string: $1"
fi
break
;;
esac
shift
done
if ! [ -z "$config_filename" ]; then
if read_config "$config_filename" ; then
# set -x
if clone_customer_repository $customer_repository_path ; then
update
fi
fi
else
error "config-file missing"
fi