ninedocker/nine.sh

366 lines
12 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
#1 : action = default up
#2 : service = optionnel
# Include bibliotheque de fonction
. ./misc/tools/e-ihm.sh
# Include variable d'environnement global
mergeenv env/.env.merge env/.env env/.env.local
. env/.env.merge
# Construction du docker-compose
echo "services:" > docker-compose.yml
# Include service
declare -A services
for dir in $(ls -d "services"/[0-9][0-9]-* | sort); do
# Include bibliotheque de fonction
if [ -f "$dir/misc/nine.sh" ]; then
. $dir/misc/nine.sh
fi
# Include variable d'environnement global
if [ -f "$dir/env/.env" ]; then
. $dir/env/.env
fi
# Include variable d'environnement local
if [ -f "$dir/env/.env.local" ]; then
. $dir/env/.env.local
fi
# Construction du docker-compose
if [ -f "$dir/dockercompose/dockercompose.yml" ]; then
cat "$dir/dockercompose/dockercompose.yml" >> docker-compose.yml
fi
# Construction .env.merge
mergeenv $dir/env/.env.merge env/.env.merge $dir/env/.env $dir/env/.env.local
# Sauvegarder le service
key="${dir##*-}"
value="${dir##*/}"
services["$key"]="$value"
done
# Construction du docker-compose
echo "networks:" >> docker-compose.yml
echo " nine-network:" >> docker-compose.yml
echo " name: nine-network" >> docker-compose.yml
# Start script
BigTitle "NINE"
#===========================================================================================================================================
#== STOP ===================================================================================================================================
#===========================================================================================================================================
#1 = service to stop if null all service
#2 = stopper par destroyall
stop() {
if [[ "$2" != 1 ]]; then Title "STOP"; fi
echo "docker-compose stop $1"
docker-compose stop $1
if [[ "$2" != 1 ]]; then echo; fi
}
#===========================================================================================================================================
#== UPSERVICE ==============================================================================================================================
#===========================================================================================================================================
#1 = service to up
wait_for_container() {
container_id="nine-$1"
container_name="$(docker inspect "${container_id}" --format '{{ .Name }}')"
waiting_done="false"
while [[ "${waiting_done}" != "true" ]]; do
container_state="$(docker inspect "${container_id}" --format '{{ .State.Status }}')"
if [[ "${container_state}" == "running" ]]; then
health_status="$(docker inspect "${container_id}" --format '{{ .State.Health.Status }}')"
if [[ ${health_status} == "healthy" ]]; then
waiting_done="true"
fi
else
waiting_done="true"
fi
sleep 1;
done;
}
upservice(){
docker-compose up -d --remove-orphans $1
if [[ $2 == "wait" ]]
then
wait_for_container $1
fi
}
function upkeycloak {
if [[ $CAS_ACTIVATE == 1 && $CAS_LOCAL == 1 ]]
then
Title ${KEYCLOAK_SERVICE_NAME^^}
# CREATE BDD
if [[ $MARIADB_ACTIVATE == 1 && $MARIADB_LOCAL == 1 ]]
then
EchoVert "DATABASE"
Echo ${KEYCLOAK_SERVICE_NAME^^}
docker-compose exec $MARIADB_SERVICE_NAME /nine/init.sh $KEYCLOAK_SERVICE_NAME
Echo
fi
EchoVert "CONTAINER"
upservice $KEYCLOAK_SERVICE_NAME
Echo
fi
}
function updokuwiki {
if [[ $DOKUWIKI_ACTIVATE == 1 && $DOKUWIKI_LOCAL == 1 ]]
then
Title ${DOKUWIKI_SERVICE_NAME^^}
EchoVert "CONTAINER"
upservice ${DOKUWIKI_SERVICE_NAME}
Echo
fi
}
function upnextcloud {
if [[ $NEXTCLOUD_ACTIVATE == 1 && $NEXTCLOUD_LOCAL == 1 ]]
then
Title ${NEXTCLOUD_SERVICE_NAME^^}
if [[ $MARIADB_ACTIVATE == 1 && $MARIADB_LOCAL == 1 ]]
then
EchoVert "DATABASE"
Echo ${NEXTCLOUD_SERVICE_NAME^^}
Echo
docker-compose exec $MARIADB_SERVICE_NAME /nine/init.sh $NEXTCLOUD_SERVICE_NAME
fi
EchoVert "CONTAINER"
upservice ${NEXTCLOUD_SERVICE_NAME}
Echo
fi
}
function upnineboard {
if [[ $NINEBOARD_ACTIVATE == 1 && $NINEBOARD_LOCAL == 1 ]]
then
Title ${NINEBOARD_SERVICE_NAME^^}
if [[ $MARIADB_ACTIVATE == 1 && $MARIADB_LOCAL == 1 ]]
then
EchoVert "DATABASE"
Echo ${NINEBOARD_SERVICE_NAME^^}
docker-compose exec $MARIADB_SERVICE_NAME /nine/init.sh $NINEBOARD_SERVICE_NAME
Echo
fi
EchoVert "CONTAINER"
upservice ${NINEBOARD_SERVICE_NAME}
chmod -R a+wr ./volume/nineboard/data
Echo
fi
}
#===========================================================================================================================================
#== UP =====================================================================================================================================
#===========================================================================================================================================
#1 = service to up if null all service
up(){
# Stop du ou des services
stop $1
# SERVICES
if [[ -z "$1" ]]
then
# Pour chaque répertoire service on execute sa fonction up
for key in $(for k in "${!services[@]}"; do echo "$k:${services[$k]}"; done | sort -t: -k2,2 | cut -d: -f1); do
up${key}
done
else
up${1}
fi
}
#===========================================================================================================================================
#== DESTROY ================================================================================================================================
#===========================================================================================================================================
destroyall(){
Question_ouinon "Souhaitez-vous supprimer l'ensemble des containers : Attention cela supprimera vraiment tout ?"
if [[ "$?" = 0 ]]
then
# Pour chaque répertoire service on execute sa fonction destroy
# On part du dernier service vers le premiser
for key in $(for k in "${!services[@]}"; do echo "$k:${services[$k]}"; done | sort -t: -k2,2r | cut -d: -f1); do
destroy${key} 1
done
fi
}
#===========================================================================================================================================
#== DESTROY ================================================================================================================================
#===========================================================================================================================================
#1 = service to destroy
#2 = ne pas poser de questions
#destroy(){
# Title "DESTROY "${1^^}
#
# stop $1 1
# docker-compose rm -s -v -f "$1"
#
#
# if [[ "$1" == "$OPENLDAP_SERVICE_NAME" && $LDAP_ACTIVATE == 1 && $LDAP_LOCAL == 1 ]]
# then
# if [[ -z $2 ]]; then Question_ouinon "Souhaitez-vous supprimer l'annuaire associé à $1 ?";fi
# if [[ "$?" = 0 || -z $2 ]]
# then
# EchoRouge Suppression
# sudo rm -rf volume/openldap/data
# fi
# fi
#
# if [[ "$1" == "$KEYCLOAK_SERVICE_NAME" && $CAS_ACTIVATE == 1 && $CAS_LOCAL == 1 ]]
# then
# if [[ -z $2 ]]; then Question_ouinon "Souhaitez-vous supprimer la BDD associé à $1 ?";fi
# if [[ "$?" = 0 || -z $2 ]]
# then
# docker-compose exec $MARIADB_SERVICE_NAME /nine/delete.sh $KEYCLOAK_SERVICE_NAME
# fi
# fi
#
# if [[ "$1" == "$DOKUWIKI_SERVICE_NAME" && $DOKUWIKI_ACTIVATE == 1 && $DOKUWIKI_LOCAL == 1 ]]
# then
# if [[ -z $2 ]]; then Question_ouinon "Souhaitez-vous supprimer la BDD associé à $1 ?";fi
# if [[ "$?" = 0 || -z $2 ]]
# then
# docker-compose exec $MARIADB_SERVICE_NAME /nine/delete.sh $DOKUWIKI_SERVICE_NAME
# sudo rm -rf volume/dokuwiki/data
# fi
# fi
#
# if [[ "$1" == "$NEXTCLOUD_SERVICE_NAME" && $NEXTCLOUD_ACTIVATE == 1 && $NEXTCLOUD_LOCAL == 1 ]]
# then
# if [[ -z $2 ]]; then Question_ouinon "Souhaitez-vous supprimer la BDD associé à $1 ainsi que les fichiers utilisateurs ?";fi
# if [[ "$?" = 0 || -z $2 ]]
# then
# docker-compose exec $MARIADB_SERVICE_NAME /nine/delete.sh $NEXTCLOUD_SERVICE_NAME
# sudo rm -rf volume/nextcloud/data
# sudo rm -rf volume/nextcloud/html
# sudo rm -rf volume/nextcloud/app
# fi
# fi
#
# if [[ "$1" == "$NINEBOARD_SERVICE_NAME" && $NINEBOARD_ACTIVATE == 1 && $NINEBOARD_LOCAL == 1 ]]
# then
# if [[ -z $2 ]]; then Question_ouinon "Souhaitez-vous supprimer la BDD associé à $1 ?";fi
# if [[ "$?" = 0 || -z $2 ]]
# then
# docker-compose exec $MARIADB_SERVICE_NAME /nine/delete.sh $NINEBOARD_SERVICE_NAME
# fi
# fi
#
# if [[ "$1" == "$NINESKELETOR_SERVICE_NAME" && $NINESKELETOR_ACTIVATE == 1 && $NINESKELETOR_LOCAL == 1 ]]
# then
# if [[ -z $2 ]]; then Question_ouinon "Souhaitez-vous supprimer la BDD associé à $1 ?";fi
# if [[ "$?" = 0 || -z $2 ]]
# then
# docker-compose exec $MARIADB_SERVICE_NAME /nine/delete.sh $NINESKELETOR_SERVICE_NAME
# fi
# fi
#
# if [[ "$1" == "$WORDPRESS_SERVICE_NAME" && $WORDPRESS_ACTIVATE == 1 && $WORDPRESS_LOCAL == 1 ]]
# then
# if [[ -z $2 ]]; then Question_ouinon "Souhaitez-vous supprimer la BDD associé à $1 ?";fi
# if [[ "$?" = 0 || -z $2 ]]
# then
# docker-compose exec $MARIADB_SERVICE_NAME /nine/delete.sh $WORDPRESS_SERVICE_NAME
# sudo rm -rf volume/wordpress/data
# fi
# fi
#}
#
#===========================================================================================================================================
#== MAIN ===================================================================================================================================
#===========================================================================================================================================
if [[ $1 == "up" || $1 == "" ]]
then
up $2
elif [[ $1 == "uplogs" || $1 == "" ]]
then
up $2
docker-compose logs -f $2
elif [[ $1 == "stop" ]]
then
stop $2
elif [[ $1 == "bash" ]]
then
if [[ -z $2 ]]
then
EchoRouge "Vous devez precisez un service"
EchoRouge "nine.sh bash monservice"
else
docker-compose exec $2 "/bin/bash"
fi
elif [[ $1 == "destroyall" ]]
then
destroyall
elif [[ $1 == "destroy" ]]
then
if [[ -z $2 ]]
then
EchoRouge "Vous devez precisez un service"
EchoRouge "nine.sh destroy monservice"
else
destroy$2
fi
elif [[ $1 == "logs" ]]
then
docker-compose logs -f $2
elif [[ $1 == "iswait" ]]
then
wait_for_container $2
elif [[ $1 == "regen" ]]
then
if [[ -z $2 ]]
then
destroyall
up
else
destroy$2
up$2
fi
else
EchoRouge "Action possible ="
EchoRouge "nine.sh > UP de l'ensemble des services actifs"
EchoRouge "nine.sh up > UP de l'ensemble des services actifs"
EchoRouge "nine.sh up monservice> UP de monservice"
EchoRouge "nine.sh uplogs > UP puis logs de l'ensemble des services actifs"
EchoRouge "nine.sh uplogs monservice> UP puis logs de monservice"
EchoRouge "nine.sh stop > STOP de l'ensemble des services"
EchoRouge "nine.sh stop monservice> STOP de monservice"
EchoRouge "nine.sh bash monservice> lance un terminel dans le conteneur de monservice"
EchoRouge "nine.sh destroyall> détruit l'ensemble des services avec l'ensemble des BDD et des volumes persistant"
EchoRouge "nine.sh destroy monservice> détruit monservices et si souhaitez sa BDD et ses volumes persistant"
EchoRouge "nine.sh logs > LOGS de l'ensemble des services"
EchoRouge "nine.sh logs monservice > LOGS de monservice"
EchoRouge "nine.sh iswait monservice > monservice est-il en cours de construction"
EchoRouge "nine.sh regen > lance destroyall puis up sur l'ensemble des service"
EchoRouge "nine.sh regen monservice > lance destroy monservice puis up monservice"
fi
echo
echo