add test that ensures bootstrap can not run when postgres is running
[discourse_docker.git] / launcher
CommitLineData
ace450bd 1#!/bin/bash
7e738616
S
2
3command=$1
4config=$2
8dea575c 5config_file=containers/"$config".yml
7e738616 6cidfile=cids/"$config".cid
1acce9e4 7cidbootstrap=cids/"$config"_boostrap.cid
5efded6a 8local_discourse=local_discourse
52388b87
SS
9image=samsaffron/discourse
10docker_path=`which docker`
7e738616 11
80c11be3
SS
12docker_ip=`/sbin/ifconfig | \
13 grep -B1 "inet addr" | \
14 awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' | \
15 grep docker0 | \
16 awk -F: '{ print $3 }';`
17
18
5f803fb4 19usage () {
7e738616
S
20 echo "Usage: launcher COMMAND CONFIG"
21 echo "Commands:"
1acce9e4
SS
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"
5f803fb4 26 echo " ssh: Start a bash shell in a running container"
1acce9e4 27 echo " logs: Docker logs for container"
408a9c19 28 echo " bootstrap: Bootstrap a container for the config based on a template"
7e738616
S
29 exit 1
30}
31
52388b87
SS
32install_docker() {
33
34 echo "Docker is not installed, make sure you are running on the 3.8 kernel"
35 echo "The best supported Docker release is Ubuntu 12.04.03 for it run the following"
36 echo
37 echo "sudo apt-get update"
38 echo "sudo apt-get install linux-image-generic-lts-raring linux-headers-generic-lts-raring"
39 echo "sudo reboot"
40 echo
41
42 echo "sudo sh -c \"wget -qO- https://get.docker.io/gpg | apt-key add -\""
43 echo "sudo sh -c \"echo deb http://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list\""
44 echo "sudo apt-get update"
45 echo "sudo apt-get install lxc-docker"
46
47 exit 1
48}
49
d90671f3 50set_volumes() {
b86ac1af 51 volumes=`cat $config_file | docker run -rm -i -a stdout -a stdin $image ruby -e \
d90671f3
SS
52 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['volumes'].map{|v| '-v ' << v['volume']['host'] << ':' << v['volume']['guest'] << ' '}.join"`
53}
54
7f77c274
SS
55set_template_info() {
56
57 templates=`cat $config_file | docker run -rm -i -a stdin -a stdout $image ruby -e \
58 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['templates']"`
59
60
61 arrTemplates=(${templates// / })
62 config_data=$(cat $config_file)
63
64 input="hack: true"
65
66
67 for template in "${arrTemplates[@]}"
68 do
69 [ ! -z $template ] && {
70 input="$input _FILE_SEPERATOR_ $(cat $template)"
71 }
72 done
73
74 # we always want our config file last so it takes priority
75 input="$input _FILE_SEPERATOR_ $config_data"
76
77 read -r -d '' env_ruby << 'RUBY'
78 require 'yaml'
79
80 input=STDIN.readlines.join
81 env = {}
82 input.split('_FILE_SEPERATOR_').each do |yml|
83 yml.strip!
84 begin
85 env.merge!(YAML.load(yml)['env'] || {})
86 rescue => e
87 puts yml
88 p e
89 end
90 end
91 puts env.map{|k,v| "-e #{k}=#{v}" }.join(" ")
92RUBY
93
94 env=`exec echo "$input" | docker run -rm -i -a stdin -a stdout $image ruby -e "$env_ruby"`
95 echo "Calculated ENV: $env"
96}
97
52388b87
SS
98[ -z $docker_path ] && {
99 install_docker
100}
101
5f803fb4
SS
102
103[ $# -ne 2 ] && {
104 usage
105}
106
7e738616
S
107if [ ! -e $config_file ]
108 then
109 echo "Config file was not found, ensure $config_file exists"
110 exit 1
111fi
112
5f803fb4
SS
113case "$command" in
114 bootstrap)
93149421 115
7f77c274 116 set_template_info
93149421 117
b9c7b50e
SS
118 base_image=`cat $config_file | docker run -rm -i -a stdin -a stdout $image ruby -e \
119 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['base_image']"`
120
121 update_pups=`cat $config_file | docker run -rm -i -a stdin -a stdout $image ruby -e \
122 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['update_pups']"`
123
b9c7b50e
SS
124 if [[ ! X"" = X"$base_image" ]]; then
125 image=$base_image
126 fi
127
d90671f3
SS
128 set_volumes
129
2162f1d4
SS
130 rm -f $cidbootstrap
131
b9c7b50e
SS
132 run_command="cd /pups &&"
133 if [[ ! "false" = $update_pups ]]; then
134 run_command="$run_command git pull &&"
135 fi
136 run_command="$run_command /pups/bin/pups --stdin"
137
7f77c274 138 (exec echo "$input" | docker run $env -e DOCKER_HOST_IP=$docker_ip -cidfile $cidbootstrap -i -a stdin -a stdout -a stderr $volumes $image \
b9c7b50e 139 /bin/bash -c "$run_command") \
2162f1d4 140 || (docker rm `cat $cidbootstrap` && rm $cidbootstrap)
9fb5f2d3 141
2162f1d4
SS
142 [ ! -e $cidbootstrap ] && echo "FAILED TO BOOTSTRAP" && exit 1
143
144 sleep 5
9fb5f2d3 145
5efded6a 146 docker commit `cat $cidbootstrap` $local_discourse/$config || echo 'FAILED TO COMMIT'
4b3aebe1 147 docker rm `cat $cidbootstrap` && rm $cidbootstrap
2162f1d4 148
2dd2e330 149 echo "Successfully bootstrapped, to startup use ./launcher start $config"
4b3aebe1 150 exit 0
5f803fb4 151 ;;
1acce9e4 152
5f803fb4
SS
153 ssh)
154 if [ ! -e $cidfile ]
155 then
156 echo "No cid found"
157 exit 1
158 else
159 cid="`cat $cidfile`"
160 address="`docker port $cid 22`"
161 split=(${address//:/ })
162 exec ssh root@${split[0]} -p ${split[1]}
163 fi
164 ;;
7e738616 165
5f803fb4
SS
166 stop)
167 if [ ! -e $cidfile ]
168 then
169 echo "No cid found"
170 exit 1
171 else
172 docker stop -t 10 `cat $cidfile`
173 exit 0
174 fi
175 ;;
7e738616 176
5f803fb4 177 logs)
7e738616 178
5f803fb4
SS
179 if [ ! -e $cidfile ]
180 then
181 echo "No cid found"
182 exit 1
183 else
184 docker logs `cat $cidfile`
185 exit 0
186 fi
187 ;;
7e738616 188
5f803fb4 189 start)
7e738616 190
5f803fb4
SS
191 if [ ! -e $cidfile ]
192 then
193 echo "No cid found, creating a new container"
b86ac1af 194 ports=`cat $config_file | docker run -rm -i -a stdout -a stdin $image ruby -e \
36941588 195 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['expose'].map{|p| '-p ' << p.to_s << ' '}.join"`
d90671f3 196
7f77c274 197 set_template_info
d90671f3
SS
198 set_volumes
199
2aa2732e 200 existing=`docker ps -a | awk '{ print $1, $(NF) }' | grep "$config$" | awk '{ print $1 }'`
90506274
SS
201 if [ ! -z $existing ]
202 then
203 echo "Found an existing container by its name, recovering cidfile, please rerun"
204 echo $existing > $cidfile
205 exit 1
206 fi
207
7f77c274 208 docker run $env -e DOCKER_HOST_IP=$docker_ip -name $config -cidfile $cidfile $ports \
80c11be3
SS
209 -d $volumes $local_discourse/$config /usr/bin/runsvdir -P /etc/service
210
5f803fb4
SS
211 exit 0
212 else
90506274
SS
213 cid=`cat $cidfile`
214
215 if [ -z $cid ]
216 then
217 echo "Detected empty cid file, deleting, please re-run"
218 rm $cidfile
219 exit 1
220 fi
221
74a4fe61 222 found=`docker ps -q -a -notrunc | grep $cid`
90506274
SS
223 if [ -z $found ]
224 then
225 echo "Invalid cid file, deleting, please re-run"
226 rm $cidfile
227 exit 1
228 fi
229
5f803fb4
SS
230 echo "cid found, ensuring container is started"
231 docker start `cat $cidfile`
232 exit 0
233 fi
234 ;;
7e738616 235
7e738616 236
5f803fb4
SS
237 destroy)
238 if [ -e $cidfile ]
239 then
240 echo "destroying container $cidfile"
241 docker stop -t 10 `cat $cidfile`
242 docker rm `cat $cidfile` && rm $cidfile
243 exit 0
244 else
245 echo "nothing to destroy cidfile does not exist"
246 exit 1
247 fi
248 ;;
249esac
7e738616 250
5f803fb4 251usage