Commit | Line | Data |
---|---|---|
ace450bd | 1 | #!/bin/bash |
7e738616 S |
2 | |
3 | command=$1 | |
4 | config=$2 | |
8dea575c | 5 | config_file=containers/"$config".yml |
7e738616 | 6 | cidfile=cids/"$config".cid |
1acce9e4 | 7 | cidbootstrap=cids/"$config"_boostrap.cid |
5efded6a | 8 | local_discourse=local_discourse |
d69fe0fb | 9 | image=samsaffron/discourse:0.1.2 |
52388b87 | 10 | docker_path=`which docker` |
7e738616 | 11 | |
80c11be3 SS |
12 | docker_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 | 19 | usage () { |
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 |
32 | install_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 | 50 | set_volumes() { |
c4498636 | 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 |
55 | set_template_info() { |
56 | ||
c4498636 | 57 | templates=`cat $config_file | docker run --rm -i -a stdin -a stdout $image ruby -e \ |
7f77c274 SS |
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 | |
4b563ee8 | 91 | puts env.map{|k,v| "-e\n#{k}=#{v}" }.join("\n") |
7f77c274 SS |
92 | RUBY |
93 | ||
c4498636 | 94 | raw=`exec echo "$input" | docker run --rm -i -a stdin -a stdout $image ruby -e "$env_ruby"` |
4b563ee8 SS |
95 | |
96 | env=() | |
97 | while read i; do | |
98 | env[${#env[@]}]=$i | |
99 | done <<< "$raw" | |
100 | ||
101 | echo "Calculated ENV: ${env[@]}" | |
7f77c274 SS |
102 | } |
103 | ||
52388b87 SS |
104 | [ -z $docker_path ] && { |
105 | install_docker | |
106 | } | |
107 | ||
5f803fb4 SS |
108 | |
109 | [ $# -ne 2 ] && { | |
110 | usage | |
111 | } | |
112 | ||
7e738616 S |
113 | if [ ! -e $config_file ] |
114 | then | |
115 | echo "Config file was not found, ensure $config_file exists" | |
116 | exit 1 | |
117 | fi | |
118 | ||
5f803fb4 SS |
119 | case "$command" in |
120 | bootstrap) | |
38c09f5e MB |
121 | # Is the image available? |
122 | # If not, pull it here so the user is aware what's happening. | |
123 | docker history $image >/dev/null 2>&1 || docker pull $image | |
93149421 | 124 | |
7f77c274 | 125 | set_template_info |
93149421 | 126 | |
c4498636 | 127 | base_image=`cat $config_file | docker run --rm -i -a stdin -a stdout $image ruby -e \ |
b9c7b50e SS |
128 | "require 'yaml'; puts YAML.load(STDIN.readlines.join)['base_image']"` |
129 | ||
c4498636 | 130 | update_pups=`cat $config_file | docker run --rm -i -a stdin -a stdout $image ruby -e \ |
b9c7b50e SS |
131 | "require 'yaml'; puts YAML.load(STDIN.readlines.join)['update_pups']"` |
132 | ||
b9c7b50e SS |
133 | if [[ ! X"" = X"$base_image" ]]; then |
134 | image=$base_image | |
135 | fi | |
136 | ||
d90671f3 SS |
137 | set_volumes |
138 | ||
2162f1d4 SS |
139 | rm -f $cidbootstrap |
140 | ||
b9c7b50e SS |
141 | run_command="cd /pups &&" |
142 | if [[ ! "false" = $update_pups ]]; then | |
143 | run_command="$run_command git pull &&" | |
144 | fi | |
145 | run_command="$run_command /pups/bin/pups --stdin" | |
146 | ||
c4498636 S |
147 | echo $run_command |
148 | ||
149 | (exec echo "$input" | docker run "${env[@]}" -e DOCKER_HOST_IP=$docker_ip --cidfile $cidbootstrap -i -a stdin -a stdout -a stderr $volumes $image \ | |
b9c7b50e | 150 | /bin/bash -c "$run_command") \ |
2162f1d4 | 151 | || (docker rm `cat $cidbootstrap` && rm $cidbootstrap) |
9fb5f2d3 | 152 | |
2162f1d4 SS |
153 | [ ! -e $cidbootstrap ] && echo "FAILED TO BOOTSTRAP" && exit 1 |
154 | ||
155 | sleep 5 | |
9fb5f2d3 | 156 | |
5efded6a | 157 | docker commit `cat $cidbootstrap` $local_discourse/$config || echo 'FAILED TO COMMIT' |
4b3aebe1 | 158 | docker rm `cat $cidbootstrap` && rm $cidbootstrap |
2162f1d4 | 159 | |
2dd2e330 | 160 | echo "Successfully bootstrapped, to startup use ./launcher start $config" |
4b3aebe1 | 161 | exit 0 |
5f803fb4 | 162 | ;; |
1acce9e4 | 163 | |
5f803fb4 SS |
164 | ssh) |
165 | if [ ! -e $cidfile ] | |
166 | then | |
167 | echo "No cid found" | |
168 | exit 1 | |
169 | else | |
170 | cid="`cat $cidfile`" | |
171 | address="`docker port $cid 22`" | |
172 | split=(${address//:/ }) | |
38c09f5e | 173 | exec ssh -o StrictHostKeyChecking=no root@${split[0]} -p ${split[1]} |
5f803fb4 SS |
174 | fi |
175 | ;; | |
7e738616 | 176 | |
5f803fb4 SS |
177 | stop) |
178 | if [ ! -e $cidfile ] | |
179 | then | |
180 | echo "No cid found" | |
181 | exit 1 | |
182 | else | |
183 | docker stop -t 10 `cat $cidfile` | |
184 | exit 0 | |
185 | fi | |
186 | ;; | |
7e738616 | 187 | |
5f803fb4 | 188 | logs) |
7e738616 | 189 | |
5f803fb4 SS |
190 | if [ ! -e $cidfile ] |
191 | then | |
192 | echo "No cid found" | |
193 | exit 1 | |
194 | else | |
195 | docker logs `cat $cidfile` | |
196 | exit 0 | |
197 | fi | |
198 | ;; | |
7e738616 | 199 | |
5f803fb4 | 200 | start) |
7e738616 | 201 | |
5f803fb4 SS |
202 | if [ ! -e $cidfile ] |
203 | then | |
204 | echo "No cid found, creating a new container" | |
c4498636 | 205 | ports=`cat $config_file | docker run --rm -i -a stdout -a stdin $image ruby -e \ |
36941588 | 206 | "require 'yaml'; puts YAML.load(STDIN.readlines.join)['expose'].map{|p| '-p ' << p.to_s << ' '}.join"` |
d90671f3 | 207 | |
7f77c274 | 208 | set_template_info |
d90671f3 SS |
209 | set_volumes |
210 | ||
2aa2732e | 211 | existing=`docker ps -a | awk '{ print $1, $(NF) }' | grep "$config$" | awk '{ print $1 }'` |
90506274 SS |
212 | if [ ! -z $existing ] |
213 | then | |
214 | echo "Found an existing container by its name, recovering cidfile, please rerun" | |
215 | echo $existing > $cidfile | |
216 | exit 1 | |
217 | fi | |
218 | ||
a2d22fdf | 219 | docker run "${env[@]}" -h "`hostname`-$config" -e DOCKER_HOST_IP=$docker_ip --name $config -t --cidfile $cidfile $ports \ |
80c11be3 SS |
220 | -d $volumes $local_discourse/$config /usr/bin/runsvdir -P /etc/service |
221 | ||
5f803fb4 SS |
222 | exit 0 |
223 | else | |
90506274 SS |
224 | cid=`cat $cidfile` |
225 | ||
226 | if [ -z $cid ] | |
227 | then | |
228 | echo "Detected empty cid file, deleting, please re-run" | |
229 | rm $cidfile | |
230 | exit 1 | |
231 | fi | |
232 | ||
a2d22fdf | 233 | found=`docker ps -q -a --no-trunc | grep $cid` |
90506274 SS |
234 | if [ -z $found ] |
235 | then | |
236 | echo "Invalid cid file, deleting, please re-run" | |
237 | rm $cidfile | |
238 | exit 1 | |
239 | fi | |
240 | ||
5f803fb4 SS |
241 | echo "cid found, ensuring container is started" |
242 | docker start `cat $cidfile` | |
243 | exit 0 | |
244 | fi | |
245 | ;; | |
7e738616 | 246 | |
7e738616 | 247 | |
5f803fb4 SS |
248 | destroy) |
249 | if [ -e $cidfile ] | |
250 | then | |
251 | echo "destroying container $cidfile" | |
252 | docker stop -t 10 `cat $cidfile` | |
253 | docker rm `cat $cidfile` && rm $cidfile | |
254 | exit 0 | |
255 | else | |
256 | echo "nothing to destroy cidfile does not exist" | |
257 | exit 1 | |
258 | fi | |
259 | ;; | |
260 | esac | |
7e738616 | 261 | |
5f803fb4 | 262 | usage |