226547fdac0c3098dab0c644df580cc6e2c1f752
[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 image=samsaffron/discourse
9 docker_path=`which docker`
10
11 usage () {
12 echo "Usage: launcher COMMAND CONFIG"
13 echo "Commands:"
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"
18 echo " ssh: Start a bash shell in a running container"
19 echo " logs: Docker logs for container"
20 echo " bootstrap: Bootstrap a container for the config base on an image"
21 exit 1
22 }
23
24 install_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
46
47 [ $# -ne 2 ] && {
48 usage
49 }
50
51 if [ ! -e $config_file ]
52 then
53 echo "Config file was not found, ensure $config_file exists"
54 exit 1
55 fi
56
57 case "$command" in
58 bootstrap)
59 exec cat $config_file | docker run -cidfile $cidbootstrap -i -a stdin -a stdout -a stderr -v `pwd`/shared:/shared samsaffron/discourse /pups/bin/pups --stdin
60 sleep 5
61 docker commit `cat $cidbootstrap` samsaffron/discourse $config
62 docker rm `cat $cidbootstrap` && rm $cidbootstrap
63 exit 0
64 ;;
65
66 ssh)
67 if [ ! -e $cidfile ]
68 then
69 echo "No cid found"
70 exit 1
71 else
72 cid="`cat $cidfile`"
73 address="`docker port $cid 22`"
74 split=(${address//:/ })
75 exec ssh root@${split[0]} -p ${split[1]}
76 fi
77 ;;
78
79 stop)
80 if [ ! -e $cidfile ]
81 then
82 echo "No cid found"
83 exit 1
84 else
85 docker stop -t 10 `cat $cidfile`
86 exit 0
87 fi
88 ;;
89
90 logs)
91
92 if [ ! -e $cidfile ]
93 then
94 echo "No cid found"
95 exit 1
96 else
97 docker logs `cat $cidfile`
98 exit 0
99 fi
100 ;;
101
102 start)
103
104 if [ ! -e $cidfile ]
105 then
106 echo "No cid found, creating a new container"
107 ports=`cat $config_file | docker run -rm -i -a stdout -a stdin samsaffron/discourse ruby -e "require 'yaml'; puts YAML.load(STDIN.readlines.join)['expose'].map{|p| '-p ' << p.to_s << ' '}.join"`
108 echo $ports
109 docker run -cidfile $cidfile $ports -d -v `pwd`/shared:/shared samsaffron/discourse:$config /usr/bin/runsvdir -P /etc/service
110 exit 0
111 else
112 echo "cid found, ensuring container is started"
113 docker start `cat $cidfile`
114 exit 0
115 fi
116 ;;
117
118
119 destroy)
120 if [ -e $cidfile ]
121 then
122 echo "destroying container $cidfile"
123 docker stop -t 10 `cat $cidfile`
124 docker rm `cat $cidfile` && rm $cidfile
125 exit 0
126 else
127 echo "nothing to destroy cidfile does not exist"
128 exit 1
129 fi
130 ;;
131 esac
132
133 usage