Merge branch '17152-collection-versions-fixes'
[arvados.git] / tools / salt-install / provision.sh
1 #!/bin/bash
2
3 # Copyright (C) The Arvados Authors. All rights reserved.
4 #
5 # SPDX-License-Identifier: CC-BY-SA-3.0
6
7 # If you want to test arvados in a single host, you can run this script, which
8 # will install it using salt masterless
9 # This script is run by the Vagrant file when you run it with
10 #
11 # vagrant up
12
13 ##########################################################
14 # This section are the basic parameters to configure the installation
15
16 # The 5 letters name you want to give your cluster
17 CLUSTER="arva2"
18 DOMAIN="arv.local"
19
20 INITIAL_USER="admin"
21
22 # If not specified, the initial user email will be composed as
23 # INITIAL_USER@CLUSTER.DOMAIN
24 INITIAL_USER_EMAIL="${INITIAL_USER}@${CLUSTER}.${DOMAIN}"
25 INITIAL_USER_PASSWORD="password"
26
27 # The example config you want to use. Currently, only "single_host" is
28 # available
29 CONFIG_DIR="single_host"
30
31 # Which release of Arvados repo you want to use
32 RELEASE="production"
33 # Which version of Arvados you want to install. Defaults to 'latest'
34 # in the desired repo
35 VERSION="latest"
36
37 # Host SSL port where you want to point your browser to access Arvados
38 # Defaults to 443 for regular runs, and to 8443 when called in Vagrant.
39 # You can point it to another port if desired
40 # In Vagrant, make sure it matches what you set in the Vagrantfile
41 # HOST_SSL_PORT=443
42
43 # This is a arvados-formula setting.
44 # If branch is set, the script will switch to it before running salt
45 # Usually not needed, only used for testing
46 # BRANCH="master"
47
48 ##########################################################
49 # Usually there's no need to modify things below this line
50
51 # Formulas versions
52 ARVADOS_TAG="v1.1.3"
53 POSTGRES_TAG="v0.41.3"
54 NGINX_TAG="v2.4.0"
55 DOCKER_TAG="v1.0.0"
56 LOCALE_TAG="v0.3.4"
57
58 set -o pipefail
59
60 # capture the directory that the script is running from
61 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
62
63 usage() {
64   echo >&2
65   echo >&2 "Usage: ${0} [-h] [-h]"
66   echo >&2
67   echo >&2 "${0} options:"
68   echo >&2 "  -d, --debug             Run salt installation in debug mode"
69   echo >&2 "  -p <N>, --ssl-port <N>  SSL port to use for the web applications"
70   echo >&2 "  -t, --test              Test installation running a CWL workflow"
71   echo >&2 "  -h, --help              Display this help and exit"
72   echo >&2 "  -v, --vagrant           Run in vagrant and use the /vagrant shared dir"
73   echo >&2
74 }
75
76 arguments() {
77   # NOTE: This requires GNU getopt (part of the util-linux package on Debian-based distros).
78   TEMP=$(getopt -o dhp:tv \
79     --long debug,help,ssl-port:,test,vagrant \
80     -n "${0}" -- "${@}")
81
82   if [ ${?} != 0 ] ; then echo "GNU getopt missing? Use -h for help"; exit 1 ; fi
83   # Note the quotes around `$TEMP': they are essential!
84   eval set -- "$TEMP"
85
86   while [ ${#} -ge 1 ]; do
87     case ${1} in
88       -d | --debug)
89         LOG_LEVEL="debug"
90         shift
91         ;;
92       -t | --test)
93         TEST="yes"
94         shift
95         ;;
96       -v | --vagrant)
97         VAGRANT="yes"
98         shift
99         ;;
100       -p | --ssl-port)
101         HOST_SSL_PORT=${2}
102         shift 2
103         ;;
104       --)
105         shift
106         break
107         ;;
108       *)
109         usage
110         exit 1
111         ;;
112     esac
113   done
114 }
115
116 LOG_LEVEL="info"
117 HOST_SSL_PORT=443
118 TESTS_DIR="tests"
119
120 arguments ${@}
121
122 # Salt's dir
123 ## states
124 S_DIR="/srv/salt"
125 ## formulas
126 F_DIR="/srv/formulas"
127 ##pillars
128 P_DIR="/srv/pillars"
129
130 apt-get update
131 apt-get install -y curl git jq
132
133 dpkg -l |grep salt-minion
134 if [ ${?} -eq 0 ]; then
135   echo "Salt already installed"
136 else
137   curl -L https://bootstrap.saltstack.com -o /tmp/bootstrap_salt.sh
138   sh /tmp/bootstrap_salt.sh -XUdfP -x python3
139   /bin/systemctl disable salt-minion.service
140 fi
141
142 # Set salt to masterless mode
143 cat > /etc/salt/minion << EOFSM
144 file_client: local
145 file_roots:
146   base:
147     - ${S_DIR}
148     - ${F_DIR}/*
149     - ${F_DIR}/*/test/salt/states/examples
150
151 pillar_roots:
152   base:
153     - ${P_DIR}
154 EOFSM
155
156 mkdir -p ${S_DIR}
157 mkdir -p ${F_DIR}
158 mkdir -p ${P_DIR}
159
160 # States
161 cat > ${S_DIR}/top.sls << EOFTSLS
162 base:
163   '*':
164     - single_host.host_entries
165     - single_host.snakeoil_certs
166     - locale
167     - nginx.passenger
168     - postgres
169     - docker
170     - arvados
171 EOFTSLS
172
173 # Pillars
174 cat > ${P_DIR}/top.sls << EOFPSLS
175 base:
176   '*':
177     - arvados
178     - docker
179     - locale
180     - nginx_api_configuration
181     - nginx_controller_configuration
182     - nginx_keepproxy_configuration
183     - nginx_keepweb_configuration
184     - nginx_passenger
185     - nginx_websocket_configuration
186     - nginx_webshell_configuration
187     - nginx_workbench2_configuration
188     - nginx_workbench_configuration
189     - postgresql
190 EOFPSLS
191
192 # Get the formula and dependencies
193 cd ${F_DIR} || exit 1
194 git clone --branch "${ARVADOS_TAG}" https://github.com/saltstack-formulas/arvados-formula.git
195 git clone --branch "${DOCKER_TAG}" https://github.com/saltstack-formulas/docker-formula.git
196 git clone --branch "${LOCALE_TAG}" https://github.com/saltstack-formulas/locale-formula.git
197 git clone --branch "${NGINX_TAG}" https://github.com/saltstack-formulas/nginx-formula.git
198 git clone --branch "${POSTGRES_TAG}" https://github.com/saltstack-formulas/postgres-formula.git
199
200 if [ "x${BRANCH}" != "x" ]; then
201   cd ${F_DIR}/arvados-formula || exit 1
202   git checkout -t origin/"${BRANCH}"
203   cd -
204 fi
205
206 if [ "x${VAGRANT}" = "xyes" ]; then
207   SOURCE_PILLARS_DIR="/vagrant/${CONFIG_DIR}"
208   TESTS_DIR="/vagrant/${TESTS_DIR}"
209 else
210   SOURCE_PILLARS_DIR="${SCRIPT_DIR}/${CONFIG_DIR}"
211   TESTS_DIR="${SCRIPT_DIR}/${TESTS_DIR}"
212 fi
213
214 # Replace cluster and domain name in the example pillars and test files
215 for f in "${SOURCE_PILLARS_DIR}"/*; do
216   sed "s/__CLUSTER__/${CLUSTER}/g;
217        s/__DOMAIN__/${DOMAIN}/g;
218        s/__RELEASE__/${RELEASE}/g;
219        s/__HOST_SSL_PORT__/${HOST_SSL_PORT}/g;
220        s/__GUEST_SSL_PORT__/${GUEST_SSL_PORT}/g;
221        s/__INITIAL_USER__/${INITIAL_USER}/g;
222        s/__INITIAL_USER_EMAIL__/${INITIAL_USER_EMAIL}/g;
223        s/__INITIAL_USER_PASSWORD__/${INITIAL_USER_PASSWORD}/g;
224        s/__VERSION__/${VERSION}/g" \
225   "${f}" > "${P_DIR}"/$(basename "${f}")
226 done
227
228 mkdir -p /tmp/cluster_tests
229 # Replace cluster and domain name in the example pillars and test files
230 for f in "${TESTS_DIR}"/*; do
231   sed "s/__CLUSTER__/${CLUSTER}/g;
232        s/__DOMAIN__/${DOMAIN}/g;
233        s/__HOST_SSL_PORT__/${HOST_SSL_PORT}/g;
234        s/__INITIAL_USER__/${INITIAL_USER}/g;
235        s/__INITIAL_USER_EMAIL__/${INITIAL_USER_EMAIL}/g;
236        s/__INITIAL_USER_PASSWORD__/${INITIAL_USER_PASSWORD}/g" \
237   ${f} > /tmp/cluster_tests/$(basename ${f})
238 done
239 chmod 755 /tmp/cluster_tests/run-test.sh
240
241 # FIXME! #16992 Temporary fix for psql call in arvados-api-server
242 if [ -e /root/.psqlrc ]; then
243   if ! ( grep 'pset pager off' /root/.psqlrc ); then
244     RESTORE_PSQL="yes"
245     cp /root/.psqlrc /root/.psqlrc.provision.backup
246   fi
247 else
248   DELETE_PSQL="yes"
249 fi
250
251 echo '\pset pager off' >> /root/.psqlrc
252 # END FIXME! #16992 Temporary fix for psql call in arvados-api-server
253
254 # Now run the install
255 salt-call --local state.apply -l ${LOG_LEVEL}
256
257 # FIXME! #16992 Temporary fix for psql call in arvados-api-server
258 if [ "x${DELETE_PSQL}" = "xyes" ]; then
259   echo "Removing .psql file"
260   rm /root/.psqlrc
261 fi
262
263 if [ "x${RESTORE_PSQL}" = "xyes" ]; then
264   echo "Restoring .psql file"
265   mv -v /root/.psqlrc.provision.backup /root/.psqlrc
266 fi
267 # END FIXME! #16992 Temporary fix for psql call in arvados-api-server
268
269 # Leave a copy of the Arvados CA so the user can copy it where it's required
270 echo "Copying the Arvados CA certificate to the installer dir, so you can import it"
271 # If running in a vagrant VM, also add default user to docker group
272 if [ "x${VAGRANT}" = "xyes" ]; then
273   cp /etc/ssl/certs/arvados-snakeoil-ca.pem /vagrant
274
275   echo "Adding the vagrant user to the docker group"
276   usermod -a -G docker vagrant
277 else
278   cp /etc/ssl/certs/arvados-snakeoil-ca.pem ${SCRIPT_DIR}
279 fi
280
281 # Test that the installation finished correctly
282 if [ "x${TEST}" = "xyes" ]; then
283   cd /tmp/cluster_tests
284   ./run-test.sh
285 fi