From b9b5fece83aa05956487f729314270d3562b037f Mon Sep 17 00:00:00 2001 From: Benoit Vianin Date: Sat, 13 Jul 2024 14:12:50 +0200 Subject: [PATCH 1/3] Refactoring of function running scripts --- docker-run.sh | 47 ++++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/docker-run.sh b/docker-run.sh index 9196c13..480c32e 100755 --- a/docker-run.sh +++ b/docker-run.sh @@ -129,6 +129,29 @@ function lockInstallation() chmod 400 /var/www/documents/install.lock } +function runScripts() +{ + if [ -d /var/www/scripts/$1 ] ; then + for file in /var/www/scripts/$1/*; do + [ ! -f $file ] && continue + + # If extension is not in PHP SQL SH, we loop + isExec=$(echo "PHP SQL SH" | grep -wio ${file##*.}) + [ -z "$isExec" ] && continue + + echo "Importing custom ${isExec} from `basename ${file}` ..." + if [ "$isExec" == "SQL" ] ; then + sed -i 's/--.*//g;' ${file} + mysql -u ${DOLI_DB_USER} -p${DOLI_DB_PASSWORD} -h ${DOLI_DB_HOST} -P ${DOLI_DB_HOST_PORT} ${DOLI_DB_NAME} < ${file} > /dev/null 2>&1 + elif [ "$isExec" == "PHP" ] ; then + php $file + elif [ "$isExec" == "SH" ] ; then + /bin/bash $file + fi + done + fi +} + function initializeDatabase() { for fileSQL in /var/www/html/install/mysql/tables/*.sql; do @@ -172,25 +195,8 @@ function initializeDatabase() echo "Enable user module ..." php /var/www/scripts/docker-init.php - if [ -d /var/www/scripts/docker-init.d ] ; then - for file in /var/www/scripts/docker-init.d/*; do - [ ! -f $file ] && continue - - # If extension is not in PHP SQL SH, we loop - isExec=$(echo "PHP SQL SH" | grep -wio ${file##*.}) - [ -z "$isExec" ] && continue - - echo "Importing custom ${isExec} from `basename ${file}` ..." - if [ "$isExec" == "SQL" ] ; then - sed -i 's/--.*//g;' ${file} - mysql -u ${DOLI_DB_USER} -p${DOLI_DB_PASSWORD} -h ${DOLI_DB_HOST} -P ${DOLI_DB_HOST_PORT} ${DOLI_DB_NAME} < ${file} > /dev/null 2>&1 - elif [ "$isExec" == "PHP" ] ; then - php $file - elif [ "$isExec" == "SH" ] ; then - /bin/bash $file - fi - done - fi + # Run init scripts + runScripts "docker-init.d" # Update ownership after initialisation of modules chown -R www-data:www-data /var/www/documents @@ -256,6 +262,9 @@ function run() lockInstallation fi fi + + # Run scripts before starting + runScripts "before-starting.d" } DOLI_DB_USER=$(get_env_value 'DOLI_DB_USER' 'doli') From e1b07b201ec31f9d5978721bb1efecf35207a933 Mon Sep 17 00:00:00 2001 From: Benoit Vianin Date: Sun, 14 Jul 2024 07:44:55 +0200 Subject: [PATCH 2/3] Update readme files --- README.md | 9 ++++++--- README.template | 9 ++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 24a259d..cf2cc8f 100644 --- a/README.md +++ b/README.md @@ -138,8 +138,10 @@ Environment variables that are compatible with docker secrets: * `DOLI_CRON_USER` => `DOLI_CRON_USER_FILE` * `DOLI_INSTANCE_UNIQUE_ID` => `DOLI_INSTANCE_UNIQUE_ID_FILE` -## Add post-deployment scripts -It is possible to execute `*.sh`, `*.sql` and/or `*.php` custom file at the end of deployment by mounting a volume in `/var/www/scripts/docker-init.d` +## Add post-deployment and before starting scripts +It is possible to execute `*.sh`, `*.sql` and/or `*.php` custom file at the end of deployment or before starting Apache by mounting volumes. +For scripts executed during deployment mount volume in `/var/www/scripts/docker-init.d`. +For scripts executed before Apache stating mount volume in `/var/www/scripts/before-starting.d`. ``` \docker-init.d |- custom_script.sql @@ -147,7 +149,7 @@ It is possible to execute `*.sh`, `*.sql` and/or `*.php` custom file at the end |- custom_script.sh ``` -Mount the volume with compose file : +Mount the volumes with compose file : ```yaml services: mariadb: @@ -167,6 +169,7 @@ services: PHP_INI_DATE_TIMEZONE: 'Europe/Paris' volumes : - volume-scripts:/var/www/scripts/docker-init.d + - before-starting-scripts:/var/www/scripts/before-starting.d ports: - "80:80" links: diff --git a/README.template b/README.template index 01a90dd..5aa9546 100644 --- a/README.template +++ b/README.template @@ -132,8 +132,10 @@ Environment variables that are compatible with docker secrets: * `DOLI_CRON_USER` => `DOLI_CRON_USER_FILE` * `DOLI_INSTANCE_UNIQUE_ID` => `DOLI_INSTANCE_UNIQUE_ID_FILE` -## Add post-deployment scripts -It is possible to execute `*.sh`, `*.sql` and/or `*.php` custom file at the end of deployment by mounting a volume in `/var/www/scripts/docker-init.d` +## Add post-deployment and before starting scripts +It is possible to execute `*.sh`, `*.sql` and/or `*.php` custom file at the end of deployment or before starting Apache by mounting volumes. +For scripts executed during deployment mount volume in `/var/www/scripts/docker-init.d`. +For scripts executed before Apache stating mount volume in `/var/www/scripts/before-starting.d`. ``` \docker-init.d |- custom_script.sql @@ -141,7 +143,7 @@ It is possible to execute `*.sh`, `*.sql` and/or `*.php` custom file at the end |- custom_script.sh ``` -Mount the volume with compose file : +Mount the volumes with compose file : ```yaml services: mariadb: @@ -161,6 +163,7 @@ services: PHP_INI_DATE_TIMEZONE: 'Europe/Paris' volumes : - volume-scripts:/var/www/scripts/docker-init.d + - before-starting-scripts:/var/www/scripts/before-starting.d ports: - "80:80" links: From d1dbcfc50a67f7dbc9cae9ba19a14f608e7f795f Mon Sep 17 00:00:00 2001 From: Benoit Vianin Date: Sun, 14 Jul 2024 07:45:42 +0200 Subject: [PATCH 3/3] Update images with new docker-run script --- images/15.0.3-php7.4/docker-run.sh | 47 ++++++++++++++++++------------ images/16.0.5-php8.1/docker-run.sh | 47 ++++++++++++++++++------------ images/17.0.4-php8.1/docker-run.sh | 47 ++++++++++++++++++------------ images/18.0.5-php8.1/docker-run.sh | 47 ++++++++++++++++++------------ images/19.0.2-php8.2/docker-run.sh | 47 ++++++++++++++++++------------ images/develop/docker-run.sh | 47 ++++++++++++++++++------------ 6 files changed, 168 insertions(+), 114 deletions(-) diff --git a/images/15.0.3-php7.4/docker-run.sh b/images/15.0.3-php7.4/docker-run.sh index 9196c13..480c32e 100755 --- a/images/15.0.3-php7.4/docker-run.sh +++ b/images/15.0.3-php7.4/docker-run.sh @@ -129,6 +129,29 @@ function lockInstallation() chmod 400 /var/www/documents/install.lock } +function runScripts() +{ + if [ -d /var/www/scripts/$1 ] ; then + for file in /var/www/scripts/$1/*; do + [ ! -f $file ] && continue + + # If extension is not in PHP SQL SH, we loop + isExec=$(echo "PHP SQL SH" | grep -wio ${file##*.}) + [ -z "$isExec" ] && continue + + echo "Importing custom ${isExec} from `basename ${file}` ..." + if [ "$isExec" == "SQL" ] ; then + sed -i 's/--.*//g;' ${file} + mysql -u ${DOLI_DB_USER} -p${DOLI_DB_PASSWORD} -h ${DOLI_DB_HOST} -P ${DOLI_DB_HOST_PORT} ${DOLI_DB_NAME} < ${file} > /dev/null 2>&1 + elif [ "$isExec" == "PHP" ] ; then + php $file + elif [ "$isExec" == "SH" ] ; then + /bin/bash $file + fi + done + fi +} + function initializeDatabase() { for fileSQL in /var/www/html/install/mysql/tables/*.sql; do @@ -172,25 +195,8 @@ function initializeDatabase() echo "Enable user module ..." php /var/www/scripts/docker-init.php - if [ -d /var/www/scripts/docker-init.d ] ; then - for file in /var/www/scripts/docker-init.d/*; do - [ ! -f $file ] && continue - - # If extension is not in PHP SQL SH, we loop - isExec=$(echo "PHP SQL SH" | grep -wio ${file##*.}) - [ -z "$isExec" ] && continue - - echo "Importing custom ${isExec} from `basename ${file}` ..." - if [ "$isExec" == "SQL" ] ; then - sed -i 's/--.*//g;' ${file} - mysql -u ${DOLI_DB_USER} -p${DOLI_DB_PASSWORD} -h ${DOLI_DB_HOST} -P ${DOLI_DB_HOST_PORT} ${DOLI_DB_NAME} < ${file} > /dev/null 2>&1 - elif [ "$isExec" == "PHP" ] ; then - php $file - elif [ "$isExec" == "SH" ] ; then - /bin/bash $file - fi - done - fi + # Run init scripts + runScripts "docker-init.d" # Update ownership after initialisation of modules chown -R www-data:www-data /var/www/documents @@ -256,6 +262,9 @@ function run() lockInstallation fi fi + + # Run scripts before starting + runScripts "before-starting.d" } DOLI_DB_USER=$(get_env_value 'DOLI_DB_USER' 'doli') diff --git a/images/16.0.5-php8.1/docker-run.sh b/images/16.0.5-php8.1/docker-run.sh index 9196c13..480c32e 100755 --- a/images/16.0.5-php8.1/docker-run.sh +++ b/images/16.0.5-php8.1/docker-run.sh @@ -129,6 +129,29 @@ function lockInstallation() chmod 400 /var/www/documents/install.lock } +function runScripts() +{ + if [ -d /var/www/scripts/$1 ] ; then + for file in /var/www/scripts/$1/*; do + [ ! -f $file ] && continue + + # If extension is not in PHP SQL SH, we loop + isExec=$(echo "PHP SQL SH" | grep -wio ${file##*.}) + [ -z "$isExec" ] && continue + + echo "Importing custom ${isExec} from `basename ${file}` ..." + if [ "$isExec" == "SQL" ] ; then + sed -i 's/--.*//g;' ${file} + mysql -u ${DOLI_DB_USER} -p${DOLI_DB_PASSWORD} -h ${DOLI_DB_HOST} -P ${DOLI_DB_HOST_PORT} ${DOLI_DB_NAME} < ${file} > /dev/null 2>&1 + elif [ "$isExec" == "PHP" ] ; then + php $file + elif [ "$isExec" == "SH" ] ; then + /bin/bash $file + fi + done + fi +} + function initializeDatabase() { for fileSQL in /var/www/html/install/mysql/tables/*.sql; do @@ -172,25 +195,8 @@ function initializeDatabase() echo "Enable user module ..." php /var/www/scripts/docker-init.php - if [ -d /var/www/scripts/docker-init.d ] ; then - for file in /var/www/scripts/docker-init.d/*; do - [ ! -f $file ] && continue - - # If extension is not in PHP SQL SH, we loop - isExec=$(echo "PHP SQL SH" | grep -wio ${file##*.}) - [ -z "$isExec" ] && continue - - echo "Importing custom ${isExec} from `basename ${file}` ..." - if [ "$isExec" == "SQL" ] ; then - sed -i 's/--.*//g;' ${file} - mysql -u ${DOLI_DB_USER} -p${DOLI_DB_PASSWORD} -h ${DOLI_DB_HOST} -P ${DOLI_DB_HOST_PORT} ${DOLI_DB_NAME} < ${file} > /dev/null 2>&1 - elif [ "$isExec" == "PHP" ] ; then - php $file - elif [ "$isExec" == "SH" ] ; then - /bin/bash $file - fi - done - fi + # Run init scripts + runScripts "docker-init.d" # Update ownership after initialisation of modules chown -R www-data:www-data /var/www/documents @@ -256,6 +262,9 @@ function run() lockInstallation fi fi + + # Run scripts before starting + runScripts "before-starting.d" } DOLI_DB_USER=$(get_env_value 'DOLI_DB_USER' 'doli') diff --git a/images/17.0.4-php8.1/docker-run.sh b/images/17.0.4-php8.1/docker-run.sh index 9196c13..480c32e 100755 --- a/images/17.0.4-php8.1/docker-run.sh +++ b/images/17.0.4-php8.1/docker-run.sh @@ -129,6 +129,29 @@ function lockInstallation() chmod 400 /var/www/documents/install.lock } +function runScripts() +{ + if [ -d /var/www/scripts/$1 ] ; then + for file in /var/www/scripts/$1/*; do + [ ! -f $file ] && continue + + # If extension is not in PHP SQL SH, we loop + isExec=$(echo "PHP SQL SH" | grep -wio ${file##*.}) + [ -z "$isExec" ] && continue + + echo "Importing custom ${isExec} from `basename ${file}` ..." + if [ "$isExec" == "SQL" ] ; then + sed -i 's/--.*//g;' ${file} + mysql -u ${DOLI_DB_USER} -p${DOLI_DB_PASSWORD} -h ${DOLI_DB_HOST} -P ${DOLI_DB_HOST_PORT} ${DOLI_DB_NAME} < ${file} > /dev/null 2>&1 + elif [ "$isExec" == "PHP" ] ; then + php $file + elif [ "$isExec" == "SH" ] ; then + /bin/bash $file + fi + done + fi +} + function initializeDatabase() { for fileSQL in /var/www/html/install/mysql/tables/*.sql; do @@ -172,25 +195,8 @@ function initializeDatabase() echo "Enable user module ..." php /var/www/scripts/docker-init.php - if [ -d /var/www/scripts/docker-init.d ] ; then - for file in /var/www/scripts/docker-init.d/*; do - [ ! -f $file ] && continue - - # If extension is not in PHP SQL SH, we loop - isExec=$(echo "PHP SQL SH" | grep -wio ${file##*.}) - [ -z "$isExec" ] && continue - - echo "Importing custom ${isExec} from `basename ${file}` ..." - if [ "$isExec" == "SQL" ] ; then - sed -i 's/--.*//g;' ${file} - mysql -u ${DOLI_DB_USER} -p${DOLI_DB_PASSWORD} -h ${DOLI_DB_HOST} -P ${DOLI_DB_HOST_PORT} ${DOLI_DB_NAME} < ${file} > /dev/null 2>&1 - elif [ "$isExec" == "PHP" ] ; then - php $file - elif [ "$isExec" == "SH" ] ; then - /bin/bash $file - fi - done - fi + # Run init scripts + runScripts "docker-init.d" # Update ownership after initialisation of modules chown -R www-data:www-data /var/www/documents @@ -256,6 +262,9 @@ function run() lockInstallation fi fi + + # Run scripts before starting + runScripts "before-starting.d" } DOLI_DB_USER=$(get_env_value 'DOLI_DB_USER' 'doli') diff --git a/images/18.0.5-php8.1/docker-run.sh b/images/18.0.5-php8.1/docker-run.sh index 9196c13..480c32e 100755 --- a/images/18.0.5-php8.1/docker-run.sh +++ b/images/18.0.5-php8.1/docker-run.sh @@ -129,6 +129,29 @@ function lockInstallation() chmod 400 /var/www/documents/install.lock } +function runScripts() +{ + if [ -d /var/www/scripts/$1 ] ; then + for file in /var/www/scripts/$1/*; do + [ ! -f $file ] && continue + + # If extension is not in PHP SQL SH, we loop + isExec=$(echo "PHP SQL SH" | grep -wio ${file##*.}) + [ -z "$isExec" ] && continue + + echo "Importing custom ${isExec} from `basename ${file}` ..." + if [ "$isExec" == "SQL" ] ; then + sed -i 's/--.*//g;' ${file} + mysql -u ${DOLI_DB_USER} -p${DOLI_DB_PASSWORD} -h ${DOLI_DB_HOST} -P ${DOLI_DB_HOST_PORT} ${DOLI_DB_NAME} < ${file} > /dev/null 2>&1 + elif [ "$isExec" == "PHP" ] ; then + php $file + elif [ "$isExec" == "SH" ] ; then + /bin/bash $file + fi + done + fi +} + function initializeDatabase() { for fileSQL in /var/www/html/install/mysql/tables/*.sql; do @@ -172,25 +195,8 @@ function initializeDatabase() echo "Enable user module ..." php /var/www/scripts/docker-init.php - if [ -d /var/www/scripts/docker-init.d ] ; then - for file in /var/www/scripts/docker-init.d/*; do - [ ! -f $file ] && continue - - # If extension is not in PHP SQL SH, we loop - isExec=$(echo "PHP SQL SH" | grep -wio ${file##*.}) - [ -z "$isExec" ] && continue - - echo "Importing custom ${isExec} from `basename ${file}` ..." - if [ "$isExec" == "SQL" ] ; then - sed -i 's/--.*//g;' ${file} - mysql -u ${DOLI_DB_USER} -p${DOLI_DB_PASSWORD} -h ${DOLI_DB_HOST} -P ${DOLI_DB_HOST_PORT} ${DOLI_DB_NAME} < ${file} > /dev/null 2>&1 - elif [ "$isExec" == "PHP" ] ; then - php $file - elif [ "$isExec" == "SH" ] ; then - /bin/bash $file - fi - done - fi + # Run init scripts + runScripts "docker-init.d" # Update ownership after initialisation of modules chown -R www-data:www-data /var/www/documents @@ -256,6 +262,9 @@ function run() lockInstallation fi fi + + # Run scripts before starting + runScripts "before-starting.d" } DOLI_DB_USER=$(get_env_value 'DOLI_DB_USER' 'doli') diff --git a/images/19.0.2-php8.2/docker-run.sh b/images/19.0.2-php8.2/docker-run.sh index 9196c13..480c32e 100755 --- a/images/19.0.2-php8.2/docker-run.sh +++ b/images/19.0.2-php8.2/docker-run.sh @@ -129,6 +129,29 @@ function lockInstallation() chmod 400 /var/www/documents/install.lock } +function runScripts() +{ + if [ -d /var/www/scripts/$1 ] ; then + for file in /var/www/scripts/$1/*; do + [ ! -f $file ] && continue + + # If extension is not in PHP SQL SH, we loop + isExec=$(echo "PHP SQL SH" | grep -wio ${file##*.}) + [ -z "$isExec" ] && continue + + echo "Importing custom ${isExec} from `basename ${file}` ..." + if [ "$isExec" == "SQL" ] ; then + sed -i 's/--.*//g;' ${file} + mysql -u ${DOLI_DB_USER} -p${DOLI_DB_PASSWORD} -h ${DOLI_DB_HOST} -P ${DOLI_DB_HOST_PORT} ${DOLI_DB_NAME} < ${file} > /dev/null 2>&1 + elif [ "$isExec" == "PHP" ] ; then + php $file + elif [ "$isExec" == "SH" ] ; then + /bin/bash $file + fi + done + fi +} + function initializeDatabase() { for fileSQL in /var/www/html/install/mysql/tables/*.sql; do @@ -172,25 +195,8 @@ function initializeDatabase() echo "Enable user module ..." php /var/www/scripts/docker-init.php - if [ -d /var/www/scripts/docker-init.d ] ; then - for file in /var/www/scripts/docker-init.d/*; do - [ ! -f $file ] && continue - - # If extension is not in PHP SQL SH, we loop - isExec=$(echo "PHP SQL SH" | grep -wio ${file##*.}) - [ -z "$isExec" ] && continue - - echo "Importing custom ${isExec} from `basename ${file}` ..." - if [ "$isExec" == "SQL" ] ; then - sed -i 's/--.*//g;' ${file} - mysql -u ${DOLI_DB_USER} -p${DOLI_DB_PASSWORD} -h ${DOLI_DB_HOST} -P ${DOLI_DB_HOST_PORT} ${DOLI_DB_NAME} < ${file} > /dev/null 2>&1 - elif [ "$isExec" == "PHP" ] ; then - php $file - elif [ "$isExec" == "SH" ] ; then - /bin/bash $file - fi - done - fi + # Run init scripts + runScripts "docker-init.d" # Update ownership after initialisation of modules chown -R www-data:www-data /var/www/documents @@ -256,6 +262,9 @@ function run() lockInstallation fi fi + + # Run scripts before starting + runScripts "before-starting.d" } DOLI_DB_USER=$(get_env_value 'DOLI_DB_USER' 'doli') diff --git a/images/develop/docker-run.sh b/images/develop/docker-run.sh index 9196c13..480c32e 100755 --- a/images/develop/docker-run.sh +++ b/images/develop/docker-run.sh @@ -129,6 +129,29 @@ function lockInstallation() chmod 400 /var/www/documents/install.lock } +function runScripts() +{ + if [ -d /var/www/scripts/$1 ] ; then + for file in /var/www/scripts/$1/*; do + [ ! -f $file ] && continue + + # If extension is not in PHP SQL SH, we loop + isExec=$(echo "PHP SQL SH" | grep -wio ${file##*.}) + [ -z "$isExec" ] && continue + + echo "Importing custom ${isExec} from `basename ${file}` ..." + if [ "$isExec" == "SQL" ] ; then + sed -i 's/--.*//g;' ${file} + mysql -u ${DOLI_DB_USER} -p${DOLI_DB_PASSWORD} -h ${DOLI_DB_HOST} -P ${DOLI_DB_HOST_PORT} ${DOLI_DB_NAME} < ${file} > /dev/null 2>&1 + elif [ "$isExec" == "PHP" ] ; then + php $file + elif [ "$isExec" == "SH" ] ; then + /bin/bash $file + fi + done + fi +} + function initializeDatabase() { for fileSQL in /var/www/html/install/mysql/tables/*.sql; do @@ -172,25 +195,8 @@ function initializeDatabase() echo "Enable user module ..." php /var/www/scripts/docker-init.php - if [ -d /var/www/scripts/docker-init.d ] ; then - for file in /var/www/scripts/docker-init.d/*; do - [ ! -f $file ] && continue - - # If extension is not in PHP SQL SH, we loop - isExec=$(echo "PHP SQL SH" | grep -wio ${file##*.}) - [ -z "$isExec" ] && continue - - echo "Importing custom ${isExec} from `basename ${file}` ..." - if [ "$isExec" == "SQL" ] ; then - sed -i 's/--.*//g;' ${file} - mysql -u ${DOLI_DB_USER} -p${DOLI_DB_PASSWORD} -h ${DOLI_DB_HOST} -P ${DOLI_DB_HOST_PORT} ${DOLI_DB_NAME} < ${file} > /dev/null 2>&1 - elif [ "$isExec" == "PHP" ] ; then - php $file - elif [ "$isExec" == "SH" ] ; then - /bin/bash $file - fi - done - fi + # Run init scripts + runScripts "docker-init.d" # Update ownership after initialisation of modules chown -R www-data:www-data /var/www/documents @@ -256,6 +262,9 @@ function run() lockInstallation fi fi + + # Run scripts before starting + runScripts "before-starting.d" } DOLI_DB_USER=$(get_env_value 'DOLI_DB_USER' 'doli')