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