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