rm an extra process
[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
42[ -z $docker_path ] && {
43 install_docker
44}
45
5f803fb4
SS
46
47[ $# -ne 2 ] && {
48 usage
49}
50
7e738616
S
51if [ ! -e $config_file ]
52 then
53 echo "Config file was not found, ensure $config_file exists"
54 exit 1
55fi
56
5f803fb4
SS
57case "$command" in
58 bootstrap)
93149421
SS
59
60 read -r -d '' ruby <<RUBY
61 require 'yaml'
62 puts YAML.load(STDIN.readlines.join)["template"]
63RUBY
64
f378ed33 65 template=`cat $config_file| docker run -rm -i -a stdin -a stdout samsaffron/discourse ruby -e "$ruby"`
93149421
SS
66
67 input=$(cat $config_file)
68 [ ! -z $template ] && {
69 input="$input _FILE_SEPERATOR_ $(cat $template)"
70 }
71
8eeefce1 72 exec echo "$input" | docker run -cidfile $cidbootstrap -i -a stdin -a stdout -a stderr -v `pwd`/shared:/shared samsaffron/discourse /pups/bin/pups --stdin
d93d239c 73 sleep 5
5f803fb4 74 docker commit `cat $cidbootstrap` samsaffron/discourse $config
4b3aebe1
SS
75 docker rm `cat $cidbootstrap` && rm $cidbootstrap
76 exit 0
5f803fb4 77 ;;
1acce9e4 78
5f803fb4
SS
79 ssh)
80 if [ ! -e $cidfile ]
81 then
82 echo "No cid found"
83 exit 1
84 else
85 cid="`cat $cidfile`"
86 address="`docker port $cid 22`"
87 split=(${address//:/ })
88 exec ssh root@${split[0]} -p ${split[1]}
89 fi
90 ;;
7e738616 91
5f803fb4
SS
92 stop)
93 if [ ! -e $cidfile ]
94 then
95 echo "No cid found"
96 exit 1
97 else
98 docker stop -t 10 `cat $cidfile`
99 exit 0
100 fi
101 ;;
7e738616 102
5f803fb4 103 logs)
7e738616 104
5f803fb4
SS
105 if [ ! -e $cidfile ]
106 then
107 echo "No cid found"
108 exit 1
109 else
110 docker logs `cat $cidfile`
111 exit 0
112 fi
113 ;;
7e738616 114
5f803fb4 115 start)
7e738616 116
5f803fb4
SS
117 if [ ! -e $cidfile ]
118 then
119 echo "No cid found, creating a new container"
36941588
SS
120 ports=`cat $config_file | docker run -rm -i -a stdout -a stdin samsaffron/discourse ruby -e \
121 "require 'yaml'; puts YAML.load(STDIN.readlines.join)['expose'].map{|p| '-p ' << p.to_s << ' '}.join"`
52388b87 122 docker run -cidfile $cidfile $ports -d -v `pwd`/shared:/shared samsaffron/discourse:$config /usr/bin/runsvdir -P /etc/service
5f803fb4
SS
123 exit 0
124 else
125 echo "cid found, ensuring container is started"
126 docker start `cat $cidfile`
127 exit 0
128 fi
129 ;;
7e738616 130
7e738616 131
5f803fb4
SS
132 destroy)
133 if [ -e $cidfile ]
134 then
135 echo "destroying container $cidfile"
136 docker stop -t 10 `cat $cidfile`
137 docker rm `cat $cidfile` && rm $cidfile
138 exit 0
139 else
140 echo "nothing to destroy cidfile does not exist"
141 exit 1
142 fi
143 ;;
144esac
7e738616 145
5f803fb4 146usage