2 # Copyright (C) The Arvados Authors. All rights reserved.
4 # SPDX-License-Identifier: AGPL-3.0
6 # A library of functions shared by the various scripts in this directory.
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
11 LICENSE_PACKAGE_TS=20151208015500
13 if [[ -z "$ARVADOS_BUILDING_VERSION" ]]; then
14 RAILS_PACKAGE_ITERATION=1
16 RAILS_PACKAGE_ITERATION="$ARVADOS_BUILDING_ITERATION"
20 echo "$@" >"$STDOUT_IF_DEBUG"
23 find_python_program() {
27 if "$prog" --version >/dev/null 2>&1; then
35 Error: $prog (from Python setuptools module) not found
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"
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
51 local subdir="$1"; shift
52 if [[ -n "$ARVADOS_BUILDING_VERSION" ]]; then
53 echo "$ARVADOS_BUILDING_VERSION"
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"
63 nohash_version_from_git() {
64 local subdir="$1"; shift
65 if [[ -n "$ARVADOS_BUILDING_VERSION" ]]; then
66 echo "$ARVADOS_BUILDING_VERSION"
69 version_from_git $subdir | cut -d. -f1-4
72 timestamp_from_git() {
73 local subdir="$1"; shift
74 format_last_commit_here "%ct" "$subdir"
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)
82 # Usage: get_native_arch
84 # Only amd64 and aarch64 are supported at the moment
94 echo "Error: architecture not supported"
102 local gem_name="$1"; shift
103 local gem_version="$(nohash_version_from_git)"
104 local gem_src_dir="$(pwd)"
106 if [[ -n "$ONLY_BUILD" ]] && [[ "$gem_name" != "$ONLY_BUILD" ]] ; then
110 if ! [[ -e "${gem_name}-${gem_version}.gem" ]]; then
111 find -maxdepth 1 -name "${gem_name}-*.gem" -delete
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"
118 # Usage: package_workbench2
119 package_workbench2() {
120 local pkgname=arvados-workbench2
121 local src=services/workbench2
122 local dst=/var/www/arvados-workbench2/workbench2
123 local description="Arvados Workbench 2"
124 if [[ -n "$ONLY_BUILD" ]] && [[ "$pkgname" != "$ONLY_BUILD" ]] ; then
128 local version="$(version_from_git)"
130 NODE_ENV=production yarn install
131 VERSION="$version" BUILD_NUMBER="$(default_iteration "$pkgname" "$version" yarn)" GIT_COMMIT="$(git rev-parse HEAD | head -c9)" yarn build
132 cd "$WORKSPACE/packages/$TARGET"
133 fpm_build "${WORKSPACE}/$src" "${WORKSPACE}/$src/build/=$dst" "$pkgname" dir "$version" \
134 --license="GNU Affero General Public License, version 3.0" \
135 --description="${description}" \
136 --config-files="/etc/arvados/$pkgname/workbench2.example.json" \
137 "$WORKSPACE/services/workbench2/etc/arvados/workbench2/workbench2.example.json=/etc/arvados/$pkgname/workbench2.example.json"
140 calculate_go_package_version() {
141 # $__returnvar has the nameref attribute set, which means it is a reference
142 # to another variable that is passed in as the first argument to this function.
143 # see https://www.gnu.org/software/bash/manual/html_node/Shell-Parameters.html
144 local -n __returnvar="$1"; shift
150 # Update the version number and build a new package if the vendor
151 # bundle has changed, or the command imports anything from the
152 # Arvados SDK and the SDK has changed.
153 declare -a checkdirs=(go.mod go.sum)
154 while [ -n "$1" ]; do
158 # Even our rails packages (version calculation happens here!) depend on a go component (arvados-server)
159 # Everything depends on the build directory.
160 checkdirs+=(sdk/go lib build)
162 for dir in ${checkdirs[@]}; do
164 ts="$(timestamp_from_git "$dir")"
165 if [[ "$ts" -gt "$timestamp" ]]; then
166 version=$(version_from_git "$dir")
171 __returnvar="$version"
174 # Usage: package_go_binary services/foo arvados-foo [deb|rpm] [amd64|arm64] "Compute foo to arbitrary precision" [apache-2.0.txt]
175 package_go_binary() {
176 local src_path="$1"; shift
177 local prog="$1"; shift
178 local package_format="$1"; shift
179 local target_arch="$1"; shift
180 local description="$1"; shift
181 local license_file="${1:-agpl-3.0.txt}"; shift
183 if [[ -n "$ONLY_BUILD" ]] && [[ "$prog" != "$ONLY_BUILD" ]]; then
184 debug_echo -e "Skipping build of $prog package."
188 native_arch=$(get_native_arch)
190 if [[ "$native_arch" != "amd64" ]] && [[ -n "$target_arch" ]] && [[ "$native_arch" != "$target_arch" ]]; then
191 echo "Error: no cross compilation support for Go on $native_arch, can not build $prog for $target_arch"
195 case "$package_format-$TARGET" in
196 # Ubuntu 20.04 does not support cross compilation because the
197 # libfuse package does not support multiarch. See
198 # <https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=983477>.
199 # Red Hat-based distributions do not support native cross compilation at
200 # all (they use a qemu-based solution we haven't implemented yet).
201 deb-ubuntu2004|rpm-*)
203 if [[ "$native_arch" == "amd64" ]] && [[ -n "$target_arch" ]] && [[ "$native_arch" != "$target_arch" ]]; then
204 echo "Error: no cross compilation support for Go on $native_arch for $TARGET, can not build $prog for $target_arch"
213 if [[ -n "$target_arch" ]]; then
216 # No target architecture specified, default to native target. When on amd64
217 # also crosscompile arm64 (when supported).
219 if [[ $cross_compilation -ne 0 ]]; then
224 for ta in ${archs[@]}; do
225 package_go_binary_worker "$src_path" "$prog" "$package_format" "$description" "$native_arch" "$ta" "$license_file"
227 if [[ $retval -ne 0 ]]; then
233 # Usage: package_go_binary services/foo arvados-foo deb "Compute foo to arbitrary precision" [amd64/arm64] [amd64/arm64] [apache-2.0.txt]
234 package_go_binary_worker() {
235 local src_path="$1"; shift
236 local prog="$1"; shift
237 local package_format="$1"; shift
238 local description="$1"; shift
239 local native_arch="${1:-amd64}"; shift
240 local target_arch="${1:-amd64}"; shift
241 local license_file="${1:-agpl-3.0.txt}"; shift
243 debug_echo "package_go_binary $src_path as $prog (native arch: $native_arch, target arch: $target_arch)"
244 local basename="${src_path##*/}"
245 calculate_go_package_version go_package_version $src_path
247 cd $WORKSPACE/packages/$TARGET
248 test_package_presence "$prog" "$go_package_version" "go" "" "$target_arch"
249 if [[ $? -ne 0 ]]; then
253 echo "Building $package_format ($target_arch) package for $prog from $src_path"
254 if [[ "$native_arch" == "amd64" ]] && [[ "$target_arch" == "arm64" ]]; then
255 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"
257 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"
262 binpath=$GOPATH/bin/${basename}
263 if [[ "${target_arch}" != "${native_arch}" ]]; then
264 switches+=("-a${target_arch}")
265 binpath="$GOPATH/bin/linux_${target_arch}/${basename}"
268 case "$package_format" in
269 # As of April 2024 we package identical Go binaries under different
270 # packages and names. This upsets the build id database, so don't
271 # register ourselves there.
272 rpm) switches+=(--rpm-rpmbuild-define="_build_id_links none") ;;
275 systemd_unit="$WORKSPACE/${src_path}/${prog}.service"
276 if [[ -e "${systemd_unit}" ]]; then
278 --after-install "${WORKSPACE}/build/go-python-package-scripts/postinst"
279 --before-remove "${WORKSPACE}/build/go-python-package-scripts/prerm"
280 "${systemd_unit}=/lib/systemd/system/${prog}.service")
282 switches+=("$WORKSPACE/${license_file}=/usr/share/doc/$prog/${license_file}")
284 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[@]}"
287 # Usage: package_go_so lib/foo arvados_foo.so arvados-foo deb amd64 "Arvados foo library"
289 local src_path="$1"; shift
290 local sofile="$1"; shift
291 local pkg="$1"; shift
292 local package_format="$1"; shift
293 local target_arch="$1"; shift # supported: amd64, arm64
294 local description="$1"; shift
296 if [[ -n "$ONLY_BUILD" ]] && [[ "$pkg" != "$ONLY_BUILD" ]]; then
297 debug_echo -e "Skipping build of $pkg package."
301 debug_echo "package_go_so $src_path as $pkg"
303 calculate_go_package_version go_package_version $src_path
304 cd $WORKSPACE/packages/$TARGET
305 test_package_presence $pkg $go_package_version go || return 1
306 cd $WORKSPACE/$src_path
307 go build -buildmode=c-shared -o ${GOPATH}/bin/${sofile}
308 cd $WORKSPACE/packages/$TARGET
310 "--url=https://arvados.org"
311 "--license=Apache License, Version 2.0"
312 "--description=${description}"
313 "$WORKSPACE/apache-2.0.txt=/usr/share/doc/$pkg/apache-2.0.txt"
315 if [[ -e "$WORKSPACE/$src_path/pam-configs-arvados" ]]; then
316 fpmargs+=("$WORKSPACE/$src_path/pam-configs-arvados=/usr/share/doc/$pkg/pam-configs-arvados-go")
318 if [[ -e "$WORKSPACE/$src_path/README" ]]; then
319 fpmargs+=("$WORKSPACE/$src_path/README=/usr/share/doc/$pkg/README")
321 fpm_build "${WORKSPACE}/${src_path}" "$GOPATH/bin/${sofile}=/usr/lib/${sofile}" "${pkg}" dir "${go_package_version}" "${fpmargs[@]}"
324 default_iteration() {
325 if [[ -n "$ARVADOS_BUILDING_VERSION" ]]; then
326 echo "$ARVADOS_BUILDING_ITERATION"
329 local package_name="$1"; shift
330 local package_version="$1"; shift
331 local package_type="$1"; shift
333 if [[ $package_version =~ ^0\.1\.([0-9]{14})(\.|$) ]] && \
334 [[ ${BASH_REMATCH[1]} -le $LICENSE_PACKAGE_TS ]]; then
340 _build_rails_package_scripts() {
341 local pkgname="$1"; shift
342 local destdir="$1"; shift
343 local srcdir="$RUN_BUILD_PACKAGES_PATH/rails-package-scripts"
344 for scriptname in postinst prerm postrm; do
345 cat "$srcdir/$pkgname.sh" "$srcdir/step2.sh" "$srcdir/$scriptname.sh" \
346 >"$destdir/$scriptname" || return $?
350 rails_package_version() {
351 local pkgname="$1"; shift
352 local srcdir="$1"; shift
353 if [[ -n "$ARVADOS_BUILDING_VERSION" ]]; then
354 echo "$ARVADOS_BUILDING_VERSION"
357 local version="$(version_from_git)"
358 if [ $pkgname = "arvados-api-server" ] ; then
359 calculate_go_package_version version cmd/arvados-server "$srcdir"
364 test_rails_package_presence() {
365 local pkgname="$1"; shift
366 local srcdir="$1"; shift
368 if [[ -n "$ONLY_BUILD" ]] && [[ "$pkgname" != "$ONLY_BUILD" ]] ; then
376 local version="$(rails_package_version "$pkgname" "$srcdir")"
380 test_package_presence $pkgname $version rails "$RAILS_PACKAGE_ITERATION"
383 get_complete_package_name() {
384 # if the errexit flag is set, unset it until this function returns
385 # otherwise, the shift calls below will abort the program if optional arguments are not supplied
386 if [ -o errexit ]; then
390 # $__returnvar has the nameref attribute set, which means it is a reference
391 # to another variable that is passed in as the first argument to this function.
392 # see https://www.gnu.org/software/bash/manual/html_node/Shell-Parameters.html
393 local -n __returnvar="$1"; shift
394 local pkgname="$1"; shift
395 local version="$1"; shift
396 local pkgtype="$1"; shift
397 local iteration="$1"; shift
398 local arch="$1"; shift
399 if [[ "$iteration" == "" ]]; then
400 iteration="$(default_iteration "$pkgname" "$version" "$pkgtype")"
403 if [[ "$arch" == "" ]]; then
404 native_arch=$(get_native_arch)
405 rpm_native_arch="x86_64"
406 if [[ "$HOSTTYPE" == "aarch64" ]]; then
407 rpm_native_arch="arm64"
409 rpm_architecture="$rpm_native_arch"
410 deb_architecture="$native_arch"
412 if [[ "$pkgtype" =~ ^(src)$ ]]; then
413 rpm_architecture="noarch"
414 deb_architecture="all"
417 rpm_architecture=$arch
418 deb_architecture=$arch
421 local complete_pkgname="${pkgname}_$version${iteration:+-$iteration}_$deb_architecture.deb"
422 if [[ "$FORMAT" == "rpm" ]]; then
423 # rpm packages get iteration 1 if we don't supply one
424 iteration=${iteration:-1}
425 complete_pkgname="$pkgname-$version-${iteration}.$rpm_architecture.rpm"
427 __returnvar=${complete_pkgname}
430 # Test if the package already exists, if not return 0, if it does return 1
431 test_package_presence() {
432 local pkgname="$1"; shift
433 local version="$1"; shift
434 local pkgtype="$1"; shift
435 local iteration="$1"; shift
436 local arch="$1"; shift
437 if [[ -n "$ONLY_BUILD" ]] && [[ "$pkgname" != "$ONLY_BUILD" ]] ; then
442 get_complete_package_name full_pkgname "$pkgname" "$version" "$pkgtype" "$iteration" "$arch"
444 # See if we can skip building the package, only if it already exists in the
445 # processed/ directory. If so, move it back to the packages directory to make
446 # sure it gets picked up by the test and/or upload steps.
447 # Get the list of packages from the repos
449 if [[ "$FORCE_BUILD" == "1" ]]; then
450 echo "Package $full_pkgname build forced with --force-build, building"
451 elif [[ "$FORMAT" == "deb" ]]; then
453 dd[debian11]=bullseye
454 dd[debian12]=bookworm
458 if [ ${pkgname:0:3} = "lib" ]; then
459 repo_subdir=${pkgname:0:4}
461 repo_subdir=${pkgname:0:1}
464 repo_pkg_list=$(curl -s -o - http://apt.arvados.org/${D}/pool/main/${repo_subdir}/${pkgname}/)
465 echo "${repo_pkg_list}" |grep -q ${full_pkgname}
466 if [ $? -eq 0 ] ; then
467 echo "Package $full_pkgname exists upstream, not rebuilding, downloading instead!"
468 curl -s -o "$WORKSPACE/packages/$TARGET/${full_pkgname}" http://apt.arvados.org/${D}/pool/main/${repo_subdir}/${pkgname}/${full_pkgname}
470 elif test -f "$WORKSPACE/packages/$TARGET/processed/${full_pkgname}" ; then
471 echo "Package $full_pkgname exists, not rebuilding!"
474 echo "Package $full_pkgname not found, building"
480 rocky8) rpm_root="CentOS/8/dev" ;;
482 echo "FIXME: Don't know RPM URL path for $TARGET, building"
486 local rpm_url="http://rpm.arvados.org/$rpm_root/$arch/$full_pkgname"
488 if curl -fs -o "$WORKSPACE/packages/$TARGET/$full_pkgname" "$rpm_url"; then
489 echo "Package $full_pkgname exists upstream, not rebuilding, downloading instead!"
491 elif [[ -f "$WORKSPACE/packages/$TARGET/processed/$full_pkgname" ]]; then
492 echo "Package $full_pkgname exists, not rebuilding!"
495 echo "Package $full_pkgname not found, building"
501 handle_rails_package() {
502 local pkgname="$1"; shift
504 if [[ -n "$ONLY_BUILD" ]] && [[ "$pkgname" != "$ONLY_BUILD" ]] ; then
507 local srcdir="$1"; shift
509 local license_path="$1"; shift
510 local version="$(rails_package_version "$pkgname" "$srcdir")"
511 echo "$version" >package-build.version
512 local scripts_dir="$(mktemp --tmpdir -d "$pkgname-XXXXXXXX.scripts")" && \
515 _build_rails_package_scripts "$pkgname" "$scripts_dir"
518 git rev-parse HEAD >git-commit.version
519 # Please make sure you read `bundle help config` carefully before you
520 # modify any of these settings. Some of their names are not intuitive.
522 # `bundle cache` caches from Git and paths, not just rubygems.org.
523 bundle config set cache_all true
524 # Disallow changes to Gemfile.
525 bundle config set deployment true
526 # Avoid loading system-wide gems (although this seems to not work 100%).
527 bundle config set disable_shared_gems true
528 # `bundle cache` only downloads gems, doesn't install them.
529 # Our Rails postinst script does the install step.
530 bundle config set no_install true
531 # As of April 2024/Bundler 2.4, `bundle cache` seems to skip downloading
532 # gems that are already available system-wide... and then it complains
533 # that your bundle is incomplete. Work around this by fetching gems
535 # TODO: Once all our supported distros have Ruby 3+, we can modify
536 # the awk script to print "NAME:VERSION" output, and pipe that directly
537 # to `xargs -0r gem fetch` for reduced overhead.
538 mkdir -p vendor/cache
540 BEGIN { OFS="\0"; ORS="\0"; }
541 (/^[A-Z ]*$/) { level1=$0; }
542 (/^ [[:alpha:]]+:$/) { level2=substr($0, 3, length($0) - 3); next; }
543 (/^ {0,3}[[:alpha:]]/) { level2=""; next; }
544 (level1 == "GEM" && level2 == "specs" && NF == 2 && $1 ~ /^[[:alpha:]][-_[:alnum:]]*$/ && $2 ~ /\([[:digit:]]+[-_+.[:alnum:]]*\)$/) {
545 print "--version", substr($2, 2, length($2) - 2), $1;
547 ' Gemfile.lock | env -C vendor/cache xargs -0r --max-args=3 gem fetch
548 # Despite the bug, we still run `bundle cache` to make sure Bundler is
549 # happy for later steps.
552 if [[ 0 != "$?" ]] || ! cd "$WORKSPACE/packages/$TARGET"; then
553 echo "ERROR: $pkgname package prep failed" >&2
554 rm -rf "$scripts_dir"
558 local railsdir="/var/www/${pkgname%-server}/current"
559 local -a pos_args=("$srcdir/=$railsdir" "$pkgname" dir "$version")
560 local license_arg="$license_path=$railsdir/$(basename "$license_path")"
561 local -a switches=(--after-install "$scripts_dir/postinst"
562 --before-remove "$scripts_dir/prerm"
563 --after-remove "$scripts_dir/postrm")
564 if [[ -z "$ARVADOS_BUILDING_VERSION" ]]; then
565 switches+=(--iteration $RAILS_PACKAGE_ITERATION)
567 # For some reason fpm excludes need to not start with /.
568 local exclude_root="${railsdir#/}"
569 for exclude in tmp log coverage Capfile\* \
571 config/application.yml \
572 config/database.yml; do
573 switches+=(-x "$exclude_root/$exclude")
575 fpm_build "${srcdir}" "${pos_args[@]}" "${switches[@]}" \
576 -x "$exclude_root/vendor/cache-*" \
577 -x "$exclude_root/vendor/bundle" "$@" "$license_arg"
578 rm -rf "$scripts_dir"
581 # Usage: handle_api_server [amd64|arm64]
582 handle_api_server () {
583 local target_arch="${1:-amd64}"; shift
585 if [[ -n "$ONLY_BUILD" ]] && [[ "$ONLY_BUILD" != "arvados-api-server" ]] ; then
586 debug_echo -e "Skipping build of arvados-api-server package."
590 native_arch=$(get_native_arch)
591 if [[ "$target_arch" != "$native_arch" ]]; then
592 echo "Error: no cross compilation support for Rails yet, can not build arvados-api-server for $ARCH"
597 # Build the API server package
598 test_rails_package_presence arvados-api-server "$WORKSPACE/services/api"
599 if [[ "$?" == "0" ]]; then
600 calculate_go_package_version arvados_server_version cmd/arvados-server
601 arvados_server_iteration=$(default_iteration "arvados-server" "$arvados_server_version" "go")
602 handle_rails_package arvados-api-server "$WORKSPACE/services/api" \
603 "$WORKSPACE/agpl-3.0.txt" --url="https://arvados.org" \
604 --description="Arvados API server - Arvados is a free and open source platform for big data science." \
605 --license="GNU Affero General Public License, version 3.0" --depends "arvados-server = ${arvados_server_version}-${arvados_server_iteration}"
609 # Usage: handle_arvados_src
610 handle_arvados_src () {
611 if [[ -n "$ONLY_BUILD" ]] && [[ "$ONLY_BUILD" != "arvados-src" ]] ; then
612 debug_echo -e "Skipping build of arvados-src package."
618 COMMIT_HASH=$(format_last_commit_here "%H")
619 arvados_src_version="$(version_from_git)"
621 cd $WORKSPACE/packages/$TARGET
622 test_package_presence arvados-src "$arvados_src_version" src ""
624 if [[ "$?" == "0" ]]; then
626 SRC_BUILD_DIR=$(mktemp -d)
627 # mktemp creates the directory with 0700 permissions by default
628 chmod 755 $SRC_BUILD_DIR
629 git clone $DASHQ_UNLESS_DEBUG "$WORKSPACE/.git" "$SRC_BUILD_DIR"
632 # go into detached-head state
633 git checkout $DASHQ_UNLESS_DEBUG "$COMMIT_HASH"
634 echo "$COMMIT_HASH" >git-commit.version
636 cd $WORKSPACE/packages/$TARGET
637 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"
639 rm -rf "$SRC_BUILD_DIR"
644 setup_build_virtualenv() {
645 PYTHON_BUILDROOT="$(mktemp --directory --tmpdir pybuild.XXXXXXXX)"
646 "$PYTHON3_EXECUTABLE" -m venv "$PYTHON_BUILDROOT/venv"
647 "$PYTHON_BUILDROOT/venv/bin/pip" install --upgrade build piprepo setuptools wheel
648 mkdir "$PYTHON_BUILDROOT/wheelhouse"
651 # Build python packages with a virtualenv built-in
652 # Usage: fpm_build_virtualenv arvados-python-client sdk/python [deb|rpm] [amd64|arm64]
653 fpm_build_virtualenv () {
655 local pkg_dir=$1; shift
656 local package_format="$1"; shift
657 local target_arch="${1:-amd64}"; shift
659 native_arch=$(get_native_arch)
660 if [[ -n "$target_arch" ]] && [[ "$native_arch" == "$target_arch" ]]; then
661 fpm_build_virtualenv_worker "$pkg" "$pkg_dir" "$package_format" "$native_arch" "$target_arch"
662 elif [[ -z "$target_arch" ]]; then
663 fpm_build_virtualenv_worker "$pkg" "$pkg_dir" "$package_format" "$native_arch" "$native_arch"
665 echo "Error: no cross compilation support for Python yet, can not build $pkg for $target_arch"
670 # Build python packages with a virtualenv built-in
671 # Usage: fpm_build_virtualenv_worker arvados-python-client sdk/python python3 [deb|rpm] [amd64|arm64] [amd64|arm64]
672 fpm_build_virtualenv_worker () {
675 local package_format="$1"; shift
676 local native_arch="${1:-amd64}"; shift
677 local target_arch=${1:-amd64}; shift
680 STDOUT_IF_DEBUG=/dev/null
681 STDERR_IF_DEBUG=/dev/null
682 DASHQ_UNLESS_DEBUG=-q
683 if [[ "$DEBUG" != "0" ]]; then
684 STDOUT_IF_DEBUG=/dev/stdout
685 STDERR_IF_DEBUG=/dev/stderr
688 if [[ "$ARVADOS_BUILDING_ITERATION" == "" ]]; then
689 ARVADOS_BUILDING_ITERATION=1
692 PACKAGE_PREFIX=$PYTHON3_PKG_PREFIX
693 if [[ "$PKG" != "arvados-docker-cleaner" ]]; then
694 PYTHON_PKG=$PACKAGE_PREFIX-$PKG
696 # Exception to our package naming convention
700 # We must always add a wheel to our repository, even if we're not building
701 # this distro package, because it might be a dependency for a later
702 # package we do build.
703 if [[ "$PKG_DIR" =~ ^.=[0-9]+\. ]]; then
704 # Not source to build, but a version to download.
705 # The rest of the function expects a filesystem path, so set one afterwards.
706 "$PYTHON_BUILDROOT/venv/bin/pip" download --dest="$PYTHON_BUILDROOT/wheelhouse" "$PKG$PKG_DIR" \
707 && PKG_DIR="$PYTHON_BUILDROOT/nonexistent"
709 # Make PKG_DIR absolute.
710 PKG_DIR="$(env -C "$WORKSPACE" readlink -e "$PKG_DIR")"
711 if [[ -e "$PKG_DIR/pyproject.toml" ]]; then
712 "$PYTHON_BUILDROOT/venv/bin/python" -m build --outdir="$PYTHON_BUILDROOT/wheelhouse" "$PKG_DIR"
714 env -C "$PKG_DIR" "$PYTHON_BUILDROOT/venv/bin/python" setup.py bdist_wheel --dist-dir="$PYTHON_BUILDROOT/wheelhouse"
717 if [[ $? -ne 0 ]]; then
718 printf "Error, unable to download/build wheel for %s @ %s" "$PKG" "$PKG_DIR"
720 elif ! "$PYTHON_BUILDROOT/venv/bin/piprepo" build "$PYTHON_BUILDROOT/wheelhouse"; then
721 printf "Error, unable to update local wheel repository"
725 if [[ -n "$ONLY_BUILD" ]] && [[ "$PYTHON_PKG" != "$ONLY_BUILD" ]] && [[ "$PKG" != "$ONLY_BUILD" ]]; then
729 local venv_dir="$PYTHON_BUILDROOT/$PYTHON_PKG"
730 echo "Creating virtualenv..."
731 if ! "$PYTHON3_EXECUTABLE" -m venv "$venv_dir"; then
732 printf "Error, unable to run\n %s -m venv %s\n" "$PYTHON3_EXECUTABLE" "$venv_dir"
734 # We must have the dependency resolver introduced in late 2020 for the rest
735 # of our install process to work.
736 # <https://blog.python.org/2020/11/pip-20-3-release-new-resolver.html>
737 elif ! "$venv_dir/bin/pip" install "pip>=20.3"; then
738 printf "Error, unable to run\n %s/bin/pip install 'pip>=20.3'\n" "$venv_dir"
742 local pip_wheel="$(ls --sort=time --reverse "$PYTHON_BUILDROOT/wheelhouse/$(echo "$PKG" | sed s/-/_/g)-"*.whl | tail -n1)"
743 if [[ -z "$pip_wheel" ]]; then
744 printf "Error, unable to find built wheel for $PKG"
746 elif ! "$venv_dir/bin/pip" install $DASHQ_UNLESS_DEBUG $CACHE_FLAG --extra-index-url="file://$PYTHON_BUILDROOT/wheelhouse/simple" "$pip_wheel"; then
747 printf "Error, unable to run
748 %s/bin/pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG --extra-index-url=file://%s %s
749 " "$venv_dir" "$PYTHON_BUILDROOT/wheelhouse/simple" "$pip_wheel"
753 # Determine the package version from the wheel
754 PYTHON_VERSION="$("$venv_dir/bin/python" "$WORKSPACE/build/pypkg_info.py" metadata "$PKG" Version)"
755 UNFILTERED_PYTHON_VERSION="$(echo "$PYTHON_VERSION" | sed 's/\.dev/~dev/; s/\([0-9]\)rc/\1~rc/')"
757 # See if we actually need to build this package; does it exist already?
758 # We can't do this earlier than here, because we need PYTHON_VERSION.
759 if ! test_package_presence "$PYTHON_PKG" "$UNFILTERED_PYTHON_VERSION" python3 "$ARVADOS_BUILDING_ITERATION" "$target_arch"; then
762 echo "Building $package_format ($target_arch) package for $PKG from $PKG_DIR"
764 # Replace the shebang lines in all python scripts, and handle the activate
765 # scripts too. This is a functional replacement of the 237 line
766 # virtualenv_tools.py script that doesn't work in python3 without serious
767 # patching, minus the parts we don't need (modifying pyc files, etc).
768 local sys_venv_dir="/usr/lib/$PYTHON_PKG"
769 local sys_venv_py="$sys_venv_dir/bin/python$PYTHON3_VERSION"
770 find "$venv_dir/bin" -type f | while read binfile; do
771 if file --mime "$binfile" | grep -q binary; then
772 : # Nothing to do for binary files
773 elif [[ "$binfile" =~ /activate(.csh|.fish|)$ ]]; then
774 sed -ri "s@VIRTUAL_ENV(=| )\".*\"@VIRTUAL_ENV\\1\"$sys_venv_dir\"@" "$binfile"
776 # Replace shebang line
777 sed -ri "1 s@^#\![^[:space:]]+/bin/python[0-9.]*@#\!$sys_venv_py@" "$binfile"
781 # Using `env -C` sets the directory where the package is built.
782 # Using `fpm --chdir` sets the root directory for source arguments.
783 declare -a COMMAND_ARR=(
784 env -C "$PYTHON_BUILDROOT" fpm
787 --version="$UNFILTERED_PYTHON_VERSION"
789 --output-type="$package_format"
790 --depends="$PYTHON3_PACKAGE"
791 --iteration="$ARVADOS_BUILDING_ITERATION"
792 --replaces="python-$PKG"
793 --url="https://arvados.org"
795 # Append fpm flags corresponding to Python package metadata.
796 readarray -d "" -O "${#COMMAND_ARR[@]}" -t COMMAND_ARR < \
797 <("$venv_dir/bin/python3" "$WORKSPACE/build/pypkg_info.py" \
798 --delimiter=\\0 --format=fpm \
799 metadata "$PKG" License Summary)
801 if [[ -n "$target_arch" ]] && [[ "$target_arch" != "amd64" ]]; then
802 COMMAND_ARR+=("-a$target_arch")
805 if [[ "$MAINTAINER" != "" ]]; then
806 COMMAND_ARR+=('--maintainer' "$MAINTAINER")
809 if [[ "$VENDOR" != "" ]]; then
810 COMMAND_ARR+=('--vendor' "$VENDOR")
813 if [[ "$DEBUG" != "0" ]]; then
814 COMMAND_ARR+=('--verbose' '--log' 'info')
817 systemd_unit="$PKG_DIR/$PKG.service"
818 if [[ -e "${systemd_unit}" ]]; then
819 COMMAND_ARR+=('--after-install' "${WORKSPACE}/build/go-python-package-scripts/postinst")
820 COMMAND_ARR+=('--before-remove' "${WORKSPACE}/build/go-python-package-scripts/prerm")
823 case "$package_format" in
827 --deb-no-default-config-files
831 # Conflict with older packages we used to publish
832 --conflicts "rh-python36-python-$PKG"
833 # Do not generate /usr/lib/.build-id links on RH8+
834 # (otherwise our packages conflict with platform-python)
835 --rpm-rpmbuild-define "_build_id_links none"
839 # Append --depends X and other arguments specified by fpm-info.sh in
840 # the package source dir. These are added last so they can override
841 # the arguments added by this script.
842 declare -a fpm_args=()
843 declare -a fpm_depends=()
845 fpminfo="$PKG_DIR/fpm-info.sh"
846 if [[ -e "$fpminfo" ]]; then
847 echo "Loading fpm overrides from $fpminfo"
848 if ! source "$fpminfo"; then
849 echo "Error, unable to source $WORKSPACE/$PKG_DIR/fpm-info.sh for $PKG"
854 for i in "${fpm_depends[@]}"; do
855 COMMAND_ARR+=('--depends' "$i")
858 # make sure the systemd service file ends up in the right place
859 # used by arvados-docker-cleaner
860 if [[ -e "${systemd_unit}" ]]; then
861 COMMAND_ARR+=("share/doc/$PKG/$PKG.service=/lib/systemd/system/$PKG.service")
864 COMMAND_ARR+=("${fpm_args[@]}")
866 while read -d "" binpath; do
867 COMMAND_ARR+=("$binpath=/usr/$binpath")
868 done < <("$venv_dir/bin/python3" "$WORKSPACE/build/pypkg_info.py" --delimiter=\\0 binfiles "$PKG")
870 # the python3-arvados-cwl-runner package comes with cwltool, expose that version
871 if [[ "$PKG" == arvados-cwl-runner ]]; then
872 COMMAND_ARR+=("bin/cwltool=/usr/bin/cwltool")
875 COMMAND_ARR+=(".=$sys_venv_dir")
877 debug_echo -e "\n${COMMAND_ARR[@]}\n"
879 FPM_RESULTS=$("${COMMAND_ARR[@]}")
882 # if something went wrong and debug is off, print out the fpm command that errored
883 if ! fpm_verify $FPM_EXIT_CODE $FPM_RESULTS && [[ "$STDOUT_IF_DEBUG" == "/dev/null" ]]; then
884 echo "fpm returned an error executing the command:"
886 echo -e "\n${COMMAND_ARR[@]}\n"
888 ls "$PYTHON_BUILDROOT"/*."$package_format"
889 mv "$PYTHON_BUILDROOT"/*."$package_format" "$WORKSPACE/packages/$TARGET/"
894 # Build packages for everything
896 # Source dir where fpm-info.sh (if any) will be found.
899 # The package source. Depending on the source type, this can be a
900 # path, or the name of the package in an upstream repository (e.g.,
904 # The name of the package to build.
907 # The type of source package. Passed to fpm -s. Default "dir".
908 PACKAGE_TYPE=${1:-dir}
910 # Optional: the package version number. Passed to fpm -v.
914 if [[ -n "$ONLY_BUILD" ]] && [[ "$PACKAGE_NAME" != "$ONLY_BUILD" ]] && [[ "$PACKAGE" != "$ONLY_BUILD" ]] ; then
918 local default_iteration_value="$(default_iteration "$PACKAGE" "$VERSION" "$PACKAGE_TYPE")"
920 declare -a COMMAND_ARR=("fpm" "-s" "$PACKAGE_TYPE" "-t" "$FORMAT")
921 if [ python = "$PACKAGE_TYPE" ] && [ deb = "$FORMAT" ]; then
922 # Dependencies are built from setup.py. Since setup.py will never
923 # refer to Debian package iterations, it doesn't make sense to
924 # enforce those in the .deb dependencies.
925 COMMAND_ARR+=(--deb-ignore-iteration-in-dependencies)
928 if [[ "$DEBUG" != "0" ]]; then
929 COMMAND_ARR+=('--verbose' '--log' 'info')
932 if [[ -n "$PACKAGE_NAME" ]]; then
933 COMMAND_ARR+=('-n' "$PACKAGE_NAME")
936 if [[ "$MAINTAINER" != "" ]]; then
937 COMMAND_ARR+=('--maintainer' "$MAINTAINER")
940 if [[ "$VENDOR" != "" ]]; then
941 COMMAND_ARR+=('--vendor' "$VENDOR")
944 if [[ "$VERSION" != "" ]]; then
945 COMMAND_ARR+=('-v' "$VERSION")
947 if [[ -n "$default_iteration_value" ]]; then
948 # We can always add an --iteration here. If another one is specified in $@,
949 # that will take precedence, as desired.
950 COMMAND_ARR+=(--iteration "$default_iteration_value")
953 # Append --depends X and other arguments specified by fpm-info.sh in
954 # the package source dir. These are added last so they can override
955 # the arguments added by this script.
956 declare -a fpm_args=()
957 declare -a build_depends=()
958 declare -a fpm_depends=()
959 declare -a fpm_conflicts=()
960 declare -a fpm_exclude=()
961 if [[ ! -d "$SRC_DIR" ]]; then
962 echo >&2 "BUG: looking in wrong dir for fpm-info.sh: $pkgdir"
965 fpminfo="${SRC_DIR}/fpm-info.sh"
966 if [[ -e "$fpminfo" ]]; then
967 debug_echo "Loading fpm overrides from $fpminfo"
970 for pkg in "${build_depends[@]}"; do
971 if [[ $TARGET =~ debian|ubuntu ]]; then
972 pkg_deb=$(ls "$WORKSPACE/packages/$TARGET/$pkg_"*.deb | sort -rg | awk 'NR==1')
973 if [[ -e $pkg_deb ]]; then
974 echo "Installing build_dep $pkg from $pkg_deb"
977 echo "Attemping to install build_dep $pkg using apt-get"
978 apt-get install -y "$pkg"
980 apt-get -y -f install
982 pkg_rpm=$(ls "$WORKSPACE/packages/$TARGET/$pkg"-[0-9]*.rpm | sort -rg | awk 'NR==1')
983 if [[ -e $pkg_rpm ]]; then
984 echo "Installing build_dep $pkg from $pkg_rpm"
987 echo "Attemping to install build_dep $pkg"
992 for i in "${fpm_depends[@]}"; do
993 COMMAND_ARR+=('--depends' "$i")
995 for i in "${fpm_conflicts[@]}"; do
996 COMMAND_ARR+=('--conflicts' "$i")
998 for i in "${fpm_exclude[@]}"; do
999 COMMAND_ARR+=('--exclude' "$i")
1002 COMMAND_ARR+=("${fpm_args[@]}")
1004 # Append remaining function arguments directly to fpm's command line.
1009 COMMAND_ARR+=("$PACKAGE")
1011 debug_echo -e "\n${COMMAND_ARR[@]}\n"
1013 FPM_RESULTS=$("${COMMAND_ARR[@]}")
1015 echo "fpm: exit code $FPM_EXIT_CODE" >>$STDOUT_IF_DEBUG
1016 echo "$FPM_RESULTS" >>$STDOUT_IF_DEBUG
1018 fpm_verify $FPM_EXIT_CODE $FPM_RESULTS
1020 # if something went wrong and debug is off, print out the fpm command that errored
1021 if [[ 0 -ne $? ]] && [[ "$STDOUT_IF_DEBUG" == "/dev/null" ]]; then
1022 echo -e "\n${COMMAND_ARR[@]}\n"
1026 # verify build results
1033 if [[ $FPM_RESULTS =~ ([A-Za-z0-9_\.~-]*\.)(deb|rpm) ]]; then
1034 FPM_PACKAGE_NAME=${BASH_REMATCH[1]}${BASH_REMATCH[2]}
1037 if [[ "$FPM_PACKAGE_NAME" == "" ]]; then
1040 echo "Error: $PACKAGE: Unable to figure out package name from fpm results:"
1045 elif [[ "$FPM_RESULTS" =~ "File already exists" ]]; then
1046 echo "Package $FPM_PACKAGE_NAME exists, not rebuilding"
1048 elif [[ 0 -ne "$FPM_EXIT_CODE" ]]; then
1050 echo "Error building package for $1:\n $FPM_RESULTS"
1057 if [[ "$FORMAT" == "deb" ]]; then
1058 $SUDO apt-get install $PACKAGES --yes
1059 elif [[ "$FORMAT" == "rpm" ]]; then
1060 $SUDO yum -q -y install $PACKAGES
1065 printf '%s %s\n' "=======" "$1"
1069 if [[ "$1" != "0" ]]; then
1070 title "$2 -- FAILED"
1071 failures+=("$2 (`timer`)")
1073 successes+=("$2 (`timer`)")
1082 if [[ -n "$t0" ]]; then
1083 echo -n "$(($SECONDS - $t0))s"
1088 for x in "${successes[@]}"
1093 if [[ ${#failures[@]} == 0 ]]
1095 if [[ ${#successes[@]} != 0 ]]; then
1096 echo "All test suites passed."
1099 echo "Failures (${#failures[@]}):"
1100 for x in "${failures[@]}"