9 docker_min_version
='0.9.1'
10 docker_rec_version
='0.11.1'
12 config_file
=containers
/"$config".yml
13 cidfile
=cids
/"$config".cid
14 cidbootstrap
=cids
/"$config"_boostrap.cid
15 local_discourse
=local_discourse
16 image
=samsaffron
/discourse
:0.2.4
17 docker_path
=`which docker.io || which docker`
19 docker_ip
=`/sbin/ifconfig | \
20 grep -B1 "inet addr" | \
21 awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' | \
23 awk -F: '{ print $3 }';`
27 echo "Usage: launcher COMMAND CONFIG [--skip-prereqs]"
29 echo " start: Start/initialize a container"
30 echo " stop: Stop a running container"
31 echo " restart: Restart a container"
32 echo " destroy: Stop and remove a container"
33 echo " ssh: Start a bash shell in a running container"
34 echo " logs: Docker logs for container"
35 echo " mailtest: Test the mail settings in a container"
36 echo " bootstrap: Bootstrap a container for the config based on a template"
37 echo " rebuild: Rebuild a container (destroy old, bootstrap, start new)"
40 echo " --skip-prereqs Don't check prerequisites"
47 IFS
=.
read -a ver_a
<<< "$1"
48 IFS
=.
read -a ver_b
<<< "$2"
50 while [[ -n $ver_a ]]; do
51 if (( ver_a
> ver_b
)); then
53 elif (( ver_b
> ver_a
)); then
62 return 1 # They are equal
67 # 1. docker daemon running?
68 test=`$docker_path info >/dev/null`
70 if [[ $?
-ne 0 ]] ; then
71 echo "Cannot connect to the docker daemon - verify it is running and you have access"
76 test=`$docker_path info 2> /dev/null | grep 'Driver: aufs'`
77 if [[ "$test" =~
"aufs" ]] ; then : ; else
78 echo "Your Docker installation is not using aufs, in the past we have had issues with it"
79 echo "If you are unable to bootstrap your image (or stop it) please report the issue at:"
80 echo "https://meta.discourse.org/t/discourse-docker-installation-without-aufs/15639"
83 # 3. running recommended docker version
84 test=($
($docker_path --version)) # Get docker version string
85 test=${test[2]//,/} # Get version alone and strip comma if exists
87 [[ "$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
89 # At least minimum version
90 if compare_version
"${docker_min_version}" "${test}"; then
91 echo "ERROR: Docker version ${test} not supported, please upgrade to at least ${docker_min_version}, or recommended ${docker_rec_version}"
95 # Recommend best version
96 if compare_version
"${docker_rec_version}" "${test}"; then
97 echo "WARNING: Docker version ${test} deprecated, recommend upgrade to ${docker_rec_version} or newer."
100 # 4. able to attach stderr / out / tty
101 test=`$docker_path run -i --rm -a stdout -a stderr $image echo working`
102 if [[ "$test" =~
"working" ]] ; then : ; else
103 echo "Your Docker installation is not working correctly"
105 echo "See: https://meta.discourse.org/t/docker-error-on-bootstrap/13657/18?u=sam"
110 if [ "$opt" != "--skip-prereqs" ] ; then
116 if [[ ! -e ~
/.ssh
/id_rsa.pub
&& ! -e ~
/.ssh
/id_dsa.pub
]] ; then
117 echo "This user has no SSH key, but a SSH key is required to access the Discourse Docker container."
118 read -p "Generate a SSH key? (Y/n) " -n 1 -r
119 if [[ $REPLY =~ ^
[Nn
]$
]] ; then
121 echo WARNING
: You may not be able to log
in to your container.
125 echo Generating SSH key
126 mkdir
-p ~
/.
ssh && ssh-keygen
-f ~
/.ssh
/id_rsa
-t rsa
-N ''
132 ssh_pub_key
="$(cat ~/.ssh/id_rsa.pub 2>/dev/null || cat ~/.ssh/id_dsa.pub)"
138 echo "Docker is not installed, make sure you are running on the 3.8 kernel"
139 echo "The best supported Docker release is Ubuntu 12.04.03 for it run the following"
141 echo "sudo apt-get update"
142 echo "sudo apt-get install linux-image-generic-lts-raring linux-headers-generic-lts-raring"
146 echo "sudo sh -c \"wget -qO- https://get.docker.io/gpg | apt-key add -\""
147 echo "sudo sh -c \"echo deb http://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list\""
148 echo "sudo apt-get update"
149 echo "sudo apt-get install lxc-docker"
155 volumes
=`cat $config_file | $docker_path run --rm -i -a stdout -a stdin $image ruby -e \
156 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['volumes'].map{|v| '-v ' << v['volume']['host'] << ':' << v['volume']['guest'] << ' '}.join"`
159 set_template_info
() {
161 templates
=`cat $config_file | $docker_path run --rm -i -a stdin -a stdout $image ruby -e \
162 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['templates']"`
165 arrTemplates
=(${templates// / })
166 config_data
=$
(cat $config_file)
171 for template
in "${arrTemplates[@]}"
173 [ ! -z $template ] && {
174 input
="$input _FILE_SEPERATOR_ $(cat $template)"
178 # we always want our config file last so it takes priority
179 input
="$input _FILE_SEPERATOR_ $config_data"
181 read -r -d '' env_ruby
<< 'RUBY'
184 input
=STDIN.readlines.
join
186 input.
split('_FILE_SEPERATOR_').each
do |yml|
189 env.merge
!(YAML.load
(yml
)['env'] ||
{})
190 rescue Psych
::SyntaxError
=> e
198 puts env.map
{|k
,v|
"-e\n#{k}=#{v}" }.
join("\n")
201 raw
=`exec echo "$input" | $docker_path run --rm -i -a stdin -a stdout $image ruby -e "$env_ruby"`
206 if [ "$i" == "*ERROR." ]; then
208 elif [ -n "$i" ]; then
213 if [ "$ok" -ne 1 ]; then
215 echo "YAML syntax error. Please check your /var/docker/containers/*.yml config files."
218 echo "Calculated ENV: ${env[@]}"
221 [ -z $docker_path ] && {
230 if [ ! -e $config_file ]
232 echo "Config file was not found, ensure $config_file exists"
234 echo "Available configs ( `cd containers && ls -dm *.yml | tr -s '\n' ' ' | awk '{ gsub(/\.yml/, ""); print }'`)"
240 if [ ! -e $config_file ]; then
241 echo "Config does not exist: $config_file" >&2
244 exec scripts
/mailtest
$config_file
253 $docker_path stop
-t 10 `cat $cidfile`
261 echo "No cid found, creating a new container"
262 ports
=`cat $config_file | $docker_path run --rm -i -a stdout -a stdin $image ruby -e \
263 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['expose'].map{|p| '-p ' << p.to_s << ' '}.join"`
268 existing
=`$docker_path ps -a | awk '{ print $1, $(NF) }' | grep "$config$" | awk '{ print $1 }'`
269 if [ ! -z $existing ]
271 echo "Found an existing container by its name, recovering cidfile, please rerun"
272 echo $existing > $cidfile
276 $docker_path run
"${env[@]}" -h "`hostname`-$config" -e DOCKER_HOST_IP
=$docker_ip --name $config -t --cidfile $cidfile $ports \
277 -d $volumes $local_discourse/$config /sbin
/runit
285 echo "Detected empty cid file, deleting, please re-run"
290 found
=`$docker_path ps -q -a --no-trunc | grep $cid`
293 echo "Invalid cid file, deleting, please re-run"
298 echo "cid found, ensuring container is started"
299 $docker_path start
`cat $cidfile`
308 # Is the image available?
309 # If not, pull it here so the user is aware what's happening.
310 $docker_path history $image >/dev
/null
2>&1 ||
$docker_path pull
$image
314 base_image
=`cat $config_file | $docker_path run --rm -i -a stdin -a stdout $image ruby -e \
315 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['base_image']"`
317 update_pups
=`cat $config_file | $docker_path run --rm -i -a stdin -a stdout $image ruby -e \
318 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['update_pups']"`
320 if [[ ! X
"" = X
"$base_image" ]]; then
328 run_command
="cd /pups &&"
329 if [[ ! "false" = $update_pups ]]; then
330 run_command
="$run_command git pull &&"
332 run_command
="$run_command /pups/bin/pups --stdin"
336 env
=("${env[@]}" "-e" "SSH_PUB_KEY=$ssh_pub_key")
338 (exec echo "$input" |
$docker_path run
"${env[@]}" -e DOCKER_HOST_IP
=$docker_ip --cidfile $cidbootstrap -i -a stdin
-a stdout
-a stderr
$volumes $image \
339 /bin
/bash
-c "$run_command") \
340 ||
($docker_path rm `cat $cidbootstrap` && rm $cidbootstrap)
342 [ ! -e $cidbootstrap ] && echo "FAILED TO BOOTSTRAP" && exit 1
346 $docker_path commit
`cat $cidbootstrap` $local_discourse/$config ||
echo 'FAILED TO COMMIT'
347 $docker_path rm `cat $cidbootstrap` && rm $cidbootstrap
353 echo "Successfully bootstrapped, to startup use ./launcher start $config"
369 address
="`$docker_path port $cid 22`"
370 split=(${address//:/ })
371 exec ssh -o StrictHostKeyChecking
=no root@
${split[0]} -p ${split[1]}
387 $docker_path logs
`cat $cidfile`
406 echo "Stopping old container"
407 $docker_path stop
-t 10 `cat $cidfile`
414 $docker_path rm `cat $cidfile` && rm $cidfile
425 echo "destroying container $cidfile"
426 $docker_path stop
-t 10 `cat $cidfile`
427 $docker_path rm `cat $cidfile` && rm $cidfile
430 echo "nothing to destroy cidfile does not exist"