don't include blog, do include docker manager
[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
5f803fb4 12usage () {
7e738616
S
13 echo "Usage: launcher COMMAND CONFIG"
14 echo "Commands:"
1acce9e4
SS
15 echo " start: Start/initialize a container"
16 echo " stop: Stop a running container"
17 echo " restart: Restart a container"
18 echo " destroy: Stop and remove a container"
5f803fb4 19 echo " ssh: Start a bash shell in a running container"
1acce9e4
SS
20 echo " logs: Docker logs for container"
21 echo " bootstrap: Bootstrap a container for the config base on an image"
7e738616
S
22 exit 1
23}
24
52388b87
SS
25install_docker() {
26
27 echo "Docker is not installed, make sure you are running on the 3.8 kernel"
28 echo "The best supported Docker release is Ubuntu 12.04.03 for it run the following"
29 echo
30 echo "sudo apt-get update"
31 echo "sudo apt-get install linux-image-generic-lts-raring linux-headers-generic-lts-raring"
32 echo "sudo reboot"
33 echo
34
35 echo "sudo sh -c \"wget -qO- https://get.docker.io/gpg | apt-key add -\""
36 echo "sudo sh -c \"echo deb http://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list\""
37 echo "sudo apt-get update"
38 echo "sudo apt-get install lxc-docker"
39
40 exit 1
41}
42
d90671f3 43set_volumes() {
b86ac1af 44 volumes=`cat $config_file | docker run -rm -i -a stdout -a stdin $image ruby -e \
d90671f3
SS
45 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['volumes'].map{|v| '-v ' << v['volume']['host'] << ':' << v['volume']['guest'] << ' '}.join"`
46}
47
52388b87
SS
48[ -z $docker_path ] && {
49 install_docker
50}
51
5f803fb4
SS
52
53[ $# -ne 2 ] && {
54 usage
55}
56
7e738616
S
57if [ ! -e $config_file ]
58 then
59 echo "Config file was not found, ensure $config_file exists"
60 exit 1
61fi
62
5f803fb4
SS
63case "$command" in
64 bootstrap)
93149421 65
9fb5f2d3
SS
66 templates=`cat $config_file | docker run -rm -i -a stdin -a stdout $image ruby -e \
67 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['templates']"`
93149421 68
9fb5f2d3 69 arrTemplates=(${templates// / })
e9aa7df1 70 config_data=$(cat $config_file)
8f993af4 71 input="hack: true"
9fb5f2d3
SS
72
73 for template in "${arrTemplates[@]}"
74 do
75 [ ! -z $template ] && {
76 input="$input _FILE_SEPERATOR_ $(cat $template)"
77 }
78 done
93149421 79
8f993af4 80 # we always want our config file last so it takes priority
e9aa7df1 81 input="$input _FILE_SEPERATOR_ $config_data"
8f993af4 82
d90671f3
SS
83 set_volumes
84
2162f1d4
SS
85 rm -f $cidbootstrap
86
87 (exec echo "$input" | docker run -cidfile $cidbootstrap -i -a stdin -a stdout -a stderr $volumes $image \
88 /bin/bash -c 'cd /pups && git pull && /pups/bin/pups --stdin') \
89 || (docker rm `cat $cidbootstrap` && rm $cidbootstrap)
9fb5f2d3 90
2162f1d4
SS
91 [ ! -e $cidbootstrap ] && echo "FAILED TO BOOTSTRAP" && exit 1
92
93 sleep 5
9fb5f2d3 94
5efded6a 95 docker commit `cat $cidbootstrap` $local_discourse/$config || echo 'FAILED TO COMMIT'
4b3aebe1 96 docker rm `cat $cidbootstrap` && rm $cidbootstrap
2162f1d4
SS
97
98 echo "Successfully bootstrappd, to starup use ./launcher start $config"
4b3aebe1 99 exit 0
5f803fb4 100 ;;
1acce9e4 101
5f803fb4
SS
102 ssh)
103 if [ ! -e $cidfile ]
104 then
105 echo "No cid found"
106 exit 1
107 else
108 cid="`cat $cidfile`"
109 address="`docker port $cid 22`"
110 split=(${address//:/ })
111 exec ssh root@${split[0]} -p ${split[1]}
112 fi
113 ;;
7e738616 114
5f803fb4
SS
115 stop)
116 if [ ! -e $cidfile ]
117 then
118 echo "No cid found"
119 exit 1
120 else
121 docker stop -t 10 `cat $cidfile`
122 exit 0
123 fi
124 ;;
7e738616 125
5f803fb4 126 logs)
7e738616 127
5f803fb4
SS
128 if [ ! -e $cidfile ]
129 then
130 echo "No cid found"
131 exit 1
132 else
133 docker logs `cat $cidfile`
134 exit 0
135 fi
136 ;;
7e738616 137
5f803fb4 138 start)
7e738616 139
5f803fb4
SS
140 if [ ! -e $cidfile ]
141 then
142 echo "No cid found, creating a new container"
b86ac1af 143 ports=`cat $config_file | docker run -rm -i -a stdout -a stdin $image ruby -e \
36941588 144 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['expose'].map{|p| '-p ' << p.to_s << ' '}.join"`
d90671f3
SS
145
146 set_volumes
147
5efded6a 148 docker run -name $config -cidfile $cidfile $ports -d $volumes $local_discourse/$config /usr/bin/runsvdir -P /etc/service
5f803fb4
SS
149 exit 0
150 else
151 echo "cid found, ensuring container is started"
152 docker start `cat $cidfile`
153 exit 0
154 fi
155 ;;
7e738616 156
7e738616 157
5f803fb4
SS
158 destroy)
159 if [ -e $cidfile ]
160 then
161 echo "destroying container $cidfile"
162 docker stop -t 10 `cat $cidfile`
163 docker rm `cat $cidfile` && rm $cidfile
164 exit 0
165 else
166 echo "nothing to destroy cidfile does not exist"
167 exit 1
168 fi
169 ;;
170esac
7e738616 171
5f803fb4 172usage