7710: add crunchrunner to gostuff
[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 # Usage: package_go_binary services/foo arvados-foo "Compute foo to arbitrary precision"
70 package_go_binary() {
71     local src_path="$1"; shift
72     local prog="$1"; shift
73     local description="$1"; shift
74
75     debug_echo "package_go_binary $src_path as $prog"
76
77     local basename="${src_path##*/}"
78
79     mkdir -p "$GOPATH/src/git.curoverse.com"
80     ln -sfn "$WORKSPACE" "$GOPATH/src/git.curoverse.com/arvados.git"
81
82     cd "$GOPATH/src/git.curoverse.com/arvados.git/$src_path"
83     local version=$(version_from_git)
84     local timestamp=$(timestamp_from_git)
85
86     # If the command imports anything from the Arvados SDK, bump the
87     # version number and build a new package whenever the SDK changes.
88     if grep -qr git.curoverse.com/arvados .; then
89         cd "$GOPATH/src/git.curoverse.com/arvados.git/sdk/go"
90         if [[ $(timestamp_from_git) -gt "$timestamp" ]]; then
91             version=$(version_from_git)
92         fi
93     fi
94
95     cd $WORKSPACE/packages/$TARGET
96     go get "git.curoverse.com/arvados.git/$src_path"
97     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"
98 }
99
100 # Build packages for everything
101 fpm_build () {
102   # The package source.  Depending on the source type, this can be a
103   # path, or the name of the package in an upstream repository (e.g.,
104   # pip).
105   PACKAGE=$1
106   shift
107   # The name of the package to build.  Defaults to $PACKAGE.
108   PACKAGE_NAME=${1:-$PACKAGE}
109   shift
110   # Optional: the vendor of the package.  Should be "Curoverse, Inc." for
111   # packages of our own software.  Passed to fpm --vendor.
112   VENDOR=$1
113   shift
114   # The type of source package.  Passed to fpm -s.  Default "python".
115   PACKAGE_TYPE=${1:-python}
116   shift
117   # Optional: the package version number.  Passed to fpm -v.
118   VERSION=$1
119   shift
120
121   case "$PACKAGE_TYPE" in
122       python)
123           # All Arvados Python2 packages depend on Python 2.7.
124           # Make sure we build with that for consistency.
125           set -- "$@" --python-bin python2.7 \
126               --python-easyinstall "$EASY_INSTALL2" \
127               --python-package-name-prefix "$PYTHON2_PKG_PREFIX" \
128               --depends "$PYTHON2_PACKAGE"
129           ;;
130       python3)
131           # fpm does not actually support a python3 package type.  Instead
132           # we recognize it as a convenience shortcut to add several
133           # necessary arguments to fpm's command line later, after we're
134           # done handling positional arguments.
135           PACKAGE_TYPE=python
136           set -- "$@" --python-bin python3 \
137               --python-easyinstall "$EASY_INSTALL3" \
138               --python-package-name-prefix "$PYTHON3_PKG_PREFIX" \
139               --depends "$PYTHON3_PACKAGE"
140           ;;
141   esac
142
143   declare -a COMMAND_ARR=("fpm" "--maintainer=Ward Vandewege <ward@curoverse.com>" "-s" "$PACKAGE_TYPE" "-t" "$FORMAT")
144   if [ python = "$PACKAGE_TYPE" ]; then
145     COMMAND_ARR+=(--exclude=\*/{dist,site}-packages/tests/\*)
146   fi
147
148   if [[ "$PACKAGE_NAME" != "$PACKAGE" ]]; then
149     COMMAND_ARR+=('-n' "$PACKAGE_NAME")
150   fi
151
152   if [[ "$VENDOR" != "" ]]; then
153     COMMAND_ARR+=('--vendor' "$VENDOR")
154   fi
155
156   if [[ "$VERSION" != "" ]]; then
157     COMMAND_ARR+=('-v' "$VERSION")
158   fi
159
160   # Append remaining function arguments directly to fpm's command line.
161   for i; do
162     COMMAND_ARR+=("$i")
163   done
164
165   # Append --depends X and other arguments specified by fpm-info.sh in
166   # the package source dir. These are added last so they can override
167   # the arguments added by this script.
168   declare -a fpm_args=()
169   declare -a fpm_depends=()
170   if [[ -d "$PACKAGE" ]]; then
171       FPM_INFO="$PACKAGE/fpm-info.sh"
172   else
173       FPM_INFO="${WORKSPACE}/backports/${PACKAGE_TYPE}-${PACKAGE}/fpm-info.sh"
174   fi
175   if [[ -e "$FPM_INFO" ]]; then
176       debug_echo "Loading fpm overrides from $FPM_INFO"
177       source "$FPM_INFO"
178   fi
179   for i in "${fpm_depends[@]}"; do
180     COMMAND_ARR+=('--depends' "$i")
181   done
182   COMMAND_ARR+=("${fpm_args[@]}")
183
184   COMMAND_ARR+=("$PACKAGE")
185
186   debug_echo -e "\n${COMMAND_ARR[@]}\n"
187
188   FPM_RESULTS=$("${COMMAND_ARR[@]}")
189   FPM_EXIT_CODE=$?
190
191   fpm_verify $FPM_EXIT_CODE $FPM_RESULTS
192 }
193
194 # verify build results
195 fpm_verify () {
196   FPM_EXIT_CODE=$1
197   shift
198   FPM_RESULTS=$@
199
200   FPM_PACKAGE_NAME=''
201   if [[ $FPM_RESULTS =~ ([A-Za-z0-9_\.-]*\.)(deb|rpm) ]]; then
202     FPM_PACKAGE_NAME=${BASH_REMATCH[1]}${BASH_REMATCH[2]}
203   fi
204
205   if [[ "$FPM_PACKAGE_NAME" == "" ]]; then
206     EXITCODE=1
207     echo "Error: $PACKAGE: Unable to figure out package name from fpm results:"
208     echo
209     echo $FPM_RESULTS
210     echo
211   elif [[ "$FPM_RESULTS" =~ "File already exists" ]]; then
212     echo "Package $FPM_PACKAGE_NAME exists, not rebuilding"
213   elif [[ 0 -ne "$FPM_EXIT_CODE" ]]; then
214     echo "Error building package for $1:\n $FPM_RESULTS"
215   fi
216 }
217
218 install_package() {
219   PACKAGES=$@
220   if [[ "$FORMAT" == "deb" ]]; then
221     $SUDO apt-get install $PACKAGES --yes
222   elif [[ "$FORMAT" == "rpm" ]]; then
223     $SUDO yum -q -y install $PACKAGES
224   fi
225 }