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