From 338fc97ab0151ce37f2eaad5a1c796bb12e83118 Mon Sep 17 00:00:00 2001 From: Fabrice Gangler Date: Sat, 30 Sep 2017 11:30:59 +0200 Subject: [PATCH] build_and_run-with-docker.sh - refactor + fixed #95 --- docker/build_and_run-with-docker.sh | 208 +++++++++++++++++++--------- 1 file changed, 140 insertions(+), 68 deletions(-) diff --git a/docker/build_and_run-with-docker.sh b/docker/build_and_run-with-docker.sh index 6b05204d..14ade85c 100755 --- a/docker/build_and_run-with-docker.sh +++ b/docker/build_and_run-with-docker.sh @@ -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 ############################################# @@ -41,9 +61,9 @@ usage () { ${BOLD}${GREEN}-s${NORM} | --source-dir MANDATORY Absolute path to ${APP_NAME} sources directory ${BOLD}${GREEN}-d${NORM} | --docker-dir MANDATORY Path to directory containing the Dockerfile. Path must be relative to SOURCE_DIR - ${BOLD}-p${NORM} | --port Default value: 8087 - ${BOLD}-n${NORM} | --container-name Default value: contrast.finder - ${BOLD}-i${NORM} | --image-name Default value: asqatasun/contrast-finder + ${BOLD}-p${NORM} | --port Default value: ${CONTAINER_EXPOSED_PORT} + ${BOLD}-n${NORM} | --container-name Default value: ${CONTAINER_NAME} + ${BOLD}-i${NORM} | --image-name Default value: ${IMAGE_NAME} ${BOLD}-t${NORM} | --tag-name Default value: ${TIMESTAMP} ${BOLD}-b${NORM} | --build-only-dir Build only webapp and (relative to SOURCE_DIR) @@ -58,7 +78,7 @@ 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 } @@ -66,7 +86,7 @@ EOF ############################################# # 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 ; @@ -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 ;; @@ -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 ;; @@ -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 ############################################# @@ -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 @@ -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}" @@ -199,14 +272,16 @@ 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" @@ -214,10 +289,9 @@ function do_docker_build() { 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 @@ -225,28 +299,8 @@ function do_docker_run() { 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() { @@ -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" ; @@ -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 @@ -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}" \ No newline at end of file