5 config_file
=containers
/"$config".yml
6 cidfile
=cids
/"$config".cid
7 cidbootstrap
=cids
/"$config"_boostrap.cid
8 local_discourse
=local_discourse
9 image
=samsaffron
/discourse
:0.1.2
10 docker_path
=`which docker`
12 docker_ip
=`/sbin/ifconfig | \
13 grep -B1 "inet addr" | \
14 awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' | \
16 awk -F: '{ print $3 }';`
20 echo "Usage: launcher COMMAND CONFIG"
22 echo " start: Start/initialize a container"
23 echo " stop: Stop a running container"
24 echo " restart: Restart a container"
25 echo " destroy: Stop and remove a container"
26 echo " ssh: Start a bash shell in a running container"
27 echo " logs: Docker logs for container"
28 echo " bootstrap: Bootstrap a container for the config based on a template"
29 echo " rebuild: Rebuild a container (destroy old, bootstrap, start new)"
35 # 1. docker daemon running?
36 test=`docker info >/dev/null`
38 if [[ $?
-ne 0 ]] ; then
39 echo "Cannot connect to the docker daemon - verify it is running and you have access"
44 test=`docker info 2> /dev/null | grep 'Driver: aufs'`
45 if [[ "$test" =~
"aufs" ]] ; then : ; else
46 echo "Your Docker installation is not using aufs"
47 echo "Device mapper and other experimental drivers are unstable"
49 echo "Please ensure your kernel is running linux extras and aufs"
50 echo "Please follow the installation guide for Docker here: http://docs.docker.io/en/latest/installation/ubuntulinux/"
54 # 3. running docker 0.10+
55 test=`docker --version | grep 0.10`
57 if [[ "$test" =~
"0.10" ]] ; then : ; else
58 echo "Your Docker installation is old, please upgrade to 0.10.0 or up"
62 # 4. able to attach stderr / out / tty
63 test=`docker run -i --rm -a stdin -a stdout -a stderr $image echo working`
64 if [[ "$test" =~
"working" ]] ; then : ; else
65 echo "Your Docker installation is not working correctly"
67 echo "See: https://meta.discourse.org/t/docker-error-on-bootstrap/13657/18?u=sam"
76 if [[ ! -e ~
/.ssh
/id_rsa.pub
&& ! -e ~
/.ssh
/id_dsa.pub
]] ; then
77 echo You have no SSH key associated to this profile
78 echo "(This will allow you ssh access into your container)"
79 read -p "Generate SSH key at ~/.ssh/id_rsa.pub? (y/N) " -n 1 -r
80 if [[ $REPLY =~ ^
[Yy
]$
]] ; then
82 echo Generating SSH key
83 mkdir
-p ~
/.
ssh && ssh-keygen
-f ~
/.ssh
/id_rsa
-t rsa
-N ''
86 echo WARNING
: You may not be able to log
in to your container.
92 ssh_pub_key
="$(cat ~/.ssh/id_rsa.pub 2>/dev/null || cat ~/.ssh/id_dsa.pub)"
98 echo "Docker is not installed, make sure you are running on the 3.8 kernel"
99 echo "The best supported Docker release is Ubuntu 12.04.03 for it run the following"
101 echo "sudo apt-get update"
102 echo "sudo apt-get install linux-image-generic-lts-raring linux-headers-generic-lts-raring"
106 echo "sudo sh -c \"wget -qO- https://get.docker.io/gpg | apt-key add -\""
107 echo "sudo sh -c \"echo deb http://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list\""
108 echo "sudo apt-get update"
109 echo "sudo apt-get install lxc-docker"
115 volumes
=`cat $config_file | docker run --rm -i -a stdout -a stdin $image ruby -e \
116 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['volumes'].map{|v| '-v ' << v['volume']['host'] << ':' << v['volume']['guest'] << ' '}.join"`
119 set_template_info
() {
121 templates
=`cat $config_file | docker run --rm -i -a stdin -a stdout $image ruby -e \
122 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['templates']"`
125 arrTemplates
=(${templates// / })
126 config_data
=$
(cat $config_file)
131 for template
in "${arrTemplates[@]}"
133 [ ! -z $template ] && {
134 input
="$input _FILE_SEPERATOR_ $(cat $template)"
138 # we always want our config file last so it takes priority
139 input
="$input _FILE_SEPERATOR_ $config_data"
141 read -r -d '' env_ruby
<< 'RUBY'
144 input
=STDIN.readlines.
join
146 input.
split('_FILE_SEPERATOR_').each
do |yml|
149 env.merge
!(YAML.load
(yml
)['env'] ||
{})
155 puts env.map
{|k
,v|
"-e\n#{k}=#{v}" }.
join("\n")
158 raw
=`exec echo "$input" | docker run --rm -i -a stdin -a stdout $image ruby -e "$env_ruby"`
165 echo "Calculated ENV: ${env[@]}"
168 [ -z $docker_path ] && {
177 if [ ! -e $config_file ]
179 echo "Config file was not found, ensure $config_file exists"
190 docker stop
-t 10 `cat $cidfile`
198 echo "No cid found, creating a new container"
199 ports
=`cat $config_file | docker run --rm -i -a stdout -a stdin $image ruby -e \
200 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['expose'].map{|p| '-p ' << p.to_s << ' '}.join"`
205 existing
=`docker ps -a | awk '{ print $1, $(NF) }' | grep "$config$" | awk '{ print $1 }'`
206 if [ ! -z $existing ]
208 echo "Found an existing container by its name, recovering cidfile, please rerun"
209 echo $existing > $cidfile
213 docker run
"${env[@]}" -h "`hostname`-$config" -e DOCKER_HOST_IP
=$docker_ip --name $config -t --cidfile $cidfile $ports \
214 -d $volumes $local_discourse/$config /usr
/bin
/runsvdir
-P /etc
/service
222 echo "Detected empty cid file, deleting, please re-run"
227 found
=`docker ps -q -a --no-trunc | grep $cid`
230 echo "Invalid cid file, deleting, please re-run"
235 echo "cid found, ensuring container is started"
236 docker start
`cat $cidfile`
245 # Is the image available?
246 # If not, pull it here so the user is aware what's happening.
247 docker
history $image >/dev
/null
2>&1 || docker pull
$image
251 base_image
=`cat $config_file | docker run --rm -i -a stdin -a stdout $image ruby -e \
252 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['base_image']"`
254 update_pups
=`cat $config_file | docker run --rm -i -a stdin -a stdout $image ruby -e \
255 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['update_pups']"`
257 if [[ ! X
"" = X
"$base_image" ]]; then
265 run_command
="cd /pups &&"
266 if [[ ! "false" = $update_pups ]]; then
267 run_command
="$run_command git pull &&"
269 run_command
="$run_command /pups/bin/pups --stdin"
273 env
=("${env[@]}" "-e" "SSH_PUB_KEY=$ssh_pub_key")
275 (exec echo "$input" | docker run
"${env[@]}" -e DOCKER_HOST_IP
=$docker_ip --cidfile $cidbootstrap -i -a stdin
-a stdout
-a stderr
$volumes $image \
276 /bin
/bash
-c "$run_command") \
277 ||
(docker
rm `cat $cidbootstrap` && rm $cidbootstrap)
279 [ ! -e $cidbootstrap ] && echo "FAILED TO BOOTSTRAP" && exit 1
283 docker commit
`cat $cidbootstrap` $local_discourse/$config ||
echo 'FAILED TO COMMIT'
284 docker
rm `cat $cidbootstrap` && rm $cidbootstrap
291 echo "Successfully bootstrapped, to startup use ./launcher start $config"
302 address
="`docker port $cid 22`"
303 split=(${address//:/ })
304 exec ssh -o StrictHostKeyChecking
=no root@
${split[0]} -p ${split[1]}
320 docker logs
`cat $cidfile`
339 echo "Stopping old container"
340 docker stop
-t 10 `cat $cidfile`
347 docker
rm `cat $cidfile` && rm $cidfile
358 echo "destroying container $cidfile"
359 docker stop
-t 10 `cat $cidfile`
360 docker
rm `cat $cidfile` && rm $cidfile
363 echo "nothing to destroy cidfile does not exist"