14198: Support expressions in TargetCluster[clusterID, ownerUUID]
[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: debian8)
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=debian8
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             packages="$2"; shift
78             ;;
79         --force-test)
80             FORCE_TEST=true
81             ;;
82         --only-build)
83             ONLY_BUILD="$2"; shift
84             ;;
85         --debug)
86             DEBUG=" --debug"
87             ARVADOS_DEBUG="1"
88             ;;
89         --command)
90             COMMAND="$2"; shift
91             ;;
92         --test-packages)
93             test_packages=1
94             ;;
95         --build-version)
96             if [[ -z "$2" ]]; then
97                 :
98             elif ! [[ "$2" =~ (.*)-(.*) ]]; then
99                 echo >&2 "FATAL: --build-version '$2' does not include an iteration. Try '${2}-1'?"
100                 exit 1
101             else
102                 ARVADOS_BUILDING_VERSION="${BASH_REMATCH[1]}"
103                 ARVADOS_BUILDING_ITERATION="${BASH_REMATCH[2]}"
104             fi
105             shift
106             ;;
107         --)
108             if [ $# -gt 1 ]; then
109                 echo >&2 "$0: unrecognized argument '$2'. Try: $0 --help"
110                 exit 1
111             fi
112             ;;
113     esac
114     shift
115 done
116
117 set -e
118
119 if [[ -n "$ARVADOS_BUILDING_VERSION" ]]; then
120     echo "build version='$ARVADOS_BUILDING_VERSION', package iteration='$ARVADOS_BUILDING_ITERATION'"
121 fi
122
123 if [[ -n "$test_packages" ]]; then
124     if [[ -n "$(find $WORKSPACE/packages/$TARGET -name '*.rpm')" ]] ; then
125         set +e
126         /usr/bin/which createrepo >/dev/null
127         if [[ "$?" != "0" ]]; then
128                 echo >&2
129                 echo >&2 "Error: please install createrepo. E.g. sudo apt-get install createrepo"
130                 echo >&2
131                 exit 1
132         fi
133         set -e
134         createrepo $WORKSPACE/packages/$TARGET
135     fi
136
137     if [[ -n "$(find $WORKSPACE/packages/$TARGET -name '*.deb')" ]] ; then
138         (cd $WORKSPACE/packages/$TARGET
139           dpkg-scanpackages .  2> >(grep -v 'warning' 1>&2) | tee Packages | gzip -c > Packages.gz
140           apt-ftparchive -o APT::FTPArchive::Release::Origin=Arvados release . > Release
141         )
142     fi
143
144     COMMAND="/jenkins/package-testing/test-packages-$TARGET.sh"
145     IMAGE="arvados/package-test:$TARGET"
146 else
147     IMAGE="arvados/build:$TARGET"
148     if [[ "$COMMAND" != "" ]]; then
149         COMMAND="/usr/local/rvm/bin/rvm-exec default bash /jenkins/$COMMAND --target $TARGET$DEBUG"
150     fi
151 fi
152
153 JENKINS_DIR=$(dirname "$(readlink -e "$0")")
154
155 if [[ -n "$test_packages" ]]; then
156     pushd "$JENKINS_DIR/package-test-dockerfiles"
157     make "$TARGET/generated"
158 else
159     pushd "$JENKINS_DIR/package-build-dockerfiles"
160     make "$TARGET/generated"
161 fi
162
163 echo $TARGET
164 cd $TARGET
165 time docker build --tag=$IMAGE .
166 popd
167
168 if test -z "$packages" ; then
169     packages="arvados-api-server
170         arvados-client
171         arvados-docker-cleaner
172         arvados-git-httpd
173         arvados-node-manager
174         arvados-src
175         arvados-workbench
176         crunch-dispatch-local
177         crunch-dispatch-slurm
178         crunch-run
179         crunchstat
180         keep-balance
181         keep-block-check
182         keepproxy
183         keep-rsync
184         keepstore
185         keep-web
186         libarvados-perl
187         python-arvados-fuse
188         python-arvados-python-client
189         python-arvados-cwl-runner"
190 fi
191
192 FINAL_EXITCODE=0
193
194 package_fails=""
195
196 mkdir -p "$WORKSPACE/apps/workbench/vendor/cache-$TARGET"
197 mkdir -p "$WORKSPACE/services/api/vendor/cache-$TARGET"
198
199 docker_volume_args=(
200     -v "$JENKINS_DIR:/jenkins"
201     -v "$WORKSPACE:/arvados"
202     -v /arvados/services/api/vendor/bundle
203     -v /arvados/apps/workbench/vendor/bundle
204     -v "$WORKSPACE/services/api/vendor/cache-$TARGET:/arvados/services/api/vendor/cache"
205     -v "$WORKSPACE/apps/workbench/vendor/cache-$TARGET:/arvados/apps/workbench/vendor/cache"
206 )
207
208 if [[ -n "$test_packages" ]]; then
209     for p in $packages ; do
210         if [[ -n "$ONLY_BUILD" ]] && [[ "$p" != "$ONLY_BUILD" ]]; then
211             continue
212         fi
213         if [[ -e "${WORKSPACE}/packages/.last_test_${TARGET}" ]] && [[ -z "$FORCE_TEST" ]]; then
214           MATCH=`find ${WORKSPACE}/packages/ -newer ${WORKSPACE}/packages/.last_test_${TARGET} -regex .*${TARGET}/$p.*`
215           if [[ "$MATCH" == "" ]]; then
216             # No new package has been built that needs testing
217             echo "Skipping $p test because no new package was built since the last test."
218             continue
219           fi
220         fi
221         echo
222         echo "START: $p test on $IMAGE" >&2
223         # ulimit option can be removed when debian8 and ubuntu1404 are retired
224         if docker run --ulimit nofile=4096:4096 \
225             --rm \
226             "${docker_volume_args[@]}" \
227             --env ARVADOS_DEBUG=$ARVADOS_DEBUG \
228             --env "TARGET=$TARGET" \
229             --env "WORKSPACE=/arvados" \
230             "$IMAGE" $COMMAND $p
231         then
232             echo "OK: $p test on $IMAGE succeeded" >&2
233         else
234             FINAL_EXITCODE=$?
235             package_fails="$package_fails $p"
236             echo "ERROR: $p test on $IMAGE failed with exit status $FINAL_EXITCODE" >&2
237         fi
238     done
239
240     touch ${WORKSPACE}/packages/.last_test_${TARGET}
241 else
242     echo
243     echo "START: build packages on $IMAGE" >&2
244     # Move existing packages and other files into the processed/ subdirectory
245     if [[ ! -e "${WORKSPACE}/packages/${TARGET}/processed" ]]; then
246       mkdir -p "${WORKSPACE}/packages/${TARGET}/processed"
247     fi
248     set +e
249     mv -f ${WORKSPACE}/packages/${TARGET}/* ${WORKSPACE}/packages/${TARGET}/processed/ 2>/dev/null
250     set -e
251     # Build packages. ulimit option can be removed when debian8 and ubuntu1404 are retired
252     if docker run --ulimit nofile=4096:4096 \
253         --rm \
254         "${docker_volume_args[@]}" \
255         --env ARVADOS_BUILDING_VERSION="$ARVADOS_BUILDING_VERSION" \
256         --env ARVADOS_BUILDING_ITERATION="$ARVADOS_BUILDING_ITERATION" \
257         --env ARVADOS_DEBUG=$ARVADOS_DEBUG \
258         --env "ONLY_BUILD=$ONLY_BUILD" \
259         "$IMAGE" $COMMAND
260     then
261         echo
262         echo "OK: build packages on $IMAGE succeeded" >&2
263     else
264         FINAL_EXITCODE=$?
265         echo "ERROR: build packages on $IMAGE failed with exit status $FINAL_EXITCODE" >&2
266     fi
267 fi
268
269 if test -n "$package_fails" ; then
270     echo "Failed package tests:$package_fails" >&2
271 fi
272
273 exit $FINAL_EXITCODE