Skip to content

Extract host and port from DB_HOST also support DB_PORT to test connection instead of only use default port 3306 #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Dec 22, 2022
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
6 changes: 4 additions & 2 deletions readme-vars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
100 changes: 63 additions & 37 deletions root/etc/cont-init.d/50-config
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,29 @@ 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
cp /app/www/.env.example /config/www/.env
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

Expand All @@ -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
Expand All @@ -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"
Expand Down