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