diff --git a/bin/docker-pull-from-env-vars.sh b/bin/docker-pull-from-env-vars.sh new file mode 100755 index 0000000..e9c6b39 --- /dev/null +++ b/bin/docker-pull-from-env-vars.sh @@ -0,0 +1,114 @@ +#!/bin/bash + +set -e + +function parse_Env_Var_From_Env_File() +{ + local env_var="${1}" + + local env_file="${2:-.env}" + + # TODO: only grep lines that are not commented out. + local env_var_line="$(grep -i ${env_var}= ${env_file})" + + # PHP_IMAGE=exadra37/php7-fpm:latest -> exadra37/php7-fpm:latest + local docker_image="${env_var_line##*=}" + + echo "${docker_image}" +} + +function parse_Env_Var_From_Docker_Compose() +{ + local env_var="${1}" + + local docker_compose_file="${2:-./vendor/exadra37-docker-compose/php7-docker-stack/src/docker-compose.yml}" + + local env_var_line=$(grep -i ${env_var}:-"${docker_compose_file}") + + # image: ${PHP_IMAGE:-exadra37/php7-fpm:latest} -> exadra37/php7-fpm:latest + local docker_image="${env_var_line##*:-}" + + # removes } from end of string + local docker_image="${docker_image::-1}" + + echo "${docker_image}" +} + +function parse_Env_Var_From_Docker_Compose() +{ + local env_var="${1}" + local env_var_line=$(grep -i ${env_var}:- ./vendor/exadra37-docker-compose/php7-docker-stack/src/docker-compose.yml) + + # image: ${PHP_IMAGE:-exadra37/php7-fpm:latest} -> exadra37/php7-fpm:latest + local docker_image="${env_var_line##*:-}" + + # removes } from end of string + local docker_image="${docker_image::-1}" + + echo "${docker_image}" +} + +function pull_Docker_Image_From_Env_File() +{ + local env_var="${1}" + + if [ -f ".env" ] + then + + if [ -z "${docker_image}" ] + then + return 1 + fi + + printf "\n---> Pulling From Env File : ${docker_image}\n" + sudo docker pull "${docker_image}" + + return 0 + fi +} + +function pull_Docker_Image_From_Docker_Compose() +{ + local env_var="${1}" + + local docker_image="$(parse_Env_Var_From_Docker_Compose "${env_var}")" + + if [ -z "${docker_image}" ] + then + return 1 + fi + + printf "\n---> Pulling From Docker Compose : ${docker_image}\n" + sudo docker pull "${docker_image}" + + return 0 +} + +function pull_Docker_Image() +{ + local env_var="${1}" + + if ! pull_Docker_Image_From_Env_File "${env_var}" + then + pull_Docker_Image_From_Docker_Compose "${env_var}" + fi +} + +function pull_Docker_Images() +{ + local DOCKER_IMAGES_ENV_VARS=( + CACHE_IMAGE + CRON_JOBS_IMAGE + DATABASE_IMAGE + HTTP_IMAGE + PHP_IMAGE + QUEUE_IMAGE + DEV_CLI_IMAGE + DATABASE_CLI_IMAGE + ) + + for env_var in "${DOCKER_IMAGES_ENV_VARS[@]}" + do + pull_Docker_Image "${env_var}" + done +} diff --git a/bin/php7-docker-stack-setup.sh b/bin/php7-docker-stack-setup.sh new file mode 100755 index 0000000..56cdfc9 --- /dev/null +++ b/bin/php7-docker-stack-setup.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +set -e + +function createDirIfNotExists() +{ + local dir_path="${1}" + + [ -d "${dir_path}" ] || mkdir -p "${dir_path}" +} + +function copyDockerDirToProject() +{ + printf "\n---> Copy Docker Stack Images to Project Root <---" + cp -rn ./vendor/exadra37-docker-compose/php7-docker-stack/src/docker . +} + +function createDefaultHostDirs() +{ + printf "\n---> Create Docker Stack Default Host Dirs <---" + + # creating the default dirs to persist the containers data. otherwise they will + # be created as root user. + createDirIfNotExists ~/.dockerize/storage/database/mysql + createDirIfNotExists ~/.dockerize/storage/elastic-search/public + createDirIfNotExists ~/.dockerize/storage/elastic-search/internal +} + +function setEnvVars() +{ + printf "\n---> Setup Env Vars <---\n\n" +} + +function setupDockerStack() +{ + createDefaultHostDirs + copyDockerDirToProject + setEnvVars +} diff --git a/bin/server b/bin/server index e6c50c6..c255a6b 100755 --- a/bin/server +++ b/bin/server @@ -1,4 +1,25 @@ -#!/bin/sh +#!/bin/bash + +set -e + +source ./vendor/exadra37-docker-compose/php7-docker-stack/bin/php7-docker-stack-setup.sh +source ./vendor/exadra37-docker-compose/php7-docker-stack/bin/docker-pull-from-env-vars.sh + +if [ "--setup-docker-stack" == "${1}" ] + then + setupDockerStack + exit 0 +fi + +# For first run we want to ensure that the ./docker folder exists in the project. +if [ ! -d ./docker ] + then + setupDockerStack +fi + +pull_Docker_Images + +exit 0 # We need to ensure that if Php7 Docker Stack is used in more than 1 project, in same machine, Docker compose is able to # create unique names for the Docker containers in each project. diff --git a/composer.json b/composer.json index 2711e64..58147fa 100644 --- a/composer.json +++ b/composer.json @@ -44,6 +44,17 @@ "bin": [ "bin/server" ], + "scripts": { + "post-package-install": [ + "./bin/php7-docker-stack-setup.sh" + ], + "post-install-cmd": [ + "./bin/php7-docker-stack-setup.sh" + ], + "post-update-cmd": [ + "./bin/php7-docker-stack-setup.sh" + ] + }, "minimum-stability": "dev", "prefer-stable": true }