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