aboutsummaryrefslogtreecommitdiffstats
path: root/src/ci/scripts/container_webtest.sh
blob: 90dbc45beab2f25685625cb99b433abd7370bd2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env bash
#
#  Licensed to the Apache Software Foundation (ASF) under one
#  or more contributor license agreements.  See the NOTICE file
#  distributed with this work for additional information
#  regarding copyright ownership.  The ASF licenses this file
#  to you under the Apache License, Version 2.0 (the
#  "License"); you may not use this file except in compliance
#  with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing,
#  software distributed under the License is distributed on an
#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#  KIND, either express or implied.  See the License for the
#  specific language governing permissions and limitations
#  under the License.
#
#  Author: Martin Stockhammer <martin_s@apache.org>
#  Date:   2017-04-16
#
#  Builds and runs a container
#
#  Tries to find the docker configuration in ../docker/${DOCKER_CFG}
#

# Always change the version, if your Dockerfile or scripts of the container change
CONTAINER_VERSION="1.0"
CONTAINER_NAME="archiva/selenium"
DOCKER_CFG="webtest"
INSTANCE_NAME="archiva-webtest"
PORT_MAPPING="4444:4444"
NETWORK_TYPE="host"

# Using high screen resolution to avoid scrolling bar in browser
export SCREEN_WIDTH="3840"
export SCREEN_HEIGHT="2160"

HERE=`dirname $0`

TAG="${CONTAINER_NAME}:${CONTAINER_VERSION}"

START_ARG="$1"

docker -v

function stop_instance() {
  CONT=`docker ps -q --filter=name=${INSTANCE_NAME}`
  if [ "${CONT}" != "" ]; then
    echo "Stopping container ${INSTANCE_NAME}"
    docker stop "${INSTANCE_NAME}"
  fi
  # We remove the instance always
  CONT=`docker ps -a -q --filter=name=${INSTANCE_NAME}`
  if [ "${CONT}" != "" ]; then
    echo "Removing container ${INSTANCE_NAME}"
    docker rm "${INSTANCE_NAME}"
  fi
}

function start_instance() {
  echo "Starting container ${INSTANCE_NAME}"
  echo "ARGS: -d -e SCREEN_WIDTH=${SCREEN_WIDTH} -e SCREEN_HEIGHT=${SCREEN_HEIGHT} --net=${NETWORK_TYPE} -p ${PORT_MAPPING} --name ${INSTANCE_NAME} ${TAG}"
  docker run -d -e "SCREEN_WIDTH=${SCREEN_WIDTH}" -e "SCREEN_HEIGHT=${SCREEN_HEIGHT}" --net="${NETWORK_TYPE}" -p "${PORT_MAPPING}" --name "${INSTANCE_NAME}" "${TAG}"
}

function print_usage() {
  echo "container_webtest start|stop"
  echo "Starts or stops the container. Builds the images if necessary"
}

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
  # Starting
  start_instance
  if [ $? -ne 0 ]; then
    echo "Error from docker run"
  fi
  docker ps
  TIMEOUT=20
  RES=1
  while [ $RES -gt 0 -a $TIMEOUT -gt 0 ]; do
    sleep 0.2
    TIMEOUT=$((TIMEOUT-1))
    docker logs "${INSTANCE_NAME}" | tail -5 |  grep -q "Selenium Server is up and running"
    RES=$?
  done
  docker logs "${INSTANCE_NAME}"
elif [ "${START_ARG}" == "stop" ]; then
  docker logs "${INSTANCE_NAME}"
  stop_instance
else
  print_usage
fi