Skip to content

Commit 16c3c51

Browse files
authored
Rework logic to account for ipv6 (#1)
Also, always set host and port
1 parent 014fb2a commit 16c3c51

File tree

1 file changed

+62
-57
lines changed

1 file changed

+62
-57
lines changed

root/etc/cont-init.d/50-config

Lines changed: 62 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,29 @@ fi
88

99
# create directory structure
1010
mkdir -p \
11-
/config/www/{uploads,files,images,themes}
11+
/config/www/{uploads,files,images,themes}
1212

1313
# check for .env and copy default if needed
1414
if [[ ! -f "/config/www/.env" ]] || [[ ! -s "/config/www/.env" ]]; then
1515
cp /app/www/.env.example /config/www/.env
1616
fi
1717

1818
# create symlinks
19-
symlinks=( \
20-
/app/www/themes \
21-
/app/www/storage/uploads/files \
22-
/app/www/storage/uploads/images \
23-
/app/www/public/uploads \
24-
/app/www/.env \
25-
/app/www/storage/logs/laravel.log
19+
symlinks=(
20+
/app/www/themes
21+
/app/www/storage/uploads/files
22+
/app/www/storage/uploads/images
23+
/app/www/public/uploads
24+
/app/www/.env
25+
/app/www/storage/logs/laravel.log
2626
)
2727

28-
for i in "${symlinks[@]}"
29-
do
30-
if [[ -e "$i" && ! -L "$i" ]]; then
31-
rm -rf "$i"
28+
for i in "${symlinks[@]}"; do
29+
if [[ -e "${i}" && ! -L "${i}" ]]; then
30+
rm -rf "${i}"
3231
fi
33-
if [[ ! -L "$i" ]]; then
34-
ln -s /config/www/"$(basename "$i")" "$i"
32+
if [[ ! -L "${i}" ]]; then
33+
ln -s /config/www/"$(basename "${i}")" "${i}"
3534
fi
3635
done
3736

@@ -41,42 +40,58 @@ if [ -n "${TEST_RUN}" ]; then
4140
fi
4241

4342
# Create API key if needed
44-
if [ ! -f "/config/BOOKSTACK_APP_KEY.txt" ];
45-
then
43+
if [ ! -f "/config/BOOKSTACK_APP_KEY.txt" ]; then
4644
echo "Generating BookStack app key for first run"
4745
key=$(php /app/www/artisan key:generate --show)
48-
echo $key > /config/BOOKSTACK_APP_KEY.txt
49-
echo "App Key set to $key you can modify the file to update /config/BOOKSTACK_APP_KEY.txt"
50-
elif [ -f "/config/BOOKSTACK_APP_KEY.txt" ];
51-
then
46+
echo "${key}" >/config/BOOKSTACK_APP_KEY.txt
47+
echo "App Key set to ${key} you can modify the file to update /config/BOOKSTACK_APP_KEY.txt"
48+
elif [ -f "/config/BOOKSTACK_APP_KEY.txt" ]; then
5249
echo "App Key found - setting variable for seds"
5350
key=$(cat /config/BOOKSTACK_APP_KEY.txt)
5451
fi
5552

5653
# .env file setup
5754
# check for the default app key or if it has been updated
58-
if grep -Fxq "APP_KEY=SomeRandomString" /config/www/.env || \
59-
! grep -Fxq "APP_KEY=${key}" /config/www/.env; then
55+
if ! grep -Fxq "APP_KEY=${key}" /config/www/.env; then
6056
sed -i "s#^APP_KEY=.*#APP_KEY=${key}#" /config/www/.env
6157
fi
62-
# check to see if db_user is set, if it is then run seds and if not then leave them
63-
if [ "${DB_USER}" ];
64-
then
65-
echo "Running config - db_user set"
66-
sed -i "s/^DB_HOST=.*/DB_HOST=${DB_HOST}/g" /config/www/.env
67-
sed -i "s/^DB_DATABASE=.*/DB_DATABASE=${DB_DATABASE}/g" /config/www/.env
68-
sed -i "s/^DB_USERNAME=.*/DB_USERNAME=${DB_USER}/g" /config/www/.env
69-
sed -i "s/^DB_PASSWORD=.*/DB_PASSWORD=${DB_PASS}/g" /config/www/.env
70-
71-
if [ -n "${DB_PORT}" ]; then
72-
if ! grep -xq "^DB_PORT=.*" /config/www/.env; then
73-
# add line DB_PORT=3306 to /config/www/.env because current /app/www/.env.example dont have it
74-
sed -i "/^DB_HOST=.*/a DB_PORT=${DB_PORT}" /config/www/.env
75-
echo "**** Insert DB_PORT=${DB_PORT} into /config/www/.env ****"
76-
else
77-
sed -i "s/^DB_PORT=.*/DB_PORT=${DB_PORT}/g" /config/www/.env
78-
fi
79-
fi
58+
59+
# if DB_HOST contains a port
60+
if echo "${DB_HOST}" | grep -qP '^(?:[0-9.]+|(?:\[[0-9a-fA-F:]+\]))(:[0-9]+)$'; then
61+
DB_HOST_PORT="${DB_HOST}"
62+
fi
63+
64+
# if DB_HOST_PORT is set
65+
if [[ -n "${DB_HOST_PORT}" ]]; then
66+
# if DB_PORT is set
67+
if [[ -n "${DB_PORT}" ]]; then
68+
echo "DB_PORT is not supported when using DB_HOST with port"
69+
sleep infinity
70+
fi
71+
DB_HOST="${DB_HOST_PORT%:*}"
72+
DB_PORT="${DB_HOST_PORT##*:}"
73+
fi
74+
75+
# if DB_PORT is not set
76+
if [[ -z "${DB_PORT}" ]]; then
77+
DB_PORT="3306"
78+
fi
79+
80+
# check to see if DB_HOST is set, if it is then run seds and if not then leave them
81+
if [[ -n "${DB_HOST}" ]]; then
82+
echo "Running config - DB_HOST set"
83+
84+
if ! grep -xq "^DB_PORT=.*" /config/www/.env; then
85+
# add DB_PORT line to /config/www/.env because current /app/www/.env.example doesn't have it
86+
sed -i "/^DB_HOST=.*/a DB_PORT=${DB_PORT}" /config/www/.env
87+
echo "**** Insert DB_PORT=${DB_PORT} into /config/www/.env ****"
88+
fi
89+
90+
sed -i "s/^DB_HOST=.*/DB_HOST=${DB_HOST}/g" /config/www/.env
91+
sed -i "s/^DB_PORT=.*/DB_PORT=${DB_PORT}/g" /config/www/.env
92+
sed -i "s/^DB_DATABASE=.*/DB_DATABASE=${DB_DATABASE}/g" /config/www/.env
93+
sed -i "s/^DB_USERNAME=.*/DB_USERNAME=${DB_USER}/g" /config/www/.env
94+
sed -i "s/^DB_PASSWORD=.*/DB_PASSWORD=${DB_PASS}/g" /config/www/.env
8095
fi
8196

8297
# set appurl
@@ -99,28 +114,18 @@ fi
99114

100115
## Bump php upload max filesize and post max size to 100MB by default
101116
if ! grep -qx '^upload_max_filesize.*$' /config/php/php-local.ini; then
102-
echo 'upload_max_filesize = 100M' >> /config/php/php-local.ini
117+
echo 'upload_max_filesize = 100M' >>/config/php/php-local.ini
103118
fi
104119
if ! grep -qx '^post_max_size.*$' /config/php/php-local.ini; then
105-
echo 'post_max_size = 100M' >> /config/php/php-local.ini
120+
echo 'post_max_size = 100M' >>/config/php/php-local.ini
106121
fi
107122

108-
# extract actual host and port from DB_HOST endpoint format, not support IPv6
109-
# DB_HOST enpoint 'domainIp:port' or 'domainIp'
110-
# DB_HOST_ONLY drop ':port' portion
111-
# DB_PORT_ONLY drop host_only portion, remains '' or ':port'
112-
# DB_PORT_ONLY drop ':' if any, remain '' or 'port'
113-
# ${DB_PORT_ONLY:-${DB_PORT:-3306}} use DB_PORT if no port provided in DB_HOST, use default 3306 if not provide DB_PORT
114-
DB_HOST_ONLY=${DB_HOST%:*}
115-
DB_PORT_ONLY=${DB_HOST#$DB_HOST_ONLY}
116-
DB_PORT_ONLY=${DB_PORT_ONLY#:}
117-
118123
# check for the mysql endpoint for 30 seconds
119-
END=$((SECONDS+30))
120-
while [ ${SECONDS} -lt ${END} ] && [ -n "${DB_HOST_ONLY+x}" ]; do
121-
if /usr/bin/nc -z ${DB_HOST_ONLY} ${DB_PORT_ONLY:-${DB_PORT:-3306}}; then
122-
if [ ! -z "$(/usr/bin/nc -w1 ${DB_HOST_ONLY} ${DB_PORT_ONLY:-${DB_PORT:-3306}})" ]; then
123-
if [ ! -z "${RUN}" ]; then
124+
END=$((SECONDS + 30))
125+
while [ ${SECONDS} -lt ${END} ] && [ -n "${DB_HOST}" ]; do
126+
if /usr/bin/nc -z "${DB_HOST}" "${DB_PORT}"; then
127+
if [ -n "$(/usr/bin/nc -w1 "${DB_HOST}" "${DB_PORT}")" ]; then
128+
if [ -n "${RUN}" ]; then
124129
break
125130
fi
126131
RUN="RAN"

0 commit comments

Comments
 (0)