Skip to content

Commit c075b28

Browse files
authored
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
2 parents a27574b + e2bd737 commit c075b28

File tree

2 files changed

+67
-39
lines changed

2 files changed

+67
-39
lines changed

readme-vars.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ param_usage_include_env: true
2727
param_env_vars:
2828
- { 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`"}
2929
- { env_var: "DB_HOST", env_value: "<yourdbhost>", desc: "for specifying the database host" }
30+
- { env_var: "DB_PORT", env_value: "<yourdbport>", desc: "for specifying the database port if not default 3306" }
3031
- { env_var: "DB_USER", env_value: "<yourdbuser>", desc: "for specifying the database user" }
31-
- { env_var: "DB_PASS", env_value: "<yourdbpass>", desc: "for specifying the database password" }
32-
- { env_var: "DB_DATABASE", env_value: "bookstackapp", desc: "for specifying the database to be used (non-alphanumeric passwords must be properly escaped.)" }
32+
- { env_var: "DB_PASS", env_value: "<yourdbpass>", desc: "for specifying the database password (non-alphanumeric passwords must be properly escaped.)" }
33+
- { env_var: "DB_DATABASE", env_value: "bookstackapp", desc: "for specifying the database to be used" }
3334

3435
param_usage_include_ports: true
3536
param_ports:
@@ -50,6 +51,7 @@ custom_compose: |
5051
- PGID=1000
5152
- APP_URL=
5253
- DB_HOST=bookstack_db
54+
- DB_PORT=3306
5355
- DB_USER=bookstack
5456
- DB_PASS=<yourdbpass>
5557
- DB_DATABASE=bookstackapp

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

Lines changed: 63 additions & 37 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,32 +40,59 @@ 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
58+
59+
# if DB_HOST contains a port and DB_HOST is not a IPv6 without brackets [..]
60+
# support ipv4:port, [ipv6]:port, and domain:port
61+
if [[ ${DB_HOST} =~ :[0-9]+$ ]] && ! [[ ${DB_HOST} =~ ^(:{0,2}[a-fA-F0-9]{1,4})+$ ]]; then
62+
DB_HOST_PORT="${DB_HOST}"
63+
fi
64+
65+
# if DB_HOST_PORT is set
66+
if [[ -n "${DB_HOST_PORT}" ]]; then
67+
# if DB_PORT is set
68+
if [[ -n "${DB_PORT}" ]]; then
69+
echo "DB_PORT is not supported when using DB_HOST with port"
70+
sleep infinity
71+
fi
72+
DB_HOST="${DB_HOST_PORT%:*}"
73+
DB_PORT="${DB_HOST_PORT##*:}"
74+
fi
75+
76+
# if DB_PORT is not set
77+
if [[ -z "${DB_PORT}" ]]; then
78+
DB_PORT="3306"
79+
fi
80+
81+
# check to see if DB_HOST is set, if it is then run seds and if not then leave them
82+
if [[ -n "${DB_HOST}" ]]; then
83+
echo "Running config - DB_HOST set"
84+
85+
if ! grep -xqE "^[#]?DB_PORT=.*" /config/www/.env; then
86+
# add DB_PORT line to /config/www/.env because current /app/www/.env.example doesn't have it
87+
sed -i -E "/^[#]?DB_HOST=.*/a DB_PORT=${DB_PORT}" /config/www/.env
88+
echo "**** Insert DB_PORT=${DB_PORT} into /config/www/.env ****"
89+
fi
90+
91+
sed -i -E "s/^[#]?DB_HOST=.*/DB_HOST=${DB_HOST}/g" /config/www/.env
92+
sed -i -E "s/^[#]?DB_PORT=.*/DB_PORT=${DB_PORT}/g" /config/www/.env
93+
sed -i -E "s/^[#]?DB_DATABASE=.*/DB_DATABASE=${DB_DATABASE}/g" /config/www/.env
94+
sed -i -E "s/^[#]?DB_USERNAME=.*/DB_USERNAME=${DB_USER}/g" /config/www/.env
95+
sed -i -E "s/^[#]?DB_PASSWORD=.*/DB_PASSWORD=${DB_PASS}/g" /config/www/.env
7096
fi
7197

7298
# set appurl
@@ -89,18 +115,18 @@ fi
89115

90116
## Bump php upload max filesize and post max size to 100MB by default
91117
if ! grep -qx '^upload_max_filesize.*$' /config/php/php-local.ini; then
92-
echo 'upload_max_filesize = 100M' >> /config/php/php-local.ini
118+
echo 'upload_max_filesize = 100M' >>/config/php/php-local.ini
93119
fi
94120
if ! grep -qx '^post_max_size.*$' /config/php/php-local.ini; then
95-
echo 'post_max_size = 100M' >> /config/php/php-local.ini
121+
echo 'post_max_size = 100M' >>/config/php/php-local.ini
96122
fi
97123

98124
# check for the mysql endpoint for 30 seconds
99-
END=$((SECONDS+30))
100-
while [ ${SECONDS} -lt ${END} ] && [ -n "${DB_HOST+x}" ]; do
101-
if /usr/bin/nc -z ${DB_HOST} 3306; then
102-
if [ ! -z "$(/usr/bin/nc -w1 ${DB_HOST} 3306)" ]; then
103-
if [ ! -z "${RUN}" ]; then
125+
END=$((SECONDS + 30))
126+
while [ ${SECONDS} -lt ${END} ] && [ -n "${DB_HOST}" ]; do
127+
if /usr/bin/nc -z "${DB_HOST}" "${DB_PORT}"; then
128+
if [ -n "$(/usr/bin/nc -w1 "${DB_HOST}" "${DB_PORT}")" ]; then
129+
if [ -n "${RUN}" ]; then
104130
break
105131
fi
106132
RUN="RAN"

0 commit comments

Comments
 (0)