changeset 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 bcba1966e6db
children 53a579906c32
files docker/libervia_cont.sh
diffstat 1 files changed, 63 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/docker/libervia_cont.sh	Thu Feb 18 17:31:09 2016 +0100
+++ b/docker/libervia_cont.sh	Thu Feb 18 17:35:07 2016 +0100
@@ -1,7 +1,7 @@
 #!/bin/sh
 
 # Libervia container manager
-# Copyright (C) 2014 Jérôme Poisson (goffi@goffi.org)
+# Copyright (C) 2014-2016 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
@@ -16,18 +16,18 @@
 # 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"
+VERSION="0.3.0"
 ACCOUNT="salutatoi"
 
 CONTAINERS="prosody sat_pubsub salut sat libervia"
 
-DETACH="-d"
-TERM="-ti"
+DK_DETACH="-d"
+DK_TERM="-ti"
 
 VOLUME_NAME="data"
 VOLUME_CONT="$ACCOUNT/$VOLUME_NAME"
 VOLUME_ALIAS="sat_data"
-VOLUME="--volumes-from $VOLUME_ALIAS"
+DK_VOLUME="--volumes-from $VOLUME_ALIAS"
 
 PUBLIC=0
 
@@ -45,11 +45,13 @@
 
 DOCKER_EXE="docker"
 
-USAGE="Usage: $0 [run|stop|update|backup|ports] [ARGS...]"
+USAGE="Usage: $0 [start|stop|restart|update|backup|ports|config] [ARGS...]"
 
 HELP_SITE="http://wiki.goffi.org/wiki/Docker/en"
 HELP_MUC="sat@chat.jabberfr.org"
 
+DEFAULT_TMP_DIR="/tmp/tmp_sat_docker"
+
 get_dyn_var() {
     # get dynamicly variable based on given name
 
@@ -129,7 +131,7 @@
 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"
+        printf "Docker is not installed or not accessible, please install it.\nYou can check $HELP_SITE for instructions\n"
         return 1
     fi
 }
@@ -177,11 +179,13 @@
 
 if [ $# -ge 1 ];then
     case $1 in
-        run) CMD=RUN;;
+        start) CMD=START;;
         stop) CMD=STOP;;
+        restart) CMD=RESTART;;
         update) CMD=UPDATE;;
         backup) CMD=BACKUP;;
         ports) CMD=PORTS;;
+        config) CMD=CONFIG;;
         -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
@@ -189,11 +193,11 @@
     esac
     shift
 else
-    CMD=RUN
+    CMD=START
 fi
 
 case $CMD in
-    RUN)
+    START)
         parse_run_args "$@"
         download_missing || exit 1
 
@@ -213,15 +217,15 @@
         printf "\nRunning Libervia\n\n"
         for CONT in $CONTAINERS; do
             case $CONT in
-                prosody) OPTS="$DETACH $TERM $VOLUME $(public_ports_arg $CONT) --name prosody"
+                prosody) OPTS="$DK_DETACH $DK_TERM $DK_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";;
+                sat_pubsub) OPTS="$DK_DETACH $DK_TERM $DK_VOLUME --name sat_pubsub --link=prosody:prosody";;
+                salut) OPTS="$DK_DETACH $DK_TERM $DK_VOLUME --name salut --link=prosody:prosody";;
+                sat) OPTS="$DK_DETACH $DK_TERM $DK_VOLUME -P $(public_ports_arg $CONT) --name sat --link=prosody:prosody";;
+                libervia) OPTS="$DK_DETACH $DK_TERM $DK_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... "
@@ -253,6 +257,10 @@
         done
         printf "\n"
         ;;
+	RESTART)
+		printf "restarting containers...\n"
+		"$0" stop && "$0" start "$@"
+		;;
     UPDATE)
         printf "updating images...\n"
         errors=0
@@ -274,7 +282,7 @@
     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
+        docker run --rm $DK_VOLUME -v "$(pwd):/backup" debian:jessie tar zcvf "/backup/$filename" -C / -h volumes
         if [ $? -eq 0 ]; then
             printf "\nBackup finished\n"
         else
@@ -285,6 +293,45 @@
     PORTS)
         list_ports
         ;;
+    CONFIG)
+        case $# in
+            0) CONF="libervia";;
+            1) CONF="$1";;
+            *) CONF="";;
+        esac
+        case $CONF in
+            libervia)
+               CONT_CONF_FILE="/home/sat/.config/sat/sat.conf"
+               ;;
+            prosody)
+               CONT_CONF_FILE="/etc/prosody/prosody_sat_cfg/prosody.cfg.lua"
+               ;;
+            *) printf "\nPlease enter type of configuration to edit (libervia, prosody)\n"
+               exit 1
+               ;;
+        esac
+
+        HOST_CONF_FILE=$(basename $CONT_CONF_FILE)
+
+        printf "\ngetting configuration for $CONF\n"
+        # we copy config file to a temporary dit
+        # then edit with $EDITOR and put it back
+
+        TMP_DIR=$(mktemp -d 2>/dev/null)
+        if [ $? -ne 0 ]; then
+            TMP_DIR="$DEFAULT_TMP_DIR"
+            mkdir -p "$TMP_DIR"
+        fi
+
+        docker cp "$VOLUME_ALIAS:$CONT_CONF_FILE" "$TMP_DIR/$HOST_CONF_FILE"
+        "$EDITOR" "$TMP_DIR/$HOST_CONF_FILE"
+        if [ $? -eq 0 -a -s "$TMP_DIR/$HOST_CONF_FILE" ]; then
+            printf "updating configuration\n"
+            docker cp "$TMP_DIR/$HOST_CONF_FILE" "$VOLUME_ALIAS:$CONT_CONF_FILE"
+        fi
+        rm -rf "$TMP_DIR"
+        ;;
+
     *) printf "Error: unknown command !"
        exit 2
 esac