Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·370 lines (303 loc) · 11.9 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·370 lines (303 loc) · 11.9 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/bin/bash
if [ -f ".env" ]; then
echo "Already installed. If you want to change settings please modify the file .env manually."
exit 0
fi
opensslBin=""
dockerBin=""
dockerComposeBin=""
isAvailable() {
tmp=$(which echo $1)
if [ $? -ne 0 ]
then
echo "docker and openssl must be installed!"
exit 1
fi
}
isPluginAvailable() {
tmp=$($dockerBin $1 > /dev/null 2>&1)
if [ $? -ne 0 ]
then
echo "docker compose plugin must be installed!"
exit 1
fi
}
createCron() {
local cronDirectory="${2:-/etc/cron.d}"
local cronName="$1"
local sourceCron="$PWD/cron.d/$cronName"
local targetCron="$cronDirectory/$cronName"
if [ ! -d "$cronDirectory" ]; then
echo "Could not create cronjob. Path $cronDirectory does not exist."
return 1
fi
if [ ! -f "$sourceCron" ]; then
echo "Could not create cronjob. Source file $sourceCron does not exist."
return 1
fi
if [ -L "$targetCron" ]; then
if [ "$(readlink "$targetCron")" = "$sourceCron" ]; then
echo "Cronjob $targetCron already exists."
return 0
fi
if ln -sfn "$sourceCron" "$targetCron"; then
echo "Cronjob symlink $targetCron was updated."
return 0
fi
echo "Could not update symlink $targetCron. Do you have permission to write there?"
return 1
fi
if [ -e "$targetCron" ]; then
echo "Cronjob target $targetCron already exists and is not a symlink. Keeping it unchanged."
return 0
fi
if ! ln -s "$sourceCron" "$targetCron"; then
echo "Could not create symlink $targetCron. Do you have permission to write there?"
return 1
fi
echo "A cronjob was created in $targetCron."
}
isValidPort() {
local port="$1"
[[ "$port" =~ ^[0-9]+$ ]] && [ "${#port}" -le 5 ] && ((10#$port >= 1 && 10#$port <= 65535))
}
# Returns 0 when a TCP port is in use and 1 when it is free. Docker mappings
# and socket inspection tools are preferred; Bash TCP connections are the fallback.
isPortInUse() {
local port="$1"
local listeners=""
local publishedPorts=""
if [ -n "$dockerBin" ] && publishedPorts=$("$dockerBin" ps --format '{{.Ports}}' 2>/dev/null); then
if printf '%s\n' "$publishedPorts" | awk -v port="$port" '
index($0, ":" port "->") { found = 1 }
END { exit(found ? 0 : 1) }
'; then
return 0
fi
fi
if command -v ss > /dev/null 2>&1 && listeners=$(ss -H -ltn 2>/dev/null); then
printf '%s\n' "$listeners" | awk -v port="$port" '
$4 ~ ("[.:]" port "$") { found = 1 }
END { exit(found ? 0 : 1) }
'
return $?
fi
if command -v lsof > /dev/null 2>&1; then
lsof -nP -iTCP:"$port" -sTCP:LISTEN -t > /dev/null 2>&1
return $?
fi
if command -v netstat > /dev/null 2>&1 && listeners=$(netstat -an 2>/dev/null); then
printf '%s\n' "$listeners" | awk -v port="$port" '
toupper($0) ~ /LISTEN/ && $4 ~ ("[.:]" port "$") { found = 1 }
END { exit(found ? 0 : 1) }
'
return $?
fi
if (: > "/dev/tcp/127.0.0.1/$port") 2>/dev/null; then
return 0
fi
if (: > "/dev/tcp/::1/$port") 2>/dev/null; then
return 0
fi
return 1
}
configureHostPort() {
local resultVariable="$1"
local label="$2"
local defaultPort="$3"
local alternativePort="$4"
local excludedPort="${5:-}"
local selectedPort=""
local checkStatus=0
isPortInUse "$defaultPort" || checkStatus=$?
if [ "$checkStatus" -eq 1 ]; then
printf -v "$resultVariable" '%s' "$defaultPort"
echo "$label port $defaultPort is available."
return 0
fi
echo "$label port $defaultPort is already in use."
while true; do
read -r -p "Please enter an available host port for $label [$alternativePort]:" selectedPort
selectedPort="${selectedPort:-$alternativePort}"
if ! isValidPort "$selectedPort"; then
echo "Please enter a port between 1 and 65535."
continue
fi
if [ -n "$excludedPort" ] && [ "$selectedPort" = "$excludedPort" ]; then
echo "$label must use a different port than $excludedPort."
continue
fi
checkStatus=0
isPortInUse "$selectedPort" || checkStatus=$?
if [ "$checkStatus" -eq 0 ]; then
echo "Port $selectedPort is already in use. Please choose another port."
continue
fi
printf -v "$resultVariable" '%s' "$selectedPort"
return 0
done
}
checkRequirements(){
isAvailable "openssl"
opensslBin=$(which openssl)
isAvailable "docker"
dockerBin=$(which docker)
isPluginAvailable "compose version"
dockerComposeBin="$dockerBin compose"
}
checkRequirements
echo "This script will guide you through the installation of the tool."
# use .env.dist as template and replace specific values during script execution
umask 0177
envTemplate=.env.dist
envTmp=.env.tmp
envEnd=.env
cp $envTemplate $envTmp
########## setup host name ##########
pveHostDefault=$(hostname)
pveHost=""
read -p "Please enter the host name of your server [$pveHostDefault]:" pveHost
pveHost="${pveHost:-${pveHostDefault}}"
$(sed "s/HOST_NAME=fewohbee/HOST_NAME=$pveHost/" $envTmp > $envTmp.tmp && mv $envTmp.tmp $envTmp)
$(sed "s/RELYING_PARTY_ID=example.com/RELYING_PARTY_ID=$pveHost/" $envTmp > $envTmp.tmp && mv $envTmp.tmp $envTmp)
########## setup certificate ##########
sslDefault="self-signed"
ssl=""
while ! [[ "$ssl" =~ ^(self-signed|letsencrypt|reverse-proxy)$ ]]
do
read -p "SSL Certificate: Using self-signed, letsencrypt or reverse-proxy? [$sslDefault]:" ssl
ssl="${ssl:-${sslDefault}}"
done
if [ "$ssl" == "letsencrypt" ]
then
# ask for email for letsencrypt
leMailDefault=""
leMail=""
read -p "Please enter your email address to get informed when your letsencrypt certificate is about to expire:" leMail
leMail="${leMail:-${leMailDefault}}"
leDomains="$pveHost"
# ask whether www should be added as letsencrypt domain
leWwwDefault="yes"
leWww=""
read -p "Add www subdomain to your letsencrypt certificate: www.${pveHost}? (yes/no) [$leWwwDefault]:" leWww
leWww="${leWww:-${leWwwDefault}}"
if [ "$leWww" == "$leWwwDefault" ]
then
leDomains="${leDomains} www.${pveHost}"
fi
$(sed 's@LETSENCRYPT=false@LETSENCRYPT=true@g' $envTmp > $envTmp.tmp && mv $envTmp.tmp $envTmp)
$(sed 's@SELF_SIGNED=true@SELF_SIGNED=false@g' $envTmp > $envTmp.tmp && mv $envTmp.tmp $envTmp)
$(sed 's@LETSENCRYPT_DOMAINS="<domain.tld>"@LETSENCRYPT_DOMAINS='"$leDomains"'@g' $envTmp > $envTmp.tmp && mv $envTmp.tmp $envTmp)
$(sed 's/EMAIL="<your mail address>"/EMAIL='"$leMail"'/g' $envTmp > $envTmp.tmp && mv $envTmp.tmp $envTmp)
fi
# reverse-proxy: SSL is terminated externally — disable internal SSL and switch compose file
if [ "$ssl" == "reverse-proxy" ]
then
$(sed 's@SELF_SIGNED=true@SELF_SIGNED=false@g' $envTmp > $envTmp.tmp && mv $envTmp.tmp $envTmp)
$(sed 's@COMPOSE_FILE=docker-compose.yml:docker-compose.override.yml@COMPOSE_FILE=docker-compose.no-ssl.yml:docker-compose.override.yml@g' $envTmp > $envTmp.tmp && mv $envTmp.tmp $envTmp)
fi
########## setup host ports ##########
listenPort=80
httpsListenPort=443
configureHostPort listenPort "HTTP (LISTEN_PORT)" 80 8080
if [ "$ssl" != "reverse-proxy" ]; then
configureHostPort httpsListenPort "HTTPS (HTTPS_LISTEN_PORT)" 443 8443 "$listenPort"
fi
sed "s@^LISTEN_PORT=.*@LISTEN_PORT=$listenPort@" "$envTmp" > "$envTmp.tmp" && mv "$envTmp.tmp" "$envTmp"
sed "s@^HTTPS_LISTEN_PORT=.*@HTTPS_LISTEN_PORT=$httpsListenPort@" "$envTmp" > "$envTmp.tmp" && mv "$envTmp.tmp" "$envTmp"
if [ "$ssl" == "letsencrypt" ] && [ "$listenPort" != "80" ]; then
echo "Warning: Let's Encrypt HTTP-01 validation requires public port 80."
echo "Forward public port 80 to host port $listenPort or certificate issuance will fail."
fi
if [ "$ssl" == "letsencrypt" ] && [ "$httpsListenPort" != "443" ]; then
echo "Note: Browsers use HTTPS port 443 by default."
echo "Forward public port 443 to host port $httpsListenPort or include the port in the URL."
fi
########## setup cron ##########
cronDefault="yes"
cronDB=""
cronDocker=""
read -p "Enable automatic backups of the database, uploaded images and fonts? (yes/no) [$cronDefault]:" cronDB
cronDB="${cronDB:-${cronDefault}}"
if [ "$cronDB" == "$cronDefault" ]
then
createCron "backup_mysql_docker"
if [ $? -eq 0 ]
then
echo "Backups will be stored in the db-backup-vol Docker volume."
fi
chmod +x backup-db.sh
fi
read -p "Enable automatic updates of docker images? (yes/no) [$cronDefault]:" cronDocker
cronDocker="${cronDocker:-${cronDefault}}"
if [ "$cronDocker" == "$cronDefault" ]
then
createCron "update-docker"
fi
### select language ###
pveLangDefault="de"
pveLang=""
while ! [[ "$pveLang" =~ ^(de|en)$ ]]
do
read -p "Please choose the language of the tool (de/en) [$pveLangDefault]:" pveLang
pveLang="${pveLang:-${pveLangDefault}}"
done
$(sed "s@LOCALE=de@LOCALE=$pveLang@g" $envTmp > $envTmp.tmp && mv $envTmp.tmp $envTmp)
echo "Setup uses the production image (fewohbee-phpfpm:latest). For development, set FEWOHBEE_VERSION=<version>-debug in .env after installation."
echo "Generating secrets and passwords."
mariadbRootPw=$(openssl rand -base64 32 | shasum | cut -f 1 -d " ")
mariadbPw=$(openssl rand -base64 32 | shasum | cut -f 1 -d " ")
mysqlBackupPw=$(openssl rand -base64 32 | shasum | cut -f 1 -d " ")
appSecret=$(openssl rand -base64 23)
$(sed 's@MARIADB_ROOT_PASSWORD=<pw>@MARIADB_ROOT_PASSWORD='"$mariadbRootPw"'@g' $envTmp > $envTmp.tmp && mv $envTmp.tmp $envTmp)
$(sed 's@MARIADB_PASSWORD=<pw>@MARIADB_PASSWORD='"$mariadbPw"'@g' $envTmp > $envTmp.tmp && mv $envTmp.tmp $envTmp)
$(sed "s@MYSQL_BACKUP_PASSWORD=<backuppassword>@MYSQL_BACKUP_PASSWORD=$mysqlBackupPw@g" $envTmp > $envTmp.tmp && mv $envTmp.tmp $envTmp)
$(sed "s@APP_SECRET=<secret>@APP_SECRET=$appSecret@g" $envTmp > $envTmp.tmp && mv $envTmp.tmp $envTmp)
# replace db password in DATABASE_URL
$(sed "s@db_password@$mariadbPw@" $envTmp > $envTmp.tmp && mv $envTmp.tmp $envTmp)
mv $envTmp $envEnd
# local overrides, never touched by updates
if [ ! -f docker-compose.override.yml ]; then
(umask 0022 && cp docker-compose.override.yml.dist docker-compose.override.yml)
fi
########## pull, build and start environment ##########
echo "Preparing and starting docker-compose setup ..."
$dockerComposeBin up -d
if [ $? -ne 0 ]
then
echo "error during docker-compose up"
exit 1
fi
########## application setup ##########
echo "Waiting for the php container to become healthy ..."
waited=0
while true; do
health=$($dockerComposeBin ps --format '{{.Service}} {{.Health}}' 2>/dev/null | grep '^php ' | awk '{print $2}')
if [ "$health" = "healthy" ]; then
break
fi
if [ $waited -ge 180 ]; then
echo "Warning: php container did not become healthy within 180s. Continuing anyway."
break
fi
echo "still waiting ..."
sleep 5
waited=$((waited + 5))
done
########## database backup user ##########
if ! $dockerComposeBin exec -T db /bin/sh -s < conf/db/create-backup-user.sh; then
echo "Warning: could not create the database backup user. See the wiki page Docker-Setup, section Backups."
fi
########## init tool ##########
$dockerComposeBin exec --user www-data php /bin/sh -c "php bin/console app:first-run"
echo "done"
if [ "$ssl" == "reverse-proxy" ]
then
echo "You can now open a browser and visit http://$pveHost (via reverse proxy)."
else
echo "You can now open a browser and visit https://$pveHost."
fi
echo "If you want to use the conversation feature please modify the section in the .env file accordingly."
echo " > see https://github.com/developeregrem/fewohbee/wiki/Konfiguration#e-mails"
echo "To use the city lookup feature please refer to: https://github.com/developeregrem/fewohbee/wiki/City-Lookup"
exit 0