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