88
99# create directory structure
1010mkdir -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
1414if [[ ! -f "/config/www/.env" ]] || [[ ! -s "/config/www/.env" ]]; then
1515 cp /app/www/.env.example /config/www/.env
1616fi
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
3635done
3736
@@ -41,32 +40,59 @@ if [ -n "${TEST_RUN}" ]; then
4140fi
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)
5451fi
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
6157fi
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
7096fi
7197
7298# set appurl
89115
90116## Bump php upload max filesize and post max size to 100MB by default
91117if ! 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
93119fi
94120if ! 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
96122fi
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