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,83 +1,80 @@
#!/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
touch $log_file touch $log_file
fi
readonly DEBUG=0
readonly INFO=1
readonly WARN=2
readonly CRIT=3
readonly ERROR=4
readonly FATAL=5
readonly MAX_DEBUG_LEVEL=6
log_level=0
set_dbg_level () {
if [ $1 < $MAX_DEBUG_LEVEL ]; then
log_level=$1
fi fi
}
dbg_level () { readonly DEBUG=0
return $log_level readonly INFO=1
} readonly WARN=2
readonly CRIT=3
readonly ERROR=4
readonly FATAL=5
readonly MAX_DEBUG_LEVEL=6
log() { log_level=0
if [[ $(("$(wc -l < $log_file)")) -ge $((100000)) ]]; then
# remove first line
sed -e 1d -i $log_file
fi
local msg="$(date +'%Y-%m-%d_%T'): $*"
printf "log:$msg\n" >&2
printf "$msg\n" >> $log_file
}
log_debug() { set_dbg_level () {
if [ $log_level = $DEBUG ]; then if [ $1 < $MAX_DEBUG_LEVEL ]; then
log "DEBUG $*" log_level=$1
fi fi
} }
log_info() { dbg_level () {
if [ $log_level -le $INFO ]; then return $log_level
log "INFO $*" }
fi
}
log_warn() { log() {
if [ $log_level -le $WARN ]; then if [[ $(("$(wc -l < $log_file)")) -ge $((100000)) ]]; then
log "WARN $*" # remove first line
fi sed -e 1d -i $log_file
} fi
local msg="$(date +'%Y-%m-%d_%T'): $*"
printf "log:$msg\n" >&2
printf "$msg\n" >> $log_file
}
log_crit() { log_debug() {
if [ $log_level -le $CRIT ]; then if [ $log_level = $DEBUG ]; then
log "CRIT $*" log "DEBUG $*"
fi fi
} }
log_error() { log_info() {
if [ $log_level -le $ERROR ]; then if [ $log_level -le $INFO ]; then
log "ERROR $*" log "INFO $*"
fi fi
} }
log_fatal() { log_warn() {
if [ $log_level -le $FATAL ]; then if [ $log_level -le $WARN ]; then
log "FATAL $*" log "WARN $*"
log "exiting ..." fi
exit 1 }
fi
}
# log "test message1" "test message2" log_crit() {
if [ $log_level -le $CRIT ]; then
log "CRIT $*"
fi
}
log_error() {
if [ $log_level -le $ERROR ]; then
log "ERROR $*"
fi
}
log_fatal() {
if [ $log_level -le $FATAL ]; then
log "FATAL $*"
log "exiting ..."
exit 1
fi
}
###############################################################################
fi ### include guard

View File

@ -1,69 +1,73 @@
#!/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)
# #
read_config() { read_config() {
local func="${FUNCNAME[0]}" local func="${FUNCNAME[0]}"
# check customer_id # check customer_id
__customer_id="$(cat "$1" | jq -r .customer_id)" __customer_id="$(cat "$1" | jq -r .customer_id)"
if [ -z "$__customer_id" ]; then if [ -z "$__customer_id" ]; then
log_fatal "$func:${LINENO} customer_id not set in $1" log_fatal "$func:${LINENO} customer_id not set in $1"
fi fi
readonly customer_id="customer_${__customer_id}" readonly customer_id="customer_${__customer_id}"
log_info "$func:${LINENO}: customer-id is $customer_id" log_info "$func:${LINENO}: customer-id is $customer_id"
# check customer_repository # check customer_repository
__repository_path="$(cat "$1" | jq -r .repository_path)" __repository_path="$(cat "$1" | jq -r .repository_path)"
if [ -z "$__repository_path" ]; then if [ -z "$__repository_path" ]; then
log_fatal "$func:${LINENO}: repository path not set in $1" log_fatal "$func:${LINENO}: repository path not set in $1"
fi fi
readonly repository_path="${__repository_path}/${customer_id}.git" readonly repository_path="${__repository_path}/${customer_id}.git"
log_info "$func:${LINENO}: repository path is $repository_path" log_info "$func:${LINENO}: repository path is $repository_path"
# check zone_group # check zone_group
local zone_group="$(cat "$1" | jq -r .zone_group)" local zone_group="$(cat "$1" | jq -r .zone_group)"
if [ -z "$zone_group" ]; then if [ -z "$zone_group" ]; then
log_fatal "$func:${LINENO}: zone_group not set in $1" log_fatal "$func:${LINENO}: zone_group not set in $1"
fi fi
# check zone # check zone
local zone="$(cat "$1" | jq -r .zone)" local zone="$(cat "$1" | jq -r .zone)"
if [ -z "$zone" ]; then if [ -z "$zone" ]; then
log_fatal "$func:${LINENO}: zone not set in $1" log_fatal "$func:${LINENO}: zone not set in $1"
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"
readonly workspace_dir="$(cat "$1" | jq -r .workspace_dir)" log_info "$func:${LINENO}: working_directory is $working_directory"
local __customer_base_dir="$working_directory/${workspace_dir}"
__customer_base_dir="${__customer_base_dir}/${customer_id}"
__customer_base_dir="${__customer_base_dir}/${customer_location}"
readonly customer_base_dir="${__customer_base_dir}/${zone_group}/${zone}" readonly workspace_dir="$(cat "$1" | jq -r .workspace_dir)"
log_info "$func:${LINENO}: customer-base-dir is $customer_base_dir"
readonly opkg_cmds_file="${customer_base_dir}/etc/psa_update/opkg_commands" local __customer_base_dir="$working_directory/${workspace_dir}"
log_info "$func:${LINENO}: opkg_cmds_file is $opkg_cmds_file" __customer_base_dir="${__customer_base_dir}/${customer_id}"
__customer_base_dir="${__customer_base_dir}/${customer_location}"
return 0 readonly customer_location_dir="$__customer_base_dir"
}
# read_config UpdateController.conf log_info "$func:${LINENO}: customer-location-dir is $customer_location_dir"
readonly customer_base_dir="${__customer_base_dir}/${zone_group}/${zone}"
log_info "$func:${LINENO}: customer-base-dir is $customer_base_dir"
readonly opkg_cmds_file="${customer_base_dir}/etc/psa_update/opkg_commands"
log_info "$func:${LINENO}: opkg_cmds_file is $opkg_cmds_file"
return 0
}
# 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"
local opkg_output=() if grep -qE "^.*\s+($cwp)\s+.*$" <<< $opkg_c; then
if ! exec_opkg_info "$package" opkg_output; then local p=$(printf '%s' "$opkg_c" | awk '{ print $NF }')
log_error "$func:${LINENO}: opkg --noaction $opkg_command failed" local opkg_output=()
revert_customer_repository ; exit 1 if ! exec_opkg_info "$p" opkg_output; then
fi log_error "$func:${LINENO}: opkg info $opkg_c failed"
revert_customer_repository ; exit 1
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}: "\
revert_customer_repository ; exit 1 "wrong md5sum for opkg packages"
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

109
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"
return 0 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 # In case the new checked-out files are not correct, revert the git
@ -91,37 +96,42 @@ 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'
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")
# make sure the keywords start with '\n' local -n output_ref=$2
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
readarray -d $'\n' output_ref < <(printf '%s' "$opkg_result") log_info "$func:${LINENO}: ... done"
if [ $? -ne 0 ]; then # log_info "$func:${LINENO}: opkg_result=${output_ref[@]}"
log_error "$func:${LINENO}: readarray finished with error" return 0
return 1
fi fi
log_info "$func:${LINENO}: ... done" log_error "$func:${LINENO}: executing info $1"
log_info "$func:${LINENO}: opkg_result=${output_ref[@]}" return 1
return 0
} }
# 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
fi
return 0 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)