5277: Build .deb for new ciso8601 dependency.
[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 version_from_git() {
89   # Generates a version number from the git log for the current working
90   # directory, and writes it to stdout.
91   local git_ts git_hash
92   declare $(TZ=UTC git log -n1 --first-parent --max-count=1 \
93       --format=format:"git_ts=%ct git_hash=%h" .)
94   echo "0.1.$(date -ud "@$git_ts" +%Y%m%d%H%M%S).$git_hash"
95 }
96
97 timestamp_from_git() {
98   # Generates a version number from the git log for the current working
99   # directory, and writes it to stdout.
100   local git_ts git_hash
101   declare $(TZ=UTC git log -n1 --first-parent --max-count=1 \
102       --format=format:"git_ts=%ct git_hash=%h" .)
103   echo "$git_ts"
104 }
105
106 handle_python_package () {
107   # This function assumes the current working directory is the python package directory
108   if [[ "$UPLOAD" != 0 ]]; then
109     # Make sure only to use sdist - that's the only format pip can deal with (sigh)
110     if [[ "$DEBUG" != 0 ]]; then
111       python setup.py sdist upload
112     else
113       python setup.py -q sdist upload
114     fi
115   else
116     # Make sure only to use sdist - that's the only format pip can deal with (sigh)
117     if [[ "$DEBUG" != 0 ]]; then
118       python setup.py sdist
119     else
120       python setup.py -q sdist
121     fi
122   fi
123 }
124
125 # Build debs for everything
126 build_and_scp_deb () {
127   # The package source.  Depending on the source type, this can be a
128   # path, or the name of the package in an upstream repository (e.g.,
129   # pip).
130   PACKAGE=$1
131   shift
132   # The name of the package to build.  Defaults to $PACKAGE.
133   PACKAGE_NAME=$1
134   shift
135   # Optional: the vendor of the package.  Should be "Curoverse, Inc." for
136   # packages of our own software.  Passed to fpm --vendor.
137   VENDOR=$1
138   shift
139   # The type of source package.  Passed to fpm -s.  Default "python".
140   PACKAGE_TYPE=$1
141   shift
142   # Optional: the package version number.  Passed to fpm -v.
143   VERSION=$1
144   shift
145
146   if [[ "$PACKAGE_NAME" == "" ]]; then
147     PACKAGE_NAME=$PACKAGE
148   fi
149
150   if [[ "$PACKAGE_TYPE" == "" ]]; then
151     PACKAGE_TYPE='python'
152   fi
153
154   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")
155
156   if [[ "$PACKAGE_NAME" != "$PACKAGE" ]]; then
157     COMMAND_ARR+=('-n' "$PACKAGE_NAME")
158   fi
159
160   if [[ "$VENDOR" != "" ]]; then
161     COMMAND_ARR+=('--vendor' "$VENDOR")
162   fi
163
164   if [[ "$VERSION" != "" ]]; then
165     COMMAND_ARR+=('-v' "$VERSION")
166   fi
167
168   # Append remaining function arguments directly to fpm's command line.
169   for i; do
170     COMMAND_ARR+=("$i")
171   done
172
173   COMMAND_ARR+=("$PACKAGE")
174
175   if [[ "$DEBUG" != 0 ]]; then
176     echo
177     echo "${COMMAND_ARR[@]}"
178     echo
179   fi
180
181   FPM_RESULTS=$("${COMMAND_ARR[@]}")
182   FPM_EXIT_CODE=$?
183
184   FPM_PACKAGE_NAME=''
185   if [[ $FPM_RESULTS =~ ([A-Za-z0-9_\-.]*\.deb) ]]; then
186     FPM_PACKAGE_NAME=${BASH_REMATCH[1]}
187   fi
188
189   if [[ "$FPM_PACKAGE_NAME" == "" ]]; then
190     EXITCODE=1
191     echo "Error: $PACKAGE: Unable to figure out package name from fpm results:"
192     echo
193     echo $FPM_RESULTS
194     echo
195   else
196     if [[ ! $FPM_RESULTS =~ "File already exists" ]]; then
197       if [[ "$FPM_EXIT_CODE" != "0" ]]; then
198         echo "Error building debian package for $1:\n $FPM_RESULTS"
199       else
200         if [[ "$UPLOAD" != 0 ]]; then
201           scp -P2222 $FPM_PACKAGE_NAME $APTUSER@$APTSERVER:tmp/
202           CALL_FREIGHT=1
203         fi
204       fi
205     else
206       echo "Debian package $FPM_PACKAGE_NAME exists, not rebuilding"
207     fi
208   fi
209 }
210
211 if [[ -f /etc/profile.d/rvm.sh ]]; then
212   source /etc/profile.d/rvm.sh
213 fi
214
215 # Make all files world-readable -- jenkins runs with umask 027, and has checked
216 # out our git tree here
217 chmod o+r "$WORKSPACE" -R
218
219 # More cleanup - make sure all executables that we'll package are 755
220 find -type d -name 'bin' |xargs -I {} find {} -type f |xargs -I {} chmod 755 {}
221
222 # Now fix our umask to something better suited to building and publishing
223 # gems and packages
224 umask 0022
225
226 if [[ "$DEBUG" != 0 ]]; then
227   echo "umask is" `umask`
228 fi
229
230 # Perl packages
231 if [[ "$DEBUG" != 0 ]]; then
232   echo -e "\nPerl packages\n"
233 fi
234
235 if [[ "$DEBUG" != 0 ]]; then
236   PERL_OUT=/dev/stdout
237 else
238   PERL_OUT=/dev/null
239 fi
240
241 cd "$WORKSPACE/sdk/perl"
242
243 if [[ -e Makefile ]]; then
244   make realclean >"$PERL_OUT"
245 fi
246 find -maxdepth 1 \( -name 'MANIFEST*' -or -name 'libarvados-perl_*.deb' \) \
247     -delete
248 rm -rf install
249
250 perl Makefile.PL >"$PERL_OUT" && \
251     make install PREFIX=install INSTALLDIRS=perl >"$PERL_OUT" && \
252     build_and_scp_deb install/=/usr libarvados-perl "Curoverse, Inc." dir \
253       "$(version_from_git)"
254
255 # Ruby gems
256 if [[ "$DEBUG" != 0 ]]; then
257   echo
258   echo "Ruby gems"
259   echo
260 fi
261
262 if type rvm-exec 2>/dev/null; then
263   FPM_GEM_PREFIX=$(rvm-exec system gem environment gemdir)
264 else
265   FPM_GEM_PREFIX=$(gem environment gemdir)
266 fi
267
268 cd "$WORKSPACE"
269 cd sdk/ruby
270 # clean up old packages
271 find -maxdepth 1 \( -name 'arvados-*.gem' -or -name 'rubygem-arvados_*.deb' \) \
272     -delete
273
274 if [[ "$DEBUG" != 0 ]]; then
275   gem build arvados.gemspec
276 else
277   # -q appears to be broken in gem version 2.2.2
278   gem build arvados.gemspec -q >/dev/null
279 fi
280
281 if [[ "$UPLOAD" != 0 ]]; then
282   # publish new gem
283   gem push arvados-*gem
284 fi
285
286 build_and_scp_deb arvados-*.gem "" "Curoverse, Inc." gem "" \
287     --prefix "$FPM_GEM_PREFIX"
288
289 # Build arvados-cli GEM
290 cd "$WORKSPACE"
291 cd sdk/cli
292 # clean up old gems
293 rm -f arvados-cli*gem
294
295 if [[ "$DEBUG" != 0 ]]; then
296   gem build arvados-cli.gemspec
297 else
298   # -q appears to be broken in gem version 2.2.2
299   gem build arvados-cli.gemspec -q >/dev/null
300 fi
301
302 if [[ "$UPLOAD" != 0 ]]; then
303   # publish new gem
304   gem push arvados-cli*gem
305 fi
306
307 # Python packages
308 if [[ "$DEBUG" != 0 ]]; then
309   echo
310   echo "Python packages"
311   echo
312 fi
313
314 cd "$WORKSPACE"
315
316 cd sdk/python
317 handle_python_package
318
319 cd ../../services/fuse
320 handle_python_package
321
322 cd ../../services/nodemanager
323 handle_python_package
324
325 if [[ ! -d "$WORKSPACE/debs" ]]; then
326   mkdir -p $WORKSPACE/debs
327 fi
328
329 # Arvados-src
330 # We use $WORKSPACE/src-build-dir as the clean directory from which to build the src package
331 if [[ ! -d "$WORKSPACE/src-build-dir" ]]; then
332   mkdir "$WORKSPACE/src-build-dir"
333   cd "$WORKSPACE"
334   if [[ "$DEBUG" != 0 ]]; then
335     git clone https://github.com/curoverse/arvados.git src-build-dir
336   else
337     git clone -q https://github.com/curoverse/arvados.git src-build-dir
338   fi
339 fi
340
341 cd "$WORKSPACE/src-build-dir"
342 # just in case, check out master
343 if [[ "$DEBUG" != 0 ]]; then
344   git checkout master
345   git pull
346   # go into detached-head state
347   git checkout `git log --format=format:%h -n1 .`
348 else
349   git checkout -q master
350   git pull -q
351   # go into detached-head state
352   git checkout -q `git log --format=format:%h -n1 .`
353 fi
354
355 # Build arvados src deb package
356 cd "$WORKSPACE"
357 PKG_VERSION=$(version_from_git)
358 cd $WORKSPACE/debs
359 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"
360
361 # clean up, check out master and step away from detached-head state
362 cd "$WORKSPACE/src-build-dir"
363 if [[ "$DEBUG" != 0 ]]; then
364   git checkout master
365 else
366   git checkout -q master
367 fi
368
369 # Keep
370 export GOPATH=$(mktemp -d)
371 mkdir -p "$GOPATH/src/git.curoverse.com"
372 ln -sfn "$WORKSPACE" "$GOPATH/src/git.curoverse.com/arvados.git"
373
374 # keepstore
375 cd "$GOPATH/src/git.curoverse.com/arvados.git/services/keepstore"
376 PKG_VERSION=$(version_from_git)
377 go get "git.curoverse.com/arvados.git/services/keepstore"
378 cd $WORKSPACE/debs
379 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"
380
381 # keepproxy
382 cd "$GOPATH/src/git.curoverse.com/arvados.git/sdk/go"
383 GO_SDK_VERSION=$(version_from_git)
384 GO_SDK_TIMESTAMP=$(timestamp_from_git)
385
386 cd "$GOPATH/src/git.curoverse.com/arvados.git/services/keepproxy"
387 KEEPPROXY_VERSION=$(version_from_git)
388 KEEPPROXY_TIMESTAMP=$(timestamp_from_git)
389
390 if [[ "$GO_SDK_TIMESTAMP" -gt "$KEEPPROXY_TIMESTAMP" ]]; then
391   PKG_VERSION=$GO_SDK_VERSION
392 else
393   PKG_VERSION=$KEEPPROXY_VERSION
394 fi
395
396 go get "git.curoverse.com/arvados.git/services/keepproxy"
397 cd $WORKSPACE/debs
398 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"
399
400 # crunchstat
401 cd "$GOPATH/src/git.curoverse.com/arvados.git/services/crunchstat"
402 PKG_VERSION=$(version_from_git)
403 go get "git.curoverse.com/arvados.git/services/crunchstat"
404 cd $WORKSPACE/debs
405 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"
406
407 # The Python SDK
408 # Please resist the temptation to add --no-python-fix-name to the fpm call here
409 # (which would remove the python- prefix from the package name), because this
410 # package is a dependency of arvados-fuse, and fpm can not omit the python-
411 # prefix from only one of the dependencies of a package...  Maybe I could
412 # whip up a patch and send it upstream, but that will be for another day. Ward,
413 # 2014-05-15
414 cd $WORKSPACE/debs
415 # Python version numbering is obscure. Strip dashes and replace them with dots
416 # to match our other version numbers. Cf. commit 4afcb8c, compliance with PEP-440.
417 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"
418
419 # The FUSE driver
420 # Please seem comment about --no-python-fix-name above; we stay consistent and do
421 # not omit the python- prefix first.
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/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"
426
427 # The node manager
428 cd $WORKSPACE/debs
429 # Python version numbering is obscure. Strip dashes and replace them with dots
430 # to match our other version numbers. Cf. commit 4afcb8c, compliance with PEP-440.
431 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"
432
433 # A few dependencies
434 for deppkg in python-gflags pyvcf google-api-python-client oauth2client \
435       pyasn1 pyasn1-modules rsa uritemplate httplib2 ws4py virtualenv \
436       pykka apache-libcloud requests six pyexecjs jsonschema ciso8601; do
437     build_and_scp_deb "$deppkg"
438 done
439
440 # cwltool from common-workflow-language. We use this in arv-run-pipeline-instance.
441 # We use $WORKSPACE/common-workflow-language as the clean directory from which to build the cwltool package
442 if [[ ! -d "$WORKSPACE/common-workflow-language" ]]; then
443   mkdir "$WORKSPACE/common-workflow-language"
444   cd "$WORKSPACE"
445   if [[ "$DEBUG" != 0 ]]; then
446     git clone https://github.com/common-workflow-language/common-workflow-language.git common-workflow-language
447   else
448     git clone -q https://github.com/common-workflow-language/common-workflow-language.git common-workflow-language
449   fi
450 fi
451
452 cd "$WORKSPACE/common-workflow-language"
453 if [[ "$DEBUG" != 0 ]]; then
454   git checkout master
455   git pull
456 else
457   git checkout -q master
458   git pull -q
459 fi
460
461 cd reference
462 handle_python_package
463 CWLTOOL_VERSION=`git log --first-parent --max-count=1 --format='format:0.1.%ct.%h'`
464
465 # Build cwltool package
466 cd $WORKSPACE/debs
467 # Python version numbering is obscure. Strip dashes and replace them with dots
468 # to match our other version numbers. Cf. commit 4afcb8c, compliance with PEP-440.
469 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)"
470
471 # Finally, publish the packages, if necessary
472 if [[ "$UPLOAD" != 0 && "$CALL_FREIGHT" != 0 ]]; then
473   ssh -p2222 $APTUSER@$APTSERVER -t "cd tmp && ls -laF *deb && freight add *deb apt/wheezy && freight cache && rm -f *deb"
474 else
475   if [[ "$UPLOAD" != 0 ]]; then
476     echo "No new packages generated. No freight run necessary."
477   fi
478 fi
479
480 # clean up temporary GOPATH
481 rm -rf "$GOPATH"
482
483 exit $EXITCODE