support base images and disabling of pups
[discourse_docker.git] / launcher
1 #!/bin/bash
2
3 command=$1
4 config=$2
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
10 docker_path=`which docker`
11
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
19 usage () {
20 echo "Usage: launcher COMMAND CONFIG"
21 echo "Commands:"
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 exit 1
30 }
31
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
50 set_volumes() {
51 volumes=`cat $config_file | docker run -rm -i -a stdout -a stdin $image ruby -e \
52 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['volumes'].map{|v| '-v ' << v['volume']['host'] << ':' << v['volume']['guest'] << ' '}.join"`
53 }
54
55 [ -z $docker_path ] && {
56 install_docker
57 }
58
59
60 [ $# -ne 2 ] && {
61 usage
62 }
63
64 if [ ! -e $config_file ]
65 then
66 echo "Config file was not found, ensure $config_file exists"
67 exit 1
68 fi
69
70 case "$command" in
71 bootstrap)
72
73 templates=`cat $config_file | docker run -rm -i -a stdin -a stdout $image ruby -e \
74 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['templates']"`
75
76 base_image=`cat $config_file | docker run -rm -i -a stdin -a stdout $image ruby -e \
77 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['base_image']"`
78
79 update_pups=`cat $config_file | docker run -rm -i -a stdin -a stdout $image ruby -e \
80 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['update_pups']"`
81
82 arrTemplates=(${templates// / })
83 config_data=$(cat $config_file)
84 input="hack: true"
85
86 if [[ ! X"" = X"$base_image" ]]; then
87 image=$base_image
88 fi
89
90 for template in "${arrTemplates[@]}"
91 do
92 [ ! -z $template ] && {
93 input="$input _FILE_SEPERATOR_ $(cat $template)"
94 }
95 done
96
97 # we always want our config file last so it takes priority
98 input="$input _FILE_SEPERATOR_ $config_data"
99
100 set_volumes
101
102 rm -f $cidbootstrap
103
104 run_command="cd /pups &&"
105 if [[ ! "false" = $update_pups ]]; then
106 run_command="$run_command git pull &&"
107 fi
108 run_command="$run_command /pups/bin/pups --stdin"
109
110 (exec echo "$input" | docker run -e DOCKER_HOST_IP=$docker_ip -cidfile $cidbootstrap -i -a stdin -a stdout -a stderr $volumes $image \
111 /bin/bash -c "$run_command") \
112 || (docker rm `cat $cidbootstrap` && rm $cidbootstrap)
113
114 [ ! -e $cidbootstrap ] && echo "FAILED TO BOOTSTRAP" && exit 1
115
116 sleep 5
117
118 docker commit `cat $cidbootstrap` $local_discourse/$config || echo 'FAILED TO COMMIT'
119 docker rm `cat $cidbootstrap` && rm $cidbootstrap
120
121 echo "Successfully bootstrappd, to starup use ./launcher start $config"
122 exit 0
123 ;;
124
125 ssh)
126 if [ ! -e $cidfile ]
127 then
128 echo "No cid found"
129 exit 1
130 else
131 cid="`cat $cidfile`"
132 address="`docker port $cid 22`"
133 split=(${address//:/ })
134 exec ssh root@${split[0]} -p ${split[1]}
135 fi
136 ;;
137
138 stop)
139 if [ ! -e $cidfile ]
140 then
141 echo "No cid found"
142 exit 1
143 else
144 docker stop -t 10 `cat $cidfile`
145 exit 0
146 fi
147 ;;
148
149 logs)
150
151 if [ ! -e $cidfile ]
152 then
153 echo "No cid found"
154 exit 1
155 else
156 docker logs `cat $cidfile`
157 exit 0
158 fi
159 ;;
160
161 start)
162
163 if [ ! -e $cidfile ]
164 then
165 echo "No cid found, creating a new container"
166 ports=`cat $config_file | docker run -rm -i -a stdout -a stdin $image ruby -e \
167 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['expose'].map{|p| '-p ' << p.to_s << ' '}.join"`
168
169 set_volumes
170
171 docker run -e DOCKER_HOST_IP=$docker_ip -name $config -cidfile $cidfile $ports \
172 -d $volumes $local_discourse/$config /usr/bin/runsvdir -P /etc/service
173
174 exit 0
175 else
176 echo "cid found, ensuring container is started"
177 docker start `cat $cidfile`
178 exit 0
179 fi
180 ;;
181
182
183 destroy)
184 if [ -e $cidfile ]
185 then
186 echo "destroying container $cidfile"
187 docker stop -t 10 `cat $cidfile`
188 docker rm `cat $cidfile` && rm $cidfile
189 exit 0
190 else
191 echo "nothing to destroy cidfile does not exist"
192 exit 1
193 fi
194 ;;
195 esac
196
197 usage