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