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

update docker init script

parent ee2d3b8f
Branches
No related tags found
1 merge request!66php8 only; added variable types; short array syntax; remove glyphicons
...@@ -20,6 +20,9 @@ ...@@ -20,6 +20,9 @@
# 2024-07-22 v1.13 <axel.hahn@unibe.ch> show info if there is no database container; speedup replacements # 2024-07-22 v1.13 <axel.hahn@unibe.ch> show info if there is no database container; speedup replacements
# 2024-07-22 v1.14 <axel.hahn@unibe.ch> show colored boxes with container status # 2024-07-22 v1.14 <axel.hahn@unibe.ch> show colored boxes with container status
# 2024-07-24 v1.15 <axel.hahn@unibe.ch> update menu output # 2024-07-24 v1.15 <axel.hahn@unibe.ch> update menu output
# 2024-07-26 v1.16 <axel.hahn@unibe.ch> hide unnecessary menu items (WIP)
# 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
# ====================================================================== # ======================================================================
cd "$( dirname "$0" )" || exit 1 cd "$( dirname "$0" )" || exit 1
...@@ -33,7 +36,7 @@ _self=$( basename "$0" ) ...@@ -33,7 +36,7 @@ _self=$( basename "$0" )
# shellcheck source=/dev/null # shellcheck source=/dev/null
. "${_self}.cfg" || exit 1 . "${_self}.cfg" || exit 1
_version="1.15" _version="1.18"
# 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"
...@@ -43,13 +46,65 @@ fgRed="\e[31m" ...@@ -43,13 +46,65 @@ fgRed="\e[31m"
fgGreen="\e[32m" fgGreen="\e[32m"
fgBrown="\e[33m" fgBrown="\e[33m"
fgBlue="\e[34m" fgBlue="\e[34m"
fgInvert="\e[7m"
fgReset="\e[0m" fgReset="\e[0m"
# ----- status varsiables
# running containers
DC_WEB_UP=0
DC_DB_UP=0
# repo of docker-php-starterkit is here?
DC_REPO=1
DC_CONFIG_CHANGED=0
DC_WEB_URL=""
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# FUNCTIONS # FUNCTIONS
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# STATUS FUNCTIONS
# get container status and set global variable DC_REPO
# DC_REPO = 0 nothing to do - repo was changed to project
# DC_REPO = 1 if repo is in selfgitrepo (must be deleted)
function _getStatus_repo(){
DC_REPO=0
git config --get remote.origin.url 2>/dev/null | grep -q $selfgitrepo && DC_REPO=1
}
# check if any of the templates has a change that must be applied
function _getStatus_template(){
_generateFiles "dryrun"
}
# get container status and set global variables
# DC_WEB_UP - web container
# DC_DB_UP - database container
# 0 = down
# 1 = up
function _getStatus_docker(){
local _out
_out=$( docker-compose -p "$APP_NAME" ps)
DC_WEB_UP=0
DC_DB_UP=0
grep -q "${APP_NAME}-server" <<< "$_out" && DC_WEB_UP=1
grep -q "${APP_NAME}-db" <<< "$_out" && DC_DB_UP=1
}
function _getWebUrl(){
DC_WEB_URL="$frontendurl"
grep -q "${APP_NAME}-server" /etc/hosts && DC_WEB_URL="https://${APP_NAME}-server/"
}
# ----------------------------------------------------------------------
# OUTPUT
# draw a headline 2 # draw a headline 2
function h2(){ function h2(){
echo echo
...@@ -62,44 +117,70 @@ function h3(){ ...@@ -62,44 +117,70 @@ function h3(){
echo -e "$fgBlue----- $*$fgReset" echo -e "$fgBlue----- $*$fgReset"
} }
# show help for param -h # helper for menu: print an inverted key
function _key(){
echo -en "\e[4;7m ${1} \e[0m"
}
# show menu in interactive mode and list keys in help with param -h
# param string optional: set to "all" to show all menu items
function showMenu(){ function showMenu(){
cat <<EOM
$( _key g ) - remove git data of starterkit local _bAll=0
test -n "$1" && _bAll=1
$( _key i ) - init application: set permissions local _spacer=" "
$( _key t ) - generate files from templates
$( _key T ) - remove generated files
$( _key u ) - startup containers docker-compose ... up -d echo
$( _key U ) - startup containers docker-compose ... up -d --build if [ $DC_REPO -eq 1 ] || [ $_bAll -eq 1 ]; then
$( _key s ) - shutdown containers docker-compose stop echo "${_spacer}$( _key g ) - remove git data of starterkit"
$( _key r ) - remove containers docker-compose rm -f echo
fi
echo "${_spacer}$( _key i ) - init application: set permissions"
$( _key m ) - more infos if [ $DC_CONFIG_CHANGED -eq 1 ] || [ $_bAll -eq 1 ]; then
$( _key o ) - open app [${APP_NAME}] $frontendurl echo "${_spacer}$( _key t ) - generate files from templates"
$( _key c ) - console (bash) fi
$( _key p ) - console check with php linter if [ $DC_CONFIG_CHANGED -eq 0 ] || [ $_bAll -eq 1 ]; then
echo "${_spacer}$( _key T ) - remove generated files"
fi
echo
if [ $DC_WEB_UP -eq 0 ] || [ $_bAll -eq 1 ]; then
if [ $DC_CONFIG_CHANGED -eq 0 ] || [ $_bAll -eq 1 ]; then
echo "${_spacer}$( _key u ) - startup containers docker-compose ... up -d"
echo "${_spacer}$( _key U ) - startup containers docker-compose ... up -d --build"
echo
echo "${_spacer}$( _key r ) - remove containers docker-compose rm -f"
fi
fi
if [ $DC_WEB_UP -eq 1 ] || [ $_bAll -eq 1 ]; then
echo "${_spacer}$( _key s ) - shutdown containers docker-compose stop"
echo
echo "${_spacer}$( _key m ) - more infos"
echo "${_spacer}$( _key o ) - open app [${APP_NAME}] $DC_WEB_URL"
echo "${_spacer}$( _key c ) - console (bash)"
echo "${_spacer}$( _key p ) - console check with php linter"
fi
echo
echo "${_spacer}$( _key q ) - quit"
$( _key q ) - quit
EOM
} }
function showHelp(){ function showHelp(){
cat <<EOH cat <<EOH
INITIALIZER FOR DOCKER APP v$_version INITIALIZER FOR DOCKER APP v$_version
A helper script written in Bash to bring up a PHP+Mysql application in docker. A helper script written in Bash to bring up a PHP+Mysql application in docker.
Source : https://git-repo.iml.unibe.ch/iml-open-source/docker-php-starterkit 📄 Source : https://git-repo.iml.unibe.ch/iml-open-source/docker-php-starterkit
Docs : https://os-docs.iml.unibe.ch/docker-php-starterkit/ 📗 Docs : https://os-docs.iml.unibe.ch/docker-php-starterkit/
License: GNU GPL 3.0 📜 License: GNU GPL 3.0
(c) Institute for Medical Education; University of Bern (c) Institute for Medical Education; University of Bern
SYNTAX: SYNTAX:
$_self [-h|-v] $_self [-h|-v]
$_self [menu key] $_self [menu key [.. menu key N]]
OPTIONS: OPTIONS:
-h show this help and exit -h show this help and exit
...@@ -110,7 +191,7 @@ MENU KEYS: ...@@ -110,7 +191,7 @@ MENU KEYS:
The same keys can be put as parameter to start this action. The same keys can be put as parameter to start this action.
You can add multiples keys to apply multiple actions. You can add multiples keys to apply multiple actions.
$( showMenu ) $( showMenu "all" )
EXAMPLES: EXAMPLES:
...@@ -122,6 +203,56 @@ EXAMPLES: ...@@ -122,6 +203,56 @@ EXAMPLES:
EOH EOH
} }
# show urls for app container
function _showBrowserurl(){
echo "In a web browser open:"
echo " $DC_WEB_URL"
}
# detect + show ports and urls for app container and db container
function _showInfos(){
_showContainers long
h2 INFO
h3 "processes webserver"
# docker-compose top
docker top "${APP_NAME}-server"
if [ ! "$DB_ADD" = "false" ]; then
h3 "processes database"
docker top "${APP_NAME}-db"
fi
h3 "What to open in browser"
if echo >"/dev/tcp/localhost/${APP_PORT}"; then
# echo "OK, app port ${APP_PORT} is reachable"
# echo
_showBrowserurl
else
echo "ERROR: app port ${APP_PORT} is not available"
fi 2>/dev/null
if [ "$DB_ADD" != "false" ]; then
h3 "Check database port"
if echo >"/dev/tcp/localhost/${DB_PORT}"; then
echo "OK, db port ${DB_PORT} is reachable"
echo
echo "In a local DB admin tool you can connect it:"
echo " host : localhost"
echo " port : ${DB_PORT}"
echo " user : root"
echo " password: ${MYSQL_ROOT_PASS}"
else
echo "NO, db port ${DB_PORT} is not available"
fi 2>/dev/null
fi
echo
}
# ----------------------------------------------------------------------
# ACTIONS
# set acl on local directory # set acl on local directory
function _setWritepermissions(){ function _setWritepermissions(){
h2 "set write permissions on ${gittarget} ..." h2 "set write permissions on ${gittarget} ..."
...@@ -156,7 +287,7 @@ function _removeGitdata(){ ...@@ -156,7 +287,7 @@ function _removeGitdata(){
h2 "Remove git data of starterkit" h2 "Remove git data of starterkit"
echo -n "Current git remote url: " echo -n "Current git remote url: "
git config --get remote.origin.url git config --get remote.origin.url
if git config --get remote.origin.url 2>/dev/null | grep $selfgitrepo >/dev/null; then if git config --get remote.origin.url 2>/dev/null | grep -q $selfgitrepo; then
echo echo
echo -n "Delete local .git and .gitignore? [y/N] > " echo -n "Delete local .git and .gitignore? [y/N] > "
read -r answer read -r answer
...@@ -180,7 +311,7 @@ function _fix_no-db(){ ...@@ -180,7 +311,7 @@ function _fix_no-db(){
fi fi
} }
# helper functiion to generate replacements using sed # helper function to generate replacements using sed
# it loops over all vars in the config file # it loops over all vars in the config file
# used in _generateFiles # used in _generateFiles
function _getreplaces(){ function _getreplaces(){
...@@ -202,15 +333,21 @@ function _getreplaces(){ ...@@ -202,15 +333,21 @@ function _getreplaces(){
# It skips if # It skips if
# - 1st line is not starting with "# TARGET: filename" # - 1st line is not starting with "# TARGET: filename"
# - target file has no updated lines # - target file has no updated lines
# If the 1st parameter is set to "dryrun" it will not generate files.
# param string dryrun optional: set to "dryrun" to not generate files
function _generateFiles(){ function _generateFiles(){
local _dryrun="$1"
DC_CONFIG_CHANGED=0
# shellcheck source=/dev/null # shellcheck source=/dev/null
. "${_self}.cfg" || exit 1 . "${_self}.cfg" || exit 1
params=$( _getreplaces | while read -r line; do echo -n "-e '$line' "; done ) params=$( _getreplaces | while read -r line; do echo -n "-e '$line' "; done )
local _tmpfile=/tmp/newfilecontent$$.tmp local _tmpfile=/tmp/newfilecontent$$.tmp
h2 "generate files from templates..."
test "$_dryrun" = "dryrun" || h2 "generate files from templates..."
for mytpl in templates/* for mytpl in templates/*
do do
# h3 $mytpl # h3 $mytpl
...@@ -220,7 +357,9 @@ function _generateFiles(){ ...@@ -220,7 +357,9 @@ function _generateFiles(){
target=$( head -1 "$mytpl" | grep "^# TARGET:" | cut -f 2- -d ":" | awk '{ print $1 }' ) target=$( head -1 "$mytpl" | grep "^# TARGET:" | cut -f 2- -d ":" | awk '{ print $1 }' )
if [ -z "$target" ]; then if [ -z "$target" ]; then
if [ "$_dryrun" != "dryrun" ]; then
echo "SKIP: $mytpl - target was not found in 1st line" echo "SKIP: $mytpl - target was not found in 1st line"
fi
_doReplace=0 _doReplace=0
fi fi
...@@ -235,22 +374,30 @@ function _generateFiles(){ ...@@ -235,22 +374,30 @@ function _generateFiles(){
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 "s#{{generator}}#GENERATED BY $_self - template: $mytpl - $_md5#g" $_tmpfile
# apply all replacements to the tmp file
eval sed -i "$params" "$_tmpfile" || exit eval sed -i "$params" "$_tmpfile" || exit
_fix_no-db $_tmpfile _fix_no-db $_tmpfile
# echo "changes for $target:" # echo "changes for $target:"
if diff --color=always "../$target" "$_tmpfile" | grep -v "$_md5" | grep -v "^---" | grep . || [ ! -f "../$target" ]; then if diff --color=always "../$target" "$_tmpfile" 2>/dev/null | grep -v "$_md5" | grep -v "^---" | grep . || [ ! -f "../$target" ]; then
if [ "$_dryrun" = "dryrun" ]
then
DC_CONFIG_CHANGED=1
else
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 OK
echo echo
fi
else else
rm -f $_tmpfile rm -f $_tmpfile
if [ "$_dryrun" != "dryrun" ]; then
echo "SKIP: $mytpl - Nothing to do." echo "SKIP: $mytpl - Nothing to do."
fi fi
fi fi
fi
done done
} }
...@@ -278,9 +425,11 @@ function _removeGeneratedFiles(){ ...@@ -278,9 +425,11 @@ function _removeGeneratedFiles(){
done done
} }
# show running containers # show running containers
function _showContainers(){ function _showContainers(){
local bLong=$1 local bLong=$1
local _out local _out
local sUp=".. UP" local sUp=".. UP"
...@@ -288,56 +437,42 @@ function _showContainers(){ ...@@ -288,56 +437,42 @@ function _showContainers(){
local Status= local Status=
local StatusWeb="$sDown" local StatusWeb="$sDown"
local StatusDb="" local StatusDb="$sDown"
local colWeb= local colWeb=
local colDb= local colDb=
colDb="$fgRed" colDb="$fgRed"
colWeb="$fgRed" colWeb="$fgRed"
_out=$( if [ -z "$bLong" ]; then if [ $DC_WEB_UP -eq 1 ]; then
docker-compose -p "$APP_NAME" ps colWeb="$fgGreen"
else StatusWeb="$sUp"
# docker ps | grep "$APP_NAME"
docker-compose -p "$APP_NAME" ps
fi)
h2 CONTAINERS
if [ "$( wc -l <<< "$_out" )" -eq 1 ]; then
if [ "$DB_ADD" = "false" ]; then
colDb="$fgGray"
Status="The web container is not running. This app has no database container."
else
StatusDb="down"
Status="No container is running for <$APP_NAME>."
fi fi
else
grep -q "${APP_NAME}-server" <<< "$_out" && colWeb="$fgGreen"
grep -q "${APP_NAME}-server" <<< "$_out" && StatusWeb="$sUp"
grep -q "${APP_NAME}-db" <<< "$_out" && colDb="$fgGreen" if [ $DC_DB_UP -eq 1 ]; then
StatusDb="$sDown" colDb="$fgGreen"
grep -q "${APP_NAME}-db" <<< "$_out" && StatusDb="$sUp" StatusDb="$sUp"
fi
if [ "$DB_ADD" = "false" ]; then if [ "$DB_ADD" = "false" ]; then
colDb="$fgGray" colDb="$fgGray"
StatusDb="" local StatusDb=".. N/A"
Status="INFO: This app has no database container." Status="This app has no database container."
fi
fi fi
printf "$colWeb __________________________ $colDb __________________________ $fgReset \n" h2 CONTAINERS
printf "$colWeb | %-22s | $colDb | %-22s | $fgReset \n" "" ""
printf "$colWeb | %-22s | $colDb | %-22s | $fgReset \n" "${APP_NAME}-web ${StatusWeb}" "${APP_NAME}-db ${StatusDb}"
printf "$colWeb | %-22s | $colDb | %-22s | $fgReset \n" " PHP ${APP_PHP_VERSION}" " ${MYSQL_IMAGE}"
printf "$colWeb | %-22s | $colDb | %-22s | $fgReset \n" " :${APP_PORT}" " :${DB_PORT}"
printf "$colWeb |__________________________| $colDb |__________________________| $fgReset \n"
if [ -n "$Status" ]; then
echo echo
printf " $colWeb$fgInvert %-32s $fgReset $colDb$fgInvert %-32s $fgReset\n" "WEB ${StatusWeb}" "DB ${StatusDb}"
printf " %-32s $fgReset %-32s $fgReset\n" "PHP ${APP_PHP_VERSION}" "${MYSQL_IMAGE}"
printf " %-32s $fgReset %-32s $fgReset\n" ":${APP_PORT}" ":${DB_PORT}"
echo
if [ -n "$Status" ]; then
echo " $Status" echo " $Status"
fi
echo echo
fi
if [ -n "$bLong" ]; then if [ -n "$bLong" ]; then
echo "$_out" echo "$_out"
...@@ -349,61 +484,6 @@ function _showContainers(){ ...@@ -349,61 +484,6 @@ function _showContainers(){
} }
# show urls for app container
function _showBrowserurl(){
echo "In a web browser open:"
echo " $frontendurl"
if grep "${APP_NAME}-server" /etc/hosts >/dev/null; then
echo " https://${APP_NAME}-server/"
fi
}
# detect + show ports and urls for app container and db container
function _showInfos(){
_showContainers long
h2 INFO
h3 "processes webserver"
# docker-compose top
docker top "${APP_NAME}-server"
if [ ! "$DB_ADD" = "false" ]; then
h3 "processes database"
docker top "${APP_NAME}-db"
fi
h3 "Check app port"
if echo >"/dev/tcp/localhost/${APP_PORT}"; then
echo "OK, app port ${APP_PORT} is reachable"
echo
_showBrowserurl
else
echo "NO, app port ${APP_PORT} is not available"
fi 2>/dev/null
if [ "$DB_ADD" != "false" ]; then
h3 "Check database port"
if echo >"/dev/tcp/localhost/${DB_PORT}"; then
echo "OK, db port ${DB_PORT} is reachable"
echo
echo "In a local DB admin tool you can connect it:"
echo " host : localhost"
echo " port : ${DB_PORT}"
echo " user : root"
echo " password: ${MYSQL_ROOT_PASS}"
else
echo "NO, db port ${DB_PORT} is not available"
fi 2>/dev/null
fi
echo
}
# helper for menu: print an inverted key
function _key(){
echo -en "\e[4;7m ${1} \e[0m"
}
# helper: wait for a return key # helper: wait for a return key
function _wait(){ function _wait(){
local _wait=15 local _wait=15
...@@ -418,13 +498,16 @@ action=$1; shift 1 ...@@ -418,13 +498,16 @@ action=$1; shift 1
while true; do while true; do
_getStatus_repo
_getStatus_docker
_getStatus_template
_getWebUrl
if [ -z "$action" ]; then if [ -z "$action" ]; then
echo "_______________________________________________________________________________" echo "_______________________________________________________________________________"
echo echo
echo printf " %-70s ______\n" "${APP_NAME^^} :: Initializer for docker"
echo " ${APP_NAME^^} :: Initializer for docker"
echo " ______"
echo "________________________________________________________________________/ $_version" echo "________________________________________________________________________/ $_version"
echo echo
...@@ -524,7 +607,7 @@ while true; do ...@@ -524,7 +607,7 @@ while true; do
;; ;;
o) o)
h2 "Open app ..." h2 "Open app ..."
xdg-open "$frontendurl" xdg-open "$DC_WEB_URL"
;; ;;
q) q)
h2 "Bye!" h2 "Bye!"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment