Skip to content

Commit

Permalink
build_and_run-with-docker.sh - refactor + fixed #95
Browse files Browse the repository at this point in the history
  • Loading branch information
dzc34 committed Sep 30, 2017
1 parent 8bf02c5 commit 338fc97
Showing 1 changed file with 140 additions and 68 deletions.
208 changes: 140 additions & 68 deletions docker/build_and_run-with-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ SCRIPT=`basename ${BASH_SOURCE[0]}`
NORM=$(tput sgr0)
NORMAL=$(tput sgr0)

declare SOURCE_DIR
declare DOCKER_DIR
declare HELP=false
declare FTESTS=false
declare SKIP_BUILD=false
declare SKIP_BUILD_TEST=false
declare SKIP_COPY=false
declare SKIP_DOCKER_BUILD=false
declare SKIP_DOCKER_RUN=false
declare USE_SUDO_DOCKER=false
declare ONLY_LOCALHOST=false
declare BUILD_ONLY_WEBAPP=false
declare BUILD_ONLY_DIR=false
declare LOG_BUILD=false
declare WEBAPP_DIR="webapp"
declare CONTAINER_EXPOSED_PORT="8087"
declare CONTAINER_NAME="contrast.finder"
declare IMAGE_NAME="asqatasun/contrast-finder"
declare TAG_NAME=${TIMESTAMP}

#############################################
# Usage
#############################################
Expand All @@ -41,9 +61,9 @@ usage () {
${BOLD}${GREEN}-s${NORM} | --source-dir <directory> MANDATORY Absolute path to ${APP_NAME} sources directory
${BOLD}${GREEN}-d${NORM} | --docker-dir <directory> MANDATORY Path to directory containing the Dockerfile.
Path must be relative to SOURCE_DIR
${BOLD}-p${NORM} | --port <port> Default value: 8087
${BOLD}-n${NORM} | --container-name <name> Default value: contrast.finder
${BOLD}-i${NORM} | --image-name <name> Default value: asqatasun/contrast-finder
${BOLD}-p${NORM} | --port <port> Default value: ${CONTAINER_EXPOSED_PORT}
${BOLD}-n${NORM} | --container-name <name> Default value: ${CONTAINER_NAME}
${BOLD}-i${NORM} | --image-name <name> Default value: ${IMAGE_NAME}
${BOLD}-t${NORM} | --tag-name <name> Default value: ${TIMESTAMP}
${BOLD}-b${NORM} | --build-only-dir <directory> Build only webapp and <directory> (relative to SOURCE_DIR)
Expand All @@ -58,15 +78,15 @@ usage () {
--log-build Log maven build
${BOLD}-h${NORM} | --help ${REV} Show this help ${NORM}
${BOLD}-t${NORM} | --functional-tests Also execute functional tests. @@@TODO
${BOLD}-f${NORM} | --functional-tests Also execute functional tests. (not yet implemented)
EOF
exit 2
}

#############################################
# Manage options and usage
#############################################
TEMP=`getopt -o s:d:p:n:i:t:b:lwht --long source-dir:,docker-dir:,port:,container-name:,image-name:,tag-name:,build-only-dir:,only-localhost,build-only-webapp,help,functional-tests,log-build,skip-build-test,skip-build,skip-copy,skip-docker-build,skip-docker-run,use-sudo-docker -- "$@"`
TEMP=`getopt -o s:d:p:n:i:t:b:lwhf --long source-dir:,docker-dir:,port:,container-name:,image-name:,tag-name:,build-only-dir:,only-localhost,build-only-webapp,help,functional-tests,log-build,skip-build-test,skip-build,skip-copy,skip-docker-build,skip-docker-run,use-sudo-docker -- "$@"`

if [[ $? != 0 ]] ; then
echo "Terminating..." >&2 ;
Expand All @@ -76,26 +96,6 @@ fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "${TEMP}"

declare SOURCE_DIR
declare DOCKER_DIR
declare HELP=false
declare FTESTS=false
declare SKIP_BUILD=false
declare SKIP_BUILD_TEST=false
declare SKIP_COPY=false
declare SKIP_DOCKER_BUILD=false
declare SKIP_DOCKER_RUN=false
declare USE_SUDO_DOCKER=false
declare ONLY_LOCALHOST=false
declare BUILD_ONLY_WEBAPP=false
declare BUILD_ONLY_DIR=false
declare LOG_BUILD=false
declare WEBAPP_DIR="webapp"
declare CONTAINER_EXPOSED_PORT="8087"
declare CONTAINER_NAME="contrast.finder"
declare IMAGE_NAME="asqatasun/contrast-finder"
declare TAG_NAME=${TIMESTAMP}

while true; do
case "$1" in
-s | --source-dir ) SOURCE_DIR="$2"; shift 2 ;;
Expand All @@ -107,7 +107,7 @@ while true; do
-b | --build-only-dir ) BUILD_ONLY_DIR="$2"; shift 2 ;;
-w | --build-only-webapp ) BUILD_ONLY_WEBAPP=true; shift ;;
-h | --help ) HELP=true; shift ;;
-t | --functional-tests ) FTESTS=true; shift ;;
-f | --functional-tests ) FTESTS=true; shift ;;
-l | --only-localhost ) ONLY_LOCALHOST=true; shift ;;
--log-build ) LOG_BUILD=true; shift ;;
--skip-build-test ) SKIP_BUILD_TEST=true; shift ;;
Expand All @@ -132,14 +132,16 @@ fi

TGZ_EXT=".tar.gz"
TGZ_BASENAME="webapp/target/contrast-finder"
URL="http://localhost:${CONTAINER_EXPOSED_PORT}/contrast-finder/"
ADD_IP=''
URL_TOMCAT="http://localhost:${CONTAINER_EXPOSED_PORT}"
if ${ONLY_LOCALHOST} ; then
ADD_IP="127.0.0.1:"; # the ":" at the end is mandatory for docker run
URL_TOMCAT="http://${ADD_IP}${CONTAINER_EXPOSED_PORT}"
fi
URL_APP="${URL_TOMCAT}/contrast-finder/"

SUDO=''
if ${USE_SUDO_DOCKER} ; then SUDO='sudo '; fi
if ${ONLY_LOCALHOST} ; then
ADD_IP="127.0.0.1:";
URL="http://127.0.0.1:${CONTAINER_EXPOSED_PORT}/contrast-finder/"
fi


#############################################
Expand All @@ -156,7 +158,77 @@ fail() {
exit -1
}

# display a step message
function display_step_msg() {
echo " -------------------------------------------------------"
echo " ${1}"
}

# ask the user to confirm (Y|y|yes|[enter]
function user_confirmation() {
RUN_TESTS=false
YES=false # -y option not yet implemented
if ${YES} ; then
RUN_TESTS=true
else
read -r -p " Are you sure? ... [Y/n] " REPLY
REPLY=${REPLY,,} # tolower
if [[ $REPLY =~ ^(yes|y| ) ]] || [[ -z $REPLY ]]; then
RUN_TESTS=true
fi
fi
echo # (optional) move to a new line
}

# Check URL (return HTTP code)
function check_URL() {
local URL=$1
set +e
local RESULT=$(curl -o /dev/null --silent --write-out '%{http_code}\n' ${URL})
set -e
echo "${RESULT}"
}

# test if URL_TOMCAT responds with an HTTP 200 code
function check_Tomcat_loading() {
local time=0
local RESULT=$(check_URL ${URL_TOMCAT})
while ((RESULT!=200))
do
RESULT=$(check_URL ${URL_TOMCAT})
if [ "${RESULT}" == "200" ]; then
display_step_msg "Tomcat server is now running ........ HTTP code = ${BOLD}${GREEN}${RESULT}${NORM}"
else
((time+=1))
echo " ... ${REV}${time}${NORM} ... Tomcat server loading ..."
sleep 1
fi
done
}

# test if URL_APP responds with an HTTP 200 code
function check_App_loading() {
local time=0
local RESULT=$(check_URL ${URL_APP})
if [ "${RESULT}" == "200" ]; then
display_step_msg "${APP_NAME} is now running ........ HTTP code = ${BOLD}${GREEN}${RESULT}${NORM}"
else
while ((RESULT!=200))
do
RESULT=$(check_URL ${URL_APP})
if [ "${RESULT}" == "200" ]; then
display_step_msg "${APP_NAME} is now running ........ HTTP code = ${BOLD}${GREEN}${RESULT}${NORM}"
else
((time+=1))
echo " ... ${REV}${time}${NORM} ... loading ${APP_NAME} ..."
sleep 1
fi
done
fi
}

function kill_previous_container() {
display_step_msg "Stop and remove the previous ${BOLD}${GREEN}Docker${NORM} container"
set +e
RUNNING=$(${SUDO} docker inspect --format="{{ .State.Status }}" ${CONTAINER_NAME} 2>/dev/null)
set -e
Expand Down Expand Up @@ -189,6 +261,7 @@ function build() {
}

function do_build() {
display_step_msg "Maven ${BOLD}${GREEN}build${NORM}"
if [[ -n "$BUILD_ONLY_DIR" && "$BUILD_ONLY_DIR" != "false" ]] ; then
build "${SOURCE_DIR}/${BUILD_ONLY_DIR}"
build "${SOURCE_DIR}/${WEBAPP_DIR}"
Expand All @@ -199,54 +272,35 @@ function do_build() {
fi
}

# copy .WAR to docker dir
# copy TAR.GZ to docker dir
function do_copy_targz() {
display_step_msg "Copy ${BOLD}${GREEN}tar.gz${NORM} to the docker directory"
cp "${SOURCE_DIR}/${TGZ_BASENAME}"*"${TGZ_EXT}" "${SOURCE_DIR}/${DOCKER_DIR}/" ||
fail "Error copying ${SOURCE_DIR}/${TGZ_BASENAME}"
}

# build Docker container
function do_docker_build() {
display_step_msg "Build ${BOLD}${GREEN}Docker${NORM} image"
(cd "${SOURCE_DIR}/${DOCKER_DIR}" ; \
${SUDO} docker build -t ${IMAGE_NAME}:${TAG_NAME} "${SOURCE_DIR}/${DOCKER_DIR}" ) ||
fail "Error building Docker image"
}

function do_docker_run() {
kill_previous_container
set +e
RESULT=$(curl -o /dev/null --silent --write-out '%{http_code}\n' ${URL})
set -e
RESULT=$(check_URL ${URL_TOMCAT})
if [ "${RESULT}" == "000" ]; then
display_step_msg "Loading ${BOLD}${GREEN}Docker${NORM} container"
DOCKER_RUN="${SUDO}docker run -p ${ADD_IP}${CONTAINER_EXPOSED_PORT}:8080 --name ${CONTAINER_NAME} -d ${IMAGE_NAME}:${TAG_NAME}"
eval ${DOCKER_RUN}
else
fail "Error running Docker container. ${CONTAINER_EXPOSED_PORT} port is already allocated"
fi

# wait a bit to let container start
# test if URL responds with 200
time=0
echo ""
while ((RESULT!=200))
do
set +e
RESULT=$(curl -o /dev/null --silent --write-out '%{http_code}\n' ${URL})
set -e
if [ "${RESULT}" == "200" ]; then
echo " -------------------------------------------------------"
echo " ${APP_NAME} is now running ........ HTTP code = ${BOLD}${GREEN}${RESULT}${NORM}"
else
((time+=1))
echo " ... ${REV}${time}${NORM} ... loading ${APP_NAME} ..."
sleep 1
fi
done
}

function do_functional_testing() {
# functional testing
fail " The functional tests are not yet implemented"
check_Tomcat_loading
check_App_loading
}

function do_maven_log_processing() {
Expand All @@ -258,7 +312,9 @@ function do_maven_log_processing() {
LOG_DIR_SRC="${LOG_DIR_SRC}";
LOG_DIR="${LOG_DIR_SRC}/target";
mkdir -p "${LOG_DIR}"
mv "${LOG_DIR_SRC}/log_maven.log" "${LOG_DIR}/"
if [ -f "${LOG_DIR_SRC}/log_maven.log" ];then
mv "${LOG_DIR_SRC}/log_maven.log" "${LOG_DIR}/"
fi
echo " -------------------------------------------------------"
cat "${LOG_DIR}/log_maven.log" | grep "<<< FAILURE! -" | tee "${LOG_DIR}/log_maven-FAIL.log" ;
cat "${LOG_DIR}/log_maven.log" | grep "WARN" > "${LOG_DIR}/log_maven-WARM.log" ;
Expand All @@ -269,7 +325,32 @@ function do_maven_log_processing() {
fi
}

# functional testing
function do_functional_testing() {
display_step_msg "Functional ${BOLD}${GREEN}testing ${NORM}"
user_confirmation
if ${RUN_TESTS} ; then
fail " The functional tests are not yet implemented"
# see http://docs.seleniumhq.org/download/maven.jsp
view_build_summary
fi
}

# view build summary
function view_build_summary () {
if ${LOG_BUILD}; then
do_maven_log_processing;
fi
echo " -------------------------------------------------------"
echo " Container ... ${CONTAINER_NAME}"
echo " Dockerfile .. ${DOCKER_DIR}"
echo " Image ....... ${IMAGE_NAME}:${TAG_NAME}"
echo " CMD ......... ${DOCKER_RUN}"
echo " -------------------------------------------------------"
echo " Shell ....... ${SUDO}docker exec -ti ${CONTAINER_NAME} /bin/bash"
echo " Log ......... ${SUDO}docker logs -f ${CONTAINER_NAME}"
echo " URL ......... ${BOLD}${GREEN}${URL_APP}${NORM}"
}

#############################################
# Do the actual job
Expand All @@ -279,14 +360,5 @@ if ! ${SKIP_BUILD}; then do_build; fi
if ! ${SKIP_COPY}; then do_copy_targz; fi
if ! ${SKIP_DOCKER_BUILD}; then do_docker_build; fi
if ! ${SKIP_DOCKER_RUN}; then do_docker_run; fi
if ${LOG_BUILD}; then do_maven_log_processing; fi
view_build_summary
if ${FTESTS}; then do_functional_testing; fi

echo " -------------------------------------------------------"
echo " Container .. ${CONTAINER_NAME}"
echo " Image ...... ${IMAGE_NAME}:${TAG_NAME}"
echo " CMD ........ ${DOCKER_RUN}"
echo " -------------------------------------------------------"
echo " Shell ...... ${SUDO}docker exec -ti ${CONTAINER_NAME} /bin/bash"
echo " Log ........ ${SUDO}docker logs -f ${CONTAINER_NAME}"
echo " URL ........ ${BOLD}${GREEN}${URL}${NORM}"

0 comments on commit 338fc97

Please sign in to comment.