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