UpdateController/update_psa_helpers

369 lines
12 KiB
Bash
Executable File

#!/bin/bash
# if [ ${git_helpers_sourced:-1} = "1" ]; then
# readonly git_helpers_sourced=${BASH_SOURCE[0]}
#else
# return 0
#fi
source ./general_utils
exec_opkg_command () {
local func="${FUNCNAME[0]}"
log_debug "$func:${LINENO} exec-ing [$*]"
local __result=$(exec_process_substitution $*)
local __result_code=$?
log_debug "$func:${LINENO} result=$__result"
printf '%s' "$__result"
return $__result_code
}
# Fetch/merge updates from predefined repository using git.
#
fetch_customer_updates() {
if ! pull_customer_repository $1; then
return 1
fi
return 0
}
copy () {
local func="${FUNCNAME[0]}"
local copy_necessary=0
readarray -td' ' files <<< "$1"
for f in ${files[@]}; do
log_debug "$func:${LINENO}: $f"
# $f is determined by git
if [[ "$f" =~ .*update[.]conf.* ]]; then
continue
fi
copy_necessary=1
PERCENT=$((PERCENT+1))
test $PERCENT -gt 100 && PERCENT=100
local __f=${f##*/}
local __m=""
local __copy_error=0
if grep -qE "^.*[.]json\s*$" <<< ${f}; then
__m="cp ${CUSTOMER_ID_BASE_DIR}/${f} to ${CONF_SYS_DIR}/${__f}"
cp "${CUSTOMER_ID_BASE_DIR}/${f}" "${CONF_SYS_DIR}/${__f}"
__copy_error=$?
elif grep -qE "^.*[.]ini\s*$" <<< ${f}; then
if [ "$__f" = "$ATBQT_INI" ]; then
__m="cp ${CUSTOMER_ID_BASE_DIR}/${f} $ATBAPP_SYS_DIR/${__f}"
cp "${CUSTOMER_ID_BASE_DIR}/${f}" $ATBAPP_SYS_DIR/${__f}
__copy_error=$?
elif [ "$__f" = "$SYSTEM_CONTROL_INI" ]; then
__m="cp ${CUSTOMER_ID_BASE_DIR}/${f} $SYSCONFIG_SYS_DIR/${__f}"
cp "${CUSTOMER_ID_BASE_DIR}/${f}" $SYSCONFIG_SYS_DIR/${__f}
__copy_error=$?
elif [ "$__f" = "$SYS_CONFIG_INI" ]; then
__m="cp ${CUSTOMER_ID_BASE_DIR}/${f} $SYSCONFIG_SYS_DIR/${__f}"
cp "${CUSTOMER_ID_BASE_DIR}/${f}" $SYSCONFIG_SYS_DIR/${__f}
__copy_error=$?
elif [ "$__f" = "$ISMASMGR_INI" ]; then
if grep -qE "$ISMASMGR_SYS_DIR" <<< "${f%/*}"; then
__m="cp ${CUSTOMER_ID_BASE_DIR}/${f}"
__m="$__m $ISMASMGR_SYS_DIR/${__f}"
cp "${CUSTOMER_ID_BASE_DIR}/${f}" $ISMASMGR_SYS_DIR/${__f}
__copy_error=$?
else
__m="cp ${CUSTOMER_ID_BASE_DIR}/${f}"
__m="$__m $SYSCONFIG_SYS_DIR/${__f}"
cp "${CUSTOMER_ID_BASE_DIR}/${f}" $SYSCONFIG_SYS_DIR/${__f}
__copy_error=$?
fi
fi
fi
if [ $__copy_error -ne 0 ]; then
log_error "$func:${LINENO}: $__m failed"
update_psa_copy_conf_and_ini_files \
$UPDATE_ISMAS_ERROR $RC_COPY_ERROR "$__m failed"
return $?
else
log_info "$func:${LINENO}: $__m ok"
update_psa_copy_conf_and_ini_files $UPDATE_ISMAS_PROGRESS \
$RC_SUCCESS "$__m ok"
fi
done
if [ $copy_necessary -eq 0 ]; then
update_psa_copy_conf_and_ini_files $UPDATE_ISMAS_PROGRESS $RC_SUCCESS \
"no copy of json/ini-files necessary"
log_debug "$func:${LINENO}: no copy of conf/ini-files necessary"
else
log_debug "$func:${LINENO}: copied *conf/*ini-files to system-dirs"
fi
return 0
}
filter_json_ini_files () {
local func="${FUNCNAME[0]}"
log_debug "$func:${LINENO} $1"
readarray -td' ' files <<< "$1"
local __system_files=""
for f in ${files[@]}; do
log_debug "$func:${LINENO} $f"
if grep -qE "^.*[.](json|ini)\s*$" <<< $f; then
if [ -z $__system_files ]; then
__system_files="${f}"
else
__system_files="$__system_files ${f}"
fi
fi
done
log_debug "$func:${LINENO} system-files=$__system_files"
printf '%s' "$__system_files"
}
#
md5_of () {
printf '%s' "$(md5sum "$1" | cut -d' ' -f1)"
}
# Check if the fetched/merged files have the correct md5 and are
# valid for the PSA.
#
check_md5_for_changed_json_and_ini_files () {
local func="${FUNCNAME[0]}"
local js_key="" # used by json-parser 'jq'
local md5sum_update_conf=""
local md5sum_repository=""
local readonly cut_md5="cut -d' ' -f1"
log_debug "$func:${LINENO} files=$1"
if ! [ -z $1 ]; then
readarray -td' ' files <<< "$1"
for file in ${files[@]}; do
log_debug "$func:${LINENO} checking file=${file}..."
local __fsuffix=${file##*.}
local __bn=$(basename $file .$__fsuffix)
if [[ "$file" =~ .*cash[.]json.* ]]; then
js_key=".conf.szeged.zg[1].z[1].etc.psa_config.$__bn"
md5sum_update_conf=$(cat $UPDATE_CONF | jq -r $js_key)
md5sum_repository="$(md5_of $CUSTOMER_ID_BASE_DIR/$file)"
elif [[ "$file" =~ .*print[0-9][0-9][.]json.* ]]; then
js_key=".conf.szeged.zg[1].z[1].etc.psa_config.$__bn"
echo "$js_key"
md5sum_update_conf=$(cat $UPDATE_CONF | jq -r $js_key)
md5sum_repository="$(md5_of $CUSTOMER_ID_BASE_DIR/$file)"
elif [[ "$file" =~ .*conf[.]json.* ]]; then
js_key=".conf.szeged.zg[1].z[1].etc.psa_config.DC2C_conf"
md5sum_update_conf=$(cat $UPDATE_CONF | jq -r $js_key)
md5sum_repository="$(md5_of $CUSTOMER_ID_BASE_DIR/$file)"
elif [[ "$file" =~ .*serial[.]json.* ]]; then
js_key=".conf.szeged.zg[1].z[1].etc.psa_config.DC2C_serial"
md5sum_update_conf=$(cat $UPDATE_CONF | jq -r $js_key)
md5sum_repository="$(md5_of $CUSTOMER_ID_BASE_DIR/$file)"
elif [[ "$file" =~ .*ATBQT[.]ini.* ]]; then
js_key=".ini.szeged.zg[1].z[1].opt.app.ATBAPP.ATBQT"
md5sum_update_conf=$(cat $UPDATE_CONF | jq -r $js_key)
md5sum_repository="$(md5_of $ATBQT_INI_FULL)"
elif [[ "$file" =~ .*sysconfig[.]ini.* ]]; then
js_key=".ini.szeged.zg[1].z[1].opt.app.sysconfig.sysconfig"
md5sum_update_conf=$(cat $UPDATE_CONF | jq -r $js_key)
md5sum_repository=$(md5_of $SYSCONFIG_SYSCONFIG_INI_FULL)
elif [[ "$file" =~ .*SystemControl[.]ini.* ]]; then
js_key=".ini.szeged.zg[1].z[1].opt.app.sysconfig.SystemControl"
md5sum_update_conf=$(cat $UPDATE_CONF | jq -r $js_key)
md5sum_repository=$(md5_of $SYSCONFIG_SYSCTRL_INI_FULL)
elif [[ "$file" =~ .*ISMASMgr/ISMASMgr[.]ini.* ]]; then
js_key=".ini.szeged.zg[1].z[1].opt.app.ISMASMgr.ISMASMgr"
md5sum_update_conf=$(cat $UPDATE_CONF | jq -r $js_key)
md5sum_repository=$(md5_of $ISMASMGR_ISMASMGR_INI_FULL)
elif [[ "$file" =~ .*sysconfig/ISMASMgr[.]ini.* ]]; then
js_key=".ini.szeged.zg[1].z[1].opt.app.sysconfig.ISMASMgr"
md5sum_update_conf=$(cat $UPDATE_CONF | jq -r $js_key)
md5sum_repository=$(md5_of $SYSCONFIG_ISMASMGR_INI_FULL)
else
continue
fi
PERCENT=$((PERCENT+1))
test $PERCENT -gt 100 && PERCENT=100
if [ "$md5sum_repository" = "$md5sum_update_conf" ]; then
log_info "$func:${LINENO}: md5sum for $file ok"
update_psa_check_hash $UPDATE_ISMAS_PROGRESS $RC_SUCCESS \
"md5sum -|$md5sum_repository|- for $file ok"
else
local __r="repository: $md5sum_repository"
local __u="update.conf=$md5sum_update_conf"
local __m="$__r != $__u"
log_error "$func:${LINENO}: md5sum for $file wrong: $__m"
update_psa_check_hash $UPDATE_ISMAS_ERROR $RC_HASH_VALUE_ERROR \
"md5sum -|$md5sum_repository|- for $file wrong"
return $?
fi
done
else
log_debug "$func:${LINENO} no changed conf-/ini-files"
fi
return 0
}
check_hardware_compatibility () {
local func="${FUNCNAME[0]}"
# RC_HW_COMPATIBILITY_ERROR=6
update_psa_check_hardware_compatibility $UPDATE_ISMAS_PROGRESS \
$RC_SUCCESS "TODO: hardware compatibility"
return $?
}
check_md5_for_opkg_packages () {
local func="${FUNCNAME[0]}"
local -n opkg_output_ref=$1
local package=""
local md5sum_opkg_info=""
local filename=""
for line in ${opkg_output_ref[@]}; do
log_info "$func:${LINENO}: line=$line"
if grep -qE "^\s*Package\s*:.*?$" <<< "$line"; then
package=${line#*:* }
elif grep -qE "^\s*MD5Sum\s*:.*?$" <<< "$line"; then
md5sum_opkg_info=${line#*:* }
elif grep -qE "^\s*Filename\s*:.*?$" <<< "$line"; then
filename=${line#*:* }
fi
done
local __update_conf="${CUSTOMER_LOCATION_DIR}/update.conf"
md5sum_repo=$(cat $__update_conf | jq -r .opkg.${package}.MD5Sum)
if ! [ -z $md5sum_repo ]; then
if [ "$md5sum_opkg_info" = "$md5sum_repo" ]; then
log_info "$func:${LINENO}: md5 $md5sum_repo OK for $package"
return 0
else
log_error "$func:${LINENO}: md5sum_repo [$md5sum_repo] "\
"!= md5sum_opkg_info [$md5sum_opkg_info] for $package"
fi
else
log_error "$func:${LINENO}: md5sum_repo empty"
fi
return 1
}
# In case the new checked-out files are not correct, revert the git
# repository to its previous state.
#
revert_customer_repository () {
local func="${FUNCNAME[0]}"
# TODO
return 0
}
# Backup before the update-process.
#
backup_previous_version () {
local func="${FUNCNAME[0]}"
# TODO
return 0
}
exec_opkg_info () {
local func="${FUNCNAME[0]}"
log_info "$func:${LINENO}: executing info $1"
opkg_result=$(exec_opkg_command "opkg info $1")
if [ $? -eq 0 ]; then
# make sure the keywords start with '\n'
opkg_result=$(sed -E -e "s/(^.*)(Package)(.*$)/\n\2\3/g"\
-e "s/(^.*)(Version)(.*$)/\1\n\2\3/g"\
-e "s/(^.*)(Depends)(.*$)/\1\n\2\3/g"\
-e "s/(^.*)(Status)(.*$)/\1\n\2\3/g"\
-e "s/(^.*)(Architecture)(.*$)/\1\n\2\3/g"\
-e "s/(^.*)(Maintainer)(.*$)/\1\n\2\3/g"\
-e "s/(^.*)(MD5Sum)(.*$)/\1\n\2\3/g"\
-e "s/(^.*)(Filename)(.*$)/\1\n\2\3/g"\
-e "s/(^.*)(Source)(.*$)/\1\n\2\3/g"\
-e "s/(^.*)(Description)(.*$)/\1\n\2\3/g"\
-e "s/(^.*)(Installed-Size)(.*$)/\1\n\2\3/g"\
-e "s/(^.*)(Section)(.*$)/\1\n\2\3/g"\
-e "s/(^.*[^-])(Size)(.*$)/\1\n\2\3/g"\
-e "s/(^.*)(Installed-Time)(.*$)/\1\n\2\3/g"
<<< "$opkg_result")
local -n output_ref=$2
readarray -d $'\n' output_ref < <(printf '%s' "$opkg_result")
if [ $? -ne 0 ]; then
log_error "$func:${LINENO}: readarray finished with error"
return 1
fi
log_info "$func:${LINENO}: ... done"
# log_info "$func:${LINENO}: opkg_result=${output_ref[@]}"
return 0
fi
log_error "$func:${LINENO}: executing info $1"
return 1
}
# Try to install new opkg-packages (in case the are some
# in the new git-checkout).
#
exec_opkg_noaction() {
local func="${FUNCNAME[0]}"
local opkg_command_no_action="opkg --noaction $1"
log_debug "$func:${LINENO}: executing $opkg_command_no_action"
local __opkg_result=$(exec_opkg_command "opkg --noaction $1")
if [ $? -eq 0 ]; then
log_info "$func:${LINENO}: opkg_result=$__opkg_result"
return 0
fi
log_error "$func:${LINENO}: error executing opkg --noaction $1"
return 1
}
# Install the new packages using opkg.
#
exec_opkg () {
local func="${FUNCNAME[0]}"
log_debug "$func:${LINENO}: executing $1"
local __opkg_result=$(exec_opkg_command "opkg $1")
if [ $? -eq 0 ]; then
log_info "$func:${LINENO}: opkg_result=$__opkg_result"
return 0
fi
log_error "$func:${LINENO}: error executing $1"
return 1
}
# In case there was some error, re-install the previous package(s)
# and use the backup to restore any conf/ini-files.
#
fallback_to_previous_version() {
local func="${FUNCNAME[0]}"
# TODO
return 0;
}
# If all went well, then execute all necessary cleanup steps.
#
cleanup_previous_version() {
local func="${FUNCNAME[0]}"
# TODO
return 0
}
check_for_apism () {
nc localhost 7778
}