Mercurial > sat_docs
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/docker/libervia_cont.sh Sun Oct 19 15:14:40 2014 +0200 @@ -0,0 +1,258 @@ +#!/bin/sh + +# Libervia container manager +# Copyright (C) 2014 Jérôme Poisson (goffi@goffi.org) + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. + +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +VERSION="0.1.0" +ACCOUNT="salutatoi" + +CONTAINERS="prosody sat_pubsub salut sat libervia" + +DETACH="-d" +TERM="-ti" + +VOLUME_CONT="$ACCOUNT/data" +VOLUME_ALIAS="sat_data" +VOLUME="--volumes-from $VOLUME_ALIAS" + +PUBLIC=0 + +PROSODY_PORTS="5222 5269" +PORT_5222_NAME="XMPP client to server" +PORT_5269_NAME="XMPP server to server" +SAT_PORTS="10143 10125 28915" +PORT_10143_NAME="IMAP server" +PORT_10125_NAME="SMTP server" +PORT_28915_NAME="XMPP file transfer" +LIBERVIA_PORTS="8080 8443" +PORT_8080_NAME="HTTP" +PORT_8443_NAME="HTTPS" +NO_PORT="No public port" + +USAGE="Usage: $0 [run|stop|update|backup|ports] [ARGS...]" + +HELP_SITE="http://wiki.goffi.org/wiki/Docker/en" +HELP_MUC="sat@chat.jabberfr.org" + +get_dyn_var() { + # get dynamicly variable based on given name + + name=$1 + var_type=$2 + name_upp=$(echo "$name" | tr '[:lower:]' '[:upper:]') + case $var_type in + ports) eval echo $(echo "\$${name_upp}_PORTS");; + port_name) eval echo $(echo "\$PORT_${name_upp}_NAME");; + esac +} + +list_ports() { + # list used ports in currently running containers + + for cont in $CONTAINERS; do + # we get variable name with uppercase container name + # some magic to get the ports + ports=$(get_dyn_var $cont ports) + + [ -n "$ports" ] && printf "== $cont ==\n\n" + + for port in $ports; do + # some magic to get port human readable name + port_name=$(get_dyn_var $port port_name) + real_port=$(docker port $cont $port 2>&1) + if [ $? -ne 0 ]; then + real_port=$NO_PORT + fi + + # we now show the ports with nice alignment + desc="port $port ($port_name):" + nb_tabs=$((5-${#desc}/8)) + printf "$desc" + for i in $(seq $nb_tabs); do + printf "\t" + done + printf "$real_port\n" + done + [ -n "$ports" ] && printf '\n' + done +} + +public_ports_arg() { + # create Docker arg to have public ports corresponding to container ports + + if [ $PUBLIC -ne 1 ]; then + return + fi + cont=$1 + ports=$(get_dyn_var $cont ports) + ARG="" + for port in $ports; do + ARG="$ARG -p $port:$port" + done + echo $ARG +} + +parse_run_args() { + # manage arguments for run command + + while [ $# -gt 0 ]; do + case "$1" in + + -h|--help) + cat << OPT_END +options available for the run command: + +-h, --help display this help message +-p, --public publish using true ports +-d DOMAIN, --domain DOMAIN use DOMAIN as domain name +OPT_END + exit 0 + ;; + + -d|--domain) + shift + if [ $# -eq 0 ]; then + printf "no domain given, --domain must be followed by a domain\n" + exit 1 + fi + DOMAIN=$1 + shift + ;; + + -p|--public) + shift + PUBLIC=1 + ;; + + *) printf "Invalid argument, please check \"$0 run --help\"\n" + exit 1 + ;; + esac + done +} + + +if [ $# -ge 1 ];then + case $1 in + run) CMD=RUN;; + stop) CMD=STOP;; + update) CMD=UPDATE;; + backup) CMD=BACKUP;; + ports) CMD=PORTS;; + -v|--version) printf "$VERSION\n"; exit 0;; + -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;; + *) echo $USAGE + exit 1 + esac + shift +else + CMD=RUN +fi + +case $CMD in + RUN) + parse_run_args "$@" + printf "Running data container... " + docker_id=$(docker run -d -ti --name $VOLUME_ALIAS $VOLUME_CONT 2>&1) + if [ $? -eq 0 ]; then + printf "OK ($docker_id)\n" + else + echo $docker_id | grep Conflict > /dev/null 2>&1 + if [ $? -eq 0 ]; then + printf "A data container already exists ($VOLUME_ALIAS), use \"docker ps -a\" to see it\n" + else + printf "Error, please check data volume\nerror message: $docker_id\n" + exit 1 + fi + fi + printf "\nRunning Libervia\n\n" + for CONT in $CONTAINERS; do + case $CONT in + prosody) OPTS="$DETACH $TERM $VOLUME $(public_ports_arg $CONT) --name prosody" + if [ -n "$DOMAIN" ]; then + OPTS="-e DOMAIN=$DOMAIN $OPTS" + fi + ;; + sat_pubsub) OPTS="$DETACH $TERM $VOLUME --name sat_pubsub --link=prosody:prosody";; + salut) OPTS="$DETACH $TERM $VOLUME --name salut --link=prosody:prosody";; + sat) OPTS="$DETACH $TERM $VOLUME -P $(public_ports_arg $CONT) --name sat --link=prosody:prosody";; + libervia) OPTS="$DETACH $TERM $VOLUME --volumes-from sat -P $(public_ports_arg $CONT) --name libervia --link=sat:sat";; + *) printf "Unkown container $CONT\n"; exit 1 + esac + printf "Launching $CONT... " + docker_id=$(docker run $OPTS $ACCOUNT/$CONT 2>&1) + if [ $? -eq 0 ]; then + printf "OK ($docker_id)\n" + else + printf "Error, please check container or ask help on XMPP MUC sat@chat.jabberfr.org\nerror message: $docker_id\n" + printf "Abandon\n" + exit 1 + fi + done + 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' + 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" + printf 'Below are the ports used to connect, you can go with your browser to Libervia HTTP port\n\n' + list_ports + ;; + STOP) + printf "stopping Libervia\n" + REVERSED="" + for CONT in $CONTAINERS; do + REVERSED="$CONT $REVERSED" + done; + for CONT in $REVERSED; do + printf "\nStopping container $CONT" + docker stop $CONT > /dev/null 2>&1 || printf "... Error while stopping $CONT\n" + printf "\rDeleting container $CONT" + docker rm $CONT > /dev/null 2>&1 || printf "... Error while removing $CONT\n" + done + printf "\n" + ;; + UPDATE) + printf "updating containers" + errors=0 + for CONT in $CONTAINERS data; do + printf "\nupdating $CONT" + docker pull $ACCOUNT/$CONT > /dev/null 2>&1 + if [ $? -ne 0 ]; then + printf "... Error while updating $ACCOUNT/$CONT" + errors=1 + fi + done + if [ errors -eq 0 ]; then + printf "\nContainers are up-to-date" + else + printf "\nSome errors happened while updating containers" + exit 1 + fi + ;; + BACKUP) + filename="sat_data_backup_$(date '+%Y-%m-%d_%H:%M:%S').tar.gz" + printf "backing up data container to $filename\n\n" + docker run --rm --volumes-from sat_data -v $(pwd):/backup debian:jessie tar zcvf /backup/$filename -C / -h volumes + if [ $? -eq 0 ]; then + printf "\nBackup finished\n" + else + printf "\nBackup Error !\n" + exit 1 + fi + ;; + PORTS) + list_ports + ;; + *) printf "Error: unknown command !" + exit 2 +esac