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=8
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 TZ=UTC git log -n1 --first-parent "--format=format:$format" .
47 # Output the version being built, or if we're building a
48 # dev/prerelease, output a version number based on the git log for
49 # the current working directory.
50 if [[ -n "$ARVADOS_BUILDING_VERSION" ]]; then
51 echo "$ARVADOS_BUILDING_VERSION"
55 local git_ts git_hash prefix
56 if [[ -n "$1" ]] ; then
62 declare $(format_last_commit_here "git_ts=%ct git_hash=%h")
63 ARVADOS_BUILDING_VERSION="$(git describe --abbrev=0).$(date -ud "@$git_ts" +%Y%m%d%H%M%S)"
64 echo "$ARVADOS_BUILDING_VERSION"
67 nohash_version_from_git() {
68 if [[ -n "$ARVADOS_BUILDING_VERSION" ]]; then
69 echo "$ARVADOS_BUILDING_VERSION"
72 version_from_git $1 | cut -d. -f1-4
75 timestamp_from_git() {
76 format_last_commit_here "%ct"
79 handle_python_package () {
80 # This function assumes the current working directory is the python package directory
81 if [ -n "$(find dist -name "*-$(nohash_version_from_git).tar.gz" -print -quit)" ]; then
82 # This package doesn't need rebuilding.
85 # Make sure only to use sdist - that's the only format pip can deal with (sigh)
86 python setup.py $DASHQ_UNLESS_DEBUG sdist
90 local gem_name="$1"; shift
91 local gem_version="$(nohash_version_from_git)"
92 local gem_src_dir="$(pwd)"
94 if [[ -n "$ONLY_BUILD" ]] && [[ "$gem_name" != "$ONLY_BUILD" ]] ; then
98 if ! [[ -e "${gem_name}-${gem_version}.gem" ]]; then
99 find -maxdepth 1 -name "${gem_name}-*.gem" -delete
101 # -q appears to be broken in gem version 2.2.2
102 $GEM build "$gem_name.gemspec" $DASHQ_UNLESS_DEBUG >"$STDOUT_IF_DEBUG" 2>"$STDERR_IF_DEBUG"
106 # Usage: package_go_binary services/foo arvados-foo "Compute foo to arbitrary precision"
107 package_go_binary() {
108 local src_path="$1"; shift
109 local prog="$1"; shift
110 local description="$1"; shift
111 local license_file="${1:-agpl-3.0.txt}"; shift
113 if [[ -n "$ONLY_BUILD" ]] && [[ "$prog" != "$ONLY_BUILD" ]] ; then
117 debug_echo "package_go_binary $src_path as $prog"
119 local basename="${src_path##*/}"
121 mkdir -p "$GOPATH/src/git.curoverse.com"
122 ln -sfn "$WORKSPACE" "$GOPATH/src/git.curoverse.com/arvados.git"
123 (cd "$GOPATH/src/git.curoverse.com/arvados.git" && "$GOPATH/bin/govendor" sync -v)
125 cd "$GOPATH/src/git.curoverse.com/arvados.git/$src_path"
126 local version="$(version_from_git)"
127 local timestamp="$(timestamp_from_git)"
129 # Update the version number and build a new package if the vendor
130 # bundle has changed, or the command imports anything from the
131 # Arvados SDK and the SDK has changed.
132 declare -a checkdirs=(vendor)
133 if grep -qr git.curoverse.com/arvados .; then
134 checkdirs+=(sdk/go lib)
136 for dir in ${checkdirs[@]}; do
137 cd "$GOPATH/src/git.curoverse.com/arvados.git/$dir"
138 ts="$(timestamp_from_git)"
139 if [[ "$ts" -gt "$timestamp" ]]; then
140 version=$(version_from_git)
145 cd $WORKSPACE/packages/$TARGET
146 test_package_presence $prog $version go
148 if [[ "$?" != "0" ]]; then
152 go get -ldflags "-X main.version=${version}" "git.curoverse.com/arvados.git/$src_path"
155 systemd_unit="$WORKSPACE/${src_path}/${prog}.service"
156 if [[ -e "${systemd_unit}" ]]; then
158 --after-install "${WORKSPACE}/build/go-python-package-scripts/postinst"
159 --before-remove "${WORKSPACE}/build/go-python-package-scripts/prerm"
160 "${systemd_unit}=/lib/systemd/system/${prog}.service")
162 switches+=("$WORKSPACE/${license_file}=/usr/share/doc/$prog/${license_file}")
164 fpm_build "$GOPATH/bin/${basename}=/usr/bin/${prog}" "${prog}" 'Curoverse, Inc.' dir "${version}" "--url=https://arvados.org" "--license=GNU Affero General Public License, version 3.0" "--description=${description}" "${switches[@]}"
167 default_iteration() {
168 if [[ -n "$ARVADOS_BUILDING_VERSION" ]]; then
169 echo "$ARVADOS_BUILDING_ITERATION"
172 local package_name="$1"; shift
173 local package_version="$1"; shift
174 local package_type="$1"; shift
176 if [[ $package_version =~ ^0\.1\.([0-9]{14})(\.|$) ]] && \
177 [[ ${BASH_REMATCH[1]} -le $LICENSE_PACKAGE_TS ]]; then
180 if [[ $package_type =~ ^python ]]; then
181 # Fix --iteration for #9242.
187 _build_rails_package_scripts() {
188 local pkgname="$1"; shift
189 local destdir="$1"; shift
190 local srcdir="$RUN_BUILD_PACKAGES_PATH/rails-package-scripts"
191 for scriptname in postinst prerm postrm; do
192 cat "$srcdir/$pkgname.sh" "$srcdir/step2.sh" "$srcdir/$scriptname.sh" \
193 >"$destdir/$scriptname" || return $?
197 test_rails_package_presence() {
198 local pkgname="$1"; shift
199 local srcdir="$1"; shift
201 if [[ -n "$ONLY_BUILD" ]] && [[ "$pkgname" != "$ONLY_BUILD" ]] ; then
209 local version="$(version_from_git)"
213 test_package_presence $pkgname $version rails "$RAILS_PACKAGE_ITERATION"
216 test_package_presence() {
217 local pkgname="$1"; shift
218 local version="$1"; shift
219 local pkgtype="$1"; shift
220 local iteration="$1"; shift
221 local arch="$1"; shift
223 if [[ -n "$ONLY_BUILD" ]] && [[ "$pkgname" != "$ONLY_BUILD" ]] ; then
227 if [[ "$iteration" == "" ]]; then
228 iteration="$(default_iteration "$pkgname" "$version" "$pkgtype")"
231 if [[ "$arch" == "" ]]; then
232 rpm_architecture="x86_64"
233 deb_architecture="amd64"
235 if [[ "$pkgtype" =~ ^(python|python3)$ ]]; then
236 rpm_architecture="noarch"
237 deb_architecture="all"
240 if [[ "$pkgtype" =~ ^(src)$ ]]; then
241 rpm_architecture="noarch"
242 deb_architecture="all"
245 # These python packages have binary components
246 if [[ "$pkgname" =~ (ruamel|ciso|pycrypto|pyyaml) ]]; then
247 rpm_architecture="x86_64"
248 deb_architecture="amd64"
251 rpm_architecture=$arch
252 deb_architecture=$arch
255 if [[ "$FORMAT" == "deb" ]]; then
256 local complete_pkgname="${pkgname}_$version${iteration:+-$iteration}_$deb_architecture.deb"
258 # rpm packages get iteration 1 if we don't supply one
259 iteration=${iteration:-1}
260 local complete_pkgname="$pkgname-$version-${iteration}.$rpm_architecture.rpm"
263 # See if we can skip building the package, only if it already exists in the
264 # processed/ directory. If so, move it back to the packages directory to make
265 # sure it gets picked up by the test and/or upload steps.
266 # Get the list of packages from the repos
268 if [[ "$FORMAT" == "deb" ]]; then
269 debian_distros="jessie precise stretch trusty wheezy xenial bionic"
271 for D in ${debian_distros}; do
272 if [ ${pkgname:0:3} = "lib" ]; then
273 repo_subdir=${pkgname:0:4}
275 repo_subdir=${pkgname:0:1}
278 repo_pkg_list=$(curl -s -o - http://apt.arvados.org/pool/${D}/main/${repo_subdir}/)
279 echo ${repo_pkg_list} |grep -q ${complete_pkgname}
280 if [ $? -eq 0 ] ; then
281 echo "Package $complete_pkgname exists, not rebuilding!"
282 curl -o ./${complete_pkgname} http://apt.arvados.org/pool/${D}/main/${repo_subdir}/${complete_pkgname}
284 elif test -f "$WORKSPACE/packages/$TARGET/processed/${complete_pkgname}" ; then
285 echo "Package $complete_pkgname exists, not rebuilding!"
288 echo "Package $complete_pkgname not found, building"
293 centos_repo="http://rpm.arvados.org/CentOS/7/dev/x86_64/"
295 repo_pkg_list=$(curl -o - ${centos_repo})
296 echo ${repo_pkg_list} |grep -q ${complete_pkgname}
297 if [ $? -eq 0 ]; then
298 echo "Package $complete_pkgname exists, not rebuilding!"
299 curl -o ./${complete_pkgname} ${centos_repo}${complete_pkgname}
302 echo "Package $complete_pkgname not found, building"
308 handle_rails_package() {
309 local pkgname="$1"; shift
311 if [[ -n "$ONLY_BUILD" ]] && [[ "$pkgname" != "$ONLY_BUILD" ]] ; then
314 local srcdir="$1"; shift
316 local license_path="$1"; shift
317 local version="$(version_from_git)"
318 echo "$version" >package-build.version
319 local scripts_dir="$(mktemp --tmpdir -d "$pkgname-XXXXXXXX.scripts")" && \
322 _build_rails_package_scripts "$pkgname" "$scripts_dir"
325 git rev-parse HEAD >git-commit.version
328 if [[ 0 != "$?" ]] || ! cd "$WORKSPACE/packages/$TARGET"; then
329 echo "ERROR: $pkgname package prep failed" >&2
330 rm -rf "$scripts_dir"
334 local railsdir="/var/www/${pkgname%-server}/current"
335 local -a pos_args=("$srcdir/=$railsdir" "$pkgname" "Curoverse, Inc." dir "$version")
336 local license_arg="$license_path=$railsdir/$(basename "$license_path")"
337 local -a switches=(--after-install "$scripts_dir/postinst"
338 --before-remove "$scripts_dir/prerm"
339 --after-remove "$scripts_dir/postrm")
340 if [[ -z "$ARVADOS_BUILDING_VERSION" ]]; then
341 switches+=(--iteration $RAILS_PACKAGE_ITERATION)
343 # For some reason fpm excludes need to not start with /.
344 local exclude_root="${railsdir#/}"
345 # .git and packages are for the SSO server, which is built from its
347 local -a exclude_list=(.git packages tmp log coverage Capfile\* \
348 config/deploy\* config/application.yml)
349 # for arvados-workbench, we need to have the (dummy) config/database.yml in the package
350 if [[ "$pkgname" != "arvados-workbench" ]]; then
351 exclude_list+=('config/database.yml')
353 for exclude in ${exclude_list[@]}; do
354 switches+=(-x "$exclude_root/$exclude")
356 fpm_build "${pos_args[@]}" "${switches[@]}" \
357 -x "$exclude_root/vendor/cache-*" \
358 -x "$exclude_root/vendor/bundle" "$@" "$license_arg"
359 rm -rf "$scripts_dir"
362 # Build packages for everything
364 # The package source. Depending on the source type, this can be a
365 # path, or the name of the package in an upstream repository (e.g.,
369 # The name of the package to build.
372 # Optional: the vendor of the package. Should be "Curoverse, Inc." for
373 # packages of our own software. Passed to fpm --vendor.
376 # The type of source package. Passed to fpm -s. Default "python".
377 PACKAGE_TYPE=${1:-python}
379 # Optional: the package version number. Passed to fpm -v.
383 if [[ -n "$ONLY_BUILD" ]] && [[ "$PACKAGE_NAME" != "$ONLY_BUILD" ]] && [[ "$PACKAGE" != "$ONLY_BUILD" ]] ; then
387 local default_iteration_value="$(default_iteration "$PACKAGE" "$VERSION" "$PACKAGE_TYPE")"
390 case "$PACKAGE_TYPE" in
392 # All Arvados Python2 packages depend on Python 2.7.
393 # Make sure we build with that for consistency.
395 set -- "$@" --python-bin python2.7 \
396 "${PYTHON_FPM_INSTALLER[@]}" \
397 --python-package-name-prefix "$PYTHON2_PKG_PREFIX" \
398 --prefix "$PYTHON2_PREFIX" \
399 --python-install-lib "$PYTHON2_INSTALL_LIB" \
400 --python-install-data . \
401 --exclude "${PYTHON2_INSTALL_LIB#/}/tests" \
402 --depends "$PYTHON2_PACKAGE"
405 # fpm does not actually support a python3 package type. Instead
406 # we recognize it as a convenience shortcut to add several
407 # necessary arguments to fpm's command line later, after we're
408 # done handling positional arguments.
411 set -- "$@" --python-bin python3 \
412 "${PYTHON3_FPM_INSTALLER[@]}" \
413 --python-package-name-prefix "$PYTHON3_PKG_PREFIX" \
414 --prefix "$PYTHON3_PREFIX" \
415 --python-install-lib "$PYTHON3_INSTALL_LIB" \
416 --python-install-data . \
417 --exclude "${PYTHON3_INSTALL_LIB#/}/tests" \
418 --depends "$PYTHON3_PACKAGE"
422 declare -a COMMAND_ARR=("fpm" "--maintainer=Ward Vandewege <ward@curoverse.com>" "-s" "$PACKAGE_TYPE" "-t" "$FORMAT")
423 if [ python = "$PACKAGE_TYPE" ] && [ deb = "$FORMAT" ]; then
424 # Dependencies are built from setup.py. Since setup.py will never
425 # refer to Debian package iterations, it doesn't make sense to
426 # enforce those in the .deb dependencies.
427 COMMAND_ARR+=(--deb-ignore-iteration-in-dependencies)
430 # 12271 - As FPM-generated packages don't include scripts by default, the
431 # packages cleanup on upgrade depends on files being listed on the %files
432 # section in the generated SPEC files. To remove DIRECTORIES, they need to
433 # be listed in that sectiontoo, so we need to add this parameter to properly
434 # remove lingering dirs. But this only works for python2: if used on
435 # python33, it includes dirs like /opt/rh/python33 that belong to
437 if [[ "$FORMAT" = rpm ]] && [[ "$python" = python2.7 ]]; then
438 COMMAND_ARR+=('--rpm-auto-add-directories')
441 if [[ "${DEBUG:-0}" != "0" ]]; then
442 COMMAND_ARR+=('--verbose' '--log' 'info')
445 if [[ -n "$PACKAGE_NAME" ]]; then
446 COMMAND_ARR+=('-n' "$PACKAGE_NAME")
449 if [[ "$VENDOR" != "" ]]; then
450 COMMAND_ARR+=('--vendor' "$VENDOR")
453 if [[ "$VERSION" != "" ]]; then
454 COMMAND_ARR+=('-v' "$VERSION")
456 if [[ -n "$default_iteration_value" ]]; then
457 # We can always add an --iteration here. If another one is specified in $@,
458 # that will take precedence, as desired.
459 COMMAND_ARR+=(--iteration "$default_iteration_value")
462 if [[ python = "$PACKAGE_TYPE" ]] && [[ -e "${PACKAGE}/${PACKAGE_NAME}.service" ]]
465 --after-install "${WORKSPACE}/build/go-python-package-scripts/postinst"
466 --before-remove "${WORKSPACE}/build/go-python-package-scripts/prerm"
470 # Append --depends X and other arguments specified by fpm-info.sh in
471 # the package source dir. These are added last so they can override
472 # the arguments added by this script.
473 declare -a fpm_args=()
474 declare -a build_depends=()
475 declare -a fpm_depends=()
476 declare -a fpm_exclude=()
477 declare -a fpm_dirs=(
478 # source dir part of 'dir' package ("/source=/dest" => "/source"):
480 # backports ("llfuse>=1.0" => "backports/python-llfuse")
481 "${WORKSPACE}/backports/${PACKAGE_TYPE}-${PACKAGE%%[<=>]*}")
482 if [[ -n "$PACKAGE_NAME" ]]; then
483 fpm_dirs+=("${WORKSPACE}/backports/${PACKAGE_NAME}")
485 for pkgdir in "${fpm_dirs[@]}"; do
486 fpminfo="$pkgdir/fpm-info.sh"
487 if [[ -e "$fpminfo" ]]; then
488 debug_echo "Loading fpm overrides from $fpminfo"
493 for pkg in "${build_depends[@]}"; do
494 if [[ $TARGET =~ debian|ubuntu ]]; then
495 pkg_deb=$(ls "$WORKSPACE/packages/$TARGET/$pkg_"*.deb | sort -rg | awk 'NR==1')
496 if [[ -e $pkg_deb ]]; then
497 echo "Installing build_dep $pkg from $pkg_deb"
500 echo "Attemping to install build_dep $pkg using apt-get"
501 apt-get install -y "$pkg"
503 apt-get -y -f install
505 pkg_rpm=$(ls "$WORKSPACE/packages/$TARGET/$pkg"-[0-9]*.rpm | sort -rg | awk 'NR==1')
506 if [[ -e $pkg_rpm ]]; then
507 echo "Installing build_dep $pkg from $pkg_rpm"
510 echo "Attemping to install build_dep $pkg"
515 for i in "${fpm_depends[@]}"; do
516 COMMAND_ARR+=('--depends' "$i")
518 for i in "${fpm_exclude[@]}"; do
519 COMMAND_ARR+=('--exclude' "$i")
522 # Append remaining function arguments directly to fpm's command line.
527 COMMAND_ARR+=("${fpm_args[@]}")
529 COMMAND_ARR+=("$PACKAGE")
531 debug_echo -e "\n${COMMAND_ARR[@]}\n"
533 FPM_RESULTS=$("${COMMAND_ARR[@]}")
536 fpm_verify $FPM_EXIT_CODE $FPM_RESULTS
538 # if something went wrong and debug is off, print out the fpm command that errored
539 if [[ 0 -ne $? ]] && [[ "$STDOUT_IF_DEBUG" == "/dev/null" ]]; then
540 echo -e "\n${COMMAND_ARR[@]}\n"
544 # verify build results
551 if [[ $FPM_RESULTS =~ ([A-Za-z0-9_\.-]*\.)(deb|rpm) ]]; then
552 FPM_PACKAGE_NAME=${BASH_REMATCH[1]}${BASH_REMATCH[2]}
555 if [[ "$FPM_PACKAGE_NAME" == "" ]]; then
558 echo "Error: $PACKAGE: Unable to figure out package name from fpm results:"
563 elif [[ "$FPM_RESULTS" =~ "File already exists" ]]; then
564 echo "Package $FPM_PACKAGE_NAME exists, not rebuilding"
566 elif [[ 0 -ne "$FPM_EXIT_CODE" ]]; then
568 echo "Error building package for $1:\n $FPM_RESULTS"
575 if [[ "$FORMAT" == "deb" ]]; then
576 $SUDO apt-get install $PACKAGES --yes
577 elif [[ "$FORMAT" == "rpm" ]]; then
578 $SUDO yum -q -y install $PACKAGES
583 txt="********** $1 **********"
584 printf "\n%*s%s\n\n" $((($COLUMNS-${#txt})/2)) "" "$txt"
588 if [[ "$1" != "0" ]]; then
589 title "!!!!!! $2 FAILED !!!!!!"
590 failures+=("$2 (`timer`)")
592 successes+=("$2 (`timer`)")
601 echo -n "$(($SECONDS - $t0))s"
605 for x in "${successes[@]}"
610 if [[ ${#failures[@]} == 0 ]]
612 echo "All test suites passed."
614 echo "Failures (${#failures[@]}):"
615 for x in "${failures[@]}"