Package cwltool.
[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 PKG_VERSION=$(version_from_git)
305
306 cd sdk/python
307 handle_python_package
308
309 cd ../../services/fuse
310 handle_python_package
311
312 cd ../../services/nodemanager
313 handle_python_package
314
315 if [[ ! -d "$WORKSPACE/debs" ]]; then
316   mkdir -p $WORKSPACE/debs
317 fi
318
319 # Arvados-src
320 # We use $WORKSPACE/src-build-dir as the clean directory from which to build the src package
321 if [[ ! -d "$WORKSPACE/src-build-dir" ]]; then
322   mkdir "$WORKSPACE/src-build-dir"
323   cd "$WORKSPACE"
324   if [[ "$DEBUG" != 0 ]]; then
325     git clone https://github.com/curoverse/arvados.git src-build-dir
326   else
327     git clone -q https://github.com/curoverse/arvados.git src-build-dir
328   fi
329 fi
330
331 cd "$WORKSPACE/src-build-dir"
332 # just in case, check out master
333 if [[ "$DEBUG" != 0 ]]; then
334   git checkout master
335   git pull
336   # go into detached-head state
337   git checkout `git log --format=format:%h -n1 .`
338 else
339   git checkout -q master
340   git pull -q
341   # go into detached-head state
342   git checkout -q `git log --format=format:%h -n1 .`
343 fi
344
345 # Build arvados src deb package
346 cd $WORKSPACE/debs
347 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"
348
349 # clean up, check out master and step away from detached-head state
350 cd "$WORKSPACE/src-build-dir"
351 if [[ "$DEBUG" != 0 ]]; then
352   git checkout master
353 else
354   git checkout -q master
355 fi
356
357 # Keep
358 export GOPATH=$(mktemp -d)
359 mkdir -p "$GOPATH/src/git.curoverse.com"
360 ln -sfn "$WORKSPACE" "$GOPATH/src/git.curoverse.com/arvados.git"
361
362 # keepstore
363 go get "git.curoverse.com/arvados.git/services/keepstore"
364 cd $WORKSPACE/debs
365 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"
366
367 # keepproxy
368 go get "git.curoverse.com/arvados.git/services/keepproxy"
369 cd $WORKSPACE/debs
370 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"
371
372 # crunchstat
373 go get "git.curoverse.com/arvados.git/services/crunchstat"
374 cd $WORKSPACE/debs
375 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"
376
377 # The Python SDK
378 # Please resist the temptation to add --no-python-fix-name to the fpm call here
379 # (which would remove the python- prefix from the package name), because this
380 # package is a dependency of arvados-fuse, and fpm can not omit the python-
381 # prefix from only one of the dependencies of a package...  Maybe I could
382 # whip up a patch and send it upstream, but that will be for another day. Ward,
383 # 2014-05-15
384 cd $WORKSPACE/debs
385 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"
386
387 # The FUSE driver
388 # Please seem comment about --no-python-fix-name above; we stay consistent and do
389 # not omit the python- prefix first.
390 cd $WORKSPACE/debs
391 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"
392
393 # The node manager
394 cd $WORKSPACE/debs
395 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"
396
397 # A few dependencies
398 for deppkg in python-gflags pyvcf google-api-python-client oauth2client \
399       pyasn1 pyasn1-modules rsa uritemplate httplib2 ws4py virtualenv \
400       pykka apache-libcloud requests six; do
401     build_and_scp_deb "$deppkg"
402 done
403
404 # cwltool from common-workflow-language. We use this in arv-run-pipeline-instance.
405 # We use $WORKSPACE/common-workflow-language as the clean directory from which to build the cwltool package
406 if [[ ! -d "$WORKSPACE/common-workflow-language" ]]; then
407   mkdir "$WORKSPACE/common-workflow-language"
408   cd "$WORKSPACE"
409   if [[ "$DEBUG" != 0 ]]; then
410     git clone https://github.com/rabix/common-workflow-language.git common-workflow-language
411   else
412     git clone -q https://github.com/rabix/common-workflow-language.git common-workflow-language
413   fi
414 fi
415
416 cd "$WORKSPACE/common-workflow-language"
417 if [[ "$DEBUG" != 0 ]]; then
418   git checkout master
419   git pull
420 else
421   git checkout -q master
422   git pull -q
423 fi
424
425 cd reference
426 handle_python_package
427 CWLTOOL_VERSION=`git log --first-parent --max-count=1 --format='format:0.1.%ct.%h'`
428
429 # Build cwltool package
430 cd $WORKSPACE/debs
431
432 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)"
433
434 # Finally, publish the packages, if necessary
435 if [[ "$UPLOAD" != 0 && "$CALL_FREIGHT" != 0 ]]; then
436   ssh -p2222 $APTUSER@$APTSERVER -t "cd tmp && ls -laF *deb && freight add *deb apt/wheezy && freight cache && rm -f *deb"
437 else
438   if [[ "$UPLOAD" != 0 ]]; then
439     echo "No new packages generated. No freight run necessary."
440   fi
441 fi
442
443 # clean up temporary GOPATH
444 rm -rf "$GOPATH"
445
446 exit $EXITCODE