15370: Fix flaky test.
[arvados.git] / build / run-build-packages-python-and-ruby.sh
1 #!/bin/bash
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: AGPL-3.0
5
6 COLUMNS=80
7
8 . `dirname "$(readlink -f "$0")"`/run-library.sh
9
10 read -rd "\000" helpmessage <<EOF
11 $(basename $0): Build Arvados Python packages and Ruby gems
12
13 Syntax:
14         WORKSPACE=/path/to/arvados $(basename $0) [options]
15
16 Options:
17
18 --debug
19     Output debug information (default: false)
20 --upload
21     If the build and test steps are successful, upload the python
22     packages to pypi and the gems to rubygems (default: false)
23 --ruby <true|false>
24     Build ruby gems (default: true)
25 --python <true|false>
26     Build python packages (default: true)
27
28 WORKSPACE=path         Path to the Arvados source tree to build packages from
29
30 EOF
31
32 exit_cleanly() {
33     trap - INT
34     report_outcomes
35     exit ${#failures[@]}
36 }
37
38 gem_wrapper() {
39   local gem_name="$1"; shift
40   local gem_directory="$1"; shift
41
42   title "Start $gem_name gem build"
43   timer_reset
44
45   cd "$gem_directory"
46   handle_ruby_gem $gem_name
47
48   checkexit $? "$gem_name gem build"
49   title "End of $gem_name gem build (`timer`)"
50 }
51
52 handle_python_package () {
53   # This function assumes the current working directory is the python package directory
54   if [ -n "$(find dist -name "*-$(nohash_version_from_git).tar.gz" -print -quit)" ]; then
55     echo "This package doesn't need rebuilding."
56     return
57   fi
58   # Make sure only to use sdist - that's the only format pip can deal with (sigh)
59   python3 setup.py $DASHQ_UNLESS_DEBUG sdist
60 }
61
62 python_wrapper() {
63   local package_name="$1"; shift
64   local package_directory="$1"; shift
65
66   title "Start $package_name python package build"
67   timer_reset
68
69   cd "$package_directory"
70   if [[ $DEBUG > 0 ]]; then
71     echo `pwd`
72   fi
73   handle_python_package
74
75   checkexit $? "$package_name python package build"
76   title "End of $package_name python package build (`timer`)"
77 }
78
79 TARGET=
80 UPLOAD=0
81 RUBY=1
82 PYTHON=1
83 DEBUG=${ARVADOS_DEBUG:-0}
84
85 PARSEDOPTS=$(getopt --name "$0" --longoptions \
86     help,debug,ruby:,python:,upload,target: \
87     -- "" "$@")
88 if [ $? -ne 0 ]; then
89     exit 1
90 fi
91
92 eval set -- "$PARSEDOPTS"
93 while [ $# -gt 0 ]; do
94     case "$1" in
95         --help)
96             echo >&2 "$helpmessage"
97             echo >&2
98             exit 1
99             ;;
100         --target)
101             TARGET="$2"; shift
102             ;;
103         --ruby)
104             RUBY="$2"; shift
105             if [ "$RUBY" != "true" ] && [ "$RUBY" != "1" ]; then
106               RUBY=0
107             else
108               RUBY=1
109             fi
110             ;;
111         --python)
112             PYTHON="$2"; shift
113             if [ "$PYTHON" != "true" ] && [ "$PYTHON" != "1" ]; then
114               PYTHON=0
115             else
116               PYTHON=1
117             fi
118             ;;
119         --upload)
120             UPLOAD=1
121             ;;
122         --debug)
123             DEBUG=1
124             ;;
125         --)
126             if [ $# -gt 1 ]; then
127                 echo >&2 "$0: unrecognized argument '$2'. Try: $0 --help"
128                 exit 1
129             fi
130             ;;
131     esac
132     shift
133 done
134
135 if ! [[ -n "$WORKSPACE" ]]; then
136   echo >&2 "$helpmessage"
137   echo >&2
138   echo >&2 "Error: WORKSPACE environment variable not set"
139   echo >&2
140   exit 1
141 fi
142
143 STDOUT_IF_DEBUG=/dev/null
144 STDERR_IF_DEBUG=/dev/null
145 DASHQ_UNLESS_DEBUG=-q
146 if [[ "$DEBUG" != 0 ]]; then
147     STDOUT_IF_DEBUG=/dev/stdout
148     STDERR_IF_DEBUG=/dev/stderr
149     DASHQ_UNLESS_DEBUG=
150 fi
151
152 RUN_BUILD_PACKAGES_PATH="`dirname \"$0\"`"
153 RUN_BUILD_PACKAGES_PATH="`( cd \"$RUN_BUILD_PACKAGES_PATH\" && pwd )`"  # absolutized and normalized
154 if [ -z "$RUN_BUILD_PACKAGES_PATH" ] ; then
155   # error; for some reason, the path is not accessible
156   # to the script (e.g. permissions re-evaled after suid)
157   exit 1  # fail
158 fi
159
160 debug_echo "$0 is running from $RUN_BUILD_PACKAGES_PATH"
161 debug_echo "Workspace is $WORKSPACE"
162
163 if [ $RUBY -eq 0 ] && [ $PYTHON -eq 0 ]; then
164   echo "Nothing to do!"
165   exit 0
166 fi
167
168 if [[ -f /etc/profile.d/rvm.sh ]]; then
169     source /etc/profile.d/rvm.sh
170     GEM="rvm-exec default gem"
171 else
172     GEM=gem
173 fi
174
175 # Make all files world-readable -- jenkins runs with umask 027, and has checked
176 # out our git tree here
177 chmod o+r "$WORKSPACE" -R
178
179 # More cleanup - make sure all executables that we'll package are 755
180 cd "$WORKSPACE"
181 find -type d -name 'bin' |xargs -I {} find {} -type f |xargs -I {} chmod 755 {}
182
183 # Now fix our umask to something better suited to building and publishing
184 # gems and packages
185 umask 0022
186
187 debug_echo "umask is" `umask`
188
189 GEM_BUILD_FAILURES=0
190 if [ $RUBY -eq 1 ]; then
191   debug_echo "Building Ruby gems"
192   gem_wrapper arvados "$WORKSPACE/sdk/ruby"
193   gem_wrapper arvados-cli "$WORKSPACE/sdk/cli"
194   gem_wrapper arvados-login-sync "$WORKSPACE/services/login-sync"
195   if [ ${#failures[@]} -ne 0 ]; then
196     GEM_BUILD_FAILURES=${#failures[@]}
197   fi
198 fi
199
200 PYTHON_BUILD_FAILURES=0
201 if [ $PYTHON -eq 1 ]; then
202   debug_echo "Building Python packages"
203   python_wrapper arvados-python-client "$WORKSPACE/sdk/python"
204   python_wrapper arvados-cwl-runner "$WORKSPACE/sdk/cwl"
205   python_wrapper arvados_fuse "$WORKSPACE/services/fuse"
206   python_wrapper crunchstat_summary "$WORKSPACE/tools/crunchstat-summary"
207   python_wrapper arvados-user-activity "$WORKSPACE/tools/user-activity"
208
209   if [ $((${#failures[@]} - $GEM_BUILD_FAILURES)) -ne 0 ]; then
210     PYTHON_BUILD_FAILURES=$((${#failures[@]} - $GEM_BUILD_FAILURES))
211   fi
212 fi
213
214 if [ $UPLOAD -ne 0 ]; then
215   echo "Uploading"
216
217   if [ $DEBUG > 0 ]; then
218     EXTRA_UPLOAD_FLAGS=" --verbose"
219   else
220     EXTRA_UPLOAD_FLAGS=""
221   fi
222
223   if [ ! -e "$WORKSPACE/packages" ]; then
224     mkdir -p "$WORKSPACE/packages"
225   fi
226
227   if [ $PYTHON -eq 1 ]; then
228     title "Start upload python packages"
229     timer_reset
230
231     if [ $PYTHON_BUILD_FAILURES -eq 0 ]; then
232       /usr/local/arvados-dev/jenkins/run_upload_packages.py $EXTRA_UPLOAD_FLAGS --workspace $WORKSPACE python
233     else
234       echo "Skipping python packages upload, there were errors building the packages"
235     fi
236     checkexit $? "upload python packages"
237     title "End of upload python packages (`timer`)"
238   fi
239
240   if [ $RUBY -eq 1 ]; then
241     title "Start upload ruby gems"
242     timer_reset
243
244     if [ $GEM_BUILD_FAILURES -eq 0 ]; then
245       /usr/local/arvados-dev/jenkins/run_upload_packages.py $EXTRA_UPLOAD_FLAGS --workspace $WORKSPACE gems
246     else
247       echo "Skipping ruby gem upload, there were errors building the packages"
248     fi
249     checkexit $? "upload ruby gems"
250     title "End of upload ruby gems (`timer`)"
251   fi
252 fi
253
254 exit_cleanly