comparison docker/libervia_cont.sh @ 21:0e78c8a4626e

Added Dockerfiles to create Docker images for easy installation + scripts to manages them. see README for details.
author Goffi <goffi@goffi.org>
date Sun, 19 Oct 2014 15:14:40 +0200
parents
children 2c861d6aa446
comparison
equal deleted inserted replaced
20:703a394b9780 21:0e78c8a4626e
1 #!/bin/sh
2
3 # Libervia container manager
4 # Copyright (C) 2014 Jérôme Poisson (goffi@goffi.org)
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Affero General Public License for more details.
15
16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 VERSION="0.1.0"
20 ACCOUNT="salutatoi"
21
22 CONTAINERS="prosody sat_pubsub salut sat libervia"
23
24 DETACH="-d"
25 TERM="-ti"
26
27 VOLUME_CONT="$ACCOUNT/data"
28 VOLUME_ALIAS="sat_data"
29 VOLUME="--volumes-from $VOLUME_ALIAS"
30
31 PUBLIC=0
32
33 PROSODY_PORTS="5222 5269"
34 PORT_5222_NAME="XMPP client to server"
35 PORT_5269_NAME="XMPP server to server"
36 SAT_PORTS="10143 10125 28915"
37 PORT_10143_NAME="IMAP server"
38 PORT_10125_NAME="SMTP server"
39 PORT_28915_NAME="XMPP file transfer"
40 LIBERVIA_PORTS="8080 8443"
41 PORT_8080_NAME="HTTP"
42 PORT_8443_NAME="HTTPS"
43 NO_PORT="No public port"
44
45 USAGE="Usage: $0 [run|stop|update|backup|ports] [ARGS...]"
46
47 HELP_SITE="http://wiki.goffi.org/wiki/Docker/en"
48 HELP_MUC="sat@chat.jabberfr.org"
49
50 get_dyn_var() {
51 # get dynamicly variable based on given name
52
53 name=$1
54 var_type=$2
55 name_upp=$(echo "$name" | tr '[:lower:]' '[:upper:]')
56 case $var_type in
57 ports) eval echo $(echo "\$${name_upp}_PORTS");;
58 port_name) eval echo $(echo "\$PORT_${name_upp}_NAME");;
59 esac
60 }
61
62 list_ports() {
63 # list used ports in currently running containers
64
65 for cont in $CONTAINERS; do
66 # we get variable name with uppercase container name
67 # some magic to get the ports
68 ports=$(get_dyn_var $cont ports)
69
70 [ -n "$ports" ] && printf "== $cont ==\n\n"
71
72 for port in $ports; do
73 # some magic to get port human readable name
74 port_name=$(get_dyn_var $port port_name)
75 real_port=$(docker port $cont $port 2>&1)
76 if [ $? -ne 0 ]; then
77 real_port=$NO_PORT
78 fi
79
80 # we now show the ports with nice alignment
81 desc="port $port ($port_name):"
82 nb_tabs=$((5-${#desc}/8))
83 printf "$desc"
84 for i in $(seq $nb_tabs); do
85 printf "\t"
86 done
87 printf "$real_port\n"
88 done
89 [ -n "$ports" ] && printf '\n'
90 done
91 }
92
93 public_ports_arg() {
94 # create Docker arg to have public ports corresponding to container ports
95
96 if [ $PUBLIC -ne 1 ]; then
97 return
98 fi
99 cont=$1
100 ports=$(get_dyn_var $cont ports)
101 ARG=""
102 for port in $ports; do
103 ARG="$ARG -p $port:$port"
104 done
105 echo $ARG
106 }
107
108 parse_run_args() {
109 # manage arguments for run command
110
111 while [ $# -gt 0 ]; do
112 case "$1" in
113
114 -h|--help)
115 cat << OPT_END
116 options available for the run command:
117
118 -h, --help display this help message
119 -p, --public publish using true ports
120 -d DOMAIN, --domain DOMAIN use DOMAIN as domain name
121 OPT_END
122 exit 0
123 ;;
124
125 -d|--domain)
126 shift
127 if [ $# -eq 0 ]; then
128 printf "no domain given, --domain must be followed by a domain\n"
129 exit 1
130 fi
131 DOMAIN=$1
132 shift
133 ;;
134
135 -p|--public)
136 shift
137 PUBLIC=1
138 ;;
139
140 *) printf "Invalid argument, please check \"$0 run --help\"\n"
141 exit 1
142 ;;
143 esac
144 done
145 }
146
147
148 if [ $# -ge 1 ];then
149 case $1 in
150 run) CMD=RUN;;
151 stop) CMD=STOP;;
152 update) CMD=UPDATE;;
153 backup) CMD=BACKUP;;
154 ports) CMD=PORTS;;
155 -v|--version) printf "$VERSION\n"; exit 0;;
156 -h|--help) printf "$USAGE\n\nYou can check $HELP_SITE for instructions, or go to $HELP_MUC XMPP MUC room for help\n"; exit 0;;
157 *) echo $USAGE
158 exit 1
159 esac
160 shift
161 else
162 CMD=RUN
163 fi
164
165 case $CMD in
166 RUN)
167 parse_run_args "$@"
168 printf "Running data container... "
169 docker_id=$(docker run -d -ti --name $VOLUME_ALIAS $VOLUME_CONT 2>&1)
170 if [ $? -eq 0 ]; then
171 printf "OK ($docker_id)\n"
172 else
173 echo $docker_id | grep Conflict > /dev/null 2>&1
174 if [ $? -eq 0 ]; then
175 printf "A data container already exists ($VOLUME_ALIAS), use \"docker ps -a\" to see it\n"
176 else
177 printf "Error, please check data volume\nerror message: $docker_id\n"
178 exit 1
179 fi
180 fi
181 printf "\nRunning Libervia\n\n"
182 for CONT in $CONTAINERS; do
183 case $CONT in
184 prosody) OPTS="$DETACH $TERM $VOLUME $(public_ports_arg $CONT) --name prosody"
185 if [ -n "$DOMAIN" ]; then
186 OPTS="-e DOMAIN=$DOMAIN $OPTS"
187 fi
188 ;;
189 sat_pubsub) OPTS="$DETACH $TERM $VOLUME --name sat_pubsub --link=prosody:prosody";;
190 salut) OPTS="$DETACH $TERM $VOLUME --name salut --link=prosody:prosody";;
191 sat) OPTS="$DETACH $TERM $VOLUME -P $(public_ports_arg $CONT) --name sat --link=prosody:prosody";;
192 libervia) OPTS="$DETACH $TERM $VOLUME --volumes-from sat -P $(public_ports_arg $CONT) --name libervia --link=sat:sat";;
193 *) printf "Unkown container $CONT\n"; exit 1
194 esac
195 printf "Launching $CONT... "
196 docker_id=$(docker run $OPTS $ACCOUNT/$CONT 2>&1)
197 if [ $? -eq 0 ]; then
198 printf "OK ($docker_id)\n"
199 else
200 printf "Error, please check container or ask help on XMPP MUC sat@chat.jabberfr.org\nerror message: $docker_id\n"
201 printf "Abandon\n"
202 exit 1
203 fi
204 done
205 printf '\nLibervia is launched and should be reachable in a couple of seconds.\nYou can check logs with "docker logs libervia" (or any other container name)\n'
206 printf "An \"admin\" account has been created, you can check its password on $VOLUME_ALIAS container, in file /home/sat/ADMIN_PWD. Config can be tuned on this container\n"
207 printf 'Below are the ports used to connect, you can go with your browser to Libervia HTTP port\n\n'
208 list_ports
209 ;;
210 STOP)
211 printf "stopping Libervia\n"
212 REVERSED=""
213 for CONT in $CONTAINERS; do
214 REVERSED="$CONT $REVERSED"
215 done;
216 for CONT in $REVERSED; do
217 printf "\nStopping container $CONT"
218 docker stop $CONT > /dev/null 2>&1 || printf "... Error while stopping $CONT\n"
219 printf "\rDeleting container $CONT"
220 docker rm $CONT > /dev/null 2>&1 || printf "... Error while removing $CONT\n"
221 done
222 printf "\n"
223 ;;
224 UPDATE)
225 printf "updating containers"
226 errors=0
227 for CONT in $CONTAINERS data; do
228 printf "\nupdating $CONT"
229 docker pull $ACCOUNT/$CONT > /dev/null 2>&1
230 if [ $? -ne 0 ]; then
231 printf "... Error while updating $ACCOUNT/$CONT"
232 errors=1
233 fi
234 done
235 if [ errors -eq 0 ]; then
236 printf "\nContainers are up-to-date"
237 else
238 printf "\nSome errors happened while updating containers"
239 exit 1
240 fi
241 ;;
242 BACKUP)
243 filename="sat_data_backup_$(date '+%Y-%m-%d_%H:%M:%S').tar.gz"
244 printf "backing up data container to $filename\n\n"
245 docker run --rm --volumes-from sat_data -v $(pwd):/backup debian:jessie tar zcvf /backup/$filename -C / -h volumes
246 if [ $? -eq 0 ]; then
247 printf "\nBackup finished\n"
248 else
249 printf "\nBackup Error !\n"
250 exit 1
251 fi
252 ;;
253 PORTS)
254 list_ports
255 ;;
256 *) printf "Error: unknown command !"
257 exit 2
258 esac