docker/entrypoint.sh

163 lines
3.6 KiB
Bash

#!/bin/bash
echo "Now in entrypoint.sh for Webcron Management"
echo "Script: 1.0.21 (2022-02-17)"
echo "User: '$(whoami)'"
echo "Group: '$(id -g -n)'"
echo "Working dir: '$(pwd)'"
# https://github.com/docker-library/wordpress/blob/master/docker-entrypoint.sh
# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
exit 1
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}
# envs that can be appended with _FILE
envs=(
APP_ENV
DATABASE_URL
DEMO_MODE
DEMO_USER
DEMO_PASS
APP_SECRET
ENCRYPTION_METHOD
HASHING_METHOD
DEBUG
COOKIE_LIFETIME
TRUSTED_PROXIES
MAILER_DSN
MAILER_FROM
)
echo "Now parsing _FILE variables."
for e in "${envs[@]}"; do
file_env "$e"
done
echo "done!"
echo "Dump envs to .env..."
rm -rf .env
touch .env
for e in "${envs[@]}"; do
echo "$e=\"${!e}\"" >> .env
done
echo "Dump auto load..."
composer dump-autoload
echo "Current working dir is '$(pwd)'"
echo "Parsing database url."
DB_CONNECTION=$(php -r "echo parse_url('$DATABASE_URL')['scheme'];")
DB_HOST=$(php -r "echo parse_url('$DATABASE_URL')['host'];")
DB_PORT=$(php -r "echo parse_url('$DATABASE_URL')['port'];")
DB_NAME=$(php -r "echo parse_url('$DATABASE_URL')['path'];")
echo "Wait for the database."
if [[ -z "$DB_PORT" ]]; then
if [[ $DB_CONNECTION == "mysql" ]]; then
DB_PORT=3306
fi
fi
if [[ -n "$DB_PORT" ]]; then
/usr/local/bin/wait-for-it.sh "${DB_HOST}:${DB_PORT}" -t 60 -- echo "DB is up."
fi
echo "Wait another 5 seconds in case the DB needs to boot."
sleep 5
echo "Done waiting for the DB to boot."
if [[ $DKR_BUILD_LOCALE == "true" ]]; then
echo "Will build all locales..."
locale-gen
else
echo "Will not build the locales..."
fi
echo "Run various symfony commands..."
if [[ $DKR_RUN_MIGRATION == "false" ]]; then
echo "Will NOT run migration commands."
else
echo "Running migration commands..."
php bin/console doctrine:schema:update --force
fi
echo "Current working dir is '$(pwd)'"
# there are 0 upgrade commands
if [[ $DKR_RUN_UPGRADE == "false" ]]; then
echo 'Will NOT run upgrade commands.'
else
echo 'Running upgrade commands...'
fi
# there are 0 verify commands
if [[ $DKR_RUN_VERIFY == "false" ]]; then
echo 'Will NOT run verification commands.'
else
echo 'Running verification commands...'
fi
# report commands
if [[ $DKR_RUN_REPORT == "false" ]]; then
echo 'Will NOT run report commands.'
else
echo 'Running report commands...'
fi
# report commands
if [[ $DEMO_MODE == "true" ]]; then
echo 'Installing demo data'
su - www-data -s "/bin/bash" -c "php $WEBCRON_ROOT/bin/console webcron:demodata"
fi
php bin/console cache:clear
php bin/console cache:warm
# set docker var.
export IS_DOCKER=true
if [ -z $APACHE_RUN_USER ]
then
APACHE_RUN_USER='www-data'
fi
if [ -z $APACHE_RUN_GROUP ]
then
APACHE_RUN_GROUP='www-data'
fi
chown -R $APACHE_RUN_USER:$APACHE_RUN_GROUP $WEBCRON_ROOT/var
echo "Go!"
su - www-data -s "/bin/bash" -c "php $WEBCRON_ROOT/bin/console webcron:daemon" &
wc_pid=$!
trap "kill $wc_pid 2> /dev/null" EXIT
exec apache2-foreground &
a2_pid=$!
trap "kill $a2_pid 2> /dev/null" EXIT
while kill -0 $wc_pid 2> /dev/null && kill -0 $a2_pid 2> /dev/null; do
sleep 5
done