File: /home/frenchy/www/french-american.org/releases/20210421135930Z/docker-service
#!/bin/bash
set -o nounset
set -o errexit
OPTIND=1
env=''
BLEU="\033[36m"
JAUNE="\033[33m"
VERT="\033[32m"
NORMAL="\033[0m"
# Usage info
show_help() {
cat << EOF
Usage: ${0##*/} [COMMAND]
Commands:
initialize
build Build docker images
start Start docker containers
stop Stop docker containers
watch Watch and compile js & css file
down Remove docker containers
install Run app installation scripts
populate Load data fixtures in app
update_registry_containers Build custom images and push to private registry
pull Pull all images to get newers
gitHooks Install pre-commits
scssLint Execute scssLint
esLint Execute esLint
styleguide-start Start Fractal Styleguide for dev
styleguide-stop Stop Fractal Styleguide
styleguide-restart Stop & Start Fractal Styleguide for dev
styleguide-build Build Fractal Styleguide
EOF
}
# Get cli options
while getopts "he:" opt; do
case $opt in
h)
show_help
exit 0
;;
e)
env=".$OPTARG"
;;
*)
show_help >&2
exit 1
;;
esac
done
# Shift off the options and optional --.
shift "$((OPTIND-1))"
if [ "$env" == '' ]; then
[ ! -f .env ] && cp .env.dist .env;
else
[ ! -f .env ] && cp .env"$env" .env;
fi
source .env
initialize() {
echo -e "\n👉"$BLEU" [Docker] Initialize - Lancement des conteneurs"$NORMAL
down
if [ "$env" == '.staging' ]; then
docker deploy -c docker-compose"$env".yml $COMPOSE_PROJECT_NAME --with-registry-auth
until [ `docker service ls|grep $COMPOSE_PROJECT_NAME|grep -v "1/1"|wc -l` -lt 1 ]; do sleep 4; echo "Waiting for stack to come up..."; done;
else
pull
start
fi
install
if [ "$env" == '' ]; then
confirmUploads && rm -rf wp-content/uploads || true;
confirm && populate
gitHooks
styleguide-start
else
populate
fi
}
start() {
docker-compose -f docker-compose"$env".yml up -d --remove-orphans
sleep 1
docker-compose -f docker-compose"$env".yml ps
}
install() {
echo -e "\n👉"$BLEU" [Docker Install]"$NORMAL
echo -e "\n👉"$BLEU" [Docker Install - Étape 1/3] Installation des dépendances Composer"$NORMAL
# docker-compose -f docker-compose"$env".yml run --rm -T --user=root --no-deps --entrypoint="/bin/bash -c" front "cd /var/www &"
docker-compose -f docker-compose"$env".yml run --rm -T --user=www-data --no-deps --entrypoint="/bin/bash -c" php-fpm "composer install"
docker-compose -f docker-compose"$env".yml run --rm -T --user=root --no-deps --entrypoint="/bin/bash -c" front "chown 33:33 /var/www -R"
echo -e "\n👉"$BLEU" [Docker Install Étape 2/3] Installation des dépendances NPM"$NORMAL
docker-compose -f docker-compose"$env".yml run --rm -T --user=www-data --no-deps --entrypoint="/bin/bash -c" front "npm install"
echo -e "\n👉"$BLEU" [Docker Install Étape 3/3] Compilation Gulp"$NORMAL
docker-compose -f docker-compose"$env".yml run --rm -T --user=www-data --no-deps --entrypoint="/bin/bash -c" front "node_modules/gulp/bin/gulp.js"
# Téléchargement du language FR et activation
# echo -e "\n👉"$BLEU" [Étape 4/4] Passage de WordPress en français - Téléchargement et installation"$NORMAL
# docker-compose -f docker-compose"$env".yml run --rm -T --user=www-data --no-deps --entrypoint="/bin/bash -c" php-fpm "wp language core install fr_FR && wp site switch-language fr_FR"
echo ""
echo -e $VERT"✔︎ Docker install ➔ OK$NORMAL"
}
confirm() {
# call with a prompt string or use a default
echo ""
read -r -p "${1:-Voulez-vous remplir la base de données (docker-service populate) ? [y/N]} " response
echo ""
case "$response" in
[yY][eE][sS]|[yY])
true
;;
*)
false
;;
esac
}
confirmUploads() {
# call with a prompt string or use a default
echo ""
read -r -p "${1:-Voulez-vous supprimer les uploads existants ? [y/N]} " response
echo ""
case "$response" in
[yY][eE][sS]|[yY])
true
;;
*)
false
;;
esac
}
populate() {
# Suppression de la base existante
echo -e "\n👉"$BLEU" [Docker Populate]"$NORMAL
echo ""
echo -e $BLEU"[Docker Populate 1/5] Suppression de la base existante"$NORMAL
docker-compose -f docker-compose"$env".yml run --rm -T --user=www-data --no-deps --entrypoint="/bin/bash -c" php-fpm "wp db drop --path='/var/www/vhost/public/wp-core' --yes"
docker-compose -f docker-compose"$env".yml run --rm -T --user=www-data --no-deps --entrypoint="/bin/bash -c" php-fpm "wp config shuffle-salts --path='/var/www/vhost/public/wp-core' --yes"
echo ""
# Création de la base de données
echo -e $BLEU"[Docker Populate 2/5] Création de la base de données"$NORMAL
docker-compose -f docker-compose"$env".yml run --rm -T --user=www-data --no-deps --entrypoint="/bin/bash -c" php-fpm "wp db create --path='/var/www/vhost/public/wp-core'"
echo ""
# Création des tables
echo -e $BLEU"[Docker Populate 3/5] Création des tables"$NORMAL
# docker-compose -f docker-compose"$env".yml run --rm -T --user=www-data --no-deps --entrypoint="/bin/bash -c" php-fpm "wp core install --path='/var/www/vhost/public/wp-core' --url= --title=wpstart --admin_user=biig --admin_password=biig --admin_email=www@biig.fr"
docker-compose -f docker-compose"$env".yml run --rm -T --user=www-data --no-deps --entrypoint="/bin/bash -c" php-fpm "wp core install --path='/var/www/vhost/public/wp-core' --url='' --title='frenchamericanfr' --admin_user=biig --admin_password=biig --admin_email=www@biig.fr"
echo ""
echo -e $BLEU"[Docker Populate 4/5] Passage de WordPress en français - Téléchargement et installation"$NORMAL
# Téléchargement du language FR et activation
docker-compose -f docker-compose"$env".yml run --rm -T --user=www-data --no-deps --entrypoint="/bin/bash -c" php-fpm "wp language core install fr_FR --path='/var/www/vhost/public/wp-core' && wp site switch-language fr_FR --path='/var/www/vhost/public/wp-core'"
echo ""
echo -e $BLEU"[Docker Populate 5/5] Activation des plugins"$NORMAL
# Téléchargement du language FR et activation
docker-compose -f docker-compose"$env".yml run --rm -T --user=www-data --no-deps --entrypoint="/bin/bash -c" php-fpm "wp plugin activate --all --path='/var/www/vhost/public/wp-core'"
echo ""
echo -e $VERT"✔︎ Docker populate ➔ OK$NORMAL"
# echo -e $BLEU"🍺🍺🍺 Le projet est lancé"$NORMAL
echo -e $VERT"Votre site est disponible : "$NORMAL
echo '- Front : http://'$APP_HOSTNAME
echo '- Back : http://'$APP_HOSTNAME'/wp-core/wp-admin/'
}
watch() {
echo -e "\n👉"$BLEU" [Docker Watch]"$NORMAL
docker-compose -f docker-compose"$env".yml run --rm -T --user=www-data --no-deps --entrypoint="/bin/bash -c" front "./node_modules/gulp/bin/gulp.js watches"
}
stop() {
docker-compose -f docker-compose"$env".yml stop
}
down() {
docker-compose -f docker-compose"$env".yml down
}
styleguide-start() {
echo -e $BLEU"Styleguide is starting (please wait...)"$NORMAL
docker-compose exec -T -d -u www-data front bash -c "./node_modules/@frctl/fractal/bin/fractal start --sync --port "$STYLEGUIDE_PORT
sleep 5
echo -e $VERT"✔︎ Styleguide is started : http://localhost:$STYLEGUIDE_PORT"
}
styleguide-stop() {
docker-compose exec -T -d front bash -c "ps aux | grep -ie fractal | awk '{print \$2}' | xargs kill -9"
echo -e $VERT"✔︎ Styleguide is down"
}
styleguide-restart() {
styleguide-stop
styleguide-start
}
styleguide-build() {
echo -e $BLEU"Styleguide is building (please wait...)"$NORMAL
docker-compose exec -T -d -u www-data front bash -c "./node_modules/@frctl/fractal/bin/fractal build"
sleep 5
echo -e $VERT"✔︎ Styleguide is build : : http://$APP_HOSTNAME/styleguide"
}
gitHooks() {
echo "add git hooks"
cp -R .git-hooks/hooks/ .git/hooks
chmod -R 744 .git/hooks
git config core.hooksPath .git/hooks
}
scssLint() {
docker-compose exec -T front bash -c "npm run scsslint"
}
esLint() {
docker-compose exec -T front bash -c "npm run eslint"
}
unitTests() {
echo "Nothing to test";
}
functionalTests() {
echo "Nothing to test";
}
pull() {
docker pull gitlab.biig.fr:4567/french-american-foundation/frenchamerican-fr/php-fpm:latest
docker pull gitlab.biig.fr:4567/french-american-foundation/frenchamerican-fr/front:latest
}
update_registry_containers() {
docker build -t gitlab.biig.fr:4567/french-american-foundation/frenchamerican-fr/front:latest docker/nginx
docker push gitlab.biig.fr:4567/french-american-foundation/frenchamerican-fr/front:latest
docker build -t gitlab.biig.fr:4567/french-american-foundation/frenchamerican-fr/php-fpm:latest docker/php7-fpm
docker push gitlab.biig.fr:4567/french-american-foundation/frenchamerican-fr/php-fpm:latest
docker build -t gitlab.biig.fr:4567/french-american-foundation/frenchamerican-fr/mariadb:latest docker/mariadb
docker push gitlab.biig.fr:4567/french-american-foundation/frenchamerican-fr/mariadb:latest
}
MEPPreprod() {
if [ "$#" -ne 1 ]; then
exit
else
if [ "$1" == "deploy" ] || [ "$1" == "rollback" ] ; then
type="$1"
else
exit
fi
fi
echo "Deploying branch: Develop thanks to ansistrano :)"
docker run -t --rm \
-v $PWD:/data \
-e CI_JOB_TOKEN=${CI_JOB_TOKEN} \
gitlab.biig.fr:4567/docker/ansistrano:with_git /bin/sh -c \
"cd /data && git clone https://gitlab-ci-token:$CI_JOB_TOKEN@gitlab.biig.fr/si/deploiement/frenchamerican-fr-deploy-key.git || true && \
cp /data/frenchamerican-fr-deploy-key/id_rsa_frenchamerican-fr /root/.ssh/id_rsa && chmod 600 /root/.ssh/id_rsa &&\
ansible-playbook -i /data/docker/ansistrano/preprod/_hosts \
/data/docker/ansistrano/preprod/"$type".yml"
}
MEPProd() {
if [ "$#" -ne 1 ]; then
exit
else
if [ "$1" == "deploy" ] || [ "$1" == "rollback" ] ; then
type="$1"
else
exit
fi
fi
echo "Deploying branch: Master thanks to ansistrano :)"
docker run -t --rm \
-v $PWD:/data \
-e CI_JOB_TOKEN=${CI_JOB_TOKEN} \
gitlab.biig.fr:4567/docker/ansistrano:with_git /bin/sh -c \
"cd /data && git clone https://gitlab-ci-token:$CI_JOB_TOKEN@gitlab.biig.fr/si/deploiement/frenchamerican-fr-deploy-key.git || true && \
cp /data/frenchamerican-fr-deploy-key/id_rsa_frenchamerican-fr /root/.ssh/id_rsa && chmod 600 /root/.ssh/id_rsa &&\
ansible-playbook -i /data/docker/ansistrano/prod/_hosts \
/data/docker/ansistrano/prod/"$type".yml"
}
# Show help if no argument was supplied
if [ $# -eq 0 ]
then
show_help >&2
exit 1
fi
case "$1" in
initEnv)
initEnv
;;
initialize)
initialize
;;
start)
start
;;
stop)
stop
;;
watch)
watch
;;
down)
down
;;
restart)
stop
start
;;
install)
install
;;
populate)
populate
;;
gitHooks)
gitHooks
;;
scssLint)
scssLint
;;
esLint)
esLint
;;
styleguide-start)
styleguide-start
;;
styleguide-stop)
styleguide-stop
;;
styleguide-restart)
styleguide-restart
;;
styleguide-build)
styleguide-build
;;
test)
unitTests
functionalTests
;;
unitTests)
unitTests
;;
functionalTests)
functionalTests
;;
update_registry_containers)
update_registry_containers
;;
pull)
pull
;;
MEPPreprod)
if [ "$#" -ne 2 ]; then
MEPPreprod
else
MEPPreprod $2
fi
;;
MEPProd)
if [ "$#" -ne 2 ]; then
MEPProd
else
MEPProd $2
fi
;;
*)
show_help >&2
exit 1
esac
exit 0