More updates for run-build-packages.sh:
[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:\n $FPM_RESULTS"
154   else
155     if [[ ! $FPM_RESULTS =~ "File already exists" ]]; then
156       if [[ "$FPM_EXIT_CODE" != "0" ]]; then
157         echo "Error building debian package for $1:\n $FPM_RESULTS"
158       else
159         if [[ "$UPLOAD" != 0 ]]; then
160           scp -P2222 $FPM_PACKAGE_NAME $APTUSER@$APTSERVER:tmp/
161           CALL_FREIGHT=1
162         fi
163       fi
164     else
165       echo "Debian package $FPM_PACKAGE_NAME exists, not rebuilding"
166     fi
167   fi
168 }
169
170 source /etc/profile.d/rvm.sh
171
172 # Make all files world-readable -- jenkins runs with umask 027, and has checked
173 # out our git tree here
174 chmod o+r "$WORKSPACE" -R
175
176 # Now fix our umask to something better suited to building and publishing
177 # gems and packages
178 umask 0022
179
180 if [[ "$DEBUG" != 0 ]]; then
181   echo "umask is" `umask`
182 fi
183
184 # Ruby gems
185 if [[ "$DEBUG" != 0 ]]; then
186   echo
187   echo "Ruby gems"
188   echo
189 fi
190
191 cd "$WORKSPACE"
192 cd sdk/ruby
193 # clean up old gems
194 rm -f arvados-*gem
195
196 if [[ "$DEBUG" != 0 ]]; then
197   gem build arvados.gemspec
198 else
199   # -q appears to be broken in gem version 2.2.2
200   gem build arvados.gemspec -q >/dev/null
201 fi
202
203 if [[ "$UPLOAD" != 0 ]]; then
204   # publish new gem
205   gem push arvados-*gem
206 fi
207
208 # Build arvados-cli GEM
209 cd "$WORKSPACE"
210 cd sdk/cli
211 # clean up old gems
212 rm -f arvados-cli*gem
213
214 if [[ "$DEBUG" != 0 ]]; then
215   gem build arvados-cli.gemspec
216 else
217   # -q appears to be broken in gem version 2.2.2
218   gem build arvados-cli.gemspec -q >/dev/null
219 fi
220
221 if [[ "$UPLOAD" != 0 ]]; then
222   # publish new gem
223   gem push arvados-cli*gem
224 fi
225
226 # Python packages
227 if [[ "$DEBUG" != 0 ]]; then
228   echo
229   echo "Python packages"
230   echo
231 fi
232
233 cd "$WORKSPACE"
234
235 GIT_HASH=`git log --format=format:%ct.%h -n1 .`
236
237 cd sdk/python
238 handle_python_package
239
240 cd ../../services/fuse
241 handle_python_package
242
243 cd ../../services/nodemanager
244 handle_python_package
245
246 if [[ ! -d "$WORKSPACE/debs" ]]; then
247   mkdir -p $WORKSPACE/debs
248 fi
249
250 # Arvados-src
251 # We use $WORKSPACE/src-build-dir as the clean directory from which to build the src package
252 if [[ ! -d "$WORKSPACE/src-build-dir" ]]; then
253   mkdir "$WORKSPACE/src-build-dir"
254   cd "$WORKSPACE"
255   if [[ "$DEBUG" != 0 ]]; then
256     git clone https://github.com/curoverse/arvados.git src-build-dir
257   else
258     git clone -q https://github.com/curoverse/arvados.git src-build-dir
259   fi
260 fi
261
262 cd "$WORKSPACE/src-build-dir"
263 # just in case, check out master
264 if [[ "$DEBUG" != 0 ]]; then
265   git checkout master
266   git pull
267   # go into detached-head state
268   git checkout `git log --format=format:%h -n1 .`
269 else
270   git checkout -q master
271   git pull -q
272   # go into detached-head state
273   git checkout -q `git log --format=format:%h -n1 .`
274 fi
275
276 # Build arvados src deb package
277 cd $WORKSPACE/debs
278 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"
279
280 # clean up, check out master and step away from detached-head state
281 cd "$WORKSPACE/src-build-dir"
282 if [[ "$DEBUG" != 0 ]]; then
283   git checkout master
284 else
285   git checkout -q master
286 fi
287
288 # Keep
289 export GOPATH=$(mktemp -d)
290 mkdir -p "$GOPATH/src/git.curoverse.com"
291 ln -sfn "$WORKSPACE" "$GOPATH/src/git.curoverse.com/arvados.git"
292
293 # keepstore
294 go get "git.curoverse.com/arvados.git/services/keepstore"
295 cd $WORKSPACE/debs
296 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"
297
298 # keepproxy
299 go get "git.curoverse.com/arvados.git/services/keepproxy"
300 cd $WORKSPACE/debs
301 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"
302
303 # crunchstat
304 go get "git.curoverse.com/arvados.git/services/crunchstat"
305 cd $WORKSPACE/debs
306 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"
307
308 # The Python SDK
309 # Please resist the temptation to add --no-python-fix-name to the fpm call here
310 # (which would remove the python- prefix from the package name), because this
311 # package is a dependency of arvados-fuse, and fpm can not omit the python-
312 # prefix from only one of the dependencies of a package...  Maybe I could
313 # whip up a patch and send it upstream, but that will be for another day. Ward,
314 # 2014-05-15
315 cd $WORKSPACE/debs
316 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"
317
318 # The FUSE driver
319 # Please seem comment about --no-python-fix-name above; we stay consistent and do
320 # not omit the python- prefix first.
321 cd $WORKSPACE/debs
322 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"
323
324 # The node manager
325 cd $WORKSPACE/debs
326 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"
327
328 # A few dependencies
329 build_and_scp_deb python-gflags
330 build_and_scp_deb pyvcf
331 build_and_scp_deb google-api-python-client
332 build_and_scp_deb httplib2
333 build_and_scp_deb ws4py
334 build_and_scp_deb virtualenv
335
336 # Finally, publish the packages, if necessary
337 if [[ "$UPLOAD" != 0 && "$CALL_FREIGHT" != 0 ]]; then
338   ssh -p2222 $APTUSER@$APTSERVER -t "cd tmp && ls -laF *deb && freight add *deb apt/wheezy && freight cache && rm -f *deb"
339 else
340   if [[ "$UPLOAD" != 0 ]]; then
341     echo "No new packages generated. No freight run necessary."
342   fi
343 fi
344
345 # clean up temporary GOPATH
346 rm -rf "$GOPATH"
347
348 exit $EXITCODE