run-build-packages: Test that fpm is callable before trying to do
[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")
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 # Now fix our umask to something better suited to building and publishing
191 # gems and packages
192 umask 0022
193
194 if [[ "$DEBUG" != 0 ]]; then
195   echo "umask is" `umask`
196 fi
197
198 # Ruby gems
199 if [[ "$DEBUG" != 0 ]]; then
200   echo
201   echo "Ruby gems"
202   echo
203 fi
204
205 cd "$WORKSPACE"
206 cd sdk/ruby
207 # clean up old gems
208 rm -f arvados-*gem
209
210 if [[ "$DEBUG" != 0 ]]; then
211   gem build arvados.gemspec
212 else
213   # -q appears to be broken in gem version 2.2.2
214   gem build arvados.gemspec -q >/dev/null
215 fi
216
217 if [[ "$UPLOAD" != 0 ]]; then
218   # publish new gem
219   gem push arvados-*gem
220 fi
221
222 # Build arvados-cli GEM
223 cd "$WORKSPACE"
224 cd sdk/cli
225 # clean up old gems
226 rm -f arvados-cli*gem
227
228 if [[ "$DEBUG" != 0 ]]; then
229   gem build arvados-cli.gemspec
230 else
231   # -q appears to be broken in gem version 2.2.2
232   gem build arvados-cli.gemspec -q >/dev/null
233 fi
234
235 if [[ "$UPLOAD" != 0 ]]; then
236   # publish new gem
237   gem push arvados-cli*gem
238 fi
239
240 # Python packages
241 if [[ "$DEBUG" != 0 ]]; then
242   echo
243   echo "Python packages"
244   echo
245 fi
246
247 cd "$WORKSPACE"
248
249 GIT_TIMESTAMP=`git log --first-parent --max-count=1 --format=format:%ct -n1 .`
250 HUMAN_READABLE_TIMESTAMP=`TZ=UTC date -d @$GIT_TIMESTAMP +%Y%m%d%H%M%S`
251 GIT_HASH=`git log --first-parent --max-count=1 --format=format:$HUMAN_READABLE_TIMESTAMP.%h -n1 .`
252
253 cd sdk/python
254 handle_python_package
255
256 cd ../../services/fuse
257 handle_python_package
258
259 cd ../../services/nodemanager
260 handle_python_package
261
262 if [[ ! -d "$WORKSPACE/debs" ]]; then
263   mkdir -p $WORKSPACE/debs
264 fi
265
266 # Arvados-src
267 # We use $WORKSPACE/src-build-dir as the clean directory from which to build the src package
268 if [[ ! -d "$WORKSPACE/src-build-dir" ]]; then
269   mkdir "$WORKSPACE/src-build-dir"
270   cd "$WORKSPACE"
271   if [[ "$DEBUG" != 0 ]]; then
272     git clone https://github.com/curoverse/arvados.git src-build-dir
273   else
274     git clone -q https://github.com/curoverse/arvados.git src-build-dir
275   fi
276 fi
277
278 cd "$WORKSPACE/src-build-dir"
279 # just in case, check out master
280 if [[ "$DEBUG" != 0 ]]; then
281   git checkout master
282   git pull
283   # go into detached-head state
284   git checkout `git log --format=format:%h -n1 .`
285 else
286   git checkout -q master
287   git pull -q
288   # go into detached-head state
289   git checkout -q `git log --format=format:%h -n1 .`
290 fi
291
292 # Build arvados src deb package
293 cd $WORKSPACE/debs
294 build_and_scp_deb $WORKSPACE/src-build-dir/=/usr/local/arvados/src arvados-src 'Curoverse, Inc.' 'dir' "0.1.$GIT_HASH" "-x '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"
295
296 # clean up, check out master and step away from detached-head state
297 cd "$WORKSPACE/src-build-dir"
298 if [[ "$DEBUG" != 0 ]]; then
299   git checkout master
300 else
301   git checkout -q master
302 fi
303
304 # Keep
305 export GOPATH=$(mktemp -d)
306 mkdir -p "$GOPATH/src/git.curoverse.com"
307 ln -sfn "$WORKSPACE" "$GOPATH/src/git.curoverse.com/arvados.git"
308
309 # keepstore
310 go get "git.curoverse.com/arvados.git/services/keepstore"
311 cd $WORKSPACE/debs
312 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"
313
314 # keepproxy
315 go get "git.curoverse.com/arvados.git/services/keepproxy"
316 cd $WORKSPACE/debs
317 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"
318
319 # crunchstat
320 go get "git.curoverse.com/arvados.git/services/crunchstat"
321 cd $WORKSPACE/debs
322 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"
323
324 # The Python SDK
325 # Please resist the temptation to add --no-python-fix-name to the fpm call here
326 # (which would remove the python- prefix from the package name), because this
327 # package is a dependency of arvados-fuse, and fpm can not omit the python-
328 # prefix from only one of the dependencies of a package...  Maybe I could
329 # whip up a patch and send it upstream, but that will be for another day. Ward,
330 # 2014-05-15
331 cd $WORKSPACE/debs
332 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"
333
334 # The FUSE driver
335 # Please seem comment about --no-python-fix-name above; we stay consistent and do
336 # not omit the python- prefix first.
337 cd $WORKSPACE/debs
338 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"
339
340 # The node manager
341 cd $WORKSPACE/debs
342 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"
343
344 # A few dependencies
345 for deppkg in python-gflags pyvcf google-api-python-client oauth2client \
346       pyasn1 pyasn1-modules rsa uritemplate httplib2 ws4py virtualenv \
347       pykka apache-libcloud; do
348     build_and_scp_deb "$deppkg"
349 done
350
351 # Finally, publish the packages, if necessary
352 if [[ "$UPLOAD" != 0 && "$CALL_FREIGHT" != 0 ]]; then
353   ssh -p2222 $APTUSER@$APTSERVER -t "cd tmp && ls -laF *deb && freight add *deb apt/wheezy && freight cache && rm -f *deb"
354 else
355   if [[ "$UPLOAD" != 0 ]]; then
356     echo "No new packages generated. No freight run necessary."
357   fi
358 fi
359
360 # clean up temporary GOPATH
361 rm -rf "$GOPATH"
362
363 exit $EXITCODE