Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Refactoring of function executing scripts during initialization or container starting #160

Merged
merged 4 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,18 @@ 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
|- custom_script.php
|- custom_script.sh
```

Mount the volume with compose file :
Mount the volumes with compose file :
```yaml
services:
mariadb:
Expand All @@ -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:
Expand Down
9 changes: 6 additions & 3 deletions README.template
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,18 @@ 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
|- custom_script.php
|- custom_script.sh
```

Mount the volume with compose file :
Mount the volumes with compose file :
```yaml
services:
mariadb:
Expand All @@ -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:
Expand Down
47 changes: 28 additions & 19 deletions docker-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down
47 changes: 28 additions & 19 deletions images/15.0.3-php7.4/docker-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down
47 changes: 28 additions & 19 deletions images/16.0.5-php8.1/docker-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down
47 changes: 28 additions & 19 deletions images/17.0.4-php8.1/docker-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down
47 changes: 28 additions & 19 deletions images/18.0.5-php8.1/docker-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down
Loading
Loading