Merge branch 'master' of git.curoverse.com:arvados into 11876-r-sdk
[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 --build-version <string>
25     Version to build (default: \$ARVADOS_BUILDING_VERSION or 0.1.timestamp.commithash)
26
27 WORKSPACE=path         Path to the Arvados source tree to build packages from
28
29 EOF
30
31 set -e
32
33 if ! [[ -n "$WORKSPACE" ]]; then
34   echo >&2 "$helpmessage"
35   echo >&2
36   echo >&2 "Error: WORKSPACE environment variable not set"
37   echo >&2
38   exit 1
39 fi
40
41 if ! [[ -d "$WORKSPACE" ]]; then
42   echo >&2 "$helpmessage"
43   echo >&2
44   echo >&2 "Error: $WORKSPACE is not a directory"
45   echo >&2
46   exit 1
47 fi
48
49 PARSEDOPTS=$(getopt --name "$0" --longoptions \
50     help,debug,test-packages,target:,command:,only-test:,only-build:,build-version: \
51     -- "" "$@")
52 if [ $? -ne 0 ]; then
53     exit 1
54 fi
55
56 TARGET=debian8
57 COMMAND=
58 DEBUG=
59
60 eval set -- "$PARSEDOPTS"
61 while [ $# -gt 0 ]; do
62     case "$1" in
63         --help)
64             echo >&2 "$helpmessage"
65             echo >&2
66             exit 1
67             ;;
68         --target)
69             TARGET="$2"; shift
70             ;;
71         --only-test)
72             test_packages=1
73             packages="$2"; shift
74             ;;
75         --only-build)
76             ONLY_BUILD="$2"; shift
77             ;;
78         --debug)
79             DEBUG=" --debug"
80             ARVADOS_DEBUG="1"
81             ;;
82         --command)
83             COMMAND="$2"; shift
84             ;;
85         --test-packages)
86             test_packages=1
87             ;;
88         --build-version)
89             if [[ -z "$2" ]]; then
90                 :
91             elif ! [[ "$2" =~ (.*)-(.*) ]]; then
92                 echo >&2 "FATAL: --build-version '$2' does not include an iteration. Try '${2}-1'?"
93                 exit 1
94             else
95                 ARVADOS_BUILDING_VERSION="${BASH_REMATCH[1]}"
96                 ARVADOS_BUILDING_ITERATION="${BASH_REMATCH[2]}"
97             fi
98             shift
99             ;;
100         --)
101             if [ $# -gt 1 ]; then
102                 echo >&2 "$0: unrecognized argument '$2'. Try: $0 --help"
103                 exit 1
104             fi
105             ;;
106     esac
107     shift
108 done
109
110 set -e
111
112 if [[ -n "$ARVADOS_BUILDING_VERSION" ]]; then
113     echo "build version='$ARVADOS_BUILDING_VERSION', package iteration='$ARVADOS_BUILDING_ITERATION'"
114 fi
115
116 if [[ -n "$test_packages" ]]; then
117     if [[ -n "$(find $WORKSPACE/packages/$TARGET -name '*.rpm')" ]] ; then
118         set +e
119         /usr/bin/which createrepo >/dev/null
120         if [[ "$?" != "0" ]]; then
121                 echo >&2
122                 echo >&2 "Error: please install createrepo. E.g. sudo apt-get install createrepo"
123                 echo >&2
124                 exit 1
125         fi
126         set -e
127         createrepo $WORKSPACE/packages/$TARGET
128     fi
129
130     if [[ -n "$(find $WORKSPACE/packages/$TARGET -name '*.deb')" ]] ; then
131         (cd $WORKSPACE/packages/$TARGET
132           dpkg-scanpackages .  2> >(grep -v 'warning' 1>&2) | tee Packages | gzip -c > Packages.gz
133           apt-ftparchive -o APT::FTPArchive::Release::Origin=Arvados release . > Release
134         )
135     fi
136
137     COMMAND="/jenkins/package-testing/test-packages-$TARGET.sh"
138     IMAGE="arvados/package-test:$TARGET"
139 else
140     IMAGE="arvados/build:$TARGET"
141     if [[ "$COMMAND" != "" ]]; then
142         COMMAND="/usr/local/rvm/bin/rvm-exec default bash /jenkins/$COMMAND --target $TARGET$DEBUG"
143     fi
144 fi
145
146 JENKINS_DIR=$(dirname "$(readlink -e "$0")")
147
148 if [[ -n "$test_packages" ]]; then
149     pushd "$JENKINS_DIR/package-test-dockerfiles"
150 else
151     pushd "$JENKINS_DIR/package-build-dockerfiles"
152     make "$TARGET/generated"
153 fi
154
155 echo $TARGET
156 cd $TARGET
157 time docker build --tag=$IMAGE .
158 popd
159
160 if test -z "$packages" ; then
161     packages="arvados-api-server
162         arvados-docker-cleaner
163         arvados-git-httpd
164         arvados-node-manager
165         arvados-src
166         arvados-workbench
167         crunch-dispatch-local
168         crunch-dispatch-slurm
169         crunch-run
170         crunchstat
171         keep-balance
172         keep-block-check
173         keepproxy
174         keep-rsync
175         keepstore
176         keep-web
177         libarvados-perl"
178
179     case "$TARGET" in
180         *)
181             packages="$packages python-arvados-fuse
182                   python-arvados-python-client python-arvados-cwl-runner"
183             ;;
184     esac
185 fi
186
187 FINAL_EXITCODE=0
188
189 package_fails=""
190
191 mkdir -p "$WORKSPACE/apps/workbench/vendor/cache-$TARGET"
192 mkdir -p "$WORKSPACE/services/api/vendor/cache-$TARGET"
193
194 docker_volume_args=(
195     -v "$JENKINS_DIR:/jenkins"
196     -v "$WORKSPACE:/arvados"
197     -v /arvados/services/api/vendor/bundle
198     -v /arvados/apps/workbench/vendor/bundle
199     -v "$WORKSPACE/services/api/vendor/cache-$TARGET:/arvados/services/api/vendor/cache"
200     -v "$WORKSPACE/apps/workbench/vendor/cache-$TARGET:/arvados/apps/workbench/vendor/cache"
201 )
202
203 if [[ -n "$test_packages" ]]; then
204     for p in $packages ; do
205         if [[ -n "$ONLY_BUILD" ]] && [[ "$p" != "$ONLY_BUILD" ]]; then
206             continue
207         fi
208         echo
209         echo "START: $p test on $IMAGE" >&2
210         if docker run --rm \
211             "${docker_volume_args[@]}" \
212             --env ARVADOS_DEBUG=$ARVADOS_DEBUG \
213             --env "TARGET=$TARGET" \
214             --env "WORKSPACE=/arvados" \
215             "$IMAGE" $COMMAND $p
216         then
217             echo "OK: $p test on $IMAGE succeeded" >&2
218         else
219             FINAL_EXITCODE=$?
220             package_fails="$package_fails $p"
221             echo "ERROR: $p test on $IMAGE failed with exit status $FINAL_EXITCODE" >&2
222         fi
223     done
224 else
225     echo
226     echo "START: build packages on $IMAGE" >&2
227     # Move existing packages and other files into the processed/ subdirectory
228     if [[ ! -e "${WORKSPACE}/packages/${TARGET}/processed" ]]; then
229       mkdir -p "${WORKSPACE}/packages/${TARGET}/processed"
230     fi
231     set +e
232     mv -f ${WORKSPACE}/packages/${TARGET}/* ${WORKSPACE}/packages/${TARGET}/processed/ 2>/dev/null
233     set -e
234     # Build packages
235     if docker run --rm \
236         "${docker_volume_args[@]}" \
237         --env ARVADOS_BUILDING_VERSION="$ARVADOS_BUILDING_VERSION" \
238         --env ARVADOS_BUILDING_ITERATION="$ARVADOS_BUILDING_ITERATION" \
239         --env ARVADOS_DEBUG=$ARVADOS_DEBUG \
240         --env "ONLY_BUILD=$ONLY_BUILD" \
241         "$IMAGE" $COMMAND
242     then
243         echo
244         echo "OK: build packages on $IMAGE succeeded" >&2
245     else
246         FINAL_EXITCODE=$?
247         echo "ERROR: build packages on $IMAGE failed with exit status $FINAL_EXITCODE" >&2
248     fi
249 fi
250
251 if test -n "$package_fails" ; then
252     echo "Failed package tests:$package_fails" >&2
253 fi
254
255 exit $FINAL_EXITCODE