Compare commits

..

11 Commits

7 changed files with 268 additions and 256 deletions

30
general_utils Normal file
View File

@ -0,0 +1,30 @@
#!/bin/bash
# set -x
source ./log_helpers
exec_process_substitution () {
local func="${FUNCNAME[0]}"
log_debug "$func:${LINENO} exec-ing [$*]"
exec {fd}< <(eval "$@")
local __result_code=$?
local ps_pid=$! # remember pid of process substitution
local __result=""
while read __tmp <&$fd; do
if ! [ -z "$__tmp" ]; then
__result="${__result}$__tmp"
fi
done
exec {fd}>&- # close fd (i.e. process substitution)
wait $ps_pid # wait for the subshell to finish
__result=${__result//[$'\r\n\t']/ } # remove \r\n\t from __result
log_debug "$func:${LINENO} result=$__result"
printf '%s' $__result
return $__result_code
}

28
git.sh
View File

@ -1,28 +0,0 @@
#!/bin/bash
set -x
commit_before_pull=""
save_commit_before_pull () {
commit_before_pull=$(git reflog | grep "HEAD@{0}" | cut -d" " -f1)
if ! [ -z "$commit_before_pull" ]; then
if grep -qE "^[[:xdigit:]]{6,}$" <<< $commit_before_pull; then
return 0
fi
fi
commit_before_pull=""
return 1
}
# save_commit_before_pull
revert_to_commit_before_pull () {
if ! [ -z "$commit_before_pull" ]; then
if grep -qE "^[[:xdigit:]]{6,}$" <<< $commit_before_pull; then
`git reset --hard "$commit_before_pull"`
if [ $? -eq 0 ]; then
return 0
fi
fi
fi
return 1
}
# revert_to_commit_before_pull

View File

@ -1,14 +1,14 @@
#!/bin/bash #!/bin/bash
# set -x # set -x
source ./log_helpers
source ./general_utils
# if [ ${git_helpers_sourced:-1} = "1" ]; then # if [ ${git_helpers_sourced:-1} = "1" ]; then
# readonly git_helpers_sourced=${BASH_SOURCE[0]} # readonly git_helpers_sourced=${BASH_SOURCE[0]}
#else
# return 0
#fi
# source ./log_helpers
readonly GIT_SSL_NO_VERIFY=true readonly GIT_SSL_NO_VERIFY=true
readonly repository_already_up_to_date=2 readonly repository_already_up_to_date=2
@ -16,36 +16,18 @@ readonly repository_already_up_to_date=2
# #
exec_git_command () { exec_git_command () {
local func="${FUNCNAME[0]}" local func="${FUNCNAME[0]}"
log_debug "$func:${LINENO} exec-ing [$*]" log_debug "$func:${LINENO} exec-ing [$*]"
exec {fd}< <(eval "$@") local __git_result=$(exec_process_substitution $*)
log_debug "$func:${LINENO} result=$__git_result"
# if [ "$*" = "git pull" ]; then printf '%s' "$__git_result"
# else
# exec {fd}< <($@)
# fi
local ps_pid=$! # remember pid of process substitution
local git_result=""
while read t <&$fd; do
if ! [ -z "$t" ]; then
git_result="${git_result}$t"
fi
done
exec {fd}>&- # close fd (i.e. process substitution)
wait $ps_pid # wait for the subshell to finish
git_result=$(printf "$git_result" | tr '\n' ' ')
printf "%s\n" $git_result
} }
# #
latest_commit () { latest_commit () {
local func="${FUNCNAME[0]}" local func="${FUNCNAME[0]}"
# git reflog -> 46c5896 HEAD@{0}: commit: Made update_helpers.sh executable # git reflog -> 46c5896 HEAD@{0}: commit: Made update_helpers executable
local c=$(git reflog | grep "HEAD@{0}" | cut -d" " -f1) local c=$(git reflog | grep "HEAD@{0}" | cut -d" " -f1)
if ! [ -z "$c" ]; then if ! [ -z "$c" ]; then
if grep -qE "^[[:xdigit:]]{6,}$" <<< $c; then if grep -qE "^[[:xdigit:]]{6,}$" <<< $c; then
@ -99,14 +81,14 @@ clone_customer_repository () {
cd - cd -
fi fi
else else
# the directory is not empty, so we assume the customer-repository # the directory is not empty, so we assume the
# has been cloned alread # customer-repository has been cloned already
if ! [[ -d ./${workspace_dir}/$customer_id ]]; then if ! [[ -d ./${workspace_dir}/$customer_id ]]; then
local m="wrong repository: $(ls -d './${workspace_dir}/*')" log_fatal "$func:${LINENO} "\
log_fatal "$func:${LINENO} $m" "wrong repository: $(ls -d './${workspace_dir}/*')"
else else
log_debug \ local __m= "./${workspace_dir}/$customer_id exists"
"$func:${LINENO} ./${workspace_dir}/$customer_id exists" log_debug "$func:${LINENO} $__m"
return 0 return 0
fi fi
fi fi
@ -129,7 +111,7 @@ cd_customer_repository () {
fi fi
if ! { cd $repository_dir; } ; then if ! { cd $repository_dir; } ; then
log_crit "$func:${LINENO}: cannot change to $repository_dir!" log_crit "$func:${LINENO}: cannot cd to $repository_dir!"
return 1 return 1
fi fi
@ -268,3 +250,4 @@ changed_file_names () {
"while in $PWD" "while in $PWD"
fi fi
} }
# fi

View File

@ -1,11 +1,8 @@
#!/bin/bash #!/bin/bash
# set -x # set -x
#if [ ${log_helpers_sourced:-1} = "1" ]; then if [ ${log_helpers_sourced:-1} = "1" ]; then # include ony once
# readonly log_helpers_sourced=${BASH_SOURCE[0]} readonly log_helpers_sourced=${BASH_SOURCE[0]}
#else
# return 0
#fi
readonly log_file=/var/log/update_controller.log readonly log_file=/var/log/update_controller.log
if ! [ -f "$log_file" ]; then if ! [ -f "$log_file" ]; then
@ -79,5 +76,5 @@ log_fatal() {
exit 1 exit 1
fi fi
} }
###############################################################################
# log "test message1" "test message2" fi ### include guard

View File

@ -1,13 +1,10 @@
#!/bin/bash #!/bin/bash
# set -x # set -x
#if [ ${read_config_sourced:-1} = "1" ]; then source ./log_helpers
# readonly read_config_sourced=${BASH_SOURCE[0]}
#else
# return 0
#fi
# source ./log_helpers if [ ${read_config_sourced:-1} = "1" ]; then # include only once
readonly read_config_sourced=${BASH_SOURCE[0]}
# read config file (JSON syntax) # read config file (JSON syntax)
# #
@ -43,27 +40,34 @@ read_config() {
fi fi
# check customer_location # check customer_location
local customer_location="$(cat "$1" | jq -r .customer_location)" local customer_location=$(cat "$1" | jq -r .customer_location)
if [ -z "$customer_location" ]; then if [ -z "$customer_location" ]; then
log_fatal "$func:${LINENO}: customer_location not set in $1" log_fatal "$func:${LINENO}: customer_location not set in $1"
fi fi
readonly working_directory="$(cat "$1" | jq -r .working_directory)" readonly working_directory="$(cat "$1" | jq -r .working_directory)"
log_info "$func:${LINENO}: working_directory is $working_directory" log_info "$func:${LINENO}: working_directory is $working_directory"
readonly workspace_dir="$(cat "$1" | jq -r .workspace_dir)" readonly workspace_dir="$(cat "$1" | jq -r .workspace_dir)"
local __customer_base_dir="$working_directory/${workspace_dir}" local __customer_base_dir="$working_directory/${workspace_dir}"
__customer_base_dir="${__customer_base_dir}/${customer_id}" __customer_base_dir="${__customer_base_dir}/${customer_id}"
__customer_base_dir="${__customer_base_dir}/${customer_location}" __customer_base_dir="${__customer_base_dir}/${customer_location}"
readonly customer_location_dir="$__customer_base_dir"
log_info "$func:${LINENO}: customer-location-dir is $customer_location_dir"
readonly customer_base_dir="${__customer_base_dir}/${zone_group}/${zone}" readonly customer_base_dir="${__customer_base_dir}/${zone_group}/${zone}"
log_info "$func:${LINENO}: customer-base-dir is $customer_base_dir" log_info "$func:${LINENO}: customer-base-dir is $customer_base_dir"
readonly opkg_cmds_file="${customer_base_dir}/etc/psa_update/opkg_commands" readonly opkg_cmds_file="${customer_base_dir}/etc/psa_update/opkg_commands"
log_info "$func:${LINENO}: opkg_cmds_file is $opkg_cmds_file"
log_info "$func:${LINENO}: opkg_cmds_file is $opkg_cmds_file"
return 0 return 0
} }
# read_config UpdateController.conf # read_config UpdateController.conf
fi

View File

@ -82,12 +82,14 @@ update() {
fi fi
done done
# TODO: backup implementieren
# Backup before any updates in case some previous test was wrong # Backup before any updates in case some previous test was wrong
if ! backup_previous_version; then if ! backup_previous_version; then
log_error "$func:${LINENO}: backup failed" log_error "$func:${LINENO}: backup failed"
revert_customer_repository ; exit 1 revert_customer_repository ; exit 1
fi fi
# TODO: implementieren, opkg pakete ausschliessen
files=$(changed_file_names) files=$(changed_file_names)
if ! check_md5_for_changed_customer_files $files ; then if ! check_md5_for_changed_customer_files $files ; then
log_error "$func:${LINENO}: new customer files wrong" log_error "$func:${LINENO}: new customer files wrong"
@ -97,31 +99,40 @@ update() {
if grep -qE ".*opkg_commands.*?" <<< $files; then if grep -qE ".*opkg_commands.*?" <<< $files; then
# read opkg_cmds: each line respresents an opkg-command # read opkg_cmds: each line respresents an opkg-command
readarray opkg_commands < <(cat $opkg_cmds_file) readarray opkg_commands < <(cat $opkg_cmds_file)
for opkg_command in "${opkg_commands[@]}"; do for opkg_c in "${opkg_commands[@]}"; do
if grep -qE "^\s*[#]+.*?$" <<< $opkg_command; then if grep -qE "^\s*[#]+.*$" <<< $opkg_c; then
continue # found comment line continue # found comment line
fi fi
# FIXME: sollte nicht gebraucht werden # package manipulation commands without package:
opkg_command=${opkg_command//[$'\r\n\t']/ } local cwp="update|upgrade|clean"
local package=$(printf '%s' "$opkg_command" | awk '{ print $NF }') # 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=() local opkg_output=()
if ! exec_opkg_info "$package" opkg_output; then if ! exec_opkg_info "$p" opkg_output; then
log_error "$func:${LINENO}: opkg --noaction $opkg_command failed" log_error "$func:${LINENO}: opkg info $opkg_c failed"
revert_customer_repository ; exit 1 revert_customer_repository ; exit 1
fi fi
if ! check_md5_for_opkg_packages opkg_output; then if ! check_md5_for_opkg_packages opkg_output; then
log_error "$func:${LINENO}: wrong md5sum for some opkg packages" log_error "$func:${LINENO}: "\
"wrong md5sum for opkg packages"
revert_customer_repository ; exit 1 revert_customer_repository ; exit 1
fi fi
fi
# perform a dry-run and check if everything might work as expected. # perform a dry-run and check if everything might work as expected.
if ! exec_opkg_no_action $opkg_c; then
log_error "$func:${LINENO}: "\
"opkg --noaction $opkg_c failed"
fi
# Actually execute the opkg command # Actually execute the opkg command
if ! exec_opkg $opkg_command; then if ! exec_opkg $opkg_c; then
log_error "$func:${LINENO}: exec_opkg $opkg_command failed" log_error "$func:${LINENO}: exec_opkg $opkg_c failed"
fallback_to_previous_version fallback_to_previous_version
revert_customer_repository ; exit 1 revert_customer_repository ; exit 1
fi fi

67
update_psa_helpers Normal file → Executable file
View File

@ -6,25 +6,19 @@
# return 0 # return 0
#fi #fi
source ./general_utils
exec_opkg_command () { exec_opkg_command () {
local func="${FUNCNAME[0]}" local func="${FUNCNAME[0]}"
log_debug "$func:${LINENO} exec-ing [$*]" log_debug "$func:${LINENO} exec-ing [$*]"
exec {fd}< <(eval "$@") local __result=$(exec_process_substitution $*)
local ps_pid=$! # remember pid of process substitution local __result_code=$?
local opkg_result="" log_debug "$func:${LINENO} result=$__result"
while read tmp <&$fd; do
if ! [ -z "$tmp" ]; then
opkg_result="${opkg_result}$tmp"
fi
done
exec {fd}>&- # close fd (i.e. process substitution) printf '%s' "$__result"
wait $ps_pid # wait for the subshell to finish return $__result_code
printf '%s' "$opkg_result"
} }
# Fetch/merge updates from predefined repository using git. # Fetch/merge updates from predefined repository using git.
@ -55,23 +49,34 @@ check_md5_for_opkg_packages () {
local func="${FUNCNAME[0]}" local func="${FUNCNAME[0]}"
local -n opkg_output_ref=$1 local -n opkg_output_ref=$1
local package="" local package=""
local md5sum="" local md5sum_opkg_info=""
local filename="" local filename=""
for line in ${opkg_output_ref[@]}; do for line in ${opkg_output_ref[@]}; do
log_info "$func:${LINENO}: line=$line"
if grep -qE "^\s*Package\s*:.*?$" <<< "$line"; then if grep -qE "^\s*Package\s*:.*?$" <<< "$line"; then
package=${line#*:* } package=${line#*:* }
printf 'package=%s\n' "$package"
elif grep -qE "^\s*MD5Sum\s*:.*?$" <<< "$line"; then elif grep -qE "^\s*MD5Sum\s*:.*?$" <<< "$line"; then
md5sum=${line#*:* } md5sum_opkg_info=${line#*:* }
printf 'md5sum=%s\n' "$md5sum"
elif grep -qE "^\s*Filename\s*:.*?$" <<< "$line"; then elif grep -qE "^\s*Filename\s*:.*?$" <<< "$line"; then
filename=${line#*:* } filename=${line#*:* }
printf 'filename=%s\n' "$filename"
fi fi
done done
log_debug "$func:${LINENO}: $package | $md5sum | $filename" 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 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 # In case the new checked-out files are not correct, revert the git
@ -91,10 +96,10 @@ backup_previous_version () {
exec_opkg_info () { exec_opkg_info () {
local func="${FUNCNAME[0]}" local func="${FUNCNAME[0]}"
log_info "$func:${LINENO}: executing $opkg_command" log_info "$func:${LINENO}: executing info $1"
opkg_result=$(exec_opkg_command "opkg info $1") opkg_result=$(exec_opkg_command "opkg info $1")
if [ $? -eq 0 ]; then
# make sure the keywords start with '\n' # make sure the keywords start with '\n'
opkg_result=$(sed -E -e "s/(^.*)(Package)(.*$)/\n\2\3/g"\ opkg_result=$(sed -E -e "s/(^.*)(Package)(.*$)/\n\2\3/g"\
-e "s/(^.*)(Version)(.*$)/\1\n\2\3/g"\ -e "s/(^.*)(Version)(.*$)/\1\n\2\3/g"\
@ -109,7 +114,8 @@ exec_opkg_info () {
-e "s/(^.*)(Installed-Size)(.*$)/\1\n\2\3/g"\ -e "s/(^.*)(Installed-Size)(.*$)/\1\n\2\3/g"\
-e "s/(^.*)(Section)(.*$)/\1\n\2\3/g"\ -e "s/(^.*)(Section)(.*$)/\1\n\2\3/g"\
-e "s/(^.*[^-])(Size)(.*$)/\1\n\2\3/g"\ -e "s/(^.*[^-])(Size)(.*$)/\1\n\2\3/g"\
-e "s/(^.*)(Installed-Time)(.*$)/\1\n\2\3/g" <<< "$opkg_result") -e "s/(^.*)(Installed-Time)(.*$)/\1\n\2\3/g"
<<< "$opkg_result")
local -n output_ref=$2 local -n output_ref=$2
@ -120,8 +126,12 @@ exec_opkg_info () {
fi fi
log_info "$func:${LINENO}: ... done" log_info "$func:${LINENO}: ... done"
log_info "$func:${LINENO}: opkg_result=${output_ref[@]}" # log_info "$func:${LINENO}: opkg_result=${output_ref[@]}"
return 0 return 0
fi
log_error "$func:${LINENO}: executing info $1"
return 1
} }
# Try to install new opkg-packages (in case the are some # Try to install new opkg-packages (in case the are some
@ -138,11 +148,16 @@ exec_opkg_no_action() {
# #
exec_opkg () { exec_opkg () {
local func="${FUNCNAME[0]}" local func="${FUNCNAME[0]}"
log_info "$func:${LINENO}: executing $opkg_command" log_debug "$func:${LINENO}: executing $1"
local opkg_result=$(exec_opkg_command "opkg $1")
local __opkg_result=$(exec_opkg_command "opkg $1")
if [ $? -eq 0 ]; then
log_info "$func:${LINENO}: opkg_result=$__opkg_result"
return 0 return 0
fi
log_error "$func:${LINENO}: error executing $1"
return 1
} }
# In case there was some error, re-install the previous package(s) # In case there was some error, re-install the previous package(s)