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