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