21700: Install Bundler system-wide in Rails postinst
[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 # Make all files world-readable -- jenkins runs with umask 027, and has checked
169 # out our git tree here
170 chmod o+r "$WORKSPACE" -R
171
172 # More cleanup - make sure all executables that we'll package are 755
173 cd "$WORKSPACE"
174 find -type d -name 'bin' |xargs -I {} find {} -type f |xargs -I {} chmod 755 {}
175
176 # Now fix our umask to something better suited to building and publishing
177 # gems and packages
178 umask 0022
179
180 debug_echo "umask is" `umask`
181
182 GEM_BUILD_FAILURES=0
183 if [ $RUBY -eq 1 ]; then
184   debug_echo "Building Ruby gems"
185   gem_wrapper arvados "$WORKSPACE/sdk/ruby"
186   gem_wrapper arvados-cli "$WORKSPACE/sdk/cli"
187   gem_wrapper arvados-login-sync "$WORKSPACE/services/login-sync"
188   if [ ${#failures[@]} -ne 0 ]; then
189     GEM_BUILD_FAILURES=${#failures[@]}
190   fi
191 fi
192
193 PYTHON_BUILD_FAILURES=0
194 if [ $PYTHON -eq 1 ]; then
195   debug_echo "Building Python packages"
196   python_wrapper arvados-python-client "$WORKSPACE/sdk/python"
197   python_wrapper arvados-cwl-runner "$WORKSPACE/sdk/cwl"
198   python_wrapper arvados_fuse "$WORKSPACE/services/fuse"
199   python_wrapper crunchstat_summary "$WORKSPACE/tools/crunchstat-summary"
200   python_wrapper arvados-user-activity "$WORKSPACE/tools/user-activity"
201
202   if [ $((${#failures[@]} - $GEM_BUILD_FAILURES)) -ne 0 ]; then
203     PYTHON_BUILD_FAILURES=$((${#failures[@]} - $GEM_BUILD_FAILURES))
204   fi
205 fi
206
207 if [ $UPLOAD -ne 0 ]; then
208   echo "Uploading"
209
210   if [ $DEBUG > 0 ]; then
211     EXTRA_UPLOAD_FLAGS=" --verbose"
212   else
213     EXTRA_UPLOAD_FLAGS=""
214   fi
215
216   if [ ! -e "$WORKSPACE/packages" ]; then
217     mkdir -p "$WORKSPACE/packages"
218   fi
219
220   if [ $PYTHON -eq 1 ]; then
221     title "Start upload python packages"
222     timer_reset
223
224     if [ $PYTHON_BUILD_FAILURES -eq 0 ]; then
225       /usr/local/arvados-dev/jenkins/run_upload_packages.py $EXTRA_UPLOAD_FLAGS --workspace $WORKSPACE python
226     else
227       echo "Skipping python packages upload, there were errors building the packages"
228     fi
229     checkexit $? "upload python packages"
230     title "End of upload python packages (`timer`)"
231   fi
232
233   if [ $RUBY -eq 1 ]; then
234     title "Start upload ruby gems"
235     timer_reset
236
237     if [ $GEM_BUILD_FAILURES -eq 0 ]; then
238       /usr/local/arvados-dev/jenkins/run_upload_packages.py $EXTRA_UPLOAD_FLAGS --workspace $WORKSPACE gems
239     else
240       echo "Skipping ruby gem upload, there were errors building the packages"
241     fi
242     checkexit $? "upload ruby gems"
243     title "End of upload ruby gems (`timer`)"
244   fi
245 fi
246
247 exit_cleanly