view docker/libervia_cont.sh @ 42:bbb900a768ea

xeps: updated xep-0355 (namespace delegation): Several updates according to feedbacks: - Added attribute filtering, mainly to manage correctly MAM - Simplification of configuration in admin mode: no more <iq/> negociation - Delegations notifications to the managing entity are now done with a <message/> stanza - Forwarding now use XEP-0297 to avoid server to parse every stanza and a to have a cleaner separation - The behaviour when managing entity is unavailable is specified - Use of namespace in field var for client mode negociation is now mandatory - Behaviour when managing a delegation kept between session in client mode, when managing entity is not available, is specified - Implementation note added for in-order processing issue (thanks to Kurt Zeilenga for pointing this) - Added an note for discovery nesting with attribute filtering - Updated namespace to reflect incompatible changes
author Goffi <goffi@goffi.org>
date Sat, 21 Mar 2015 20:07:46 +0100
parents 9ca1e7fe0f87
children f5067e3112ba
line wrap: on
line source

#!/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.2.0"
ACCOUNT="salutatoi"

CONTAINERS="prosody sat_pubsub salut sat libervia"

DETACH="-d"
TERM="-ti"

VOLUME_NAME="data"
VOLUME_CONT="$ACCOUNT/$VOLUME_NAME"
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"

DOCKER_EXE="docker"

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
}

download_missing() {
    # download images wich are not present locally
    for cont in $CONTAINERS $VOLUME_NAME; do
        image="$ACCOUNT/$cont"
        docker inspect $image:latest > /dev/null 2>&1
        if [ $? -ne 0 ]; then
            printf "$image is not present locally, downloading it\n"
            docker pull $image:latest
            if [ $? -eq 0 ]; then
                printf "\nDownload of latest $image finished\n\n"
            else
                printf "\nError while downloading $image, please check your connection and logs\n"
                return 1
            fi
        fi
    done
}

check_docker() {
    which $DOCKER_EXE > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        printf "Docker is not installed or not accessible, please install it.\nYou can check http://wiki.goffi.org/wiki/Docker/en for instructions\n"
        return 1
    fi
}

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
}

check_docker || exit 1

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 "$@"
        download_missing || exit 1

        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 images...\n"
        errors=0
        for CONT in $CONTAINERS data; do
            printf "\n*** updating $CONT ***\n"
            docker pull $ACCOUNT/$CONT:latest
            if [ $? -ne 0 ]; then
                printf "\nError while updating $ACCOUNT/$CONT\n"
                errors=1
            fi
        done
        if [ $errors -eq 0 ]; then
            printf "\n\nImages are up-to-date\n"
        else
            printf "\n\nSome errors happened while updating images\n"
            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