3021: Leave an API server running while test suites run.
[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 source /etc/profile.d/rvm.sh
212
213 # Make all files world-readable -- jenkins runs with umask 027, and has checked
214 # out our git tree here
215 chmod o+r "$WORKSPACE" -R
216
217 # More cleanup - make sure all executables that we'll package are 755
218 find -type d -name 'bin' |xargs -I {} find {} -type f |xargs -I {} chmod 755 {}
219
220 # Now fix our umask to something better suited to building and publishing
221 # gems and packages
222 umask 0022
223
224 if [[ "$DEBUG" != 0 ]]; then
225   echo "umask is" `umask`
226 fi
227
228 # Perl packages
229 if [[ "$DEBUG" != 0 ]]; then
230   echo -e "\nPerl packages\n"
231 fi
232
233 if [[ "$DEBUG" != 0 ]]; then
234   PERL_OUT=/dev/stdout
235 else
236   PERL_OUT=/dev/null
237 fi
238
239 cd "$WORKSPACE/sdk/perl"
240
241 if [[ -e Makefile ]]; then
242   make realclean >"$PERL_OUT"
243 fi
244 find -maxdepth 1 \( -name 'MANIFEST*' -or -name 'libarvados-perl_*.deb' \) \
245     -delete
246 rm -rf install
247
248 perl Makefile.PL >"$PERL_OUT" && \
249     make install PREFIX=install INSTALLDIRS=perl >"$PERL_OUT" && \
250     build_and_scp_deb install/=/usr libarvados-perl "Curoverse, Inc." dir \
251       "$(version_from_git)"
252
253 # Ruby gems
254 if [[ "$DEBUG" != 0 ]]; then
255   echo
256   echo "Ruby gems"
257   echo
258 fi
259
260 if type rvm-exec 2>/dev/null; then
261   FPM_GEM_PREFIX=$(rvm-exec system gem environment gemdir)
262 else
263   FPM_GEM_PREFIX=$(gem environment gemdir)
264 fi
265
266 cd "$WORKSPACE"
267 cd sdk/ruby
268 # clean up old packages
269 find -maxdepth 1 \( -name 'arvados-*.gem' -or -name 'rubygem-arvados_*.deb' \) \
270     -delete
271
272 if [[ "$DEBUG" != 0 ]]; then
273   gem build arvados.gemspec
274 else
275   # -q appears to be broken in gem version 2.2.2
276   gem build arvados.gemspec -q >/dev/null
277 fi
278
279 if [[ "$UPLOAD" != 0 ]]; then
280   # publish new gem
281   gem push arvados-*gem
282 fi
283
284 build_and_scp_deb arvados-*.gem "" "Curoverse, Inc." gem "" \
285     --prefix "$FPM_GEM_PREFIX"
286
287 # Build arvados-cli GEM
288 cd "$WORKSPACE"
289 cd sdk/cli
290 # clean up old gems
291 rm -f arvados-cli*gem
292
293 if [[ "$DEBUG" != 0 ]]; then
294   gem build arvados-cli.gemspec
295 else
296   # -q appears to be broken in gem version 2.2.2
297   gem build arvados-cli.gemspec -q >/dev/null
298 fi
299
300 if [[ "$UPLOAD" != 0 ]]; then
301   # publish new gem
302   gem push arvados-cli*gem
303 fi
304
305 # Python packages
306 if [[ "$DEBUG" != 0 ]]; then
307   echo
308   echo "Python packages"
309   echo
310 fi
311
312 cd "$WORKSPACE"
313
314 cd sdk/python
315 handle_python_package
316
317 cd ../../services/fuse
318 handle_python_package
319
320 cd ../../services/nodemanager
321 handle_python_package
322
323 if [[ ! -d "$WORKSPACE/debs" ]]; then
324   mkdir -p $WORKSPACE/debs
325 fi
326
327 # Arvados-src
328 # We use $WORKSPACE/src-build-dir as the clean directory from which to build the src package
329 if [[ ! -d "$WORKSPACE/src-build-dir" ]]; then
330   mkdir "$WORKSPACE/src-build-dir"
331   cd "$WORKSPACE"
332   if [[ "$DEBUG" != 0 ]]; then
333     git clone https://github.com/curoverse/arvados.git src-build-dir
334   else
335     git clone -q https://github.com/curoverse/arvados.git src-build-dir
336   fi
337 fi
338
339 cd "$WORKSPACE/src-build-dir"
340 # just in case, check out master
341 if [[ "$DEBUG" != 0 ]]; then
342   git checkout master
343   git pull
344   # go into detached-head state
345   git checkout `git log --format=format:%h -n1 .`
346 else
347   git checkout -q master
348   git pull -q
349   # go into detached-head state
350   git checkout -q `git log --format=format:%h -n1 .`
351 fi
352
353 # Build arvados src deb package
354 cd "$WORKSPACE"
355 PKG_VERSION=$(version_from_git)
356 cd $WORKSPACE/debs
357 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"
358
359 # clean up, check out master and step away from detached-head state
360 cd "$WORKSPACE/src-build-dir"
361 if [[ "$DEBUG" != 0 ]]; then
362   git checkout master
363 else
364   git checkout -q master
365 fi
366
367 # Keep
368 export GOPATH=$(mktemp -d)
369 mkdir -p "$GOPATH/src/git.curoverse.com"
370 ln -sfn "$WORKSPACE" "$GOPATH/src/git.curoverse.com/arvados.git"
371
372 # keepstore
373 cd "$GOPATH/src/git.curoverse.com/arvados.git/services/keepstore"
374 PKG_VERSION=$(version_from_git)
375 go get "git.curoverse.com/arvados.git/services/keepstore"
376 cd $WORKSPACE/debs
377 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"
378
379 # keepproxy
380 cd "$GOPATH/src/git.curoverse.com/arvados.git/sdk/go"
381 GO_SDK_VERSION=$(version_from_git)
382 GO_SDK_TIMESTAMP=$(timestamp_from_git)
383
384 cd "$GOPATH/src/git.curoverse.com/arvados.git/services/keepproxy"
385 KEEPPROXY_VERSION=$(version_from_git)
386 KEEPPROXY_TIMESTAMP=$(timestamp_from_git)
387
388 if [[ "$GO_SDK_TIMESTAMP" -gt "$KEEPPROXY_TIMESTAMP" ]]; then
389   PKG_VERSION=$GO_SDK_VERSION
390 else
391   PKG_VERSION=$KEEPPROXY_VERSION
392 fi
393
394 go get "git.curoverse.com/arvados.git/services/keepproxy"
395 cd $WORKSPACE/debs
396 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"
397
398 # crunchstat
399 cd "$GOPATH/src/git.curoverse.com/arvados.git/services/crunchstat"
400 PKG_VERSION=$(version_from_git)
401 go get "git.curoverse.com/arvados.git/services/crunchstat"
402 cd $WORKSPACE/debs
403 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"
404
405 # The Python SDK
406 # Please resist the temptation to add --no-python-fix-name to the fpm call here
407 # (which would remove the python- prefix from the package name), because this
408 # package is a dependency of arvados-fuse, and fpm can not omit the python-
409 # prefix from only one of the dependencies of a package...  Maybe I could
410 # whip up a patch and send it upstream, but that will be for another day. Ward,
411 # 2014-05-15
412 cd $WORKSPACE/debs2
413 # Python version numbering is obscure. Strip dashes and replace them with dots
414 # to match our other version numbers. Cf. commit 4afcb8c, compliance with PEP-440.
415 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"
416
417 # The FUSE driver
418 # Please seem comment about --no-python-fix-name above; we stay consistent and do
419 # not omit the python- prefix first.
420 cd $WORKSPACE/debs
421 # Python version numbering is obscure. Strip dashes and replace them with dots
422 # to match our other version numbers. Cf. commit 4afcb8c, compliance with PEP-440.
423 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"
424
425 # The node manager
426 cd $WORKSPACE/debs
427 # Python version numbering is obscure. Strip dashes and replace them with dots
428 # to match our other version numbers. Cf. commit 4afcb8c, compliance with PEP-440.
429 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"
430
431 # A few dependencies
432 for deppkg in python-gflags pyvcf google-api-python-client oauth2client \
433       pyasn1 pyasn1-modules rsa uritemplate httplib2 ws4py virtualenv \
434       pykka apache-libcloud requests six pyexecjs jsonschema; do
435     build_and_scp_deb "$deppkg"
436 done
437
438 # cwltool from common-workflow-language. We use this in arv-run-pipeline-instance.
439 # We use $WORKSPACE/common-workflow-language as the clean directory from which to build the cwltool package
440 if [[ ! -d "$WORKSPACE/common-workflow-language" ]]; then
441   mkdir "$WORKSPACE/common-workflow-language"
442   cd "$WORKSPACE"
443   if [[ "$DEBUG" != 0 ]]; then
444     git clone https://github.com/common-workflow-language/common-workflow-language.git common-workflow-language
445   else
446     git clone -q https://github.com/common-workflow-language/common-workflow-language.git common-workflow-language
447   fi
448 fi
449
450 cd "$WORKSPACE/common-workflow-language"
451 if [[ "$DEBUG" != 0 ]]; then
452   git checkout master
453   git pull
454 else
455   git checkout -q master
456   git pull -q
457 fi
458
459 cd reference
460 handle_python_package
461 CWLTOOL_VERSION=`git log --first-parent --max-count=1 --format='format:0.1.%ct.%h'`
462
463 # Build cwltool package
464 cd $WORKSPACE/debs
465 # Python version numbering is obscure. Strip dashes and replace them with dots
466 # to match our other version numbers. Cf. commit 4afcb8c, compliance with PEP-440.
467 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)"
468
469 # Finally, publish the packages, if necessary
470 if [[ "$UPLOAD" != 0 && "$CALL_FREIGHT" != 0 ]]; then
471   ssh -p2222 $APTUSER@$APTSERVER -t "cd tmp && ls -laF *deb && freight add *deb apt/wheezy && freight cache && rm -f *deb"
472 else
473   if [[ "$UPLOAD" != 0 ]]; then
474     echo "No new packages generated. No freight run necessary."
475   fi
476 fi
477
478 # clean up temporary GOPATH
479 rm -rf "$GOPATH"
480
481 exit $EXITCODE