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