From 7505ec6394b3e36ce03d52e507e5bdf1cf933f53 Mon Sep 17 00:00:00 2001 From: Jan Hryniuk Date: Fri, 21 Apr 2017 10:29:29 +0200 Subject: [PATCH 1/2] Devided docker.sh script to smaller onse --- bash/buildImages.sh | 29 +++++++++++++++++++++++++++++ bash/checkContainerExists.sh | 11 +++++++++++ bash/defineVariables.sh | 17 +++++++++++++++++ bash/runBuild.sh | 17 +++++++++++++++++ bash/runInBackground.sh | 12 ++++++++++++ docker-compose.local.yml | 2 +- docker.sh | 19 +++++++++++++++++++ docker/php56xdebug/Dockerfile | 2 ++ docker/php70xdebug/Dockerfile | 2 ++ docker/php71xdebug/Dockerfile | 2 ++ 10 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 bash/buildImages.sh create mode 100644 bash/checkContainerExists.sh create mode 100644 bash/defineVariables.sh create mode 100644 bash/runBuild.sh create mode 100644 bash/runInBackground.sh diff --git a/bash/buildImages.sh b/bash/buildImages.sh new file mode 100644 index 0000000..f0d10f3 --- /dev/null +++ b/bash/buildImages.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +function buildImages { + NAME=$1 + VERSION=$2 + USERID=${3:-1000} + PHP=${4-:"all"} + + + imageExists "${NAME}:${VERSION}-php56xdebug" + if [[ $? == 0 ]] && [[ "$PHP" == "php56xdebug" || "$PHP" == "all" ]]; then + docker build -t "${NAME}:${VERSION}-php56xdebug" --build-arg "USERID=$USERID" "docker/php56xdebug" + fi + + imageExists "${NAME}:${VERSION}-php7xdebug" + if [[ $? == 0 ]] && [[ "$PHP" == "php7xdebug" || "$PHP" == "all" ]]; then + docker build -t "${NAME}:${VERSION}-php7xdebug" --build-arg "USERID=$USERID" "docker/php7xdebug" + fi + + imageExists "${NAME}:${VERSION}-php71xdebug" + if [[ $? == 0 ]] && [[ "$PHP" == "php71xdebug" || "$PHP" == "all" ]]; then + docker build -t "${NAME}:${VERSION}-php71xdebug" --build-arg "USERID=$USERID" "docker/php71xdebug" + fi + + imageExists "${NAME}:${VERSION}-nginx" + if [[ $? == 0 ]]; then + docker build -t "${NAME}:${VERSION}-nginx" "docker/nginx" + fi +} diff --git a/bash/checkContainerExists.sh b/bash/checkContainerExists.sh new file mode 100644 index 0000000..bab2d85 --- /dev/null +++ b/bash/checkContainerExists.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +function imageExists { + IMAGE_NAME=$1 + if docker history -q "$IMAGE_NAME" > /dev/null 2>&1; then + echo - "$IMAGE_NAME already exist" + return 1; + fi + + return 0; +} diff --git a/bash/defineVariables.sh b/bash/defineVariables.sh new file mode 100644 index 0000000..a5dac15 --- /dev/null +++ b/bash/defineVariables.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +function defineVariables +{ + export PROJECT_NAME=$(cat ".project_name") + export PROJECT_XDEBUG_ENABLED=false + export PROJECT_WEB_DIR=${PROJECT_WEB_DIR:="web"} + export PROJECT_INDEX_FILE=${PROJECT_INDEX_FILE:="index.php"} + export PROJECT_DEV_INDEX_FILE=${PROJECT_DEV_INDEX_FILE:="index_dev.php"} + export APP_NAME=$(echo $(cat "composer.json" | grep name | head -1 | awk -F: '{ print $2 }' | sed 's/[",\r]//g' | tr -dc '[:alnum:]\n\r' | tr '[:upper:]' '[:lower:]')) + export APP_VERSION=$(echo $(cat "composer.json" | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[",\r]//g')) + export USERID=$(id -u); + + echo "User ID: $USERID"; + echo -e "\nIMAGE VERSION: $APP_NAME:$APP_VERSION\n"; + declare -i BUILD_STATUS=0; +} diff --git a/bash/runBuild.sh b/bash/runBuild.sh new file mode 100644 index 0000000..86d9093 --- /dev/null +++ b/bash/runBuild.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +function runBuild { + export IMAGE_VERSION=$1 + + BUILD_OUTPUT=$(docker-compose up php) + + docker-compose kill > /dev/null 2>&1 + docker-compose rm -f -v > /dev/null 2>&1 + + echo -e "$BUILD_OUTPUT"; + if echo "$BUILD_OUTPUT" | grep -q "exited with code 0"; then + BUILD_STATUS=0; + else + BUILD_STATUS=1; + fi +} diff --git a/bash/runInBackground.sh b/bash/runInBackground.sh new file mode 100644 index 0000000..1ae2365 --- /dev/null +++ b/bash/runInBackground.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +function runInBackground { + export IMAGE_VERSION=$1 + + docker-compose -f docker-compose.yml -f docker-compose.local.yml kill > /dev/null 2>&1 + docker-compose -f docker-compose.yml -f docker-compose.local.yml rm -f -v > /dev/null 2>&1 + docker-compose -f docker-compose.yml -f docker-compose.local.yml up -d nginx php mysql + docker-compose -f docker-compose.yml -f docker-compose.local.yml exec php bash + docker-compose -f docker-compose.yml -f docker-compose.local.yml kill > /dev/null 2>&1 + docker-compose -f docker-compose.yml -f docker-compose.local.yml rm -f -v > /dev/null 2>&1 +} diff --git a/docker-compose.local.yml b/docker-compose.local.yml index 13053cd..bafcbb8 100644 --- a/docker-compose.local.yml +++ b/docker-compose.local.yml @@ -13,7 +13,7 @@ mysql: - ./data:/var/lib/mysql nginx: - image: "${PROJECT_NAME}:${APP_VERSION}-nginx" + image: "${APP_NAME}:${APP_VERSION}-nginx" ports: - "8000:80" links: diff --git a/docker.sh b/docker.sh index c1d5052..1da32ea 100755 --- a/docker.sh +++ b/docker.sh @@ -16,6 +16,24 @@ echo "User ID: $USERID"; echo -e "\nIMAGE VERSION: $APP_NAME:$APP_VERSION\n"; declare -i BUILD_STATUS=0; +__DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +cd "${__DIR}" + + +FILES=( + 'buildImages.sh' + 'checkContainerExists.sh' + 'defineVariables.sh' + 'runBuild.sh' + 'runInBackground.sh' +) + + +for FILE in "${FILES[@]}" ; do + source "bash/${FILE}" +done + + echo -e "All defined variables:\n" ( set -o posix ; set ) | grep 'PROJECT|APP' @@ -26,6 +44,7 @@ function imageExists { return 1; fi + return 0; } diff --git a/docker/php56xdebug/Dockerfile b/docker/php56xdebug/Dockerfile index fa20229..665f3bb 100644 --- a/docker/php56xdebug/Dockerfile +++ b/docker/php56xdebug/Dockerfile @@ -24,6 +24,8 @@ RUN apt-get -qq update --fix-missing && \ libkrb5-dev \ cloc \ git-core \ + zip \ + unzip \ && rm -rf /var/lib/apt/lists/* COPY docker-php-pecl-install /usr/local/bin/ diff --git a/docker/php70xdebug/Dockerfile b/docker/php70xdebug/Dockerfile index 001c74d..f7b534d 100644 --- a/docker/php70xdebug/Dockerfile +++ b/docker/php70xdebug/Dockerfile @@ -24,6 +24,8 @@ RUN apt-get -qq update --fix-missing && \ libkrb5-dev \ cloc \ git-core \ + zip \ + unzip \ && rm -rf /var/lib/apt/lists/* COPY docker-php-pecl-install /usr/local/bin/ diff --git a/docker/php71xdebug/Dockerfile b/docker/php71xdebug/Dockerfile index 63edf42..eab1c00 100644 --- a/docker/php71xdebug/Dockerfile +++ b/docker/php71xdebug/Dockerfile @@ -24,6 +24,8 @@ RUN apt-get -qq update --fix-missing && \ libkrb5-dev \ cloc \ git-core \ + zip \ + unzip \ && rm -rf /var/lib/apt/lists/* COPY docker-php-pecl-install /usr/local/bin/ From fb517028d0fe0fbce7d326a8058231c76b286259 Mon Sep 17 00:00:00 2001 From: Jan Hryniuk Date: Tue, 2 May 2017 10:28:08 +0200 Subject: [PATCH 2/2] Rebase --- bash/buildImages.sh | 13 ++- ...ContainerExists.sh => checkImageExists.sh} | 0 bash/defineVariables.sh | 9 +- bash/runBuild.sh | 3 +- bash/runInBackground.sh | 1 + docker.sh | 83 +------------------ 6 files changed, 14 insertions(+), 95 deletions(-) rename bash/{checkContainerExists.sh => checkImageExists.sh} (100%) diff --git a/bash/buildImages.sh b/bash/buildImages.sh index f0d10f3..7f922b6 100644 --- a/bash/buildImages.sh +++ b/bash/buildImages.sh @@ -6,24 +6,23 @@ function buildImages { USERID=${3:-1000} PHP=${4-:"all"} - imageExists "${NAME}:${VERSION}-php56xdebug" if [[ $? == 0 ]] && [[ "$PHP" == "php56xdebug" || "$PHP" == "all" ]]; then - docker build -t "${NAME}:${VERSION}-php56xdebug" --build-arg "USERID=$USERID" "docker/php56xdebug" + docker build -t "${NAME}:${VERSION}-php56xdebug" --build-arg "USERID=$USERID" docker/php56xdebug fi - imageExists "${NAME}:${VERSION}-php7xdebug" - if [[ $? == 0 ]] && [[ "$PHP" == "php7xdebug" || "$PHP" == "all" ]]; then - docker build -t "${NAME}:${VERSION}-php7xdebug" --build-arg "USERID=$USERID" "docker/php7xdebug" + imageExists "${NAME}:${VERSION}-php70xdebug" + if [[ $? == 0 ]] && [[ "$PHP" == "php70xdebug" || "$PHP" == "all" ]]; then + docker build -t "${NAME}:${VERSION}-php70xdebug" --build-arg "USERID=$USERID" docker/php70xdebug fi imageExists "${NAME}:${VERSION}-php71xdebug" if [[ $? == 0 ]] && [[ "$PHP" == "php71xdebug" || "$PHP" == "all" ]]; then - docker build -t "${NAME}:${VERSION}-php71xdebug" --build-arg "USERID=$USERID" "docker/php71xdebug" + docker build -t "${NAME}:${VERSION}-php71xdebug" --build-arg "USERID=$USERID" docker/php71xdebug fi imageExists "${NAME}:${VERSION}-nginx" if [[ $? == 0 ]]; then - docker build -t "${NAME}:${VERSION}-nginx" "docker/nginx" + docker build -t "${NAME}:${VERSION}-nginx" docker/nginx fi } diff --git a/bash/checkContainerExists.sh b/bash/checkImageExists.sh similarity index 100% rename from bash/checkContainerExists.sh rename to bash/checkImageExists.sh diff --git a/bash/defineVariables.sh b/bash/defineVariables.sh index a5dac15..942f016 100644 --- a/bash/defineVariables.sh +++ b/bash/defineVariables.sh @@ -2,15 +2,14 @@ function defineVariables { - export PROJECT_NAME=$(cat ".project_name") - export PROJECT_XDEBUG_ENABLED=false + export APP_NAME=$(echo $(cat composer.json | grep name | head -1 | awk -F: '{ print $2 }' | sed 's/[",\r]//g' | tr -dc '[:alnum:]\n\r' | tr '[:upper:]' '[:lower:]')) + export APP_VERSION=$(echo $(cat composer.json | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[",\r]//g')) + export PROJECT_NAME="$APP_NAME" export PROJECT_WEB_DIR=${PROJECT_WEB_DIR:="web"} export PROJECT_INDEX_FILE=${PROJECT_INDEX_FILE:="index.php"} export PROJECT_DEV_INDEX_FILE=${PROJECT_DEV_INDEX_FILE:="index_dev.php"} - export APP_NAME=$(echo $(cat "composer.json" | grep name | head -1 | awk -F: '{ print $2 }' | sed 's/[",\r]//g' | tr -dc '[:alnum:]\n\r' | tr '[:upper:]' '[:lower:]')) - export APP_VERSION=$(echo $(cat "composer.json" | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[",\r]//g')) - export USERID=$(id -u); + USERID=$(id -u); echo "User ID: $USERID"; echo -e "\nIMAGE VERSION: $APP_NAME:$APP_VERSION\n"; declare -i BUILD_STATUS=0; diff --git a/bash/runBuild.sh b/bash/runBuild.sh index 86d9093..4a38727 100644 --- a/bash/runBuild.sh +++ b/bash/runBuild.sh @@ -2,9 +2,8 @@ function runBuild { export IMAGE_VERSION=$1 - + export PROJECT_XDEBUG_ENABLED=${2:-false} BUILD_OUTPUT=$(docker-compose up php) - docker-compose kill > /dev/null 2>&1 docker-compose rm -f -v > /dev/null 2>&1 diff --git a/bash/runInBackground.sh b/bash/runInBackground.sh index 1ae2365..4cbf86d 100644 --- a/bash/runInBackground.sh +++ b/bash/runInBackground.sh @@ -2,6 +2,7 @@ function runInBackground { export IMAGE_VERSION=$1 + export PROJECT_XDEBUG_ENABLED=${2:-false} docker-compose -f docker-compose.yml -f docker-compose.local.yml kill > /dev/null 2>&1 docker-compose -f docker-compose.yml -f docker-compose.local.yml rm -f -v > /dev/null 2>&1 diff --git a/docker.sh b/docker.sh index 1da32ea..6880747 100755 --- a/docker.sh +++ b/docker.sh @@ -4,105 +4,26 @@ PROJECT_TASK_NAME=$1; PROJECT_PHP_VERSION=${2:-71}; PROJECT_WITH_COVERAGE=${3:-false}; -export APP_NAME=$(echo $(cat composer.json | grep name | head -1 | awk -F: '{ print $2 }' | sed 's/[",\r]//g' | tr -dc '[:alnum:]\n\r' | tr '[:upper:]' '[:lower:]')) -export APP_VERSION=$(echo $(cat composer.json | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[",\r]//g')) -export PROJECT_NAME="$APP_NAME" -export PROJECT_WEB_DIR=${PROJECT_WEB_DIR:="web"} -export PROJECT_INDEX_FILE=${PROJECT_INDEX_FILE:="index.php"} -export PROJECT_DEV_INDEX_FILE=${PROJECT_DEV_INDEX_FILE:="index_dev.php"} - -USERID=$(id -u); -echo "User ID: $USERID"; -echo -e "\nIMAGE VERSION: $APP_NAME:$APP_VERSION\n"; -declare -i BUILD_STATUS=0; - __DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" cd "${__DIR}" - FILES=( 'buildImages.sh' - 'checkContainerExists.sh' + 'checkImageExists.sh' 'defineVariables.sh' 'runBuild.sh' 'runInBackground.sh' ) - for FILE in "${FILES[@]}" ; do source "bash/${FILE}" done +defineVariables echo -e "All defined variables:\n" ( set -o posix ; set ) | grep 'PROJECT|APP' -function imageExists { - IMAGE_NAME=$1 - if docker history -q "$IMAGE_NAME" > /dev/null 2>&1; then - echo - "$IMAGE_NAME already exist" - return 1; - fi - - - return 0; -} - -function buildImages { - NAME=$1 - VERSION=$2 - USERID=${3:-1000} - PHP=${4-:"all"} - - imageExists "${NAME}:${VERSION}-php56xdebug" - if [[ $? == 0 ]] && [[ "$PHP" == "php56xdebug" || "$PHP" == "all" ]]; then - docker build -t "${NAME}:${VERSION}-php56xdebug" --build-arg "USERID=$USERID" docker/php56xdebug - fi - - imageExists "${NAME}:${VERSION}-php70xdebug" - if [[ $? == 0 ]] && [[ "$PHP" == "php70xdebug" || "$PHP" == "all" ]]; then - docker build -t "${NAME}:${VERSION}-php70xdebug" --build-arg "USERID=$USERID" docker/php70xdebug - fi - - imageExists "${NAME}:${VERSION}-php71xdebug" - if [[ $? == 0 ]] && [[ "$PHP" == "php71xdebug" || "$PHP" == "all" ]]; then - docker build -t "${NAME}:${VERSION}-php71xdebug" --build-arg "USERID=$USERID" docker/php71xdebug - fi - - imageExists "${NAME}:${VERSION}-nginx" - if [[ $? == 0 ]]; then - docker build -t "${NAME}:${VERSION}-nginx" docker/nginx - fi -} - -function runBuild { - export IMAGE_VERSION=$1 - export PROJECT_XDEBUG_ENABLED=${2:-false} - BUILD_OUTPUT=$(docker-compose up php) - docker-compose kill > /dev/null 2>&1 - docker-compose rm -f -v > /dev/null 2>&1 - - echo -e "$BUILD_OUTPUT"; - if echo "$BUILD_OUTPUT" | grep -q "exited with code 0"; then - BUILD_STATUS=0; - else - BUILD_STATUS=1; - fi - -} - -function runInBackground { - export IMAGE_VERSION=$1 - export PROJECT_XDEBUG_ENABLED=${2:-false} - - docker-compose -f docker-compose.yml -f docker-compose.local.yml kill > /dev/null 2>&1 - docker-compose -f docker-compose.yml -f docker-compose.local.yml rm -f -v > /dev/null 2>&1 - docker-compose -f docker-compose.yml -f docker-compose.local.yml up -d nginx php mysql - docker-compose -f docker-compose.yml -f docker-compose.local.yml exec php bash - docker-compose -f docker-compose.yml -f docker-compose.local.yml kill > /dev/null 2>&1 - docker-compose -f docker-compose.yml -f docker-compose.local.yml rm -f -v > /dev/null 2>&1 -} - if [[ $PROJECT_TASK_NAME == '' ]]; then echo -e "Available tasks:"; echo -e "'./docker.sh build ${PROJECT_PHP_VERSION} ${PROJECT_WITH_COVERAGE}'";