comparison docker/libervia_cont.sh @ 86:f5067e3112ba

docker (libervia_cont): new commands: - renamed "run" to "start" for consistency with other tools - added "restart" command which do a stop then a start - added "config" command which edit container configuration with $EDITOR. "libervia" and "prosody" can be specified, default is libervia.
author Goffi <goffi@goffi.org>
date Thu, 18 Feb 2016 17:35:07 +0100
parents 9ca1e7fe0f87
children 53a579906c32
comparison
equal deleted inserted replaced
85:bcba1966e6db 86:f5067e3112ba
1 #!/bin/sh 1 #!/bin/sh
2 2
3 # Libervia container manager 3 # Libervia container manager
4 # Copyright (C) 2014 Jérôme Poisson (goffi@goffi.org) 4 # Copyright (C) 2014-2016 Jérôme Poisson (goffi@goffi.org)
5 5
6 # This program is free software: you can redistribute it and/or modify 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 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 8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version. 9 # (at your option) any later version.
14 # GNU Affero General Public License for more details. 14 # GNU Affero General Public License for more details.
15 15
16 # You should have received a copy of the GNU Affero General Public License 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/>. 17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 18
19 VERSION="0.2.0" 19 VERSION="0.3.0"
20 ACCOUNT="salutatoi" 20 ACCOUNT="salutatoi"
21 21
22 CONTAINERS="prosody sat_pubsub salut sat libervia" 22 CONTAINERS="prosody sat_pubsub salut sat libervia"
23 23
24 DETACH="-d" 24 DK_DETACH="-d"
25 TERM="-ti" 25 DK_TERM="-ti"
26 26
27 VOLUME_NAME="data" 27 VOLUME_NAME="data"
28 VOLUME_CONT="$ACCOUNT/$VOLUME_NAME" 28 VOLUME_CONT="$ACCOUNT/$VOLUME_NAME"
29 VOLUME_ALIAS="sat_data" 29 VOLUME_ALIAS="sat_data"
30 VOLUME="--volumes-from $VOLUME_ALIAS" 30 DK_VOLUME="--volumes-from $VOLUME_ALIAS"
31 31
32 PUBLIC=0 32 PUBLIC=0
33 33
34 PROSODY_PORTS="5222 5269" 34 PROSODY_PORTS="5222 5269"
35 PORT_5222_NAME="XMPP client to server" 35 PORT_5222_NAME="XMPP client to server"
43 PORT_8443_NAME="HTTPS" 43 PORT_8443_NAME="HTTPS"
44 NO_PORT="No public port" 44 NO_PORT="No public port"
45 45
46 DOCKER_EXE="docker" 46 DOCKER_EXE="docker"
47 47
48 USAGE="Usage: $0 [run|stop|update|backup|ports] [ARGS...]" 48 USAGE="Usage: $0 [start|stop|restart|update|backup|ports|config] [ARGS...]"
49 49
50 HELP_SITE="http://wiki.goffi.org/wiki/Docker/en" 50 HELP_SITE="http://wiki.goffi.org/wiki/Docker/en"
51 HELP_MUC="sat@chat.jabberfr.org" 51 HELP_MUC="sat@chat.jabberfr.org"
52
53 DEFAULT_TMP_DIR="/tmp/tmp_sat_docker"
52 54
53 get_dyn_var() { 55 get_dyn_var() {
54 # get dynamicly variable based on given name 56 # get dynamicly variable based on given name
55 57
56 name=$1 58 name=$1
127 } 129 }
128 130
129 check_docker() { 131 check_docker() {
130 which $DOCKER_EXE > /dev/null 2>&1 132 which $DOCKER_EXE > /dev/null 2>&1
131 if [ $? -ne 0 ]; then 133 if [ $? -ne 0 ]; then
132 printf "Docker is not installed or not accessible, please install it.\nYou can check http://wiki.goffi.org/wiki/Docker/en for instructions\n" 134 printf "Docker is not installed or not accessible, please install it.\nYou can check $HELP_SITE for instructions\n"
133 return 1 135 return 1
134 fi 136 fi
135 } 137 }
136 138
137 parse_run_args() { 139 parse_run_args() {
175 177
176 check_docker || exit 1 178 check_docker || exit 1
177 179
178 if [ $# -ge 1 ];then 180 if [ $# -ge 1 ];then
179 case $1 in 181 case $1 in
180 run) CMD=RUN;; 182 start) CMD=START;;
181 stop) CMD=STOP;; 183 stop) CMD=STOP;;
184 restart) CMD=RESTART;;
182 update) CMD=UPDATE;; 185 update) CMD=UPDATE;;
183 backup) CMD=BACKUP;; 186 backup) CMD=BACKUP;;
184 ports) CMD=PORTS;; 187 ports) CMD=PORTS;;
188 config) CMD=CONFIG;;
185 -v|--version) printf "$VERSION\n"; exit 0;; 189 -v|--version) printf "$VERSION\n"; exit 0;;
186 -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;; 190 -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;;
187 *) echo $USAGE 191 *) echo $USAGE
188 exit 1 192 exit 1
189 esac 193 esac
190 shift 194 shift
191 else 195 else
192 CMD=RUN 196 CMD=START
193 fi 197 fi
194 198
195 case $CMD in 199 case $CMD in
196 RUN) 200 START)
197 parse_run_args "$@" 201 parse_run_args "$@"
198 download_missing || exit 1 202 download_missing || exit 1
199 203
200 printf "Running data container... " 204 printf "Running data container... "
201 docker_id=$(docker run -d -ti --name $VOLUME_ALIAS $VOLUME_CONT 2>&1) 205 docker_id=$(docker run -d -ti --name $VOLUME_ALIAS $VOLUME_CONT 2>&1)
211 fi 215 fi
212 fi 216 fi
213 printf "\nRunning Libervia\n\n" 217 printf "\nRunning Libervia\n\n"
214 for CONT in $CONTAINERS; do 218 for CONT in $CONTAINERS; do
215 case $CONT in 219 case $CONT in
216 prosody) OPTS="$DETACH $TERM $VOLUME $(public_ports_arg $CONT) --name prosody" 220 prosody) OPTS="$DK_DETACH $DK_TERM $DK_VOLUME $(public_ports_arg $CONT) --name prosody"
217 if [ -n "$DOMAIN" ]; then 221 if [ -n "$DOMAIN" ]; then
218 OPTS="-e DOMAIN=$DOMAIN $OPTS" 222 OPTS="-e DOMAIN=$DOMAIN $OPTS"
219 fi 223 fi
220 ;; 224 ;;
221 sat_pubsub) OPTS="$DETACH $TERM $VOLUME --name sat_pubsub --link=prosody:prosody";; 225 sat_pubsub) OPTS="$DK_DETACH $DK_TERM $DK_VOLUME --name sat_pubsub --link=prosody:prosody";;
222 salut) OPTS="$DETACH $TERM $VOLUME --name salut --link=prosody:prosody";; 226 salut) OPTS="$DK_DETACH $DK_TERM $DK_VOLUME --name salut --link=prosody:prosody";;
223 sat) OPTS="$DETACH $TERM $VOLUME -P $(public_ports_arg $CONT) --name sat --link=prosody:prosody";; 227 sat) OPTS="$DK_DETACH $DK_TERM $DK_VOLUME -P $(public_ports_arg $CONT) --name sat --link=prosody:prosody";;
224 libervia) OPTS="$DETACH $TERM $VOLUME --volumes-from sat -P $(public_ports_arg $CONT) --name libervia --link=sat:sat";; 228 libervia) OPTS="$DK_DETACH $DK_TERM $DK_VOLUME --volumes-from sat -P $(public_ports_arg $CONT) --name libervia --link=sat:sat";;
225 *) printf "Unkown container $CONT\n"; exit 1 229 *) printf "Unkown container $CONT\n"; exit 1
226 esac 230 esac
227 printf "Launching $CONT... " 231 printf "Launching $CONT... "
228 docker_id=$(docker run $OPTS $ACCOUNT/$CONT 2>&1) 232 docker_id=$(docker run $OPTS $ACCOUNT/$CONT 2>&1)
229 if [ $? -eq 0 ]; then 233 if [ $? -eq 0 ]; then
251 printf "\rDeleting container $CONT" 255 printf "\rDeleting container $CONT"
252 docker rm $CONT > /dev/null 2>&1 || printf "... Error while removing $CONT\n" 256 docker rm $CONT > /dev/null 2>&1 || printf "... Error while removing $CONT\n"
253 done 257 done
254 printf "\n" 258 printf "\n"
255 ;; 259 ;;
260 RESTART)
261 printf "restarting containers...\n"
262 "$0" stop && "$0" start "$@"
263 ;;
256 UPDATE) 264 UPDATE)
257 printf "updating images...\n" 265 printf "updating images...\n"
258 errors=0 266 errors=0
259 for CONT in $CONTAINERS data; do 267 for CONT in $CONTAINERS data; do
260 printf "\n*** updating $CONT ***\n" 268 printf "\n*** updating $CONT ***\n"
272 fi 280 fi
273 ;; 281 ;;
274 BACKUP) 282 BACKUP)
275 filename="sat_data_backup_$(date '+%Y-%m-%d_%H:%M:%S').tar.gz" 283 filename="sat_data_backup_$(date '+%Y-%m-%d_%H:%M:%S').tar.gz"
276 printf "backing up data container to $filename\n\n" 284 printf "backing up data container to $filename\n\n"
277 docker run --rm --volumes-from sat_data -v $(pwd):/backup debian:jessie tar zcvf /backup/$filename -C / -h volumes 285 docker run --rm $DK_VOLUME -v "$(pwd):/backup" debian:jessie tar zcvf "/backup/$filename" -C / -h volumes
278 if [ $? -eq 0 ]; then 286 if [ $? -eq 0 ]; then
279 printf "\nBackup finished\n" 287 printf "\nBackup finished\n"
280 else 288 else
281 printf "\nBackup Error !\n" 289 printf "\nBackup Error !\n"
282 exit 1 290 exit 1
283 fi 291 fi
284 ;; 292 ;;
285 PORTS) 293 PORTS)
286 list_ports 294 list_ports
287 ;; 295 ;;
296 CONFIG)
297 case $# in
298 0) CONF="libervia";;
299 1) CONF="$1";;
300 *) CONF="";;
301 esac
302 case $CONF in
303 libervia)
304 CONT_CONF_FILE="/home/sat/.config/sat/sat.conf"
305 ;;
306 prosody)
307 CONT_CONF_FILE="/etc/prosody/prosody_sat_cfg/prosody.cfg.lua"
308 ;;
309 *) printf "\nPlease enter type of configuration to edit (libervia, prosody)\n"
310 exit 1
311 ;;
312 esac
313
314 HOST_CONF_FILE=$(basename $CONT_CONF_FILE)
315
316 printf "\ngetting configuration for $CONF\n"
317 # we copy config file to a temporary dit
318 # then edit with $EDITOR and put it back
319
320 TMP_DIR=$(mktemp -d 2>/dev/null)
321 if [ $? -ne 0 ]; then
322 TMP_DIR="$DEFAULT_TMP_DIR"
323 mkdir -p "$TMP_DIR"
324 fi
325
326 docker cp "$VOLUME_ALIAS:$CONT_CONF_FILE" "$TMP_DIR/$HOST_CONF_FILE"
327 "$EDITOR" "$TMP_DIR/$HOST_CONF_FILE"
328 if [ $? -eq 0 -a -s "$TMP_DIR/$HOST_CONF_FILE" ]; then
329 printf "updating configuration\n"
330 docker cp "$TMP_DIR/$HOST_CONF_FILE" "$VOLUME_ALIAS:$CONT_CONF_FILE"
331 fi
332 rm -rf "$TMP_DIR"
333 ;;
334
288 *) printf "Error: unknown command !" 335 *) printf "Error: unknown command !"
289 exit 2 336 exit 2
290 esac 337 esac