16470: Upgrades rails to 5.2
[arvados.git] / build / run-library.sh
1 #!/bin/bash -xe
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: AGPL-3.0
5
6 # A library of functions shared by the various scripts in this directory.
7
8 # This is the timestamp about when we merged changed to include licenses
9 # with Arvados packages.  We use it as a heuristic to add revisions for
10 # older packages.
11 LICENSE_PACKAGE_TS=20151208015500
12
13 if [[ -z "$ARVADOS_BUILDING_VERSION" ]]; then
14     RAILS_PACKAGE_ITERATION=1
15 else
16     RAILS_PACKAGE_ITERATION="$ARVADOS_BUILDING_ITERATION"
17 fi
18
19 debug_echo () {
20     echo "$@" >"$STDOUT_IF_DEBUG"
21 }
22
23 find_python_program() {
24     prog="$1"
25     shift
26     for prog in "$@"; do
27         if "$prog" --version >/dev/null 2>&1; then
28             echo "$prog"
29             return 0
30         fi
31     done
32     cat >&2 <<EOF
33 $helpmessage
34
35 Error: $prog (from Python setuptools module) not found
36
37 EOF
38     exit 1
39 }
40
41 format_last_commit_here() {
42     local format="$1"; shift
43     local dir="${1:-.}"; shift
44     TZ=UTC git log -n1 --first-parent "--format=format:$format" "$dir"
45 }
46
47 version_from_git() {
48     # Output the version being built, or if we're building a
49     # dev/prerelease, output a version number based on the git log for
50     # the given $subdir.
51     local subdir="$1"; shift
52     if [[ -n "$ARVADOS_BUILDING_VERSION" ]]; then
53         echo "$ARVADOS_BUILDING_VERSION"
54         return
55     fi
56
57     local git_ts git_hash
58     declare $(format_last_commit_here "git_ts=%ct git_hash=%h" "$subdir")
59     ARVADOS_BUILDING_VERSION="$($WORKSPACE/build/version-at-commit.sh $git_hash)"
60     echo "$ARVADOS_BUILDING_VERSION"
61 }
62
63 nohash_version_from_git() {
64     if [[ -n "$ARVADOS_BUILDING_VERSION" ]]; then
65         echo "$ARVADOS_BUILDING_VERSION"
66         return
67     fi
68     version_from_git | cut -d. -f1-4
69 }
70
71 timestamp_from_git() {
72     local subdir="$1"; shift
73     format_last_commit_here "%ct" "$subdir"
74 }
75
76 calculate_python_sdk_cwl_package_versions() {
77   python_sdk_ts=$(cd sdk/python && timestamp_from_git)
78   cwl_runner_ts=$(cd sdk/cwl && timestamp_from_git)
79
80   python_sdk_version=$(cd sdk/python && nohash_version_from_git)
81   cwl_runner_version=$(cd sdk/cwl && nohash_version_from_git)
82
83   if [[ $python_sdk_ts -gt $cwl_runner_ts ]]; then
84     cwl_runner_version=$python_sdk_version
85   fi
86 }
87
88 handle_python_package () {
89   # This function assumes the current working directory is the python package directory
90   if [ -n "$(find dist -name "*-$(nohash_version_from_git).tar.gz" -print -quit)" ]; then
91     # This package doesn't need rebuilding.
92     return
93   fi
94   # Make sure only to use sdist - that's the only format pip can deal with (sigh)
95   python setup.py $DASHQ_UNLESS_DEBUG sdist
96 }
97
98 handle_ruby_gem() {
99     local gem_name="$1"; shift
100     local gem_version="$(nohash_version_from_git)"
101     local gem_src_dir="$(pwd)"
102
103     if [[ -n "$ONLY_BUILD" ]] && [[ "$gem_name" != "$ONLY_BUILD" ]] ; then
104         return 0
105     fi
106
107     if ! [[ -e "${gem_name}-${gem_version}.gem" ]]; then
108         find -maxdepth 1 -name "${gem_name}-*.gem" -delete
109
110         # -q appears to be broken in gem version 2.2.2
111         $GEM build "$gem_name.gemspec" $DASHQ_UNLESS_DEBUG >"$STDOUT_IF_DEBUG" 2>"$STDERR_IF_DEBUG"
112     fi
113 }
114
115 calculate_go_package_version() {
116   # $__returnvar has the nameref attribute set, which means it is a reference
117   # to another variable that is passed in as the first argument to this function.
118   # see https://www.gnu.org/software/bash/manual/html_node/Shell-Parameters.html
119   local -n __returnvar="$1"; shift
120   local oldpwd="$PWD"
121
122   cd "$WORKSPACE"
123   go mod download
124
125   # Update the version number and build a new package if the vendor
126   # bundle has changed, or the command imports anything from the
127   # Arvados SDK and the SDK has changed.
128   declare -a checkdirs=(go.mod go.sum)
129   while [ -n "$1" ]; do
130       checkdirs+=("$1")
131       shift
132   done
133   if grep -qr git.arvados.org/arvados .; then
134       checkdirs+=(sdk/go lib)
135   fi
136   local timestamp=0
137   for dir in ${checkdirs[@]}; do
138       cd "$WORKSPACE"
139       ts="$(timestamp_from_git "$dir")"
140       if [[ "$ts" -gt "$timestamp" ]]; then
141           version=$(version_from_git "$dir")
142           timestamp="$ts"
143       fi
144   done
145   cd "$oldpwd"
146   __returnvar="$version"
147 }
148
149 # Usage: package_go_binary services/foo arvados-foo "Compute foo to arbitrary precision" [apache-2.0.txt]
150 package_go_binary() {
151     local src_path="$1"; shift
152     local prog="$1"; shift
153     local description="$1"; shift
154     local license_file="${1:-agpl-3.0.txt}"; shift
155
156     if [[ -n "$ONLY_BUILD" ]] && [[ "$prog" != "$ONLY_BUILD" ]]; then
157       # arvados-workbench depends on arvados-server at build time, so even when
158       # only arvados-workbench is being built, we need to build arvados-server too
159       if [[ "$prog" != "arvados-server" ]] || [[ "$ONLY_BUILD" != "arvados-workbench" ]]; then
160         return 0
161       fi
162     fi
163
164     debug_echo "package_go_binary $src_path as $prog"
165
166     local basename="${src_path##*/}"
167     calculate_go_package_version go_package_version $src_path
168
169     cd $WORKSPACE/packages/$TARGET
170     test_package_presence $prog $go_package_version go
171
172     if [[ "$?" != "0" ]]; then
173       return 1
174     fi
175
176     go get -ldflags "-X git.arvados.org/arvados.git/lib/cmd.version=${go_package_version} -X main.version=${go_package_version}" "git.arvados.org/arvados.git/$src_path"
177
178     local -a switches=()
179     systemd_unit="$WORKSPACE/${src_path}/${prog}.service"
180     if [[ -e "${systemd_unit}" ]]; then
181         switches+=(
182             --after-install "${WORKSPACE}/build/go-python-package-scripts/postinst"
183             --before-remove "${WORKSPACE}/build/go-python-package-scripts/prerm"
184             "${systemd_unit}=/lib/systemd/system/${prog}.service")
185     fi
186     switches+=("$WORKSPACE/${license_file}=/usr/share/doc/$prog/${license_file}")
187
188     fpm_build "${WORKSPACE}/${src_path}" "$GOPATH/bin/${basename}=/usr/bin/${prog}" "${prog}" dir "${go_package_version}" "--url=https://arvados.org" "--license=GNU Affero General Public License, version 3.0" "--description=${description}" "${switches[@]}"
189 }
190
191 # Usage: package_go_so lib/foo arvados_foo.so arvados-foo "Arvados foo library"
192 package_go_so() {
193     local src_path="$1"; shift
194     local sofile="$1"; shift
195     local pkg="$1"; shift
196     local description="$1"; shift
197
198     debug_echo "package_go_so $src_path as $pkg"
199
200     calculate_go_package_version go_package_version $src_path
201     cd $WORKSPACE/packages/$TARGET
202     test_package_presence $pkg $go_package_version go || return 1
203     cd $WORKSPACE/$src_path
204     go build -buildmode=c-shared -o ${GOPATH}/bin/${sofile}
205     cd $WORKSPACE/packages/$TARGET
206     local -a fpmargs=(
207         "--url=https://arvados.org"
208         "--license=Apache License, Version 2.0"
209         "--description=${description}"
210         "$WORKSPACE/apache-2.0.txt=/usr/share/doc/$pkg/apache-2.0.txt"
211     )
212     if [[ -e "$WORKSPACE/$src_path/pam-configs-arvados" ]]; then
213         fpmargs+=("$WORKSPACE/$src_path/pam-configs-arvados=/usr/share/pam-configs/arvados-go")
214     fi
215     if [[ -e "$WORKSPACE/$src_path/README" ]]; then
216         fpmargs+=("$WORKSPACE/$src_path/README=/usr/share/doc/$pkg/README")
217     fi
218     fpm_build "${WORKSPACE}/${src_path}" "$GOPATH/bin/${sofile}=/usr/lib/${sofile}" "${pkg}" dir "${go_package_version}" "${fpmargs[@]}"
219 }
220
221 default_iteration() {
222     if [[ -n "$ARVADOS_BUILDING_VERSION" ]]; then
223         echo "$ARVADOS_BUILDING_ITERATION"
224         return
225     fi
226     local package_name="$1"; shift
227     local package_version="$1"; shift
228     local package_type="$1"; shift
229     local iteration=1
230     if [[ $package_version =~ ^0\.1\.([0-9]{14})(\.|$) ]] && \
231            [[ ${BASH_REMATCH[1]} -le $LICENSE_PACKAGE_TS ]]; then
232         iteration=2
233     fi
234     if [[ $package_type =~ ^python ]]; then
235       # Fix --iteration for #9242.
236       iteration=2
237     fi
238     echo $iteration
239 }
240
241 _build_rails_package_scripts() {
242     local pkgname="$1"; shift
243     local destdir="$1"; shift
244     local srcdir="$RUN_BUILD_PACKAGES_PATH/rails-package-scripts"
245     for scriptname in postinst prerm postrm; do
246         cat "$srcdir/$pkgname.sh" "$srcdir/step2.sh" "$srcdir/$scriptname.sh" \
247             >"$destdir/$scriptname" || return $?
248     done
249 }
250
251 rails_package_version() {
252     local pkgname="$1"; shift
253     local srcdir="$1"; shift
254     if [[ -n "$ARVADOS_BUILDING_VERSION" ]]; then
255         echo "$ARVADOS_BUILDING_VERSION"
256         return
257     fi
258     local version="$(version_from_git)"
259     if [ $pkgname = "arvados-api-server" -o $pkgname = "arvados-workbench" ] ; then
260         calculate_go_package_version version cmd/arvados-server "$srcdir"
261     fi
262     echo $version
263 }
264
265 test_rails_package_presence() {
266   local pkgname="$1"; shift
267   local srcdir="$1"; shift
268
269   if [[ -n "$ONLY_BUILD" ]] && [[ "$pkgname" != "$ONLY_BUILD" ]] ; then
270     return 1
271   fi
272
273   tmppwd=`pwd`
274
275   cd $srcdir
276
277   local version="$(rails_package_version "$pkgname" "$srcdir")"
278
279   cd $tmppwd
280
281   test_package_presence $pkgname $version rails "$RAILS_PACKAGE_ITERATION"
282 }
283
284 get_complete_package_name() {
285   # if the errexit flag is set, unset it until this function returns
286   # otherwise, the shift calls below will abort the program if optional arguments are not supplied
287   if [ -o errexit ]; then
288     set +e
289     trap 'set -e' RETURN
290   fi
291   # $__returnvar has the nameref attribute set, which means it is a reference
292   # to another variable that is passed in as the first argument to this function.
293   # see https://www.gnu.org/software/bash/manual/html_node/Shell-Parameters.html
294   local -n __returnvar="$1"; shift
295   local pkgname="$1"; shift
296   local version="$1"; shift
297   local pkgtype="$1"; shift
298   local iteration="$1"; shift
299   local arch="$1"; shift
300   if [[ "$iteration" == "" ]]; then
301       iteration="$(default_iteration "$pkgname" "$version" "$pkgtype")"
302   fi
303
304   if [[ "$arch" == "" ]]; then
305     rpm_architecture="x86_64"
306     deb_architecture="amd64"
307
308     if [[ "$pkgtype" =~ ^(src)$ ]]; then
309       rpm_architecture="noarch"
310       deb_architecture="all"
311     fi
312
313     # These python packages have binary components
314     if [[ "$pkgname" =~ (ruamel|ciso|pycrypto|pyyaml) ]]; then
315       rpm_architecture="x86_64"
316       deb_architecture="amd64"
317     fi
318   else
319     rpm_architecture=$arch
320     deb_architecture=$arch
321   fi
322
323   local complete_pkgname="${pkgname}_$version${iteration:+-$iteration}_$deb_architecture.deb"
324   if [[ "$FORMAT" == "rpm" ]]; then
325       # rpm packages get iteration 1 if we don't supply one
326       iteration=${iteration:-1}
327       complete_pkgname="$pkgname-$version-${iteration}.$rpm_architecture.rpm"
328   fi
329   __returnvar=${complete_pkgname}
330 }
331
332 # Test if the package already exists, if not return 0, if it does return 1
333 test_package_presence() {
334     local pkgname="$1"; shift
335     local version="$1"; shift
336     local pkgtype="$1"; shift
337     local iteration="$1"; shift
338     local arch="$1"; shift
339     if [[ -n "$ONLY_BUILD" ]] && [[ "$pkgname" != "$ONLY_BUILD" ]] ; then
340       # arvados-workbench depends on arvados-server at build time, so even when
341       # only arvados-workbench is being built, we need to build arvados-server too
342       if [[ "$pkgname" != "arvados-server" ]] || [[ "$ONLY_BUILD" != "arvados-workbench" ]]; then
343         return 1
344       fi
345     fi
346
347     local full_pkgname
348     get_complete_package_name full_pkgname $pkgname $version $pkgtype $iteration $arch
349
350     # See if we can skip building the package, only if it already exists in the
351     # processed/ directory. If so, move it back to the packages directory to make
352     # sure it gets picked up by the test and/or upload steps.
353     # Get the list of packages from the repos
354
355     if [[ "$FORCE_BUILD" == "1" ]]; then
356       echo "Package $full_pkgname build forced with --force-build, building"
357     elif [[ "$FORMAT" == "deb" ]]; then
358       declare -A dd
359       dd[debian9]=stretch
360       dd[debian10]=buster
361       dd[ubuntu1604]=xenial
362       dd[ubuntu1804]=bionic
363       D=${dd[$TARGET]}
364       if [ ${pkgname:0:3} = "lib" ]; then
365         repo_subdir=${pkgname:0:4}
366       else
367         repo_subdir=${pkgname:0:1}
368       fi
369
370       repo_pkg_list=$(curl -s -o - http://apt.arvados.org/pool/${D}-dev/main/${repo_subdir}/${pkgname}/)
371       echo "${repo_pkg_list}" |grep -q ${full_pkgname}
372       if [ $? -eq 0 ] ; then
373         echo "Package $full_pkgname exists upstream, not rebuilding, downloading instead!"
374         curl -s -o "$WORKSPACE/packages/$TARGET/${full_pkgname}" http://apt.arvados.org/pool/${D}-dev/main/${repo_subdir}/${pkgname}/${full_pkgname}
375         return 1
376       elif test -f "$WORKSPACE/packages/$TARGET/processed/${full_pkgname}" ; then
377         echo "Package $full_pkgname exists, not rebuilding!"
378         return 1
379       else
380         echo "Package $full_pkgname not found, building"
381         return 0
382       fi
383     else
384       centos_repo="http://rpm.arvados.org/CentOS/7/dev/x86_64/"
385
386       repo_pkg_list=$(curl -s -o - ${centos_repo})
387       echo ${repo_pkg_list} |grep -q ${full_pkgname}
388       if [ $? -eq 0 ]; then
389         echo "Package $full_pkgname exists upstream, not rebuilding, downloading instead!"
390         curl -s -o "$WORKSPACE/packages/$TARGET/${full_pkgname}" ${centos_repo}${full_pkgname}
391         return 1
392       elif test -f "$WORKSPACE/packages/$TARGET/processed/${full_pkgname}" ; then
393         echo "Package $full_pkgname exists, not rebuilding!"
394         return 1
395       else
396         echo "Package $full_pkgname not found, building"
397         return 0
398       fi
399     fi
400 }
401
402 handle_rails_package() {
403     local pkgname="$1"; shift
404
405     if [[ -n "$ONLY_BUILD" ]] && [[ "$pkgname" != "$ONLY_BUILD" ]] ; then
406         return 0
407     fi
408     local srcdir="$1"; shift
409     cd "$srcdir"
410     local license_path="$1"; shift
411     local version="$(rails_package_version "$pkgname" "$srcdir")"
412     echo "$version" >package-build.version
413     local scripts_dir="$(mktemp --tmpdir -d "$pkgname-XXXXXXXX.scripts")" && \
414     (
415         set -e
416         _build_rails_package_scripts "$pkgname" "$scripts_dir"
417         cd "$srcdir"
418         mkdir -p tmp
419         git rev-parse HEAD >git-commit.version
420         bundle package --all
421     )
422     if [[ 0 != "$?" ]] || ! cd "$WORKSPACE/packages/$TARGET"; then
423         echo "ERROR: $pkgname package prep failed" >&2
424         rm -rf "$scripts_dir"
425         EXITCODE=1
426         return 1
427     fi
428     local railsdir="/var/www/${pkgname%-server}/current"
429     local -a pos_args=("$srcdir/=$railsdir" "$pkgname" dir "$version")
430     local license_arg="$license_path=$railsdir/$(basename "$license_path")"
431     local -a switches=(--after-install "$scripts_dir/postinst"
432                        --before-remove "$scripts_dir/prerm"
433                        --after-remove "$scripts_dir/postrm")
434     if [[ -z "$ARVADOS_BUILDING_VERSION" ]]; then
435         switches+=(--iteration $RAILS_PACKAGE_ITERATION)
436     fi
437     # For some reason fpm excludes need to not start with /.
438     local exclude_root="${railsdir#/}"
439     # .git and packages are for the SSO server, which is built from its
440     # repository root.
441     local -a exclude_list=(.git packages tmp log coverage Capfile\* \
442                            config/deploy\* config/application.yml)
443     # for arvados-workbench, we need to have the (dummy) config/database.yml in the package
444     if  [[ "$pkgname" != "arvados-workbench" ]]; then
445       exclude_list+=('config/database.yml')
446     fi
447     for exclude in ${exclude_list[@]}; do
448         switches+=(-x "$exclude_root/$exclude")
449     done
450     fpm_build "${srcdir}" "${pos_args[@]}" "${switches[@]}" \
451               -x "$exclude_root/vendor/cache-*" \
452               -x "$exclude_root/vendor/bundle" "$@" "$license_arg"
453     rm -rf "$scripts_dir"
454 }
455
456 # Build python packages with a virtualenv built-in
457 fpm_build_virtualenv () {
458   PKG=$1
459   shift
460   PKG_DIR=$1
461   shift
462   PACKAGE_TYPE=${1:-python}
463   shift
464
465   # Set up
466   STDOUT_IF_DEBUG=/dev/null
467   STDERR_IF_DEBUG=/dev/null
468   DASHQ_UNLESS_DEBUG=-q
469   if [[ "$DEBUG" != "0" ]]; then
470       STDOUT_IF_DEBUG=/dev/stdout
471       STDERR_IF_DEBUG=/dev/stderr
472       DASHQ_UNLESS_DEBUG=
473   fi
474   if [[ "$ARVADOS_BUILDING_ITERATION" == "" ]]; then
475     ARVADOS_BUILDING_ITERATION=1
476   fi
477
478   local python=""
479   case "$PACKAGE_TYPE" in
480     python3)
481         python=python3
482         if [[ "$FORMAT" != "rpm" ]]; then
483           pip=pip3
484         else
485           # In CentOS, we use a different mechanism to get the right version of pip
486           pip=pip
487         fi
488         PACKAGE_PREFIX=$PYTHON3_PKG_PREFIX
489         ;;
490     python)
491         # All Arvados Python2 packages depend on Python 2.7.
492         # Make sure we build with that for consistency.
493         python=python2.7
494         pip=pip
495         PACKAGE_PREFIX=$PYTHON2_PKG_PREFIX
496         ;;
497   esac
498
499   if [[ "$PKG" != "libpam-arvados" ]] &&
500      [[ "$PKG" != "arvados-node-manager" ]] &&
501      [[ "$PKG" != "arvados-docker-cleaner" ]]; then
502     PYTHON_PKG=$PACKAGE_PREFIX-$PKG
503   else
504     # Exception to our package naming convention
505     PYTHON_PKG=$PKG
506   fi
507
508   # arvados-python-client sdist should always be built, to be available
509   # for other dependent packages.
510   if [[ -n "$ONLY_BUILD" ]] && [[ "arvados-python-client" != "$PKG" ]] && [[ "$PYTHON_PKG" != "$ONLY_BUILD" ]] && [[ "$PKG" != "$ONLY_BUILD" ]]; then
511     return 0
512   fi
513
514   cd $WORKSPACE/$PKG_DIR
515
516   rm -rf dist/*
517
518   # Get the latest setuptools
519   if ! $pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U 'setuptools<45'; then
520     echo "Error, unable to upgrade setuptools with"
521     echo "  $pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U 'setuptools<45'"
522     exit 1
523   fi
524   # filter a useless warning (when building the cwltest package) from the stderr output
525   if ! $python setup.py $DASHQ_UNLESS_DEBUG sdist 2> >(grep -v 'warning: no previously-included files matching'); then
526     echo "Error, unable to run $python setup.py sdist for $PKG"
527     exit 1
528   fi
529
530   PACKAGE_PATH=`(cd dist; ls *tar.gz)`
531
532   if [[ "arvados-python-client" == "$PKG" ]]; then
533     PYSDK_PATH=`pwd`/dist/
534   fi
535
536   if [[ -n "$ONLY_BUILD" ]] && [[ "$PYTHON_PKG" != "$ONLY_BUILD" ]] && [[ "$PKG" != "$ONLY_BUILD" ]]; then
537     return 0
538   fi
539
540   # Determine the package version from the generated sdist archive
541   PYTHON_VERSION=${ARVADOS_BUILDING_VERSION:-$(awk '($1 == "Version:"){print $2}' *.egg-info/PKG-INFO)}
542
543   # See if we actually need to build this package; does it exist already?
544   # We can't do this earlier than here, because we need PYTHON_VERSION...
545   # This isn't so bad; the sdist call above is pretty quick compared to
546   # the invocation of virtualenv and fpm, below.
547   if ! test_package_presence "$PYTHON_PKG" $PYTHON_VERSION $PACKAGE_TYPE $ARVADOS_BUILDING_ITERATION; then
548     return 0
549   fi
550
551   echo "Building $FORMAT package for $PKG from $PKG_DIR"
552
553   # Package the sdist in a virtualenv
554   echo "Creating virtualenv..."
555
556   cd dist
557
558   rm -rf build
559   rm -f $PYTHON_PKG*deb
560   echo "virtualenv version: `virtualenv --version`"
561   virtualenv_command="virtualenv --python `which $python` $DASHQ_UNLESS_DEBUG build/usr/share/$python/dist/$PYTHON_PKG"
562
563   if ! $virtualenv_command; then
564     echo "Error, unable to run"
565     echo "  $virtualenv_command"
566     exit 1
567   fi
568
569   if ! build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U pip; then
570     echo "Error, unable to upgrade pip with"
571     echo "  build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U pip"
572     exit 1
573   fi
574   echo "pip version:        `build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip --version`"
575
576   if ! build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U 'setuptools<45'; then
577     echo "Error, unable to upgrade setuptools with"
578     echo "  build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U 'setuptools<45'"
579     exit 1
580   fi
581   echo "setuptools version: `build/usr/share/$python/dist/$PYTHON_PKG/bin/$python -c 'import setuptools; print(setuptools.__version__)'`"
582
583   if ! build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U wheel; then
584     echo "Error, unable to upgrade wheel with"
585     echo "  build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U wheel"
586     exit 1
587   fi
588   echo "wheel version:      `build/usr/share/$python/dist/$PYTHON_PKG/bin/wheel version`"
589
590   if [[ "$TARGET" != "centos7" ]] || [[ "$PYTHON_PKG" != "python-arvados-fuse" ]]; then
591     build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -f $PYSDK_PATH $PACKAGE_PATH
592   else
593     # centos7 needs these special tweaks to install python-arvados-fuse
594     build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG docutils
595     PYCURL_SSL_LIBRARY=nss build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -f $PYSDK_PATH $PACKAGE_PATH
596   fi
597
598   if [[ "$?" != "0" ]]; then
599     echo "Error, unable to run"
600     echo "  build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -f $PYSDK_PATH $PACKAGE_PATH"
601     exit 1
602   fi
603
604   cd build/usr/share/$python/dist/$PYTHON_PKG/
605
606   # Replace the shebang lines in all python scripts, and handle the activate
607   # scripts too. This is a functional replacement of the 237 line
608   # virtualenv_tools.py script that doesn't work in python3 without serious
609   # patching, minus the parts we don't need (modifying pyc files, etc).
610   for binfile in `ls bin/`; do
611     if ! file --mime bin/$binfile |grep -q binary; then
612       # Not a binary file
613       if [[ "$binfile" =~ ^activate(.csh|.fish|)$ ]]; then
614         # these 'activate' scripts need special treatment
615         sed -i "s/VIRTUAL_ENV=\".*\"/VIRTUAL_ENV=\"\/usr\/share\/$python\/dist\/$PYTHON_PKG\"/" bin/$binfile
616         sed -i "s/VIRTUAL_ENV \".*\"/VIRTUAL_ENV \"\/usr\/share\/$python\/dist\/$PYTHON_PKG\"/" bin/$binfile
617       else
618         if grep -q -E '^#!.*/bin/python\d?' bin/$binfile; then
619           # Replace shebang line
620           sed -i "1 s/^.*$/#!\/usr\/share\/$python\/dist\/$PYTHON_PKG\/bin\/python/" bin/$binfile
621         fi
622       fi
623     fi
624   done
625
626   cd - >$STDOUT_IF_DEBUG
627
628   find build -iname '*.pyc' -exec rm {} \;
629   find build -iname '*.pyo' -exec rm {} \;
630
631   # Finally, generate the package
632   echo "Creating package..."
633
634   declare -a COMMAND_ARR=("fpm" "-s" "dir" "-t" "$FORMAT")
635
636   if [[ "$MAINTAINER" != "" ]]; then
637     COMMAND_ARR+=('--maintainer' "$MAINTAINER")
638   fi
639
640   if [[ "$VENDOR" != "" ]]; then
641     COMMAND_ARR+=('--vendor' "$VENDOR")
642   fi
643
644   COMMAND_ARR+=('--url' 'https://arvados.org')
645
646   # Get description
647   DESCRIPTION=`grep '\sdescription' $WORKSPACE/$PKG_DIR/setup.py|cut -f2 -d=|sed -e "s/[',\\"]//g"`
648   COMMAND_ARR+=('--description' "$DESCRIPTION")
649
650   # Get license string
651   LICENSE_STRING=`grep license $WORKSPACE/$PKG_DIR/setup.py|cut -f2 -d=|sed -e "s/[',\\"]//g"`
652   COMMAND_ARR+=('--license' "$LICENSE_STRING")
653
654   # 12271 - As FPM-generated packages don't include scripts by default, the
655   # packages cleanup on upgrade depends on files being listed on the %files
656   # section in the generated SPEC files. To remove DIRECTORIES, they need to
657   # be listed in that section too, so we need to add this parameter to properly
658   # remove lingering dirs. But this only works for python2: if used on
659   # python33, it includes dirs like /opt/rh/python33 that belong to
660   # other packages.
661   if [[ "$FORMAT" == "rpm" ]] && [[ "$python" == "python2.7" ]]; then
662     COMMAND_ARR+=('--rpm-auto-add-directories')
663   fi
664
665   if [[ "$PKG" == "arvados-python-client" ]] || [[ "$PKG" == "arvados-fuse" ]]; then
666     if [[ "$python" == "python2.7" ]]; then
667       COMMAND_ARR+=('--conflicts' "$PYTHON3_PKG_PREFIX-$PKG")
668     else
669       COMMAND_ARR+=('--conflicts' "$PYTHON2_PKG_PREFIX-$PKG")
670     fi
671   fi
672
673   if [[ "$DEBUG" != "0" ]]; then
674     COMMAND_ARR+=('--verbose' '--log' 'info')
675   fi
676
677   COMMAND_ARR+=('-v' "$PYTHON_VERSION")
678   COMMAND_ARR+=('--iteration' "$ARVADOS_BUILDING_ITERATION")
679   COMMAND_ARR+=('-n' "$PYTHON_PKG")
680   COMMAND_ARR+=('-C' "build")
681
682   systemd_unit="$WORKSPACE/$PKG_DIR/$PKG.service"
683   if [[ -e "${systemd_unit}" ]]; then
684     COMMAND_ARR+=('--after-install' "${WORKSPACE}/build/go-python-package-scripts/postinst")
685     COMMAND_ARR+=('--before-remove' "${WORKSPACE}/build/go-python-package-scripts/prerm")
686   fi
687
688   if [[ "$python" == "python2.7" ]]; then
689     COMMAND_ARR+=('--depends' "$PYTHON2_PACKAGE")
690   else
691     COMMAND_ARR+=('--depends' "$PYTHON3_PACKAGE")
692   fi
693
694   # avoid warning
695   COMMAND_ARR+=('--deb-no-default-config-files')
696
697   # Append --depends X and other arguments specified by fpm-info.sh in
698   # the package source dir. These are added last so they can override
699   # the arguments added by this script.
700   declare -a fpm_args=()
701   declare -a fpm_depends=()
702
703   fpminfo="$WORKSPACE/$PKG_DIR/fpm-info.sh"
704   if [[ -e "$fpminfo" ]]; then
705     echo "Loading fpm overrides from $fpminfo"
706     if ! source "$fpminfo"; then
707       echo "Error, unable to source $WORKSPACE/$PKG_DIR/fpm-info.sh for $PKG"
708       exit 1
709     fi
710   fi
711
712   for i in "${fpm_depends[@]}"; do
713     COMMAND_ARR+=('--depends' "$i")
714   done
715
716   # make sure the systemd service file ends up in the right place
717   # used by arvados-docker-cleaner and arvados-node-manager
718   if [[ -e "${systemd_unit}" ]]; then
719     COMMAND_ARR+=("usr/share/$python/dist/$PKG/share/doc/$PKG/$PKG.service=/lib/systemd/system/$PKG.service")
720   fi
721
722   COMMAND_ARR+=("${fpm_args[@]}")
723
724   # Make sure to install all our package binaries in /usr/bin.
725   # We have to walk $WORKSPACE/$PKG_DIR/bin rather than
726   # $WORKSPACE/build/usr/share/$python/dist/$PYTHON_PKG/bin/ to get the list
727   # because the latter also includes all the python binaries for the virtualenv.
728   # We have to take the copies of our binaries from the latter directory, though,
729   # because those are the ones we rewrote the shebang line of, above.
730   if [[ -e "$WORKSPACE/$PKG_DIR/bin" ]]; then
731     for binary in `ls $WORKSPACE/$PKG_DIR/bin`; do
732       COMMAND_ARR+=("usr/share/$python/dist/$PYTHON_PKG/bin/$binary=/usr/bin/")
733     done
734   fi
735
736   # the libpam module should place a few files in the correct place for the pam
737   # subsystem
738   if [[ -e "$WORKSPACE/$PKG_DIR/dist/build/usr/share/$python/dist/$PYTHON_PKG/lib/security/libpam_arvados.py" ]]; then
739     COMMAND_ARR+=("usr/share/$python/dist/$PYTHON_PKG/lib/security/libpam_arvados.py=/usr/lib/security/")
740   fi
741   if [[ -e "$WORKSPACE/$PKG_DIR/dist/build/usr/share/$python/dist/$PYTHON_PKG/share/pam-configs/arvados" ]]; then
742     COMMAND_ARR+=("usr/share/$python/dist/$PYTHON_PKG/share/pam-configs/arvados=/usr/share/pam-configs/")
743   fi
744
745   # the python-arvados-cwl-runner package comes with cwltool, expose that version
746   if [[ -e "$WORKSPACE/$PKG_DIR/dist/build/usr/share/python2.7/dist/python-arvados-cwl-runner/bin/cwltool" ]]; then
747     COMMAND_ARR+=("usr/share/python2.7/dist/python-arvados-cwl-runner/bin/cwltool=/usr/bin/")
748   fi
749
750   COMMAND_ARR+=(".")
751
752   FPM_RESULTS=$("${COMMAND_ARR[@]}")
753   FPM_EXIT_CODE=$?
754
755   # if something went wrong and debug is off, print out the fpm command that errored
756   if ! fpm_verify $FPM_EXIT_CODE $FPM_RESULTS && [[ "$STDOUT_IF_DEBUG" == "/dev/null" ]]; then
757     echo "fpm returned an error executing the command:"
758     echo
759     echo -e "\n${COMMAND_ARR[@]}\n"
760   else
761     echo `ls *$FORMAT`
762     mv $WORKSPACE/$PKG_DIR/dist/*$FORMAT $WORKSPACE/packages/$TARGET/
763   fi
764   echo
765 }
766
767 # Build packages for everything
768 fpm_build () {
769   # Source dir where fpm-info.sh (if any) will be found.
770   SRC_DIR=$1
771   shift
772   # The package source.  Depending on the source type, this can be a
773   # path, or the name of the package in an upstream repository (e.g.,
774   # pip).
775   PACKAGE=$1
776   shift
777   # The name of the package to build.
778   PACKAGE_NAME=$1
779   shift
780   # The type of source package.  Passed to fpm -s.  Default "dir".
781   PACKAGE_TYPE=${1:-dir}
782   shift
783   # Optional: the package version number.  Passed to fpm -v.
784   VERSION=$1
785   shift
786
787   if [[ -n "$ONLY_BUILD" ]] && [[ "$PACKAGE_NAME" != "$ONLY_BUILD" ]] && [[ "$PACKAGE" != "$ONLY_BUILD" ]] ; then
788     # arvados-workbench depends on arvados-server at build time, so even when
789     # only arvados-workbench is being built, we need to build arvados-server too
790     if [[ "$PACKAGE_NAME" != "arvados-server" ]] || [[ "$ONLY_BUILD" != "arvados-workbench" ]]; then
791       return 0
792     fi
793   fi
794
795   local default_iteration_value="$(default_iteration "$PACKAGE" "$VERSION" "$PACKAGE_TYPE")"
796
797   declare -a COMMAND_ARR=("fpm" "-s" "$PACKAGE_TYPE" "-t" "$FORMAT")
798   if [ python = "$PACKAGE_TYPE" ] && [ deb = "$FORMAT" ]; then
799       # Dependencies are built from setup.py.  Since setup.py will never
800       # refer to Debian package iterations, it doesn't make sense to
801       # enforce those in the .deb dependencies.
802       COMMAND_ARR+=(--deb-ignore-iteration-in-dependencies)
803   fi
804
805   # 12271 - As FPM-generated packages don't include scripts by default, the
806   # packages cleanup on upgrade depends on files being listed on the %files
807   # section in the generated SPEC files. To remove DIRECTORIES, they need to
808   # be listed in that section too, so we need to add this parameter to properly
809   # remove lingering dirs. But this only works for python2: if used on
810   # python33, it includes dirs like /opt/rh/python33 that belong to
811   # other packages.
812   if [[ "$FORMAT" = rpm ]] && [[ "$python" = python2.7 ]]; then
813     COMMAND_ARR+=('--rpm-auto-add-directories')
814   fi
815
816   if [[ "$DEBUG" != "0" ]]; then
817     COMMAND_ARR+=('--verbose' '--log' 'info')
818   fi
819
820   if [[ -n "$PACKAGE_NAME" ]]; then
821     COMMAND_ARR+=('-n' "$PACKAGE_NAME")
822   fi
823
824   if [[ "$MAINTAINER" != "" ]]; then
825     COMMAND_ARR+=('--maintainer' "$MAINTAINER")
826   fi
827
828   if [[ "$VENDOR" != "" ]]; then
829     COMMAND_ARR+=('--vendor' "$VENDOR")
830   fi
831
832   if [[ "$VERSION" != "" ]]; then
833     COMMAND_ARR+=('-v' "$VERSION")
834   fi
835   if [[ -n "$default_iteration_value" ]]; then
836       # We can always add an --iteration here.  If another one is specified in $@,
837       # that will take precedence, as desired.
838       COMMAND_ARR+=(--iteration "$default_iteration_value")
839   fi
840
841   # Append --depends X and other arguments specified by fpm-info.sh in
842   # the package source dir. These are added last so they can override
843   # the arguments added by this script.
844   declare -a fpm_args=()
845   declare -a build_depends=()
846   declare -a fpm_depends=()
847   declare -a fpm_exclude=()
848   if [[ ! -d "$SRC_DIR" ]]; then
849       echo >&2 "BUG: looking in wrong dir for fpm-info.sh: $pkgdir"
850       exit 1
851   fi
852   fpminfo="${SRC_DIR}/fpm-info.sh"
853   if [[ -e "$fpminfo" ]]; then
854       debug_echo "Loading fpm overrides from $fpminfo"
855       source "$fpminfo"
856   fi
857   for pkg in "${build_depends[@]}"; do
858       if [[ $TARGET =~ debian|ubuntu ]]; then
859           pkg_deb=$(ls "$WORKSPACE/packages/$TARGET/$pkg_"*.deb | sort -rg | awk 'NR==1')
860           if [[ -e $pkg_deb ]]; then
861               echo "Installing build_dep $pkg from $pkg_deb"
862               dpkg -i "$pkg_deb"
863           else
864               echo "Attemping to install build_dep $pkg using apt-get"
865               apt-get install -y "$pkg"
866           fi
867           apt-get -y -f install
868       else
869           pkg_rpm=$(ls "$WORKSPACE/packages/$TARGET/$pkg"-[0-9]*.rpm | sort -rg | awk 'NR==1')
870           if [[ -e $pkg_rpm ]]; then
871               echo "Installing build_dep $pkg from $pkg_rpm"
872               rpm -i "$pkg_rpm"
873           else
874               echo "Attemping to install build_dep $pkg"
875               rpm -i "$pkg"
876           fi
877       fi
878   done
879   for i in "${fpm_depends[@]}"; do
880     COMMAND_ARR+=('--depends' "$i")
881   done
882   for i in "${fpm_exclude[@]}"; do
883     COMMAND_ARR+=('--exclude' "$i")
884   done
885
886   # Append remaining function arguments directly to fpm's command line.
887   for i; do
888     COMMAND_ARR+=("$i")
889   done
890
891   COMMAND_ARR+=("${fpm_args[@]}")
892
893   COMMAND_ARR+=("$PACKAGE")
894
895   debug_echo -e "\n${COMMAND_ARR[@]}\n"
896
897   FPM_RESULTS=$("${COMMAND_ARR[@]}")
898   FPM_EXIT_CODE=$?
899
900   fpm_verify $FPM_EXIT_CODE $FPM_RESULTS
901
902   # if something went wrong and debug is off, print out the fpm command that errored
903   if [[ 0 -ne $? ]] && [[ "$STDOUT_IF_DEBUG" == "/dev/null" ]]; then
904     echo -e "\n${COMMAND_ARR[@]}\n"
905   fi
906 }
907
908 # verify build results
909 fpm_verify () {
910   FPM_EXIT_CODE=$1
911   shift
912   FPM_RESULTS=$@
913
914   FPM_PACKAGE_NAME=''
915   if [[ $FPM_RESULTS =~ ([A-Za-z0-9_\.-]*\.)(deb|rpm) ]]; then
916     FPM_PACKAGE_NAME=${BASH_REMATCH[1]}${BASH_REMATCH[2]}
917   fi
918
919   if [[ "$FPM_PACKAGE_NAME" == "" ]]; then
920     EXITCODE=1
921     echo
922     echo "Error: $PACKAGE: Unable to figure out package name from fpm results:"
923     echo
924     echo $FPM_RESULTS
925     echo
926     return 1
927   elif [[ "$FPM_RESULTS" =~ "File already exists" ]]; then
928     echo "Package $FPM_PACKAGE_NAME exists, not rebuilding"
929     return 0
930   elif [[ 0 -ne "$FPM_EXIT_CODE" ]]; then
931     EXITCODE=1
932     echo "Error building package for $1:\n $FPM_RESULTS"
933     return 1
934   fi
935 }
936
937 install_package() {
938   PACKAGES=$@
939   if [[ "$FORMAT" == "deb" ]]; then
940     $SUDO apt-get install $PACKAGES --yes
941   elif [[ "$FORMAT" == "rpm" ]]; then
942     $SUDO yum -q -y install $PACKAGES
943   fi
944 }
945
946 title() {
947     printf '%s %s\n' "=======" "$1"
948 }
949
950 checkexit() {
951     if [[ "$1" != "0" ]]; then
952         title "$2 -- FAILED"
953         failures+=("$2 (`timer`)")
954     else
955         successes+=("$2 (`timer`)")
956     fi
957 }
958
959 timer_reset() {
960     t0=$SECONDS
961 }
962
963 timer() {
964     if [[ -n "$t0" ]]; then
965         echo -n "$(($SECONDS - $t0))s"
966     fi
967 }
968
969 report_outcomes() {
970     for x in "${successes[@]}"
971     do
972         echo "Pass: $x"
973     done
974
975     if [[ ${#failures[@]} == 0 ]]
976     then
977         if [[ ${#successes[@]} != 0 ]]; then
978            echo "All test suites passed."
979         fi
980     else
981         echo "Failures (${#failures[@]}):"
982         for x in "${failures[@]}"
983         do
984             echo "Fail: $x"
985         done
986     fi
987 }