19 Commits

Author SHA1 Message Date
96d76b856a Added MAINTENANCE flag (-m). If set, simulate WAIT-button in ISMAS 2023-05-24 16:47:54 +02:00
9eff70b780 fixed typo 2023-05-24 16:46:52 +02:00
2a8be547a8 Added FATAL_FAILURE to log_helpers. Do not send to APIS in case of FATAL_FAILURE. 2023-05-24 16:46:21 +02:00
17521771e3 Use log_info instead of log_debug for some debug messages 2023-05-24 16:45:01 +02:00
515c77bed4 Use GIT_UPDATE_LOG instaed of GIT_PULL_TMP / OPKG_CMDS_TMP. 2023-05-24 13:06:25 +02:00
f2617d8151 Use GIT_UPDATE_LOG.
pull_customer_repository():
    Check the file GIT_UPDATE_LOG for keywords DOWNLOAD or EXECUTE.
2023-05-24 13:04:39 +02:00
4f9c6155e1 Always log to file. Log to terminal depending on debug level. 2023-05-24 13:02:56 +02:00
33511a5bcb simulate WAIT-button 2023-04-25 14:26:36 +02:00
bd0d07f56f Added git_branch() and git_customer_branch() 2022-07-08 17:23:03 +02:00
b8bc61daea Set log_level to INFO 2022-07-08 13:50:16 +02:00
27cd99f96a Add update_psa with parameter to skip WAIT-test 2022-07-08 13:21:47 +02:00
729778043a Removed syntactic errors. 2022-06-25 21:27:14 +02:00
31e1aad762 Removed the copy operation for the githooks aunder .githooks. 2022-06-25 21:26:20 +02:00
f8c091ad0c Add get_blob and get_commit_for_blob 2022-06-25 17:18:13 +02:00
e5e6c4c1cd Minor change: check_for_running_apism 2022-06-25 15:22:34 +02:00
95a9307631 check_for_apism, check_default_route and check_for_route_to_repository
exit the script if condition is not fulfilled.
2022-06-25 15:19:43 +02:00
fc1c2221b1 Added check_default_route and check_route_for_repository. 2022-06-25 15:11:49 +02:00
9b1549306d Added checks:
check_default_route -> exit if no route set
check_route_to_repository -> exit if no such route
check_for_apism
2022-06-25 15:08:44 +02:00
a8f691741b Added APISM running 2022-06-25 15:07:23 +02:00
6 changed files with 140 additions and 71 deletions

View File

@@ -57,6 +57,18 @@ revert_to_commit_before_pull () {
} }
# revert_to_commit_before_pull # revert_to_commit_before_pull
git_branch () {
# always relative to a git repository
echo $(git branch -a | grep '*' | awk '{ print $2 }')
}
git_customer_branch () {
if cd_customer_repository; then
customer_branch=$(git_branch)
cd_home;
echo $customer_branch
fi
}
# clone the customer repository in ./workspace. # clone the customer repository in ./workspace.
# this is done only once. # this is done only once.
# #
@@ -78,18 +90,17 @@ clone_customer_repository () {
log_debug "$func:${LINENO} cloning ${1} ..." log_debug "$func:${LINENO} cloning ${1} ..."
if cd "./$WORKSPACE_DIR" if cd "./$WORKSPACE_DIR"
then then
$(exec_git_command git clone "$1") exec_git_command git clone "$1"
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
log_debug "$func:${LINENO} cloning ${1} done" log_debug "$func:${LINENO} cloning ${1} done"
GIT_CLONE_EXECUTED=1 GIT_CLONE_EXECUTED=1
rm -f $GIT_PULL_TMP
rm -f $OPKG_CMDS_TMP
# after cloning, cd into repository, and re-initialize, # after cloning, cd into repository, and re-initialize,
# setting the work-tree as "/". This has the effect that # setting the work-tree as "/". This has the effect that
# a "git pull" will automatically fetched files in the # a "git pull" will automatically fetched files in the
# corresponding sytem-folders. # corresponding sytem-folders.
if cd ${CUSTOMER_ID_BASE_DIR}; then if cd ${CUSTOMER_ID_BASE_DIR}; then
# local res=$(exec_git_command git --git-dir=.git --work-tree=/ init) # configure hooks directory
exec_git_command git config core.hooksPath .githooks
exec_git_command git checkout "$LOCAL_BRANCH" exec_git_command git checkout "$LOCAL_BRANCH"
if [[ $? -eq 0 ]]; then if [[ $? -eq 0 ]]; then
log_debug "$func:${LINENO} checked out local branch $LOCAL_BRANCH" log_debug "$func:${LINENO} checked out local branch $LOCAL_BRANCH"
@@ -102,19 +113,13 @@ clone_customer_repository () {
exec_git_command git reset --hard "$LOCAL_BRANCH" exec_git_command git reset --hard "$LOCAL_BRANCH"
if [[ $? -eq 0 ]]; then if [[ $? -eq 0 ]]; then
log_debug "$func:${LINENO} reset --hard $LOCAL_BRANCH" log_debug "$func:${LINENO} reset --hard $LOCAL_BRANCH"
# re-initialized. copy post-merge (a hook called CLONE_CUSTOMER_REPOSITORY=true
# when 'git pull' is executed and changed data log_debug "$func:${LINENO} re-init of ${1} done"
# are received). cd_home; return 0
if cp ".githooks/post-merge" ".git/hooks"; then fi
log_debug "$func:${LINENO} copied post-merge to .git/hooks" fi
CLONE_CUSTOMER_REPOSITORY=true
log_debug "$func:${LINENO} re-init of ${1} done"
cd_home; return 0
fi
fi
fi
fi fi
fi fi
fi fi
fi fi
cd_home; return 1 cd_home; return 1
@@ -175,26 +180,20 @@ pull_customer_repository () {
return 1 return 1
fi fi
#local commit_before_pull=$(latest_commit) log_crit "$func:${LINENO}: executing 'git pull'..."
#if [ -z $commit_before_pull ]; then
# log_warn "$func:${LINENO}: commit_before_pull empty"
# cd_home ; return 1
#fi
#log_debug "$func:${LINENO}: commit_before_pull=$commit_before_pull"
rm -f "$OPKG_CMDS_TMP"
rm -f "$GIT_PULL_TMP"
log_debug "$func:${LINENO}: executing 'git pull'..."
exec_git_command 'git pull' exec_git_command 'git pull'
# GIT_PULL_TMP created by hook post-merge. it contains the names of the # GIT_UPDATE_LOG updated by hook post-merge. it contains the names of the
# changed files. # changed files or opkg-commands to execute.
if [[ -f $GIT_PULL_TMP ]]; then # If 'git pull' has fetched new data, then there are lines starting with
# DOWNLOAD or EXECUTE.
grep -E -q "^[[:space:]]*(DOWNLOAD|EXECUTE)" "$GIT_UPDATE_LOG"
if [ $? -eq 0 ]; then
log_info "$func:${LINENO}: new data fetched from repository"
cd_home; return 0 cd_home; return 0
fi fi
log_warn "$func:${LINENO}: no data fetched form repository" log_warn "$func:${LINENO}: no data fetched from repository"
cd_home; return 1 cd_home; return 1
} }
# pull_customer_repository customer_281 # pull_customer_repository customer_281
@@ -224,4 +223,20 @@ changed_file_names () {
"while in $PWD" "while in $PWD"
fi fi
} }
# fi
get_blob () { # get the blob of the file(name) passed as $1
# note: this can be used for any file in the filesystem
echo $(git hash-object $1)
}
get_commit_for_blob () {
blob=$(get_blob $1)
if [ ! -z $blob ]; then
# search for the blob in all commits for the file(name) $1
echo $(echo $(git log --all --pretty=format:%H -- $1) |
xargs -I{} sh -c "git ls-tree {} -- $1 |
grep -q $blob && echo {}")
fi
}

View File

@@ -1,10 +1,6 @@
#!/bin/bash #!/bin/bash
# set -x # set -x
RED='\e[0;31m'
GREEN='\e[0;32m'
NC='\e[0m' # No Color
if [ "${log_helpers_sourced:-1}" = "1" ]; then # include only once if [ "${log_helpers_sourced:-1}" = "1" ]; then # include only once
readonly log_helpers_sourced=${BASH_SOURCE[0]} readonly log_helpers_sourced=${BASH_SOURCE[0]}
@@ -21,7 +17,8 @@ if [ "${log_helpers_sourced:-1}" = "1" ]; then # include only once
readonly FATAL=5 readonly FATAL=5
readonly MAX_DEBUG_LEVEL=6 readonly MAX_DEBUG_LEVEL=6
log_level=0 log_level=$INFO
FATAL_FAILURE=false
set_dbg_level () { set_dbg_level () {
if [ $1 < $MAX_DEBUG_LEVEL ]; then if [ $1 < $MAX_DEBUG_LEVEL ]; then
@@ -33,50 +30,61 @@ if [ "${log_helpers_sourced:-1}" = "1" ]; then # include only once
return $log_level return $log_level
} }
log() { log_to_file() {
if [[ $(("$(wc -l < $log_file)")) -ge $((100000)) ]]; then if [[ $(("$(wc -l < $log_file)")) -ge $((100000)) ]]; then
# remove first line # remove first line
sed -e 1d -i $log_file sed -e 1d -i $log_file
fi fi
local msg="$(date +'%Y-%m-%d_%T'): $*" local msg="$(date +'%Y-%m-%d_%T'): $*"
echo "log:$msg" >&2
echo "$msg" >> $log_file echo "$msg" >> $log_file
} }
log() {
local msg="$(date +'%Y-%m-%d_%T'): $*"
echo "log:$msg" >&2
}
log_debug() { log_debug() {
if [ $log_level = $DEBUG ]; then log_to_file "DEBUG $*"
if [ $log_level -le $DEBUG ]; then
log "DEBUG $*" log "DEBUG $*"
fi fi
} }
log_info() { log_info() {
log_to_file "INFO $*"
if [ $log_level -le $INFO ]; then if [ $log_level -le $INFO ]; then
log "${GREEN}INFO $*" log "INFO $*"
fi fi
} }
log_warn() { log_warn() {
log_to_file "WARN $*"
if [ $log_level -le $WARN ]; then if [ $log_level -le $WARN ]; then
log "${RED}WARN $*" log "WARN $*"
fi fi
} }
log_crit() { log_crit() {
log_to_file "CRIT $*"
if [ $log_level -le $CRIT ]; then if [ $log_level -le $CRIT ]; then
log "${RED}CRIT $*" log "CRIT $*"
fi fi
} }
log_error() { log_error() {
log_to_file "ERROR $*"
if [ $log_level -le $ERROR ]; then if [ $log_level -le $ERROR ]; then
log "${RED}ERROR $*" log "ERROR $*"
fi fi
} }
log_fatal() { log_fatal() {
log_to_file "FATAL $*"
if [ $log_level -le $FATAL ]; then if [ $log_level -le $FATAL ]; then
log "${RED}FATAL $*" log "FATAL $*"
log "${RED}exiting ..." log "exiting ..."
FATAL_FAILURE=true
exit 1 exit 1
fi fi
} }

View File

@@ -22,17 +22,19 @@ if [ ${read_config_sourced:-1} = "1" ]; then # include only once
readonly UPDATEPSABEGIN=./.updatepsa/begin readonly UPDATEPSABEGIN=./.updatepsa/begin
readonly UPDATEPSAEND=./.updatepsa/end readonly UPDATEPSAEND=./.updatepsa/end
readonly OPKG_CMDS_TMP=/tmp/opkg_commands # name of file containing the logs following a 'git pull':
readonly GIT_PULL_TMP=/tmp/git_pull # basically, there are two kinds of entries:
readonly GIT_UPDATE_LOG=/opt/app/tools/atbupdate/update_log.csv
readonly PROGRAM=`basename $0` readonly PROGRAM=`basename $0`
readonly WORKSPACE_DIR=workspace readonly WORKSPACE_DIR=workspace
GIT_CLONE_EXECUTED=0
EXITCODE=$RC_SUCCESS EXITCODE=$RC_SUCCESS
CLONE_CUSTOMER_REPOSITORY=false CLONE_CUSTOMER_REPOSITORY=false
GIT_CLONE_EXECUTED=0
APISM_RUNNING=0
# read config file (JSON syntax) # read config file (JSON syntax)
read_config() { read_config() {
local func="${FUNCNAME[0]}" local func="${FUNCNAME[0]}"

View File

@@ -5,6 +5,7 @@
# UPDATE PSA # UPDATE PSA
# Usage: # Usage:
# update_psa [ --? ] # update_psa [ --? ]
# [ -m ]
# [ --help ] # [ --help ]
# [ --wdir "working_directory" ] # [ --wdir "working_directory" ]
# [ --file "config_file" ] # [ --file "config_file" ]
@@ -23,9 +24,15 @@ if [ -z $IFS ]; then
IFS=$'\n' IFS=$'\n'
fi fi
MAINTENANCE="NO"
# parse commandline parameters # parse commandline parameters
while test $# -gt 0; do while test $# -gt 0; do
case $1 in case $1 in
--maintenance | -m )
shift
MAINTENANCE="YES"
;;
--file | --fil | --fi | --f | -file | -fil | -fi | -f ) --file | --fil | --fi | --f | -file | -fil | -fi | -f )
shift shift
CONFIGFILENAME="$1" CONFIGFILENAME="$1"
@@ -47,6 +54,7 @@ while test $# -gt 0; do
exit 0 exit 0
;; ;;
--dbg | --db | --d | -dbg | -db | -d ) --dbg | --db | --d | -dbg | -db | -d )
shift
set_dbg_level $DEBUG set_dbg_level $DEBUG
;; ;;
-*) -*)
@@ -64,18 +72,31 @@ if cd "$WORKING_DIRECTORY"; then
if read_config if read_config
then then
check_default_route
check_route_to_repository "185.191.219.134" # git.mimbach.de
check_for_running_apism
if clone_customer_repository ${CUSTOMER_REPOSITORY_PATH} if clone_customer_repository ${CUSTOMER_REPOSITORY_PATH}
then then
check_sanity_of_repository check_sanity_of_repository
#set_updates_available if [ "$MAINTENANCE" = "NO" ]; then
#while : log_info "update_psa:${LINENO}: MAINTENANCE OFF. Checking for updates..."
#do # someone must have activated the WAIT-button in ISMAS.
# sleep 5 # Request the status of this button in updates_available().
# updates_available && break if ! updates_available; then
#done update_psa_false_alarm "update requested, but no WAIT state detected !!!"
log_fatal "update_psa:${LINENO}: update requested, but no WAIT state detected !!!"
# update_psa "testing" fi
else
# simulate an activated WAIT-button
set_updates_available
while :
do
sleep 5
updates_available && break
done
fi
# updates available
update_psa update_psa
fi fi
fi fi

View File

@@ -373,8 +373,37 @@ cleanup_previous_version() {
return 0 return 0
} }
check_for_apism () { check_for_running_apism () {
nc localhost 7778 local func="${FUNCNAME[0]}"
if nc localhost 7778
then
APISM_RUNNING=1
log_info "$func:${LINENO}: APISM is running..."
return 0
fi
log_fatal "$func:${LINENO}: !!! APISM NOT RUNNING !!!"
}
check_default_route () {
local func="${FUNCNAME[0]}"
if ip route | head -n 1 | grep -q '^default'
then
log_info "$func:${LINENO}: default route set"
return 0
fi
log_fatal "$func:${LINENO}: !!! NO DEFAULT ROUTE SET !!!"
}
check_route_to_repository () {
local func="${FUNCNAME[0]}"
# ip_address=$1, e.g. git.mimbach.de (185.191.219.134)
# 185.191.219.134 via 192.168.5.254 ...
if test "$#" -eq 1 && ip route get "$1" | head -n 1 | grep -q "^$1"
then
log_info "$func:${LINENO}: route to repository available"
return 0
fi
log_fatal "$func:${LINENO}: !!! NO ROUTE TO REPO SET !!!"
} }
get_customer_id () { get_customer_id () {

View File

@@ -13,6 +13,8 @@ trap collect_current_configuration EXIT
collect_current_configuration () { collect_current_configuration () {
local func="${FUNCNAME[0]}" local func="${FUNCNAME[0]}"
$FATAL_FAILURE && return 1
# TODO: eventuell muss die version neu berechnet werden # TODO: eventuell muss die version neu berechnet werden
current_settings_to_ismas current_settings_to_ismas
@@ -28,15 +30,7 @@ collect_current_configuration () {
#done #done
} }
update_psa() { # calling with a parameter is used for testing update_psa() {
if [ $# -eq 0 ]; then
if ! updates_available; then # check for false alarm
update_psa_false_alarm "update requested, but no WAIT state detected"
exit $EXITCODE
fi
fi
update_psa_activated # message to ISMAS update_psa_activated # message to ISMAS
local func="${FUNCNAME[0]}" local func="${FUNCNAME[0]}"