Also build a package for python-six.
[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 handle_python_package () {
89   # This function assumes the current working directory is the python package directory
90   if [[ "$UPLOAD" != 0 ]]; then
91     # Make sure only to use sdist - that's the only format pip can deal with (sigh)
92     if [[ "$DEBUG" != 0 ]]; then
93       python setup.py sdist upload
94     else
95       python setup.py -q sdist upload
96     fi
97   else
98     # Make sure only to use sdist - that's the only format pip can deal with (sigh)
99     if [[ "$DEBUG" != 0 ]]; then
100       python setup.py sdist
101     else
102       python setup.py -q sdist
103     fi
104   fi
105 }
106
107 # Build debs for everything
108 build_and_scp_deb () {
109   PACKAGE=$1
110   shift
111   PACKAGE_NAME=$1
112   shift
113   VENDOR=$1
114   shift
115   PACKAGE_TYPE=$1
116   shift
117   VERSION=$1
118   shift
119
120   if [[ "$PACKAGE_NAME" == "" ]]; then
121     PACKAGE_NAME=$PACKAGE
122   fi
123
124   if [[ "$PACKAGE_TYPE" == "" ]]; then
125     PACKAGE_TYPE='python'
126   fi
127
128   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")
129
130   if [[ "$PACKAGE_NAME" != "$PACKAGE" ]]; then
131     COMMAND_ARR+=('-n' "$PACKAGE_NAME")
132   fi
133
134   if [[ "$VENDOR" != "" ]]; then
135     COMMAND_ARR+=('--vendor' "$VENDOR")
136   fi
137
138   if [[ "$VERSION" != "" ]]; then
139     COMMAND_ARR+=('-v' "$VERSION")
140   fi
141
142   for i; do
143     COMMAND_ARR+=("$i")
144   done
145
146   COMMAND_ARR+=("$PACKAGE")
147
148   if [[ "$DEBUG" != 0 ]]; then
149     echo
150     echo "${COMMAND_ARR[@]}"
151     echo
152   fi
153
154   FPM_RESULTS=$("${COMMAND_ARR[@]}")
155   FPM_EXIT_CODE=$?
156
157   FPM_PACKAGE_NAME=''
158   if [[ $FPM_RESULTS =~ ([A-Za-z0-9_\-.]*\.deb) ]]; then
159     FPM_PACKAGE_NAME=${BASH_REMATCH[1]}
160   fi
161
162   if [[ "$FPM_PACKAGE_NAME" == "" ]]; then
163     EXITCODE=1
164     echo "Error: $PACKAGE: Unable to figure out package name from fpm results:"
165     echo
166     echo $FPM_RESULTS
167     echo
168   else
169     if [[ ! $FPM_RESULTS =~ "File already exists" ]]; then
170       if [[ "$FPM_EXIT_CODE" != "0" ]]; then
171         echo "Error building debian package for $1:\n $FPM_RESULTS"
172       else
173         if [[ "$UPLOAD" != 0 ]]; then
174           scp -P2222 $FPM_PACKAGE_NAME $APTUSER@$APTSERVER:tmp/
175           CALL_FREIGHT=1
176         fi
177       fi
178     else
179       echo "Debian package $FPM_PACKAGE_NAME exists, not rebuilding"
180     fi
181   fi
182 }
183
184 source /etc/profile.d/rvm.sh
185
186 # Make all files world-readable -- jenkins runs with umask 027, and has checked
187 # out our git tree here
188 chmod o+r "$WORKSPACE" -R
189
190 # More cleanup - make sure all executables that we'll package are 755
191 find -type d -name 'bin' |xargs -I {} find {} -type f |xargs -I {} chmod 755 {}
192
193 # Now fix our umask to something better suited to building and publishing
194 # gems and packages
195 umask 0022
196
197 if [[ "$DEBUG" != 0 ]]; then
198   echo "umask is" `umask`
199 fi
200
201 # Ruby gems
202 if [[ "$DEBUG" != 0 ]]; then
203   echo
204   echo "Ruby gems"
205   echo
206 fi
207
208 cd "$WORKSPACE"
209 cd sdk/ruby
210 # clean up old gems
211 rm -f arvados-*gem
212
213 if [[ "$DEBUG" != 0 ]]; then
214   gem build arvados.gemspec
215 else
216   # -q appears to be broken in gem version 2.2.2
217   gem build arvados.gemspec -q >/dev/null
218 fi
219
220 if [[ "$UPLOAD" != 0 ]]; then
221   # publish new gem
222   gem push arvados-*gem
223 fi
224
225 # Build arvados-cli GEM
226 cd "$WORKSPACE"
227 cd sdk/cli
228 # clean up old gems
229 rm -f arvados-cli*gem
230
231 if [[ "$DEBUG" != 0 ]]; then
232   gem build arvados-cli.gemspec
233 else
234   # -q appears to be broken in gem version 2.2.2
235   gem build arvados-cli.gemspec -q >/dev/null
236 fi
237
238 if [[ "$UPLOAD" != 0 ]]; then
239   # publish new gem
240   gem push arvados-cli*gem
241 fi
242
243 # Python packages
244 if [[ "$DEBUG" != 0 ]]; then
245   echo
246   echo "Python packages"
247   echo
248 fi
249
250 cd "$WORKSPACE"
251
252 GIT_TIMESTAMP=`git log --first-parent --max-count=1 --format=format:%ct -n1 .`
253 HUMAN_READABLE_TIMESTAMP=`TZ=UTC date -d @$GIT_TIMESTAMP +%Y%m%d%H%M%S`
254 GIT_HASH=`git log --first-parent --max-count=1 --format=format:$HUMAN_READABLE_TIMESTAMP.%h -n1 .`
255
256 cd sdk/python
257 handle_python_package
258
259 cd ../../services/fuse
260 handle_python_package
261
262 cd ../../services/nodemanager
263 handle_python_package
264
265 if [[ ! -d "$WORKSPACE/debs" ]]; then
266   mkdir -p $WORKSPACE/debs
267 fi
268
269 # Arvados-src
270 # We use $WORKSPACE/src-build-dir as the clean directory from which to build the src package
271 if [[ ! -d "$WORKSPACE/src-build-dir" ]]; then
272   mkdir "$WORKSPACE/src-build-dir"
273   cd "$WORKSPACE"
274   if [[ "$DEBUG" != 0 ]]; then
275     git clone https://github.com/curoverse/arvados.git src-build-dir
276   else
277     git clone -q https://github.com/curoverse/arvados.git src-build-dir
278   fi
279 fi
280
281 cd "$WORKSPACE/src-build-dir"
282 # just in case, check out master
283 if [[ "$DEBUG" != 0 ]]; then
284   git checkout master
285   git pull
286   # go into detached-head state
287   git checkout `git log --format=format:%h -n1 .`
288 else
289   git checkout -q master
290   git pull -q
291   # go into detached-head state
292   git checkout -q `git log --format=format:%h -n1 .`
293 fi
294
295 # Build arvados src deb package
296 cd $WORKSPACE/debs
297 build_and_scp_deb $WORKSPACE/src-build-dir/=/usr/local/arvados/src arvados-src 'Curoverse, Inc.' 'dir' "0.1.$GIT_HASH" "--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"
298
299 # clean up, check out master and step away from detached-head state
300 cd "$WORKSPACE/src-build-dir"
301 if [[ "$DEBUG" != 0 ]]; then
302   git checkout master
303 else
304   git checkout -q master
305 fi
306
307 # Keep
308 export GOPATH=$(mktemp -d)
309 mkdir -p "$GOPATH/src/git.curoverse.com"
310 ln -sfn "$WORKSPACE" "$GOPATH/src/git.curoverse.com/arvados.git"
311
312 # keepstore
313 go get "git.curoverse.com/arvados.git/services/keepstore"
314 cd $WORKSPACE/debs
315 build_and_scp_deb $GOPATH/bin/keepstore=/usr/bin/keepstore keepstore 'Curoverse, Inc.' 'dir' "0.1.$GIT_HASH" "--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"
316
317 # keepproxy
318 go get "git.curoverse.com/arvados.git/services/keepproxy"
319 cd $WORKSPACE/debs
320 build_and_scp_deb $GOPATH/bin/keepproxy=/usr/bin/keepproxy keepproxy 'Curoverse, Inc.' 'dir' "0.1.$GIT_HASH" "--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"
321
322 # crunchstat
323 go get "git.curoverse.com/arvados.git/services/crunchstat"
324 cd $WORKSPACE/debs
325 build_and_scp_deb $GOPATH/bin/crunchstat=/usr/bin/crunchstat crunchstat 'Curoverse, Inc.' 'dir' "0.1.$GIT_HASH" "--url=https://arvados.org" "--license=GNU Affero General Public License, version 3.0" "--description=Crunchstat gathers cpu/memory/network statistics of running Crunch jobs"
326
327 # The Python SDK
328 # Please resist the temptation to add --no-python-fix-name to the fpm call here
329 # (which would remove the python- prefix from the package name), because this
330 # package is a dependency of arvados-fuse, and fpm can not omit the python-
331 # prefix from only one of the dependencies of a package...  Maybe I could
332 # whip up a patch and send it upstream, but that will be for another day. Ward,
333 # 2014-05-15
334 cd $WORKSPACE/debs
335 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"
336
337 # The FUSE driver
338 # Please seem comment about --no-python-fix-name above; we stay consistent and do
339 # not omit the python- prefix first.
340 cd $WORKSPACE/debs
341 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"
342
343 # The node manager
344 cd $WORKSPACE/debs
345 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"
346
347 # A few dependencies
348 for deppkg in python-gflags pyvcf google-api-python-client oauth2client \
349       pyasn1 pyasn1-modules rsa uritemplate httplib2 ws4py virtualenv \
350       pykka apache-libcloud requests six; do
351     build_and_scp_deb "$deppkg"
352 done
353
354 # Finally, publish the packages, if necessary
355 if [[ "$UPLOAD" != 0 && "$CALL_FREIGHT" != 0 ]]; then
356   ssh -p2222 $APTUSER@$APTSERVER -t "cd tmp && ls -laF *deb && freight add *deb apt/wheezy && freight cache && rm -f *deb"
357 else
358   if [[ "$UPLOAD" != 0 ]]; then
359     echo "No new packages generated. No freight run necessary."
360   fi
361 fi
362
363 # clean up temporary GOPATH
364 rm -rf "$GOPATH"
365
366 exit $EXITCODE