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