-
Jonathan Foucher authoredJonathan Foucher authored
docker-service 10.17 KiB
#!/bin/bash
set -o nounset
set -o errexit
# Reset in case getopts has been used previously in the shell.
OPTIND=1
# Initialize variables
env=''
# 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
# Common operations
initialize() {
down
pullImages
start
install
populate
jwtKeys
}
localinit() {
build
start
install
populate
jwtKeys
}
build() {
docker build docker/nginx -t nginx
docker build docker/php -t php
docker-compose -f docker-compose"$env".yml build
}
start() {
docker-compose -f docker-compose"$env".yml up -d --remove-orphans
sleep 1
docker-compose -f docker-compose"$env".yml ps
}
jwtKeys() {
if [ ! -f config/jwt/private.pem ]; then
echo "JWT keys do not exist, generate them"
docker-compose -f docker-compose"$env".yml exec php bin/console lexik:jwt:generate-keypair
else
echo "JWT keys already exist, skip"
fi
}
install() {
docker-compose -f docker-compose"$env".yml run -T --rm --user=www-data --no-deps --entrypoint="/bin/bash -c" php 'until nc -z db 3306; do sleep 1; echo "Waiting for DB to come up..."; done'
docker-compose -f docker-compose"$env".yml run -T --rm --user=www-data --no-deps --entrypoint="/bin/bash -c" php "php -d memory_limit=-1 /usr/local/bin/composer install --prefer-dist --no-interaction --optimize-autoloader &&
bin/console doctrine:database:create --if-not-exists &&
bin/console doctrine:migration:migrate -n &&
php -d memory_limit=-1 bin/console cache:clear --env=prod --no-debug &&
php -d memory_limit=-1 bin/console cache:warmup --env=prod --no-debug"
# Load CSS & JS compilation
docker-compose -f docker-compose"$env".yml run -T --rm --user=www-data --no-deps --entrypoint="/bin/bash -c" php "npm install && npm run build"
}
installDb() {
docker-compose -f docker-compose"$env".yml run -T --rm --user=www-data --no-deps --entrypoint="/bin/bash -c" php "bin/console doctrine:database:drop --force &&
bin/console doctrine:database:create &&
bin/console doctrine:migration:migrate -n"
}
populate() {
docker-compose -f docker-compose"$env".yml run -T --rm --user=www-data --no-deps --entrypoint="/bin/bash -c" php "php -d memory_limit=-1 bin/console hautelook:fixtures:load -n"
}
serve() {
if [ "$#" -ge 1 ]; then
docker-compose -f docker-compose"$env".yml run -T --rm --user=www-data --no-deps --entrypoint="/bin/bash -c" php "npm && npm run dev --$1"
fi
docker-compose -f docker-compose"$env".yml run -T --rm --user=www-data --no-deps --entrypoint="/bin/bash -c" php "npm && npm run dev"
}
stop() {
docker-compose -f docker-compose"$env".yml stop
}
down() {
docker-compose -f docker-compose"$env".yml down --remove-orphans
}
csFixer() {
paths=""
config=".php_cs.dist"
if [ "$#" -ge 1 ]; then
paths="$1"
docker run --rm --user www-data --env-file=.env.test -v ${PWD}/:/var/www/gestion-rgpd/ -w /var/www/gestion-rgpd/ $DOCKER_IMAGE_PHP bash -c "php -l $paths"
if [ "$#" -eq 2 ]; then
config="$2"
fi
fi
docker run --rm --user www-data --env-file=.env.test -v ${PWD}/:/var/www/gestion-rgpd/ -w /var/www/gestion-rgpd/ $DOCKER_IMAGE_PHP bash -c "vendor/bin/php-cs-fixer fix --config=$config $paths -vv"
}
phpStan() {
docker run --rm --user www-data --env-file=.env.test -v ${PWD}/:/var/www/gestion-rgpd/ -w /var/www/gestion-rgpd/ $DOCKER_IMAGE_PHP bash -c "vendor/bin/phpstan analyse --error-format=raw -c phpstan.neon -l2 src"
}
# Tests
tests() {
unitTests
qualityTests
functionalTests
}
unitTests() {
echo -e "\n\n\nUnit Tests"
make tu
}
security() {
echo -e "\n\n\nSecurity check"
make security
}
qualityTests() {
docker-compose -f docker-compose.test.yml up -d php
# Code Style
echo -e "\n\n\nCs Fixer :"
docker-compose -f docker-compose.test.yml exec -T --user www-data php vendor/bin/php-cs-fixer fix --config=.php_cs.dist --dry-run --diff -vv
# Lint PHP files
docker-compose -f docker-compose.test.yml exec -T --user www-data php php -l src/
docker-compose -f docker-compose.test.yml exec -T --user www-data php php -l features/
# Lint YAML files
docker-compose -f docker-compose.test.yml exec -T --user www-data php bin/console lint:yaml --env=prod config/
docker-compose -f docker-compose.test.yml exec -T --user www-data php bin/console lint:yaml --env=prod src/
# Lint Twig files
#docker-compose -f docker-compose.test.yml exec -T --user www-data php bin/console lint:twig --env=prod app/Resources/views/
# Bundles security check
docker-compose -f docker-compose.test.yml exec -T --user www-data php bin/console security:check
docker-compose -f docker-compose.test.yml down
}
functionalTests() {
if [ "$#" -ne 1 ]; then
format='progress'
paths=''
else
format='pretty'
paths="$1"
fi
docker-compose -f docker-compose.test.yml up -d benevoles-back
docker-compose -f docker-compose.test.yml ps
docker-compose -f docker-compose.test.yml exec -T --user www-data php bash -c 'until nc -z db 3306; do sleep 1; echo "Waiting for DB to come up..."; done'
docker-compose -f docker-compose.test.yml exec -T --user www-data php bash -c "bin/console doctrine:database:create --if-not-exists"
docker-compose -f docker-compose.test.yml exec -T --user www-data php bash -c "bin/console doctrine:schema:update --force"
docker-compose -f docker-compose.test.yml exec -T --user www-data php bash -c "vendor/bin/behat $paths -v --format=$format"
docker-compose -f docker-compose.test.yml down
}
debug(){
docker-compose -f docker-compose"$env".yml run -T --user=www-data --no-deps --entrypoint="/bin/bash -c" -e XDEBUG_CONFIG="idekey=PHPSTORM" -e PHP_IDE_CONFIG="serverName=localhost" php "php -dxdebug.remote_host=docker.for.mac.host.internal $@"
}
# Images management
pushImages() {
docker login --username gitlab-ci-token --password $CI_JOB_TOKEN $CI_REGISTRY
pushImagePhp
pushImageNginx
}
pushImagePhp() {
docker pull $DOCKER_IMAGE_PHP || true
docker build --cache-from $DOCKER_IMAGE_PHP -t $DOCKER_IMAGE_PHP docker/php
docker push $DOCKER_IMAGE_PHP
}
pushImageNginx() {
docker pull $DOCKER_IMAGE_NGINX || true
docker build --cache-from $DOCKER_IMAGE_NGINX -t $DOCKER_IMAGE_NGINX docker/nginx
docker push $DOCKER_IMAGE_NGINX
}
pullImages() {
docker pull $DOCKER_IMAGE_PHP
docker pull $DOCKER_IMAGE_NGINX
}
# Usage info
show_help() {
cat << EOF
Usage: ${0##*/} [-e] [COMMAND]
Options:
-e string Specify env ("test"|"dev") (default "dev")
Commands:
initialize Start the project no matter what state it is in
localinit Build the containers and start
build Build docker containers
start Start docker containers
install Run app installation scripts
jwtKeys Generate JWT keys
installDb Reset database
populate Load data fixtures in app
serve Serve Webpack Encore files for local dev
stop Stop docker containers
down Remove docker containers
debug XDebug
csFixer Execute php-cs-fixer and php lint
phpStan Execute phpStan
tests Run unit tests and functional tests
unitTests Run unit tests
qualityTests Run quality tests
functionalTests Run functional tests
pushImages Update registry image
pushImagePhp Update registry php image
pushImageNginx Update registry nginx api image
pullImages Pull latest images from private repository
securityCheck Check vulnerabilities in project libraries
EOF
}
# Show help if no argument was supplied
if [ $# -eq 0 ]
then
show_help >&2
exit 1
fi
case "$1" in
initialize)
initialize
;;
localinit)
localinit
;;
build)
build
;;
start)
start
;;
stop)
stop
;;
down)
down
;;
restart)
stop
start
;;
install)
install
;;
installDb)
installDb
;;
populate)
populate
;;
jwtKeys)
jwtKeys
;;
serve)
if [ "$#" -ne 2 ]; then
serve
else
serve $2
fi
;;
tests)
tests
;;
unitTests)
unitTests
;;
qualityTests)
qualityTests
;;
functionalTests)
if [ "$#" -ne 2 ]; then
functionalTests
else
functionalTests $2
fi
;;
csFixer)
if [ "$#" -ne 2 ]; then
csFixer
else
csFixer $2
fi
;;
phpStan)
phpStan
;;
pushImages)
pushImages
;;
pushImagePhp)
pushImagePhp
;;
pushImageNginx)
pushImageNginx
;;
pullImages)
pullImages
;;
securityCheck)
security
;;
*)
show_help >&2
exit 1
esac
exit 0