From ddd6ba58931dc7e9f8170257c3820f5f7c17f9ed Mon Sep 17 00:00:00 2001 From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch> Date: Mon, 22 Jul 2024 12:01:41 +0200 Subject: [PATCH] show info if there is no database container; speedup replacements --- docker/init.sh | 60 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/docker/init.sh b/docker/init.sh index 4e2118a..3ff7f15 100755 --- a/docker/init.sh +++ b/docker/init.sh @@ -17,6 +17,7 @@ # 2023-12-07 v1.10 <www.axel-hahn.de> simplyfy console command; add php linter # 2024-07-01 v1.11 <www.axel-hahn.de> diff with colored output; suppress errors on port check # 2024-07-19 v1.12 <axel.hahn@unibe.ch> apply shell fixes +# 2024-07-22 v1.13 <axel.hahn@unibe.ch> show info if there is no database container; speedup replacements # ====================================================================== cd "$( dirname "$0" )" || exit 1 @@ -33,7 +34,7 @@ _self=$( basename "$0" ) # git@git-repo.iml.unibe.ch:iml-open-source/docker-php-starterkit.git selfgitrepo="docker-php-starterkit.git" -_version="1.12" +_version="1.13" # ---------------------------------------------------------------------- # FUNCTIONS @@ -169,6 +170,23 @@ function _fix_no-db(){ fi } +# helper functiion to generate replacements using sed +# it loops over all vars in the config file +# used in _generateFiles +function _getreplaces(){ + # loop over vars to make the replacement + grep "^[a-zA-Z]" "$_self.cfg" | while read -r line + do + # echo replacement: $line + mykey=$( echo "$line" | cut -f 1 -d '=' ) + myvalue="$( eval echo \"\$"$mykey"\" )" + + # TODO: multiline values fail here in replacement with sed + echo -e "s#{{$mykey}}#${myvalue}#g" + + done +} + # loop over all files in templates subdir make replacements and generate # a target file. # It skips if @@ -176,14 +194,14 @@ function _fix_no-db(){ # - target file has no updated lines function _generateFiles(){ - # re-read config vars - # shellcheck source=/dev/null . "${_self}.cfg" || exit 1 + params=$( _getreplaces | while read -r line; do echo -n "-e '$line' "; done ) + local _tmpfile=/tmp/newfilecontent$$.tmp h2 "generate files from templates..." - for mytpl in templates/* + time for mytpl in templates/* do # h3 $mytpl local _doReplace=1 @@ -207,19 +225,7 @@ function _generateFiles(){ local _md5; _md5=$( md5sum $_tmpfile | awk '{ print $1 }' ) sed -i "s#{{generator}}#GENERATED BY $_self - template: $mytpl - $_md5#g" $_tmpfile - # loop over vars to make the replacement - grep "^[a-zA-Z]" "$_self.cfg" | while read -r line - do - # echo replacement: $line - mykey=$( echo "$line" | cut -f 1 -d '=' ) - myvalue="$( eval echo \"\$"$mykey"\" )" - - if grep "{{$mykey}}" $_tmpfile >/dev/null; then - - # TODO: multiline values fail here in replacement with sed - sed -i -e "s#{{$mykey}}#${myvalue}#g" $_tmpfile - fi - done + eval sed -i "$params" "$_tmpfile" || exit _fix_no-db $_tmpfile @@ -229,12 +235,12 @@ function _generateFiles(){ mkdir -p "$( dirname ../"$target" )" || exit 2 mv "$_tmpfile" "../$target" || exit 2 echo OK + echo else rm -f $_tmpfile echo "SKIP: $mytpl - Nothing to do." fi fi - echo done } @@ -265,12 +271,26 @@ function _removeGeneratedFiles(){ # show running containers function _showContainers(){ local bLong=$1 + local _out h2 CONTAINERS - if [ -z "$bLong" ]; then + _out=$( if [ -z "$bLong" ]; then docker-compose -p "$APP_NAME" ps else docker ps | grep "$APP_NAME" + fi) + if [ "$( wc -l <<< "$_out" )" -eq 1 ]; then + if [ "$DB_ADD" = "false" ]; then + echo "The web container is <$APP_NAME> is not running. This app has no database container." + else + echo "No container is running for <$APP_NAME>." + fi + else + echo "$_out" + if [ "$DB_ADD" = "false" ]; then + echo "INFO: This app has no database container." + fi fi + } @@ -340,7 +360,7 @@ while true; do if [ -z "$action" ]; then echo - echo -e "\e[32m===== INITIALIZER FOR DOCKER APP [$APP_NAME] v$_version ===== \e[0m\n\r" + echo -e "\e[32m----===###| INITIALIZER FOR DOCKER v$_version | $APP_NAME |###===---\e[0m" _showContainers -- GitLab