example mail
[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
52388b87
SS
55[ -z $docker_path ] && {
56 install_docker
57}
58
5f803fb4
SS
59
60[ $# -ne 2 ] && {
61 usage
62}
63
7e738616
S
64if [ ! -e $config_file ]
65 then
66 echo "Config file was not found, ensure $config_file exists"
67 exit 1
68fi
69
5f803fb4
SS
70case "$command" in
71 bootstrap)
93149421 72
9fb5f2d3
SS
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']"`
93149421 75
9fb5f2d3 76 arrTemplates=(${templates// / })
e9aa7df1 77 config_data=$(cat $config_file)
8f993af4 78 input="hack: true"
9fb5f2d3
SS
79
80 for template in "${arrTemplates[@]}"
81 do
82 [ ! -z $template ] && {
83 input="$input _FILE_SEPERATOR_ $(cat $template)"
84 }
85 done
93149421 86
8f993af4 87 # we always want our config file last so it takes priority
e9aa7df1 88 input="$input _FILE_SEPERATOR_ $config_data"
8f993af4 89
d90671f3
SS
90 set_volumes
91
2162f1d4
SS
92 rm -f $cidbootstrap
93
80c11be3 94 (exec echo "$input" | docker run -e DOCKER_HOST_IP=$docker_ip -cidfile $cidbootstrap -i -a stdin -a stdout -a stderr $volumes $image \
2162f1d4
SS
95 /bin/bash -c 'cd /pups && git pull && /pups/bin/pups --stdin') \
96 || (docker rm `cat $cidbootstrap` && rm $cidbootstrap)
9fb5f2d3 97
2162f1d4
SS
98 [ ! -e $cidbootstrap ] && echo "FAILED TO BOOTSTRAP" && exit 1
99
100 sleep 5
9fb5f2d3 101
5efded6a 102 docker commit `cat $cidbootstrap` $local_discourse/$config || echo 'FAILED TO COMMIT'
4b3aebe1 103 docker rm `cat $cidbootstrap` && rm $cidbootstrap
2162f1d4
SS
104
105 echo "Successfully bootstrappd, to starup use ./launcher start $config"
4b3aebe1 106 exit 0
5f803fb4 107 ;;
1acce9e4 108
5f803fb4
SS
109 ssh)
110 if [ ! -e $cidfile ]
111 then
112 echo "No cid found"
113 exit 1
114 else
115 cid="`cat $cidfile`"
116 address="`docker port $cid 22`"
117 split=(${address//:/ })
118 exec ssh root@${split[0]} -p ${split[1]}
119 fi
120 ;;
7e738616 121
5f803fb4
SS
122 stop)
123 if [ ! -e $cidfile ]
124 then
125 echo "No cid found"
126 exit 1
127 else
128 docker stop -t 10 `cat $cidfile`
129 exit 0
130 fi
131 ;;
7e738616 132
5f803fb4 133 logs)
7e738616 134
5f803fb4
SS
135 if [ ! -e $cidfile ]
136 then
137 echo "No cid found"
138 exit 1
139 else
140 docker logs `cat $cidfile`
141 exit 0
142 fi
143 ;;
7e738616 144
5f803fb4 145 start)
7e738616 146
5f803fb4
SS
147 if [ ! -e $cidfile ]
148 then
149 echo "No cid found, creating a new container"
b86ac1af 150 ports=`cat $config_file | docker run -rm -i -a stdout -a stdin $image ruby -e \
36941588 151 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['expose'].map{|p| '-p ' << p.to_s << ' '}.join"`
d90671f3
SS
152
153 set_volumes
154
80c11be3
SS
155 docker run -e DOCKER_HOST_IP=$docker_ip -name $config -cidfile $cidfile $ports \
156 -d $volumes $local_discourse/$config /usr/bin/runsvdir -P /etc/service
157
5f803fb4
SS
158 exit 0
159 else
160 echo "cid found, ensuring container is started"
161 docker start `cat $cidfile`
162 exit 0
163 fi
164 ;;
7e738616 165
7e738616 166
5f803fb4
SS
167 destroy)
168 if [ -e $cidfile ]
169 then
170 echo "destroying container $cidfile"
171 docker stop -t 10 `cat $cidfile`
172 docker rm `cat $cidfile` && rm $cidfile
173 exit 0
174 else
175 echo "nothing to destroy cidfile does not exist"
176 exit 1
177 fi
178 ;;
179esac
7e738616 180
5f803fb4 181usage