2026-02-09 11:29:19 +01:00

264 lines
6.9 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
############################################################
# Decription: gestion docker compose
# Author: Gilles Mouchet (gilles.mouchet@gmail.com)
# Creation Date: 08.02.2026
# Version actuelle: 1.0.0
#
# Changelog:
# V1.0.0 - 08.02.2026 - GMo
# Ajouté
# - Création du script
#
############################################################
#Constantes
SCRIPT_NAME=$(basename "$0")
UPDATE_DATE=08.02.2026
VERSION=1.0.0
WP_ROOT_FOLDER=/home/docker/wp
WP_SITE_FOLDER=$WP_ROOT_FOLDER/wp-site
WP_DB_FOLDER=$WP_ROOT_FOLDER/wp-db
RED='\e[1;31m'
GREEN='\e[0;32m'
ORANGE='\e[0;33m'
YELLOW='\e[1;33m'
BLUE='\e[0;34m'
PURPLE='\e[1;35m'
CYAN='\e[1;36m'
NC='\e[0m'
# Define functions to display usage information
version() {
cat<<EOF
${SCRIPT_NAME} - Version: $VERSION ($UPDATE_DATE) - Gilles Mouchet (gilles.mouchet@gmail.com)
Non-Commercial Use License See LICENSE for details
EOF
exit 1
}
# Define functions to display usage information
usage() {
cat << EOF
Manage docker compose
${SCRIPT_NAME} - Version: $VERSION ($UPDATE_DATE) - Gilles Mouchet (gilles.mouchet@gmail.com)
Usage: $SCRIPT_NAME <options>
Options:
--bash open shell root in container wp
--console start docker compose as console
--delete stop and delete container and delete files in ${WP_ROOT_FOLDER}
--down stop docker compose and delete container
--env <env_name> environment name (MANDATORY)
--stop stop docker compose
--start start docker compose as daemon
--version version
--help this help
EOF
exit 1
}
inst-wpcli() {
# install wp-cli.phar
if [ ! -f "$WP_SITE_FOLDER/$WP_CLI" ]; then
echo "Install wp-cli.phar."
sudo curl -s -o $WP_SITE_FOLDER/$WP_CLI $WP_CLI_URL
sudo chmod +x $WP_SITE_FOLDER/$WP_CLI
fi
}
check-container-exist(){
if [ ! "$(docker ps -aq -f name=^/${WP_SITE}$)" ]; then
echo -e "\n${RED}Container '$WP_SITE' do not exist!${NC}"
echo -e "Use commande './${SCRIPT_NAME} --build'\n"
exit 1
fi
if [ ! "$(docker ps -aq -f name=^/${WP_DB}$)" ]; then
echo -e "\n${RED}Container '$WP_DB' do not exist!${NC}"
echo -e "Use commande './${SCRIPT_NAME} --build'\n"
exit 1
fi
}
check-container-up(){
# check if stack is up ()
if [ ! "$(docker inspect -f '{{.State.Status}}' "$WP_SITE" 2>/dev/null)" = "running" ]; then
echo -e "\n${RED}$WP_SITE not started${NC}"
echo -e "Execute './${SCRIPT_NAME} --up' to start containers\n"
exit
fi
if [ ! "$(docker inspect -f '{{.State.Status}}' "$WP_DB" 2>/dev/null)" = "running" ]; then
echo -e "${RED}$WP_DB not started${NC}"
echo "Execute './${SCRIPT_NAME} --up' to start containers"
exit
fi
}
#-----------------------------------------------------------
# MAIN
#-----------------------------------------------------------
# create destinations folders
if [ ! -d "$WP_ROOT_FOLDER" ]; then
#echo "create $WP_ROOT_FOLDER"
sudo mkdir -p $WP_ROOT_FOLDER
fi
# check param exist
if [ -z "$1" ]; then
usage
exit
fi
# analysis of arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--help)
usage
;;
--version)
version
;;
--env)
if [[ -n "$2" && "$2" != -* ]]; then
ENV="$2"
shift 2
else
echo -e "\n${RED}Error: --env requires an argument.${NC}"
usage
fi
;;
--bash)
MODE="bash"
shift
;;
--build)
MODE="build"
shift
;;
--console)
MODE="console"
shift
;;
--down)
MODE="down"
shift
;;
--delete)
MODE="delete"
shift
;;
--stop)
MODE="stop"
shift
;;
--start)
MODE="start"
shift
;;
*)
echo -e "\n${RED}Error: Unknown option !${NC}: $1"
usage
;;
esac
done
# if we have an ENV but no mode (--bash, --console, ...)
if [[ -n "$ENV" && -z "$MODE" ]]; then
echo -e "\n${RED}\nError: Missing options${NC}"
usage
fi
# if we have a mode but no ENV (since --env is mandatory outside of help/version)
if [[ -n "$MODE" && -z "$ENV" ]]; then
echo -e "\n${RED}Error: The --env option is required to use --$MODE.${NC}"
usage
fi
# if nothing is provided
if [[ -z "$ENV" && -z "$MODE" ]]; then
usage
fi
echo -e "${GREEN}------------------------"
echo "Environment: $ENV"
echo -e "------------------------${NC}"
# check if env file exist
# copy file env
if [ -f "env-$ENV" ]; then
cp env-$ENV .env
. .env
sed -e "s/_FQDN_/$HOST_NAME/g" \
-e "s/_SITE_TITLE_/$SITE_TITLE/g" < db-tmpl.sql > dump-db/wp-db.sql
else
echo -e "\n${RED}Error: file env-$ENV do not exist${NC}\n"
exit 1
fi
case "$MODE" in
bash)
check-container-exist
check-container-up
docker exec --user root -it wp-site /bin/bash
;;
build)
docker compose up -d
echo "Waiting for status 'healthy'... of container $WP_DB"
until [ "$(docker inspect -f '{{.State.Health.Status}}' $WP_DB)" == "healthy" ]; do
echo -n "."
sleep 2
done
echo " "
inst-wpcli
docker exec "$WP_SITE" ./wp-cli.phar plugin install gantry5 --activate --allow-root
docker exec "$WP_SITE" ./wp-cli.phar theme install $GANTRY_THEME_HYDROGEN_URL --activate --allow-root
sudo chown 33:33 $WP_SITE_FOLDER -R
docker compose stop
;;
console)
check-container-exist
docker compose up
#sudo chown 33:33 $WP_SITE_FOLDER -R
;;
delete)
echo -e "${RED}-----------------------------------------------------------------------------------------------------"
echo -e "Do you really want to delete the contents of the /home/docker/${root_app} folder [y/N]?"
echo -e "----------------------------------------------------------------------------------------------------- ${NC}"
unset answer
read answer
if [ "${answer}" != "y" ]; then
echo -e "${ORANGE}Operation cancelled!${NC}"
exit 1
fi
docker compose down
#sleep 5
sudo rm -rf $WP_ROOT_FOLDER
exit
;;
down)
docker compose down
exit
;;
stop)
docker compose stop
exit
;;
start)
check-container-exist
docker compose up -d
echo "Waiting for status 'healthy'... of container $WP_DB"
until [ "$(docker inspect -f '{{.State.Health.Status}}' $WP_DB)" == "healthy" ]; do
echo -n "."
sleep 2
done
echo " "
;;
esac