blob: da6d38146117405951e90f7f5e1f6da6da060ae9 [file] [log] [blame]
Carlos Sanchez8cb67a02016-04-20 13:40:44 +02001#!/bin/bash
Thomas LEVEIL58695bb2015-09-04 21:51:10 +00002
3# check dependencies
4(
5 type docker &>/dev/null || ( echo "docker is not available"; exit 1 )
Vincent Latombebb8f8c92016-07-13 18:18:14 +02006 type unzip &>/dev/null || ( echo "unzip is not available"; exit 1 )
Thomas LEVEIL58695bb2015-09-04 21:51:10 +00007 type curl &>/dev/null || ( echo "curl is not available"; exit 1 )
8)>&2
9
10# Assert that $1 is the outputof a command $2
11function assert {
12 local expected_output=$1
13 shift
Carlos Sanchez8cb67a02016-04-20 13:40:44 +020014 local actual_output
15 actual_output=$("$@")
Carlos Sanchez2d68b192016-04-20 13:26:59 +020016 actual_output="${actual_output//[$'\t\r\n']}" # remove newlines
Thomas LEVEIL58695bb2015-09-04 21:51:10 +000017 if ! [ "$actual_output" = "$expected_output" ]; then
Carlos Sanchezc24ef822016-02-29 12:10:31 +010018 echo "expected: \"$expected_output\""
19 echo "actual: \"$actual_output\""
Thomas LEVEIL58695bb2015-09-04 21:51:10 +000020 false
21 fi
22}
23
24# Retry a command $1 times until it succeeds. Wait $2 seconds between retries.
25function retry {
26 local attempts=$1
27 shift
28 local delay=$1
29 shift
30 local i
31
32 for ((i=0; i < attempts; i++)); do
33 run "$@"
34 if [ "$status" -eq 0 ]; then
35 return 0
36 fi
37 sleep $delay
38 done
39
Carlos Sanchez8cb67a02016-04-20 13:40:44 +020040 echo "Command \"$*\" failed $attempts times. Status: $status. Output: $output" >&2
Thomas LEVEIL58695bb2015-09-04 21:51:10 +000041 false
42}
43
Carlos Sanchez59d9ef62016-11-09 13:08:41 +010044function docker_build {
45 if [ -n "$JENKINS_VERSION" ]; then
46 docker build --build-arg JENKINS_VERSION=$JENKINS_VERSION --build-arg JENKINS_SHA=$JENKINS_SHA "$@"
47 else
48 docker build "$@"
49 fi
50}
51
Thomas LEVEIL58695bb2015-09-04 21:51:10 +000052function get_jenkins_url {
Carlos Sanchezc24ef822016-02-29 12:10:31 +010053 if [ -z "${DOCKER_HOST}" ]; then
Carlos Sanchez758cd602016-05-02 11:57:04 -070054 DOCKER_IP=localhost
Carlos Sanchezd1f26652015-09-11 18:54:47 +020055 else
Carlos Sanchez8cb67a02016-04-20 13:40:44 +020056 DOCKER_IP=$(echo "$DOCKER_HOST" | sed -e 's|tcp://\(.*\):[0-9]*|\1|')
Carlos Sanchezd1f26652015-09-11 18:54:47 +020057 fi
Carlos Sanchez8cb67a02016-04-20 13:40:44 +020058 echo "http://$DOCKER_IP:$(docker port "$SUT_CONTAINER" 8080 | cut -d: -f2)"
Thomas LEVEIL58695bb2015-09-04 21:51:10 +000059}
60
Carlos Sanchez5a51d0a2016-04-21 13:50:25 +020061function get_jenkins_password {
62 docker logs "$SUT_CONTAINER" 2>&1 | grep -A 2 "Please use the following password to proceed to installation" | tail -n 1
63}
64
Thomas LEVEIL58695bb2015-09-04 21:51:10 +000065function test_url {
Carlos Sanchez5a51d0a2016-04-21 13:50:25 +020066 run curl --user "admin:$(get_jenkins_password)" --output /dev/null --silent --head --fail --connect-timeout 30 --max-time 60 "$(get_jenkins_url)$1"
Thomas LEVEIL58695bb2015-09-04 21:51:10 +000067 if [ "$status" -eq 0 ]; then
68 true
69 else
70 echo "URL $(get_jenkins_url)$1 failed" >&2
71 echo "output: $output" >&2
72 false
73 fi
74}
Carlos Sanchez5fc9cd02016-02-28 15:25:18 +010075
76function cleanup {
Carlos Sanchez8cb67a02016-04-20 13:40:44 +020077 docker kill "$1" &>/dev/null ||:
78 docker rm -fv "$1" &>/dev/null ||:
Carlos Sanchez5fc9cd02016-02-28 15:25:18 +010079}