Build package for arvados-cwl-runner.
[arvados-dev.git] / jenkins / run-build-packages.sh
1 #!/bin/bash
2
3 . `dirname "$(readlink -f "$0")"`/run-library.sh
4 . `dirname "$(readlink -f "$0")"`/libcloud-pin
5
6 read -rd "\000" helpmessage <<EOF
7 $(basename $0): Build Arvados packages
8
9 Syntax:
10         WORKSPACE=/path/to/arvados $(basename $0) [options]
11
12 Options:
13
14 --build-bundle-packages  (default: false)
15     Build api server and workbench packages with vendor/bundle included
16 --debug
17     Output debug information (default: false)
18 --target
19     Distribution to build packages for (default: debian7)
20 --command
21     Build command to execute (defaults to the run command defined in the
22     Docker image)
23
24 WORKSPACE=path         Path to the Arvados source tree to build packages from
25
26 EOF
27
28 EXITCODE=0
29 DEBUG=${ARVADOS_DEBUG:-0}
30 TARGET=debian7
31 COMMAND=
32
33 PARSEDOPTS=$(getopt --name "$0" --longoptions \
34     help,build-bundle-packages,debug,target: \
35     -- "" "$@")
36 if [ $? -ne 0 ]; then
37     exit 1
38 fi
39
40 eval set -- "$PARSEDOPTS"
41 while [ $# -gt 0 ]; do
42     case "$1" in
43         --help)
44             echo >&2 "$helpmessage"
45             echo >&2
46             exit 1
47             ;;
48         --target)
49             TARGET="$2"; shift
50             ;;
51         --debug)
52             DEBUG=1
53             ;;
54         --command)
55             COMMAND="$2"; shift
56             ;;
57         --)
58             if [ $# -gt 1 ]; then
59                 echo >&2 "$0: unrecognized argument '$2'. Try: $0 --help"
60                 exit 1
61             fi
62             ;;
63     esac
64     shift
65 done
66
67 if [[ "$COMMAND" != "" ]]; then
68   COMMAND="/usr/local/rvm/bin/rvm-exec default bash /jenkins/$COMMAND --target $TARGET"
69 fi
70
71 STDOUT_IF_DEBUG=/dev/null
72 STDERR_IF_DEBUG=/dev/null
73 DASHQ_UNLESS_DEBUG=-q
74 if [[ "$DEBUG" != 0 ]]; then
75     STDOUT_IF_DEBUG=/dev/stdout
76     STDERR_IF_DEBUG=/dev/stderr
77     DASHQ_UNLESS_DEBUG=
78 fi
79
80 declare -a PYTHON_BACKPORTS PYTHON3_BACKPORTS
81
82 PYTHON2_VERSION=2.7
83 PYTHON3_VERSION=$(python3 -c 'import sys; print("{v.major}.{v.minor}".format(v=sys.version_info))')
84
85 case "$TARGET" in
86     debian7)
87         FORMAT=deb
88         PYTHON2_PACKAGE=python$PYTHON2_VERSION
89         PYTHON2_PKG_PREFIX=python
90         PYTHON3_PACKAGE=python$PYTHON3_VERSION
91         PYTHON3_PKG_PREFIX=python3
92         PYTHON_BACKPORTS=(python-gflags pyvcf google-api-python-client==1.4.2 \
93             oauth2client==1.5.2 pyasn1==0.1.7 pyasn1-modules==0.0.5 \
94             rsa uritemplate httplib2 ws4py pykka six pyexecjs jsonschema \
95             ciso8601 pycrypto backports.ssl_match_hostname llfuse==0.41.1 \
96             'pycurl<7.21.5')
97         PYTHON3_BACKPORTS=(docker-py six requests websocket-client)
98         ;;
99     debian8)
100         FORMAT=deb
101         PYTHON2_PACKAGE=python$PYTHON2_VERSION
102         PYTHON2_PKG_PREFIX=python
103         PYTHON3_PACKAGE=python$PYTHON3_VERSION
104         PYTHON3_PKG_PREFIX=python3
105         PYTHON_BACKPORTS=(python-gflags pyvcf google-api-python-client==1.4.2 \
106             oauth2client==1.5.2 pyasn1==0.1.7 pyasn1-modules==0.0.5 \
107             rsa uritemplate httplib2 ws4py pykka six pyexecjs jsonschema \
108             ciso8601 pycrypto backports.ssl_match_hostname llfuse==0.41.1 \
109             'pycurl<7.21.5')
110         PYTHON3_BACKPORTS=(docker-py six requests websocket-client)
111         ;;
112     ubuntu1204)
113         FORMAT=deb
114         PYTHON2_PACKAGE=python$PYTHON2_VERSION
115         PYTHON2_PKG_PREFIX=python
116         PYTHON3_PACKAGE=python$PYTHON3_VERSION
117         PYTHON3_PKG_PREFIX=python3
118         PYTHON_BACKPORTS=(python-gflags pyvcf google-api-python-client==1.4.2 \
119             oauth2client==1.5.2 pyasn1==0.1.7 pyasn1-modules==0.0.5 \
120             rsa uritemplate httplib2 ws4py pykka six pyexecjs jsonschema \
121             ciso8601 pycrypto backports.ssl_match_hostname llfuse==0.41.1 \
122             'pycurl<7.21.5')
123         PYTHON3_BACKPORTS=(docker-py six requests websocket-client)
124         ;;
125     ubuntu1404)
126         FORMAT=deb
127         PYTHON2_PACKAGE=python$PYTHON2_VERSION
128         PYTHON2_PKG_PREFIX=python
129         PYTHON3_PACKAGE=python$PYTHON3_VERSION
130         PYTHON3_PKG_PREFIX=python3
131         PYTHON_BACKPORTS=(pyasn1==0.1.7 pyvcf pyasn1-modules==0.0.5 llfuse==0.41.1 ciso8601 \
132             google-api-python-client==1.4.2 six uritemplate oauth2client==1.5.2 httplib2 \
133             rsa 'pycurl<7.21.5' backports.ssl_match_hostname)
134         PYTHON3_BACKPORTS=(docker-py requests websocket-client)
135         ;;
136     centos6)
137         FORMAT=rpm
138         PYTHON2_PACKAGE=$(rpm -qf "$(which python$PYTHON2_VERSION)" --queryformat '%{NAME}\n')
139         PYTHON2_PKG_PREFIX=$PYTHON2_PACKAGE
140         PYTHON3_PACKAGE=$(rpm -qf "$(which python$PYTHON3_VERSION)" --queryformat '%{NAME}\n')
141         PYTHON3_PKG_PREFIX=$PYTHON3_PACKAGE
142         PYTHON_BACKPORTS=(python-gflags pyvcf google-api-python-client==1.4.2 \
143             oauth2client==1.5.2 pyasn1==0.1.7 pyasn1-modules==0.0.5 \
144             rsa uritemplate httplib2 ws4py pykka six pyexecjs jsonschema \
145             ciso8601 pycrypto backports.ssl_match_hostname 'pycurl<7.21.5' \
146             python-daemon lockfile llfuse==0.41.1 'pbr<1.0')
147         PYTHON3_BACKPORTS=(docker-py six requests websocket-client)
148         export PYCURL_SSL_LIBRARY=nss
149         ;;
150     *)
151         echo -e "$0: Unknown target '$TARGET'.\n" >&2
152         exit 1
153         ;;
154 esac
155
156
157 if ! [[ -n "$WORKSPACE" ]]; then
158   echo >&2 "$helpmessage"
159   echo >&2
160   echo >&2 "Error: WORKSPACE environment variable not set"
161   echo >&2
162   exit 1
163 fi
164
165 # Test for fpm
166 fpm --version >/dev/null 2>&1
167
168 if [[ "$?" != 0 ]]; then
169   echo >&2 "$helpmessage"
170   echo >&2
171   echo >&2 "Error: fpm not found"
172   echo >&2
173   exit 1
174 fi
175
176 EASY_INSTALL2=$(find_easy_install -$PYTHON2_VERSION "")
177 EASY_INSTALL3=$(find_easy_install -$PYTHON3_VERSION 3)
178
179 RUN_BUILD_PACKAGES_PATH="`dirname \"$0\"`"
180 RUN_BUILD_PACKAGES_PATH="`( cd \"$RUN_BUILD_PACKAGES_PATH\" && pwd )`"  # absolutized and normalized
181 if [ -z "$RUN_BUILD_PACKAGES_PATH" ] ; then
182   # error; for some reason, the path is not accessible
183   # to the script (e.g. permissions re-evaled after suid)
184   exit 1  # fail
185 fi
186
187 debug_echo "$0 is running from $RUN_BUILD_PACKAGES_PATH"
188 debug_echo "Workspace is $WORKSPACE"
189
190 if [[ -f /etc/profile.d/rvm.sh ]]; then
191     source /etc/profile.d/rvm.sh
192     GEM="rvm-exec default gem"
193 else
194     GEM=gem
195 fi
196
197 # Make all files world-readable -- jenkins runs with umask 027, and has checked
198 # out our git tree here
199 chmod o+r "$WORKSPACE" -R
200
201 # More cleanup - make sure all executables that we'll package are 755
202 find -type d -name 'bin' |xargs -I {} find {} -type f |xargs -I {} chmod 755 {}
203
204 # Now fix our umask to something better suited to building and publishing
205 # gems and packages
206 umask 0022
207
208 debug_echo "umask is" `umask`
209
210 if [[ ! -d "$WORKSPACE/packages/$TARGET" ]]; then
211   mkdir -p $WORKSPACE/packages/$TARGET
212 fi
213
214 # Perl packages
215 debug_echo -e "\nPerl packages\n"
216
217 cd "$WORKSPACE/sdk/perl"
218
219 if [[ -e Makefile ]]; then
220   make realclean >"$STDOUT_IF_DEBUG"
221 fi
222 find -maxdepth 1 \( -name 'MANIFEST*' -or -name "libarvados-perl*.$FORMAT" \) \
223     -delete
224 rm -rf install
225
226 perl Makefile.PL INSTALL_BASE=install >"$STDOUT_IF_DEBUG" && \
227     make install INSTALLDIRS=perl >"$STDOUT_IF_DEBUG" && \
228     fpm_build install/lib/=/usr/share libarvados-perl \
229     "Curoverse, Inc." dir "$(version_from_git)" install/man/=/usr/share/man \
230     "$WORKSPACE/LICENSE-2.0.txt=/usr/share/doc/libarvados-perl/LICENSE-2.0.txt" && \
231     mv --no-clobber libarvados-perl*.$FORMAT "$WORKSPACE/packages/$TARGET/"
232
233 # Ruby gems
234 debug_echo -e "\nRuby gems\n"
235
236 FPM_GEM_PREFIX=$($GEM environment gemdir)
237
238 cd "$WORKSPACE/sdk/ruby"
239 handle_ruby_gem arvados
240
241 cd "$WORKSPACE/sdk/cli"
242 handle_ruby_gem arvados-cli
243
244 cd "$WORKSPACE/services/login-sync"
245 handle_ruby_gem arvados-login-sync
246
247 # Python packages
248 debug_echo -e "\nPython packages\n"
249
250 cd "$WORKSPACE/sdk/pam"
251 handle_python_package
252
253 cd "$WORKSPACE/sdk/python"
254 handle_python_package
255
256 cd "$WORKSPACE/sdk/cwl"
257 handle_python_package
258
259 cd "$WORKSPACE/services/fuse"
260 handle_python_package
261
262 cd "$WORKSPACE/services/nodemanager"
263 handle_python_package
264
265 # arvados-src
266 (
267     set -e
268
269     cd "$WORKSPACE"
270     COMMIT_HASH=$(format_last_commit_here "%H")
271
272     SRC_BUILD_DIR=$(mktemp -d)
273     # mktemp creates the directory with 0700 permissions by default
274     chmod 755 $SRC_BUILD_DIR
275     git clone $DASHQ_UNLESS_DEBUG "$WORKSPACE/.git" "$SRC_BUILD_DIR"
276     cd "$SRC_BUILD_DIR"
277
278     # go into detached-head state
279     git checkout $DASHQ_UNLESS_DEBUG "$COMMIT_HASH"
280     echo "$COMMIT_HASH" >git-commit.version
281
282     cd "$SRC_BUILD_DIR"
283     PKG_VERSION=$(version_from_git)
284     cd $WORKSPACE/packages/$TARGET
285     fpm_build $SRC_BUILD_DIR/=/usr/local/arvados/src arvados-src 'Curoverse, Inc.' 'dir' "$PKG_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"
286
287     rm -rf "$SRC_BUILD_DIR"
288 )
289
290 # Go binaries
291 export GOPATH=$(mktemp -d)
292 package_go_binary services/keepstore keepstore \
293     "Keep storage daemon, accessible to clients on the LAN"
294 package_go_binary services/keepproxy keepproxy \
295     "Make a Keep cluster accessible to clients that are not on the LAN"
296 package_go_binary services/keep-web keep-web \
297     "Static web hosting service for user data stored in Arvados Keep"
298 package_go_binary services/datamanager arvados-data-manager \
299     "Ensure block replication levels, report disk usage, and determine which blocks should be deleted when space is needed"
300 package_go_binary services/arv-git-httpd arvados-git-httpd \
301     "Provide authenticated http access to Arvados-hosted git repositories"
302 package_go_binary services/crunchstat crunchstat \
303     "Gather cpu/memory/network statistics of running Crunch jobs"
304 package_go_binary tools/keep-rsync keep-rsync \
305     "Copy all data from one set of Keep servers to another"
306
307 # The Python SDK
308 # Please resist the temptation to add --no-python-fix-name to the fpm call here
309 # (which would remove the python- prefix from the package name), because this
310 # package is a dependency of arvados-fuse, and fpm can not omit the python-
311 # prefix from only one of the dependencies of a package...  Maybe I could
312 # whip up a patch and send it upstream, but that will be for another day. Ward,
313 # 2014-05-15
314 cd $WORKSPACE/packages/$TARGET
315 rm -rf "$WORKSPACE/sdk/python/build"
316 fpm_build $WORKSPACE/sdk/python "${PYTHON2_PKG_PREFIX}-arvados-python-client" 'Curoverse, Inc.' 'python' "$(awk '($1 == "Version:"){print $2}' $WORKSPACE/sdk/python/arvados_python_client.egg-info/PKG-INFO)" "--url=https://arvados.org" "--description=The Arvados Python SDK" --deb-recommends=git
317
318 # cwl-runner
319 cd $WORKSPACE/packages/$TARGET
320 rm -rf "$WORKSPACE/sdk/cwl/build"
321 fpm_build $WORKSPACE/sdk/cwl "${PYTHON2_PKG_PREFIX}-arvados-cwl-runner" 'Curoverse, Inc.' 'python' "$(awk '($1 == "Version:"){print $2}' $WORKSPACE/sdk/cwl/arvados_cwl_runner.egg-info/PKG-INFO)" "--url=https://arvados.org" "--description=The Arvados CWL runner"
322
323 # The PAM module
324 if [[ $TARGET =~ debian|ubuntu ]]; then
325     cd $WORKSPACE/packages/$TARGET
326     rm -rf "$WORKSPACE/sdk/pam/build"
327     fpm_build $WORKSPACE/sdk/pam libpam-arvados 'Curoverse, Inc.' 'python' "$(awk '($1 == "Version:"){print $2}' $WORKSPACE/sdk/pam/arvados_pam.egg-info/PKG-INFO)" "--url=https://arvados.org" "--description=PAM module for authenticating shell logins using Arvados API tokens" --depends libpam-python
328 fi
329
330 # The FUSE driver
331 # Please see comment about --no-python-fix-name above; we stay consistent and do
332 # not omit the python- prefix first.
333 cd $WORKSPACE/packages/$TARGET
334 rm -rf "$WORKSPACE/services/fuse/build"
335 fpm_build $WORKSPACE/services/fuse "${PYTHON2_PKG_PREFIX}-arvados-fuse" 'Curoverse, Inc.' 'python' "$(awk '($1 == "Version:"){print $2}' $WORKSPACE/services/fuse/arvados_fuse.egg-info/PKG-INFO)" "--url=https://arvados.org" "--description=The Keep FUSE driver"
336
337 # The node manager
338 cd $WORKSPACE/packages/$TARGET
339 rm -rf "$WORKSPACE/services/nodemanager/build"
340 fpm_build $WORKSPACE/services/nodemanager arvados-node-manager 'Curoverse, Inc.' 'python' "$(awk '($1 == "Version:"){print $2}' $WORKSPACE/services/nodemanager/arvados_node_manager.egg-info/PKG-INFO)" "--url=https://arvados.org" "--description=The Arvados node manager"
341
342 # The Docker image cleaner
343 cd $WORKSPACE/packages/$TARGET
344 rm -rf "$WORKSPACE/services/dockercleaner/build"
345 fpm_build $WORKSPACE/services/dockercleaner arvados-docker-cleaner 'Curoverse, Inc.' 'python3' "$(awk '($1 == "Version:"){print $2}' $WORKSPACE/services/dockercleaner/arvados_docker_cleaner.egg-info/PKG-INFO)" "--url=https://arvados.org" "--description=The Arvados Docker image cleaner"
346
347 # Forked libcloud
348 LIBCLOUD_DIR=$(mktemp -d)
349 (
350     cd $LIBCLOUD_DIR
351     git clone $DASHQ_UNLESS_DEBUG https://github.com/curoverse/libcloud.git .
352     git checkout apache-libcloud-$LIBCLOUD_PIN
353     # libcloud is absurdly noisy without -q, so force -q here
354     OLD_DASHQ_UNLESS_DEBUG=$DASHQ_UNLESS_DEBUG
355     DASHQ_UNLESS_DEBUG=-q
356     handle_python_package
357     DASHQ_UNLESS_DEBUG=$OLD_DASHQ_UNLESS_DEBUG
358 )
359 fpm_build $LIBCLOUD_DIR "$PYTHON2_PKG_PREFIX"-apache-libcloud
360 rm -rf $LIBCLOUD_DIR
361
362 # Python 2 dependencies
363 declare -a PIP_DOWNLOAD_SWITCHES=(--no-deps)
364 # Add --no-use-wheel if this pip knows it.
365 pip wheel --help >/dev/null 2>&1
366 case "$?" in
367     0) PIP_DOWNLOAD_SWITCHES+=(--no-use-wheel) ;;
368     2) ;;
369     *) echo "WARNING: `pip wheel` test returned unknown exit code $?" ;;
370 esac
371
372 for deppkg in "${PYTHON_BACKPORTS[@]}"; do
373     outname=$(echo "$deppkg" | sed -e 's/^python-//' -e 's/[<=>].*//' -e 's/_/-/g' -e "s/^/${PYTHON2_PKG_PREFIX}-/")
374     case "$deppkg" in
375         httplib2|google-api-python-client)
376             # Work around 0640 permissions on some package files.
377             # See #7591 and #7991.
378             pyfpm_workdir=$(mktemp --tmpdir -d pyfpm-XXXXXX) && (
379                 set -e
380                 cd "$pyfpm_workdir"
381                 pip install "${PIP_DOWNLOAD_SWITCHES[@]}" --download . "$deppkg"
382                 tar -xf "$deppkg"-*.tar*
383                 cd "$deppkg"-*/
384                 "python$PYTHON2_VERSION" setup.py $DASHQ_UNLESS_DEBUG egg_info build
385                 chmod -R go+rX .
386                 set +e
387                 # --iteration 2 provides an upgrade for previously built
388                 # buggy packages.
389                 fpm_build . "$outname" "" python "" --iteration 2
390                 # The upload step uses the package timestamp to determine
391                 # whether it's new.  --no-clobber plays nice with that.
392                 mv --no-clobber "$outname"*.$FORMAT "$WORKSPACE/packages/$TARGET"
393             )
394             if [ 0 != "$?" ]; then
395                 echo "ERROR: $deppkg build process failed"
396                 EXITCODE=1
397             fi
398             if [ -n "$pyfpm_workdir" ]; then
399                 rm -rf "$pyfpm_workdir"
400             fi
401             ;;
402         *)
403             fpm_build "$deppkg" "$outname"
404             ;;
405     esac
406 done
407
408 # Python 3 dependencies
409 for deppkg in "${PYTHON3_BACKPORTS[@]}"; do
410     outname=$(echo "$deppkg" | sed -e 's/^python-//' -e 's/[<=>].*//' -e 's/_/-/g' -e "s/^/${PYTHON3_PKG_PREFIX}-/")
411     # The empty string is the vendor argument: these aren't Curoverse software.
412     fpm_build "$deppkg" "$outname" "" python3
413 done
414
415 # Build the API server package
416 handle_rails_package arvados-api-server "$WORKSPACE/services/api" \
417     "$WORKSPACE/agpl-3.0.txt" --url="https://arvados.org" \
418     --description="Arvados API server - Arvados is a free and open source platform for big data science." \
419     --license="GNU Affero General Public License, version 3.0"
420
421 # Build the workbench server package
422 (
423     set -e
424     cd "$WORKSPACE/apps/workbench"
425
426     # We need to bundle to be ready even when we build a package without vendor directory
427     # because asset compilation requires it.
428     bundle install --path vendor/bundle >"$STDOUT_IF_DEBUG"
429
430     # clear the tmp directory; the asset generation step will recreate tmp/cache/assets,
431     # and we want that in the package, so it's easier to not exclude the tmp directory
432     # from the package - empty it instead.
433     rm -rf tmp
434     mkdir tmp
435
436     # Set up application.yml and production.rb so that asset precompilation works
437     \cp config/application.yml.example config/application.yml -f
438     \cp config/environments/production.rb.example config/environments/production.rb -f
439     sed -i 's/secret_token: ~/secret_token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/' config/application.yml
440
441     RAILS_ENV=production RAILS_GROUPS=assets bundle exec rake assets:precompile >/dev/null
442
443     # Remove generated configuration files so they don't go in the package.
444     rm config/application.yml config/environments/production.rb
445 )
446
447 if [[ "$?" != "0" ]]; then
448   echo "ERROR: Asset precompilation failed"
449   EXITCODE=1
450 else
451   handle_rails_package arvados-workbench "$WORKSPACE/apps/workbench" \
452       "$WORKSPACE/agpl-3.0.txt" --url="https://arvados.org" \
453       --description="Arvados Workbench - Arvados is a free and open source platform for big data science." \
454       --license="GNU Affero General Public License, version 3.0"
455 fi
456
457 # clean up temporary GOPATH
458 rm -rf "$GOPATH"
459
460 exit $EXITCODE