Skip to content
Snippets Groups Projects
Commit aa87098a authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

update docker init

parent 0b07d770
No related branches found
No related tags found
1 merge request!72OP#7651 CI Server: beide AWX ansteuerbar machen https://projects.iml.unibe.ch/work_packages/7651
...@@ -24,20 +24,25 @@ ...@@ -24,20 +24,25 @@
# 2024-07-29 v1.17 <www.axel-hahn.de> hide unnecessary menu items; reorder functions # 2024-07-29 v1.17 <www.axel-hahn.de> hide unnecessary menu items; reorder functions
# 2024-08-14 v1.18 <www.axel-hahn.de> update container view # 2024-08-14 v1.18 <www.axel-hahn.de> update container view
# 2024-09-20 v1.19 <www.axel-hahn.de> detect dockerd-rootless (hides menu item to set permissions) # 2024-09-20 v1.19 <www.axel-hahn.de> detect dockerd-rootless (hides menu item to set permissions)
# 2024-10-16 v1.20 <axel.hahn@unibe.ch> add db import and export
# 2024-10-25 v1.21 <axel.hahn@unibe.ch> create missing subdir dbdumps
# 2024-10-30 v1.22 <axel.hahn@unibe.ch> added: Open Mysql client in container
# 2024-10-30 v1.23 <axel.hahn@unibe.ch> added: show menu hints why some menu items are visible
# 2024-11-20 v1.24 <axel.hahn@unibe.ch> fix menu with started database less app; apply template permissions on target file; add $WEBURL; remove $frontendurl
# ====================================================================== # ======================================================================
cd "$( dirname "$0" )" || exit 1 cd "$( dirname "$0" )" || exit 1
# init used vars # init used vars
gittarget= gittarget=
frontendurl= WEBURL=
_self=$( basename "$0" ) _self=$( basename "$0" )
# shellcheck source=/dev/null # shellcheck source=/dev/null
. "${_self}.cfg" || exit 1 . "${_self}.cfg" || exit 1
_version="1.19" _version="1.24"
# git@git-repo.iml.unibe.ch:iml-open-source/docker-php-starterkit.git # git@git-repo.iml.unibe.ch:iml-open-source/docker-php-starterkit.git
selfgitrepo="docker-php-starterkit.git" selfgitrepo="docker-php-starterkit.git"
...@@ -61,8 +66,12 @@ DC_REPO=1 ...@@ -61,8 +66,12 @@ DC_REPO=1
DC_CONFIG_CHANGED=0 DC_CONFIG_CHANGED=0
# absolute urls for web app
DC_WEB_URL="" DC_WEB_URL=""
DC_DUMP_DIR=dbdumps
DC_SHOW_MENUHINTS=0
isDockerRootless=0 isDockerRootless=0
ps -ef | grep dockerd-rootless | grep -q $USER && isDockerRootless=1 ps -ef | grep dockerd-rootless | grep -q $USER && isDockerRootless=1
...@@ -70,6 +79,19 @@ ps -ef | grep dockerd-rootless | grep -q $USER && isDockerRootless=1 ...@@ -70,6 +79,19 @@ ps -ef | grep dockerd-rootless | grep -q $USER && isDockerRootless=1
# FUNCTIONS # FUNCTIONS
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# check config for changes in newer versions
function _checkConfig(){
# --- v1.24
if [ -z "$WEBURL" ]; then
echo -e "${fgBrown}INFO: add 'WEBURL=\"/\"' in your ${_self}.cfg. It is a new var since v1.24"
WEBURL="/"
fi
if [ -n "$frontendurl" ]; then
echo "INFO: Remove frontendurl=$frontendurl in your ${_self}.cfg. It is obsolete since v1.24"
fi
}
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# STATUS FUNCTIONS # STATUS FUNCTIONS
...@@ -99,11 +121,26 @@ function _getStatus_docker(){ ...@@ -99,11 +121,26 @@ function _getStatus_docker(){
DC_DB_UP=0 DC_DB_UP=0
grep -q "${APP_NAME}-server" <<< "$_out" && DC_WEB_UP=1 grep -q "${APP_NAME}-server" <<< "$_out" && DC_WEB_UP=1
grep -q "${APP_NAME}-db" <<< "$_out" && DC_DB_UP=1 grep -q "${APP_NAME}-db" <<< "$_out" && DC_DB_UP=1
if [ "$DB_ADD" != "false" ] && [ ! -d "${DC_DUMP_DIR}" ]; then
echo "INFO: creating subdir ${DC_DUMP_DIR} to import/ export databases ..."
mkdir "${DC_DUMP_DIR}" || exit 1
return
fi
} }
# Get web url of the application
# It is for support of Nginx Docker Proxy
# https://github.com/axelhahn/nginx-docker-proxy
# It returns http://localhost:<port> or a https://<appname> plus $WEBURL
function _getWebUrl(){ function _getWebUrl(){
DC_WEB_URL="$frontendurl" if grep -q "^[0-9\.]* ${APP_NAME}-server" /etc/hosts; then
grep -q "${APP_NAME}-server" /etc/hosts && DC_WEB_URL="https://${APP_NAME}-server/" DC_WEB_URL="https://${APP_NAME}-server$WEBURL"
else
DC_WEB_URL=http://localhost:${APP_PORT}$WEBURL
fi
set +vx
} }
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
...@@ -123,7 +160,16 @@ function h3(){ ...@@ -123,7 +160,16 @@ function h3(){
# helper for menu: print an inverted key # helper for menu: print an inverted key
function _key(){ function _key(){
echo -en "\e[4;7m ${1} \e[0m" echo -en "$fgInvert ${1} $fgReset"
}
# helper for menu: show hint text
# param int FLag _bAll (i true the txt will be hidden)
# param string message to show
function menuhint(){
local _bAll="$1"
shift 1
test $DC_SHOW_MENUHINTS -ne 0 && test "$_bAll" -eq "0" && ( echo -e "$fgBlue $*$fgReset" )
} }
# show menu in interactive mode and list keys in help with param -h # show menu in interactive mode and list keys in help with param -h
...@@ -137,38 +183,63 @@ function showMenu(){ ...@@ -137,38 +183,63 @@ function showMenu(){
echo echo
if [ $DC_REPO -eq 1 ] || [ $_bAll -eq 1 ]; then if [ $DC_REPO -eq 1 ] || [ $_bAll -eq 1 ]; then
menuhint $_bAll "Git data of starterkit were found"
echo "${_spacer}$( _key g ) - remove git data of starterkit" echo "${_spacer}$( _key g ) - remove git data of starterkit"
echo echo
fi fi
if [ $isDockerRootless -eq 1 ] || [ $_bAll -eq 1 ]; then if [ $isDockerRootless -eq 1 ] || [ $_bAll -eq 1 ]; then
menuhint $_bAll "Because rootless docker was found"
echo "${_spacer}$( _key i ) - init application: set permissions" echo "${_spacer}$( _key i ) - init application: set permissions"
echo
fi fi
if [ $DC_CONFIG_CHANGED -eq 1 ] || [ $_bAll -eq 1 ]; then if [ $DC_CONFIG_CHANGED -eq 1 ] || [ $_bAll -eq 1 ]; then
menuhint $_bAll "Config was changed"
echo "${_spacer}$( _key t ) - generate files from templates" echo "${_spacer}$( _key t ) - generate files from templates"
echo
fi fi
if [ $DC_CONFIG_CHANGED -eq 0 ] || [ $_bAll -eq 1 ]; then if [ $DC_CONFIG_CHANGED -eq 0 ] || [ $_bAll -eq 1 ]; then
menuhint $_bAll "Config is unchanged"
echo "${_spacer}$( _key T ) - remove generated files" echo "${_spacer}$( _key T ) - remove generated files"
echo
fi fi
echo if [ $DC_WEB_UP -eq 0 ] \
if [ $DC_WEB_UP -eq 0 ] || [ $_bAll -eq 1 ]; then || [ $DC_DB_UP -eq 0 ] && [ ! "$DB_ADD" = "false" ] \
|| [ $_bAll -eq 1 \
]; then
if [ $DC_CONFIG_CHANGED -eq 0 ] || [ $_bAll -eq 1 ]; then if [ $DC_CONFIG_CHANGED -eq 0 ] || [ $_bAll -eq 1 ]; then
menuhint $_bAll "A container is down and config is unchanged"
echo "${_spacer}$( _key u ) - startup containers docker-compose ... up -d" echo "${_spacer}$( _key u ) - startup containers docker-compose ... up -d"
echo "${_spacer}$( _key U ) - startup containers docker-compose ... up -d --build" echo "${_spacer}$( _key U ) - startup containers docker-compose ... up -d --build"
echo echo
echo "${_spacer}$( _key r ) - remove containers docker-compose rm -f" echo "${_spacer}$( _key r ) - remove containers docker-compose rm -f"
echo
fi fi
fi fi
if [ $DC_WEB_UP -eq 1 ] || [ $_bAll -eq 1 ]; then if [ $DC_WEB_UP -eq 1 ] || [ $DC_DB_UP -eq 1 ] || [ $_bAll -eq 1 ]; then
menuhint $_bAll "A container is up"
echo "${_spacer}$( _key s ) - shutdown containers docker-compose stop" echo "${_spacer}$( _key s ) - shutdown containers docker-compose stop"
echo echo
echo "${_spacer}$( _key m ) - more infos" echo "${_spacer}$( _key m ) - more infos"
echo "${_spacer}$( _key o ) - open app [${APP_NAME}] $DC_WEB_URL" echo "${_spacer}$( _key o ) - open app [${APP_NAME}] $DC_WEB_URL"
echo "${_spacer}$( _key c ) - console (bash)" echo "${_spacer}$( _key c ) - console (bash)"
echo
fi
if [ $DC_WEB_UP -eq 1 ] || [ $_bAll -eq 1 ]; then
menuhint $_bAll "Web container is up"
echo "${_spacer}$( _key p ) - console check with php linter" echo "${_spacer}$( _key p ) - console check with php linter"
echo
fi fi
echo if [ $DC_DB_UP -eq 1 ] || [ $_bAll -eq 1 ]; then
echo
menuhint $_bAll "Database container is up"
echo "${_spacer}$( _key d ) - Dump container database"
echo "${_spacer}$( _key D ) - Import Dump into container database"
echo "${_spacer}$( _key M ) - Open Mysql client in database container"
echo
fi
menuhint $_bAll "Always available"
echo "${_spacer}$( _key q ) - quit" echo "${_spacer}$( _key q ) - quit"
} }
...@@ -313,7 +384,7 @@ function _fix_no-db(){ ...@@ -313,7 +384,7 @@ function _fix_no-db(){
local iStart; typeset -i iStart local iStart; typeset -i iStart
iStart=$( grep -Fn "$CUTTER_NO_DATABASE" "${_file}" | cut -f 1 -d ':' )-1 iStart=$( grep -Fn "$CUTTER_NO_DATABASE" "${_file}" | cut -f 1 -d ':' )-1
if [ $iStart -gt 0 ]; then if [ $iStart -gt 0 ]; then
sed -ni "1,${iStart}p" "${_file}" sed -n "$sed_no_backup" "1,${iStart}p" "${_file}"
fi fi
fi fi
} }
...@@ -375,14 +446,15 @@ function _generateFiles(){ ...@@ -375,14 +446,15 @@ function _generateFiles(){
# write file from line 2 to a tmp file # write file from line 2 to a tmp file
sed -n '2,$p' "$mytpl" >"$_tmpfile" sed -n '2,$p' "$mytpl" >"$_tmpfile"
chmod "$( stat -c %a "$mytpl" )" "$_tmpfile"
# add generator # add generator
# sed -i "s#{{generator}}#generated by $0 - template: $mytpl - $( date )#g" $_tmpfile # sed -i "s#{{generator}}#generated by $0 - template: $mytpl - $( date )#g" $_tmpfile
local _md5; _md5=$( md5sum $_tmpfile | awk '{ print $1 }' ) local _md5; _md5=$( md5sum $_tmpfile | awk '{ print $1 }' )
sed -i "s#{{generator}}#GENERATED BY $_self - template: $mytpl - $_md5#g" $_tmpfile sed -i "$sed_no_backup" "s#{{generator}}#GENERATED BY $_self - template: $mytpl - $_md5#g" $_tmpfile
# apply all replacements to the tmp file # apply all replacements to the tmp file
eval sed -i "$params" "$_tmpfile" || exit eval sed "$sed_no_backup" "$params" "$_tmpfile" || exit
_fix_no-db $_tmpfile _fix_no-db $_tmpfile
...@@ -395,7 +467,7 @@ function _generateFiles(){ ...@@ -395,7 +467,7 @@ function _generateFiles(){
echo -n "$mytpl - changes detected - writing [$target] ... " echo -n "$mytpl - changes detected - writing [$target] ... "
mkdir -p "$( dirname ../"$target" )" || exit 2 mkdir -p "$( dirname ../"$target" )" || exit 2
mv "$_tmpfile" "../$target" || exit 2 mv "$_tmpfile" "../$target" || exit 2
echo OK echo -e "${fgGreen}OK${fgReset}"
echo echo
fi fi
else else
...@@ -424,7 +496,7 @@ function _removeGeneratedFiles(){ ...@@ -424,7 +496,7 @@ function _removeGeneratedFiles(){
echo -n "REMOVING " echo -n "REMOVING "
ls -l "../$target" || exit 2 ls -l "../$target" || exit 2
rm -f "../$target" || exit 2 rm -f "../$target" || exit 2
echo OK echo -e "${fgGreen}OK${fgReset}"
else else
echo "SKIP: $target" echo "SKIP: $target"
fi fi
...@@ -498,10 +570,94 @@ function _wait(){ ...@@ -498,10 +570,94 @@ function _wait(){
echo echo
} }
# DB TOOL - dump db from container
function _dbDump(){
local _iKeepDumps;
typeset -i _iKeepDumps=5
local _iStart;
typeset -i _iStart=$_iKeepDumps+1;
if [ $DC_DB_UP -eq 0 ]; then
echo "Database container is not running. Aborting."
return
fi
outfile=${DC_DUMP_DIR}/${MYSQL_DB}_$( date +%Y%m%d_%H%M%S ).sql
echo -n "dumping ${MYSQL_DB} ... "
if docker exec -i "${APP_NAME}-db" mysqldump -uroot -p${MYSQL_ROOT_PASS} ${MYSQL_DB} > "$outfile"; then
echo -n "OK ... Gzip ... "
if gzip "${outfile}"; then
echo "OK"
ls -l "$outfile.gz"
# CLEANUP
echo
echo "--- Cleanup: keep $_iKeepDumps files."
ls -1t ${DC_DUMP_DIR}/* | sed -n "$_iStart,\$p" | while read -r delfile
do
echo "CLEANUP: Deleting $delfile ... "
rm -f "$delfile"
done
echo
echo -n "Size of dump directory: "
du -hs ${DC_DUMP_DIR} | awk '{ print $1 }'
else
echo "ERROR"
rm -f "$outfile"
fi
else
echo "ERROR"
rm -f "$outfile"
fi
}
# DB TOOL - import local database dump into container
function _dbImport(){
echo "--- Available dumps:"
ls -ltr ${DC_DUMP_DIR}/*.gz | sed "s#^# #g"
if [ $DC_DB_UP -eq 0 ]; then
echo "Database container is not running. Aborting."
return
fi
echo -n "Dump file to import into ${MYSQL_DB} > "
read -r dumpfile
if [ -z "$dumpfile" ]; then
echo "Abort - no value was given."
return
fi
if [ ! -f "$dumpfile" ]; then
echo "Abort - wrong filename."
return
fi
echo -n "Importing $dumpfile ... "
# Mac OS compatibility
# if zcat "$dumpfile" | docker exec -i "${APP_NAME}-db" mysql -uroot -p${MYSQL_ROOT_PASS} "${MYSQL_DB}"
if cat "$dumpfile" | zcat | docker exec -i "${APP_NAME}-db" mysql -uroot -p${MYSQL_ROOT_PASS} "${MYSQL_DB}"
then
echo "OK"
else
echo "ERROR"
fi
}
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# MAIN # MAIN
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
_checkConfig
# Mac OS compatibility
case "$OSTYPE" in
darwin*|bsd*)
sed_no_backup=" -i '' "
;;
*)
sed_no_backup="-i"
;;
esac
action=$1; shift 1 action=$1; shift 1
while true; do while true; do
...@@ -612,6 +768,18 @@ while true; do ...@@ -612,6 +768,18 @@ while true; do
echo "Start your docker container first." echo "Start your docker container first."
fi fi
;; ;;
d)
h2 "DB tools :: dump"
_dbDump
;;
D)
h2 "DB tools :: import"
_dbImport
;;
M)
h2 "DB tools :: mysql client"
docker exec -it "${APP_NAME}-db" mysql -uroot -p${MYSQL_ROOT_PASS} "${MYSQL_DB}"
;;
o) o)
h2 "Open app ..." h2 "Open app ..."
xdg-open "$DC_WEB_URL" xdg-open "$DC_WEB_URL"
......
...@@ -69,6 +69,6 @@ DOCKER_USER_UID=33 ...@@ -69,6 +69,6 @@ DOCKER_USER_UID=33
CUTTER_NO_DATABASE="CUT-HERE-FOR-NO-DATABASE" CUTTER_NO_DATABASE="CUT-HERE-FOR-NO-DATABASE"
frontendurl=http://localhost:${APP_PORT}/ WEBURL=/
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment