forked from mimolabs/mimo-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-install.sh
More file actions
executable file
·522 lines (444 loc) · 14.7 KB
/
docker-install.sh
File metadata and controls
executable file
·522 lines (444 loc) · 14.7 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#!/usr/bin/env bash
fails=0
echo
echo -e "\e[38;5;42mWelcome to the \e[91mMIMO Community Edition!\e[0m \e[38;5;42mLet's get this show on the road.\e[0m"
echo "Checking installation, please wait a moment."
echo
check_root() {
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root, please run sudo or log in as root first." 1>&2
exit 1
fi
}
check_linux_memory() {
echo `free -g --si | awk ' /Mem:/ {print $2} '`
}
check_docker () {
docker_path=`which docker.io || which docker`
if [ -z $docker_path ]; then
read -p "Docker not installed. Hit enter to install from https://get.docker.com/ or type Ctrl+C to exit"
curl https://get.docker.com/ | sh
fi
docker_path=`which docker.io || which docker`
if [ -z $docker_path ]; then
echo Installing Docker failed. Exiting.
exit
fi
}
check_docker_compose () {
docker_path=`which docker-compose`
if [ -z $docker_path ]; then
# read -p "Docker Compose not installed. Hit enter to install or type Ctrl+C to exit"
curl -s -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
echo `docker-compose --version`
echo ""
fi
docker_path=`which docker.io || which docker`
if [ -z $docker_path ]; then
echo Installing Docker failed. Exiting.
exit
fi
}
check_dns() {
dns=`getent hosts ${1} | awk '{ print $1 }'`
echo $dns
}
check_disk_and_memory() {
mem_free=$(check_linux_memory)
if [ "$mem_free" -lt 1 ]; then
echo "WARNING: MIMO requires 1GB RAM. This system doesn't have"
echo "sufficient memory."
echo
echo "Please provision a server with at least 1GB RAM"
exit 1
fi
free_disk="$(df /var | tail -n 1 | awk '{print $4}')"
if [ "$free_disk" -lt 5000 ]; then
echo "WARNING: MIMO requires at least 5GB free disk space. This system"
echo "doesn't have enough disk space."
echo
echo "Please free up some space or expand your disk before continuing."
echo
exit 1
fi
}
check_port() {
if lsof -Pi :${1} -sTCP:LISTEN -t >/dev/null; then
echo ${1} is in use
echo "Please ensure you stop any services that are running on port ${1}"
exit 1
fi
}
kill_docker() {
# if [ $DEBUG ] ; then
# docker-compose down --rmi all
# else
# fi
docker-compose -f docker-compose.yml down >> /dev/null 2>&1
docker-compose -f docker-compose-lets-encrypt.yml down >> /dev/null 2>&1
}
check_config_exists() {
FILE='production.vars'
if [ ! -f $FILE ]; then
cp $FILE.orig $FILE
fi
FILE='api.vars'
if [ ! -f $FILE ]; then
echo -n '' > $FILE
fi
FILE='dashboard.vars'
if [ ! -f $FILE ]; then
echo -n '' > $FILE
fi
FILE='splash.vars'
if [ ! -f $FILE ]; then
echo -n '' > $FILE
fi
}
check_ports() {
check_port "80"
check_port "443"
}
find_in_file() {
val=`sed -n -e "s/${1}=//p" production.vars`
echo "$val"
}
test_application_running() {
fails=$(($fails + 1))
until [ $fails -ge 10 ] ; do
for i in {1..10}; do
# if [ $i -gt 1 ] ; then
# echo "Retrying ($fails) "
# fi
response=$(curl --write-out %{http_code} -k -L --silent --output /dev/null https://api.${1}/_healthz)
if [ "${response}" == 200 ] ; then
break
fi
if [ $i == 10 ] ; then
# docker exec nginx-letsencrypt /app/force_renew
echo -e "Trying certificate generation again\e[0m"
docker-compose -f docker-compose-lets-encrypt.yml down; docker-compose -f docker-compose-lets-encrypt.yml up -d
test_application_running ${1}
fi
cursor=$cursor.
echo -ne "${cursor}\r"
sleep 3
done
echo
echo -e "\e[38;5;42m[SUCCESS] MIMO is up and running!\e[0m"
echo '----------------------------'
echo
echo "We've emailed an email to the admin user with instructions on how to complete the installation."
echo
echo "If the email doesn't arrive, please check you entered valid SMTP credentials."
echo
echo 'You stay classy!'
echo
exit 0
done
echo -e "\e[91m[ERROR] MIMO certificates did not install successfully, exiting.\e[0m"
}
update_config() {
public_ip=`curl -s ifconfig.co`
local ok_config='no'
local production_config='production.vars'
local changelog='change.log'
hostname=`find_in_file MIMO_DOMAIN`
hostname_orig=`find_in_file MIMO_DOMAIN`
admin_user=`find_in_file MIMO_ADMIN_USER`
admin_user_orig=$admin_user
smtp_host=`find_in_file MIMO_SMTP_HOST`
smtp_host_orig=$smtp_host
smtp_port=`find_in_file MIMO_SMTP_PORT`
smtp_port_orig=$smtp_port
smtp_user=`find_in_file MIMO_SMTP_USER`
smtp_user_orig=$smtp_user
smtp_pass=`find_in_file MIMO_SMTP_PASS`
smtp_pass_orig=$smtp_pass
smtp_domain_orig=`find_in_file MIMO_SMTP_DOMAIN`
dashboard_url=`find_in_file MIMO_DASHBOARD_URL`
api_url=`find_in_file MIMO_API_URL`
letsencrypt_email=`find_in_file LETSENCRYPT_EMAIL`
letsencrypt_email_orig=$letsencrypt_email
cloudflare='no'
while [[ "$ok_config" == "no" ]]
do
if [ ! -z "$hostname" ]
then
read -p "What's the domain for your MIMO installation? [$hostname]: " new_value
if [ ! -z "$new_value" ]
then
hostname="$new_value"
fi
if [[ ! $hostname =~ ^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$ ]]
then
echo
echo "[WARNING!!!] MIMO needs a valid hostname. IP addresses are not supported!"
echo ""
echo "A valid hostname should look like 'example.com' and not 'test.example.com'"
echo "Setting your hostname to example.com"
echo
hostname="example.com"
fi
fi
if [ ! -z "$admin_user" ]
then
read -p "enter your email address [$admin_user]: " new_value
if [ ! -z "$new_value" ]
then
admin_user="$new_value"
fi
if [[ ! $admin_user =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$ ]]
then
echo
echo "[WARNING!!!] MIMO needs a valid email, please try again!"
echo
admin_user="user@example.com"
fi
fi
if [ ! -z "$letsencrypt_email" ]
then
if [ "$letsencrypt_email" == "email@example.com" ] ; then
letsencrypt_email=$admin_user
fi
read -p "enter an email for Let's Encrypt [$letsencrypt_email]: " new_value
if [ ! -z "$new_value" ]
then
letsencrypt_email="$new_value"
fi
fi
if [ ! -z "$smtp_host" ]
then
read -p "Enter your SMTP hostname [$smtp_host]: " new_value
if [ ! -z "$new_value" ]
then
smtp_host="$new_value"
fi
if [ "$smtp_host" == "smtp.sendgrid.net" ]
then
smtp_port=2525
fi
if [ "$smtp_address" == "smtp.mailgun.org" ]
then
smtp_port=587
fi
fi
if [ ! -z "$smtp_port" ]
then
read -p "Enter your SMTP port [$smtp_port]: " new_value
if [ ! -z "$new_value" ]
then
smtp_port="$new_value"
fi
fi
if [ ! -z "$smtp_user" ]
then
read -p "Enter your SMTP username [$smtp_user]: " new_value
if [ ! -z "$new_value" ]
then
smtp_user="$new_value"
fi
fi
if [ ! -z "$smtp_pass" ]
then
read -p "enter your SMTP password [$smtp_pass]: " new_value
if [ ! -z "$new_value" ]
then
smtp_pass="$new_value"
fi
fi
if [ ! -z "$cloudflare" ]
then
read -p "are you using Cloudflare? [$cloudflare]: " new_value
if [ ! -z "$new_value" ]
then
cloudflare="$new_value"
fi
fi
echo -e "\nDoes this look right?\n"
echo "Hostname : $hostname"
echo "Admin Email : $admin_user"
echo "Let's Encrypt Email : $letsencrypt_email"
echo "SMTP Host : $smtp_host"
echo "SMTP Port : $smtp_port"
echo "SMTP User : $smtp_user"
echo "SMTP Password : $smtp_pass"
echo ""
read -p "Hit ENTER to continue. Type 'no' to try again. Ctrl+C will exit: " ok_config
done
echo "Writing configs to $production_config. Then we'll start the magic"
postgres_pass=`find_in_file POSTGRES_PASSWORD`
rails_secret=`find_in_file RAILS_SECRET_KEY`
secret_key=`find_in_file SECRET_KEY_BASE`
cp $production_config $production_config.backup
sed -i -e "s/MIMO_DOMAIN=${hostname_orig}/MIMO_DOMAIN=$hostname/w $changelog" $production_config
if [ -s $changelog ]
then
echo "Added ${hostname} as primary domain"
rm $changelog
fi
sed -i -e "s~MIMO_DASHBOARD_URL=${dashboard_url}~MIMO_DASHBOARD_URL=https://dashboard.$hostname~w $changelog" $production_config
if [ -s $changelog ]
then
echo "Added https://dashboard.${hostname} as dashboard url"
rm $changelog
fi
sed -i -e "s~MIMO_API_URL=${api_url}~MIMO_API_URL=https://api.$hostname~w $changelog" $production_config
if [ -s $changelog ]
then
echo "Added https://api.${hostname} as API url"
rm $changelog
fi
sed -i -e "s/MIMO_SMTP_HOST=${smtp_host_orig}/MIMO_SMTP_HOST=$smtp_host/w $changelog" $production_config
if [ -s $changelog ]
then
echo "Added ${smtp_host} as SMTP hostname"
rm $changelog
fi
sed -i -e "s/MIMO_SMTP_PORT=${smtp_port_orig}/MIMO_SMTP_PORT=$smtp_port/w $changelog" $production_config
if [ -s $changelog ]
then
echo "Added ${smtp_port} as SMTP port"
rm $changelog
fi
sed -i -e "s/MIMO_SMTP_USER=${smtp_user_orig}/MIMO_SMTP_USER=$smtp_user/w $changelog" $production_config
if [ -s $changelog ]
then
echo "Added ${smtp_user} as SMTP user"
rm $changelog
fi
sed -i -e "s/MIMO_SMTP_PASS=${smtp_pass_orig}/MIMO_SMTP_PASS=$smtp_pass/w $changelog" $production_config
if [ -s $changelog ]
then
rm $changelog
fi
sed -i -e "s/MIMO_SMTP_DOMAIN=${smtp_domain_orig}/MIMO_SMTP_DOMAIN=$hostname/w $changelog" $production_config
if [ -s $changelog ]
then
# echo "Added ${hostname} as SMTP domain"
rm $changelog
fi
sed -i -e "s/MIMO_ADMIN_USER=${admin_user_orig}/MIMO_ADMIN_USER=$admin_user/w $changelog" $production_config
if [ -s $changelog ] ; then
echo "Added ${admin_user} as admin user"
rm $changelog
fi
if [ "$rails_secret" == "KEY" ] ; then
rails_secret=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 64 | head -n 1)
sed -i -e "s/RAILS_SECRET_KEY=KEY/RAILS_SECRET_KEY=$rails_secret/w $changelog" $production_config
if [ -s $changelog ] ; then
# echo "Updated RAILS SECRET"
rm $changelog
fi
fi
if [ "$secret_key" == "KEY" ] ; then
secret_key=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 64 | head -n 1)
sed -i -e "s/SECRET_KEY_BASE=KEY/SECRET_KEY_BASE=$secret_key/w $changelog" $production_config
if [ -s $changelog ] ; then
# echo "Updated secret keybase"
rm $changelog
fi
fi
if [ "$postgres_pass" == "PASS" ] ; then
postgres_pass=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
sed -i -e "s/POSTGRES_PASSWORD=PASS/POSTGRES_PASSWORD=$postgres_pass/w $changelog" $production_config
if [ -s $changelog ] ; then
echo "Updated postgres password"
rm $changelog
fi
fi
val=`find_in_file PUBLIC_IP`
sed -i -e "s/PUBLIC_IP=${val}/PUBLIC_IP=$public_ip/g" $production_config
if [ "$cloudflare" == "no" ] ; then
ip=`check_dns "api.${hostname}"`
if [ "${ip}" != "${public_ip}" ] ; then
echo -e "\e[91m[ERROR] api.${hostname} does not resolve to this host. Please update your DNS records!!.\e[0m"
echo "If you're using Cloudflare, please disabled their proxy for installation. You can enable it again after."
exit 1
fi
ip=`check_dns "dashboard.${hostname}"`
if [ "${ip}" != "${public_ip}" ] ; then
echo -e "\e[91m[ERROR] dashboard.${hostname} does not resolve to this host. Please update your DNS records!! Your server's public IP is ${public_ip}!! \e[0m"
echo
echo -e "Once you've updated your DNS, run the installer again."
exit 1
fi
fi
echo -n "" > api.vars
echo -n "" > dashboard.vars
### We should not do this since it will overwrite the custom vars
echo -n "" > splash.vars
echo "VIRTUAL_HOST=api.${hostname},admin.${hostname}" >> api.vars
echo "VIRTUAL_HOST=dashboard.${hostname}" >> dashboard.vars
echo "VIRTUAL_HOST=splash.${hostname}" >> splash.vars
if [ $letsencrypt_email ] ; then
echo "LETSENCRYPT_HOST=api.${hostname},admin.${hostname}" >> api.vars
echo "LETSENCRYPT_HOST=dashboard.${hostname}" >> dashboard.vars
if [ $TEST ] ; then
echo "Enabling Let\'s Encrypt Test Mode"
echo "LETSENCRYPT_TEST=true" >> api.vars
echo "LETSENCRYPT_TEST=true" >> dashboard.vars
fi
sed -i -e "s/LETSENCRYPT_EMAIL=${letsencrypt_email_orig}/LETSENCRYPT_EMAIL=$letsencrypt_email/w $changelog" $production_config
if [ -s $changelog ] ; then
echo "Added ${letsencrypt_email} as let's encrypt user"
rm $changelog
fi
else
echo "\e[91m[ERROR] You must set an email for let's encrypt! Otherwise we cannot secure your installation....\e[0m"
fi
echo "FACEBOOK_CLIENT_ID=" >> splash.vars
echo "FACEBOOK_CLIENT_SECRET=" >> splash.vars
echo "GOOGLE_CLIENT_ID=" >> splash.vars
echo "GOOGLE_CLIENT_SECRET=" >> splash.vars
echo "TWITTER_CLIENT_ID=" >> splash.vars
echo "TWITTER_CLIENT_SECRET=" >> splash.vars
if [ $DEBUG ] ; then
docker-compose up --pull --force-recreate
# docker-compose pull && docker-compose up --force-recreate -d
elif [ $FOREGROUND ] ; then
docker-compose down; docker-compose -f docker-compose.yml pull && docker-compose -f docker-compose.yml up
else
docker-compose down; docker-compose -f docker-compose.yml pull && docker-compose -f docker-compose.yml up -d
# --force-recreate
fi
echo
echo -e "\e[38;2;240;143;104mStarting MIMO. Please wait while the installation completes...\e[0m"
echo "If this is the first time you've installed MIMO, it may take a few minutes to generate your keys"
cursor=.
for i in {1..100}; do
response=$(curl --write-out %{http_code} -k -L --silent --output /dev/null http://api.$hostname/_healthz)
if [ "${response}" == 200 ] ; then
break
fi
if [ $i == 100 ] ; then
echo -e "\e[91m[ERROR] MIMO installation did not complete successfully.\e[0m"
echo
echo "Run ./docker-logs.sh for more information and try again."
echo
exit 1
fi
cursor=$cursor.
echo -ne "${cursor}\r"
sleep 3
done
echo 'API started, creating the SSL certificates'
echo
docker-compose -f docker-compose-lets-encrypt.yml up -d
### Wait for Docker to load let's encrypt before continuing...
until $(docker ps | grep -q letsencrypt); do
cursor=$cursor.
echo -ne "${cursor}\r"
sleep 5
done
test_application_running $hostname
}
check_root
check_docker
check_docker_compose
check_disk_and_memory
check_config_exists
kill_docker
check_ports
update_config