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