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