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
# set -x
source ./log_helpers
source ./general_utils
# if [ ${git_helpers_sourced:-1} = "1" ]; then
# readonly git_helpers_sourced=${BASH_SOURCE[0]}
#else
# return 0
#fi
# source ./log_helpers
readonly GIT_SSL_NO_VERIFY=true
readonly repository_already_up_to_date=2
@ -16,36 +16,18 @@ readonly repository_already_up_to_date=2
#
exec_git_command () {
local func="${FUNCNAME[0]}"
log_debug "$func:${LINENO} exec-ing [$*]"
local __git_result=$(exec_process_substitution $*)
log_debug "$func:${LINENO} result=$__git_result"
exec {fd}< <(eval "$@")
# if [ "$*" = "git pull" ]; then
# 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
printf '%s' "$__git_result"
}
#
latest_commit () {
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)
if ! [ -z "$c" ]; then
if grep -qE "^[[:xdigit:]]{6,}$" <<< $c; then
@ -99,14 +81,14 @@ clone_customer_repository () {
cd -
fi
else
# the directory is not empty, so we assume the customer-repository
# has been cloned alread
# the directory is not empty, so we assume the
# customer-repository has been cloned already
if ! [[ -d ./${workspace_dir}/$customer_id ]]; then
local m="wrong repository: $(ls -d './${workspace_dir}/*')"
log_fatal "$func:${LINENO} $m"
log_fatal "$func:${LINENO} "\
"wrong repository: $(ls -d './${workspace_dir}/*')"
else
log_debug \
"$func:${LINENO} ./${workspace_dir}/$customer_id exists"
local __m= "./${workspace_dir}/$customer_id exists"
log_debug "$func:${LINENO} $__m"
return 0
fi
fi
@ -129,7 +111,7 @@ cd_customer_repository () {
fi
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
fi
@ -268,3 +250,4 @@ changed_file_names () {
"while in $PWD"
fi
}
# fi

View File

@ -1,83 +1,80 @@
#!/bin/bash
# set -x
#if [ ${log_helpers_sourced:-1} = "1" ]; then
# readonly log_helpers_sourced=${BASH_SOURCE[0]}
#else
# return 0
#fi
readonly log_file=/var/log/update_controller.log
if ! [ -f "$log_file" ]; then
touch $log_file
fi
if [ ${log_helpers_sourced:-1} = "1" ]; then # include ony once
readonly log_helpers_sourced=${BASH_SOURCE[0]}
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
readonly log_file=/var/log/update_controller.log
if ! [ -f "$log_file" ]; then
touch $log_file
fi
}
dbg_level () {
return $log_level
}
readonly DEBUG=0
readonly INFO=1
readonly WARN=2
readonly CRIT=3
readonly ERROR=4
readonly FATAL=5
readonly MAX_DEBUG_LEVEL=6
log() {
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_level=0
log_debug() {
if [ $log_level = $DEBUG ]; then
log "DEBUG $*"
fi
}
set_dbg_level () {
if [ $1 < $MAX_DEBUG_LEVEL ]; then
log_level=$1
fi
}
log_info() {
if [ $log_level -le $INFO ]; then
log "INFO $*"
fi
}
dbg_level () {
return $log_level
}
log_warn() {
if [ $log_level -le $WARN ]; then
log "WARN $*"
fi
}
log() {
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_crit() {
if [ $log_level -le $CRIT ]; then
log "CRIT $*"
fi
}
log_debug() {
if [ $log_level = $DEBUG ]; then
log "DEBUG $*"
fi
}
log_error() {
if [ $log_level -le $ERROR ]; then
log "ERROR $*"
fi
}
log_info() {
if [ $log_level -le $INFO ]; then
log "INFO $*"
fi
}
log_fatal() {
if [ $log_level -le $FATAL ]; then
log "FATAL $*"
log "exiting ..."
exit 1
fi
}
log_warn() {
if [ $log_level -le $WARN ]; then
log "WARN $*"
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
# set -x
#if [ ${read_config_sourced:-1} = "1" ]; then
# readonly read_config_sourced=${BASH_SOURCE[0]}
#else
# return 0
#fi
source ./log_helpers
# 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() {
local func="${FUNCNAME[0]}"
# read config file (JSON syntax)
#
read_config() {
local func="${FUNCNAME[0]}"
# check customer_id
__customer_id="$(cat "$1" | jq -r .customer_id)"
if [ -z "$__customer_id" ]; then
log_fatal "$func:${LINENO} customer_id not set in $1"
fi
readonly customer_id="customer_${__customer_id}"
log_info "$func:${LINENO}: customer-id is $customer_id"
# check customer_repository
__repository_path="$(cat "$1" | jq -r .repository_path)"
if [ -z "$__repository_path" ]; then
log_fatal "$func:${LINENO}: repository path not set in $1"
fi
readonly repository_path="${__repository_path}/${customer_id}.git"
log_info "$func:${LINENO}: repository path is $repository_path"
# check zone_group
local zone_group="$(cat "$1" | jq -r .zone_group)"
if [ -z "$zone_group" ]; then
log_fatal "$func:${LINENO}: zone_group not set in $1"
fi
# check zone
local zone="$(cat "$1" | jq -r .zone)"
if [ -z "$zone" ]; then
log_fatal "$func:${LINENO}: zone not set in $1"
fi
# check customer_location
local customer_location="$(cat "$1" | jq -r .customer_location)"
if [ -z "$customer_location" ]; then
log_fatal "$func:${LINENO}: customer_location not set in $1"
fi
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)"
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}"
log_info "$func:${LINENO}: customer-base-dir is $customer_base_dir"
# check customer_id
__customer_id="$(cat "$1" | jq -r .customer_id)"
if [ -z "$__customer_id" ]; then
log_fatal "$func:${LINENO} customer_id not set in $1"
fi
readonly customer_id="customer_${__customer_id}"
log_info "$func:${LINENO}: customer-id is $customer_id"
# check customer_repository
__repository_path="$(cat "$1" | jq -r .repository_path)"
if [ -z "$__repository_path" ]; then
log_fatal "$func:${LINENO}: repository path not set in $1"
fi
readonly repository_path="${__repository_path}/${customer_id}.git"
log_info "$func:${LINENO}: repository path is $repository_path"
# check zone_group
local zone_group="$(cat "$1" | jq -r .zone_group)"
if [ -z "$zone_group" ]; then
log_fatal "$func:${LINENO}: zone_group not set in $1"
fi
# check zone
local zone="$(cat "$1" | jq -r .zone)"
if [ -z "$zone" ]; then
log_fatal "$func:${LINENO}: zone not set in $1"
fi
# check customer_location
local customer_location=$(cat "$1" | jq -r .customer_location)
if [ -z "$customer_location" ]; then
log_fatal "$func:${LINENO}: customer_location not set in $1"
fi
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)"
readonly opkg_cmds_file="${customer_base_dir}/etc/psa_update/opkg_commands"
log_info "$func:${LINENO}: opkg_cmds_file is $opkg_cmds_file"
local __customer_base_dir="$working_directory/${workspace_dir}"
__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"
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"
# read_config UpdateController.conf
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
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
# TODO: implementieren, opkg pakete ausschliessen
files=$(changed_file_names)
if ! check_md5_for_changed_customer_files $files ; then
log_error "$func:${LINENO}: new customer files wrong"
@ -97,31 +99,40 @@ update() {
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_command in "${opkg_commands[@]}"; do
if grep -qE "^\s*[#]+.*?$" <<< $opkg_command; then
for opkg_c in "${opkg_commands[@]}"; do
if grep -qE "^\s*[#]+.*$" <<< $opkg_c; then
continue # found comment line
fi
# FIXME: sollte nicht gebraucht werden
opkg_command=${opkg_command//[$'\r\n\t']/ }
local package=$(printf '%s' "$opkg_command" | awk '{ print $NF }')
local opkg_output=()
if ! exec_opkg_info "$package" opkg_output; then
log_error "$func:${LINENO}: opkg --noaction $opkg_command failed"
revert_customer_repository ; exit 1
fi
# package manipulation commands without package:
local cwp="update|upgrade|clean"
# informational commands without package:
cwp="${cwp}|list|list-installed|list-upgradable"
if ! check_md5_for_opkg_packages opkg_output; then
log_error "$func:${LINENO}: wrong md5sum for some opkg packages"
revert_customer_repository ; exit 1
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_no_action $opkg_c; then
log_error "$func:${LINENO}: "\
"opkg --noaction $opkg_c failed"
fi
# Actually execute the opkg command
if ! exec_opkg $opkg_command; then
log_error "$func:${LINENO}: exec_opkg $opkg_command failed"
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

115
update_psa_helpers Normal file → Executable file
View File

@ -6,25 +6,19 @@
# return 0
#fi
source ./general_utils
exec_opkg_command () {
local func="${FUNCNAME[0]}"
log_debug "$func:${LINENO} exec-ing [$*]"
exec {fd}< <(eval "$@")
local ps_pid=$! # remember pid of process substitution
local __result=$(exec_process_substitution $*)
local __result_code=$?
local opkg_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)
wait $ps_pid # wait for the subshell to finish
log_debug "$func:${LINENO} result=$__result"
printf '%s' "$opkg_result"
printf '%s' "$__result"
return $__result_code
}
# Fetch/merge updates from predefined repository using git.
@ -55,23 +49,34 @@ check_md5_for_opkg_packages () {
local func="${FUNCNAME[0]}"
local -n opkg_output_ref=$1
local package=""
local md5sum=""
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#*:* }
printf 'package=%s\n' "$package"
elif grep -qE "^\s*MD5Sum\s*:.*?$" <<< "$line"; then
md5sum=${line#*:* }
printf 'md5sum=%s\n' "$md5sum"
md5sum_opkg_info=${line#*:* }
elif grep -qE "^\s*Filename\s*:.*?$" <<< "$line"; then
filename=${line#*:* }
printf 'filename=%s\n' "$filename"
fi
done
log_debug "$func:${LINENO}: $package | $md5sum | $filename"
return 0
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
@ -91,37 +96,42 @@ backup_previous_version () {
exec_opkg_info () {
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")
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
# 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
log_info "$func:${LINENO}: ... done"
# log_info "$func:${LINENO}: opkg_result=${output_ref[@]}"
return 0
fi
log_info "$func:${LINENO}: ... done"
log_info "$func:${LINENO}: opkg_result=${output_ref[@]}"
return 0
log_error "$func:${LINENO}: executing info $1"
return 1
}
# Try to install new opkg-packages (in case the are some
@ -138,11 +148,16 @@ exec_opkg_no_action() {
#
exec_opkg () {
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)