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