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