Merge branch 'master' into 7370-package-install-testing
[arvados-dev.git] / jenkins / run-library.sh
1 #!/bin/bash
2
3 # A library of functions shared by the various scripts in this directory.
4
5 debug_echo () {
6     echo "$@" >"$STDOUT_IF_DEBUG"
7 }
8
9 find_easy_install() {
10     for version_suffix in "$@"; do
11         if "easy_install$version_suffix" --version >/dev/null 2>&1; then
12             echo "easy_install$version_suffix"
13             return 0
14         fi
15     done
16     cat >&2 <<EOF
17 $helpmessage
18
19 Error: easy_install$1 (from Python setuptools module) not found
20
21 EOF
22     exit 1
23 }
24
25 format_last_commit_here() {
26     local format=$1; shift
27     TZ=UTC git log -n1 --first-parent "--format=format:$format" .
28 }
29
30 version_from_git() {
31   # Generates a version number from the git log for the current working
32   # directory, and writes it to stdout.
33   local git_ts git_hash
34   declare $(format_last_commit_here "git_ts=%ct git_hash=%h")
35   echo "0.1.$(date -ud "@$git_ts" +%Y%m%d%H%M%S).$git_hash"
36 }
37
38 nohash_version_from_git() {
39     version_from_git | cut -d. -f1-3
40 }
41
42 timestamp_from_git() {
43     format_last_commit_here "%ct"
44 }
45
46 handle_python_package () {
47   # This function assumes the current working directory is the python package directory
48   if [ -n "$(find dist -name "*-$(nohash_version_from_git).tar.gz" -print -quit)" ]; then
49     # This package doesn't need rebuilding.
50     return
51   fi
52   # Make sure only to use sdist - that's the only format pip can deal with (sigh)
53   python setup.py $DASHQ_UNLESS_DEBUG sdist
54 }
55
56 handle_ruby_gem() {
57     local gem_name=$1; shift
58     local gem_version=$(nohash_version_from_git)
59     local gem_src_dir="$(pwd)"
60
61     if ! [[ -e "${gem_name}-${gem_version}.gem" ]]; then
62         find -maxdepth 1 -name "${gem_name}-*.gem" -delete
63
64         # -q appears to be broken in gem version 2.2.2
65         $GEM build "$gem_name.gemspec" $DASHQ_UNLESS_DEBUG >"$STDOUT_IF_DEBUG" 2>"$STDERR_IF_DEBUG"
66     fi
67 }
68
69 # Build packages for everything
70 fpm_build () {
71   # The package source.  Depending on the source type, this can be a
72   # path, or the name of the package in an upstream repository (e.g.,
73   # pip).
74   PACKAGE=$1
75   shift
76   # The name of the package to build.  Defaults to $PACKAGE.
77   PACKAGE_NAME=${1:-$PACKAGE}
78   shift
79   # Optional: the vendor of the package.  Should be "Curoverse, Inc." for
80   # packages of our own software.  Passed to fpm --vendor.
81   VENDOR=$1
82   shift
83   # The type of source package.  Passed to fpm -s.  Default "python".
84   PACKAGE_TYPE=${1:-python}
85   shift
86   # Optional: the package version number.  Passed to fpm -v.
87   VERSION=$1
88   shift
89
90   case "$PACKAGE_TYPE" in
91       python)
92           # All Arvados Python2 packages depend on Python 2.7.
93           # Make sure we build with that for consistency.
94           set -- "$@" --python-bin python2.7 \
95               --python-easyinstall "$EASY_INSTALL2" \
96               --python-package-name-prefix "$PYTHON2_PKG_PREFIX" \
97               --depends "$PYTHON2_PACKAGE"
98           ;;
99       python3)
100           # fpm does not actually support a python3 package type.  Instead
101           # we recognize it as a convenience shortcut to add several
102           # necessary arguments to fpm's command line later, after we're
103           # done handling positional arguments.
104           PACKAGE_TYPE=python
105           set -- "$@" --python-bin python3 \
106               --python-easyinstall "$EASY_INSTALL3" \
107               --python-package-name-prefix "$PYTHON3_PKG_PREFIX" \
108               --depends "$PYTHON3_PACKAGE"
109           ;;
110   esac
111
112   declare -a COMMAND_ARR=("fpm" "--maintainer=Ward Vandewege <ward@curoverse.com>" "-s" "$PACKAGE_TYPE" "-t" "$FORMAT")
113   if [ python = "$PACKAGE_TYPE" ]; then
114     COMMAND_ARR+=(--exclude=\*/{dist,site}-packages/tests/\*)
115   fi
116
117   if [[ "$PACKAGE_NAME" != "$PACKAGE" ]]; then
118     COMMAND_ARR+=('-n' "$PACKAGE_NAME")
119   fi
120
121   if [[ "$VENDOR" != "" ]]; then
122     COMMAND_ARR+=('--vendor' "$VENDOR")
123   fi
124
125   if [[ "$VERSION" != "" ]]; then
126     COMMAND_ARR+=('-v' "$VERSION")
127   fi
128
129   # Append remaining function arguments directly to fpm's command line.
130   for i; do
131     COMMAND_ARR+=("$i")
132   done
133
134   # Append --depends X and other arguments specified by fpm-info.sh in
135   # the package source dir. These are added last so they can override
136   # the arguments added by this script.
137   declare -a fpm_args=()
138   declare -a fpm_depends=()
139   if [[ -d "$PACKAGE" ]]; then
140       FPM_INFO="$PACKAGE/fpm-info.sh"
141   else
142       FPM_INFO="${WORKSPACE}/backports/${PACKAGE_TYPE}-${PACKAGE}/fpm-info.sh"
143   fi
144   if [[ -e "$FPM_INFO" ]]; then
145       debug_echo "Loading fpm overrides from $FPM_INFO"
146       source "$FPM_INFO"
147   fi
148   for i in "${fpm_depends[@]}"; do
149     COMMAND_ARR+=('--depends' "$i")
150   done
151   COMMAND_ARR+=("${fpm_args[@]}")
152
153   COMMAND_ARR+=("$PACKAGE")
154
155   debug_echo -e "\n${COMMAND_ARR[@]}\n"
156
157   FPM_RESULTS=$("${COMMAND_ARR[@]}")
158   FPM_EXIT_CODE=$?
159
160   fpm_verify $FPM_EXIT_CODE $FPM_RESULTS
161 }
162
163 # verify build results
164 fpm_verify () {
165   FPM_EXIT_CODE=$1
166   shift
167   FPM_RESULTS=$@
168
169   FPM_PACKAGE_NAME=''
170   if [[ $FPM_RESULTS =~ ([A-Za-z0-9_\.-]*\.)(deb|rpm) ]]; then
171     FPM_PACKAGE_NAME=${BASH_REMATCH[1]}${BASH_REMATCH[2]}
172   fi
173
174   if [[ "$FPM_PACKAGE_NAME" == "" ]]; then
175     EXITCODE=1
176     echo "Error: $PACKAGE: Unable to figure out package name from fpm results:"
177     echo
178     echo $FPM_RESULTS
179     echo
180   elif [[ "$FPM_RESULTS" =~ "File already exists" ]]; then
181     echo "Package $FPM_PACKAGE_NAME exists, not rebuilding"
182   elif [[ 0 -ne "$FPM_EXIT_CODE" ]]; then
183     echo "Error building package for $1:\n $FPM_RESULTS"
184   fi
185 }
186
187 install_package() {
188   PACKAGES=$@
189   if [[ "$FORMAT" == "deb" ]]; then
190     $SUDO apt-get install $PACKAGES --yes
191   elif [[ "$FORMAT" == "rpm" ]]; then
192     $SUDO yum -q -y install $PACKAGES
193   fi
194 }