-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
docker-entrypoint.sh
264 lines (219 loc) · 9.54 KB
/
docker-entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/bin/bash
# Default values for admin user and password
DEFAULT_ADMIN_EMAIL="admin@example.com"
DEFAULT_ADMIN_PASSWORD=$(openssl rand -base64 12) # Generate a random password
# Default values for environment variables
DEFAULT_APP_BASE_DIR=""
DEFAULT_APP_DEBUG=true
DEFAULT_APP_LANG="en"
DEFAULT_APP_WORKSPACE="My plant home"
DEFAULT_APP_ONLINE_MINUTE_LIMIT=5
DEFAULT_APP_OVERDUE_TASK_HOURS=10
DEFAULT_APP_CRON_PW=$(openssl rand -base64 12)
DEFAULT_APP_CRON_MAIL_LIMIT=5
DEFAULT_LOG_ENABLE=true
DEFAULT_SMTP_FROMNAME="Test"
DEFAULT_SMTP_FROMADDRESS="test@domain.tld"
DEFAULT_SMTP_HOST=""
DEFAULT_SMTP_PORT=587
DEFAULT_SMTP_USERNAME=""
DEFAULT_SMTP_PASSWORD=""
DEFAULT_SMTP_ENCRYPTION="tls"
# Use environment variables if provided, otherwise use defaults
ADMIN_EMAIL="${APP_ADMIN_EMAIL:-$DEFAULT_ADMIN_EMAIL}"
ADMIN_PASSWORD="${APP_ADMIN_PASSWORD:-$DEFAULT_ADMIN_PASSWORD}"
APP_BASE_DIR="${APP_BASE_DIR:-$DEFAULT_APP_BASE_DIR}"
APP_DEBUG=${APP_DEBUG:-$DEFAULT_APP_DEBUG}
APP_LANG="${APP_LANG:-$DEFAULT_APP_LANG}"
APP_WORKSPACE="${APP_WORKSPACE:-$DEFAULT_APP_WORKSPACE}"
APP_ONLINE_MINUTE_LIMIT=${APP_ONLINE_MINUTE_LIMIT:-$DEFAULT_APP_ONLINE_MINUTE_LIMIT}
APP_OVERDUE_TASK_HOURS=${APP_OVERDUE_TASK_HOURS:-$DEFAULT_APP_OVERDUE_TASK_HOURS}
APP_CRON_PW="${APP_CRON_PW:-$DEFAULT_APP_CRON_PW}"
APP_CRON_MAIL_LIMIT=${APP_CRON_MAIL_LIMIT:-$DEFAULT_APP_CRON_MAIL_LIMIT}
LOG_ENABLE={$LOG_ENABLE:-$DEFAULT_LOG_ENABLE}
SMTP_FROMNAME="${SMTP_FROMNAME:-$DEFAULT_SMTP_FROMNAME}"
SMTP_FROMADDRESS="${SMTP_FROMADDRESS:-$DEFAULT_SMTP_FROMADDRESS}"
SMTP_HOST="${SMTP_HOST:-$DEFAULT_SMTP_HOST}"
SMTP_PORT=${SMTP_PORT:-$DEFAULT_SMTP_PORT}
SMTP_USERNAME="${SMTP_USERNAME:-$DEFAULT_SMTP_USERNAME}"
SMTP_PASSWORD="${SMTP_PASSWORD:-$DEFAULT_SMTP_PASSWORD}"
SMTP_ENCRYPTION="${SMTP_ENCRYPTION:-$DEFAULT_SMTP_ENCRYPTION}"
# Function to set PHP error reporting based on APP_DEBUG
configure_php_error_reporting() {
if [ "$APP_DEBUG" = "false" ]; then
# Suppress warnings and notices if APP_DEBUG is false
echo 'error_reporting = E_ALL & ~E_NOTICE & ~E_WARNING & ~E_DEPRECATED' > /usr/local/etc/php/conf.d/errors.ini
echo 'display_errors = Off' >> /usr/local/etc/php/conf.d/errors.ini
else
# Show all errors if APP_DEBUG is true
echo 'error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT' > /usr/local/etc/php/conf.d/errors.ini
echo 'display_errors = On' >> /usr/local/etc/php/conf.d/errors.ini
fi
}
# Function to create the .env file
create_environment_file() {
rm -f "/var/www/html/.env"
cat <<-EOF >> /var/www/html/.env
# App settings
APP_NAME="HortusFox"
APP_DEBUG=$APP_DEBUG
APP_BASEDIR="$APP_BASE_DIR"
APP_LANG="$APP_LANG"
APP_WORKSPACE="$APP_WORKSPACE"
APP_ENABLESCROLLER="$APP_ENABLE_SCROLLER"
APP_OVERLAYALPHA="$APP_OVERLAY_ALPHA"
APP_ENABLECHAT="$APP_ENABLE_CHAT"
APP_ONLINEMINUTELIMIT="$APP_ONLINE_MINUTE_LIMIT"
APP_SHOWCHATONLINEUSERS="$APP_SHOW_CHAT_ONLINE_USERS"
APP_SHOWCHATTYPINGINDICATOR="$APP_SHOW_CHAT_TYPING_INDICATOR"
APP_OVERDUETASK_HOURS=$APP_OVERDUE_TASK_HOURS
APP_CRONPW="$APP_CRON_PW"
APP_CRONJOB_MAILLIMIT=$APP_CRON_MAIL_LIMIT
APP_GITHUB_URL="https://github.com/danielbrendel/hortusfox-web"
APP_GITHUB_SPONSOR="https://github.com/sponsors/danielbrendel"
APP_DONATION_KOFI="https://ko-fi.com/danielbrendel"
APP_SERVICE_URL="https://www.hortusfox.com"
APP_SOCIAL_DISCORD="https://discord.gg/kc6xGmjzVS"
APP_SOCIAL_MASTODON="https://mastodon.social/@hortusfox"
APP_SOCIAL_YOUTUBE="https://www.youtube.com/@HortusFox"
APP_SOCIAL_INSTAGRAM="https://www.instagram.com/hortusfox/"
APP_ENABLEHISTORY=$APP_ENABLE_HISTORY
APP_HISTORY_NAME="$APP_HISTORY_NAME"
# Session
SESSION_ENABLE=true
SESSION_DURATION=31536000
SESSION_NAME=null
# Photo resize factors
PHOTO_RESIZE_FACTOR_DEFAULT=$PHOTO_RESIZE_FACTOR_DEFAULT
PHOTO_RESIZE_FACTOR_1=$PHOTO_RESIZE_FACTOR_1
PHOTO_RESIZE_FACTOR_2=$PHOTO_RESIZE_FACTOR_2
PHOTO_RESIZE_FACTOR_3=$PHOTO_RESIZE_FACTOR_3
PHOTO_RESIZE_FACTOR_4=$PHOTO_RESIZE_FACTOR_4
PHOTO_RESIZE_FACTOR_5=$PHOTO_RESIZE_FACTOR_5
# Database settings
DB_ENABLE=true
DB_HOST="$DB_HOST"
DB_USER="$DB_USERNAME"
DB_PASSWORD="$DB_PASSWORD"
DB_PORT=$DB_PORT
DB_DATABASE="$DB_DATABASE"
DB_DRIVER=mysql
DB_CHARSET="$DB_CHARSET"
# SMTP Settings
SMTP_FROMNAME="$SMTP_FROMNAME"
SMTP_FROMADDRESS="$SMTP_FROMADDRESS"
SMTP_HOST="$SMTP_HOST"
SMTP_PORT=$SMTP_PORT
SMTP_USERNAME="$SMTP_USERNAME"
SMTP_PASSWORD="$SMTP_PASSWORD"
SMTP_ENCRYPTION="$SMTP_ENCRYPTION"
# Logging
LOG_ENABLE=$LOG_ENABLE
EOF
}
# Function to check if initial settings were created and add if not
add_initial_settings_if_missing() {
local settings_count=$(mysql -u "$DB_USERNAME" -p"$DB_PASSWORD" -h "$DB_HOST" -P "$DB_PORT" -D "$DB_DATABASE" -N -s -e "SELECT COUNT(*) FROM AppModel WHERE id=1;")
if [[ $settings_count -gt 0 ]]; then
echo "App settings profile already exists. Skipping creation."
else
echo "App settings profile does not exist. Creating..."
create_app_settings
fi
}
# Function to create initial settings
create_app_settings() {
mysql -u "$DB_USERNAME" -p"$DB_PASSWORD" -h "$DB_HOST" -P "$DB_PORT" -D "$DB_DATABASE" -e "INSERT INTO AppModel (id, workspace, language, cronjob_pw, smtp_fromname, smtp_fromaddress, smtp_host, smtp_port, smtp_username, smtp_password, smtp_encryption, created_at) VALUES (NULL, '$APP_WORKSPACE', '$APP_LANG', '$APP_CRON_PW', '$SMTP_FROMNAME', '$SMTP_FROMADDRESS', '$SMTP_HOST', $SMTP_PORT, '$SMTP_USERNAME', '$SMTP_PASSWORD', '$SMTP_ENCRYPTION', CURRENT_TIMESTAMP);"
echo "App settings profile created."
}
# Function to check if the admin user exists and add if not
add_admin_user_if_missing() {
local user_count=$(mysql -u "$DB_USERNAME" -p"$DB_PASSWORD" -h "$DB_HOST" -P "$DB_PORT" -D "$DB_DATABASE" -N -s -e "SELECT COUNT(*) FROM users WHERE email='$ADMIN_EMAIL';")
if [[ $user_count -gt 0 ]]; then
echo "Admin user ($ADMIN_EMAIL) already exists. Skipping user creation."
else
echo "Admin user ($ADMIN_EMAIL) does not exist. Creating..."
create_admin_user
fi
}
# Function to create an admin user
create_admin_user() {
# Use PHP to hash the password
local hashed_password=$(php -r "echo password_hash('$ADMIN_PASSWORD', PASSWORD_BCRYPT);")
# Insert the new admin user into the database
mysql -u "$DB_USERNAME" -p"$DB_PASSWORD" -h "$DB_HOST" -P "$DB_PORT" -D "$DB_DATABASE" -e "INSERT INTO users (id, name, email, password, password_reset, admin, lang, chatcolor, show_log, show_plants_aoru, notify_tasks_overdue, notify_tasks_tomorrow, last_seen_msg, last_typing, last_action, created_at) VALUES (NULL, 'Admin', '$ADMIN_EMAIL', '$hashed_password', NULL, 1, NULL, NULL, 1, 1, 1, 1, NULL, NULL, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);"
echo "Admin user created. Username: $ADMIN_EMAIL. Password: **HIDDEN**"
}
set_apache_server_name() {
if [ -n "$APACHE_SERVER_NAME" ]; then
echo "ServerName $APACHE_SERVER_NAME" >> /etc/apache2/apache2.conf;
fi
}
# Function to check DB connection
check_db() {
mysql -u "$DB_USERNAME" -p"$DB_PASSWORD" -h "$DB_HOST" -P "$DB_PORT" -D "$DB_DATABASE" -N -s -e "SELECT 1;" > /dev/null 2>&1
}
# Function to wait for the database
wait_for_db() {
local delay=5 # delay in seconds
local attempt=1
while ! check_db; do
echo "Waiting for database to be available... Attempt $attempt"
attempt=$((attempt+1))
sleep "$delay"
done
echo "Database is available."
}
# Configure PHP error reporting
configure_php_error_reporting
# Create .env configuration file
create_environment_file
# To get rid of apache warnings, you can set the server name with the env var.
set_apache_server_name
# Call the wait_for_db function
wait_for_db
# Copy migration content
cp /tmp/migrations/* /var/www/html/app/migrations
# Set permissions to folder for migrations
chown -R www-data:www-data /var/www/html/app/migrations
# Show product version
php asatru product:version
# Run database migrations
if [ $(mysql -u "$DB_USERNAME" -p"$DB_PASSWORD" -h "$DB_HOST" -P "$DB_PORT" -D "$DB_DATABASE" -N -s -e "SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA = '$DB_DATABASE' AND TABLE_NAME = 'plants';") -eq 1 ]
then
echo "Running unapplied database migrations..."
php asatru migrate:list
php asatru migrate:upgrade
else
echo "Running full database migrations..."
php asatru migrate:fresh
fi
# Check if app settings profile exists and create it if not.
add_initial_settings_if_missing
# Check if admin user exists and create it if not.
add_admin_user_if_missing
# Add default calendar classes if missing
php asatru calendar:classes
# Add default plant attributes if missing
php asatru plants:attributes
# Copy default images
cp /tmp/img/* /var/www/html/public/img
# Set permissions to folders for images
chown -R www-data:www-data /var/www/html/public/img
chmod 755 /var/www/html/public/img
# Set permissions to folder for logs
chown -R www-data:www-data /var/www/html/app/logs
# Set permissions to folder for backups
chown -R www-data:www-data /var/www/html/public/backup
chmod 755 /var/www/html/public/backup
# Copy themes content
cp -r /tmp/themes/* /var/www/html/public/themes
# Set permissions to folder for themes
chown -R www-data:www-data /var/www/html/public/themes
# Set permissions to public folder
chown -R www-data:www-data /var/www/html/public
chmod 755 /var/www/html/public
# Print informational message
echo -e "\033[32mThe system is now ready for operation.\033[39m"
# Then exec the container's main process (CMD)
exec "$@"