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