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