21700: Install Bundler system-wide in Rails postinst
[arvados.git] / tools / arvbox / lib / arvbox / docker / common.sh
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 export DEBIAN_FRONTEND=noninteractive
6 export PATH=${PATH}:/usr/local/go/bin:/var/lib/arvados/bin:/opt/arvados-py/bin:/usr/src/arvados/sdk/cli/binstubs
7 export npm_config_cache=/var/lib/npm
8 export npm_config_cache_min=Infinity
9 export R_LIBS=/var/lib/Rlibs
10 export HOME=$(getent passwd arvbox | cut -d: -f6)
11 export ARVADOS_CONTAINER_PATH=/var/lib/arvados-arvbox
12 export GEM_HOME=$HOME/.gem
13 GEMLOCK=$HOME/gems.lock
14
15 export LANG=en_US.UTF-8
16 export LANGUAGE=en_US:en
17 export LC_ALL=en_US.UTF-8
18
19 defaultdev=$(/sbin/ip route|awk '/default/ { print $5 }')
20 dockerip=$(/sbin/ip route | grep default | awk '{ print $3 }')
21 containerip=$(ip addr show $defaultdev | grep 'inet ' | sed 's/ *inet \(.*\)\/.*/\1/')
22 if test -s /var/run/localip_override ; then
23     localip=$(cat /var/run/localip_override)
24 else
25     localip=$containerip
26 fi
27
28 root_cert=$ARVADOS_CONTAINER_PATH/root-cert.pem
29 root_cert_key=$ARVADOS_CONTAINER_PATH/root-cert.key
30 server_cert=$ARVADOS_CONTAINER_PATH/server-cert-${localip}.pem
31 server_cert_key=$ARVADOS_CONTAINER_PATH/server-cert-${localip}.key
32
33 declare -A services
34 services=(
35   [workbench2]=3000
36   [workbench2-ssl]=443
37   [api]=8004
38   [controller]=8003
39   [controller-ssl]=8000
40   [arv-git-httpd-ssl]=9000
41   [arv-git-httpd]=9001
42   [keep-web]=9003
43   [keep-web-ssl]=9002
44   [keep-web-dl-ssl]=9004
45   [keepproxy]=25100
46   [keepproxy-ssl]=25101
47   [keepstore0]=25107
48   [keepstore1]=25108
49   [ssh]=22
50   [doc]=8001
51   [websockets]=8005
52   [websockets-ssl]=8002
53   [webshell]=4201
54   [webshell-ssl]=4202
55 )
56
57 if test "$(id arvbox -u 2>/dev/null)" = 0 ; then
58     PGUSER=postgres
59     PGGROUP=postgres
60 else
61     PGUSER=arvbox
62     PGGROUP=arvbox
63 fi
64
65 run_bundler() {
66     flock $GEMLOCK /var/lib/arvados/bin/gem install --conservative --no-document --user --version '~> 2.4.0' bundler
67
68     BUNDLER=bundle
69     if test -x $PWD/bin/bundle ; then
70         # If present, use the one associated with rails API
71         BUNDLER=$PWD/bin/bundle
72     fi
73
74     # Use Gemfile.lock only if it is git tracked.
75     if git ls-files --error-unmatch Gemfile.lock ; then
76         flock $GEMLOCK $BUNDLER config set --local frozen true
77     else
78         flock $GEMLOCK $BUNDLER config set --local frozen false
79     fi
80     flock $GEMLOCK $BUNDLER config set --local deployment false
81
82     if test -z "$(flock $GEMLOCK /var/lib/arvados/bin/gem list | grep 'arvados[[:blank:]].*[0-9.]*dev')" ; then
83         (cd /usr/src/arvados/sdk/ruby && \
84         /var/lib/arvados/bin/gem build arvados.gemspec && flock $GEMLOCK /var/lib/arvados/bin/gem install $(ls -1 *.gem | sort -r | head -n1))
85     fi
86
87     if ! flock $GEMLOCK $BUNDLER install --verbose --local "$@" ; then
88         flock $GEMLOCK $BUNDLER install --verbose "$@"
89     fi
90 }
91
92 bundler_binstubs() {
93     BUNDLER=bundle
94     if test -x $PWD/bin/bundle ; then
95         # If present, use the one associated with rails API
96         BUNDLER=$PWD/bin/bundle
97     fi
98     flock $GEMLOCK $BUNDLER binstubs --all
99 }
100
101 # Usage: Pass any number of directories. Relative directories will be taken as
102 # relative to /usr/src/arvados. This function will build an sdist from each,
103 # then pip install them all in the arvbox virtualenv.
104 pip_install_sdist() {
105     local sdist_dir="$(mktemp --directory --tmpdir py_sdist.XXXXXXXX)"
106     trap 'rm -rf "$sdist_dir"' RETURN
107     local src_dir
108     for src_dir in "$@"; do
109         case "$src_dir" in
110             /*) ;;
111             *) src_dir="/usr/src/arvados/$src_dir" ;;
112         esac
113         env -C "$src_dir" /opt/arvados-py/bin/python3 setup.py sdist --dist-dir="$sdist_dir" \
114             || return
115     done
116     /opt/arvados-py/bin/pip install "$sdist_dir"/* || return
117     return
118 }