summaryrefslogtreecommitdiff
path: root/start-container-docker.sh
blob: a8f5bbeabacfc5ad8070f3d72f439f16e6c099af (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
114
#!/bin/bash
set -e

# Start a local docker instance with the requested arch and distro

# This script is meant to be executed from Jenkins jobs inside TCWG
# lab. It prints shell commands meant to be executed in the parent
# shell, consisting in:
# - a trap statement, to cleanup the container upon exit
# - definition of ${CONTAINER}, used to prefix commands that you want
#   to run inside the container.
# - definition of ${session_host} and ${session_port}, can be used for
#   a remote connexion to the container

if [ $# -ne 2 -a $# -ne 3 ]; then
    echo Usage: $0 arch flavour [host]
    echo "  arch: architecture (eg: amd64, i386, arm64, armhf)"
    echo "  flavour: distribution (eg: trusty)"
    echo "  host: hostname where the container will run, defaults to localhost"
    echo "        useful if the name resolution does not work correctly"
    exit 1
fi

# BUILD_NUMBER, JOB_NAME and WORKSPACE are set by Jenkins
if [ "x$BUILD_NUMBER" = "x" ]; then
    echo "BUILD_NUMBER is not defined. Are you executing this from Jenkins ?"
    exit 1
fi
if [ "x$JOB_NAME" = "x" ]; then
    echo "JOB_NAME is not defined. Are you executing this from Jenkins ?"
    exit 1
fi
if [ "x$WORKSPACE" = "x" ]; then
    echo "WORKSPACE is not defined. Are you executing this from Jenkins ?"
    exit 1
fi

container_arch=$1
distro=$2
session_host=$3
[ x"$session_host" = x ] && session_host=localhost
task="build"

# Save stdout/stderr file descriptors
exec 3>&1 4>&2

# Make sure all output goes to stderr
exec 1>&2

case "${distro}:${task}" in
    "trusty":"build")
	image=linaro/ci-${container_arch}-tcwg-ubuntu:${distro}
	;;
    *)
	image=linaro/ci-${container_arch}-tcwg-${task}-ubuntu:${distro}
	;;
esac

DOCKER="docker -H $session_host:2375"
$DOCKER pull $image
session_name=$(echo $BUILD_NUMBER-$JOB_NAME | sed -e "s#[/=,]#-#g")
session_id=$($DOCKER run --name $session_name -dtP \
		 -v $HOME/snapshots-ref:$HOME/snapshots-ref:ro \
		 -v $WORKSPACE:$WORKSPACE \
		 --memory=7500M --pids-limit=5000 \
		 $image)

if [ x"$session_id" = x ]; then
    exit 1
fi

# Remove the docker instance we have just created in case something
# goes wrong.
trap "$DOCKER rm -fv $session_id ; exec 1>&3 2>&4" EXIT

session_port=$($DOCKER port $session_id 22 | cut -d: -f 2)

rundir="$(pwd -P)"
mydir="$(dirname $0)"
cd "${mydir}"
mydir="$(pwd)"
cd ${rundir}

# Wait until the ssh server is ready to serve connexions

# Make sure connexion messages go to stderr, so that in case of
# success stdout contains only the connexion info expected by the
# caller.
count=20
while [ $count -gt 0 ]
do
    sleep 5
    expect ${mydir}/check-server.exp ${session_host} ${session_port} && break
    echo SSH not ready, waiting.....
    count=$((count - 1))
done

if [ $count -eq 0 ]; then
    echo SSH server did not respond, exiting
    exit 1
fi

# Do not remove the container upon exit: it is now ready
trap EXIT

# Restore stdout/stderr
exec 1>&3 2>&4

cat <<EOF
trap "$DOCKER rm -fv $session_id" EXIT
CONTAINER="ssh -p $session_port -A $session_host"
session_host=${session_host}
session_port=${session_port}
EOF