Use virtualenv to get a newer setuptools that doesn't munge Python versions. No issue #
[arvados-dev.git] / jenkins / run-build-packages.sh
1 #!/bin/bash
2
3
4 read -rd "\000" helpmessage <<EOF
5 $(basename $0): Build Arvados packages and (optionally) upload them.
6
7 Syntax:
8         WORKSPACE=/path/to/arvados $(basename $0) [options]
9
10 Options:
11
12 --upload               Upload packages (default: false)
13 --scp-user USERNAME    Scp user for apt server (only required when --upload is specified)
14 --apt-server HOSTNAME  Apt server hostname (only required when --upload is specified)
15 --debug                Output debug information (default: false)
16
17 WORKSPACE=path         Path to the Arvados source tree to build packages from
18
19 EOF
20
21 EXITCODE=0
22 CALL_FREIGHT=0
23
24 DEBUG=0
25 UPLOAD=0
26
27 while [[ -n "$1" ]]
28 do
29     arg="$1"; shift
30     case "$arg" in
31         --help)
32             echo >&2 "$helpmessage"
33             echo >&2
34             exit 1
35             ;;
36         --scp-user)
37             APTUSER="$1"; shift
38             ;;
39         --apt-server)
40             APTSERVER="$1"; shift
41             ;;
42         --debug)
43             DEBUG=1
44             ;;
45         --upload)
46             UPLOAD=1
47             ;;
48         *)
49             echo >&2 "$0: Unrecognized option: '$arg'. Try: $0 --help"
50             exit 1
51             ;;
52     esac
53 done
54
55 # Sanity checks
56 if [[ "$UPLOAD" != '0' && ("$APTUSER" == '' || "$APTSERVER" == '') ]]; then
57   echo >&2 "$helpmessage"
58   echo >&2
59   echo >&2 "Error: please specify --scp-user and --apt-server if --upload is set"
60   echo >&2
61   exit 1
62 fi
63
64 # Sanity check
65 if ! [[ -n "$WORKSPACE" ]]; then
66   echo >&2 "$helpmessage"
67   echo >&2
68   echo >&2 "Error: WORKSPACE environment variable not set"
69   echo >&2
70   exit 1
71 fi
72
73 # Test for fpm
74 fpm --version >/dev/null 2>&1
75
76 if [[ "$?" != 0 ]]; then
77   echo >&2 "$helpmessage"
78   echo >&2
79   echo >&2 "Error: fpm not found"
80   echo >&2
81   exit 1
82 fi
83
84 if [[ "$DEBUG" != 0 ]]; then
85   echo "Workspace is $WORKSPACE"
86 fi
87
88 # We need a recent setuptools. Older ones replace + with - in our
89 # version numbers, making them non-PEP-440-compliant and
90 # non-uploadable.
91 VENV=$(mktemp -d)
92 echo "Using virtualenv $VENV"
93 virtualenv "$VENV"
94 . "$VENV/bin/activate"
95
96 version_from_git() {
97   # Generates a version number from the git log for the current working
98   # directory, and writes it to stdout.
99   local git_ts git_hash
100   declare $(TZ=UTC git log -n1 --first-parent --max-count=1 \
101       --format=format:"git_ts=%ct git_hash=%h" .)
102   echo "0.1.$(date -ud "@$git_ts" +%Y%m%d%H%M%S).$git_hash"
103 }
104
105 timestamp_from_git() {
106   # Generates a version number from the git log for the current working
107   # directory, and writes it to stdout.
108   local git_ts git_hash
109   declare $(TZ=UTC git log -n1 --first-parent --max-count=1 \
110       --format=format:"git_ts=%ct git_hash=%h" .)
111   echo "$git_ts"
112 }
113
114 handle_python_package () {
115   # This function assumes the current working directory is the python package directory
116   if [[ "$UPLOAD" != 0 ]]; then
117     # Make sure only to use sdist - that's the only format pip can deal with (sigh)
118     if [[ "$DEBUG" != 0 ]]; then
119       python setup.py sdist upload
120     else
121       python setup.py -q sdist upload
122     fi
123   else
124     # Make sure only to use sdist - that's the only format pip can deal with (sigh)
125     if [[ "$DEBUG" != 0 ]]; then
126       python setup.py sdist
127     else
128       python setup.py -q sdist
129     fi
130   fi
131 }
132
133 # Build debs for everything
134 build_and_scp_deb () {
135   # The package source.  Depending on the source type, this can be a
136   # path, or the name of the package in an upstream repository (e.g.,
137   # pip).
138   PACKAGE=$1
139   shift
140   # The name of the package to build.  Defaults to $PACKAGE.
141   PACKAGE_NAME=$1
142   shift
143   # Optional: the vendor of the package.  Should be "Curoverse, Inc." for
144   # packages of our own software.  Passed to fpm --vendor.
145   VENDOR=$1
146   shift
147   # The type of source package.  Passed to fpm -s.  Default "python".
148   PACKAGE_TYPE=$1
149   shift
150   # Optional: the package version number.  Passed to fpm -v.
151   VERSION=$1
152   shift
153
154   if [[ "$PACKAGE_NAME" == "" ]]; then
155     PACKAGE_NAME=$PACKAGE
156   fi
157
158   if [[ "$PACKAGE_TYPE" == "" ]]; then
159     PACKAGE_TYPE='python'
160   fi
161
162   declare -a COMMAND_ARR=("fpm" "--maintainer=Ward Vandewege <ward@curoverse.com>" "-s" "$PACKAGE_TYPE" "-t" "deb" "-x" "usr/local/lib/python2.7/dist-packages/tests")
163
164   if [[ "$PACKAGE_NAME" != "$PACKAGE" ]]; then
165     COMMAND_ARR+=('-n' "$PACKAGE_NAME")
166   fi
167
168   if [[ "$VENDOR" != "" ]]; then
169     COMMAND_ARR+=('--vendor' "$VENDOR")
170   fi
171
172   if [[ "$VERSION" != "" ]]; then
173     COMMAND_ARR+=('-v' "$VERSION")
174   fi
175
176   # Append remaining function arguments directly to fpm's command line.
177   for i; do
178     COMMAND_ARR+=("$i")
179   done
180
181   COMMAND_ARR+=("$PACKAGE")
182
183   if [[ "$DEBUG" != 0 ]]; then
184     echo
185     echo "${COMMAND_ARR[@]}"
186     echo
187   fi
188
189   FPM_RESULTS=$("${COMMAND_ARR[@]}")
190   FPM_EXIT_CODE=$?
191
192   FPM_PACKAGE_NAME=''
193   if [[ $FPM_RESULTS =~ ([A-Za-z0-9_\-.]*\.deb) ]]; then
194     FPM_PACKAGE_NAME=${BASH_REMATCH[1]}
195   fi
196
197   if [[ "$FPM_PACKAGE_NAME" == "" ]]; then
198     EXITCODE=1
199     echo "Error: $PACKAGE: Unable to figure out package name from fpm results:"
200     echo
201     echo $FPM_RESULTS
202     echo
203   else
204     if [[ ! $FPM_RESULTS =~ "File already exists" ]]; then
205       if [[ "$FPM_EXIT_CODE" != "0" ]]; then
206         echo "Error building debian package for $1:\n $FPM_RESULTS"
207       else
208         if [[ "$UPLOAD" != 0 ]]; then
209           scp -P2222 $FPM_PACKAGE_NAME $APTUSER@$APTSERVER:tmp/
210           CALL_FREIGHT=1
211         fi
212       fi
213     else
214       echo "Debian package $FPM_PACKAGE_NAME exists, not rebuilding"
215     fi
216   fi
217 }
218
219 if [[ -f /etc/profile.d/rvm.sh ]]; then
220   source /etc/profile.d/rvm.sh
221 fi
222
223 # Make all files world-readable -- jenkins runs with umask 027, and has checked
224 # out our git tree here
225 chmod o+r "$WORKSPACE" -R
226
227 # More cleanup - make sure all executables that we'll package are 755
228 find -type d -name 'bin' |xargs -I {} find {} -type f |xargs -I {} chmod 755 {}
229
230 # Now fix our umask to something better suited to building and publishing
231 # gems and packages
232 umask 0022
233
234 if [[ "$DEBUG" != 0 ]]; then
235   echo "umask is" `umask`
236 fi
237
238 # Perl packages
239 if [[ "$DEBUG" != 0 ]]; then
240   echo -e "\nPerl packages\n"
241 fi
242
243 if [[ "$DEBUG" != 0 ]]; then
244   PERL_OUT=/dev/stdout
245 else
246   PERL_OUT=/dev/null
247 fi
248
249 cd "$WORKSPACE/sdk/perl"
250
251 if [[ -e Makefile ]]; then
252   make realclean >"$PERL_OUT"
253 fi
254 find -maxdepth 1 \( -name 'MANIFEST*' -or -name 'libarvados-perl_*.deb' \) \
255     -delete
256 rm -rf install
257
258 perl Makefile.PL >"$PERL_OUT" && \
259     make install PREFIX=install INSTALLDIRS=perl >"$PERL_OUT" && \
260     build_and_scp_deb install/=/usr libarvados-perl "Curoverse, Inc." dir \
261       "$(version_from_git)"
262
263 # Ruby gems
264 if [[ "$DEBUG" != 0 ]]; then
265   echo
266   echo "Ruby gems"
267   echo
268 fi
269
270 if type rvm-exec 2>/dev/null; then
271   FPM_GEM_PREFIX=$(rvm-exec system gem environment gemdir)
272 else
273   FPM_GEM_PREFIX=$(gem environment gemdir)
274 fi
275
276 cd "$WORKSPACE"
277 cd sdk/ruby
278 # clean up old packages
279 find -maxdepth 1 \( -name 'arvados-*.gem' -or -name 'rubygem-arvados_*.deb' \) \
280     -delete
281
282 if [[ "$DEBUG" != 0 ]]; then
283   gem build arvados.gemspec
284 else
285   # -q appears to be broken in gem version 2.2.2
286   gem build arvados.gemspec -q >/dev/null
287 fi
288
289 if [[ "$UPLOAD" != 0 ]]; then
290   # publish new gem
291   gem push arvados-*gem
292 fi
293
294 build_and_scp_deb arvados-*.gem "" "Curoverse, Inc." gem "" \
295     --prefix "$FPM_GEM_PREFIX"
296
297 # Build arvados-cli GEM
298 cd "$WORKSPACE"
299 cd sdk/cli
300 # clean up old gems
301 rm -f arvados-cli*gem
302
303 if [[ "$DEBUG" != 0 ]]; then
304   gem build arvados-cli.gemspec
305 else
306   # -q appears to be broken in gem version 2.2.2
307   gem build arvados-cli.gemspec -q >/dev/null
308 fi
309
310 if [[ "$UPLOAD" != 0 ]]; then
311   # publish new gem
312   gem push arvados-cli*gem
313 fi
314
315 # Python packages
316 if [[ "$DEBUG" != 0 ]]; then
317   echo
318   echo "Python packages"
319   echo
320 fi
321
322 cd "$WORKSPACE"
323
324 cd sdk/python
325 handle_python_package
326
327 cd ../../services/fuse
328 handle_python_package
329
330 cd ../../services/nodemanager
331 handle_python_package
332
333 if [[ ! -d "$WORKSPACE/debs" ]]; then
334   mkdir -p $WORKSPACE/debs
335 fi
336
337 # Arvados-src
338 # We use $WORKSPACE/src-build-dir as the clean directory from which to build the src package
339 if [[ ! -d "$WORKSPACE/src-build-dir" ]]; then
340   mkdir "$WORKSPACE/src-build-dir"
341   cd "$WORKSPACE"
342   if [[ "$DEBUG" != 0 ]]; then
343     git clone https://github.com/curoverse/arvados.git src-build-dir
344   else
345     git clone -q https://github.com/curoverse/arvados.git src-build-dir
346   fi
347 fi
348
349 cd "$WORKSPACE/src-build-dir"
350 # just in case, check out master
351 if [[ "$DEBUG" != 0 ]]; then
352   git checkout master
353   git pull
354   # go into detached-head state
355   git checkout `git log --format=format:%h -n1 .`
356 else
357   git checkout -q master
358   git pull -q
359   # go into detached-head state
360   git checkout -q `git log --format=format:%h -n1 .`
361 fi
362
363 # Build arvados src deb package
364 cd "$WORKSPACE"
365 PKG_VERSION=$(version_from_git)
366 cd $WORKSPACE/debs
367 build_and_scp_deb $WORKSPACE/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"
368
369 # clean up, check out master and step away from detached-head state
370 cd "$WORKSPACE/src-build-dir"
371 if [[ "$DEBUG" != 0 ]]; then
372   git checkout master
373 else
374   git checkout -q master
375 fi
376
377 # Keep
378 export GOPATH=$(mktemp -d)
379 mkdir -p "$GOPATH/src/git.curoverse.com"
380 ln -sfn "$WORKSPACE" "$GOPATH/src/git.curoverse.com/arvados.git"
381
382 # keepstore
383 cd "$GOPATH/src/git.curoverse.com/arvados.git/services/keepstore"
384 PKG_VERSION=$(version_from_git)
385 go get "git.curoverse.com/arvados.git/services/keepstore"
386 cd $WORKSPACE/debs
387 build_and_scp_deb $GOPATH/bin/keepstore=/usr/bin/keepstore keepstore 'Curoverse, Inc.' 'dir' "$PKG_VERSION" "--url=https://arvados.org" "--license=GNU Affero General Public License, version 3.0" "--description=Keepstore is the Keep storage daemon, accessible to clients on the LAN"
388
389 # keepproxy
390 cd "$GOPATH/src/git.curoverse.com/arvados.git/sdk/go"
391 GO_SDK_VERSION=$(version_from_git)
392 GO_SDK_TIMESTAMP=$(timestamp_from_git)
393
394 cd "$GOPATH/src/git.curoverse.com/arvados.git/services/keepproxy"
395 KEEPPROXY_VERSION=$(version_from_git)
396 KEEPPROXY_TIMESTAMP=$(timestamp_from_git)
397
398 if [[ "$GO_SDK_TIMESTAMP" -gt "$KEEPPROXY_TIMESTAMP" ]]; then
399   PKG_VERSION=$GO_SDK_VERSION
400 else
401   PKG_VERSION=$KEEPPROXY_VERSION
402 fi
403
404 go get "git.curoverse.com/arvados.git/services/keepproxy"
405 cd $WORKSPACE/debs
406 build_and_scp_deb $GOPATH/bin/keepproxy=/usr/bin/keepproxy keepproxy 'Curoverse, Inc.' 'dir' "$PKG_VERSION" "--url=https://arvados.org" "--license=GNU Affero General Public License, version 3.0" "--description=Keepproxy makes a Keep cluster accessible to clients that are not on the LAN"
407
408 # crunchstat
409 cd "$GOPATH/src/git.curoverse.com/arvados.git/services/crunchstat"
410 PKG_VERSION=$(version_from_git)
411 go get "git.curoverse.com/arvados.git/services/crunchstat"
412 cd $WORKSPACE/debs
413 build_and_scp_deb $GOPATH/bin/crunchstat=/usr/bin/crunchstat crunchstat 'Curoverse, Inc.' 'dir' "$PKG_VERSION" "--url=https://arvados.org" "--license=GNU Affero General Public License, version 3.0" "--description=Crunchstat gathers cpu/memory/network statistics of running Crunch jobs"
414
415 # The Python SDK
416 # Please resist the temptation to add --no-python-fix-name to the fpm call here
417 # (which would remove the python- prefix from the package name), because this
418 # package is a dependency of arvados-fuse, and fpm can not omit the python-
419 # prefix from only one of the dependencies of a package...  Maybe I could
420 # whip up a patch and send it upstream, but that will be for another day. Ward,
421 # 2014-05-15
422 cd $WORKSPACE/debs
423 # Python version numbering is obscure. Strip dashes and replace them with dots
424 # to match our other version numbers. Cf. commit 4afcb8c, compliance with PEP-440.
425 build_and_scp_deb $WORKSPACE/sdk/python python-arvados-python-client 'Curoverse, Inc.' 'python' "$(awk '($1 == "Version:"){ gsub(/-/,".",$2); print $2 }' $WORKSPACE/sdk/python/arvados_python_client.egg-info/PKG-INFO)" "--url=https://arvados.org" "--description=The Arvados Python SDK"
426
427 # The FUSE driver
428 # Please seem comment about --no-python-fix-name above; we stay consistent and do
429 # not omit the python- prefix first.
430 cd $WORKSPACE/debs
431 # Python version numbering is obscure. Strip dashes and replace them with dots
432 # to match our other version numbers. Cf. commit 4afcb8c, compliance with PEP-440.
433 build_and_scp_deb $WORKSPACE/services/fuse python-arvados-fuse 'Curoverse, Inc.' 'python' "$(awk '($1 == "Version:"){ gsub(/-/,".",$2); print $2 }' $WORKSPACE/services/fuse/arvados_fuse.egg-info/PKG-INFO)" "--url=https://arvados.org" "--description=The Keep FUSE driver"
434
435 # The node manager
436 cd $WORKSPACE/debs
437 # Python version numbering is obscure. Strip dashes and replace them with dots
438 # to match our other version numbers. Cf. commit 4afcb8c, compliance with PEP-440.
439 build_and_scp_deb $WORKSPACE/services/nodemanager arvados-node-manager 'Curoverse, Inc.' 'python' "$(awk '($1 == "Version:"){ gsub(/-/,".",$2); print $2}' $WORKSPACE/services/nodemanager/arvados_node_manager.egg-info/PKG-INFO)" "--url=https://arvados.org" "--description=The Arvados node manager"
440
441 # A few dependencies
442 for deppkg in python-gflags pyvcf google-api-python-client oauth2client \
443       pyasn1 pyasn1-modules rsa uritemplate httplib2 ws4py virtualenv \
444       pykka apache-libcloud requests six pyexecjs jsonschema; do
445     build_and_scp_deb "$deppkg"
446 done
447
448 # cwltool from common-workflow-language. We use this in arv-run-pipeline-instance.
449 # We use $WORKSPACE/common-workflow-language as the clean directory from which to build the cwltool package
450 if [[ ! -d "$WORKSPACE/common-workflow-language" ]]; then
451   mkdir "$WORKSPACE/common-workflow-language"
452   cd "$WORKSPACE"
453   if [[ "$DEBUG" != 0 ]]; then
454     git clone https://github.com/common-workflow-language/common-workflow-language.git common-workflow-language
455   else
456     git clone -q https://github.com/common-workflow-language/common-workflow-language.git common-workflow-language
457   fi
458 fi
459
460 cd "$WORKSPACE/common-workflow-language"
461 if [[ "$DEBUG" != 0 ]]; then
462   git checkout master
463   git pull
464 else
465   git checkout -q master
466   git pull -q
467 fi
468
469 cd reference
470 handle_python_package
471 CWLTOOL_VERSION=`git log --first-parent --max-count=1 --format='format:0.1.%ct.%h'`
472
473 # Build cwltool package
474 cd $WORKSPACE/debs
475 # Python version numbering is obscure. Strip dashes and replace them with dots
476 # to match our other version numbers. Cf. commit 4afcb8c, compliance with PEP-440.
477 build_and_scp_deb $WORKSPACE/common-workflow-language/reference cwltool 'Common Workflow Language Working Group' 'python' "$(awk '($1 == "Version:"){ gsub(/-/,".",$2); print $2 }' $WORKSPACE/common-workflow-language/reference/cwltool.egg-info/PKG-INFO)"
478
479 # Finally, publish the packages, if necessary
480 if [[ "$UPLOAD" != 0 && "$CALL_FREIGHT" != 0 ]]; then
481   ssh -p2222 $APTUSER@$APTSERVER -t "cd tmp && ls -laF *deb && freight add *deb apt/wheezy && freight cache && rm -f *deb"
482 else
483   if [[ "$UPLOAD" != 0 ]]; then
484     echo "No new packages generated. No freight run necessary."
485   fi
486 fi
487
488 # clean up temporary GOPATH and VENV
489 rm -rf "$GOPATH"
490 rm -rf "$VENV"
491
492 exit $EXITCODE