Merge branch '15615-retry-wb1-tests'
[arvados.git] / build / run-build-packages-one-target.sh
1 #!/bin/bash
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: AGPL-3.0
5
6 read -rd "\000" helpmessage <<EOF
7 $(basename $0): Orchestrate run-build-packages.sh for one target
8
9 Syntax:
10         WORKSPACE=/path/to/arvados $(basename $0) [options]
11
12 --target <target>
13     Distribution to build packages for (default: debian9)
14 --command
15     Build command to execute (default: use built-in Docker image command)
16 --test-packages
17     Run package install test script "test-packages-[target].sh"
18 --debug
19     Output debug information (default: false)
20 --only-build <package>
21     Build only a specific package
22 --only-test <package>
23     Test only a specific package
24 --force-test
25     Test even if there is no new untested package
26 --build-version <string>
27     Version to build (default:
28     \$ARVADOS_BUILDING_VERSION-\$ARVADOS_BUILDING_ITERATION or
29     0.1.timestamp.commithash)
30
31 WORKSPACE=path         Path to the Arvados source tree to build packages from
32
33 EOF
34
35 set -e
36
37 if ! [[ -n "$WORKSPACE" ]]; then
38   echo >&2 "$helpmessage"
39   echo >&2
40   echo >&2 "Error: WORKSPACE environment variable not set"
41   echo >&2
42   exit 1
43 fi
44
45 if ! [[ -d "$WORKSPACE" ]]; then
46   echo >&2 "$helpmessage"
47   echo >&2
48   echo >&2 "Error: $WORKSPACE is not a directory"
49   echo >&2
50   exit 1
51 fi
52
53 PARSEDOPTS=$(getopt --name "$0" --longoptions \
54     help,debug,test-packages,target:,command:,only-test:,force-test,only-build:,build-version: \
55     -- "" "$@")
56 if [ $? -ne 0 ]; then
57     exit 1
58 fi
59
60 TARGET=debian9
61 COMMAND=
62 DEBUG=
63
64 eval set -- "$PARSEDOPTS"
65 while [ $# -gt 0 ]; do
66     case "$1" in
67         --help)
68             echo >&2 "$helpmessage"
69             echo >&2
70             exit 1
71             ;;
72         --target)
73             TARGET="$2"; shift
74             ;;
75         --only-test)
76             test_packages=1
77             testing_one_package=1
78             packages="$2"; shift
79             ;;
80         --force-test)
81             FORCE_TEST=true
82             ;;
83         --only-build)
84             ONLY_BUILD="$2"; shift
85             ;;
86         --debug)
87             DEBUG=" --debug"
88             ARVADOS_DEBUG="1"
89             ;;
90         --command)
91             COMMAND="$2"; shift
92             ;;
93         --test-packages)
94             test_packages=1
95             ;;
96         --build-version)
97             if [[ -z "$2" ]]; then
98                 :
99             elif ! [[ "$2" =~ (.*)-(.*) ]]; then
100                 echo >&2 "FATAL: --build-version '$2' does not include an iteration. Try '${2}-1'?"
101                 exit 1
102             else
103                 ARVADOS_BUILDING_VERSION="${BASH_REMATCH[1]}"
104                 ARVADOS_BUILDING_ITERATION="${BASH_REMATCH[2]}"
105             fi
106             shift
107             ;;
108         --)
109             if [ $# -gt 1 ]; then
110                 echo >&2 "$0: unrecognized argument '$2'. Try: $0 --help"
111                 exit 1
112             fi
113             ;;
114     esac
115     shift
116 done
117
118 set -e
119
120 if [[ -n "$ARVADOS_BUILDING_VERSION" ]]; then
121     echo "build version='$ARVADOS_BUILDING_VERSION', package iteration='$ARVADOS_BUILDING_ITERATION'"
122 fi
123
124 if [[ -n "$test_packages" ]]; then
125   if [[ -n "$(find $WORKSPACE/packages/$TARGET -name '*.rpm')" ]] ; then
126     set +e
127     /usr/bin/which createrepo >/dev/null
128     if [[ "$?" != "0" ]]; then
129       echo >&2
130       echo >&2 "Error: please install createrepo. E.g. sudo apt-get install createrepo"
131       echo >&2
132       exit 1
133     fi
134     set -e
135     createrepo $WORKSPACE/packages/$TARGET
136   fi
137
138   if [[ -n "$(find $WORKSPACE/packages/$TARGET -name '*.deb')" ]] ; then
139     (cd $WORKSPACE/packages/$TARGET
140       dpkg-scanpackages .  2> >(grep -v 'warning' 1>&2) | tee Packages | gzip -c > Packages.gz
141       apt-ftparchive -o APT::FTPArchive::Release::Origin=Arvados release . > Release
142     )
143   fi
144
145   COMMAND="/jenkins/package-testing/test-packages-$TARGET.sh"
146   IMAGE="arvados/package-test:$TARGET"
147 else
148   IMAGE="arvados/build:$TARGET"
149   if [[ "$COMMAND" != "" ]]; then
150     COMMAND="/usr/local/rvm/bin/rvm-exec default bash /jenkins/$COMMAND --target $TARGET$DEBUG"
151   fi
152 fi
153
154 JENKINS_DIR=$(dirname "$(readlink -e "$0")")
155
156 if [[ -n "$test_packages" ]]; then
157     pushd "$JENKINS_DIR/package-test-dockerfiles"
158     make "$TARGET/generated"
159 else
160     pushd "$JENKINS_DIR/package-build-dockerfiles"
161     make "$TARGET/generated"
162 fi
163
164 echo $TARGET
165 cd $TARGET
166 time docker build --tag=$IMAGE .
167 popd
168
169 if test -z "$packages" ; then
170     packages="arvados-api-server
171         arvados-client
172         arvados-docker-cleaner
173         arvados-git-httpd
174         arvados-node-manager
175         arvados-src
176         arvados-workbench
177         crunch-dispatch-local
178         crunch-dispatch-slurm
179         crunch-run
180         crunchstat
181         keep-balance
182         keep-block-check
183         keepproxy
184         keep-rsync
185         keepstore
186         keep-web
187         libarvados-perl
188         python-arvados-fuse
189         python-arvados-python-client
190         python-arvados-cwl-runner"
191 fi
192
193 FINAL_EXITCODE=0
194
195 package_fails=""
196
197 mkdir -p "$WORKSPACE/apps/workbench/vendor/cache-$TARGET"
198 mkdir -p "$WORKSPACE/services/api/vendor/cache-$TARGET"
199
200 docker_volume_args=(
201     -v "$JENKINS_DIR:/jenkins"
202     -v "$WORKSPACE:/arvados"
203     -v /arvados/services/api/vendor/bundle
204     -v /arvados/apps/workbench/vendor/bundle
205     -v "$WORKSPACE/services/api/vendor/cache-$TARGET:/arvados/services/api/vendor/cache"
206     -v "$WORKSPACE/apps/workbench/vendor/cache-$TARGET:/arvados/apps/workbench/vendor/cache"
207 )
208
209 if [[ -n "$test_packages" ]]; then
210     for p in $packages ; do
211         if [[ -n "$ONLY_BUILD" ]] && [[ "$p" != "$ONLY_BUILD" ]]; then
212             continue
213         fi
214         if [[ -e "${WORKSPACE}/packages/.last_test_${TARGET}" ]] && [[ -z "$FORCE_TEST" ]]; then
215           MATCH=`find ${WORKSPACE}/packages/ -newer ${WORKSPACE}/packages/.last_test_${TARGET} -regex .*${TARGET}/$p.*`
216           if [[ "$MATCH" == "" ]]; then
217             # No new package has been built that needs testing
218             echo "Skipping $p test because no new package was built since the last test."
219             continue
220           fi
221         fi
222         # If we're testing all packages, we should not error out on packages that don't exist.
223         # If we are testing one specific package only (i.e. --only-test was given), we should
224         # error out if that package does not exist.
225         if [[ -z "$testing_one_package" ]]; then
226           MATCH=`find ${WORKSPACE}/packages/ -regextype posix-extended -regex .*${TARGET}/$p.*\\(deb\\|rpm\\)`
227           if [[ "$MATCH" == "" ]]; then
228             # No new package has been built that needs testing
229             echo "Skipping $p test because no package file is available to test."
230             continue
231           fi
232         fi
233         echo
234         echo "START: $p test on $IMAGE" >&2
235         if docker run \
236             --rm \
237             "${docker_volume_args[@]}" \
238             --env ARVADOS_DEBUG=$ARVADOS_DEBUG \
239             --env "TARGET=$TARGET" \
240             --env "WORKSPACE=/arvados" \
241             "$IMAGE" $COMMAND $p
242         then
243             echo "OK: $p test on $IMAGE succeeded" >&2
244         else
245             FINAL_EXITCODE=$?
246             package_fails="$package_fails $p"
247             echo "ERROR: $p test on $IMAGE failed with exit status $FINAL_EXITCODE" >&2
248         fi
249     done
250
251     if [[ "$FINAL_EXITCODE" == "0" ]]; then
252       touch ${WORKSPACE}/packages/.last_test_${TARGET}
253     fi
254 else
255     echo
256     echo "START: build packages on $IMAGE" >&2
257     # Move existing packages and other files into the processed/ subdirectory
258     if [[ ! -e "${WORKSPACE}/packages/${TARGET}/processed" ]]; then
259       mkdir -p "${WORKSPACE}/packages/${TARGET}/processed"
260     fi
261     set +e
262     mv -f ${WORKSPACE}/packages/${TARGET}/* ${WORKSPACE}/packages/${TARGET}/processed/ 2>/dev/null
263     set -e
264     # Build packages.
265     if docker run \
266         --rm \
267         "${docker_volume_args[@]}" \
268         --env ARVADOS_BUILDING_VERSION="$ARVADOS_BUILDING_VERSION" \
269         --env ARVADOS_BUILDING_ITERATION="$ARVADOS_BUILDING_ITERATION" \
270         --env ARVADOS_DEBUG=$ARVADOS_DEBUG \
271         --env "ONLY_BUILD=$ONLY_BUILD" \
272         "$IMAGE" $COMMAND
273     then
274         echo
275         echo "OK: build packages on $IMAGE succeeded" >&2
276     else
277         FINAL_EXITCODE=$?
278         echo "ERROR: build packages on $IMAGE failed with exit status $FINAL_EXITCODE" >&2
279     fi
280 fi
281
282 if test -n "$package_fails" ; then
283     echo "Failed package tests:$package_fails" >&2
284 fi
285
286 exit $FINAL_EXITCODE