remove debug code. Sorry.
[discourse_docker.git] / launcher
1 #!/bin/bash
2
3 usage () {
4 echo "Usage: launcher COMMAND CONFIG [--skip-prereqs]"
5 echo "Commands:"
6 echo " start: Start/initialize a container"
7 echo " stop: Stop a running container"
8 echo " restart: Restart a container"
9 echo " destroy: Stop and remove a container"
10 echo " enter: Use nsenter to enter a container"
11 echo " logs: Docker logs for container"
12 echo " bootstrap: Bootstrap a container for the config based on a template"
13 echo " rebuild: Rebuild a container (destroy old, bootstrap, start new)"
14 echo " cleanup: Remove all containers that have stopped for > 24 hours"
15 echo
16 echo "Options:"
17 echo " --skip-prereqs Don't check prerequisites or resource requirements"
18 echo " --docker-args Extra arguments to pass when running docker"
19 exit 1
20 }
21
22 command=$1
23 config=$2
24 opt=$3
25
26 # Docker doesn't like uppercase characters, spaces or special characters, catch it now before we build everything out and then find out
27 re='[A-Z/ !@#$%^&*()+~`=]'
28 if [[ $config =~ $re ]];
29 then
30 echo
31 echo "ERROR: Config name must not contain upper case characters, spaces or special characters. Correct config name and rerun $0."
32 echo
33 exit 1
34 fi
35
36 cd "$(dirname "$0")"
37
38 docker_min_version='1.6.0'
39 docker_rec_version='1.6.0'
40 git_min_version='1.8.0'
41 git_rec_version='1.8.0'
42
43 config_file=containers/"$config".yml
44 cidbootstrap=cids/"$config"_bootstrap.cid
45 local_discourse=local_discourse
46 image=discourse/discourse:1.0.17
47 docker_path=`which docker.io || which docker`
48 git_path=`which git`
49 template_path=samples/standalone.yml
50 changelog=/tmp/changelog # used to test whether sed did anything
51
52 if [ "${SUPERVISED}" = "true" ]; then
53 restart_policy="--restart=no"
54 attach_on_start="-a"
55 attach_on_run="-a stdout -a stderr"
56 else
57 attach_on_run="-d"
58 fi
59
60 if [ -n "$DOCKER_HOST" ]; then
61 docker_ip=`sed -e 's/^tcp:\/\/\(.*\):.*$/\1/' <<< "$DOCKER_HOST"`
62 elif [ -x "$(which ip 2>/dev/null)" ]; then
63 docker_ip=`ip addr show docker0 | \
64 grep 'inet ' | \
65 awk '{ split($2,a,"/"); print a[1] }';`
66 else
67 docker_ip=`ifconfig | \
68 grep -B1 "inet addr" | \
69 awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' | \
70 grep docker0 | \
71 awk -F: '{ print $3 }';`
72 fi
73
74 compare_version() {
75 declare -a ver_a
76 declare -a ver_b
77 IFS=. read -a ver_a <<< "$1"
78 IFS=. read -a ver_b <<< "$2"
79
80 while [[ -n $ver_a ]]; do
81 if (( ver_a > ver_b )); then
82 return 0
83 elif (( ver_b > ver_a )); then
84 return 1
85 else
86 unset ver_a[0]
87 ver_a=("${ver_a[@]}")
88 unset ver_b[0]
89 ver_b=("${ver_b[@]}")
90 fi
91 done
92 return 1 # They are equal
93 }
94
95
96 install_docker() {
97
98 echo "Docker is not installed, you will need to install Docker in order to run Discourse"
99 echo "Please visit https://docs.docker.com/installation/ for instructions on how to do this for your system"
100 echo
101 echo "If you are running a recent Ubuntu Server, try the following:"
102 echo "sudo apt-get install docker-engine"
103
104 exit 1
105 }
106
107 prereqs() {
108
109 if [ -z $docker_path ]; then
110 install_docker
111 fi
112
113 # 1. docker daemon running?
114 # we send stderr to /dev/null cause we don't care about warnings,
115 # it usually complains about swap which does not matter
116 test=`$docker_path info 2> /dev/null`
117 if [[ $? -ne 0 ]] ; then
118 echo "Cannot connect to the docker daemon - verify it is running and you have access"
119 exit 1
120 fi
121
122 # 2. running aufs or btrfs
123 test=`$docker_path info 2> /dev/null | grep 'Driver: '`
124 if [[ "$test" =~ [aufs|btrfs|zfs|overlay] ]] ; then : ; else
125 echo "Your Docker installation is not using a supported filesystem if we were to proceed you may have a broken install."
126 echo "aufs is the recommended filesystem you should be using (zfs/btrfs and overlay may work as well)"
127 echo "You can tell what filesystem you are using by running \"docker info\" and looking at the driver"
128 echo
129 echo "If you wish to continue anyway using your existing unsupported filesystem, "
130 echo "read the source code of launcher and figure out how to bypass this."
131 exit 1
132 fi
133
134 # 3. running recommended docker version
135 test=($($docker_path --version)) # Get docker version string
136 test=${test[2]//,/} # Get version alone and strip comma if exists
137
138 # At least minimum version
139 if compare_version "${docker_min_version}" "${test}"; then
140 echo "ERROR: Docker version ${test} not supported, please upgrade to at least ${docker_min_version}, or recommended ${docker_rec_version}"
141 exit 1
142 fi
143
144 # Recommend best version
145 if compare_version "${docker_rec_version}" "${test}"; then
146 echo "WARNING: Docker version ${test} deprecated, recommend upgrade to ${docker_rec_version} or newer."
147 fi
148
149 # 4. discourse docker image is downloaded
150 test=`$docker_path images | awk '{print $1 ":" $2 }' | grep "$image"`
151
152 if [ -z "$test" ]; then
153 echo
154 echo "WARNING: We are about to start downloading the Discourse base image"
155 echo "This process may take anywhere between a few minutes to an hour, depending on your network speed"
156 echo
157 echo "Please be patient"
158 echo
159
160 fi
161
162 # 5. running recommended git version
163 test=($($git_path --version)) # Get git version string
164 test=${test[2]//,/} # Get version alone and strip comma if exists
165
166 # At least minimum version
167 if compare_version "${git_min_version}" "${test}"; then
168 echo "ERROR: Git version ${test} not supported, please upgrade to at least ${git_min_version}, or recommended ${git_rec_version}"
169 exit 1
170 fi
171
172 # Recommend best version
173 if compare_version "${git_rec_version}" "${test}"; then
174 echo "WARNING: Git version ${test} deprecated, recommend upgrade to ${git_rec_version} or newer."
175 fi
176
177 # 6. able to attach stderr / out / tty
178 test=`$docker_path run $user_args -i --rm -a stdout -a stderr $image echo working`
179 if [[ "$test" =~ "working" ]] ; then : ; else
180 echo "Your Docker installation is not working correctly"
181 echo
182 echo "See: https://meta.discourse.org/t/docker-error-on-bootstrap/13657/18?u=sam"
183 exit 1
184 fi
185
186 }
187
188
189
190 check_resources() {
191 # Memory
192 resources="ok"
193 avail_mem="$(LANG=C free -m | grep '^Mem:' | awk '{print $2}')"
194 if [ "$avail_mem" -lt 900 ]; then
195 resources="insufficient"
196 echo "WARNING: You do not appear to have sufficient memory to run Discourse."
197 echo
198 echo "Your system may not work properly, or future upgrades of Discourse may"
199 echo "not complete successfully."
200 echo
201 echo "See https://github.com/discourse/discourse/blob/master/docs/INSTALL-cloud.md#create-new-cloud-server"
202 elif [ "$avail_mem" -lt 1800 ]; then
203 total_swap="$(LANG=C free -m | grep ^Swap: | awk '{print $2}')"
204 if [ "$total_swap" -lt 1000 ]; then
205 resources="insufficient"
206 echo "WARNING: You must have at least 1GB of swap when running with less"
207 echo "than 2GB of RAM."
208 echo
209 echo "Your system may not work properly, or future upgrades of Discourse may"
210 echo "not complete successfully."
211 echo
212 echo "See https://github.com/discourse/discourse/blob/master/docs/INSTALL-cloud.md#set-up-swap-if-needed"
213 fi
214 fi
215
216 # Disk space
217 free_disk="$(df /var | tail -n 1 | awk '{print $4}')"
218 if [ "$free_disk" -lt 5000 ]; then
219 resources="insufficient"
220 echo "WARNING: You must have at least 5GB of *free* disk space to run Discourse."
221 echo
222 echo "Insufficient disk space may result in problems running your site, and may"
223 echo "not even allow Discourse installation to complete successfully."
224 echo
225 echo "Please free up some space, or expand your disk, before continuing."
226 echo
227 echo "Run \`apt-get autoremove && apt-get autoclean\` to clean up unused packages and \`./launcher cleanup\` to remove stale Docker containers."
228 exit 1
229 fi
230
231 if [ -t 0 ] && [ "$resources" != "ok" ]; then
232 echo
233 read -p "Press ENTER to continue, or Ctrl-C to exit and give your system more resources"
234 fi
235 }
236
237 check_ports() {
238 local valid=$(netstat -tln | awk '{print $4}' | grep ":${1}\$")
239
240 if [ -n "$valid" ]; then
241 echo "Launcher has detected that port ${1} is in use."
242 echo
243 echo "If you are trying to run Discourse simultaneously with another web server like Apache or nginx, you will need to bind to a different port."
244 echo "See https://meta.discourse.org/t/17247 for help."
245 echo "To continue anyway, re-run Launcher with --skip-prereqs"
246 exit 1
247 fi
248 }
249
250 if [ "$opt" != "--skip-prereqs" ] ; then
251 prereqs
252 fi
253
254 if [ "$opt" == "--docker-args" ] ; then
255 user_args=$4
256 else
257 user_args=""
258 fi
259
260 host_run() {
261 read -r -d '' env_ruby << 'RUBY'
262 require 'yaml'
263
264 input = STDIN.readlines.join
265 yaml = YAML.load(input)
266
267 if host_run = yaml['host_run']
268 params = yaml['params'] || {}
269 host_run.each do |run|
270 params.each do |k,v|
271 run = run.gsub("$#{k}", v)
272 end
273 STDOUT.write "#{run}--SEP--"
274 end
275 end
276 RUBY
277
278 host_run=`cat $config_file | $docker_path run $user_args --rm -i -a stdout -a stdin $image ruby -e "$env_ruby"`
279
280 while [ "$host_run" ] ; do
281 iter=${host_run%%--SEP--*}
282 echo
283 echo "Host run: $iter"
284 $iter || exit 1
285 echo
286 host_run="${host_run#*--SEP--}"
287 done
288 }
289
290
291 set_volumes() {
292 volumes=`cat $config_file | $docker_path run $user_args --rm -i -a stdout -a stdin $image ruby -e \
293 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['volumes'].map{|v| '-v ' << v['volume']['host'] << ':' << v['volume']['guest'] << ' '}.join"`
294 }
295
296 set_links() {
297 links=`cat $config_file | $docker_path run $user_args --rm -i -a stdout -a stdin $image ruby -e \
298 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['links'].map{|l| '--link ' << l['link']['name'] << ':' << l['link']['alias'] << ' '}.join"`
299 }
300
301 set_template_info() {
302
303 templates=`cat $config_file | $docker_path run $user_args --rm -i -a stdin -a stdout $image ruby -e \
304 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['templates']"`
305
306 arrTemplates=(${templates// / })
307 config_data=$(cat $config_file)
308
309 input="hack: true"
310
311 for template in "${arrTemplates[@]}"
312 do
313 [ ! -z $template ] && {
314 input="$input _FILE_SEPERATOR_ $(cat $template)"
315 }
316 done
317
318 # we always want our config file last so it takes priority
319 input="$input _FILE_SEPERATOR_ $config_data"
320
321 read -r -d '' env_ruby << 'RUBY'
322 require 'yaml'
323
324 input=STDIN.readlines.join
325 # default to UTF-8 for the dbs sake
326 env = {'LANG' => 'en_US.UTF-8'}
327 input.split('_FILE_SEPERATOR_').each do |yml|
328 yml.strip!
329 begin
330 env.merge!(YAML.load(yml)['env'] || {})
331 rescue Psych::SyntaxError => e
332 puts e
333 puts "*ERROR."
334 rescue => e
335 puts yml
336 p e
337 end
338 end
339 puts env.map{|k,v| "-e\n#{k}=#{v}" }.join("\n")
340 RUBY
341
342 raw=`exec echo "$input" | $docker_path run $user_args --rm -i -a stdin -a stdout $image ruby -e "$env_ruby"`
343
344 env=()
345 ok=1
346 while read i; do
347 if [ "$i" == "*ERROR." ]; then
348 ok=0
349 elif [ -n "$i" ]; then
350 env[${#env[@]}]=$i
351 fi
352 done <<< "$raw"
353
354 if [ "$ok" -ne 1 ]; then
355 echo "${env[@]}"
356 echo "YAML syntax error. Please check your /var/discourse/containers/*.yml config files."
357 exit 1
358 fi
359 }
360
361 if [ -z $docker_path ]; then
362 install_docker
363 fi
364
365 [ "$command" == "cleanup" ] && {
366 echo
367 echo "The following command will"
368 echo "- Delete all docker images for old containers"
369 echo "- Delete all stopped and orphan containers"
370 echo
371 read -p "Are you sure (Y/n): " -n 1 -r && echo
372 if [[ $REPLY =~ ^[Yy]$ || ! $REPLY ]]
373 then
374 space=$(df /var/lib/docker | awk '{ print $4 }' | grep -v Available)
375 echo "Starting Cleanup (bytes free $space)"
376
377 STATE_DIR=./.gc-state scripts/docker-gc
378
379 space=$(df /var/lib/docker | awk '{ print $4 }' | grep -v Available)
380 echo "Finished Cleanup (bytes free $space)"
381
382 else
383 exit 1
384 fi
385 exit 0
386 }
387
388 [ $# -lt 2 ] && {
389 usage
390 }
391
392 if [[ ! -e $config_file && $command -ne "memconfig" ]]
393 then
394 echo "Config file was not found, ensure $config_file exists"
395 echo
396 echo "Available configs ( `cd containers && ls -dm *.yml | tr -s '\n' ' ' | awk '{ gsub(/\.yml/, ""); print }'`)"
397 exit 1
398 fi
399
400 docker_version=($($docker_path --version))
401 docker_version=${test[2]//,/}
402 restart_policy=${restart_policy:---restart=always}
403
404 set_existing_container(){
405 existing=`$docker_path ps -a | awk '{ print $1, $(NF) }' | grep " $config$" | awk '{ print $1 }'`
406 }
407
408 run_stop() {
409
410 set_existing_container
411
412 if [ ! -z $existing ]
413 then
414 (
415 set -x
416 $docker_path stop -t 10 $config
417 )
418 else
419 echo "$config was not started !"
420 exit 1
421 fi
422 }
423
424 set_run_image() {
425 run_image=`cat $config_file | $docker_path run $user_args --rm -i -a stdin -a stdout $image ruby -e \
426 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['run_image']"`
427
428 if [ -z "$run_image" ]; then
429 run_image="$local_discourse/$config"
430 fi
431 }
432
433 set_boot_command() {
434 boot_command=`cat $config_file | $docker_path run $user_args --rm -i -a stdin -a stdout $image ruby -e \
435 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['boot_command']"`
436
437 if [ -z "$boot_command" ]; then
438
439 no_boot_command=`cat $config_file | $docker_path run $user_args --rm -i -a stdin -a stdout $image ruby -e \
440 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['no_boot_command']"`
441
442 if [ -z "$no_boot_command" ]; then
443 boot_command="/sbin/boot"
444 fi
445 fi
446 }
447
448 scale_ram_and_cpu() {
449
450 # grab info about total system ram and physical (NOT LOGICAL!) CPU cores
451 avail_mem="$(LANG=C free -m | grep '^Mem:' | awk '{print $2}')"
452 avail_gb=$(( $avail_mem / 950 ))
453 avail_cores=`cat /proc/cpuinfo | grep "cpu cores" | uniq | awk '{print $4}'`
454 echo "Found ${avail_gb}GB of memory and $avail_cores physical CPU cores"
455
456 # db_shared_buffers: 128MB for 1GB, 256MB for 2GB, or 256MB * GB, max 4096MB
457 if [ "$avail_gb" -eq "1" ]
458 then
459 db_shared_buffers=128
460 else
461 if [ "$avail_gb" -eq "2" ]
462 then
463 db_shared_buffers=256
464 else
465 db_shared_buffers=$(( 256 * $avail_gb ))
466 fi
467 fi
468 db_shared_buffers=$(( db_shared_buffers < 4096 ? db_shared_buffers : 4096 ))
469
470 sed -i -e "s/^ #db_shared_buffers:.*/ db_shared_buffers: \"${db_shared_buffers}MB\"/w $changelog" $config_file
471 if [ -s $changelog ]
472 then
473 echo "setting db_shared_buffers = ${db_shared_buffers}MB based on detected CPU/RAM"
474 rm $changelog
475 fi
476
477
478 # UNICORN_WORKERS: 2 * GB for 2GB or less, or 2 * CPU, max 8
479 if [ "$avail_gb" -le "2" ]
480 then
481 unicorn_workers=$(( 2 * $avail_gb ))
482 else
483 unicorn_workers=$(( 2 * $avail_cores ))
484 fi
485 unicorn_workers=$(( unicorn_workers < 8 ? unicorn_workers : 8 ))
486
487 sed -i -e "s/^ #UNICORN_WORKERS:.*/ UNICORN_WORKERS: ${unicorn_workers}/w $changelog" $config_file
488 if [ -s $changelog ]
489 then
490 echo "setting UNICORN_WORKERS = ${unicorn_workers} based on detected CPU/RAM"
491 rm $changelog
492 fi
493
494 }
495
496 run_start() {
497
498 existing=`$docker_path ps | awk '{ print $1, $(NF) }' | grep " $config$" | awk '{ print $1 }'`
499 echo $existing
500 if [ ! -z $existing ]
501 then
502 echo "Nothing to do, your container has already started!"
503 exit 0
504 fi
505
506 existing=`$docker_path ps -a | awk '{ print $1, $(NF) }' | grep " $config$" | awk '{ print $1 }'`
507 if [ ! -z $existing ]
508 then
509 echo "starting up existing container"
510 (
511 set -x
512 $docker_path start $config
513 )
514 exit 0
515 fi
516
517 host_run
518 ports=`cat $config_file | $docker_path run $user_args --rm -i -a stdout -a stdin $image ruby -e \
519 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['expose'].map{|p| \"-p #{p}\"}.join(' ')"`
520
521 IFS='-p ' read -a array <<< "$ports"
522 for element in "${array[@]}"
523 do
524 IFS=':' read -a args <<< "$element"
525 if [ "${#args[@]}" == "2" ]; then
526 check_ports "${args[0]}"
527 elif [ "${#args[@]}" == "3" ]; then
528 check_ports "${args[1]}"
529 fi
530 done
531
532 docker_args=`cat $config_file | $docker_path run $user_args --rm -i -a stdout -a stdin $image ruby -e \
533 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['docker_args']"`
534
535 set_template_info
536 set_volumes
537 set_links
538 set_run_image
539 set_boot_command
540
541 # get hostname and settings from container configuration
542 for envar in "${env[@]}"
543 do
544 if [[ $envar == DOCKER_USE_HOSTNAME* ]] || [[ $envar == DISCOURSE_HOSTNAME* ]]
545 then
546 # use as environment variable
547 eval $envar
548 fi
549 done
550
551 (
552 hostname=`hostname -s`
553 # overwrite hostname
554 if [ "$DOCKER_USE_HOSTNAME" = "true" ]
555 then
556 hostname=$DISCOURSE_HOSTNAME
557 else
558 hostname=$hostname-$config
559 fi
560
561 # we got to normalize so we only have allowed strings, this is more comprehensive but lets see how bash does first
562 # hostname=`$docker_path run $user_args --rm $image ruby -e 'print ARGV[0].gsub(/[^a-zA-Z-]/, "-")' $hostname`
563 # docker added more hostname rules
564 hostname=${hostname/_/-}
565
566 set -x
567 $docker_path run $user_args $links $attach_on_run $restart_policy "${env[@]}" -h "$hostname" \
568 -e DOCKER_HOST_IP=$docker_ip --name $config -t $ports $volumes $docker_args $run_image $boot_command
569
570 )
571 exit 0
572
573 }
574
575 valid_config_check() {
576
577 valid_config="y"
578 for x in DISCOURSE_SMTP_ADDRESS DISCOURSE_SMTP_USER_NAME DISCOURSE_SMTP_PASSWORD \
579 DISCOURSE_DEVELOPER_EMAILS DISCOURSE_HOSTNAME
580 do
581 mail_var=`grep "^ $x:" $config_file`
582 local result=$?
583 local default="example.com"
584 if (( result == 0 ))
585 then
586 if [[ $mail_var = *"$default"* ]]
587 then
588 echo "Warning: $x left at incorrect default of example.com"
589 valid_config="n"
590 fi
591 else
592 echo "Warning: $x not configured"
593 valid_config="n"
594 fi
595 done
596 if [ -t 0 ] && [ "$valid_config" != "y" ]; then
597 echo
598 read -p "Press Ctrl-C to exit and edit $config_file or ENTER to continue"
599 fi
600 }
601
602 run_bootstrap() {
603
604 # Does your system meet the minimum requirements?
605 if [ "$opt" != "--skip-prereqs" ] ; then
606 check_resources
607 fi
608
609 # is our configuration file valid?
610 valid_config_check
611
612 # make minor scaling adjustments for RAM and CPU
613 scale_ram_and_cpu
614
615 # I got no frigging clue what this does, ask Sam Saffron. It RUNS STUFF ON THE HOST I GUESS?
616 host_run
617
618 # Is the image available?
619 # If not, pull it here so the user is aware what's happening.
620 $docker_path history $image >/dev/null 2>&1 || $docker_path pull $image
621
622 set_template_info
623
624 base_image=`cat $config_file | $docker_path run $user_args --rm -i -a stdin -a stdout $image ruby -e \
625 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['base_image']"`
626
627 update_pups=`cat $config_file | $docker_path run $user_args --rm -i -a stdin -a stdout $image ruby -e \
628 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['update_pups']"`
629
630 if [[ ! X"" = X"$base_image" ]]; then
631 image=$base_image
632 fi
633
634 set_volumes
635 set_links
636
637 rm -f $cidbootstrap
638
639 run_command="cd /pups &&"
640 if [[ ! "false" = $update_pups ]]; then
641 run_command="$run_command git pull &&"
642 fi
643 run_command="$run_command /pups/bin/pups --stdin"
644
645 echo $run_command
646
647 (exec echo "$input" | $docker_path run $user_args $links "${env[@]}" -e DOCKER_HOST_IP=$docker_ip --cidfile $cidbootstrap -i -a stdin -a stdout -a stderr $volumes $image \
648 /bin/bash -c "$run_command") \
649 || ($docker_path rm `cat $cidbootstrap` && rm $cidbootstrap)
650
651 [ ! -e $cidbootstrap ] && echo "** FAILED TO BOOTSTRAP ** please scroll up and look for earlier error messages, there may be more than one" && exit 1
652
653 sleep 5
654
655 $docker_path commit `cat $cidbootstrap` $local_discourse/$config || echo 'FAILED TO COMMIT'
656 $docker_path rm `cat $cidbootstrap` && rm $cidbootstrap
657 }
658
659
660
661 case "$command" in
662 bootstrap)
663 run_bootstrap
664 echo "Successfully bootstrapped, to startup use ./launcher start $config"
665 exit 0
666 ;;
667
668 enter)
669 exec $docker_path exec -it $config /bin/bash --login
670 ;;
671
672 stop)
673 run_stop
674 exit 0
675 ;;
676
677 logs)
678
679 $docker_path logs $config
680 exit 0
681 ;;
682
683 restart)
684 run_stop
685 run_start
686 exit 0
687 ;;
688
689 start)
690 run_start
691 exit 0
692 ;;
693
694 rebuild)
695 if [ "$(git symbolic-ref --short HEAD)" == "master" ]; then
696 echo "Ensuring discourse docker is up to date"
697
698 git remote update
699
700 LOCAL=$(git rev-parse @)
701 REMOTE=$(git rev-parse @{u})
702 BASE=$(git merge-base @ @{u})
703
704 if [ $LOCAL = $REMOTE ]; then
705 echo "Discourse Docker is up-to-date"
706
707 elif [ $LOCAL = $BASE ]; then
708 echo "Updating Discourse Docker"
709 git pull || (echo 'failed to update' && exit 1)
710 exec /bin/bash $0 $@
711
712 elif [ $REMOTE = $BASE ]; then
713 echo "Your version of Discourse Docker is ahead of origin"
714
715 else
716 echo "Discourse Docker has diverged source, this is only expected in Dev mode"
717 fi
718
719 fi
720
721 set_existing_container
722
723 if [ ! -z $existing ]
724 then
725 echo "Stopping old container"
726 (
727 set -x
728 $docker_path stop -t 10 $config
729 )
730 fi
731
732 run_bootstrap
733
734 if [ ! -z $existing ]
735 then
736 echo "Removing old container"
737 (
738 set -x
739 $docker_path rm $config
740 )
741 fi
742
743 run_start
744 exit 0
745 ;;
746
747
748 destroy)
749 (set -x; $docker_path stop -t 10 $config && $docker_path rm $config) || (echo "$config was not found" && exit 0)
750 exit 0
751 ;;
752 esac
753
754 usage