add a hook for ssh
[discourse_docker.git] / launcher
1 #!/bin/bash
2
3 command=$1
4 config=$2
5 config_file="$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 usage () {
13 echo "Usage: launcher COMMAND CONFIG"
14 echo "Commands:"
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"
19 echo " ssh: Start a bash shell in a running container"
20 echo " logs: Docker logs for container"
21 echo " bootstrap: Bootstrap a container for the config base on an image"
22 exit 1
23 }
24
25 install_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
43 set_volumes() {
44 volumes=`cat $config_file | docker run -rm -i -a stdout -a stdin $image ruby -e \
45 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['volumes'].map{|v| '-v ' << v['volume']['host'] << ':' << v['volume']['guest'] << ' '}.join"`
46 }
47
48 [ -z $docker_path ] && {
49 install_docker
50 }
51
52
53 [ $# -ne 2 ] && {
54 usage
55 }
56
57 if [ ! -e $config_file ]
58 then
59 echo "Config file was not found, ensure $config_file exists"
60 exit 1
61 fi
62
63 case "$command" in
64 bootstrap)
65
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']"`
68
69 arrTemplates=(${templates// / })
70 input=$(cat $config_file)
71
72
73 for template in "${arrTemplates[@]}"
74 do
75 [ ! -z $template ] && {
76 input="$input _FILE_SEPERATOR_ $(cat $template)"
77 }
78 done
79
80 set_volumes
81
82 rm -f $cidbootstrap
83
84 (exec echo "$input" | docker run -cidfile $cidbootstrap -i -a stdin -a stdout -a stderr $volumes $image \
85 /bin/bash -c 'cd /pups && git pull && /pups/bin/pups --stdin') \
86 || (docker rm `cat $cidbootstrap` && rm $cidbootstrap)
87
88 [ ! -e $cidbootstrap ] && echo "FAILED TO BOOTSTRAP" && exit 1
89
90 sleep 5
91
92 docker commit `cat $cidbootstrap` $local_discourse/$config || echo 'FAILED TO COMMIT'
93 docker rm `cat $cidbootstrap` && rm $cidbootstrap
94
95 echo "Successfully bootstrappd, to starup use ./launcher start $config"
96 exit 0
97 ;;
98
99 ssh)
100 if [ ! -e $cidfile ]
101 then
102 echo "No cid found"
103 exit 1
104 else
105 cid="`cat $cidfile`"
106 address="`docker port $cid 22`"
107 split=(${address//:/ })
108 exec ssh root@${split[0]} -p ${split[1]}
109 fi
110 ;;
111
112 stop)
113 if [ ! -e $cidfile ]
114 then
115 echo "No cid found"
116 exit 1
117 else
118 docker stop -t 10 `cat $cidfile`
119 exit 0
120 fi
121 ;;
122
123 logs)
124
125 if [ ! -e $cidfile ]
126 then
127 echo "No cid found"
128 exit 1
129 else
130 docker logs `cat $cidfile`
131 exit 0
132 fi
133 ;;
134
135 start)
136
137 if [ ! -e $cidfile ]
138 then
139 echo "No cid found, creating a new container"
140 ports=`cat $config_file | docker run -rm -i -a stdout -a stdin $image ruby -e \
141 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['expose'].map{|p| '-p ' << p.to_s << ' '}.join"`
142
143 set_volumes
144
145 docker run -name $config -cidfile $cidfile $ports -d $volumes $local_discourse/$config /usr/bin/runsvdir -P /etc/service
146 exit 0
147 else
148 echo "cid found, ensuring container is started"
149 docker start `cat $cidfile`
150 exit 0
151 fi
152 ;;
153
154
155 destroy)
156 if [ -e $cidfile ]
157 then
158 echo "destroying container $cidfile"
159 docker stop -t 10 `cat $cidfile`
160 docker rm `cat $cidfile` && rm $cidfile
161 exit 0
162 else
163 echo "nothing to destroy cidfile does not exist"
164 exit 1
165 fi
166 ;;
167 esac
168
169 usage