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