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