Merge pull request #136 from NetLah-external/improve-nc-mysql-host-port

Extract host and port from DB_HOST also support DB_PORT to test connection instead of only use default port 3306
This commit is contained in:
Eric Nemchik 2022-12-22 16:33:41 -06:00 committed by GitHub
commit c075b282cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 39 deletions

View File

@ -27,9 +27,10 @@ param_usage_include_env: true
param_env_vars:
- { env_var: "APP_URL", env_value: "", desc: "for specifying the IP:port or URL your application will be accessed on (ie. `http://192.168.1.1:6875` or `https://bookstack.mydomain.com`"}
- { env_var: "DB_HOST", env_value: "<yourdbhost>", desc: "for specifying the database host" }
- { env_var: "DB_PORT", env_value: "<yourdbport>", desc: "for specifying the database port if not default 3306" }
- { env_var: "DB_USER", env_value: "<yourdbuser>", desc: "for specifying the database user" }
- { env_var: "DB_PASS", env_value: "<yourdbpass>", desc: "for specifying the database password" }
- { env_var: "DB_DATABASE", env_value: "bookstackapp", desc: "for specifying the database to be used (non-alphanumeric passwords must be properly escaped.)" }
- { env_var: "DB_PASS", env_value: "<yourdbpass>", desc: "for specifying the database password (non-alphanumeric passwords must be properly escaped.)" }
- { env_var: "DB_DATABASE", env_value: "bookstackapp", desc: "for specifying the database to be used" }
param_usage_include_ports: true
param_ports:
@ -50,6 +51,7 @@ custom_compose: |
- PGID=1000
- APP_URL=
- DB_HOST=bookstack_db
- DB_PORT=3306
- DB_USER=bookstack
- DB_PASS=<yourdbpass>
- DB_DATABASE=bookstackapp

View File

@ -8,7 +8,7 @@ fi
# create directory structure
mkdir -p \
/config/www/{uploads,files,images,themes}
/config/www/{uploads,files,images,themes}
# check for .env and copy default if needed
if [[ ! -f "/config/www/.env" ]] || [[ ! -s "/config/www/.env" ]]; then
@ -16,22 +16,21 @@ if [[ ! -f "/config/www/.env" ]] || [[ ! -s "/config/www/.env" ]]; then
fi
# create symlinks
symlinks=( \
/app/www/themes \
/app/www/storage/uploads/files \
/app/www/storage/uploads/images \
/app/www/public/uploads \
/app/www/.env \
/app/www/storage/logs/laravel.log
symlinks=(
/app/www/themes
/app/www/storage/uploads/files
/app/www/storage/uploads/images
/app/www/public/uploads
/app/www/.env
/app/www/storage/logs/laravel.log
)
for i in "${symlinks[@]}"
do
if [[ -e "$i" && ! -L "$i" ]]; then
rm -rf "$i"
for i in "${symlinks[@]}"; do
if [[ -e "${i}" && ! -L "${i}" ]]; then
rm -rf "${i}"
fi
if [[ ! -L "$i" ]]; then
ln -s /config/www/"$(basename "$i")" "$i"
if [[ ! -L "${i}" ]]; then
ln -s /config/www/"$(basename "${i}")" "${i}"
fi
done
@ -41,32 +40,59 @@ if [ -n "${TEST_RUN}" ]; then
fi
# Create API key if needed
if [ ! -f "/config/BOOKSTACK_APP_KEY.txt" ];
then
if [ ! -f "/config/BOOKSTACK_APP_KEY.txt" ]; then
echo "Generating BookStack app key for first run"
key=$(php /app/www/artisan key:generate --show)
echo $key > /config/BOOKSTACK_APP_KEY.txt
echo "App Key set to $key you can modify the file to update /config/BOOKSTACK_APP_KEY.txt"
elif [ -f "/config/BOOKSTACK_APP_KEY.txt" ];
then
echo "${key}" >/config/BOOKSTACK_APP_KEY.txt
echo "App Key set to ${key} you can modify the file to update /config/BOOKSTACK_APP_KEY.txt"
elif [ -f "/config/BOOKSTACK_APP_KEY.txt" ]; then
echo "App Key found - setting variable for seds"
key=$(cat /config/BOOKSTACK_APP_KEY.txt)
fi
# .env file setup
# check for the default app key or if it has been updated
if grep -Fxq "APP_KEY=SomeRandomString" /config/www/.env || \
! grep -Fxq "APP_KEY=${key}" /config/www/.env; then
if ! grep -Fxq "APP_KEY=${key}" /config/www/.env; then
sed -i "s#^APP_KEY=.*#APP_KEY=${key}#" /config/www/.env
fi
# check to see if db_user is set, if it is then run seds and if not then leave them
if [ "${DB_USER}" ];
then
echo "Running config - db_user set"
sed -i "s/DB_HOST=.*/DB_HOST=${DB_HOST}/g" /config/www/.env
sed -i "s/DB_DATABASE=.*/DB_DATABASE=${DB_DATABASE}/g" /config/www/.env
sed -i "s/DB_USERNAME=.*/DB_USERNAME=${DB_USER}/g" /config/www/.env
sed -i "s/DB_PASSWORD=.*/DB_PASSWORD=${DB_PASS}/g" /config/www/.env
# if DB_HOST contains a port and DB_HOST is not a IPv6 without brackets [..]
# support ipv4:port, [ipv6]:port, and domain:port
if [[ ${DB_HOST} =~ :[0-9]+$ ]] && ! [[ ${DB_HOST} =~ ^(:{0,2}[a-fA-F0-9]{1,4})+$ ]]; then
DB_HOST_PORT="${DB_HOST}"
fi
# if DB_HOST_PORT is set
if [[ -n "${DB_HOST_PORT}" ]]; then
# if DB_PORT is set
if [[ -n "${DB_PORT}" ]]; then
echo "DB_PORT is not supported when using DB_HOST with port"
sleep infinity
fi
DB_HOST="${DB_HOST_PORT%:*}"
DB_PORT="${DB_HOST_PORT##*:}"
fi
# if DB_PORT is not set
if [[ -z "${DB_PORT}" ]]; then
DB_PORT="3306"
fi
# check to see if DB_HOST is set, if it is then run seds and if not then leave them
if [[ -n "${DB_HOST}" ]]; then
echo "Running config - DB_HOST set"
if ! grep -xqE "^[#]?DB_PORT=.*" /config/www/.env; then
# add DB_PORT line to /config/www/.env because current /app/www/.env.example doesn't have it
sed -i -E "/^[#]?DB_HOST=.*/a DB_PORT=${DB_PORT}" /config/www/.env
echo "**** Insert DB_PORT=${DB_PORT} into /config/www/.env ****"
fi
sed -i -E "s/^[#]?DB_HOST=.*/DB_HOST=${DB_HOST}/g" /config/www/.env
sed -i -E "s/^[#]?DB_PORT=.*/DB_PORT=${DB_PORT}/g" /config/www/.env
sed -i -E "s/^[#]?DB_DATABASE=.*/DB_DATABASE=${DB_DATABASE}/g" /config/www/.env
sed -i -E "s/^[#]?DB_USERNAME=.*/DB_USERNAME=${DB_USER}/g" /config/www/.env
sed -i -E "s/^[#]?DB_PASSWORD=.*/DB_PASSWORD=${DB_PASS}/g" /config/www/.env
fi
# set appurl
@ -89,18 +115,18 @@ fi
## Bump php upload max filesize and post max size to 100MB by default
if ! grep -qx '^upload_max_filesize.*$' /config/php/php-local.ini; then
echo 'upload_max_filesize = 100M' >> /config/php/php-local.ini
echo 'upload_max_filesize = 100M' >>/config/php/php-local.ini
fi
if ! grep -qx '^post_max_size.*$' /config/php/php-local.ini; then
echo 'post_max_size = 100M' >> /config/php/php-local.ini
echo 'post_max_size = 100M' >>/config/php/php-local.ini
fi
# check for the mysql endpoint for 30 seconds
END=$((SECONDS+30))
while [ ${SECONDS} -lt ${END} ] && [ -n "${DB_HOST+x}" ]; do
if /usr/bin/nc -z ${DB_HOST} 3306; then
if [ ! -z "$(/usr/bin/nc -w1 ${DB_HOST} 3306)" ]; then
if [ ! -z "${RUN}" ]; then
END=$((SECONDS + 30))
while [ ${SECONDS} -lt ${END} ] && [ -n "${DB_HOST}" ]; do
if /usr/bin/nc -z "${DB_HOST}" "${DB_PORT}"; then
if [ -n "$(/usr/bin/nc -w1 "${DB_HOST}" "${DB_PORT}")" ]; then
if [ -n "${RUN}" ]; then
break
fi
RUN="RAN"