summaryrefslogtreecommitdiff
path: root/slaves/linux/build-wrapper
blob: fd04202dc44f8c6775ca67d7061e59e461b4479b (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
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash

# wrapper to run a jenkins job in a specific chroot.
#
# SUITE and ARCHITECTURE are passed in the environment
#
#
# SUITE is passed on to the scripts run in the chroot.
# Extra environment variables can be passed on by specifying their names on the command line.
#
#  - scripts to be run life in directories named after their jobname.
#  - in those dirs, the setup script or directory will be run outside the
#    schroot.  The SESSION_ID variable has the name of the session we just
#    created.
#  - the build script/directory will be run as an unprivileged user inside
#    the session.
#  - If the item in question is a directory, run-parts will be used on it.

set -u
set -e
set -x

fullpath=$(readlink -f "$0")
basedir=$(dirname "$fullpath")
. "$basedir/common.sh"

usage()
{
cat << EOF
usage: $0 [-t <sync-to-remote> [-t...]] [-f <sync-from-remote> [-f...]] [-k <env-variable> [-k]]
  -k <NAME>     keeps environment variable NAME when calling chroot session
  -t/-f <PATH>  sync PATH to/from remote when building remotely
EOF
}
declare -a sync_args
declare -a keep_env_names
while getopts "t:f:k:" OPTION; do
	case "$OPTION" in
		t)
			check_arg "sync-to-remote argument" "$OPTARG"
			sync_args+=("-t" "$OPTARG")
			;;
		f)
			check_arg "sync-from-remote argument" "$OPTARG"
			sync_args+=("-f" "$OPTARG")
			;;
		k)
			check_arg "keep-env argument" "$OPTARG"
			keep_env_names+=("$OPTARG")
			;;
		*)
			echo >&2 "Invalid option $OPTION"
			exit 1
	esac
done
shift $(($OPTIND - 1))
if [ "$#" -ge 1 ] ; then
	echo >&2 "Invalid arguments: $*"
	exit 1
fi

init_args
if [ "$(hostname -f)" != "${NODE_NAME:-}" ] ; then
  relay_to_remote "${sync_args[@]}" "slaves/linux/build-wrapper"
fi

echo "=== [build-wrapper] begin ==="

reboot_lock=/var/run/reboot-lock
exec 201< "$reboot_lock"
if flock --shared -w 0 201 ; then
    echo "Acquired reboot lock file: $reboot_lock."
else
    echo >&2 "Failed to acquire reboot lock file: $reboot_lock."
    exit 1
fi

# Check if we have an schroot by that name
base_chroot="$SUITE-$ARCHITECTURE-sbuild"
if ! schroot -l -c "$base_chroot" > /dev/null 2>&1; then
	echo >&2 "Invalid chroot: $base_chroot."
	exit 1
fi

if [ -n "${buildscriptdir:-}" ]; then
  jobdir="$basedir/$buildscriptdir"
else
  jobdir="$basedir/$jobname"
fi

# And check if we have build scripts
if ! [ -d "$jobdir" ]; then
	echo >&2 "$jobdir does not exist or is not a directory."
	exit 1
fi


# Setting up the build environment
trap 'cleanup' EXIT
echo "Prepare build environment."
chroot=$(schroot --chroot "$base_chroot" --begin-session)
if [ -z "$chroot" ]; then
	echo >&2 "Setting up chroot failed."
	exit 1
fi

# pass extra environment
declare -a keep_env
keep_env+=("SESSION_ID=$chroot")
keep_env_names+=("jobname")
keep_env_names+=("SUITE")
if [ -n "${GIT_BRANCH:-}" ]; then
	keep_env_names+=("GIT_BRANCH")
fi
for i in "${keep_env_names[@]}"; do
	keep_env+=("$i=${!i}")
done

#echo "Full env:"
#env

echo "Setup:"
f="$jobdir/setup"
if [ -d "$f" ]; then env "${keep_env[@]}" run-parts "$f";
                else env "${keep_env[@]}" "$f"; fi
echo "Build:"
keep_env+=("PATH=/usr/lib/ccache:/usr/local/bin:/usr/bin:/bin")
f="$jobdir/build"

echo "=== [build-wrapper] start actual build ==="

set +e
(
  set -e
  if [ -d "$f" ]; then schroot --run-session --chroot "$chroot"         -- env "${keep_env[@]}" run-parts "$f";
                  else schroot --run-session --chroot "$chroot"         -- env "${keep_env[@]}" "$f"; fi
)
rc="$?"

echo "=== [build-wrapper] end ==="
exit "$rc"