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

# Start a local schroot 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 cleanup statement, to remove the container on exit for instance
# - 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

usage() {
    echo "Usage: $0 --arch container-arch --distro flavour [--session-host host] [--session-name name] [--task {build|test}]"
    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)"
    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"

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
	    ;;
	*)
	    echo "Unsupported option: $1"
	    usage
	    ;;
    esac
done

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

[ 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
schroot_image="tcwg-${task}-${container_arch}-${distro}"
SCHROOT="ssh -p ${session_port} -A ${session_host} schroot"
session_id=$(${SCHROOT} -b -c chroot:$schroot_image --preserve-environment)

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

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

cat <<EOF
ulimit -u 5000 # FIXME how to support this remotely?
# Sometimes /dev/pts can't get unmounted on the first try.
# Workaround by retrying.
CONTAINER="${SCHROOT} -r -c session:$session_id --preserve-environment -- "
CONTAINER_CLEANUP="${SCHROOT} -f -e -c session:$session_id"
session_host=${session_host}
session_port=${session_port}
EOF