Gilles Mouchet bf62784b5c v1.0.0
2026-03-09 13:50:22 +01:00

198 lines
5.7 KiB
Bash
Executable File
Raw Permalink 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
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
--build create files for persistent datas and containers
--console start docker compose as console
--delete stop and delete container and files in ${WP_ROOT_FOLDER}
--down stop docker compose and delete container
--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
#-----------------------------------------------------------
# read env file
if [ -f ".env" ]; then
. .env
else
echo -e "\n${RED}[ERROR]: file .env do not exist${NC}\n"
exit 1
fi
# check param exist
if [ -z "$1" ]; then
usage
exit
fi
# analysis of arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--bash)
check-container-exist
check-container-up
docker exec --user root -it ${WP_SITE} /bin/bash
shift
;;
--build)
# set var on DB
sed -e "s/_FQDN_/$HOST_NAME/g" \
-e "s/_SITE_TITLE_/$SITE_TITLE/g" < db-tmpl.sql > dump-db/wp-db.sql
# create destinations folders
if [ ! -d "$WP_ROOT_FOLDER" ]; then
sudo mkdir -p $WP_ROOT_FOLDER
fi
# start container
docker compose up -d
# install wp client line interface
inst-wpcli
# install gantry5 plugin
docker exec "$WP_SITE" ./wp-cli.phar plugin install gantry5 --activate --allow-root
# install helium theme
docker exec "$WP_SITE" ./wp-cli.phar theme install $GANTRY_THEME_HELIUM_URL --activate --allow-root
# set right
sudo chown 33:33 $WP_SITE_FOLDER -R
# stop containers
docker compose stop
shift
;;
--console)
check-container-exist
if [ "$(docker inspect -f '{{.State.Status}}' "$WP_SITE" 2>/dev/null)" = "running" ]; then
echo -e "\n${RED}$WP_SITE is started${NC}"
echo -e "Execute './${SCRIPT_NAME} --stop' to sto containers\n"
exit
fi
docker compose up
shift
;;
--down)
doicker compose down
shift
;;
--delete)
echo -e "${RED}-----------------------------------------------------------------------------------------------------"
echo -e "Do you really want to delete the contents of the ${WP_ROOT_FOLDER} 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
sudo rm -rf /root/.wp-cli
shift
;;
--stop)
docker compose stop
shift
;;
--start)
check-container-exist
docker compose up -d
shift
;;
--help)
usage
;;
--version)
version
;;
*)
echo -e "\n${RED}Error: Unknown option !${NC}: $1"
usage
;;
esac
done