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