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