Make sdk/cli testable in interactive mode.
[arvados.git] / build / run-build-packages.sh
1 #!/bin/bash
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: AGPL-3.0
5
6 . `dirname "$(readlink -f "$0")"`/run-library.sh
7 . `dirname "$(readlink -f "$0")"`/libcloud-pin.sh
8
9 read -rd "\000" helpmessage <<EOF
10 $(basename $0): Build Arvados packages
11
12 Syntax:
13         WORKSPACE=/path/to/arvados $(basename $0) [options]
14
15 Options:
16
17 --build-bundle-packages  (default: false)
18     Build api server and workbench packages with vendor/bundle included
19 --debug
20     Output debug information (default: false)
21 --target <target>
22     Distribution to build packages for (default: debian8)
23 --only-build <package>
24     Build only a specific package (or $ONLY_BUILD from environment)
25 --command
26     Build command to execute (defaults to the run command defined in the
27     Docker image)
28
29 WORKSPACE=path         Path to the Arvados source tree to build packages from
30
31 EOF
32
33 # Begin of user configuration
34
35 # set to --no-cache-dir to disable pip caching
36 CACHE_FLAG=
37
38 MAINTAINER="Ward Vandewege <wvandewege@veritasgenetics.com>"
39 VENDOR="Veritas Genetics, Inc."
40
41 # End of user configuration
42
43 DEBUG=${ARVADOS_DEBUG:-0}
44 EXITCODE=0
45 TARGET=debian8
46 COMMAND=
47
48 PARSEDOPTS=$(getopt --name "$0" --longoptions \
49     help,build-bundle-packages,debug,target:,only-build: \
50     -- "" "$@")
51 if [ $? -ne 0 ]; then
52     exit 1
53 fi
54
55 eval set -- "$PARSEDOPTS"
56 while [ $# -gt 0 ]; do
57     case "$1" in
58         --help)
59             echo >&2 "$helpmessage"
60             echo >&2
61             exit 1
62             ;;
63         --target)
64             TARGET="$2"; shift
65             ;;
66         --only-build)
67             ONLY_BUILD="$2"; shift
68             ;;
69         --debug)
70             DEBUG=1
71             ;;
72         --command)
73             COMMAND="$2"; shift
74             ;;
75         --)
76             if [ $# -gt 1 ]; then
77                 echo >&2 "$0: unrecognized argument '$2'. Try: $0 --help"
78                 exit 1
79             fi
80             ;;
81     esac
82     shift
83 done
84
85 if [[ "$COMMAND" != "" ]]; then
86   COMMAND="/usr/local/rvm/bin/rvm-exec default bash /jenkins/$COMMAND --target $TARGET"
87 fi
88
89 STDOUT_IF_DEBUG=/dev/null
90 STDERR_IF_DEBUG=/dev/null
91 DASHQ_UNLESS_DEBUG=-q
92 if [[ "$DEBUG" != 0 ]]; then
93     STDOUT_IF_DEBUG=/dev/stdout
94     STDERR_IF_DEBUG=/dev/stderr
95     DASHQ_UNLESS_DEBUG=
96 fi
97
98 declare -a PYTHON_BACKPORTS PYTHON3_BACKPORTS
99
100 PYTHON2_VERSION=2.7
101 PYTHON3_VERSION=$(python3 -c 'import sys; print("{v.major}.{v.minor}".format(v=sys.version_info))')
102
103 ## These defaults are suitable for any Debian-based distribution.
104 # You can customize them as needed in distro sections below.
105 PYTHON2_PACKAGE=python$PYTHON2_VERSION
106 PYTHON2_PKG_PREFIX=python
107 PYTHON2_PREFIX=/usr
108 PYTHON2_INSTALL_LIB=lib/python$PYTHON2_VERSION/dist-packages
109
110 PYTHON3_PACKAGE=python$PYTHON3_VERSION
111 PYTHON3_PKG_PREFIX=python3
112 PYTHON3_PREFIX=/usr
113 PYTHON3_INSTALL_LIB=lib/python$PYTHON3_VERSION/dist-packages
114 ## End Debian Python defaults.
115
116 case "$TARGET" in
117     debian*)
118         FORMAT=deb
119         ;;
120     ubuntu*)
121         FORMAT=deb
122         ;;
123     centos*)
124         FORMAT=rpm
125         PYTHON2_PACKAGE=$(rpm -qf "$(which python$PYTHON2_VERSION)" --queryformat '%{NAME}\n')
126         PYTHON2_PKG_PREFIX=$PYTHON2_PACKAGE
127         PYTHON2_INSTALL_LIB=lib/python$PYTHON2_VERSION/site-packages
128         PYTHON3_PACKAGE=$(rpm -qf "$(which python$PYTHON3_VERSION)" --queryformat '%{NAME}\n')
129         PYTHON3_PKG_PREFIX=$PYTHON3_PACKAGE
130         PYTHON3_PREFIX=/opt/rh/rh-python35/root/usr
131         PYTHON3_INSTALL_LIB=lib/python$PYTHON3_VERSION/site-packages
132         export PYCURL_SSL_LIBRARY=nss
133         ;;
134     *)
135         echo -e "$0: Unknown target '$TARGET'.\n" >&2
136         exit 1
137         ;;
138 esac
139
140
141 if ! [[ -n "$WORKSPACE" ]]; then
142   echo >&2 "$helpmessage"
143   echo >&2
144   echo >&2 "Error: WORKSPACE environment variable not set"
145   echo >&2
146   exit 1
147 fi
148
149 # Test for fpm
150 fpm --version >/dev/null 2>&1
151
152 if [[ "$?" != 0 ]]; then
153   echo >&2 "$helpmessage"
154   echo >&2
155   echo >&2 "Error: fpm not found"
156   echo >&2
157   exit 1
158 fi
159
160 PYTHON2_FPM_INSTALLER=(--python-easyinstall "$(find_python_program easy_install-$PYTHON2_VERSION easy_install)")
161 install3=$(find_python_program easy_install-$PYTHON3_VERSION easy_install3 pip-$PYTHON3_VERSION pip3)
162 if [[ $install3 =~ easy_ ]]; then
163     PYTHON3_FPM_INSTALLER=(--python-easyinstall "$install3")
164 else
165     PYTHON3_FPM_INSTALLER=(--python-pip "$install3")
166 fi
167
168 RUN_BUILD_PACKAGES_PATH="`dirname \"$0\"`"
169 RUN_BUILD_PACKAGES_PATH="`( cd \"$RUN_BUILD_PACKAGES_PATH\" && pwd )`"  # absolutized and normalized
170 if [ -z "$RUN_BUILD_PACKAGES_PATH" ] ; then
171   # error; for some reason, the path is not accessible
172   # to the script (e.g. permissions re-evaled after suid)
173   exit 1  # fail
174 fi
175
176 debug_echo "$0 is running from $RUN_BUILD_PACKAGES_PATH"
177 debug_echo "Workspace is $WORKSPACE"
178
179 if [[ -f /etc/profile.d/rvm.sh ]]; then
180     source /etc/profile.d/rvm.sh
181     GEM="rvm-exec default gem"
182 else
183     GEM=gem
184 fi
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 cd "$WORKSPACE"
192 find -type d -name 'bin' |xargs -I {} find {} -type f |xargs -I {} chmod 755 {}
193
194 # Now fix our umask to something better suited to building and publishing
195 # gems and packages
196 umask 0022
197
198 debug_echo "umask is" `umask`
199
200 if [[ ! -d "$WORKSPACE/packages/$TARGET" ]]; then
201   mkdir -p $WORKSPACE/packages/$TARGET
202   chown --reference="$WORKSPACE" "$WORKSPACE/packages/$TARGET"
203 fi
204
205 # Perl packages
206 debug_echo -e "\nPerl packages\n"
207
208 if [[ -z "$ONLY_BUILD" ]] || [[ "libarvados-perl" = "$ONLY_BUILD" ]] ; then
209   cd "$WORKSPACE/sdk/perl"
210   libarvados_perl_version="$(version_from_git)"
211
212   cd $WORKSPACE/packages/$TARGET
213   test_package_presence libarvados-perl "$libarvados_perl_version"
214
215   if [[ "$?" == "0" ]]; then
216     cd "$WORKSPACE/sdk/perl"
217
218     if [[ -e Makefile ]]; then
219       make realclean >"$STDOUT_IF_DEBUG"
220     fi
221     find -maxdepth 1 \( -name 'MANIFEST*' -or -name "libarvados-perl*.$FORMAT" \) \
222         -delete
223     rm -rf install
224
225     perl Makefile.PL INSTALL_BASE=install >"$STDOUT_IF_DEBUG" && \
226         make install INSTALLDIRS=perl >"$STDOUT_IF_DEBUG" && \
227         fpm_build install/lib/=/usr/share libarvados-perl \
228         dir "$(version_from_git)" install/man/=/usr/share/man \
229         "$WORKSPACE/apache-2.0.txt=/usr/share/doc/libarvados-perl/apache-2.0.txt" && \
230         mv --no-clobber libarvados-perl*.$FORMAT "$WORKSPACE/packages/$TARGET/"
231   fi
232 fi
233
234 # Ruby gems
235 debug_echo -e "\nRuby gems\n"
236
237 FPM_GEM_PREFIX=$($GEM environment gemdir)
238
239 cd "$WORKSPACE/sdk/ruby"
240 handle_ruby_gem arvados
241
242 cd "$WORKSPACE/sdk/cli"
243 handle_ruby_gem arvados-cli
244
245 cd "$WORKSPACE/services/login-sync"
246 handle_ruby_gem arvados-login-sync
247
248 # Python packages
249 debug_echo -e "\nPython packages\n"
250
251 # arvados-src
252 (
253     cd "$WORKSPACE"
254     COMMIT_HASH=$(format_last_commit_here "%H")
255     arvados_src_version="$(version_from_git)"
256
257     cd $WORKSPACE/packages/$TARGET
258     test_package_presence arvados-src $arvados_src_version src ""
259
260     if [[ "$?" == "0" ]]; then
261       cd "$WORKSPACE"
262       SRC_BUILD_DIR=$(mktemp -d)
263       # mktemp creates the directory with 0700 permissions by default
264       chmod 755 $SRC_BUILD_DIR
265       git clone $DASHQ_UNLESS_DEBUG "$WORKSPACE/.git" "$SRC_BUILD_DIR"
266       cd "$SRC_BUILD_DIR"
267
268       # go into detached-head state
269       git checkout $DASHQ_UNLESS_DEBUG "$COMMIT_HASH"
270       echo "$COMMIT_HASH" >git-commit.version
271
272       cd "$SRC_BUILD_DIR"
273       PKG_VERSION=$(version_from_git)
274       cd $WORKSPACE/packages/$TARGET
275       fpm_build $SRC_BUILD_DIR/=/usr/local/arvados/src arvados-src 'dir' "$PKG_VERSION" "--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"
276
277       rm -rf "$SRC_BUILD_DIR"
278     fi
279 )
280
281 # Go binaries
282 cd $WORKSPACE/packages/$TARGET
283 export GOPATH=$(mktemp -d)
284 go get github.com/kardianos/govendor
285 package_go_binary cmd/arvados-client arvados-client \
286     "Arvados command line tool (beta)"
287 package_go_binary cmd/arvados-server arvados-server \
288     "Arvados server daemons"
289 package_go_binary cmd/arvados-server arvados-controller \
290     "Arvados cluster controller daemon"
291 package_go_binary cmd/arvados-server arvados-dispatch-cloud \
292     "Arvados cluster cloud dispatch"
293 package_go_binary sdk/go/crunchrunner crunchrunner \
294     "Crunchrunner executes a command inside a container and uploads the output"
295 package_go_binary services/arv-git-httpd arvados-git-httpd \
296     "Provide authenticated http access to Arvados-hosted git repositories"
297 package_go_binary services/crunch-dispatch-local crunch-dispatch-local \
298     "Dispatch Crunch containers on the local system"
299 package_go_binary services/crunch-dispatch-slurm crunch-dispatch-slurm \
300     "Dispatch Crunch containers to a SLURM cluster"
301 package_go_binary services/crunch-run crunch-run \
302     "Supervise a single Crunch container"
303 package_go_binary services/crunchstat crunchstat \
304     "Gather cpu/memory/network statistics of running Crunch jobs"
305 package_go_binary services/health arvados-health \
306     "Check health of all Arvados cluster services"
307 package_go_binary services/keep-balance keep-balance \
308     "Rebalance and garbage-collect data blocks stored in Arvados Keep"
309 package_go_binary services/keepproxy keepproxy \
310     "Make a Keep cluster accessible to clients that are not on the LAN"
311 package_go_binary services/keepstore keepstore \
312     "Keep storage daemon, accessible to clients on the LAN"
313 package_go_binary services/keep-web keep-web \
314     "Static web hosting service for user data stored in Arvados Keep"
315 package_go_binary services/ws arvados-ws \
316     "Arvados Websocket server"
317 package_go_binary tools/sync-groups arvados-sync-groups \
318     "Synchronize remote groups into Arvados from an external source"
319 package_go_binary tools/keep-block-check keep-block-check \
320     "Verify that all data from one set of Keep servers to another was copied"
321 package_go_binary tools/keep-rsync keep-rsync \
322     "Copy all data from one set of Keep servers to another"
323 package_go_binary tools/keep-exercise keep-exercise \
324     "Performance testing tool for Arvados Keep"
325
326 # The Python SDK
327 fpm_build_virtualenv "arvados-python-client" "sdk/python"
328 fpm_build_virtualenv "arvados-python-client" "sdk/python" "python3"
329
330 # Arvados cwl runner
331 fpm_build_virtualenv "arvados-cwl-runner" "sdk/cwl"
332
333 # The PAM module
334 fpm_build_virtualenv "libpam-arvados" "sdk/pam"
335
336 # The FUSE driver
337 fpm_build_virtualenv "arvados-fuse" "services/fuse"
338
339 # The node manager
340 fpm_build_virtualenv "arvados-node-manager" "services/nodemanager"
341
342 # The Docker image cleaner
343 fpm_build_virtualenv "arvados-docker-cleaner" "services/dockercleaner" "python3"
344
345 # The Arvados crunchstat-summary tool
346 fpm_build_virtualenv "crunchstat-summary" "tools/crunchstat-summary"
347
348 # The cwltest package, which lives out of tree
349 cd "$WORKSPACE"
350 if [[ -e "$WORKSPACE/cwltest" ]]; then
351         rm -rf "$WORKSPACE/cwltest"
352 fi
353 git clone https://github.com/common-workflow-language/cwltest.git
354 # signal to our build script that we want a cwltest executable installed in /usr/bin/
355 mkdir cwltest/bin && touch cwltest/bin/cwltest
356 fpm_build_virtualenv "cwltest" "cwltest"
357 rm -rf "$WORKSPACE/cwltest"
358
359 # Build the API server package
360 test_rails_package_presence arvados-api-server "$WORKSPACE/services/api"
361 if [[ "$?" == "0" ]]; then
362   handle_rails_package arvados-api-server "$WORKSPACE/services/api" \
363       "$WORKSPACE/agpl-3.0.txt" --url="https://arvados.org" \
364       --description="Arvados API server - Arvados is a free and open source platform for big data science." \
365       --license="GNU Affero General Public License, version 3.0"
366 fi
367
368 # Build the workbench server package
369 test_rails_package_presence arvados-workbench "$WORKSPACE/apps/workbench"
370 if [[ "$?" == "0" ]] ; then
371   (
372       set -e
373       cd "$WORKSPACE/apps/workbench"
374
375       # We need to bundle to be ready even when we build a package without vendor directory
376       # because asset compilation requires it.
377       bundle install --system >"$STDOUT_IF_DEBUG"
378
379       # clear the tmp directory; the asset generation step will recreate tmp/cache/assets,
380       # and we want that in the package, so it's easier to not exclude the tmp directory
381       # from the package - empty it instead.
382       rm -rf tmp
383       mkdir tmp
384
385       # Set up application.yml and production.rb so that asset precompilation works
386       \cp config/application.yml.example config/application.yml -f
387       \cp config/environments/production.rb.example config/environments/production.rb -f
388       sed -i 's/secret_token: ~/secret_token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/' config/application.yml
389       sed -i 's/keep_web_url: false/keep_web_url: exampledotcom/' config/application.yml
390
391       RAILS_ENV=production RAILS_GROUPS=assets bundle exec rake npm:install >/dev/null
392       RAILS_ENV=production RAILS_GROUPS=assets bundle exec rake assets:precompile >/dev/null
393
394       # Remove generated configuration files so they don't go in the package.
395       rm config/application.yml config/environments/production.rb
396   )
397
398   if [[ "$?" != "0" ]]; then
399     echo "ERROR: Asset precompilation failed"
400     EXITCODE=1
401   else
402     handle_rails_package arvados-workbench "$WORKSPACE/apps/workbench" \
403         "$WORKSPACE/agpl-3.0.txt" --url="https://arvados.org" \
404         --description="Arvados Workbench - Arvados is a free and open source platform for big data science." \
405         --license="GNU Affero General Public License, version 3.0"
406   fi
407 fi
408
409 # clean up temporary GOPATH
410 rm -rf "$GOPATH"
411
412 exit $EXITCODE