Use docker-gc by spotify for cleanup
[discourse_docker.git] / launcher
1 #!/bin/bash
2
3 command=$1
4 config=$2
5 opt=$3
6
7 # Docker doesn't like uppercase characters, spaces or special characters, catch it now before we build everything out and then find out
8 re='[A-Z/ !@#$%^&*()+~`=]'
9 if [[ $config =~ $re ]];
10 then
11 echo
12 echo "ERROR: Config name must not contain upper case characters, spaces or special characters. Correct config name and rerun $0."
13 echo
14 exit 1
15 fi
16
17 cd "$(dirname "$0")"
18
19 docker_min_version='1.2.0'
20 docker_rec_version='1.2.0'
21
22 config_file=containers/"$config".yml
23 cidbootstrap=cids/"$config"_bootstrap.cid
24 local_discourse=local_discourse
25 image=samsaffron/discourse:1.0.12
26 docker_path=`which docker.io || which docker`
27
28 if [ "${SUPERVISED}" = "true" ]; then
29 restart_policy="--restart=no"
30 attach_on_start="-a"
31 attach_on_run="-a stdout -a stderr"
32 else
33 attach_on_run="-d"
34 fi
35
36 if [ -n "$DOCKER_HOST" ]; then
37 docker_ip=`sed -e 's/^tcp:\/\/\(.*\):.*$/\1/' <<< "$DOCKER_HOST"`
38 elif [ -x "$(which ip 2>/dev/null)" ]; then
39 docker_ip=`ip addr show docker0 | \
40 grep 'inet ' | \
41 awk '{ split($2,a,"/"); print a[1] }';`
42 else
43 docker_ip=`ifconfig | \
44 grep -B1 "inet addr" | \
45 awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' | \
46 grep docker0 | \
47 awk -F: '{ print $3 }';`
48 fi
49
50
51 usage () {
52 echo "Usage: launcher COMMAND CONFIG [--skip-prereqs]"
53 echo "Commands:"
54 echo " start: Start/initialize a container"
55 echo " stop: Stop a running container"
56 echo " restart: Restart a container"
57 echo " destroy: Stop and remove a container"
58 echo " enter: Use nsenter to enter a container"
59 echo " ssh: Start a bash shell in a running container"
60 echo " logs: Docker logs for container"
61 echo " bootstrap: Bootstrap a container for the config based on a template"
62 echo " rebuild: Rebuild a container (destroy old, bootstrap, start new)"
63 echo " cleanup: Remove all containers that have stopped for > 24 hours"
64 echo
65 echo "Options:"
66 echo " --skip-prereqs Don't check prerequisites"
67 echo " --docker-args Extra arguments to pass when running docker"
68 exit 1
69 }
70
71 compare_version() {
72 declare -a ver_a
73 declare -a ver_b
74 IFS=. read -a ver_a <<< "$1"
75 IFS=. read -a ver_b <<< "$2"
76
77 while [[ -n $ver_a ]]; do
78 if (( ver_a > ver_b )); then
79 return 0
80 elif (( ver_b > ver_a )); then
81 return 1
82 else
83 unset ver_a[0]
84 ver_a=("${ver_a[@]}")
85 unset ver_b[0]
86 ver_b=("${ver_b[@]}")
87 fi
88 done
89 return 1 # They are equal
90 }
91
92 prereqs() {
93
94 # 1. docker daemon running?
95 # we send stderr to /dev/null cause we don't care about warnings,
96 # it usually complains about swap which does not matter
97 test=`$docker_path info 2> /dev/null`
98
99 if [[ $? -ne 0 ]] ; then
100 echo "Cannot connect to the docker daemon - verify it is running and you have access"
101 exit 1
102 fi
103
104 # 2. running aufs or btrfs
105 test=`$docker_path info 2> /dev/null | grep 'Driver: '`
106 if [[ "$test" =~ [aufs|btrfs|zfs|overlay] ]] ; then : ; else
107 echo "Your Docker installation is not using a supported filesystem if we were to proceed you may have a broken install."
108 echo "aufs is the recommended filesystem you should be using (zfs/btrfs and overlay may work as well)"
109 echo "You can tell what filesystem you are using by running \"docker info\" and looking at the driver"
110 echo ""
111 echo "If you wish to continue anyway using your existing unsupported filesystem"
112 echo "read the source code of launcher and figure out how to bypass this."
113 exit 1
114 fi
115
116 # 3. running recommended docker version
117 test=($($docker_path --version)) # Get docker version string
118 test=${test[2]//,/} # Get version alone and strip comma if exists
119
120 [[ "$test" =~ "0.12.0" ]] && echo "You are running a broken version of Docker, please upgrade ASAP. See: https://meta.discourse.org/t/the-installation-stopped-in-the-middle/16311/ for more details." && exit 1
121
122 # At least minimum version
123 if compare_version "${docker_min_version}" "${test}"; then
124 echo "ERROR: Docker version ${test} not supported, please upgrade to at least ${docker_min_version}, or recommended ${docker_rec_version}"
125 exit 1
126 fi
127
128 # Recommend best version
129 if compare_version "${docker_rec_version}" "${test}"; then
130 echo "WARNING: Docker version ${test} deprecated, recommend upgrade to ${docker_rec_version} or newer."
131 fi
132
133 # 4. discourse docker image is downloaded
134 test=`$docker_path images | awk '{print $1 ":" $2 }' | grep "$image"`
135
136 if [ -z "$test" ]; then
137 echo
138 echo "WARNING: We are about to start downloading the Discourse base image"
139 echo "This process may take anywhere between a few minutes to an hour, depending on your network speed"
140 echo
141 echo "Please be patient"
142 echo
143
144 fi
145
146 # 5. able to attach stderr / out / tty
147 test=`$docker_path run $user_args -i --rm -a stdout -a stderr $image echo working`
148 if [[ "$test" =~ "working" ]] ; then : ; else
149 echo "Your Docker installation is not working correctly"
150 echo
151 echo "See: https://meta.discourse.org/t/docker-error-on-bootstrap/13657/18?u=sam"
152 exit 1
153 fi
154 }
155
156 if [ "$opt" != "--skip-prereqs" ] ; then
157 prereqs
158 fi
159
160 if [ "$opt" == "--docker-args" ] ; then
161 user_args=$4
162 else
163 user_args=""
164 fi
165
166 get_ssh_pub_key() {
167 local ${ssh_key_locations}
168 ssh_key_locations=(
169 ~/.ssh/id_ed25519.pub
170 ~/.ssh/id_ecdsa.pub
171 ~/.ssh/id_rsa.pub
172 ~/.ssh/id_dsa.pub
173 ~core/.ssh/authorized_keys
174 )
175
176 local $keyfile
177 for keyfile in "${ssh_key_locations[@]}"; do
178 if [[ -e ${keyfile} ]] ; then
179 ssh_pub_key="$(cat ${keyfile})"
180 return 0
181 fi
182 done
183
184 return 0
185 }
186
187
188 install_docker() {
189
190 echo "Docker is not installed, make sure you are running on the 3.8 kernel"
191 echo "The best supported Docker release is Ubuntu 12.04.03 for it run the following"
192 echo
193 echo "sudo apt-get update"
194 echo "sudo apt-get install linux-image-generic-lts-raring linux-headers-generic-lts-raring"
195 echo "sudo reboot"
196 echo
197
198 echo "sudo sh -c \"wget -qO- https://get.docker.io/gpg | apt-key add -\""
199 echo "sudo sh -c \"echo deb http://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list\""
200 echo "sudo apt-get update"
201 echo "sudo apt-get install lxc-docker"
202
203 exit 1
204 }
205
206 host_run() {
207 read -r -d '' env_ruby << 'RUBY'
208 require 'yaml'
209
210 input = STDIN.readlines.join
211 yaml = YAML.load(input)
212
213 if host_run = yaml['host_run']
214 params = yaml['params'] || {}
215 host_run.each do |run|
216 params.each do |k,v|
217 run = run.gsub("$#{k}", v)
218 end
219 STDOUT.write "#{run}--SEP--"
220 end
221 end
222 RUBY
223
224 host_run=`cat $config_file | $docker_path run $user_args --rm -i -a stdout -a stdin $image ruby -e "$env_ruby"`
225
226 while [ "$host_run" ] ; do
227 iter=${host_run%%--SEP--*}
228 echo
229 echo "Host run: $iter"
230 $iter || exit 1
231 echo
232 host_run="${host_run#*--SEP--}"
233 done
234 }
235
236
237 set_volumes() {
238 volumes=`cat $config_file | $docker_path run $user_args --rm -i -a stdout -a stdin $image ruby -e \
239 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['volumes'].map{|v| '-v ' << v['volume']['host'] << ':' << v['volume']['guest'] << ' '}.join"`
240 }
241
242 set_links() {
243 links=`cat $config_file | $docker_path run $user_args --rm -i -a stdout -a stdin $image ruby -e \
244 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['links'].map{|l| '--link ' << l['link']['name'] << ':' << l['link']['alias'] << ' '}.join"`
245 }
246
247 set_template_info() {
248
249 templates=`cat $config_file | $docker_path run $user_args --rm -i -a stdin -a stdout $image ruby -e \
250 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['templates']"`
251
252
253 arrTemplates=(${templates// / })
254 config_data=$(cat $config_file)
255
256 input="hack: true"
257
258
259 for template in "${arrTemplates[@]}"
260 do
261 [ ! -z $template ] && {
262 input="$input _FILE_SEPERATOR_ $(cat $template)"
263 }
264 done
265
266 # we always want our config file last so it takes priority
267 input="$input _FILE_SEPERATOR_ $config_data"
268
269 read -r -d '' env_ruby << 'RUBY'
270 require 'yaml'
271
272 input=STDIN.readlines.join
273 # default to UTF-8 for the dbs sake
274 env = {'LANG' => 'en_US.UTF-8'}
275 input.split('_FILE_SEPERATOR_').each do |yml|
276 yml.strip!
277 begin
278 env.merge!(YAML.load(yml)['env'] || {})
279 rescue Psych::SyntaxError => e
280 puts e
281 puts "*ERROR."
282 rescue => e
283 puts yml
284 p e
285 end
286 end
287 puts env.map{|k,v| "-e\n#{k}=#{v}" }.join("\n")
288 RUBY
289
290 raw=`exec echo "$input" | $docker_path run $user_args --rm -i -a stdin -a stdout $image ruby -e "$env_ruby"`
291
292 env=()
293 ok=1
294 while read i; do
295 if [ "$i" == "*ERROR." ]; then
296 ok=0
297 elif [ -n "$i" ]; then
298 env[${#env[@]}]=$i
299 fi
300 done <<< "$raw"
301
302 if [ "$ok" -ne 1 ]; then
303 echo "${env[@]}"
304 echo "YAML syntax error. Please check your /var/discourse/containers/*.yml config files."
305 exit 1
306 fi
307 }
308
309 [ -z $docker_path ] && {
310 install_docker
311 }
312
313 [ "$command" == "cleanup" ] && {
314 echo
315 echo "The following command will"
316 echo "- Delete all docker images for old containers"
317 echo "- Delete all stopped and orphan containers"
318 echo
319 read -p "Are you sure (Y/n): " -n 1 -r && echo
320 if [[ $REPLY =~ ^[Yy]$ || ! $REPLY ]]
321 then
322 space=$(df /var/lib/docker | awk '{ print $4 }' | grep -v Available)
323 echo "Starting Cleanup (bytes free $space)"
324
325 STATE_DIR=./.gc-state scripts/docker-gc
326
327 space=$(df /var/lib/docker | awk '{ print $4 }' | grep -v Available)
328 echo "Finished Cleanup (bytes free $space)"
329
330 else
331 exit 1
332 fi
333 exit 0
334 }
335
336 [ $# -lt 2 ] && {
337 usage
338 }
339
340 if [ ! -e $config_file ]
341 then
342 echo "Config file was not found, ensure $config_file exists"
343 echo ""
344 echo "Available configs ( `cd containers && ls -dm *.yml | tr -s '\n' ' ' | awk '{ gsub(/\.yml/, ""); print }'`)"
345 exit 1
346 fi
347
348
349 docker_version=($($docker_path --version))
350 docker_version=${test[2]//,/}
351
352 if compare_version "1.2.0" "$docker_version"; then
353 echo "We recommend you upgrade docker, the version you are running has no restart policies, on reboot your container may not start up"
354 restart_policy=""
355 else
356 restart_policy=${restart_policy:---restart=always}
357 fi
358
359 set_existing_container(){
360 existing=`$docker_path ps -a | awk '{ print $1, $(NF) }' | grep " $config$" | awk '{ print $1 }'`
361 }
362
363 run_stop(){
364
365 set_existing_container
366
367 if [ ! -z $existing ]
368 then
369 (
370 set -x
371 $docker_path stop -t 10 $config
372 )
373 else
374 echo "$config was not started !"
375 exit 1
376 fi
377 }
378
379 run_start(){
380
381 existing=`$docker_path ps | awk '{ print $1, $(NF) }' | grep " $config$" | awk '{ print $1 }'`
382 echo $existing
383 if [ ! -z $existing ]
384 then
385 echo "Nothing to do, your container has already started!"
386 exit 1
387 fi
388
389 existing=`$docker_path ps -a | awk '{ print $1, $(NF) }' | grep " $config$" | awk '{ print $1 }'`
390 if [ ! -z $existing ]
391 then
392 echo "starting up existing container"
393 (
394 set -x
395 $docker_path start $config
396 )
397 exit 0
398 fi
399
400 host_run
401 ports=`cat $config_file | $docker_path run $user_args --rm -i -a stdout -a stdin $image ruby -e \
402 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['expose'].map{|p| '-p ' << p.to_s << ' '}.join"`
403
404 set_template_info
405 set_volumes
406 set_links
407
408 (
409 hostname=`hostname`
410 set -x
411 $docker_path run $user_args $links $attach_on_run $restart_policy "${env[@]}" -h "$hostname-$config" \
412 -e DOCKER_HOST_IP=$docker_ip --name $config -t $ports $volumes $local_discourse/$config /sbin/boot
413
414 )
415 exit 0
416
417 }
418
419 run_bootstrap(){
420
421 host_run
422
423 get_ssh_pub_key
424
425 # Is the image available?
426 # If not, pull it here so the user is aware what's happening.
427 $docker_path history $image >/dev/null 2>&1 || $docker_path pull $image
428
429 set_template_info
430
431 base_image=`cat $config_file | $docker_path run $user_args --rm -i -a stdin -a stdout $image ruby -e \
432 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['base_image']"`
433
434 update_pups=`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)['update_pups']"`
436
437 if [[ ! X"" = X"$base_image" ]]; then
438 image=$base_image
439 fi
440
441 set_volumes
442 set_links
443
444 rm -f $cidbootstrap
445
446 run_command="cd /pups &&"
447 if [[ ! "false" = $update_pups ]]; then
448 run_command="$run_command git pull &&"
449 fi
450 run_command="$run_command /pups/bin/pups --stdin"
451
452 echo $run_command
453
454 env=("${env[@]}" "-e" "SSH_PUB_KEY=$ssh_pub_key")
455
456 (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 \
457 /bin/bash -c "$run_command") \
458 || ($docker_path rm `cat $cidbootstrap` && rm $cidbootstrap)
459
460 [ ! -e $cidbootstrap ] && echo "** FAILED TO BOOTSTRAP ** please scroll up and look for earlier error messages, there may be more than one" && exit 1
461
462 sleep 5
463
464 $docker_path commit `cat $cidbootstrap` $local_discourse/$config || echo 'FAILED TO COMMIT'
465 $docker_path rm `cat $cidbootstrap` && rm $cidbootstrap
466 }
467
468 case "$command" in
469 bootstrap)
470 run_bootstrap
471 echo "Successfully bootstrapped, to startup use ./launcher start $config"
472 exit 0
473 ;;
474
475 enter)
476 exec $docker_path exec -it $config /bin/bash
477 ;;
478
479 ssh)
480 existing=`$docker_path ps | awk '{ print $1, $(NF) }' | grep " $config$" | awk '{ print $1 }'`
481
482 if [[ ! -z $existing ]]; then
483 address="`$docker_path port $config 22`"
484 split=(${address//:/ })
485 exec ssh -o StrictHostKeyChecking=no root@${split[0]} -p ${split[1]}
486 else
487 echo "$config is not running!"
488 exit 1
489 fi
490 ;;
491
492 stop)
493 run_stop
494 exit 0
495 ;;
496
497 logs)
498
499 $docker_path logs $config
500 exit 0
501 ;;
502
503 restart)
504 run_stop
505 run_start
506 exit 0
507 ;;
508
509 start)
510 run_start
511 exit 0
512 ;;
513
514 rebuild)
515 if [ "$(git symbolic-ref --short HEAD)" == "master" ]; then
516 echo "Ensuring discourse docker is up to date"
517
518 git remote update
519
520 LOCAL=$(git rev-parse @)
521 REMOTE=$(git rev-parse @{u})
522 BASE=$(git merge-base @ @{u})
523
524 if [ $LOCAL = $REMOTE ]; then
525 echo "Discourse Docker is up-to-date"
526
527 elif [ $LOCAL = $BASE ]; then
528 echo "Updating Discourse Docker"
529 git pull || (echo 'failed to update' && exit 1)
530 exec /bin/bash $0 $@
531
532 elif [ $REMOTE = $BASE ]; then
533 echo "Your version of Discourse Docker is ahead of origin"
534
535 else
536 echo "Discourse Docker has diverged source, this is only expected in Dev mode"
537 fi
538
539 fi
540
541 set_existing_container
542
543 if [ ! -z $existing ]
544 then
545 echo "Stopping old container"
546 (
547 set -x
548 $docker_path stop -t 10 $config
549 )
550 fi
551
552 run_bootstrap
553
554 if [ ! -z $existing ]
555 then
556 echo "Removing old container"
557 (
558 set -x
559 $docker_path rm $config
560 )
561 fi
562
563 run_start
564 exit 0
565 ;;
566
567
568 destroy)
569 (set -x; $docker_path stop -t 10 $config && $docker_path rm $config) || (echo "$config was not found" && exit 0)
570 exit 0
571 ;;
572 esac
573
574 usage