use $web_file not app.yml
[discourse_docker.git] / discourse-setup
CommitLineData
f17af951 1#!/usr/bin/env bash
275735af
JP
2DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
3cd $DIR
c2d3ee4a 4
ebdd72f3
JA
5##
6## Make sure only root can run our script
7##
8check_root() {
9 if [[ $EUID -ne 0 ]]; then
10 echo "This script must be run as root. Please sudo or log in as root first." 1>&2
11 exit 1
12 fi
13}
14
07f3b11e 15##
7f2d6260 16## Check whether a connection to HOSTNAME ($1) on PORT ($2) is possible
07f3b11e 17##
7f2d6260
JP
18connect_to_port () {
19 HOST="$1"
20 PORT="$2"
b6379984
RH
21 VERIFY=$(date +%s | sha256sum | base64 | head -c 20)
22 if ! [ -x "$(command -v nc)" ]; then
23 echo "In order to check the connection to $HOST:$PORT we need to open a socket using netcat."
24 echo However netcat is not installed on your system. You can continue without this check
25 echo or abort the setup, install netcat and try again.
26 while true; do
27 read -p "Would you like to continue without this check? [yn] " yn
28 case $yn in
29 [Yy]*) return 2 ;;
30 [Nn]*) exit ;;
31 *) echo "Please answer y or n." ;;
32 esac
33 done
07f3b11e 34 else
b6379984
RH
35 echo -e "HTTP/1.1 200 OK\n\n $VERIFY" | nc -w 4 -l -p $PORT >/dev/null 2>&1 &
36 if curl --proto =http -s $HOST:$PORT --connect-timeout 3 | grep $VERIFY >/dev/null 2>&1; then
37 return 0
38 else
39 curl --proto =http -s localhost:$PORT >/dev/null 2>&1
40 return 1
41 fi
07f3b11e 42 fi
7f2d6260 43}
07f3b11e 44
b6379984 45check_IP_match() {
7f2d6260
JP
46 HOST="$1"
47 echo
48 echo Checking your domain name . . .
b6379984
RH
49 connect_to_port $HOST 443; ec=$?
50 case $ec in
51 0)
7f2d6260 52 echo "Connection to $HOST succeeded."
b6379984
RH
53 ;;
54 1)
3a4be87a 55 echo "WARNING: Port 443 of computer does not appear to be accessible using hostname: $HOST."
b6379984 56 if connect_to_port $HOST 80; then
3a4be87a
JP
57 echo
58 echo SUCCESS: A connection to port 80 succeeds!
b6379984
RH
59 echo This suggests that your DNS settings are correct,
60 echo but something is keeping traffic to port 443 from getting to your server.
61 echo Check your networking configuration to see that connections to port 443 are allowed.
62 else
3a4be87a 63 echo "WARNING: Connection to http://$HOST (port 80) also fails."
b6379984 64 echo
3a4be87a
JP
65 echo "This suggests that $HOST resolves to some IP address that does not reach this "
66 echo machine where you are installing discourse.
b6379984
RH
67 fi
68 echo
3a4be87a
JP
69 echo "The first thing to do is confirm that $HOST resolves to the IP address of this server."
70 echo You usually do this at the same place you purchased the domain.
71 echo
72 echo If you are sure that the IP address resolves correctly, it could be a firewall issue.
73 echo A web search for \"open ports YOUR CLOUD SERVICE\" might help.
74 echo
75 echo This tool is designed only for the most standard installations. If you cannot resolve
76 echo the issue above, you will need to edit containers/app.yml yourself and then type
77 echo
78 echo ./launcher rebuild app
b6379984 79 echo
d3e3857d 80 exit 1
b6379984
RH
81 ;;
82 2)
83 echo "Continuing without port check."
84 ;;
85 esac
07f3b11e
JP
86}
87
c87c4b0a 88##
18602189
JP
89## Do we have docker?
90##
91check_and_install_docker () {
7cf781fc 92 docker_path=`which docker.io || which docker`
18602189
JP
93 if [ -z $docker_path ]; then
94 read -p "Docker not installed. Enter to install from https://get.docker.com/ or Ctrl+C to exit"
c87c4b0a 95 curl https://get.docker.com/ | sh
18602189 96 fi
7cf781fc 97 docker_path=`which docker.io || which docker`
18602189
JP
98 if [ -z $docker_path ]; then
99 echo Docker install failed. Quitting.
100 exit
101 fi
102}
103
69dcbef5
SG
104##
105## What are we running on
106##
107check_OS() {
108 echo `uname -s`
109}
110
e5ec8aa1
SG
111##
112## OS X available memory
113##
114check_osx_memory() {
275735af 115 echo `free -m | awk '/Mem:/ {print $2}'`
e5ec8aa1
SG
116}
117
118##
119## Linux available memory
120##
121check_linux_memory() {
20e812e3
TS
122 ## some VMs report just under 1GB of RAM, so
123 ## make an exception and allow those with more
124 ## than 989MB
125 mem=`free -m --si | awk ' /Mem:/ {print $2}'`
126 if [ "$mem" -ge 990 -a "$mem" -lt 1000 ]; then
127 echo 1
128 else
129 echo `free -g --si | awk ' /Mem:/ {print $2} '`
130 fi
e5ec8aa1 131}
c87c4b0a 132
c2d3ee4a
JA
133##
134## Do we have enough memory and disk space for Discourse?
135##
136check_disk_and_memory() {
c87c4b0a 137
69dcbef5
SG
138 os_type=$(check_OS)
139 avail_mem=0
17f62d87 140 if [ "$os_type" == "Darwin" ]; then
e5ec8aa1 141 avail_mem=$(check_osx_memory)
69dcbef5 142 else
e5ec8aa1 143 avail_mem=$(check_linux_memory)
69dcbef5
SG
144 fi
145
c6374a12 146 if [ "$avail_mem" -lt 1 ]; then
51890305
JA
147 echo "WARNING: Discourse requires 1GB RAM to run. This system does not appear"
148 echo "to have sufficient memory."
c2d3ee4a 149 echo
51890305
JA
150 echo "Your site may not work properly, or future upgrades of Discourse may not"
151 echo "complete successfully."
c87c4b0a 152 exit 1
cdd99376 153 fi
c87c4b0a 154
f7bb85e6 155 if [ "$avail_mem" -le 2 ]; then
c6374a12 156 total_swap=`free -g --si | awk ' /Swap:/ {print $2} '`
7f2d6260 157
c6374a12 158 if [ "$total_swap" -lt 2 ]; then
bd7e6e26
JP
159 echo "WARNING: Discourse requires at least 2GB of swap when running with 2GB of RAM"
160 echo "or less. This system does not appear to have sufficient swap space."
c2d3ee4a 161 echo
8f70d450 162 echo "Without sufficient swap space, your site may not work properly, and future"
51890305 163 echo "upgrades of Discourse may not complete successfully."
c2d3ee4a 164 echo
7f2d6260
JP
165 echo "Ctrl+C to exit or wait 5 seconds to have a 2GB swapfile created."
166 sleep 5
c87c4b0a 167
8f70d450
JA
168 ##
169 ## derived from https://meta.discourse.org/t/13880
c87c4b0a 170 ##
8f70d450 171 install -o root -g root -m 0600 /dev/null /swapfile
72a7bdf0 172 fallocate -l 2G /swapfile
8f70d450
JA
173 mkswap /swapfile
174 swapon /swapfile
7c2777f9 175 echo "/swapfile swap swap auto 0 0" | tee -a /etc/fstab
7802f679 176 sysctl -w vm.swappiness=10
874fa4d3 177 echo 'vm.swappiness = 10' > /etc/sysctl.d/30-discourse-swap.conf
8f70d450 178
c6374a12
JP
179 total_swap=`free -g --si | awk ' /Swap:/ {print $2} '`
180 if [ "$total_swap" -lt 2 ]; then
8e7862c4 181 echo "Failed to create swap: are you root? Are you running on real hardware, or a fully virtualized server?"
8f70d450
JA
182 exit 1
183 fi
c87c4b0a 184
c2d3ee4a
JA
185 fi
186 fi
187
c6374a12 188
c2d3ee4a
JA
189 free_disk="$(df /var | tail -n 1 | awk '{print $4}')"
190 if [ "$free_disk" -lt 5000 ]; then
51890305
JA
191 echo "WARNING: Discourse requires at least 5GB free disk space. This system"
192 echo "does not appear to have sufficient disk space."
c2d3ee4a 193 echo
51890305
JA
194 echo "Insufficient disk space may result in problems running your site, and"
195 echo "may not even allow Discourse installation to complete successfully."
c2d3ee4a
JA
196 echo
197 echo "Please free up some space, or expand your disk, before continuing."
198 echo
51890305
JA
199 echo "Run \`apt-get autoremove && apt-get autoclean\` to clean up unused"
200 echo "packages and \`./launcher cleanup\` to remove stale Docker containers."
c2d3ee4a
JA
201 exit 1
202 fi
203
c2d3ee4a
JA
204}
205
206
207##
208## If we have lots of RAM or lots of CPUs, bump up the defaults to scale better
209##
210scale_ram_and_cpu() {
211
642b870f 212 local changelog=/tmp/changelog.$PPID
c2d3ee4a 213 # grab info about total system ram and physical (NOT LOGICAL!) CPU cores
e5ec8aa1
SG
214 avail_gb=0
215 avail_cores=0
216 os_type=$(check_OS)
17f62d87 217 if [ "$os_type" == "Darwin" ]; then
e5ec8aa1
SG
218 avail_gb=$(check_osx_memory)
219 avail_cores=`sysctl hw.ncpu | awk '/hw.ncpu:/ {print $2}'`
220 else
96568673
TH
221 avail_gb=$(check_linux_memory)
222 avail_cores=$((`awk '/cpu cores/ {print $4;exit}' /proc/cpuinfo`*`sort /proc/cpuinfo | uniq | grep -c "physical id"`))
e5ec8aa1 223 fi
c2d3ee4a
JA
224 echo "Found ${avail_gb}GB of memory and $avail_cores physical CPU cores"
225
226 # db_shared_buffers: 128MB for 1GB, 256MB for 2GB, or 256MB * GB, max 4096MB
227 if [ "$avail_gb" -eq "1" ]
228 then
229 db_shared_buffers=128
230 else
231 if [ "$avail_gb" -eq "2" ]
232 then
233 db_shared_buffers=256
234 else
235 db_shared_buffers=$(( 256 * $avail_gb ))
236 fi
237 fi
238 db_shared_buffers=$(( db_shared_buffers < 4096 ? db_shared_buffers : 4096 ))
239
f5cf127d 240 sed -i -e "s/^ #\?db_shared_buffers:.*/ db_shared_buffers: \"${db_shared_buffers}MB\"/w $changelog" $data_file
c2d3ee4a
JA
241 if [ -s $changelog ]
242 then
243 echo "setting db_shared_buffers = ${db_shared_buffers}MB"
244 rm $changelog
245 fi
246
c2d3ee4a
JA
247 # UNICORN_WORKERS: 2 * GB for 2GB or less, or 2 * CPU, max 8
248 if [ "$avail_gb" -le "2" ]
249 then
250 unicorn_workers=$(( 2 * $avail_gb ))
251 else
252 unicorn_workers=$(( 2 * $avail_cores ))
253 fi
254 unicorn_workers=$(( unicorn_workers < 8 ? unicorn_workers : 8 ))
255
f5cf127d 256 sed -i -e "s/^ #\?UNICORN_WORKERS:.*/ UNICORN_WORKERS: ${unicorn_workers}/w $changelog" $web_file
c2d3ee4a
JA
257 if [ -s $changelog ]
258 then
259 echo "setting UNICORN_WORKERS = ${unicorn_workers}"
260 rm $changelog
261 fi
262
f5cf127d 263 echo $data_file memory parameters updated.
c2d3ee4a
JA
264}
265
266
c87c4b0a 267##
c2d3ee4a
JA
268## standard http / https ports must not be occupied
269##
270check_ports() {
271 check_port "80"
272 check_port "443"
273 echo "Ports 80 and 443 are free for use"
274}
275
276
277##
278## check a port to see if it is already in use
279##
280check_port() {
c87c4b0a 281
c2d3ee4a
JA
282 local valid=$(netstat -tln | awk '{print $4}' | grep ":${1}\$")
283
284 if [ -n "$valid" ]; then
285 echo "Port ${1} appears to already be in use."
286 echo
f5cf127d
JP
287 echo "This will show you what command is using port ${1}"
288 lsof -i tcp:${1} -s tcp:listen
289 echo
51890305
JA
290 echo "If you are trying to run Discourse simultaneously with another web"
291 echo "server like Apache or nginx, you will need to bind to a different port"
c87c4b0a 292 echo
51890305 293 echo "See https://meta.discourse.org/t/17247"
f17af951
JP
294 echo
295 echo "If you are reconfiguring an already-configured Discourse, use "
296 echo
297 echo "./launcher stop app"
298 echo
299 echo "to stop Discourse before you reconfigure it and try again."
c2d3ee4a
JA
300 exit 1
301 fi
302}
303
f17af951
JP
304##
305## read a variable from the config file
306##
307read_config() {
f5cf127d 308 config_line=`egrep "^ #?$1:" $web_file`
04a06dd0 309 read_config_result=`echo $config_line | awk -F":" '{print $2}'`
f17af951
JP
310 read_config_result=`echo $read_config_result | sed "s/^\([\"']\)\(.*\)\1\$/\2/g"`
311}
312
04a06dd0
JP
313read_default() {
314 config_line=`egrep "^ #?$1:" samples/standalone.yml`
315 read_default_result=`echo $config_line | awk -F":" '{print $2}'`
316 read_default_result=`echo $read_config_result | sed "s/^\([\"']\)\(.*\)\1\$/\2/g"`
317}
f17af951 318
991162e7 319assert_maxmind_license_key() {
991162e7
JP
320 if ! grep DISCOURSE_MAXMIND_LICENSE_KEY $web_file
321 then
322 echo "Adding MAXMIND placeholder to $web_file"
32c7b987 323 sed -i '/^.*LETSENCRYPT_ACCOUNT_EMAIL.*/a \ \ #DISCOURSE_MAXMIND_LICENSE_KEY: 1234567890123456' $web_file
991162e7
JP
324 fi
325}
326
c2d3ee4a
JA
327##
328## prompt user for typical Discourse config file values
329##
4b1b25e3 330ask_user_for_config() {
c87c4b0a 331
f17af951
JP
332 # NOTE: Defaults now come from standalone.yml
333
642b870f 334 local changelog=/tmp/changelog.$PPID
f17af951
JP
335 read_config "DISCOURSE_SMTP_ADDRESS"
336 local smtp_address=$read_config_result
337 # NOTE: if there are spaces between emails, this breaks, but a human should be paying attention
338 read_config "DISCOURSE_DEVELOPER_EMAILS"
339 local developer_emails=$read_config_result
340 read_config "DISCOURSE_SMTP_PASSWORD"
341 local smtp_password=$read_config_result
342 read_config "DISCOURSE_SMTP_PORT"
343 local smtp_port=$read_config_result
344 read_config "DISCOURSE_SMTP_USER_NAME"
345 local smtp_user_name=$read_config_result
346 if [ "$smtp_password" = "pa$$word" ]
347 then
348 smtp_password = ""
349 fi
350 read_config "LETSENCRYPT_ACCOUNT_EMAIL"
351 local letsencrypt_account_email=$read_config_result
17f62d87
JP
352 if [ -z $letsencrypt_account_email ]
353 then
354 letsencrypt_account_email="me@example.com"
355 fi
356 if [ "$letsencrypt_account_email" = "me@example.com" ]
f17af951
JP
357 then
358 local letsencrypt_status="ENTER to skip"
359 else
360 local letsencrypt_status="Enter 'OFF' to disable."
361 fi
362
991162e7
JP
363 read_config "DISCOURSE_MAXMIND_LICENSE_KEY"
364 local maxmind_license_key=$read_config_result
365 if [ -z $maxmind_license_key ]
366 then
367 maxmind_license_key="1234567890123456"
368 fi
369 if [ "$maxmind_license_key" == "1234567890123456" ]
370 then
371 local maxmind_status="ENTER to continue without MAXMIND GeoLite2 geolocation database"
372 fi
373
f17af951
JP
374 read_config "DISCOURSE_HOSTNAME"
375 hostname=$read_config_result
c2d3ee4a
JA
376
377 local new_value=""
378 local config_ok="n"
379 local update_ok="y"
c87c4b0a 380
c2d3ee4a
JA
381 echo ""
382
383 while [[ "$config_ok" == "n" ]]
384 do
04a06dd0 385 if [ ! -z "$hostname" ]
c2d3ee4a
JA
386 then
387 read -p "Hostname for your Discourse? [$hostname]: " new_value
275735af
JP
388 if [ ! -z "$new_value" ]
389 then
390 hostname="$new_value"
391 fi
392 if [[ $hostname =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
c2d3ee4a 393 then
275735af
JP
394 echo
395 echo "Discourse requires a DNS hostname. IP addresses are unsupported and will not work."
396 echo
397 hostname="discourse.example.com"
c2d3ee4a
JA
398 fi
399 fi
c87c4b0a 400
d3e3857d
RSS
401 check_IP_match $hostname
402
04a06dd0 403 if [ ! -z "$developer_emails" ]
c2d3ee4a 404 then
3d34d989 405 local email_valid="n"
406 until [ "$email_valid" == "y" ]
407 do
408 read -p "Email address for admin account(s)? [$developer_emails]: " new_value
409 if [ ! -z "$new_value" ]
410 then
411 if [[ ${#new_value} -ge 7 && $new_value == *@* ]]
412 then
413 developer_emails="$new_value"
414 email_valid="y"
415 else
416 echo
417 echo "[Error] Invalid email address"
418 echo
419 fi
420 else
421 email_valid="y"
422 fi
423 done
c2d3ee4a 424 fi
c87c4b0a 425
04a06dd0 426 if [ ! -z "$smtp_address" ]
c2d3ee4a
JA
427 then
428 read -p "SMTP server address? [$smtp_address]: " new_value
275735af 429 if [ ! -z "$new_value" ]
c2d3ee4a 430 then
275735af 431 smtp_address="$new_value"
c2d3ee4a
JA
432 fi
433 fi
c87c4b0a 434
04a06dd0 435 if [ ! -z "$smtp_port" ]
c2d3ee4a 436 then
7c2777f9 437 read -p "SMTP port? [$smtp_port]: " new_value
275735af 438 if [ ! -z "$new_value" ]
7c2777f9 439 then
275735af 440 smtp_port="$new_value"
7c2777f9 441 fi
c2d3ee4a 442 fi
c87c4b0a 443
7c2777f9 444 ##
275735af 445 ## automatically set correct user name based on common mail providers unless it's been set
7c2777f9 446 ##
275735af 447 if [ "$smtp_user_name" == "user@example.com" ]
c2d3ee4a 448 then
275735af
JP
449 if [ "$smtp_address" == "smtp.sparkpostmail.com" ]
450 then
451 smtp_user_name="SMTP_Injection"
452 fi
453 if [ "$smtp_address" == "smtp.sendgrid.net" ]
454 then
455 smtp_user_name="apikey"
456 fi
457 if [ "$smtp_address" == "smtp.mailgun.org" ]
458 then
459 smtp_user_name="postmaster@$hostname"
460 fi
7c2777f9 461 fi
c87c4b0a 462
275735af 463 if [ ! -z "$smtp_user_name" ]
c2d3ee4a
JA
464 then
465 read -p "SMTP user name? [$smtp_user_name]: " new_value
23c730f0 466 if [ ! -z "$new_value" ]
c2d3ee4a 467 then
23c730f0 468 smtp_user_name="$new_value"
c2d3ee4a
JA
469 fi
470 fi
c87c4b0a 471
c2d3ee4a 472 read -p "SMTP password? [$smtp_password]: " new_value
275735af 473 if [ ! -z "$new_value" ]
c2d3ee4a 474 then
275735af 475 smtp_password="$new_value"
c2d3ee4a 476 fi
c87c4b0a 477
c2d3ee4a
JA
478 if [ ! -z $letsencrypt_account_email ]
479 then
026a664e 480 read -p "Optional email address for Let's Encrypt warnings? ($letsencrypt_status) [$letsencrypt_account_email]: " new_value
275735af 481 if [ ! -z "$new_value" ]
c2d3ee4a 482 then
275735af 483 letsencrypt_account_email="$new_value"
f17af951 484 if [ "${new_value,,}" = "off" ]
c2d3ee4a
JA
485 then
486 letsencrypt_status="ENTER to skip"
487 else
488 letsencrypt_status="Enter 'OFF' to disable."
489 fi
490 fi
491 fi
492
991162e7
JP
493 read_config "DISCOURSE_MAXMIND_LICENSE_KEY"
494 local maxmind_license_key=$read_config_result
495 read -p "Optional Maxmind License key ($maxmind_status) [$maxmind_license_key]: " new_value
496 if [ ! -z "$new_value" ]
497 then
498 maxmind_license_key="$new_value"
499 fi
500
51890305 501 echo -e "\nDoes this look right?\n"
c2d3ee4a
JA
502 echo "Hostname : $hostname"
503 echo "Email : $developer_emails"
504 echo "SMTP address : $smtp_address"
7c2777f9 505 echo "SMTP port : $smtp_port"
c2d3ee4a
JA
506 echo "SMTP username : $smtp_user_name"
507 echo "SMTP password : $smtp_password"
c87c4b0a 508
c2d3ee4a
JA
509 if [ "$letsencrypt_status" == "Enter 'OFF' to disable." ]
510 then
511 echo "Let's Encrypt : $letsencrypt_account_email"
512 fi
c87c4b0a 513
991162e7
JP
514 if [ "$maxmind_license_key" != "1234567890123456" ]
515 then
516 echo "Maxmind license: $maxmind_license_key"
517 else
518 echo "Maxmind license: (unset)"
519 fi
07f3b11e 520
c2d3ee4a 521 echo ""
ac1a2d67 522 read -p "ENTER to continue, 'n' to try again, Ctrl+C to exit: " config_ok
c2d3ee4a
JA
523 done
524
f5cf127d 525 sed -i -e "s/^ DISCOURSE_HOSTNAME:.*/ DISCOURSE_HOSTNAME: $hostname/w $changelog" $web_file
c2d3ee4a
JA
526 if [ -s $changelog ]
527 then
528 rm $changelog
529 else
530 echo "DISCOURSE_HOSTNAME change failed."
531 update_ok="n"
532 fi
533
f5cf127d 534 sed -i -e "s/^ DISCOURSE_DEVELOPER_EMAILS:.*/ DISCOURSE_DEVELOPER_EMAILS: \'$developer_emails\'/w $changelog" $web_file
c2d3ee4a
JA
535 if [ -s $changelog ]
536 then
537 rm $changelog
538 else
539 echo "DISCOURSE_DEVELOPER_EMAILS change failed."
540 update_ok="n"
541 fi
542
f5cf127d 543 sed -i -e "s/^ DISCOURSE_SMTP_ADDRESS:.*/ DISCOURSE_SMTP_ADDRESS: $smtp_address/w $changelog" $web_file
c2d3ee4a
JA
544 if [ -s $changelog ]
545 then
546 rm $changelog
547 else
548 echo "DISCOURSE_SMTP_ADDRESS change failed."
549 update_ok="n"
550 fi
551
f5cf127d 552 sed -i -e "s/^ #\?DISCOURSE_SMTP_PORT:.*/ DISCOURSE_SMTP_PORT: $smtp_port/w $changelog" $web_file
7c2777f9
JA
553 if [ -s $changelog ]
554 then
555 rm $changelog
556 else
557 echo "DISCOURSE_SMTP_PORT change failed."
558 update_ok="n"
559 fi
560
f5cf127d 561 sed -i -e "s/^ #\?DISCOURSE_SMTP_USER_NAME:.*/ DISCOURSE_SMTP_USER_NAME: $smtp_user_name/w $changelog" $web_file
c2d3ee4a
JA
562 if [ -s $changelog ]
563 then
564 rm $changelog
565 else
566 echo "DISCOURSE_SMTP_USER_NAME change failed."
567 update_ok="n"
568 fi
569
275735af 570 if [[ "$smtp_password" == *"\""* ]]
c2d3ee4a 571 then
275735af
JP
572 SLASH="BROKEN"
573 echo "========================================"
574 echo "WARNING!!!"
575 echo "Your password contains a quote (\")"
576 echo "Your SMTP Password will not be set. You will need to edit app.yml to enter it."
577 echo "========================================"
c2d3ee4a 578 update_ok="n"
275735af
JP
579 else
580 SLASH="|"
581 if [[ "$smtp_password" == *"$SLASH"* ]]
582 then SLASH="+"
583 if [[ "$smtp_password" == *"$SLASH"* ]]
584 then
585 SLASH="Q"
586 if [[ "$smtp_password" == *"$SLASH"* ]]
587 then
588 SLASH="BROKEN"
589 echo "========================================"
590 echo "WARNING!!!"
591 echo "Your password contains all available delimiters (+, |, and Q). "
592 echo "Your SMTP Password will not be set. You will need to edit app.yml to enter it."
593 echo "========================================"
594 update_ok="n"
595 fi
596 fi
597 fi
c2d3ee4a 598 fi
275735af
JP
599 if [[ "$SLASH" != "BROKEN" ]]
600 then
601 sed -i -e "s${SLASH}^ #\?DISCOURSE_SMTP_PASSWORD:.*${SLASH} DISCOURSE_SMTP_PASSWORD: \"${smtp_password}\"${SLASH}w $changelog" $web_file
c2d3ee4a 602
275735af
JP
603 if [ -s $changelog ]
604 then
605 rm $changelog
606 else
607 echo "DISCOURSE_SMTP_PASSWORD change failed."
608 update_ok="n"
609 fi
610 fi
f17af951 611
026a664e
RSS
612 echo "Enabling Let's Encrypt"
613 sed -i -e "s/^ #\?LETSENCRYPT_ACCOUNT_EMAIL:.*/ LETSENCRYPT_ACCOUNT_EMAIL: $letsencrypt_account_email/w $changelog" $web_file
614 if [ -s $changelog ]
615 then
616 rm $changelog
617 else
618 echo "LETSENCRYPT_ACCOUNT_EMAIL change failed."
619 update_ok="n"
620 fi
621 local src='^ #\?- "templates\/web.ssl.template.yml"'
622 local dst=' \- "templates\/web.ssl.template.yml"'
623 sed -i -e "s/$src/$dst/w $changelog" $web_file
624 if [ -s $changelog ]
625 then
991162e7 626 echo "web.ssl.template.yml enabled"
026a664e
RSS
627 else
628 update_ok="n"
629 echo "web.ssl.template.yml NOT ENABLED--was it on already?"
630 fi
631 local src='^ #\?- "templates\/web.letsencrypt.ssl.template.yml"'
632 local dst=' - "templates\/web.letsencrypt.ssl.template.yml"'
c2d3ee4a 633
026a664e
RSS
634 sed -i -e "s/$src/$dst/w $changelog" $web_file
635 if [ -s $changelog ]
636 then
637 echo "letsencrypt.ssl.template.yml enabled"
638 else
639 update_ok="n"
640 echo "letsencrypt.ssl.template.yml NOT ENABLED -- was it on already?"
641 fi
c2d3ee4a 642
991162e7
JP
643 echo
644 if [ $maxmind_license_key != "1234567890123456" ]
645 then
646 echo "Setting MAXMIND key to $maxmind_license_key in $web_file"
647 sed -i -e "s/^.*DISCOURSE_MAXMIND_LICENSE_KEY:.*/ DISCOURSE_MAXMIND_LICENSE_KEY: $maxmind_license_key/w $changelog" $web_file
648 if [ -s $changelog ]
649 then
650 rm $changelog
651 else
652 echo "DISCOURSE_MAXMIND_LICENSE_KEY change failed."
653 update_ok="n"
654 fi
655 fi
656
c2d3ee4a
JA
657 if [ "$update_ok" == "y" ]
658 then
659 echo -e "\nConfiguration file at $config_file updated successfully!\n"
660 else
661 echo -e "\nUnfortunately, there was an error changing $config_file\n"
7f2d6260 662 echo -d "This may happen if you have made unexpected changes."
c2d3ee4a
JA
663 exit 1
664 fi
665}
666
667##
668## is our config file valid? Does it have the required fields set?
669##
4b1b25e3 670validate_config() {
c2d3ee4a
JA
671
672 valid_config="y"
c87c4b0a 673
c2d3ee4a
JA
674 for x in DISCOURSE_SMTP_ADDRESS DISCOURSE_SMTP_USER_NAME DISCOURSE_SMTP_PASSWORD \
675 DISCOURSE_DEVELOPER_EMAILS DISCOURSE_HOSTNAME
676 do
04a06dd0
JP
677 read_config $x
678 local result=$read_config_result
679 read_default $x
680 local default=$read_default_result
c2d3ee4a 681
04a06dd0 682 if [ ! -z "$result" ]
c2d3ee4a 683 then
17f62d87 684 if [[ "$config_line" = *"$default"* ]]
c2d3ee4a 685 then
04a06dd0 686 echo "$x left at incorrect default of $default"
c2d3ee4a
JA
687 valid_config="n"
688 fi
689 config_val=`echo $config_line | awk '{print $2}'`
690 if [ -z $config_val ]
691 then
04a06dd0 692 echo "$x was not configured"
c2d3ee4a
JA
693 valid_config="n"
694 fi
695 else
696 echo "$x not present"
697 valid_config="n"
698 fi
699 done
c87c4b0a 700
c2d3ee4a 701 if [ "$valid_config" != "y" ]; then
f5cf127d
JP
702 echo -e "\nSorry, these $web_file settings aren't valid -- can't continue!"
703 echo "If you have unusual requirements, edit $web_file and then: "
d8613c71 704 echo "./launcher bootstrap $app_name"
c2d3ee4a
JA
705 exit 1
706 fi
707}
708
709
710##
711## template file names
712##
f5cf127d
JP
713
714if [ "$1" == "2container" ]
715then
716 app_name=web_only
717 data_name=data
718 web_template=samples/web_only.yml
719 data_template=samples/data.yml
720 web_file=containers/$app_name.yml
721 data_file=containers/$data_name.yml
722else
723 app_name=app
724 data_name=app
725 web_template=samples/standalone.yml
726 data_template=""
727 web_file=containers/$app_name.yml
728 data_file=containers/$app_name.yml
729fi
730 changelog=/tmp/changelog
c2d3ee4a 731
4b1b25e3
JA
732##
733## Check requirements before creating a copy of a config file we won't edit
734##
ebdd72f3 735check_root
18602189 736check_and_install_docker
642b870f 737check_disk_and_memory
991162e7 738assert_maxmind_license_key
642b870f 739
f5cf127d 740if [ -a "$web_file" ]
c2d3ee4a 741then
f5cf127d 742 echo "The configuration file $web_file already exists!"
f17af951
JP
743 echo
744 echo ". . . reconfiguring . . ."
745 echo
7f2d6260 746 echo
17f62d87
JP
747 DATE=`date +"%Y-%m-%d-%H%M%S"`
748 BACKUP=$app_name.yml.$DATE.bak
749 echo Saving old file as $BACKUP
275735af 750 cp $web_file containers/$BACKUP
7f2d6260
JP
751 echo "Stopping existing container in 5 seconds or Control-C to cancel."
752 sleep 5
753 ./launcher stop app
17f62d87 754 echo
c2d3ee4a 755else
f5cf127d
JP
756 check_ports
757 cp -v $web_template $web_file
758 if [ "$data_name" == "data" ]
759 then
760 echo "--------------------------------------------------"
275735af 761 echo "This two container setup is currently unsupported. Use at your own risk!"
f5cf127d
JP
762 echo "--------------------------------------------------"
763 DISCOURSE_DB_PASSWORD=`date +%s | sha256sum | base64 | head -c 20`
764
765 sed -i -e "s/DISCOURSE_DB_PASSWORD: SOME_SECRET/DISCOURSE_DB_PASSWORD: $DISCOURSE_DB_PASSWORD/w $changelog" $web_file
766 if [ -s $changelog ]
767 then
768 rm $changelog
769 else
770 echo "Problem changing DISCOURSE_DB_PASSWORD" in $web_file
771 fi
772
773 cp -v $data_template $data_file
774 quote=\'
775 sed -i -e "s/password ${quote}SOME_SECRET${quote}/password '$DISCOURSE_DB_PASSWORD'/w $changelog" $data_file
776 if [ -s $changelog ]
777 then
778 rm $changelog
779 else
780 echo "Problem changing DISCOURSE_DB_PASSWORD" in $data_file
781 fi
782 fi
c2d3ee4a
JA
783fi
784
c2d3ee4a 785scale_ram_and_cpu
4b1b25e3
JA
786ask_user_for_config
787validate_config
c2d3ee4a 788
4b1b25e3
JA
789##
790## if we reach this point without exiting, OK to proceed
f17af951 791## rebuild won't fail if there's nothing to rebuild and does the restart
4b1b25e3 792##
7f2d6260 793echo "Updates successful. Rebuilding in 5 seconds."
f17af951 794sleep 5 # Just a chance to ^C in case they were too fast on the draw
f5cf127d
JP
795if [ "$data_name" == "$app_name" ]
796then
797 echo Building $app_name
798 ./launcher rebuild $app_name
799else
800 echo Building $data_name now . . .
801 ./launcher rebuild $data_name
802 echo Building $app_name now . . .
803 ./launcher rebuild $app_name
804fi