From e72a9115a7fa4a1e820969f93c7d97e4a4b97392 Mon Sep 17 00:00:00 2001 From: Martin Stockhammer Date: Sat, 7 Apr 2018 11:46:21 +0200 Subject: [PATCH] Changing docker image handling for webtest build --- src/ci/docker/webtest/Dockerfile | 14 ++- src/ci/docker/webtest/entry_point.sh | 6 +- src/ci/docker/webtest/x_run.sh | 5 + src/ci/scripts/container_webtest.sh | 155 +++++++++++++++++++++------ 4 files changed, 142 insertions(+), 38 deletions(-) create mode 100644 src/ci/docker/webtest/x_run.sh diff --git a/src/ci/docker/webtest/Dockerfile b/src/ci/docker/webtest/Dockerfile index 4c5a729b1..79a383c24 100644 --- a/src/ci/docker/webtest/Dockerfile +++ b/src/ci/docker/webtest/Dockerfile @@ -42,7 +42,7 @@ RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key >/dev/null -ARG CHROME_DRIVER_VERSION=2.29 +ARG CHROME_DRIVER_VERSION=2.37 RUN wget --no-verbose -O /tmp/chromedriver_linux64.zip https://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip \ && rm -rf /opt/selenium/chromedriver \ && unzip /tmp/chromedriver_linux64.zip -d /opt/selenium \ @@ -52,18 +52,24 @@ RUN wget --no-verbose -O /tmp/chromedriver_linux64.zip https://chromedriver.stor && ln -fs /opt/selenium/chromedriver-$CHROME_DRIVER_VERSION /usr/bin/chromedriver \ >/dev/null -RUN apt-get -qqy install openjdk-8-jdk xvfb dbus locales >/dev/null \ +RUN apt-get -qqy install openjdk-8-jdk xvfb dbus locales fluxbox >/dev/null \ && rm -rf /var/lib/apt/lists/* /var/cache/apt/* \ >/dev/null +RUN apt-get -qqy purge perl \ + && apt-get -qqy autoremove + ARG SELENIUM_VERSION=2.53.1 RUN mkdir -p /opt/bin && wget --no-verbose -O /opt/bin/selenium-server-standalone.jar https://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-$SELENIUM_VERSION.jar \ && chmod 644 /opt/bin/selenium-server-standalone.jar COPY entry_point.sh /opt/bin/entry_point.sh +COPY x_run.sh /opt/bin/x_run.sh COPY chrome_launcher.sh /usr/bin/google-chrome -RUN chmod +x /opt/bin/entry_point.sh -RUN chmod +x /usr/bin/google-chrome +RUN chmod +x /opt/bin/entry_point.sh \ + && chmod +x /usr/bin/google-chrome \ + && chmod +x /opt/bin/x_run.sh + ENV SCREEN_WIDTH 1600 ENV SCREEN_HEIGHT 1200 diff --git a/src/ci/docker/webtest/entry_point.sh b/src/ci/docker/webtest/entry_point.sh index 4c6c1974a..7daea6113 100644 --- a/src/ci/docker/webtest/entry_point.sh +++ b/src/ci/docker/webtest/entry_point.sh @@ -42,9 +42,11 @@ fi rm -f /tmp/.X*lock +export JAVA_OPTS +export SE_OPTS + xvfb-run -a -n $X_START_NUM --server-args="-screen 0 $GEOMETRY -ac +extension RANDR" \ - java ${JAVA_OPTS} -jar /opt/bin/selenium-server-standalone.jar \ - ${SE_OPTS} & + /opt/bin/x_run.sh & NODE_PID=$! trap shutdown SIGTERM SIGINT diff --git a/src/ci/docker/webtest/x_run.sh b/src/ci/docker/webtest/x_run.sh new file mode 100644 index 000000000..313e50511 --- /dev/null +++ b/src/ci/docker/webtest/x_run.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +nohup fluxbox >/dev/null 2>&1 & +sleep 3 +exec java ${JAVA_OPTS} -jar /opt/bin/selenium-server-standalone.jar ${SE_OPTS} \ No newline at end of file diff --git a/src/ci/scripts/container_webtest.sh b/src/ci/scripts/container_webtest.sh index eae366e50..65d4c15ec 100755 --- a/src/ci/scripts/container_webtest.sh +++ b/src/ci/scripts/container_webtest.sh @@ -22,16 +22,31 @@ # # Builds and runs a container # -# Tries to find the docker configuration in ../docker/${DOCKER_CFG} +# For consistent testing environment, it's the best to build the image locally and push it to +# the Docker hub repository. There exists one repository on Dockerhub: apachearchiva. +# The login data for the Dockerhub ID has to be given by the environment variable: DOCKER_HUB_PW +# +# The script first checks, if a image with the given CONTAINER_VERSION exists. If not, it tries to +# pull the image from Docker hub. If the pull fails, it tries to build a local one. +# +# If you have changes on the image, you should change CONTAINER_VERSION in this file, create it locally +# and push it to the Dockerhub repository: apachearchiva/build-webtest:${CONTAINER_VERSION} +# After that, push the changes to the git repository. +# +# The script tries to cleanup orphaned images from the local docker repository, if the CONTAINER_VERSION +# has changed. # # Always change the version, if your Dockerfile or scripts of the container change -CONTAINER_VERSION="1.0" +CONTAINER_VERSION="1.1" CONTAINER_NAME="archiva/selenium" DOCKER_CFG="webtest" INSTANCE_NAME="archiva-webtest" PORT_MAPPING="4444:4444" NETWORK_TYPE="host" +DOCKER_HUB_ID="apachearchiva" +DOCKER_HUB_REPO="${DOCKER_HUB_ID}/build-webtest" +DOCKER_HUB_TAG="${DOCKER_HUB_REPO}:${CONTAINER_VERSION}" # Using high screen resolution to avoid scrolling bar in browser export SCREEN_WIDTH="3840" @@ -40,11 +55,83 @@ export SCREEN_HEIGHT="2160" HERE=`dirname $0` TAG="${CONTAINER_NAME}:${CONTAINER_VERSION}" - -START_ARG="$1" +VERBOSE=1 +REMOVE_LOCALLY=1 +while [ ! -z "$1" ]; do + case "$1" in + -v) + VERBOSE=0 + ;; + -r) + REMOVE_LOCALLY=0 + ;; + *) + START_ARG=$1 + ;; + esac + shift +done docker -v +if [ -z "${DOCKER_HUB_PW}" ]; then + echo "WARNING: The docker hub password is not provided on the environment." +fi + + +function cleanup_orphaned() { + echo "Checking for orphaned images:" + while read IMG; do + echo "Removing ${IMG}" + docker rmi "${IMG}" + done < <(docker images "${CONTAINER_NAME}" | awk -vVER="${CONTAINER_VERSION}" '$2 !~ VER && $2 !~ /TAG/ { printf("%s:%s\n",$1,$2) }') + while read IMG; do + echo "Removing ${IMG}" + docker rmi "${IMG}" + done < <(docker images "${DOCKER_HUB_REPO}" | awk -vVER="${CONTAINER_VERSION}" '$2 !~ VER && $2 !~ /TAG/ { printf("%s:%s\n",$1,$2) }') +} + +cleanup() { + cleanup_orphaned + docker logout 1>/dev/null 2>&1 +} + +trap cleanup EXIT + +function build_image() { + echo "Building image ${TAG}" + DOCKER_DIR="${HERE}/../docker/${DOCKER_CFG}" + MY_PWD=`pwd` + cd ${DOCKER_DIR} || { echo "Could not change to docker directory ${DOCKER_CFG}"; exit 1; } + docker build --force-rm -t "${TAG}" . + if [ $? -ne 0 ]; then + cd ${MY_PWD} + echo "Could not build docker image" + exit 1 + fi + cd ${MY_PWD} + IMG_ID=`docker images -q ${TAG}` +} + +function get_image() { + IMG_ID=$(docker images -q "${DOCKER_HUB_TAG}") + if [ -z "${IMG_ID}" ]; then + if [ ! -z "${DOCKER_HUB_PW}" ]; then + echo "${DOCKER_HUB_PW}" | docker login --username "${DOCKER_HUB_ID}" --password-stdin + fi + docker pull "${DOCKER_HUB_TAG}" + docker logout + IMG_ID=$(docker images -q "${DOCKER_HUB_TAG}") + if [ -z "${IMG_ID}" ]; then + echo "Could not load docker image from remote. Trying to build a local one." + build_image + else + docker tag "${DOCKER_HUB_TAG}" "${TAG}" + fi + fi +} + + function stop_instance() { CONT=`docker ps -q --filter=name=${INSTANCE_NAME}` if [ "${CONT}" != "" ]; then @@ -66,40 +153,40 @@ function start_instance() { } function print_usage() { - echo "container_webtest start|stop" + echo "container_webtest [-v] [-r] start|stop" echo "Starts or stops the container. Builds the images if necessary" + echo " -v: Print verbose information about the docker container and environment." + echo " -r: Remove a local found image and try to re-pull the image" } echo "Date: $(date)" -docker ps -echo "netstat" -netstat -anp |grep 4444 +if [ $VERBOSE -eq 0 ]; then + docker ps + echo "netstat" + netstat -anp |grep 4444 +fi + if [ "${START_ARG}" == "start" ]; then - IMG=`docker images -q ${TAG}` - # Build the image, if it does not exist - if [ "${IMG}" == "" ]; then - echo "Building image ${TAG}" - DOCKER_DIR="${HERE}/../docker/${DOCKER_CFG}" - MY_PWD=`pwd` - cd ${DOCKER_DIR} || { echo "Could not change to docker directory ${DOCKER_CFG}"; exit 1; } - docker build --force-rm -t "${TAG}" . - if [ $? -ne 0 ]; then - cd ${MY_PWD} - echo "Could not build docker image" - exit 1 - fi - cd ${MY_PWD} - IMG=`docker images -q ${TAG}` - fi # Removing old instances stop_instance + IMG_ID=`docker images -q ${TAG}` + # Build the image, if it does not exist + if [ -z "${IMG_ID}" ]; then + get_image + elif [ $REMOVE_LOCALLY -eq 0 ]; then + docker rmi "${TAG}" 1>/dev/null 2>&1 + docker rmi "${DOCKER_HUB_TAG}" 1>/dev/null 2>&1 + get_image + fi # Starting start_instance if [ $? -ne 0 ]; then echo "Error from docker run" fi - docker ps + if [ $VERBOSE -eq 0 ]; then + docker ps + fi TIMEOUT=20 RES=1 while [ $RES -gt 0 -a $TIMEOUT -gt 0 ]; do @@ -108,14 +195,18 @@ if [ "${START_ARG}" == "start" ]; then docker logs "${INSTANCE_NAME}" | tail -5 | grep -q "Selenium Server is up and running" RES=$? done - docker logs "${INSTANCE_NAME}" - echo "netstat: " - netstat -anp |grep 4444 - echo "Trying curl on Webdriver port: " - curl "http://localhost:4444/wd/hub" - echo "Result: "$? + if [ $VERBOSE -eq 0 ]; then + docker logs "${INSTANCE_NAME}" + echo "netstat: " + netstat -anp |grep 4444 + echo "Trying curl on Webdriver port: " + curl "http://localhost:4444/wd/hub" + echo "Result: "$? + fi elif [ "${START_ARG}" == "stop" ]; then - docker logs "${INSTANCE_NAME}" + if [ $VERBOSE -eq 0 ]; then + docker logs "${INSTANCE_NAME}" + fi stop_instance else print_usage -- 2.39.5