Merge branch '3894-improve-run-tests' closes #3894
[arvados-dev.git] / jenkins / run-build-packages.sh
1 #!/bin/bash
2
3 EXITCODE=0
4 CALL_FREIGHT=0
5
6 APTUSER=$1
7 APTSERVER=$2
8
9 if [[ "$APTUSER" == '' ]]; then
10   echo "Syntax: $0 <aptuser> <aptserver>"
11   exit 1
12 fi
13
14 if [[ "$APTSERVER" == '' ]]; then
15   echo "Syntax: $0 <aptuser> <aptserver>"
16   exit 1
17 fi
18
19 source /etc/profile.d/rvm.sh
20 echo $WORKSPACE
21
22 # Build arvados GEM
23 echo "Build and publish ruby gem"
24 cd "$WORKSPACE"
25 cd sdk/ruby
26 # clean up old gems
27 rm -f arvados-*gem
28 gem build arvados.gemspec
29 # publish new gem
30 gem push arvados-*gem
31
32 # Build arvados-cli GEM
33 echo "Build and publish ruby gem"
34 cd "$WORKSPACE"
35 cd sdk/cli
36 # clean up old gems
37 rm -f arvados-cli*gem
38 gem build arvados-cli.gemspec
39 # publish new gem
40 gem push arvados-cli*gem
41
42 # Build arvados-python-client Python package
43 echo "Build and publish arvados-python-client package"
44 cd "$WORKSPACE"
45
46 GIT_HASH=`git log --format=format:%ct.%h -n1 .`
47
48 cd sdk/python
49
50 # We mess with this file below, reset it here
51 git checkout setup.py
52
53 # Make sure only to use sdist - that's the only format pip can deal with (sigh)
54 python setup.py egg_info -b ".$GIT_HASH" sdist upload
55
56 cd ../../services/fuse
57
58 # We mess with this file below, reset it here
59 git checkout setup.py
60
61 # Make sure only to use sdist - that's the only format pip can deal with (sigh)
62 python setup.py egg_info -b ".$GIT_HASH" sdist upload
63
64 # Build debs for everything
65
66 # Build arvados src deb package
67
68 build_and_scp_deb () {
69   PACKAGE=$1
70   PACKAGE_NAME=$2
71   # Put spaces in $3 and you will regret it. Despite the use of arrays below.
72   # Because, bash sucks.
73   VENDOR=${3// /_}
74   PACKAGE_TYPE=$4
75   VERSION=$5
76   EXTRA_ARGUMENTS=$6
77
78   if [[ "$PACKAGE_NAME" == "" ]]; then
79     PACKAGE_NAME=$PACKAGE
80   fi
81
82   if [[ "$PACKAGE_TYPE" == "" ]]; then
83     PACKAGE_TYPE='python'
84   fi
85
86   COMMAND_ARR=("fpm" "-s" "$PACKAGE_TYPE" "-t" "deb")
87
88   if [[ "$PACKAGE_NAME" != "$PACKAGE" ]]; then
89     COMMAND_ARR+=('-n' "$PACKAGE_NAME")
90   fi
91
92   if [[ "$VENDOR" != "" ]]; then
93     COMMAND_ARR+=('--vendor' "$VENDOR")
94   fi
95
96   if [[ "$VERSION" != "" ]]; then
97     COMMAND_ARR+=('-v' "$VERSION")
98   fi
99
100   for a in $EXTRA_ARGUMENTS; do
101     COMMAND_ARR+=("$a")
102   done
103
104   COMMAND_ARR+=("$PACKAGE")
105
106   FPM_RESULTS=$(${COMMAND_ARR[@]})
107   FPM_EXIT_CODE=$?
108   echo ${COMMAND_ARR[@]}
109
110   FPM_PACKAGE_NAME=''
111   if [[ $FPM_RESULTS =~ ([A-Za-z0-9_\-.]*\.deb) ]]; then
112     FPM_PACKAGE_NAME=${BASH_REMATCH[1]}
113   fi
114
115   if [[ "$FPM_PACKAGE_NAME" == "" ]]; then
116     EXITCODE=1
117     echo "Error: Unabled figure out package name from fpm results:\n $FPM_RESULTS"
118   else
119     if [[ ! $FPM_RESULTS =~ "File already exists" ]]; then
120       if [[ "$FPM_EXIT_CODE" != "0" ]]; then
121         echo "Error building debian package for $1:\n $FPM_RESULTS"
122       else
123         scp -P2222 $FPM_PACKAGE_NAME $APTUSER@$APTSERVER:tmp/
124         CALL_FREIGHT=1
125       fi
126     else
127       echo "Debian package $FPM_PACKAGE_NAME exists, not rebuilding"
128     fi
129   fi
130 }
131
132 if [[ ! -d "$WORKSPACE/debs" ]]; then
133   mkdir -p $WORKSPACE/debs
134 fi
135
136 # Arvados-src
137 # We use $WORKSPACE/src-build-dir as the clean directory from which to build the src package
138 if [[ ! -d "$WORKSPACE/src-build-dir" ]]; then
139   mkdir "$WORKSPACE/src-build-dir"
140   cd "$WORKSPACE"
141   git clone https://github.com/curoverse/arvados.git src-build-dir
142 fi  
143
144 cd "$WORKSPACE/src-build-dir"
145 # just in case, check out master
146 git checkout master
147 git pull
148 # go into detached-head state
149 git checkout `git log --format=format:%h -n1 .`
150 cd $WORKSPACE
151
152 cd $WORKSPACE/debs
153 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*'"
154
155 # clean up, check out master and step away from detached-head state
156 cd "$WORKSPACE/src-build-dir"
157 git checkout master
158
159 # Keep
160 export GOPATH=$(mktemp -d)
161 mkdir -p "$GOPATH/src/git.curoverse.com"
162 ln -sfn "$WORKSPACE" "$GOPATH/src/git.curoverse.com/arvados.git"
163
164 # keepstore
165 go get "git.curoverse.com/arvados.git/services/keepstore"
166 cd $WORKSPACE/debs
167 build_and_scp_deb $GOPATH/bin/keepstore=/usr/bin/keepstore keepstore 'Curoverse, Inc.' 'dir' "0.1.$GIT_HASH"
168
169 # keepproxy
170 go get "git.curoverse.com/arvados.git/services/keepproxy"
171 cd $WORKSPACE/debs
172 build_and_scp_deb $GOPATH/bin/keepproxy=/usr/bin/keepproxy keepproxy 'Curoverse, Inc.' 'dir' "0.1.$GIT_HASH"
173
174 # crunchstat
175 go get "git.curoverse.com/arvados.git/services/crunchstat"
176 cd $WORKSPACE/debs
177 build_and_scp_deb $GOPATH/bin/crunchstat=/usr/bin/crunchstat crunchstat 'Curoverse, Inc.' 'dir' "0.1.$GIT_HASH"
178
179 # The Python SDK
180 cd $WORKSPACE/sdk/python
181 sed -i'' -e "s:version='0.1':version='0.1.$GIT_HASH':" setup.py
182
183 cd $WORKSPACE/debs
184
185 # Please resist the temptation to add --no-python-fix-name to the fpm call here
186 # (which would remove the python- prefix from the package name), because this
187 # package is a dependency of arvados-fuse, and fpm can not omit the python-
188 # prefix from only one of the dependencies of a package...  Maybe I could
189 # whip up a patch and send it upstream, but that will be for another day. Ward,
190 # 2014-05-15
191 build_and_scp_deb $WORKSPACE/sdk/python python-arvados-python-client 'Curoverse, Inc.' 'python' "0.1.${GIT_HASH}"
192
193 # The FUSE driver
194 cd $WORKSPACE/services/fuse
195 sed -i'' -e "s:version='0.1':version='0.1.$GIT_HASH':" setup.py
196
197 cd $WORKSPACE/debs
198
199 # Please seem comment about --no-python-fix-name above; we stay consistent and do
200 # not omit the python- prefix first.
201 build_and_scp_deb $WORKSPACE/services/fuse python-arvados-fuse 'Curoverse, Inc.' 'python' "0.1.${GIT_HASH}"
202
203 # A few dependencies
204 build_and_scp_deb python-gflags
205 build_and_scp_deb pyvcf
206 build_and_scp_deb google-api-python-client
207 build_and_scp_deb httplib2
208 build_and_scp_deb ws4py
209 build_and_scp_deb virtualenv
210
211 # Finally, publish the packages, if necessary
212 if [[ "$CALL_FREIGHT" != "0" ]]; then
213   ssh -p2222 $APTUSER@$APTSERVER -t "cd tmp && ls -laF *deb && freight add *deb apt/wheezy && freight cache && rm -f *deb"
214 else
215   echo "No new packages generated. No freight run necessary."
216 fi
217
218 # clean up temporary GOPATH
219 rm -rf "$GOPATH"
220
221 exit $EXITCODE