summaryrefslogtreecommitdiff
path: root/start-container-none.sh
blob: 1c66d061e9ac7b6a94f8f7cc58992989dae31971 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash
set -e

# Pretend to start a local container. Parameters are ignored but
# required to have the same interface as the other scripts in this
# directory. We check if arch and distro match the local machine's
# values.

# 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:
# - definition of ${CONTAINER}, used to prefix commands that you want
#   to run inside the container.
# - definition of ${CONTAINER_CLEANUP}, a cleanup statement remove the
#   container on exit for instance
# - definition of ${session_host} and ${session_port}, can be used for
#   a remote connexion to the container
usage() {
    echo "Usage: $0 --arch container-arch --distro flavour [--session-host host] [--session-name name] [--task {build|test}]"
    echo "Usage: $0 --arch container-arch --distro flavour [--session-host host] [--session-name name] [--task {build|test}] [--weight weight]"
    echo
    echo "  container-arch: architecture (eg: amd64, i386, arm64, armhf)"
    echo "  distro: distribution (eg: trusty)"
    echo "  session-host: hostname where the container will run, defaults to localhost"
    echo "    useful if the name resolution does not work correctly"
    echo "  session-name: session, in case the default '$BUILD_NUMBER-$JOB_NAME' is not suitable"
    echo "  task: type of container (build or test, default=build)"
    echo "  weight: container weight, reserves resources. Default=1"
    exit 1
}

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

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

container_arch=
distro=
session_host=
session_name=
task="build"
weight=1

while [ $# -ge 1 ]
do
    case $1 in
	--arch)
	    container_arch=$2
	    [ x${container_arch} = x ] && usage
	    shift 2
	    ;;
	--distro)
	    distro=$2
	    [ x${distro} = x ] && usage
	    shift 2
	    ;;
	--session-host)
	    session_host=$2
	    [ x${session_host} = x ] && usage
	    shift 2
	    ;;
	--session-name)
	    session_name=$2
	    [ x${session_name} = x ] && usage
	    shift 2
	    ;;
	--task)
	    task=$2
	    [ x${task} = x ] && usage
	    shift 2
	    ;;
	--weight)
	    weight=$2
	    [ x${weight} = x ] && usage
	    shift 2
	    ;;
	*)
	    echo "Unsupported option: $1"
	    usage
	    ;;
    esac
done

[ x${container_arch} = x ] && usage
[ x${distro} = x ] && usage

case $(uname -m) in
    x86_64) my_arch=amd64 ;;
    i686) my_arch=i386 ;;
    aarch64) my_arch=arm64 ;;
    armv7l|armv8l) my_arch=armhf ;;
    *) my_arch=$(uname -m) ;;
esac

if [ x${container_arch} != x${my_arch} ]; then
    echo "Error: requested container arch (${container_arch}) does not match this machine's arch (${my_arch})"
    exit 1
fi

my_distro=$(lsb_release -c | awk '{print $2;}')
if [ x${distro} != x${my_distro} ]; then
    echo "Error: requested distro (${distro}) does not match this machine's distro (${my_distro})"
    exit 1
fi

if [ x"$DOCKER_HOST" = x"" ]; then
    [ x"$session_host" = x ] && session_host=localhost
    # Append .tcwglab if not already present
    case ${session_host} in
	*.tcwglab) ;;
	*) session_host=${session_host}.tcwglab
    esac
    session_port=22
else
    session_host="$(echo $DOCKER_HOST | sed -e "s#^tcp://\(.*\):.*#\1#")"
    session_port="$(ssh $session_host docker port $DOCKER_CONTAINER_ID 22)"
    session_port="$(echo $session_port | cut -d: -f 2)"
fi

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

cat <<EOF
ulimit -u 5000 # FIXME: Does not work remotely
CONTAINER="ssh -p ${session_port} ${session_host}"
CONTAINER_CLEANUP=""
session_host=${session_host}
session_port=${session_port}
EOF