Skip to content

Commit

Permalink
Improved logic of devided bash scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Hryniuk authored and janhryniukpgs committed Apr 24, 2017
1 parent bda0086 commit 23dbd58
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 46 deletions.
17 changes: 11 additions & 6 deletions bash/build.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env bash

function buildImages {
NAME=$1
VERSION=$2
USERID=${3:-1000}
PHP=${4-:"all"}
DIR=$1
NAME=$2
VERSION=$3
USERID=${4:-1000}
PHP=${5-:"all"}


imageExists "${NAME}:${VERSION}-php56xdebug"
Expand All @@ -29,9 +30,13 @@ function buildImages {
}

function runBuild {
export IMAGE_VERSION=$1
cd "${__DIR}"
DIR=$1
export IMAGE_VERSION=$2

cd "${DIR}"

BUILD_OUTPUT=$(docker-compose up php)

docker-compose kill > /dev/null 2>&1
docker-compose rm -f -v > /dev/null 2>&1

Expand Down
19 changes: 10 additions & 9 deletions bash/run.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/usr/bin/env bash

function runInBackground {
export IMAGE_VERSION=$1
DIR=$1
export IMAGE_VERSION=$2
DOCKER_COMPOSE_FILE="${DIR}/docker-compose.yml"
DOCKER_COMPOSE_LOCAL_FILE="${DIR}/docker-compose.local.yml"

cd "${__DIR}"

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
docker-compose -f "${DOCKER_COMPOSE_FILE}" -f "${DOCKER_COMPOSE_LOCAL_FILE}" kill > /dev/null 2>&1
docker-compose -f "${DOCKER_COMPOSE_FILE}" -f "${DOCKER_COMPOSE_LOCAL_FILE}" rm -f -v > /dev/null 2>&1
docker-compose -f "${DOCKER_COMPOSE_FILE}" -f "${DOCKER_COMPOSE_LOCAL_FILE}" up -d nginx php mysql
docker-compose -f "${DOCKER_COMPOSE_FILE}" -f "${DOCKER_COMPOSE_LOCAL_FILE}" exec php bash
docker-compose -f "${DOCKER_COMPOSE_FILE}" -f "${DOCKER_COMPOSE_LOCAL_FILE}" kill > /dev/null 2>&1
docker-compose -f "${DOCKER_COMPOSE_FILE}" -f "${DOCKER_COMPOSE_LOCAL_FILE}" rm -f -v > /dev/null 2>&1
}
27 changes: 15 additions & 12 deletions bash/variable.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#!/usr/bin/env bash

export PROJECT_NAME=$(cat "${__DIR}/.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 "${__DIR}/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 "${__DIR}/composer.json" | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[",\r]//g'))
export USERID=$(id -u);
function defineVariables
{
DIR=$1
export PROJECT_NAME=$(cat "${DIR}/.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 "${DIR}/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 "${DIR}/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;
echo "User ID: $USERID";
echo -e "\nIMAGE VERSION: $APP_NAME:$APP_VERSION\n";
declare -i BUILD_STATUS=0;
}
2 changes: 1 addition & 1 deletion docker-compose.local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
42 changes: 24 additions & 18 deletions docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ TASK_NAME=$1;

__DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"


for FILE in $(ls "${__DIR}/bash") ; do
source "${__DIR}/bash/${FILE}"
done

defineVariables "${__DIR}"

if [[ $TASK_NAME == '' ]]; then
echo -e "Available commands:";
echo -e "'build-images' - building docker images";
Expand All @@ -28,44 +29,49 @@ fi

case $TASK_NAME in
'build-images')
buildImages "${APP_NAME}" "${APP_VERSION}" "${USERID}" "all"
buildImages "${__DIR}" "${APP_NAME}" "${APP_VERSION}" "${USERID}" "all"
;;
'build-56')
buildImages "${APP_NAME}" "${APP_VERSION}" "${USERID}" "php56xdebug"
runBuild "${APP_NAME}:${APP_VERSION}-php56xdebug"
buildImages "${__DIR}" "${APP_NAME}" "${APP_VERSION}" "${USERID}" "php56xdebug"
runBuild "${__DIR}" "${APP_NAME}:${APP_VERSION}-php56xdebug"
;;
'build-56-coverage')
export PROJECT_ENABLED_XDEBUG=true
buildImages "${APP_NAME}" "${APP_VERSION}" "${USERID}" "php56xdebug"
runBuild "${APP_NAME}:${APP_VERSION}-php56xdebug"
buildImages "${__DIR}" "${APP_NAME}" "${APP_VERSION}" "${USERID}" "php56xdebug"
runBuild "${__DIR}" "${APP_NAME}:${APP_VERSION}-php56xdebug"
;;
'run-56-coverage')
export PROJECT_ENABLED_XDEBUG=true
buildImages "${APP_NAME}" "${APP_VERSION}" "${USERID}" "php56xdebug"
runInBackground "${APP_NAME}:${APP_VERSION}-php56xdebug"
buildImages "${__DIR}" "${APP_NAME}" "${APP_VERSION}" "${USERID}" "php56xdebug"
runInBackground "${__DIR}" "${APP_NAME}:${APP_VERSION}-php56xdebug"
;;
'build' | 'build-71')
buildImages "${APP_NAME}" "${APP_VERSION}" "${USERID}" "php71xdebug"
runBuild "${APP_NAME}:${APP_VERSION}-php71xdebug"
buildImages "${__DIR}" "${APP_NAME}" "${APP_VERSION}" "${USERID}" "php71xdebug"
runBuild "${__DIR}" "${APP_NAME}:${APP_VERSION}-php71xdebug"
;;
'build-coverage' | 'build-71-coverage')
export PROJECT_ENABLED_XDEBUG=true
buildImages "${APP_NAME}" "${APP_VERSION}" "${USERID}" "php71xdebug"
runBuild "${APP_NAME}:${APP_VERSION}-php71xdebug"
buildImages "${__DIR}" "${APP_NAME}" "${APP_VERSION}" "${USERID}" "php71xdebug"
runBuild "${__DIR}" "${APP_NAME}:${APP_VERSION}-php71xdebug"
;;
'run-coverage' | 'run-71-coverage')
export PROJECT_ENABLED_XDEBUG=true
buildImages "${APP_NAME}" "${APP_VERSION}" "${USERID}" "php71xdebug"
runInBackground "${APP_NAME}:${APP_VERSION}-php71xdebug"
buildImages "${__DIR}" "${APP_NAME}" "${APP_VERSION}" "${USERID}" "php71xdebug"
runInBackground "${__DIR}" "${APP_NAME}:${APP_VERSION}-php71xdebug"
;;
'build-7')
buildImages "${APP_NAME}" "${APP_VERSION}" "${USERID}" "php7xdebug"
runBuild "${APP_NAME}:${APP_VERSION}-php7xdebug"
buildImages "${__DIR}" "${APP_NAME}" "${APP_VERSION}" "${USERID}" "php7xdebug"
runBuild "${__DIR}" "${APP_NAME}:${APP_VERSION}-php7xdebug"
;;
'build-7-coverage')
export PROJECT_ENABLED_XDEBUG=true
buildImages "${APP_NAME}" "${APP_VERSION}" "${USERID}" "php7xdebug"
runBuild "${APP_NAME}:${APP_VERSION}-php7xdebug"
buildImages "${__DIR}" "${APP_NAME}" "${APP_VERSION}" "${USERID}" "php7xdebug"
runBuild "${__DIR}" "${APP_NAME}:${APP_VERSION}-php7xdebug"
;;
'run-7-coverage')
export PROJECT_ENABLED_XDEBUG=true
buildImages "${__DIR}" "${APP_NAME}" "${APP_VERSION}" "${USERID}" "php7xdebug"
runInBackground "${__DIR}" "${APP_NAME}:${APP_VERSION}-php1xdebug"
;;
esac

Expand Down

0 comments on commit 23dbd58

Please sign in to comment.