From bf68e31521cad883530c2560ec60513ed4cbd410 Mon Sep 17 00:00:00 2001 From: Vaadasch Date: Mon, 29 Apr 2024 18:57:56 +0200 Subject: [PATCH] Add customs scripts via volume mount (#150) * docker-init.d * Custom scripts in volume docker-init.d * FIX Custom scripts in volume docker-init.d --------- Co-authored-by: Vaadasch --- README.md | 37 ++++++++++++++++++++++++++++ README.template | 37 ++++++++++++++++++++++++++++ docker-init.php | 4 +-- docker-run.sh | 20 +++++++++++++++ images/15.0.3-php7.4/docker-init.php | 4 +-- images/15.0.3-php7.4/docker-run.sh | 20 +++++++++++++++ images/16.0.5-php8.1/docker-init.php | 4 +-- images/16.0.5-php8.1/docker-run.sh | 20 +++++++++++++++ images/17.0.4-php8.1/docker-init.php | 4 +-- images/17.0.4-php8.1/docker-run.sh | 20 +++++++++++++++ images/18.0.5-php8.1/docker-init.php | 4 +-- images/18.0.5-php8.1/docker-run.sh | 20 +++++++++++++++ images/19.0.1-php8.2/docker-init.php | 4 +-- images/19.0.1-php8.2/docker-run.sh | 20 +++++++++++++++ images/develop/docker-init.php | 4 +-- images/develop/docker-run.sh | 20 +++++++++++++++ 16 files changed, 228 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 0e42eab..a494de9 100644 --- a/README.md +++ b/README.md @@ -139,3 +139,40 @@ Environment variables that are compatible with docker secrets: * `DOLI_CRON_KEY` => `DOLI_CRON_KEY_FILE` * `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` +``` +\docker-init.d +|- custom_script.sql +|- custom_script.php +|- custom_script.sh +``` + +Mount the volume with compose file : +```yaml +version: "3" + +services: + mariadb: + image: mariadb:latest + environment: + MYSQL_ROOT_PASSWORD: root + MYSQL_DATABASE: dolibarr + + web: + image: tuxgasy/dolibarr + environment: + DOLI_DB_HOST: mariadb + DOLI_DB_USER: root + DOLI_DB_PASSWORD: root + DOLI_DB_NAME: dolibarr + DOLI_URL_ROOT: 'http://0.0.0.0' + PHP_INI_DATE_TIMEZONE: 'Europe/Paris' + volumes : + - volume-scripts:/var/www/scripts/docker-init.d + ports: + - "80:80" + links: + - mariadb +``` diff --git a/README.template b/README.template index 19ff418..83b02f8 100644 --- a/README.template +++ b/README.template @@ -133,3 +133,40 @@ Environment variables that are compatible with docker secrets: * `DOLI_CRON_KEY` => `DOLI_CRON_KEY_FILE` * `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` +``` +\docker-init.d +|- custom_script.sql +|- custom_script.php +|- custom_script.sh +``` + +Mount the volume with compose file : +```yaml +version: "3" + +services: + mariadb: + image: mariadb:latest + environment: + MYSQL_ROOT_PASSWORD: root + MYSQL_DATABASE: dolibarr + + web: + image: tuxgasy/dolibarr + environment: + DOLI_DB_HOST: mariadb + DOLI_DB_USER: root + DOLI_DB_PASSWORD: root + DOLI_DB_NAME: dolibarr + DOLI_URL_ROOT: 'http://0.0.0.0' + PHP_INI_DATE_TIMEZONE: 'Europe/Paris' + volumes : + - volume-scripts:/var/www/scripts/docker-init.d + ports: + - "80:80" + links: + - mariadb +``` diff --git a/docker-init.php b/docker-init.php index 69b3e55..e75cbd2 100644 --- a/docker-init.php +++ b/docker-init.php @@ -33,12 +33,12 @@ if ($res > 0 ) { $s = $country->id.':'.$country->code.':'.$country->label; dolibarr_set_const($db, "MAIN_INFO_SOCIETE_COUNTRY", $s, 'chaine', 0, '', $conf->entity); - printf('Configuring for country : '.$s); + printf('Configuring for country : '.$s."\n"); activateModulesRequiredByCountry($country->code); $db->commit(); } else { - printf('Unable to find country '.$countryCode); + printf('Unable to find country '.$countryCode."\n"); } } diff --git a/docker-run.sh b/docker-run.sh index b34b583..9196c13 100755 --- a/docker-run.sh +++ b/docker-run.sh @@ -172,6 +172,26 @@ 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 + # Update ownership after initialisation of modules chown -R www-data:www-data /var/www/documents } diff --git a/images/15.0.3-php7.4/docker-init.php b/images/15.0.3-php7.4/docker-init.php index 69b3e55..e75cbd2 100644 --- a/images/15.0.3-php7.4/docker-init.php +++ b/images/15.0.3-php7.4/docker-init.php @@ -33,12 +33,12 @@ if ($res > 0 ) { $s = $country->id.':'.$country->code.':'.$country->label; dolibarr_set_const($db, "MAIN_INFO_SOCIETE_COUNTRY", $s, 'chaine', 0, '', $conf->entity); - printf('Configuring for country : '.$s); + printf('Configuring for country : '.$s."\n"); activateModulesRequiredByCountry($country->code); $db->commit(); } else { - printf('Unable to find country '.$countryCode); + printf('Unable to find country '.$countryCode."\n"); } } diff --git a/images/15.0.3-php7.4/docker-run.sh b/images/15.0.3-php7.4/docker-run.sh index b34b583..9196c13 100755 --- a/images/15.0.3-php7.4/docker-run.sh +++ b/images/15.0.3-php7.4/docker-run.sh @@ -172,6 +172,26 @@ 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 + # Update ownership after initialisation of modules chown -R www-data:www-data /var/www/documents } diff --git a/images/16.0.5-php8.1/docker-init.php b/images/16.0.5-php8.1/docker-init.php index 69b3e55..e75cbd2 100644 --- a/images/16.0.5-php8.1/docker-init.php +++ b/images/16.0.5-php8.1/docker-init.php @@ -33,12 +33,12 @@ if ($res > 0 ) { $s = $country->id.':'.$country->code.':'.$country->label; dolibarr_set_const($db, "MAIN_INFO_SOCIETE_COUNTRY", $s, 'chaine', 0, '', $conf->entity); - printf('Configuring for country : '.$s); + printf('Configuring for country : '.$s."\n"); activateModulesRequiredByCountry($country->code); $db->commit(); } else { - printf('Unable to find country '.$countryCode); + printf('Unable to find country '.$countryCode."\n"); } } diff --git a/images/16.0.5-php8.1/docker-run.sh b/images/16.0.5-php8.1/docker-run.sh index b34b583..9196c13 100755 --- a/images/16.0.5-php8.1/docker-run.sh +++ b/images/16.0.5-php8.1/docker-run.sh @@ -172,6 +172,26 @@ 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 + # Update ownership after initialisation of modules chown -R www-data:www-data /var/www/documents } diff --git a/images/17.0.4-php8.1/docker-init.php b/images/17.0.4-php8.1/docker-init.php index 69b3e55..e75cbd2 100644 --- a/images/17.0.4-php8.1/docker-init.php +++ b/images/17.0.4-php8.1/docker-init.php @@ -33,12 +33,12 @@ if ($res > 0 ) { $s = $country->id.':'.$country->code.':'.$country->label; dolibarr_set_const($db, "MAIN_INFO_SOCIETE_COUNTRY", $s, 'chaine', 0, '', $conf->entity); - printf('Configuring for country : '.$s); + printf('Configuring for country : '.$s."\n"); activateModulesRequiredByCountry($country->code); $db->commit(); } else { - printf('Unable to find country '.$countryCode); + printf('Unable to find country '.$countryCode."\n"); } } diff --git a/images/17.0.4-php8.1/docker-run.sh b/images/17.0.4-php8.1/docker-run.sh index b34b583..9196c13 100755 --- a/images/17.0.4-php8.1/docker-run.sh +++ b/images/17.0.4-php8.1/docker-run.sh @@ -172,6 +172,26 @@ 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 + # Update ownership after initialisation of modules chown -R www-data:www-data /var/www/documents } diff --git a/images/18.0.5-php8.1/docker-init.php b/images/18.0.5-php8.1/docker-init.php index 69b3e55..e75cbd2 100644 --- a/images/18.0.5-php8.1/docker-init.php +++ b/images/18.0.5-php8.1/docker-init.php @@ -33,12 +33,12 @@ if ($res > 0 ) { $s = $country->id.':'.$country->code.':'.$country->label; dolibarr_set_const($db, "MAIN_INFO_SOCIETE_COUNTRY", $s, 'chaine', 0, '', $conf->entity); - printf('Configuring for country : '.$s); + printf('Configuring for country : '.$s."\n"); activateModulesRequiredByCountry($country->code); $db->commit(); } else { - printf('Unable to find country '.$countryCode); + printf('Unable to find country '.$countryCode."\n"); } } diff --git a/images/18.0.5-php8.1/docker-run.sh b/images/18.0.5-php8.1/docker-run.sh index b34b583..9196c13 100755 --- a/images/18.0.5-php8.1/docker-run.sh +++ b/images/18.0.5-php8.1/docker-run.sh @@ -172,6 +172,26 @@ 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 + # Update ownership after initialisation of modules chown -R www-data:www-data /var/www/documents } diff --git a/images/19.0.1-php8.2/docker-init.php b/images/19.0.1-php8.2/docker-init.php index 69b3e55..e75cbd2 100644 --- a/images/19.0.1-php8.2/docker-init.php +++ b/images/19.0.1-php8.2/docker-init.php @@ -33,12 +33,12 @@ if ($res > 0 ) { $s = $country->id.':'.$country->code.':'.$country->label; dolibarr_set_const($db, "MAIN_INFO_SOCIETE_COUNTRY", $s, 'chaine', 0, '', $conf->entity); - printf('Configuring for country : '.$s); + printf('Configuring for country : '.$s."\n"); activateModulesRequiredByCountry($country->code); $db->commit(); } else { - printf('Unable to find country '.$countryCode); + printf('Unable to find country '.$countryCode."\n"); } } diff --git a/images/19.0.1-php8.2/docker-run.sh b/images/19.0.1-php8.2/docker-run.sh index b34b583..9196c13 100755 --- a/images/19.0.1-php8.2/docker-run.sh +++ b/images/19.0.1-php8.2/docker-run.sh @@ -172,6 +172,26 @@ 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 + # Update ownership after initialisation of modules chown -R www-data:www-data /var/www/documents } diff --git a/images/develop/docker-init.php b/images/develop/docker-init.php index 69b3e55..e75cbd2 100644 --- a/images/develop/docker-init.php +++ b/images/develop/docker-init.php @@ -33,12 +33,12 @@ if ($res > 0 ) { $s = $country->id.':'.$country->code.':'.$country->label; dolibarr_set_const($db, "MAIN_INFO_SOCIETE_COUNTRY", $s, 'chaine', 0, '', $conf->entity); - printf('Configuring for country : '.$s); + printf('Configuring for country : '.$s."\n"); activateModulesRequiredByCountry($country->code); $db->commit(); } else { - printf('Unable to find country '.$countryCode); + printf('Unable to find country '.$countryCode."\n"); } } diff --git a/images/develop/docker-run.sh b/images/develop/docker-run.sh index b34b583..9196c13 100755 --- a/images/develop/docker-run.sh +++ b/images/develop/docker-run.sh @@ -172,6 +172,26 @@ 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 + # Update ownership after initialisation of modules chown -R www-data:www-data /var/www/documents }