15516: various fixes:
[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     TZ=UTC git log -n1 --first-parent "--format=format:$format" .
44 }
45
46 version_from_git() {
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"
52         return
53     fi
54
55     local git_ts git_hash prefix
56     if [[ -n "$1" ]] ; then
57         prefix="$1"
58     else
59         prefix="0.1"
60     fi
61
62     declare $(format_last_commit_here "git_ts=%ct git_hash=%h")
63     ARVADOS_BUILDING_VERSION="$(git tag -l |sort -V -r |head -n1).$(date -ud "@$git_ts" +%Y%m%d%H%M%S)"
64     echo "$ARVADOS_BUILDING_VERSION"
65 }
66
67 nohash_version_from_git() {
68     if [[ -n "$ARVADOS_BUILDING_VERSION" ]]; then
69         echo "$ARVADOS_BUILDING_VERSION"
70         return
71     fi
72     version_from_git $1 | cut -d. -f1-4
73 }
74
75 timestamp_from_git() {
76     format_last_commit_here "%ct"
77 }
78
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.
83     return
84   fi
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
87 }
88
89 handle_ruby_gem() {
90     local gem_name="$1"; shift
91     local gem_version="$(nohash_version_from_git)"
92     local gem_src_dir="$(pwd)"
93
94     if [[ -n "$ONLY_BUILD" ]] && [[ "$gem_name" != "$ONLY_BUILD" ]] ; then
95         return 0
96     fi
97
98     if ! [[ -e "${gem_name}-${gem_version}.gem" ]]; then
99         find -maxdepth 1 -name "${gem_name}-*.gem" -delete
100
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"
103     fi
104 }
105
106 calculate_go_package_version() {
107   local -n __returnvar="$1"; shift
108   local src_path="$1"; shift
109
110   mkdir -p "$GOPATH/src/git.curoverse.com"
111   ln -sfn "$WORKSPACE" "$GOPATH/src/git.curoverse.com/arvados.git"
112   (cd "$GOPATH/src/git.curoverse.com/arvados.git" && "$GOPATH/bin/govendor" sync -v)
113
114   cd "$GOPATH/src/git.curoverse.com/arvados.git/$src_path"
115   local version="$(version_from_git)"
116   local timestamp="$(timestamp_from_git)"
117
118   # Update the version number and build a new package if the vendor
119   # bundle has changed, or the command imports anything from the
120   # Arvados SDK and the SDK has changed.
121   declare -a checkdirs=(vendor)
122   if grep -qr git.curoverse.com/arvados .; then
123       checkdirs+=(sdk/go lib)
124   fi
125   for dir in ${checkdirs[@]}; do
126       cd "$GOPATH/src/git.curoverse.com/arvados.git/$dir"
127       ts="$(timestamp_from_git)"
128       if [[ "$ts" -gt "$timestamp" ]]; then
129           version=$(version_from_git)
130           timestamp="$ts"
131       fi
132   done
133
134   __returnvar="$version"
135 }
136
137 # Usage: package_go_binary services/foo arvados-foo "Compute foo to arbitrary precision"
138 package_go_binary() {
139     local src_path="$1"; shift
140     local prog="$1"; shift
141     local description="$1"; shift
142     local license_file="${1:-agpl-3.0.txt}"; shift
143
144     if [[ -n "$ONLY_BUILD" ]] && [[ "$prog" != "$ONLY_BUILD" ]]; then
145       # arvados-workbench depends on arvados-server at build time, so even when
146       # only arvados-workbench is being built, we need to build arvados-server too
147       if [[ "$prog" != "arvados-server" ]] || [[ "$ONLY_BUILD" != "arvados-workbench" ]]; then
148         return 0
149       fi
150     fi
151
152     debug_echo "package_go_binary $src_path as $prog"
153
154     local basename="${src_path##*/}"
155     calculate_go_package_version go_package_version $src_path
156
157     cd $WORKSPACE/packages/$TARGET
158     test_package_presence $prog $go_package_version go
159
160     if [[ "$?" != "0" ]]; then
161       return 1
162     fi
163
164     go get -ldflags "-X main.version=${go_package_version}" "git.curoverse.com/arvados.git/$src_path"
165
166     local -a switches=()
167     systemd_unit="$WORKSPACE/${src_path}/${prog}.service"
168     if [[ -e "${systemd_unit}" ]]; then
169         switches+=(
170             --after-install "${WORKSPACE}/build/go-python-package-scripts/postinst"
171             --before-remove "${WORKSPACE}/build/go-python-package-scripts/prerm"
172             "${systemd_unit}=/lib/systemd/system/${prog}.service")
173     fi
174     switches+=("$WORKSPACE/${license_file}=/usr/share/doc/$prog/${license_file}")
175
176     fpm_build "$GOPATH/bin/${basename}=/usr/bin/${prog}" "${prog}" dir "${go_package_version}" "--url=https://arvados.org" "--license=GNU Affero General Public License, version 3.0" "--description=${description}" "${switches[@]}"
177 }
178
179 default_iteration() {
180     if [[ -n "$ARVADOS_BUILDING_VERSION" ]]; then
181         echo "$ARVADOS_BUILDING_ITERATION"
182         return
183     fi
184     local package_name="$1"; shift
185     local package_version="$1"; shift
186     local package_type="$1"; shift
187     local iteration=1
188     if [[ $package_version =~ ^0\.1\.([0-9]{14})(\.|$) ]] && \
189            [[ ${BASH_REMATCH[1]} -le $LICENSE_PACKAGE_TS ]]; then
190         iteration=2
191     fi
192     if [[ $package_type =~ ^python ]]; then
193       # Fix --iteration for #9242.
194       iteration=2
195     fi
196     echo $iteration
197 }
198
199 _build_rails_package_scripts() {
200     local pkgname="$1"; shift
201     local destdir="$1"; shift
202     local srcdir="$RUN_BUILD_PACKAGES_PATH/rails-package-scripts"
203     for scriptname in postinst prerm postrm; do
204         cat "$srcdir/$pkgname.sh" "$srcdir/step2.sh" "$srcdir/$scriptname.sh" \
205             >"$destdir/$scriptname" || return $?
206     done
207 }
208
209 test_rails_package_presence() {
210   local pkgname="$1"; shift
211   local srcdir="$1"; shift
212
213   if [[ -n "$ONLY_BUILD" ]] && [[ "$pkgname" != "$ONLY_BUILD" ]] ; then
214     return 1
215   fi
216
217   tmppwd=`pwd`
218
219   cd $srcdir
220
221   local version="$(version_from_git)"
222
223   cd $tmppwd
224
225   test_package_presence $pkgname $version rails "$RAILS_PACKAGE_ITERATION"
226 }
227
228 get_complete_package_name() {
229   # if the errexit flag is set, unset it until this function returns
230   # otherwise, the shift calls below will abort the program if optional arguments are not supplied
231   if [ -o errexit ]; then
232     set +e
233     trap 'set -e' RETURN
234   fi
235   local -n __returnvar="$1"; shift
236   local pkgname="$1"; shift
237   local version="$1"; shift
238   local pkgtype="$1"; shift
239   local iteration="$1"; shift
240   local arch="$1"; shift
241   if [[ "$iteration" == "" ]]; then
242       iteration="$(default_iteration "$pkgname" "$version" "$pkgtype")"
243   fi
244
245   if [[ "$arch" == "" ]]; then
246     rpm_architecture="x86_64"
247     deb_architecture="amd64"
248
249     if [[ "$pkgtype" =~ ^(src)$ ]]; then
250       rpm_architecture="noarch"
251       deb_architecture="all"
252     fi
253
254     # These python packages have binary components
255     if [[ "$pkgname" =~ (ruamel|ciso|pycrypto|pyyaml) ]]; then
256       rpm_architecture="x86_64"
257       deb_architecture="amd64"
258     fi
259   else
260     rpm_architecture=$arch
261     deb_architecture=$arch
262   fi
263
264   local complete_pkgname="${pkgname}_$version${iteration:+-$iteration}_$deb_architecture.deb"
265   if [[ "$FORMAT" == "rpm" ]]; then
266       # rpm packages get iteration 1 if we don't supply one
267       iteration=${iteration:-1}
268       complete_pkgname="$pkgname-$version-${iteration}.$rpm_architecture.rpm"
269   fi
270   __returnvar=${complete_pkgname}
271 }
272
273 # Test if the package already exists, if not return 0, if it does return 1
274 test_package_presence() {
275     local pkgname="$1"; shift
276     local version="$1"; shift
277     local pkgtype="$1"; shift
278     local iteration="$1"; shift
279     local arch="$1"; shift
280     if [[ -n "$ONLY_BUILD" ]] && [[ "$pkgname" != "$ONLY_BUILD" ]] ; then
281       # arvados-workbench depends on arvados-server at build time, so even when
282       # only arvados-workbench is being built, we need to build arvados-server too
283       if [[ "$pkgname" != "arvados-server" ]] || [[ "$ONLY_BUILD" != "arvados-workbench" ]]; then
284         return 1
285       fi
286     fi
287
288     local full_pkgname
289     get_complete_package_name full_pkgname $pkgname $version $pkgtype $iteration $arch
290
291     # See if we can skip building the package, only if it already exists in the
292     # processed/ directory. If so, move it back to the packages directory to make
293     # sure it gets picked up by the test and/or upload steps.
294     # Get the list of packages from the repos
295
296     if [[ "$FORMAT" == "deb" ]]; then
297       declare -A dd
298       dd[debian9]=stretch
299       dd[debian10]=buster
300       dd[ubuntu1604]=xenial
301       dd[ubuntu1804]=bionic
302       D=${dd[$TARGET]}
303       if [ ${pkgname:0:3} = "lib" ]; then
304         repo_subdir=${pkgname:0:4}
305       else
306         repo_subdir=${pkgname:0:1}
307       fi
308
309       repo_pkg_list=$(curl -s -o - http://apt.arvados.org/pool/${D}/main/${repo_subdir}/)
310       echo ${repo_pkg_list} |grep -q ${full_pkgname}
311       if [ $? -eq 0 ] ; then
312         echo "Package $full_pkgname exists upstream, not rebuilding, downloading instead!"
313         curl -s -o "$WORKSPACE/packages/$TARGET/${full_pkgname}" http://apt.arvados.org/pool/${D}/main/${repo_subdir}/${full_pkgname}
314         return 1
315       elif test -f "$WORKSPACE/packages/$TARGET/processed/${full_pkgname}" ; then
316         echo "Package $full_pkgname exists, not rebuilding!"
317         return 1
318       else
319         echo "Package $full_pkgname not found, building"
320         return 0
321       fi
322     else
323       centos_repo="http://rpm.arvados.org/CentOS/7/dev/x86_64/"
324
325       repo_pkg_list=$(curl -s -o - ${centos_repo})
326       echo ${repo_pkg_list} |grep -q ${full_pkgname}
327       if [ $? -eq 0 ]; then
328         echo "Package $full_pkgname exists upstream, not rebuilding, downloading instead!"
329         curl -s -o "$WORKSPACE/packages/$TARGET/${full_pkgname}" ${centos_repo}${full_pkgname}
330         return 1
331       elif test -f "$WORKSPACE/packages/$TARGET/processed/${full_pkgname}" ; then
332         echo "Package $full_pkgname exists, not rebuilding!"
333         return 1
334       else
335         echo "Package $full_pkgname not found, building"
336         return 0
337       fi
338     fi
339 }
340
341 handle_rails_package() {
342     local pkgname="$1"; shift
343
344     if [[ -n "$ONLY_BUILD" ]] && [[ "$pkgname" != "$ONLY_BUILD" ]] ; then
345         return 0
346     fi
347     local srcdir="$1"; shift
348     cd "$srcdir"
349     local license_path="$1"; shift
350     local version="$(version_from_git)"
351     echo "$version" >package-build.version
352     local scripts_dir="$(mktemp --tmpdir -d "$pkgname-XXXXXXXX.scripts")" && \
353     (
354         set -e
355         _build_rails_package_scripts "$pkgname" "$scripts_dir"
356         cd "$srcdir"
357         mkdir -p tmp
358         git rev-parse HEAD >git-commit.version
359         bundle package --all
360     )
361     if [[ 0 != "$?" ]] || ! cd "$WORKSPACE/packages/$TARGET"; then
362         echo "ERROR: $pkgname package prep failed" >&2
363         rm -rf "$scripts_dir"
364         EXITCODE=1
365         return 1
366     fi
367     local railsdir="/var/www/${pkgname%-server}/current"
368     local -a pos_args=("$srcdir/=$railsdir" "$pkgname" dir "$version")
369     local license_arg="$license_path=$railsdir/$(basename "$license_path")"
370     local -a switches=(--after-install "$scripts_dir/postinst"
371                        --before-remove "$scripts_dir/prerm"
372                        --after-remove "$scripts_dir/postrm")
373     if [[ -z "$ARVADOS_BUILDING_VERSION" ]]; then
374         switches+=(--iteration $RAILS_PACKAGE_ITERATION)
375     fi
376     # For some reason fpm excludes need to not start with /.
377     local exclude_root="${railsdir#/}"
378     # .git and packages are for the SSO server, which is built from its
379     # repository root.
380     local -a exclude_list=(.git packages tmp log coverage Capfile\* \
381                            config/deploy\* config/application.yml)
382     # for arvados-workbench, we need to have the (dummy) config/database.yml in the package
383     if  [[ "$pkgname" != "arvados-workbench" ]]; then
384       exclude_list+=('config/database.yml')
385     fi
386     for exclude in ${exclude_list[@]}; do
387         switches+=(-x "$exclude_root/$exclude")
388     done
389     fpm_build "${pos_args[@]}" "${switches[@]}" \
390               -x "$exclude_root/vendor/cache-*" \
391               -x "$exclude_root/vendor/bundle" "$@" "$license_arg"
392     rm -rf "$scripts_dir"
393 }
394
395 # Build python packages with a virtualenv built-in
396 fpm_build_virtualenv () {
397   PKG=$1
398   shift
399   PKG_DIR=$1
400   shift
401   PACKAGE_TYPE=${1:-python}
402   shift
403
404   # Set up
405   STDOUT_IF_DEBUG=/dev/null
406   STDERR_IF_DEBUG=/dev/null
407   DASHQ_UNLESS_DEBUG=-q
408   if [[ "$DEBUG" != "0" ]]; then
409       STDOUT_IF_DEBUG=/dev/stdout
410       STDERR_IF_DEBUG=/dev/stderr
411       DASHQ_UNLESS_DEBUG=
412   fi
413   if [[ "$ARVADOS_BUILDING_ITERATION" == "" ]]; then
414     ARVADOS_BUILDING_ITERATION=1
415   fi
416
417   local python=""
418   case "$PACKAGE_TYPE" in
419     python3)
420         python=python3
421         if [[ "$FORMAT" != "rpm" ]]; then
422           pip=pip3
423         else
424           # In CentOS, we use a different mechanism to get the right version of pip
425           pip=pip
426         fi
427         PACKAGE_PREFIX=$PYTHON3_PKG_PREFIX
428         ;;
429     python)
430         # All Arvados Python2 packages depend on Python 2.7.
431         # Make sure we build with that for consistency.
432         python=python2.7
433         pip=pip
434         PACKAGE_PREFIX=$PYTHON2_PKG_PREFIX
435         ;;
436   esac
437
438   if [[ "$PKG" != "libpam-arvados" ]] &&
439      [[ "$PKG" != "arvados-node-manager" ]] &&
440      [[ "$PKG" != "arvados-docker-cleaner" ]]; then
441     PYTHON_PKG=$PACKAGE_PREFIX-$PKG
442   else
443     # Exception to our package naming convention
444     PYTHON_PKG=$PKG
445   fi
446
447   # arvados-python-client sdist should always be built, to be available
448   # for other dependant packages.
449   if [[ -n "$ONLY_BUILD" ]] && [[ "arvados-python-client" != "$PKG" ]] && [[ "$PYTHON_PKG" != "$ONLY_BUILD" ]] && [[ "$PKG" != "$ONLY_BUILD" ]]; then
450     return 0
451   fi
452
453   cd $WORKSPACE/$PKG_DIR
454
455   rm -rf dist/*
456
457   # Get the latest setuptools
458   if ! $pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U setuptools; then
459     echo "Error, unable to upgrade setuptools with"
460     echo "  $pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U setuptools"
461     exit 1
462   fi
463   # filter a useless warning (when building the cwltest package) from the stderr output
464   if ! $python setup.py $DASHQ_UNLESS_DEBUG sdist 2> >(grep -v 'warning: no previously-included files matching'); then
465     echo "Error, unable to run $python setup.py sdist for $PKG"
466     exit 1
467   fi
468
469   PACKAGE_PATH=`(cd dist; ls *tar.gz)`
470
471   if [[ "arvados-python-client" == "$PKG" ]]; then
472     PYSDK_PATH=`pwd`/dist/
473   fi
474
475   if [[ -n "$ONLY_BUILD" ]] && [[ "$PYTHON_PKG" != "$ONLY_BUILD" ]] && [[ "$PKG" != "$ONLY_BUILD" ]]; then
476     return 0
477   fi
478
479   # Determine the package version from the generated sdist archive
480   PYTHON_VERSION=${ARVADOS_BUILDING_VERSION:-$(awk '($1 == "Version:"){print $2}' *.egg-info/PKG-INFO)}
481
482   # See if we actually need to build this package; does it exist already?
483   # We can't do this earlier than here, because we need PYTHON_VERSION...
484   # This isn't so bad; the sdist call above is pretty quick compared to
485   # the invocation of virtualenv and fpm, below.
486   if ! test_package_presence "$PYTHON_PKG" $PYTHON_VERSION $PACKAGE_TYPE $ARVADOS_BUILDING_ITERATION; then
487     return 0
488   fi
489
490   echo "Building $FORMAT package for $PKG from $PKG_DIR"
491
492   # Package the sdist in a virtualenv
493   echo "Creating virtualenv..."
494
495   cd dist
496
497   rm -rf build
498   rm -f $PYTHON_PKG*deb
499   echo "virtualenv version: `virtualenv --version`"
500   virtualenv_command="virtualenv --python `which $python` $DASHQ_UNLESS_DEBUG build/usr/share/$python/dist/$PYTHON_PKG"
501
502   if ! $virtualenv_command; then
503     echo "Error, unable to run"
504     echo "  $virtualenv_command"
505     exit 1
506   fi
507
508   if ! build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U pip; then
509     echo "Error, unable to upgrade pip with"
510     echo "  build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U pip"
511     exit 1
512   fi
513   echo "pip version:        `build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip --version`"
514
515   if ! build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U setuptools; then
516     echo "Error, unable to upgrade setuptools with"
517     echo "  build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U setuptools"
518     exit 1
519   fi
520   echo "setuptools version: `build/usr/share/$python/dist/$PYTHON_PKG/bin/$python -c 'import setuptools; print(setuptools.__version__)'`"
521
522   if ! build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U wheel; then
523     echo "Error, unable to upgrade wheel with"
524     echo "  build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -U wheel"
525     exit 1
526   fi
527   echo "wheel version:      `build/usr/share/$python/dist/$PYTHON_PKG/bin/wheel version`"
528
529   if [[ "$TARGET" != "centos7" ]] || [[ "$PYTHON_PKG" != "python-arvados-fuse" ]]; then
530     build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -f $PYSDK_PATH $PACKAGE_PATH
531   else
532     # centos7 needs these special tweaks to install python-arvados-fuse
533     build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG docutils
534     PYCURL_SSL_LIBRARY=nss build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -f $PYSDK_PATH $PACKAGE_PATH
535   fi
536
537   if [[ "$?" != "0" ]]; then
538     echo "Error, unable to run"
539     echo "  build/usr/share/$python/dist/$PYTHON_PKG/bin/$pip install $DASHQ_UNLESS_DEBUG $CACHE_FLAG -f $PYSDK_PATH $PACKAGE_PATH"
540     exit 1
541   fi
542
543   cd build/usr/share/$python/dist/$PYTHON_PKG/
544
545   # Replace the shebang lines in all python scripts, and handle the activate
546   # scripts too This is a functional replacement of the 237 line
547   # virtualenv_tools.py script that doesn't work in python3 without serious
548   # patching, minus the parts we don't need (modifying pyc files, etc).
549   for binfile in `ls bin/`; do
550     if ! file --mime bin/$binfile |grep -q binary; then
551       # Not a binary file
552       if [[ "$binfile" =~ ^activate(.csh|.fish|)$ ]]; then
553         # these 'activate' scripts need special treatment
554         sed -i "s/VIRTUAL_ENV=\".*\"/VIRTUAL_ENV=\"\/usr\/share\/$python\/dist\/$PYTHON_PKG\"/" bin/$binfile
555         sed -i "s/VIRTUAL_ENV \".*\"/VIRTUAL_ENV \"\/usr\/share\/$python\/dist\/$PYTHON_PKG\"/" bin/$binfile
556       else
557         if grep -q -E '^#!.*/bin/python\d?' bin/$binfile; then
558           # Replace shebang line
559           sed -i "1 s/^.*$/#!\/usr\/share\/$python\/dist\/$PYTHON_PKG\/bin\/python/" bin/$binfile
560         fi
561       fi
562     fi
563   done
564
565   cd - >$STDOUT_IF_DEBUG
566
567   find build -iname '*.pyc' -exec rm {} \;
568   find build -iname '*.pyo' -exec rm {} \;
569
570   # Finally, generate the package
571   echo "Creating package..."
572
573   declare -a COMMAND_ARR=("fpm" "-s" "dir" "-t" "$FORMAT")
574
575   if [[ "$MAINTAINER" != "" ]]; then
576     COMMAND_ARR+=('--maintainer' "$MAINTAINER")
577   fi
578
579   if [[ "$VENDOR" != "" ]]; then
580     COMMAND_ARR+=('--vendor' "$VENDOR")
581   fi
582
583   COMMAND_ARR+=('--url' 'https://arvados.org')
584
585   # Get description
586   DESCRIPTION=`grep '\sdescription' $WORKSPACE/$PKG_DIR/setup.py|cut -f2 -d=|sed -e "s/[',\\"]//g"`
587   COMMAND_ARR+=('--description' "$DESCRIPTION")
588
589   # Get license string
590   LICENSE_STRING=`grep license $WORKSPACE/$PKG_DIR/setup.py|cut -f2 -d=|sed -e "s/[',\\"]//g"`
591   COMMAND_ARR+=('--license' "$LICENSE_STRING")
592
593   # 12271 - As FPM-generated packages don't include scripts by default, the
594   # packages cleanup on upgrade depends on files being listed on the %files
595   # section in the generated SPEC files. To remove DIRECTORIES, they need to
596   # be listed in that sectiontoo, so we need to add this parameter to properly
597   # remove lingering dirs. But this only works for python2: if used on
598   # python33, it includes dirs like /opt/rh/python33 that belong to
599   # other packages.
600   if [[ "$FORMAT" == "rpm" ]] && [[ "$python" == "python2.7" ]]; then
601     COMMAND_ARR+=('--rpm-auto-add-directories')
602   fi
603
604   if [[ "$PKG" == "arvados-python-client" ]]; then
605     if [[ "$python" == "python2.7" ]]; then
606       COMMAND_ARR+=('--conflicts' "$PYTHON3_PKG_PREFIX-$PKG")
607     else
608       COMMAND_ARR+=('--conflicts' "$PYTHON2_PKG_PREFIX-$PKG")
609     fi
610   fi
611
612   if [[ "$DEBUG" != "0" ]]; then
613     COMMAND_ARR+=('--verbose' '--log' 'info')
614   fi
615
616   COMMAND_ARR+=('-v' "$PYTHON_VERSION")
617   COMMAND_ARR+=('--iteration' "$ARVADOS_BUILDING_ITERATION")
618   COMMAND_ARR+=('-n' "$PYTHON_PKG")
619   COMMAND_ARR+=('-C' "build")
620
621   if [[ -e "$WORKSPACE/$PKG_DIR/$PKG.service" ]]; then
622     COMMAND_ARR+=('--after-install' "${WORKSPACE}/build/go-python-package-scripts/postinst")
623     COMMAND_ARR+=('--before-remove' "${WORKSPACE}/build/go-python-package-scripts/prerm")
624   fi
625
626   if [[ "$python" == "python2.7" ]]; then
627     COMMAND_ARR+=('--depends' "$PYTHON2_PACKAGE")
628   else
629     COMMAND_ARR+=('--depends' "$PYTHON3_PACKAGE")
630   fi
631
632   # avoid warning
633   COMMAND_ARR+=('--deb-no-default-config-files')
634
635   # Append --depends X and other arguments specified by fpm-info.sh in
636   # the package source dir. These are added last so they can override
637   # the arguments added by this script.
638   declare -a fpm_args=()
639   declare -a fpm_depends=()
640
641   fpminfo="$WORKSPACE/$PKG_DIR/fpm-info.sh"
642   if [[ -e "$fpminfo" ]]; then
643     echo "Loading fpm overrides from $fpminfo"
644     if ! source "$fpminfo"; then
645       echo "Error, unable to source $WORKSPACE/$PKG_DIR/fpm-info.sh for $PKG"
646       exit 1
647     fi
648   fi
649
650   for i in "${fpm_depends[@]}"; do
651     COMMAND_ARR+=('--depends' "$i")
652   done
653
654   COMMAND_ARR+=("${fpm_args[@]}")
655
656   # Make sure to install all our package binaries in /usr/bin.
657   # We have to walk $WORKSPACE/$PKG_DIR/bin rather than
658   # $WORKSPACE/build/usr/share/$python/dist/$PYTHON_PKG/bin/ to get the list
659   # because the latter also includes all the python binaries for the virtualenv.
660   # We have to take the copies of our binaries from the latter directory, though,
661   # because those are the ones we rewrote the shebang line of, above.
662   if [[ -e "$WORKSPACE/$PKG_DIR/bin" ]]; then
663     for binary in `ls $WORKSPACE/$PKG_DIR/bin`; do
664       COMMAND_ARR+=("usr/share/$python/dist/$PYTHON_PKG/bin/$binary=/usr/bin/")
665     done
666   fi
667
668   # the libpam module should place a few files in the correct place for the pam
669   # subsystem
670   if [[ -e "$WORKSPACE/$PKG_DIR/dist/build/usr/share/$python/dist/$PYTHON_PKG/lib/security/libpam_arvados.py" ]]; then
671     COMMAND_ARR+=("usr/share/$python/dist/$PYTHON_PKG/lib/security/libpam_arvados.py=/usr/lib/security/")
672   fi
673   if [[ -e "$WORKSPACE/$PKG_DIR/dist/build/usr/share/$python/dist/$PYTHON_PKG/share/pam-configs/arvados" ]]; then
674     COMMAND_ARR+=("usr/share/$python/dist/$PYTHON_PKG/share/pam-configs/arvados=/usr/share/pam-configs/")
675   fi
676
677   # the python-arvados-cwl-runner package comes with cwltool, expose that version
678   if [[ -e "$WORKSPACE/$PKG_DIR/dist/build/usr/share/python2.7/dist/python-arvados-cwl-runner/bin/cwltool" ]]; then
679     COMMAND_ARR+=("usr/share/python2.7/dist/python-arvados-cwl-runner/bin/cwltool=/usr/bin/")
680   fi
681
682   COMMAND_ARR+=(".")
683
684   FPM_RESULTS=$("${COMMAND_ARR[@]}")
685   FPM_EXIT_CODE=$?
686
687   # if something went wrong and debug is off, print out the fpm command that errored
688   if ! fpm_verify $FPM_EXIT_CODE $FPM_RESULTS && [[ "$STDOUT_IF_DEBUG" == "/dev/null" ]]; then
689     echo "fpm returned an error executing the command:"
690     echo
691     echo -e "\n${COMMAND_ARR[@]}\n"
692   else
693     echo `ls *$FORMAT`
694     mv $WORKSPACE/$PKG_DIR/dist/*$FORMAT $WORKSPACE/packages/$TARGET/
695   fi
696   echo
697 }
698
699 # Build packages for everything
700 fpm_build () {
701   # The package source.  Depending on the source type, this can be a
702   # path, or the name of the package in an upstream repository (e.g.,
703   # pip).
704   PACKAGE=$1
705   shift
706   # The name of the package to build.
707   PACKAGE_NAME=$1
708   shift
709   # The type of source package.  Passed to fpm -s.  Default "dir".
710   PACKAGE_TYPE=${1:-dir}
711   shift
712   # Optional: the package version number.  Passed to fpm -v.
713   VERSION=$1
714   shift
715
716   if [[ -n "$ONLY_BUILD" ]] && [[ "$PACKAGE_NAME" != "$ONLY_BUILD" ]] && [[ "$PACKAGE" != "$ONLY_BUILD" ]] ; then
717     # arvados-workbench depends on arvados-server at build time, so even when
718     # only arvados-workbench is being built, we need to build arvados-server too
719     if [[ "$PACKAGE_NAME" != "arvados-server" ]] || [[ "$ONLY_BUILD" != "arvados-workbench" ]]; then
720       return 0
721     fi
722   fi
723
724   local default_iteration_value="$(default_iteration "$PACKAGE" "$VERSION" "$PACKAGE_TYPE")"
725
726   declare -a COMMAND_ARR=("fpm" "-s" "$PACKAGE_TYPE" "-t" "$FORMAT")
727   if [ python = "$PACKAGE_TYPE" ] && [ deb = "$FORMAT" ]; then
728       # Dependencies are built from setup.py.  Since setup.py will never
729       # refer to Debian package iterations, it doesn't make sense to
730       # enforce those in the .deb dependencies.
731       COMMAND_ARR+=(--deb-ignore-iteration-in-dependencies)
732   fi
733
734   # 12271 - As FPM-generated packages don't include scripts by default, the
735   # packages cleanup on upgrade depends on files being listed on the %files
736   # section in the generated SPEC files. To remove DIRECTORIES, they need to
737   # be listed in that section too, so we need to add this parameter to properly
738   # remove lingering dirs. But this only works for python2: if used on
739   # python33, it includes dirs like /opt/rh/python33 that belong to
740   # other packages.
741   if [[ "$FORMAT" = rpm ]] && [[ "$python" = python2.7 ]]; then
742     COMMAND_ARR+=('--rpm-auto-add-directories')
743   fi
744
745   if [[ "$DEBUG" != "0" ]]; then
746     COMMAND_ARR+=('--verbose' '--log' 'info')
747   fi
748
749   if [[ -n "$PACKAGE_NAME" ]]; then
750     COMMAND_ARR+=('-n' "$PACKAGE_NAME")
751   fi
752
753   if [[ "$MAINTAINER" != "" ]]; then
754     COMMAND_ARR+=('--maintainer' "$MAINTAINER")
755   fi
756
757   if [[ "$VENDOR" != "" ]]; then
758     COMMAND_ARR+=('--vendor' "$VENDOR")
759   fi
760
761   if [[ "$VERSION" != "" ]]; then
762     COMMAND_ARR+=('-v' "$VERSION")
763   fi
764   if [[ -n "$default_iteration_value" ]]; then
765       # We can always add an --iteration here.  If another one is specified in $@,
766       # that will take precedence, as desired.
767       COMMAND_ARR+=(--iteration "$default_iteration_value")
768   fi
769
770   # Append --depends X and other arguments specified by fpm-info.sh in
771   # the package source dir. These are added last so they can override
772   # the arguments added by this script.
773   declare -a fpm_args=()
774   declare -a build_depends=()
775   declare -a fpm_depends=()
776   declare -a fpm_exclude=()
777   declare -a fpm_dirs=(
778       # source dir part of 'dir' package ("/source=/dest" => "/source"):
779       "${PACKAGE%%=/*}")
780   for pkgdir in "${fpm_dirs[@]}"; do
781       fpminfo="$pkgdir/fpm-info.sh"
782       if [[ -e "$fpminfo" ]]; then
783           debug_echo "Loading fpm overrides from $fpminfo"
784           source "$fpminfo"
785           break
786       fi
787   done
788   for pkg in "${build_depends[@]}"; do
789       if [[ $TARGET =~ debian|ubuntu ]]; then
790           pkg_deb=$(ls "$WORKSPACE/packages/$TARGET/$pkg_"*.deb | sort -rg | awk 'NR==1')
791           if [[ -e $pkg_deb ]]; then
792               echo "Installing build_dep $pkg from $pkg_deb"
793               dpkg -i "$pkg_deb"
794           else
795               echo "Attemping to install build_dep $pkg using apt-get"
796               apt-get install -y "$pkg"
797           fi
798           apt-get -y -f install
799       else
800           pkg_rpm=$(ls "$WORKSPACE/packages/$TARGET/$pkg"-[0-9]*.rpm | sort -rg | awk 'NR==1')
801           if [[ -e $pkg_rpm ]]; then
802               echo "Installing build_dep $pkg from $pkg_rpm"
803               rpm -i "$pkg_rpm"
804           else
805               echo "Attemping to install build_dep $pkg"
806               rpm -i "$pkg"
807           fi
808       fi
809   done
810   for i in "${fpm_depends[@]}"; do
811     COMMAND_ARR+=('--depends' "$i")
812   done
813   for i in "${fpm_exclude[@]}"; do
814     COMMAND_ARR+=('--exclude' "$i")
815   done
816
817   # Append remaining function arguments directly to fpm's command line.
818   for i; do
819     COMMAND_ARR+=("$i")
820   done
821
822   COMMAND_ARR+=("${fpm_args[@]}")
823
824   COMMAND_ARR+=("$PACKAGE")
825
826   debug_echo -e "\n${COMMAND_ARR[@]}\n"
827
828   FPM_RESULTS=$("${COMMAND_ARR[@]}")
829   FPM_EXIT_CODE=$?
830
831   fpm_verify $FPM_EXIT_CODE $FPM_RESULTS
832
833   # if something went wrong and debug is off, print out the fpm command that errored
834   if [[ 0 -ne $? ]] && [[ "$STDOUT_IF_DEBUG" == "/dev/null" ]]; then
835     echo -e "\n${COMMAND_ARR[@]}\n"
836   fi
837 }
838
839 # verify build results
840 fpm_verify () {
841   FPM_EXIT_CODE=$1
842   shift
843   FPM_RESULTS=$@
844
845   FPM_PACKAGE_NAME=''
846   if [[ $FPM_RESULTS =~ ([A-Za-z0-9_\.-]*\.)(deb|rpm) ]]; then
847     FPM_PACKAGE_NAME=${BASH_REMATCH[1]}${BASH_REMATCH[2]}
848   fi
849
850   if [[ "$FPM_PACKAGE_NAME" == "" ]]; then
851     EXITCODE=1
852     echo
853     echo "Error: $PACKAGE: Unable to figure out package name from fpm results:"
854     echo
855     echo $FPM_RESULTS
856     echo
857     return 1
858   elif [[ "$FPM_RESULTS" =~ "File already exists" ]]; then
859     echo "Package $FPM_PACKAGE_NAME exists, not rebuilding"
860     return 0
861   elif [[ 0 -ne "$FPM_EXIT_CODE" ]]; then
862     EXITCODE=1
863     echo "Error building package for $1:\n $FPM_RESULTS"
864     return 1
865   fi
866 }
867
868 install_package() {
869   PACKAGES=$@
870   if [[ "$FORMAT" == "deb" ]]; then
871     $SUDO apt-get install $PACKAGES --yes
872   elif [[ "$FORMAT" == "rpm" ]]; then
873     $SUDO yum -q -y install $PACKAGES
874   fi
875 }
876
877 title() {
878     printf '%s %s\n' "=======" "$1"
879 }
880
881 checkexit() {
882     if [[ "$1" != "0" ]]; then
883         title "$2 -- FAILED"
884         failures+=("$2 (`timer`)")
885     else
886         successes+=("$2 (`timer`)")
887     fi
888 }
889
890 timer_reset() {
891     t0=$SECONDS
892 }
893
894 timer() {
895     echo -n "$(($SECONDS - $t0))s"
896 }
897
898 report_outcomes() {
899     for x in "${successes[@]}"
900     do
901         echo "Pass: $x"
902     done
903
904     if [[ ${#failures[@]} == 0 ]]
905     then
906         if [[ ${#successes[@]} != 0 ]]; then
907            echo "All test suites passed."
908         fi
909     else
910         echo "Failures (${#failures[@]}):"
911         for x in "${failures[@]}"
912         do
913             echo "Fail: $x"
914         done
915     fi
916 }