19070: Still trying to fix test_with_arvbox
[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 # Usage: get_native_arch
83 get_native_arch() {
84   # Only amd64 and aarch64 are supported at the moment
85   local native_arch=""
86   case "$HOSTTYPE" in
87     x86_64)
88       native_arch="amd64"
89       ;;
90     aarch64)
91       native_arch="arm64"
92       ;;
93     *)
94       echo "Error: architecture not supported"
95       exit 1
96       ;;
97   esac
98   echo $native_arch
99 }
100
101 handle_ruby_gem() {
102     local gem_name="$1"; shift
103     local gem_version="$(nohash_version_from_git)"
104     local gem_src_dir="$(pwd)"
105
106     if [[ -n "$ONLY_BUILD" ]] && [[ "$gem_name" != "$ONLY_BUILD" ]] ; then
107         return 0
108     fi
109
110     if ! [[ -e "${gem_name}-${gem_version}.gem" ]]; then
111         find -maxdepth 1 -name "${gem_name}-*.gem" -delete
112
113         # -q appears to be broken in gem version 2.2.2
114         $GEM build "$gem_name.gemspec" $DASHQ_UNLESS_DEBUG >"$STDOUT_IF_DEBUG" 2>"$STDERR_IF_DEBUG"
115     fi
116 }
117
118 calculate_go_package_version() {
119   # $__returnvar has the nameref attribute set, which means it is a reference
120   # to another variable that is passed in as the first argument to this function.
121   # see https://www.gnu.org/software/bash/manual/html_node/Shell-Parameters.html
122   local -n __returnvar="$1"; shift
123   local oldpwd="$PWD"
124
125   cd "$WORKSPACE"
126   go mod download
127
128   # Update the version number and build a new package if the vendor
129   # bundle has changed, or the command imports anything from the
130   # Arvados SDK and the SDK has changed.
131   declare -a checkdirs=(go.mod go.sum)
132   while [ -n "$1" ]; do
133       checkdirs+=("$1")
134       shift
135   done
136   # Even our rails packages (version calculation happens here!) depend on a go component (arvados-server)
137   # Everything depends on the build directory.
138   checkdirs+=(sdk/go lib build)
139   local timestamp=0
140   for dir in ${checkdirs[@]}; do
141       cd "$WORKSPACE"
142       ts="$(timestamp_from_git "$dir")"
143       if [[ "$ts" -gt "$timestamp" ]]; then
144           version=$(version_from_git "$dir")
145           timestamp="$ts"
146       fi
147   done
148   cd "$oldpwd"
149   __returnvar="$version"
150 }
151
152 # Usage: package_go_binary services/foo arvados-foo [deb|rpm] [amd64|arm64] "Compute foo to arbitrary precision" [apache-2.0.txt]
153 package_go_binary() {
154   local src_path="$1"; shift
155   local prog="$1"; shift
156   local package_format="$1"; shift
157   local target_arch="$1"; shift
158   local description="$1"; shift
159   local license_file="${1:-agpl-3.0.txt}"; shift
160
161   if [[ -n "$ONLY_BUILD" ]] && [[ "$prog" != "$ONLY_BUILD" ]]; then
162     # arvados-workbench depends on arvados-server at build time, so even when
163     # only arvados-workbench is being built, we need to build arvados-server too
164     if [[ "$prog" != "arvados-server" ]] || [[ "$ONLY_BUILD" != "arvados-workbench" ]]; then
165       debug_echo -e "Skipping build of $prog package."
166       return 0
167     fi
168   fi
169
170   native_arch=$(get_native_arch)
171
172   if [[ "$native_arch" != "amd64" ]] && [[ -n "$target_arch" ]] && [[ "$native_arch" != "$target_arch" ]]; then
173     echo "Error: no cross compilation support for Go on $native_arch, can not build $prog for $target_arch"
174     return 1
175   fi
176
177   cross_compilation=1
178   if [[ "$TARGET" == "centos7" ]]; then
179     if [[ "$native_arch" == "amd64" ]] && [[ -n "$target_arch" ]] && [[ "$native_arch" != "$target_arch" ]]; then
180       echo "Error: no cross compilation support for Go on $native_arch for $TARGET, can not build $prog for $target_arch"
181       return 1
182     fi
183     cross_compilation=0
184   fi
185
186   if [[ "$package_format" == "deb" ]] &&
187      [[ "$TARGET" == "debian10" ]] || [[ "$TARGET" == "ubuntu1804" ]] || [[ "$TARGET" == "ubuntu2004" ]]; then
188     # Due to bug https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=983477 the libfuse-dev package for arm64 does
189     # not install properly side by side with the amd64 version before Debian 11.
190     if [[ "$native_arch" == "amd64" ]] && [[ -n "$target_arch" ]] && [[ "$native_arch" != "$target_arch" ]]; then
191       echo "Error: no cross compilation support for Go on $native_arch for $TARGET, can not build $prog for $target_arch"
192       return 1
193     fi
194     cross_compilation=0
195   fi
196
197   if [[ -n "$target_arch" ]]; then
198     archs=($target_arch)
199   else
200     # No target architecture specified, default to native target. When on amd64
201     # also crosscompile arm64 (when supported).
202     archs=($native_arch)
203     if [[ $cross_compilation -ne 0 ]]; then
204       archs+=("arm64")
205     fi
206   fi
207
208   for ta in ${archs[@]}; do
209     package_go_binary_worker "$src_path" "$prog" "$package_format" "$description" "$native_arch" "$ta" "$license_file"
210     retval=$?
211     if [[ $retval -ne 0 ]]; then
212       return $retval
213     fi
214   done
215 }
216
217 # Usage: package_go_binary services/foo arvados-foo deb "Compute foo to arbitrary precision" [amd64/arm64] [amd64/arm64] [apache-2.0.txt]
218 package_go_binary_worker() {
219     local src_path="$1"; shift
220     local prog="$1"; shift
221     local package_format="$1"; shift
222     local description="$1"; shift
223     local native_arch="${1:-amd64}"; shift
224     local target_arch="${1:-amd64}"; shift
225     local license_file="${1:-agpl-3.0.txt}"; shift
226
227     debug_echo "package_go_binary $src_path as $prog (native arch: $native_arch, target arch: $target_arch)"
228     local basename="${src_path##*/}"
229     calculate_go_package_version go_package_version $src_path
230
231     cd $WORKSPACE/packages/$TARGET
232     test_package_presence "$prog" "$go_package_version" "go" "" "$target_arch"
233     if [[ $? -ne 0 ]]; then
234       return 0
235     fi
236
237     echo "Building $package_format ($target_arch) package for $prog from $src_path"
238     if [[ "$native_arch" == "amd64" ]] && [[ "$target_arch" == "arm64" ]]; then
239       CGO_ENABLED=1 CC=aarch64-linux-gnu-gcc GOARCH=${target_arch} go install -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"
240     else
241       GOARCH=${arch} go install -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"
242     fi
243
244     local -a switches=()
245
246     binpath=$GOPATH/bin/${basename}
247     if [[ "${target_arch}" != "${native_arch}" ]]; then
248       switches+=("-a${target_arch}")
249       binpath="$GOPATH/bin/linux_${target_arch}/${basename}"
250     fi
251
252     systemd_unit="$WORKSPACE/${src_path}/${prog}.service"
253     if [[ -e "${systemd_unit}" ]]; then
254         switches+=(
255             --after-install "${WORKSPACE}/build/go-python-package-scripts/postinst"
256             --before-remove "${WORKSPACE}/build/go-python-package-scripts/prerm"
257             "${systemd_unit}=/lib/systemd/system/${prog}.service")
258     fi
259     switches+=("$WORKSPACE/${license_file}=/usr/share/doc/$prog/${license_file}")
260
261     fpm_build "${WORKSPACE}/${src_path}" "$binpath=/usr/bin/${prog}" "${prog}" dir "${go_package_version}" "--url=https://arvados.org" "--license=GNU Affero General Public License, version 3.0" "--description=${description}" "${switches[@]}"
262 }
263
264 # Usage: package_go_so lib/foo arvados_foo.so arvados-foo deb amd64 "Arvados foo library"
265 package_go_so() {
266     local src_path="$1"; shift
267     local sofile="$1"; shift
268     local pkg="$1"; shift
269     local package_format="$1"; shift
270     local target_arch="$1"; shift # supported: amd64, arm64
271     local description="$1"; shift
272
273     if [[ -n "$ONLY_BUILD" ]] && [[ "$pkg" != "$ONLY_BUILD" ]]; then
274       debug_echo -e "Skipping build of $pkg package."
275       return 0
276     fi
277
278     debug_echo "package_go_so $src_path as $pkg"
279
280     calculate_go_package_version go_package_version $src_path
281     cd $WORKSPACE/packages/$TARGET
282     test_package_presence $pkg $go_package_version go || return 1
283     cd $WORKSPACE/$src_path
284     go build -buildmode=c-shared -o ${GOPATH}/bin/${sofile}
285     cd $WORKSPACE/packages/$TARGET
286     local -a fpmargs=(
287         "--url=https://arvados.org"
288         "--license=Apache License, Version 2.0"
289         "--description=${description}"
290         "$WORKSPACE/apache-2.0.txt=/usr/share/doc/$pkg/apache-2.0.txt"
291     )
292     if [[ -e "$WORKSPACE/$src_path/pam-configs-arvados" ]]; then
293         fpmargs+=("$WORKSPACE/$src_path/pam-configs-arvados=/usr/share/doc/$pkg/pam-configs-arvados-go")
294     fi
295     if [[ -e "$WORKSPACE/$src_path/README" ]]; then
296         fpmargs+=("$WORKSPACE/$src_path/README=/usr/share/doc/$pkg/README")
297     fi
298     fpm_build "${WORKSPACE}/${src_path}" "$GOPATH/bin/${sofile}=/usr/lib/${sofile}" "${pkg}" dir "${go_package_version}" "${fpmargs[@]}"
299 }
300
301 default_iteration() {
302     if [[ -n "$ARVADOS_BUILDING_VERSION" ]]; then
303         echo "$ARVADOS_BUILDING_ITERATION"
304         return
305     fi
306     local package_name="$1"; shift
307     local package_version="$1"; shift
308     local package_type="$1"; shift
309     local iteration=1
310     if [[ $package_version =~ ^0\.1\.([0-9]{14})(\.|$) ]] && \
311            [[ ${BASH_REMATCH[1]} -le $LICENSE_PACKAGE_TS ]]; then
312         iteration=2
313     fi
314     echo $iteration
315 }
316
317 _build_rails_package_scripts() {
318     local pkgname="$1"; shift
319     local destdir="$1"; shift
320     local srcdir="$RUN_BUILD_PACKAGES_PATH/rails-package-scripts"
321     for scriptname in postinst prerm postrm; do
322         cat "$srcdir/$pkgname.sh" "$srcdir/step2.sh" "$srcdir/$scriptname.sh" \
323             >"$destdir/$scriptname" || return $?
324     done
325 }
326
327 rails_package_version() {
328     local pkgname="$1"; shift
329     local srcdir="$1"; shift
330     if [[ -n "$ARVADOS_BUILDING_VERSION" ]]; then
331         echo "$ARVADOS_BUILDING_VERSION"
332         return
333     fi
334     local version="$(version_from_git)"
335     if [ $pkgname = "arvados-api-server" -o $pkgname = "arvados-workbench" ] ; then
336         calculate_go_package_version version cmd/arvados-server "$srcdir"
337     fi
338     echo $version
339 }
340
341 test_rails_package_presence() {
342   local pkgname="$1"; shift
343   local srcdir="$1"; shift
344
345   if [[ -n "$ONLY_BUILD" ]] && [[ "$pkgname" != "$ONLY_BUILD" ]] ; then
346     return 1
347   fi
348
349   tmppwd=`pwd`
350
351   cd $srcdir
352
353   local version="$(rails_package_version "$pkgname" "$srcdir")"
354
355   cd $tmppwd
356
357   test_package_presence $pkgname $version rails "$RAILS_PACKAGE_ITERATION"
358 }
359
360 get_complete_package_name() {
361   # if the errexit flag is set, unset it until this function returns
362   # otherwise, the shift calls below will abort the program if optional arguments are not supplied
363   if [ -o errexit ]; then
364     set +e
365     trap 'set -e' RETURN
366   fi
367   # $__returnvar has the nameref attribute set, which means it is a reference
368   # to another variable that is passed in as the first argument to this function.
369   # see https://www.gnu.org/software/bash/manual/html_node/Shell-Parameters.html
370   local -n __returnvar="$1"; shift
371   local pkgname="$1"; shift
372   local version="$1"; shift
373   local pkgtype="$1"; shift
374   local iteration="$1"; shift
375   local arch="$1"; shift
376   if [[ "$iteration" == "" ]]; then
377       iteration="$(default_iteration "$pkgname" "$version" "$pkgtype")"
378   fi
379
380   if [[ "$arch" == "" ]]; then
381     native_arch=$(get_native_arch)
382     rpm_native_arch="x86_64"
383     if [[ "$HOSTTYPE" == "aarch64" ]]; then
384       rpm_native_arch="arm64"
385     fi
386     rpm_architecture="$rpm_native_arch"
387     deb_architecture="$native_arch"
388
389     if [[ "$pkgtype" =~ ^(src)$ ]]; then
390       rpm_architecture="noarch"
391       deb_architecture="all"
392     fi
393   else
394     rpm_architecture=$arch
395     deb_architecture=$arch
396   fi
397
398   local complete_pkgname="${pkgname}_$version${iteration:+-$iteration}_$deb_architecture.deb"
399   if [[ "$FORMAT" == "rpm" ]]; then
400       # rpm packages get iteration 1 if we don't supply one
401       iteration=${iteration:-1}
402       complete_pkgname="$pkgname-$version-${iteration}.$rpm_architecture.rpm"
403   fi
404   __returnvar=${complete_pkgname}
405 }
406
407 # Test if the package already exists, if not return 0, if it does return 1
408 test_package_presence() {
409     local pkgname="$1"; shift
410     local version="$1"; shift
411     local pkgtype="$1"; shift
412     local iteration="$1"; shift
413     local arch="$1"; shift
414     if [[ -n "$ONLY_BUILD" ]] && [[ "$pkgname" != "$ONLY_BUILD" ]] ; then
415       # arvados-workbench depends on arvados-server at build time, so even when
416       # only arvados-workbench is being built, we need to build arvados-server too
417       if [[ "$pkgname" != "arvados-server" ]] || [[ "$ONLY_BUILD" != "arvados-workbench" ]]; then
418         return 1
419       fi
420     fi
421
422     local full_pkgname
423     get_complete_package_name full_pkgname "$pkgname" "$version" "$pkgtype" "$iteration" "$arch"
424
425     # See if we can skip building the package, only if it already exists in the
426     # processed/ directory. If so, move it back to the packages directory to make
427     # sure it gets picked up by the test and/or upload steps.
428     # Get the list of packages from the repos
429
430     if [[ "$FORCE_BUILD" == "1" ]]; then
431       echo "Package $full_pkgname build forced with --force-build, building"
432     elif [[ "$FORMAT" == "deb" ]]; then
433       declare -A dd
434       dd[debian10]=buster
435       dd[debian11]=bullseye
436       dd[ubuntu1804]=bionic
437       dd[ubuntu2004]=focal
438       D=${dd[$TARGET]}
439       if [ ${pkgname:0:3} = "lib" ]; then
440         repo_subdir=${pkgname:0:4}
441       else
442         repo_subdir=${pkgname:0:1}
443       fi
444
445       repo_pkg_list=$(curl -s -o - http://apt.arvados.org/${D}/pool/main/${repo_subdir}/${pkgname}/)
446       echo "${repo_pkg_list}" |grep -q ${full_pkgname}
447       if [ $? -eq 0 ] ; then
448         echo "Package $full_pkgname exists upstream, not rebuilding, downloading instead!"
449         curl -s -o "$WORKSPACE/packages/$TARGET/${full_pkgname}" http://apt.arvados.org/${D}/pool/main/${repo_subdir}/${pkgname}/${full_pkgname}
450         return 1
451       elif test -f "$WORKSPACE/packages/$TARGET/processed/${full_pkgname}" ; then
452         echo "Package $full_pkgname exists, not rebuilding!"
453         return 1
454       else
455         echo "Package $full_pkgname not found, building"
456         return 0
457       fi
458     else
459       centos_repo="http://rpm.arvados.org/CentOS/7/dev/x86_64/"
460
461       repo_pkg_list=$(curl -s -o - ${centos_repo})
462       echo ${repo_pkg_list} |grep -q ${full_pkgname}
463       if [ $? -eq 0 ]; then
464         echo "Package $full_pkgname exists upstream, not rebuilding, downloading instead!"
465         curl -s -o "$WORKSPACE/packages/$TARGET/${full_pkgname}" ${centos_repo}${full_pkgname}
466         return 1
467       elif test -f "$WORKSPACE/packages/$TARGET/processed/${full_pkgname}" ; then
468         echo "Package $full_pkgname exists, not rebuilding!"
469         return 1
470       else
471         echo "Package $full_pkgname not found, building"
472         return 0
473       fi
474     fi
475 }
476
477 handle_rails_package() {
478     local pkgname="$1"; shift
479
480     if [[ -n "$ONLY_BUILD" ]] && [[ "$pkgname" != "$ONLY_BUILD" ]] ; then
481         return 0
482     fi
483     local srcdir="$1"; shift
484     cd "$srcdir"
485     local license_path="$1"; shift
486     local version="$(rails_package_version "$pkgname" "$srcdir")"
487     echo "$version" >package-build.version
488     local scripts_dir="$(mktemp --tmpdir -d "$pkgname-XXXXXXXX.scripts")" && \
489     (
490         set -e
491         _build_rails_package_scripts "$pkgname" "$scripts_dir"
492         cd "$srcdir"
493         mkdir -p tmp
494         git rev-parse HEAD >git-commit.version
495         bundle config set cache_all true
496         bundle package
497     )
498     if [[ 0 != "$?" ]] || ! cd "$WORKSPACE/packages/$TARGET"; then
499         echo "ERROR: $pkgname package prep failed" >&2
500         rm -rf "$scripts_dir"
501         EXITCODE=1
502         return 1
503     fi
504     local railsdir="/var/www/${pkgname%-server}/current"
505     local -a pos_args=("$srcdir/=$railsdir" "$pkgname" dir "$version")
506     local license_arg="$license_path=$railsdir/$(basename "$license_path")"
507     local -a switches=(--after-install "$scripts_dir/postinst"
508                        --before-remove "$scripts_dir/prerm"
509                        --after-remove "$scripts_dir/postrm")
510     if [[ -z "$ARVADOS_BUILDING_VERSION" ]]; then
511         switches+=(--iteration $RAILS_PACKAGE_ITERATION)
512     fi
513     # For some reason fpm excludes need to not start with /.
514     local exclude_root="${railsdir#/}"
515     local -a exclude_list=(tmp log coverage Capfile\* \
516                            config/deploy\* config/application.yml)
517     # for arvados-workbench, we need to have the (dummy) config/database.yml in the package
518     if  [[ "$pkgname" != "arvados-workbench" ]]; then
519       exclude_list+=('config/database.yml')
520     fi
521     for exclude in ${exclude_list[@]}; do
522         switches+=(-x "$exclude_root/$exclude")
523     done
524     fpm_build "${srcdir}" "${pos_args[@]}" "${switches[@]}" \
525               -x "$exclude_root/vendor/cache-*" \
526               -x "$exclude_root/vendor/bundle" "$@" "$license_arg"
527     rm -rf "$scripts_dir"
528 }
529
530 # Usage: handle_api_server [amd64|arm64]
531 handle_api_server () {
532   local target_arch="${1:-amd64}"; shift
533
534   if [[ -n "$ONLY_BUILD" ]] && [[ "$ONLY_BUILD" != "arvados-api-server" ]] ; then
535     debug_echo -e "Skipping build of arvados-api-server package."
536     return 0
537   fi
538
539   native_arch=$(get_native_arch)
540   if [[ "$target_arch" != "$native_arch" ]]; then
541     echo "Error: no cross compilation support for Rails yet, can not build arvados-api-server for $ARCH"
542     echo
543     exit 1
544   fi
545
546   # Build the API server package
547   test_rails_package_presence arvados-api-server "$WORKSPACE/services/api"
548   if [[ "$?" == "0" ]]; then
549     calculate_go_package_version arvados_server_version cmd/arvados-server
550     arvados_server_iteration=$(default_iteration "arvados-server" "$arvados_server_version" "go")
551     handle_rails_package arvados-api-server "$WORKSPACE/services/api" \
552         "$WORKSPACE/agpl-3.0.txt" --url="https://arvados.org" \
553         --description="Arvados API server - Arvados is a free and open source platform for big data science." \
554         --license="GNU Affero General Public License, version 3.0" --depends "arvados-server = ${arvados_server_version}-${arvados_server_iteration}"
555   fi
556 }
557
558 # Usage: handle_workbench [amd64|arm64]
559 handle_workbench () {
560   local target_arch="${1:-amd64}"; shift
561   if [[ -n "$ONLY_BUILD" ]] && [[ "$ONLY_BUILD" != "arvados-workbench" ]] ; then
562     debug_echo -e "Skipping build of arvados-workbench package."
563     return 0
564   fi
565
566   native_arch=$(get_native_arch)
567   if [[ "$target_arch" != "$native_arch" ]]; then
568     echo "Error: no cross compilation support for Rails yet, can not build arvados-workbench for $native_arch"
569     echo
570     exit 1
571   fi
572
573   if [[ "$native_arch" != "amd64" ]]; then
574     echo "Error: building the arvados-workbench package is not yet supported on this architecture ($native_arch)."
575     echo
576     exit 1
577   fi
578
579   # Build the workbench server package
580   test_rails_package_presence arvados-workbench "$WORKSPACE/apps/workbench"
581   if [[ "$?" == "0" ]] ; then
582     calculate_go_package_version arvados_server_version cmd/arvados-server
583     arvados_server_iteration=$(default_iteration "arvados-server" "$arvados_server_version" "go")
584
585     (
586         set -e
587
588         # The workbench package has a build-time dependency on the arvados-server
589         # package for config manipulation, so install it first.
590         cd $WORKSPACE/cmd/arvados-server
591         get_complete_package_name arvados_server_pkgname arvados-server ${arvados_server_version} go
592
593         arvados_server_pkg_path="$WORKSPACE/packages/$TARGET/${arvados_server_pkgname}"
594         if [[ ! -e ${arvados_server_pkg_path} ]]; then
595           arvados_server_pkg_path="$WORKSPACE/packages/$TARGET/processed/${arvados_server_pkgname}"
596         fi
597         if [[ "$FORMAT" == "deb" ]]; then
598           dpkg -i ${arvados_server_pkg_path}
599         else
600           rpm -i ${arvados_server_pkg_path}
601         fi
602
603         cd "$WORKSPACE/apps/workbench"
604
605         # We need to bundle to be ready even when we build a package without vendor directory
606         # because asset compilation requires it.
607         bundle config set --local system 'true' >"$STDOUT_IF_DEBUG"
608         bundle install >"$STDOUT_IF_DEBUG"
609
610         # clear the tmp directory; the asset generation step will recreate tmp/cache/assets,
611         # and we want that in the package, so it's easier to not exclude the tmp directory
612         # from the package - empty it instead.
613         rm -rf tmp
614         mkdir tmp
615
616         # Set up an appropriate config.yml
617         arvados-server config-dump -config <(cat /etc/arvados/config.yml 2>/dev/null || echo  "Clusters: {zzzzz: {}}") > /tmp/x
618         mkdir -p /etc/arvados/
619         mv /tmp/x /etc/arvados/config.yml
620         perl -p -i -e 'BEGIN{undef $/;} s/WebDAV(.*?):\n( *)ExternalURL: ""/WebDAV$1:\n$2ExternalURL: "example.com"/g' /etc/arvados/config.yml
621
622         ARVADOS_CONFIG=none RAILS_ENV=production RAILS_GROUPS=assets bin/rake npm:install >"$STDOUT_IF_DEBUG"
623         ARVADOS_CONFIG=none RAILS_ENV=production RAILS_GROUPS=assets bin/rake assets:precompile >"$STDOUT_IF_DEBUG"
624
625         # Remove generated configuration files so they don't go in the package.
626         rm -rf /etc/arvados/
627     )
628
629     if [[ "$?" != "0" ]]; then
630       echo "ERROR: Asset precompilation failed"
631       EXITCODE=1
632     else
633       handle_rails_package arvados-workbench "$WORKSPACE/apps/workbench" \
634           "$WORKSPACE/agpl-3.0.txt" --url="https://arvados.org" \
635           --description="Arvados Workbench - Arvados is a free and open source platform for big data science." \
636           --license="GNU Affero General Public License, version 3.0" --depends "arvados-server = ${arvados_server_version}-${arvados_server_iteration}"
637     fi
638   fi
639 }
640
641 # Usage: handle_cwltest [deb|rpm] [amd64|arm64]
642 handle_cwltest () {
643   local package_format="$1"; shift
644   local target_arch="${1:-amd64}"; shift
645
646   if [[ -n "$ONLY_BUILD" ]] && [[ "$ONLY_BUILD" != "python3-cwltest" ]] ; then
647     debug_echo -e "Skipping build of cwltest package."
648     return 0
649   fi
650   cd "$WORKSPACE"
651   if [[ -e "$WORKSPACE/cwltest" ]]; then
652     rm -rf "$WORKSPACE/cwltest"
653   fi
654   git clone https://github.com/common-workflow-language/cwltest.git
655   # signal to our build script that we want a cwltest executable installed in /usr/bin/
656   mkdir cwltest/bin && touch cwltest/bin/cwltest
657   fpm_build_virtualenv "cwltest" "cwltest" "$package_format" "$target_arch"
658   # The python->python3 metapackage
659   build_metapackage "cwltest" "cwltest"
660   cd "$WORKSPACE"
661   rm -rf "$WORKSPACE/cwltest"
662 }
663
664 # Usage: handle_arvados_src
665 handle_arvados_src () {
666   if [[ -n "$ONLY_BUILD" ]] && [[ "$ONLY_BUILD" != "arvados-src" ]] ; then
667     debug_echo -e "Skipping build of arvados-src package."
668     return 0
669   fi
670   # arvados-src
671   (
672       cd "$WORKSPACE"
673       COMMIT_HASH=$(format_last_commit_here "%H")
674       arvados_src_version="$(version_from_git)"
675
676       cd $WORKSPACE/packages/$TARGET
677       test_package_presence arvados-src "$arvados_src_version" src ""
678
679       if [[ "$?" == "0" ]]; then
680         cd "$WORKSPACE"
681         SRC_BUILD_DIR=$(mktemp -d)
682         # mktemp creates the directory with 0700 permissions by default
683         chmod 755 $SRC_BUILD_DIR
684         git clone $DASHQ_UNLESS_DEBUG "$WORKSPACE/.git" "$SRC_BUILD_DIR"
685         cd "$SRC_BUILD_DIR"
686
687         # go into detached-head state
688         git checkout $DASHQ_UNLESS_DEBUG "$COMMIT_HASH"
689         echo "$COMMIT_HASH" >git-commit.version
690
691         cd $WORKSPACE/packages/$TARGET
692         fpm_build "$WORKSPACE" $SRC_BUILD_DIR/=/usr/local/arvados/src arvados-src 'dir' "$arvados_src_version" "--exclude=usr/local/arvados/src/.git" "--url=https://arvados.org" "--license=GNU Affero General Public License, version 3.0" "--description=The Arvados source code" "--architecture=all"
693
694         rm -rf "$SRC_BUILD_DIR"
695       fi
696   )
697 }
698
699 # Usage: handle_libarvados_perl
700 handle_libarvados_perl () {
701   if [[ -n "$ONLY_BUILD" ]] && [[ "$ONLY_BUILD" != "libarvados-perl" ]] ; then
702     debug_echo -e "Skipping build of libarvados-perl package."
703     return 0
704   fi
705   # The perl sdk subdirectory is so old that it has no tag in its history,
706   # which causes version_at_commit.sh to fail. Just rebuild it every time.
707   cd "$WORKSPACE"
708   libarvados_perl_version="$(version_from_git)"
709   cd "$WORKSPACE/sdk/perl"
710
711   cd $WORKSPACE/packages/$TARGET
712   test_package_presence libarvados-perl "$libarvados_perl_version"
713
714   if [[ "$?" == "0" ]]; then
715     cd "$WORKSPACE/sdk/perl"
716
717     if [[ -e Makefile ]]; then
718       make realclean >"$STDOUT_IF_DEBUG"
719     fi
720     find -maxdepth 1 \( -name 'MANIFEST*' -or -name "libarvados-perl*.$FORMAT" \) \
721         -delete
722     rm -rf install
723
724     perl Makefile.PL INSTALL_BASE=install >"$STDOUT_IF_DEBUG" && \
725         make install INSTALLDIRS=perl >"$STDOUT_IF_DEBUG" && \
726         fpm_build "$WORKSPACE/sdk/perl" install/lib/=/usr/share libarvados-perl \
727         dir "$libarvados_perl_version" install/man/=/usr/share/man \
728         "$WORKSPACE/apache-2.0.txt=/usr/share/doc/libarvados-perl/apache-2.0.txt" && \
729         mv --no-clobber libarvados-perl*.$FORMAT "$WORKSPACE/packages/$TARGET/"
730   fi
731 }
732
733 # Build python packages with a virtualenv built-in
734 # Usage: fpm_build_virtualenv arvados-python-client sdk/python [deb|rpm] [amd64|arm64]
735 fpm_build_virtualenv () {
736   local pkg=$1; shift
737   local pkg_dir=$1; shift
738   local package_format="$1"; shift
739   local target_arch="${1:-amd64}"; shift
740
741   native_arch=$(get_native_arch)
742
743   if [[ "$pkg" != "arvados-docker-cleaner" ]]; then
744     PYTHON_PKG=$PYTHON3_PKG_PREFIX-$pkg
745   else
746     # Exception to our package naming convention
747     PYTHON_PKG=$pkg
748   fi
749
750   if [[ -n "$ONLY_BUILD" ]] && [[ "$PYTHON_PKG" != "$ONLY_BUILD" ]]; then
751     # arvados-python-client sdist should always be built if we are building a
752     # python package.
753     if [[ "$ONLY_BUILD" != "python3-arvados-cwl-runner" ]] &&
754        [[ "$ONLY_BUILD" != "python3-arvados-fuse" ]] &&
755        [[ "$ONLY_BUILD" != "python3-crunchstat-summary" ]] &&
756        [[ "$ONLY_BUILD" != "arvados-docker-cleaner" ]] &&
757        [[ "$ONLY_BUILD" != "python3-arvados-user-activity" ]]; then
758       debug_echo -e "Skipping build of $pkg package."
759       return 0
760     fi
761   fi
762
763   if [[ -n "$target_arch" ]] && [[ "$native_arch" == "$target_arch" ]]; then
764       fpm_build_virtualenv_worker "$pkg" "$pkg_dir" "$package_format" "$native_arch" "$target_arch"
765   elif [[ -z "$target_arch" ]]; then
766     fpm_build_virtualenv_worker "$pkg" "$pkg_dir" "$package_format" "$native_arch" "$native_arch"
767   else
768     echo "Error: no cross compilation support for Python yet, can not build $pkg for $target_arch"
769     return 1
770   fi
771 }
772
773 # Build python packages with a virtualenv built-in
774 # Usage: fpm_build_virtualenv_worker arvados-python-client sdk/python python3 [deb|rpm] [amd64|arm64] [amd64|arm64]
775 fpm_build_virtualenv_worker () {
776   PKG=$1; shift
777   PKG_DIR=$1; shift
778   local package_format="$1"; shift
779   local native_arch="${1:-amd64}"; shift
780   local target_arch=${1:-amd64}; shift
781
782   # Set up
783   STDOUT_IF_DEBUG=/dev/null
784   STDERR_IF_DEBUG=/dev/null
785   DASHQ_UNLESS_DEBUG=-q
786   if [[ "$DEBUG" != "0" ]]; then
787       STDOUT_IF_DEBUG=/dev/stdout
788       STDERR_IF_DEBUG=/dev/stderr
789       DASHQ_UNLESS_DEBUG=
790   fi
791   if [[ "$ARVADOS_BUILDING_ITERATION" == "" ]]; then
792     ARVADOS_BUILDING_ITERATION=1
793   fi
794
795   local python=$PYTHON3_EXECUTABLE
796   pip=pip3
797   PACKAGE_PREFIX=$PYTHON3_PKG_PREFIX
798
799   if [[ "$PKG" != "arvados-docker-cleaner" ]]; then
800     PYTHON_PKG=$PACKAGE_PREFIX-$PKG
801   else
802     # Exception to our package naming convention
803     PYTHON_PKG=$PKG
804   fi
805
806   cd $WORKSPACE/$PKG_DIR
807
808   rm -rf dist/*
809
810   # Get the latest setuptools
811   if ! $pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U 'setuptools<45'; then
812     echo "Error, unable to upgrade setuptools with"
813     echo "  $pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U 'setuptools<45'"
814     exit 1
815   fi
816   # filter a useless warning (when building the cwltest package) from the stderr output
817   if ! $python setup.py $DASHQ_UNLESS_DEBUG sdist 2> >(grep -v 'warning: no previously-included files matching'); then
818     echo "Error, unable to run $python setup.py sdist for $PKG"
819     exit 1
820   fi
821
822   PACKAGE_PATH=`(cd dist; ls *tar.gz)`
823
824   if [[ "arvados-python-client" == "$PKG" ]]; then
825     PYSDK_PATH=`pwd`/dist/
826   fi
827
828   if [[ -n "$ONLY_BUILD" ]] && [[ "$PYTHON_PKG" != "$ONLY_BUILD" ]] && [[ "$PKG" != "$ONLY_BUILD" ]]; then
829     return 0
830   fi
831
832   # Determine the package version from the generated sdist archive
833   if [[ -n "$ARVADOS_BUILDING_VERSION" ]] ; then
834       UNFILTERED_PYTHON_VERSION=$ARVADOS_BUILDING_VERSION
835       PYTHON_VERSION=$(echo -n $ARVADOS_BUILDING_VERSION | sed s/~dev/.dev/g | sed s/~rc/rc/g)
836   else
837       PYTHON_VERSION=$(awk '($1 == "Version:"){print $2}' *.egg-info/PKG-INFO)
838       UNFILTERED_PYTHON_VERSION=$(echo -n $PYTHON_VERSION | sed s/\.dev/~dev/g |sed 's/\([0-9]\)rc/\1~rc/g')
839   fi
840
841   # See if we actually need to build this package; does it exist already?
842   # We can't do this earlier than here, because we need PYTHON_VERSION...
843   # This isn't so bad; the sdist call above is pretty quick compared to
844   # the invocation of virtualenv and fpm, below.
845   if ! test_package_presence "$PYTHON_PKG" "$UNFILTERED_PYTHON_VERSION" "$python" "$ARVADOS_BUILDING_ITERATION" "$target_arch"; then
846     return 0
847   fi
848
849   echo "Building $package_format ($target_arch) package for $PKG from $PKG_DIR"
850
851   # Package the sdist in a virtualenv
852   echo "Creating virtualenv..."
853
854   cd dist
855
856   rm -rf build
857   rm -f $PYTHON_PKG*deb
858   echo "virtualenv version: `virtualenv --version`"
859   virtualenv_command="virtualenv --python `which $python` $DASHQ_UNLESS_DEBUG build/usr/share/$python/dist/$PYTHON_PKG"
860
861   if ! $virtualenv_command; then
862     echo "Error, unable to run"
863     echo "  $virtualenv_command"
864     exit 1
865   fi
866
867   if ! build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U pip; then
868     echo "Error, unable to upgrade pip with"
869     echo "  build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U pip"
870     exit 1
871   fi
872   echo "pip version:        `build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip --version`"
873
874   if ! build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U 'setuptools<45'; then
875     echo "Error, unable to upgrade setuptools with"
876     echo "  build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U 'setuptools<45'"
877     exit 1
878   fi
879   echo "setuptools version: `build/usr/share/$python/dist/$PYTHON_PKG/bin/$python -c 'import setuptools; print(setuptools.__version__)'`"
880
881   if ! build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U wheel; then
882     echo "Error, unable to upgrade wheel with"
883     echo "  build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U wheel"
884     exit 1
885   fi
886   echo "wheel version:      `build/usr/share/$python/dist/$PYTHON_PKG/bin/wheel version`"
887
888   if [[ "$TARGET" != "centos7" ]] || [[ "$PYTHON_PKG" != "python-arvados-fuse" ]]; then
889     build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -f $PYSDK_PATH $PACKAGE_PATH
890   else
891     # centos7 needs these special tweaks to install python-arvados-fuse
892     build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG docutils
893     PYCURL_SSL_LIBRARY=nss build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -f $PYSDK_PATH $PACKAGE_PATH
894   fi
895
896   if [[ "$?" != "0" ]]; then
897     echo "Error, unable to run"
898     echo "  build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -f $PYSDK_PATH $PACKAGE_PATH"
899     exit 1
900   fi
901
902   cd build/usr/share/$python/dist/$PYTHON_PKG/
903
904   # Replace the shebang lines in all python scripts, and handle the activate
905   # scripts too. This is a functional replacement of the 237 line
906   # virtualenv_tools.py script that doesn't work in python3 without serious
907   # patching, minus the parts we don't need (modifying pyc files, etc).
908   for binfile in `ls bin/`; do
909     if ! file --mime bin/$binfile |grep -q binary; then
910       # Not a binary file
911       if [[ "$binfile" =~ ^activate(.csh|.fish|)$ ]]; then
912         # these 'activate' scripts need special treatment
913         sed -i "s/VIRTUAL_ENV=\".*\"/VIRTUAL_ENV=\"\/usr\/share\/$python\/dist\/$PYTHON_PKG\"/" bin/$binfile
914         sed -i "s/VIRTUAL_ENV \".*\"/VIRTUAL_ENV \"\/usr\/share\/$python\/dist\/$PYTHON_PKG\"/" bin/$binfile
915       else
916         if grep -q -E '^#!.*/bin/python\d?' bin/$binfile; then
917           # Replace shebang line
918           sed -i "1 s/^.*$/#!\/usr\/share\/$python\/dist\/$PYTHON_PKG\/bin\/python/" bin/$binfile
919         fi
920       fi
921     fi
922   done
923
924   cd - >$STDOUT_IF_DEBUG
925
926   find build -iname '*.pyc' -exec rm {} \;
927   find build -iname '*.pyo' -exec rm {} \;
928
929   # Finally, generate the package
930   echo "Creating package..."
931
932   declare -a COMMAND_ARR=("fpm" "-s" "dir" "-t" "$package_format")
933
934   if [[ -n "$target_arch" ]] && [[ "$target_arch" != "amd64" ]]; then
935     COMMAND_ARR+=("-a$target_arch")
936   fi
937
938   if [[ "$MAINTAINER" != "" ]]; then
939     COMMAND_ARR+=('--maintainer' "$MAINTAINER")
940   fi
941
942   if [[ "$VENDOR" != "" ]]; then
943     COMMAND_ARR+=('--vendor' "$VENDOR")
944   fi
945
946   COMMAND_ARR+=('--url' 'https://arvados.org')
947
948   # Get description
949   DESCRIPTION=`grep '\sdescription' $WORKSPACE/$PKG_DIR/setup.py|cut -f2 -d=|sed -e "s/[',\\"]//g"`
950   COMMAND_ARR+=('--description' "$DESCRIPTION")
951
952   # Get license string
953   LICENSE_STRING=`grep license $WORKSPACE/$PKG_DIR/setup.py|cut -f2 -d=|sed -e "s/[',\\"]//g"`
954   COMMAND_ARR+=('--license' "$LICENSE_STRING")
955
956   if [[ "$package_format" == "rpm" ]]; then
957     # Make sure to conflict with the old rh-python36 packages we used to publish
958     COMMAND_ARR+=('--conflicts' "rh-python36-python-$PKG")
959   fi
960
961   if [[ "$DEBUG" != "0" ]]; then
962     COMMAND_ARR+=('--verbose' '--log' 'info')
963   fi
964
965   COMMAND_ARR+=('-v' $(echo -n "$PYTHON_VERSION" | sed s/.dev/~dev/g | sed s/rc/~rc/g))
966   COMMAND_ARR+=('--iteration' "$ARVADOS_BUILDING_ITERATION")
967   COMMAND_ARR+=('-n' "$PYTHON_PKG")
968   COMMAND_ARR+=('-C' "build")
969
970   systemd_unit="$WORKSPACE/$PKG_DIR/$PKG.service"
971   if [[ -e "${systemd_unit}" ]]; then
972     COMMAND_ARR+=('--after-install' "${WORKSPACE}/build/go-python-package-scripts/postinst")
973     COMMAND_ARR+=('--before-remove' "${WORKSPACE}/build/go-python-package-scripts/prerm")
974   fi
975
976   COMMAND_ARR+=('--depends' "$PYTHON3_PACKAGE")
977
978   # avoid warning
979   COMMAND_ARR+=('--deb-no-default-config-files')
980
981   # Append --depends X and other arguments specified by fpm-info.sh in
982   # the package source dir. These are added last so they can override
983   # the arguments added by this script.
984   declare -a fpm_args=()
985   declare -a fpm_depends=()
986
987   fpminfo="$WORKSPACE/$PKG_DIR/fpm-info.sh"
988   if [[ -e "$fpminfo" ]]; then
989     echo "Loading fpm overrides from $fpminfo"
990     if ! source "$fpminfo"; then
991       echo "Error, unable to source $WORKSPACE/$PKG_DIR/fpm-info.sh for $PKG"
992       exit 1
993     fi
994   fi
995
996   for i in "${fpm_depends[@]}"; do
997     COMMAND_ARR+=('--depends' "$i")
998   done
999
1000   for i in "${fpm_depends[@]}"; do
1001     COMMAND_ARR+=('--replaces' "python-$PKG")
1002   done
1003
1004   # make sure the systemd service file ends up in the right place
1005   # used by arvados-docker-cleaner
1006   if [[ -e "${systemd_unit}" ]]; then
1007     COMMAND_ARR+=("usr/share/$python/dist/$PKG/share/doc/$PKG/$PKG.service=/lib/systemd/system/$PKG.service")
1008   fi
1009
1010   COMMAND_ARR+=("${fpm_args[@]}")
1011
1012   # Make sure to install all our package binaries in /usr/bin.
1013   # We have to walk $WORKSPACE/$PKG_DIR/bin rather than
1014   # $WORKSPACE/build/usr/share/$python/dist/$PYTHON_PKG/bin/ to get the list
1015   # because the latter also includes all the python binaries for the virtualenv.
1016   # We have to take the copies of our binaries from the latter directory, though,
1017   # because those are the ones we rewrote the shebang line of, above.
1018   if [[ -e "$WORKSPACE/$PKG_DIR/bin" ]]; then
1019     for binary in `ls $WORKSPACE/$PKG_DIR/bin`; do
1020       COMMAND_ARR+=("usr/share/$python/dist/$PYTHON_PKG/bin/$binary=/usr/bin/")
1021     done
1022   fi
1023
1024   # the python3-arvados-cwl-runner package comes with cwltool, expose that version
1025   if [[ -e "$WORKSPACE/$PKG_DIR/dist/build/usr/share/$python/dist/$PYTHON_PKG/bin/cwltool" ]]; then
1026     COMMAND_ARR+=("usr/share/$python/dist/$PYTHON_PKG/bin/cwltool=/usr/bin/")
1027   fi
1028
1029   COMMAND_ARR+=(".")
1030
1031   debug_echo -e "\n${COMMAND_ARR[@]}\n"
1032
1033   FPM_RESULTS=$("${COMMAND_ARR[@]}")
1034   FPM_EXIT_CODE=$?
1035
1036   # if something went wrong and debug is off, print out the fpm command that errored
1037   if ! fpm_verify $FPM_EXIT_CODE $FPM_RESULTS && [[ "$STDOUT_IF_DEBUG" == "/dev/null" ]]; then
1038     echo "fpm returned an error executing the command:"
1039     echo
1040     echo -e "\n${COMMAND_ARR[@]}\n"
1041   else
1042     echo `ls *$package_format`
1043     mv $WORKSPACE/$PKG_DIR/dist/*$package_format $WORKSPACE/packages/$TARGET/
1044   fi
1045   echo
1046 }
1047
1048 # build_metapackage builds meta packages that help with the python to python 3 package migration
1049 build_metapackage() {
1050   # base package name (e.g. arvados-python-client)
1051   BASE_NAME=$1
1052   shift
1053   PKG_DIR=$1
1054   shift
1055
1056   if [[ -n "$ONLY_BUILD" ]] && [[ "python-$BASE_NAME" != "$ONLY_BUILD" ]]; then
1057     return 0
1058   fi
1059
1060   if [[ "$ARVADOS_BUILDING_ITERATION" == "" ]]; then
1061     ARVADOS_BUILDING_ITERATION=1
1062   fi
1063
1064   if [[ -z "$ARVADOS_BUILDING_VERSION" ]]; then
1065     cd $WORKSPACE/$PKG_DIR
1066     pwd
1067     rm -rf dist/*
1068
1069     # Get the latest setuptools
1070     if ! pip3 install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U 'setuptools<45'; then
1071       echo "Error, unable to upgrade setuptools with XY"
1072       echo "  pip3 install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U 'setuptools<45'"
1073       exit 1
1074     fi
1075     # filter a useless warning (when building the cwltest package) from the stderr output
1076     if ! python3 setup.py $DASHQ_UNLESS_DEBUG sdist 2> >(grep -v 'warning: no previously-included files matching'); then
1077       echo "Error, unable to run python3 setup.py sdist for $PKG"
1078       exit 1
1079     fi
1080
1081     PYTHON_VERSION=$(awk '($1 == "Version:"){print $2}' *.egg-info/PKG-INFO)
1082     UNFILTERED_PYTHON_VERSION=$(echo -n $PYTHON_VERSION | sed s/\.dev/~dev/g |sed 's/\([0-9]\)rc/\1~rc/g')
1083
1084   else
1085     UNFILTERED_PYTHON_VERSION=$ARVADOS_BUILDING_VERSION
1086     PYTHON_VERSION=$(echo -n $ARVADOS_BUILDING_VERSION | sed s/~dev/.dev/g | sed s/~rc/rc/g)
1087   fi
1088
1089   cd - >$STDOUT_IF_DEBUG
1090   if [[ -d "$BASE_NAME" ]]; then
1091     rm -rf $BASE_NAME
1092   fi
1093   mkdir $BASE_NAME
1094   cd $BASE_NAME
1095
1096   if [[ "$FORMAT" == "deb" ]]; then
1097     cat >ns-control <<EOF
1098 Section: misc
1099 Priority: optional
1100 Standards-Version: 3.9.2
1101
1102 Package: python-${BASE_NAME}
1103 Version: ${PYTHON_VERSION}-${ARVADOS_BUILDING_ITERATION}
1104 Maintainer: Arvados Package Maintainers <packaging@arvados.org>
1105 Depends: python3-${BASE_NAME}
1106 Description: metapackage to ease the upgrade to the Pyhon 3 version of ${BASE_NAME}
1107  This package is a metapackage that will automatically install the new version of
1108  ${BASE_NAME} which is Python 3 based and has a different name.
1109 EOF
1110
1111     /usr/bin/equivs-build ns-control
1112     if [[ $? -ne 0 ]]; then
1113       echo "Error running 'equivs-build ns-control', is the 'equivs' package installed?"
1114       return 1
1115     fi
1116   elif [[ "$FORMAT" == "rpm" ]]; then
1117     cat >meta.spec <<EOF
1118 Summary: metapackage to ease the upgrade to the Python 3 version of ${BASE_NAME}
1119 Name: python-${BASE_NAME}
1120 Version: ${PYTHON_VERSION}
1121 Release: ${ARVADOS_BUILDING_ITERATION}
1122 License: distributable
1123
1124 Requires: python3-${BASE_NAME}
1125
1126 %description
1127 This package is a metapackage that will automatically install the new version of
1128 python-${BASE_NAME} which is Python 3 based and has a different name.
1129
1130 %prep
1131
1132 %build
1133
1134 %clean
1135
1136 %install
1137
1138 %post
1139
1140 %files
1141
1142
1143 %changelog
1144 * Mon Apr 12 2021 Arvados Package Maintainers <packaging@arvados.org>
1145 - initial release
1146 EOF
1147
1148     /usr/bin/rpmbuild -ba meta.spec
1149     if [[ $? -ne 0 ]]; then
1150       echo "Error running 'rpmbuild -ba meta.spec', is the 'rpm-build' package installed?"
1151       return 1
1152     else
1153       mv /root/rpmbuild/RPMS/x86_64/python-${BASE_NAME}*.${FORMAT} .
1154       if [[ $? -ne 0 ]]; then
1155         echo "Error finding rpm file output of 'rpmbuild -ba meta.spec'"
1156         return 1
1157       fi
1158     fi
1159   else
1160     echo "Unknown format"
1161     return 1
1162   fi
1163
1164   if [[ $EXITCODE -ne 0 ]]; then
1165     return 1
1166   else
1167     echo `ls *$FORMAT`
1168     mv *$FORMAT $WORKSPACE/packages/$TARGET/
1169   fi
1170
1171   # clean up
1172   cd - >$STDOUT_IF_DEBUG
1173   if [[ -d "$BASE_NAME" ]]; then
1174     rm -rf $BASE_NAME
1175   fi
1176 }
1177
1178 # Build packages for everything
1179 fpm_build () {
1180   # Source dir where fpm-info.sh (if any) will be found.
1181   SRC_DIR=$1
1182   shift
1183   # The package source.  Depending on the source type, this can be a
1184   # path, or the name of the package in an upstream repository (e.g.,
1185   # pip).
1186   PACKAGE=$1
1187   shift
1188   # The name of the package to build.
1189   PACKAGE_NAME=$1
1190   shift
1191   # The type of source package.  Passed to fpm -s.  Default "dir".
1192   PACKAGE_TYPE=${1:-dir}
1193   shift
1194   # Optional: the package version number.  Passed to fpm -v.
1195   VERSION=$1
1196   shift
1197
1198   if [[ -n "$ONLY_BUILD" ]] && [[ "$PACKAGE_NAME" != "$ONLY_BUILD" ]] && [[ "$PACKAGE" != "$ONLY_BUILD" ]] ; then
1199     # arvados-workbench depends on arvados-server at build time, so even when
1200     # only arvados-workbench is being built, we need to build arvados-server too
1201     if [[ "$PACKAGE_NAME" != "arvados-server" ]] || [[ "$ONLY_BUILD" != "arvados-workbench" ]]; then
1202       return 0
1203     fi
1204   fi
1205
1206   local default_iteration_value="$(default_iteration "$PACKAGE" "$VERSION" "$PACKAGE_TYPE")"
1207
1208   declare -a COMMAND_ARR=("fpm" "-s" "$PACKAGE_TYPE" "-t" "$FORMAT")
1209   if [ python = "$PACKAGE_TYPE" ] && [ deb = "$FORMAT" ]; then
1210       # Dependencies are built from setup.py.  Since setup.py will never
1211       # refer to Debian package iterations, it doesn't make sense to
1212       # enforce those in the .deb dependencies.
1213       COMMAND_ARR+=(--deb-ignore-iteration-in-dependencies)
1214   fi
1215
1216   if [[ "$DEBUG" != "0" ]]; then
1217     COMMAND_ARR+=('--verbose' '--log' 'info')
1218   fi
1219
1220   if [[ -n "$PACKAGE_NAME" ]]; then
1221     COMMAND_ARR+=('-n' "$PACKAGE_NAME")
1222   fi
1223
1224   if [[ "$MAINTAINER" != "" ]]; then
1225     COMMAND_ARR+=('--maintainer' "$MAINTAINER")
1226   fi
1227
1228   if [[ "$VENDOR" != "" ]]; then
1229     COMMAND_ARR+=('--vendor' "$VENDOR")
1230   fi
1231
1232   if [[ "$VERSION" != "" ]]; then
1233     COMMAND_ARR+=('-v' "$VERSION")
1234   fi
1235   if [[ -n "$default_iteration_value" ]]; then
1236       # We can always add an --iteration here.  If another one is specified in $@,
1237       # that will take precedence, as desired.
1238       COMMAND_ARR+=(--iteration "$default_iteration_value")
1239   fi
1240
1241   # Append --depends X and other arguments specified by fpm-info.sh in
1242   # the package source dir. These are added last so they can override
1243   # the arguments added by this script.
1244   declare -a fpm_args=()
1245   declare -a build_depends=()
1246   declare -a fpm_depends=()
1247   declare -a fpm_conflicts=()
1248   declare -a fpm_exclude=()
1249   if [[ ! -d "$SRC_DIR" ]]; then
1250       echo >&2 "BUG: looking in wrong dir for fpm-info.sh: $pkgdir"
1251       exit 1
1252   fi
1253   fpminfo="${SRC_DIR}/fpm-info.sh"
1254   if [[ -e "$fpminfo" ]]; then
1255       debug_echo "Loading fpm overrides from $fpminfo"
1256       source "$fpminfo"
1257   fi
1258   for pkg in "${build_depends[@]}"; do
1259       if [[ $TARGET =~ debian|ubuntu ]]; then
1260           pkg_deb=$(ls "$WORKSPACE/packages/$TARGET/$pkg_"*.deb | sort -rg | awk 'NR==1')
1261           if [[ -e $pkg_deb ]]; then
1262               echo "Installing build_dep $pkg from $pkg_deb"
1263               dpkg -i "$pkg_deb"
1264           else
1265               echo "Attemping to install build_dep $pkg using apt-get"
1266               apt-get install -y "$pkg"
1267           fi
1268           apt-get -y -f install
1269       else
1270           pkg_rpm=$(ls "$WORKSPACE/packages/$TARGET/$pkg"-[0-9]*.rpm | sort -rg | awk 'NR==1')
1271           if [[ -e $pkg_rpm ]]; then
1272               echo "Installing build_dep $pkg from $pkg_rpm"
1273               rpm -i "$pkg_rpm"
1274           else
1275               echo "Attemping to install build_dep $pkg"
1276               rpm -i "$pkg"
1277           fi
1278       fi
1279   done
1280   for i in "${fpm_depends[@]}"; do
1281     COMMAND_ARR+=('--depends' "$i")
1282   done
1283   for i in "${fpm_conflicts[@]}"; do
1284     COMMAND_ARR+=('--conflicts' "$i")
1285   done
1286   for i in "${fpm_exclude[@]}"; do
1287     COMMAND_ARR+=('--exclude' "$i")
1288   done
1289
1290   COMMAND_ARR+=("${fpm_args[@]}")
1291
1292   # Append remaining function arguments directly to fpm's command line.
1293   for i; do
1294     COMMAND_ARR+=("$i")
1295   done
1296
1297   COMMAND_ARR+=("$PACKAGE")
1298
1299   debug_echo -e "\n${COMMAND_ARR[@]}\n"
1300
1301   FPM_RESULTS=$("${COMMAND_ARR[@]}")
1302   FPM_EXIT_CODE=$?
1303
1304   fpm_verify $FPM_EXIT_CODE $FPM_RESULTS
1305
1306   # if something went wrong and debug is off, print out the fpm command that errored
1307   if [[ 0 -ne $? ]] && [[ "$STDOUT_IF_DEBUG" == "/dev/null" ]]; then
1308     echo -e "\n${COMMAND_ARR[@]}\n"
1309   fi
1310 }
1311
1312 # verify build results
1313 fpm_verify () {
1314   FPM_EXIT_CODE=$1
1315   shift
1316   FPM_RESULTS=$@
1317
1318   FPM_PACKAGE_NAME=''
1319   if [[ $FPM_RESULTS =~ ([A-Za-z0-9_\.-]*\.)(deb|rpm) ]]; then
1320     FPM_PACKAGE_NAME=${BASH_REMATCH[1]}${BASH_REMATCH[2]}
1321   fi
1322
1323   if [[ "$FPM_PACKAGE_NAME" == "" ]]; then
1324     EXITCODE=1
1325     echo
1326     echo "Error: $PACKAGE: Unable to figure out package name from fpm results:"
1327     echo
1328     echo $FPM_RESULTS
1329     echo
1330     return 1
1331   elif [[ "$FPM_RESULTS" =~ "File already exists" ]]; then
1332     echo "Package $FPM_PACKAGE_NAME exists, not rebuilding"
1333     return 0
1334   elif [[ 0 -ne "$FPM_EXIT_CODE" ]]; then
1335     EXITCODE=1
1336     echo "Error building package for $1:\n $FPM_RESULTS"
1337     return 1
1338   fi
1339 }
1340
1341 install_package() {
1342   PACKAGES=$@
1343   if [[ "$FORMAT" == "deb" ]]; then
1344     $SUDO apt-get install $PACKAGES --yes
1345   elif [[ "$FORMAT" == "rpm" ]]; then
1346     $SUDO yum -q -y install $PACKAGES
1347   fi
1348 }
1349
1350 title() {
1351     printf '%s %s\n' "=======" "$1"
1352 }
1353
1354 checkexit() {
1355     if [[ "$1" != "0" ]]; then
1356         title "$2 -- FAILED"
1357         failures+=("$2 (`timer`)")
1358     else
1359         successes+=("$2 (`timer`)")
1360     fi
1361 }
1362
1363 timer_reset() {
1364     t0=$SECONDS
1365 }
1366
1367 timer() {
1368     if [[ -n "$t0" ]]; then
1369         echo -n "$(($SECONDS - $t0))s"
1370     fi
1371 }
1372
1373 report_outcomes() {
1374     for x in "${successes[@]}"
1375     do
1376         echo "Pass: $x"
1377     done
1378
1379     if [[ ${#failures[@]} == 0 ]]
1380     then
1381         if [[ ${#successes[@]} != 0 ]]; then
1382            echo "All test suites passed."
1383         fi
1384     else
1385         echo "Failures (${#failures[@]}):"
1386         for x in "${failures[@]}"
1387         do
1388             echo "Fail: $x"
1389         done
1390     fi
1391 }