18663: add keepbalance support
[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 set -o pipefail
14
15 # capture the directory that the script is running from
16 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
17
18 usage() {
19   echo >&2
20   echo >&2 "Usage: ${0} [-h] [-h]"
21   echo >&2
22   echo >&2 "${0} options:"
23   echo >&2 "  -d, --debug                                 Run salt installation in debug mode"
24   echo >&2 "  -c <local.params>, --config <local.params>  Path to the local.params config file"
25   echo >&2 "  -t, --test                                  Test installation running a CWL workflow"
26   echo >&2 "  -r, --roles                                 List of Arvados roles to apply to the host, comma separated"
27   echo >&2 "                                              Possible values are:"
28   echo >&2 "                                                api"
29   echo >&2 "                                                controller"
30   echo >&2 "                                                dispatcher"
31   echo >&2 "                                                keepproxy"
32   echo >&2 "                                                keepbalance"
33   echo >&2 "                                                keepstore"
34   echo >&2 "                                                keepweb"
35   echo >&2 "                                                shell"
36   echo >&2 "                                                webshell"
37   echo >&2 "                                                websocket"
38   echo >&2 "                                                workbench"
39   echo >&2 "                                                workbench2"
40   echo >&2 "                                              Defaults to applying them all"
41   echo >&2 "  -h, --help                                  Display this help and exit"
42   echo >&2 "  --dump-config <dest_dir>                    Dumps the pillars and states to a directory"
43   echo >&2 "                                              This parameter does not perform any installation at all. It's"
44   echo >&2 "                                              intended to give you a parsed sot of configuration files so"
45   echo >&2 "                                              you can inspect them or use them in you Saltstack infrastructure."
46   echo >&2 "                                              It"
47   echo >&2 "                                                - parses the pillar and states templates,"
48   echo >&2 "                                                - downloads the helper formulas with their desired versions,"
49   echo >&2 "                                                - prepares the 'top.sls' files both for pillars and states"
50   echo >&2 "                                                  for the selected role/s"
51   echo >&2 "                                                - writes the resulting files into <dest_dir>"
52   echo >&2 "  -v, --vagrant                               Run in vagrant and use the /vagrant shared dir"
53   echo >&2 "  --development                               Run in dev mode, using snakeoil certs"
54   echo >&2
55 }
56
57 arguments() {
58   # NOTE: This requires GNU getopt (part of the util-linux package on Debian-based distros).
59   if ! which getopt > /dev/null; then
60     echo >&2 "GNU getopt is required to run this script. Please install it and re-reun it"
61     exit 1
62   fi
63
64   TEMP=$(getopt -o c:dhp:r:tv \
65     --long config:,debug,development,dump-config:,help,roles:,test,vagrant \
66     -n "${0}" -- "${@}")
67
68   if [ ${?} != 0 ];
69     then echo "Please check the parameters you entered and re-run again"
70     exit 1
71   fi
72   # Note the quotes around `$TEMP': they are essential!
73   eval set -- "$TEMP"
74
75   while [ ${#} -ge 1 ]; do
76     case ${1} in
77       -c | --config)
78         CONFIG_FILE=${2}
79         shift 2
80         ;;
81       -d | --debug)
82         LOG_LEVEL="debug"
83         shift
84         set -x
85         ;;
86       --dump-config)
87         if [[ ${2} = /* ]]; then
88           DUMP_SALT_CONFIG_DIR=${2}
89         else
90           DUMP_SALT_CONFIG_DIR=${PWD}/${2}
91         fi
92         ## states
93         S_DIR="${DUMP_SALT_CONFIG_DIR}/salt"
94         ## formulas
95         F_DIR="${DUMP_SALT_CONFIG_DIR}/formulas"
96         ## pillars
97         P_DIR="${DUMP_SALT_CONFIG_DIR}/pillars"
98         ## tests
99         T_DIR="${DUMP_SALT_CONFIG_DIR}/tests"
100         DUMP_CONFIG="yes"
101         shift 2
102         ;;
103       --development)
104         DEV_MODE="yes"
105         shift 1
106         ;;
107       -r | --roles)
108         for i in ${2//,/ }
109           do
110             # Verify the role exists
111             if [[ ! "database,api,controller,keepstore,websocket,keepweb,workbench2,webshell,keepbalance,keepproxy,shell,workbench,dispatcher" == *"$i"* ]]; then
112               echo "The role '${i}' is not a valid role"
113               usage
114               exit 1
115             fi
116             ROLES="${ROLES} ${i}"
117           done
118           shift 2
119         ;;
120       -t | --test)
121         TEST="yes"
122         shift
123         ;;
124       -v | --vagrant)
125         VAGRANT="yes"
126         shift
127         ;;
128       --)
129         shift
130         break
131         ;;
132       *)
133         usage
134         exit 1
135         ;;
136     esac
137   done
138 }
139
140 copy_custom_cert() {
141   cert_dir=${1}
142   cert_name=${2}
143
144   mkdir -p /srv/salt/certs
145
146   if [ -f ${cert_dir}/${cert_name}.crt ]; then
147     cp -v ${cert_dir}/${cert_name}.crt /srv/salt/certs/arvados-${cert_name}.pem
148   else
149     echo "${cert_dir}/${cert_name}.crt does not exist. Exiting"
150     exit 1
151   fi
152   if [ -f ${cert_dir}/${cert_name}.key ]; then
153     cp -v ${cert_dir}/${cert_name}.key /srv/salt/certs/arvados-${cert_name}.key
154   else
155     echo "${cert_dir}/${cert_name}.key does not exist. Exiting"
156     exit 1
157   fi
158 }
159
160 DEV_MODE="no"
161 CONFIG_FILE="${SCRIPT_DIR}/local.params"
162 CONFIG_DIR="local_config_dir"
163 DUMP_CONFIG="no"
164 LOG_LEVEL="info"
165 CONTROLLER_EXT_SSL_PORT=443
166 TESTS_DIR="tests"
167
168 CLUSTER=""
169 DOMAIN=""
170
171 # Hostnames/IPs used for single-host deploys
172 IP_INT="127.0.1.1"
173
174 # Initial user setup
175 INITIAL_USER=""
176 INITIAL_USER_EMAIL=""
177 INITIAL_USER_PASSWORD=""
178
179 CONTROLLER_EXT_SSL_PORT=8000
180 KEEP_EXT_SSL_PORT=25101
181 # Both for collections and downloads
182 KEEPWEB_EXT_SSL_PORT=9002
183 WEBSHELL_EXT_SSL_PORT=4202
184 WEBSOCKET_EXT_SSL_PORT=8002
185 WORKBENCH1_EXT_SSL_PORT=443
186 WORKBENCH2_EXT_SSL_PORT=3001
187
188 SSL_MODE="self-signed"
189 USE_LETSENCRYPT_ROUTE53="no"
190 CUSTOM_CERTS_DIR="${SCRIPT_DIR}/certs"
191
192 ## These are ARVADOS-related parameters
193 # For a stable release, change RELEASE "production" and VERSION to the
194 # package version (including the iteration, e.g. X.Y.Z-1) of the
195 # release.
196 # The "local.params.example.*" files already set "RELEASE=production"
197 # to deploy  production-ready packages
198 RELEASE="development"
199 VERSION="latest"
200
201 # These are arvados-formula-related parameters
202 # An arvados-formula tag. For a stable release, this should be a
203 # branch name (e.g. X.Y-dev) or tag for the release.
204 # ARVADOS_TAG="2.2.0"
205 # BRANCH="main"
206
207 # Other formula versions we depend on
208 POSTGRES_TAG="v0.44.0"
209 NGINX_TAG="v2.8.0"
210 DOCKER_TAG="v2.4.0"
211 LOCALE_TAG="v0.3.4"
212 LETSENCRYPT_TAG="v2.1.0"
213
214 # Salt's dir
215 DUMP_SALT_CONFIG_DIR=""
216 ## states
217 S_DIR="/srv/salt"
218 ## formulas
219 F_DIR="/srv/formulas"
220 ## pillars
221 P_DIR="/srv/pillars"
222 ## tests
223 T_DIR="/tmp/cluster_tests"
224
225 arguments ${@}
226
227 if [ -s ${CONFIG_FILE} ]; then
228   source ${CONFIG_FILE}
229 else
230   echo >&2 "You don't seem to have a config file with initial values."
231   echo >&2 "Please create a '${CONFIG_FILE}' file as described in"
232   echo >&2 "  * https://doc.arvados.org/install/salt-single-host.html#single_host, or"
233   echo >&2 "  * https://doc.arvados.org/install/salt-multi-host.html#multi_host_multi_hostnames"
234   exit 1
235 fi
236
237 if [ ! -d ${CONFIG_DIR} ]; then
238   echo >&2 "You don't seem to have a config directory with pillars and states."
239   echo >&2 "Please create a '${CONFIG_DIR}' directory (as configured in your '${CONFIG_FILE}'). Please see"
240   echo >&2 "  * https://doc.arvados.org/install/salt-single-host.html#single_host, or"
241   echo >&2 "  * https://doc.arvados.org/install/salt-multi-host.html#multi_host_multi_hostnames"
242   exit 1
243 fi
244
245 if grep -q 'fixme_or_this_wont_work' ${CONFIG_FILE} ; then
246   echo >&2 "The config file ${CONFIG_FILE} has some parameters that need to be modified."
247   echo >&2 "Please, fix them and re-run the provision script."
248   exit 1
249 fi
250
251 if ! grep -qE '^[[:alnum:]]{5}$' <<<${CLUSTER} ; then
252   echo >&2 "ERROR: <CLUSTER> must be exactly 5 alphanumeric characters long"
253   echo >&2 "Fix the cluster name in the 'local.params' file and re-run the provision script"
254   exit 1
255 fi
256
257 # Only used in single_host/single_name deploys
258 if [ ! -z "${HOSTNAME_EXT}" ] ; then
259   # We need to add some extra control vars to manage a single certificate vs. multiple
260   USE_SINGLE_HOSTNAME="yes"
261   # Make sure that the value configured as IP_INT is a real IP on the system.
262   # If we don't error out early here when there is a mismatch, the formula will
263   # fail with hard to interpret nginx errors later on.
264   ip addr list |grep -q " ${IP_INT}/"
265   if [[ $? -ne 0 ]]; then
266     echo "Unable to find the IP_INT address '${IP_INT}' on the system, please correct the value in local.params. Exiting..."
267     exit 1
268   fi
269 else
270   USE_SINGLE_HOSTNAME="no"
271   # We set this variable, anyway, so sed lines do not fail and we don't need to add more
272   # conditionals
273   HOSTNAME_EXT="${CLUSTER}.${DOMAIN}"
274 fi
275
276 if [ "${DUMP_CONFIG}" = "yes" ]; then
277   echo "The provision installer will just dump a config under ${DUMP_SALT_CONFIG_DIR} and exit"
278 else
279   # Install a few dependency packages
280   # First, let's figure out the OS we're working on
281   OS_ID=$(grep ^ID= /etc/os-release |cut -f 2 -d=  |cut -f 2 -d \")
282   echo "Detected distro: ${OS_ID}"
283
284   case ${OS_ID} in
285     "centos")
286       echo "WARNING! Disabling SELinux, see https://dev.arvados.org/issues/18019"
287       sed -i 's/SELINUX=enforcing/SELINUX=permissive' /etc/sysconfig/selinux
288       setenforce permissive
289       yum install -y  curl git jq
290       ;;
291     "debian"|"ubuntu")
292       DEBIAN_FRONTEND=noninteractive apt update
293       DEBIAN_FRONTEND=noninteractive apt install -y curl git jq
294       ;;
295   esac
296
297   if which salt-call; then
298     echo "Salt already installed"
299   else
300     curl -L https://bootstrap.saltstack.com -o /tmp/bootstrap_salt.sh
301     sh /tmp/bootstrap_salt.sh -XdfP -x python3
302     /bin/systemctl stop salt-minion.service
303     /bin/systemctl disable salt-minion.service
304   fi
305
306   # Set salt to masterless mode
307   cat > /etc/salt/minion << EOFSM
308 failhard: "True"
309
310 file_client: local
311 file_roots:
312   base:
313     - ${S_DIR}
314     - ${F_DIR}/*
315
316 pillar_roots:
317   base:
318     - ${P_DIR}
319 EOFSM
320 fi
321
322 mkdir -p ${S_DIR} ${F_DIR} ${P_DIR} ${T_DIR}
323
324 # Get the formula and dependencies
325 cd ${F_DIR} || exit 1
326 echo "Cloning formulas"
327 rm -rf ${F_DIR}/* || exit 1
328 git clone --quiet https://github.com/saltstack-formulas/docker-formula.git ${F_DIR}/docker
329 ( cd docker && git checkout --quiet tags/"${DOCKER_TAG}" -b "${DOCKER_TAG}" )
330
331 echo "...locale"
332 git clone --quiet https://github.com/saltstack-formulas/locale-formula.git ${F_DIR}/locale
333 ( cd locale && git checkout --quiet tags/"${LOCALE_TAG}" -b "${LOCALE_TAG}" )
334
335 echo "...nginx"
336 git clone --quiet https://github.com/saltstack-formulas/nginx-formula.git ${F_DIR}/nginx
337 ( cd nginx && git checkout --quiet tags/"${NGINX_TAG}" -b "${NGINX_TAG}" )
338
339 echo "...postgres"
340 git clone --quiet https://github.com/saltstack-formulas/postgres-formula.git ${F_DIR}/postgres
341 ( cd postgres && git checkout --quiet tags/"${POSTGRES_TAG}" -b "${POSTGRES_TAG}" )
342
343 echo "...letsencrypt"
344 git clone --quiet https://github.com/saltstack-formulas/letsencrypt-formula.git ${F_DIR}/letsencrypt
345 ( cd letsencrypt && git checkout --quiet tags/"${LETSENCRYPT_TAG}" -b "${LETSENCRYPT_TAG}" )
346
347 echo "...arvados"
348 git clone --quiet https://git.arvados.org/arvados-formula.git ${F_DIR}/arvados
349
350 # If we want to try a specific branch of the formula
351 if [ "x${BRANCH}" != "x" ]; then
352   ( cd ${F_DIR}/arvados && git checkout --quiet -t origin/"${BRANCH}" -b "${BRANCH}" )
353 elif [ "x${ARVADOS_TAG}" != "x" ]; then
354 ( cd ${F_DIR}/arvados && git checkout --quiet tags/"${ARVADOS_TAG}" -b "${ARVADOS_TAG}" )
355 fi
356
357 if [ "x${VAGRANT}" = "xyes" ]; then
358   EXTRA_STATES_DIR="/home/vagrant/${CONFIG_DIR}/states"
359   SOURCE_PILLARS_DIR="/home/vagrant/${CONFIG_DIR}/pillars"
360   SOURCE_TESTS_DIR="/home/vagrant/${TESTS_DIR}"
361 else
362   EXTRA_STATES_DIR="${SCRIPT_DIR}/${CONFIG_DIR}/states"
363   SOURCE_PILLARS_DIR="${SCRIPT_DIR}/${CONFIG_DIR}/pillars"
364   SOURCE_TESTS_DIR="${SCRIPT_DIR}/${TESTS_DIR}"
365 fi
366
367 SOURCE_STATES_DIR="${EXTRA_STATES_DIR}"
368
369 echo "Writing pillars and states"
370
371 # Replace variables (cluster,  domain, etc) in the pillars, states and tests
372 # to ease deployment for newcomers
373 if [ ! -d "${SOURCE_PILLARS_DIR}" ]; then
374   echo "${SOURCE_PILLARS_DIR} does not exist or is not a directory. Exiting."
375   exit 1
376 fi
377 for f in $(ls "${SOURCE_PILLARS_DIR}"/*); do
378   sed "s#__ANONYMOUS_USER_TOKEN__#${ANONYMOUS_USER_TOKEN}#g;
379        s#__BLOB_SIGNING_KEY__#${BLOB_SIGNING_KEY}#g;
380        s#__CONTROLLER_EXT_SSL_PORT__#${CONTROLLER_EXT_SSL_PORT}#g;
381        s#__CLUSTER__#${CLUSTER}#g;
382        s#__DOMAIN__#${DOMAIN}#g;
383        s#__HOSTNAME_EXT__#${HOSTNAME_EXT}#g;
384        s#__IP_INT__#${IP_INT}#g;
385        s#__INITIAL_USER_EMAIL__#${INITIAL_USER_EMAIL}#g;
386        s#__INITIAL_USER_PASSWORD__#${INITIAL_USER_PASSWORD}#g;
387        s#__INITIAL_USER__#${INITIAL_USER}#g;
388        s#__LE_AWS_REGION__#${LE_AWS_REGION}#g;
389        s#__LE_AWS_SECRET_ACCESS_KEY__#${LE_AWS_SECRET_ACCESS_KEY}#g;
390        s#__LE_AWS_ACCESS_KEY_ID__#${LE_AWS_ACCESS_KEY_ID}#g;
391        s#__DATABASE_PASSWORD__#${DATABASE_PASSWORD}#g;
392        s#__KEEPWEB_EXT_SSL_PORT__#${KEEPWEB_EXT_SSL_PORT}#g;
393        s#__KEEP_EXT_SSL_PORT__#${KEEP_EXT_SSL_PORT}#g;
394        s#__MANAGEMENT_TOKEN__#${MANAGEMENT_TOKEN}#g;
395        s#__RELEASE__#${RELEASE}#g;
396        s#__SYSTEM_ROOT_TOKEN__#${SYSTEM_ROOT_TOKEN}#g;
397        s#__VERSION__#${VERSION}#g;
398        s#__WEBSHELL_EXT_SSL_PORT__#${WEBSHELL_EXT_SSL_PORT}#g;
399        s#__WEBSOCKET_EXT_SSL_PORT__#${WEBSOCKET_EXT_SSL_PORT}#g;
400        s#__WORKBENCH1_EXT_SSL_PORT__#${WORKBENCH1_EXT_SSL_PORT}#g;
401        s#__WORKBENCH2_EXT_SSL_PORT__#${WORKBENCH2_EXT_SSL_PORT}#g;
402        s#__CLUSTER_INT_CIDR__#${CLUSTER_INT_CIDR}#g;
403        s#__CONTROLLER_INT_IP__#${CONTROLLER_INT_IP}#g;
404        s#__WEBSOCKET_INT_IP__#${WEBSOCKET_INT_IP}#g;
405        s#__KEEP_INT_IP__#${KEEP_INT_IP}#g;
406        s#__KEEPSTORE0_INT_IP__#${KEEPSTORE0_INT_IP}#g;
407        s#__KEEPSTORE1_INT_IP__#${KEEPSTORE1_INT_IP}#g;
408        s#__KEEPWEB_INT_IP__#${KEEPWEB_INT_IP}#g;
409        s#__WEBSHELL_INT_IP__#${WEBSHELL_INT_IP}#g;
410        s#__SHELL_INT_IP__#${SHELL_INT_IP}#g;
411        s#__WORKBENCH1_INT_IP__#${WORKBENCH1_INT_IP}#g;
412        s#__WORKBENCH2_INT_IP__#${WORKBENCH2_INT_IP}#g;
413        s#__DATABASE_INT_IP__#${DATABASE_INT_IP}#g;
414        s#__WORKBENCH_SECRET_KEY__#${WORKBENCH_SECRET_KEY}#g" \
415   "${f}" > "${P_DIR}"/$(basename "${f}")
416 done
417
418 if [ "x${TEST}" = "xyes" ] && [ ! -d "${SOURCE_TESTS_DIR}" ]; then
419   echo "You requested to run tests, but ${SOURCE_TESTS_DIR} does not exist or is not a directory. Exiting."
420   exit 1
421 fi
422 mkdir -p ${T_DIR}
423 # Replace cluster and domain name in the test files
424 for f in $(ls "${SOURCE_TESTS_DIR}"/*); do
425   FILTERS="s#__CLUSTER__#${CLUSTER}#g;
426        s#__CONTROLLER_EXT_SSL_PORT__#${CONTROLLER_EXT_SSL_PORT}#g;
427        s#__DOMAIN__#${DOMAIN}#g;
428        s#__IP_INT__#${IP_INT}#g;
429        s#__INITIAL_USER_EMAIL__#${INITIAL_USER_EMAIL}#g;
430        s#__INITIAL_USER_PASSWORD__#${INITIAL_USER_PASSWORD}#g
431        s#__INITIAL_USER__#${INITIAL_USER}#g;
432        s#__DATABASE_PASSWORD__#${DATABASE_PASSWORD}#g;
433        s#__SYSTEM_ROOT_TOKEN__#${SYSTEM_ROOT_TOKEN}#g"
434   if [ "$USE_SINGLE_HOSTNAME" = "yes" ]; then
435     FILTERS="s#__CLUSTER__.__DOMAIN__#${HOSTNAME_EXT}#g;
436        $FILTERS"
437   fi
438   sed "$FILTERS" \
439     "${f}" > ${T_DIR}/$(basename "${f}")
440 done
441 chmod 755 ${T_DIR}/run-test.sh
442
443 # Replace helper state files that differ from the formula's examples
444 if [ -d "${SOURCE_STATES_DIR}" ]; then
445   mkdir -p "${F_DIR}"/extra/extra
446
447   for f in $(ls "${SOURCE_STATES_DIR}"/*); do
448     sed "s#__ANONYMOUS_USER_TOKEN__#${ANONYMOUS_USER_TOKEN}#g;
449          s#__CLUSTER__#${CLUSTER}#g;
450          s#__BLOB_SIGNING_KEY__#${BLOB_SIGNING_KEY}#g;
451          s#__CONTROLLER_EXT_SSL_PORT__#${CONTROLLER_EXT_SSL_PORT}#g;
452          s#__DOMAIN__#${DOMAIN}#g;
453          s#__HOSTNAME_EXT__#${HOSTNAME_EXT}#g;
454          s#__IP_INT__#${IP_INT}#g;
455          s#__INITIAL_USER_EMAIL__#${INITIAL_USER_EMAIL}#g;
456          s#__INITIAL_USER_PASSWORD__#${INITIAL_USER_PASSWORD}#g;
457          s#__INITIAL_USER__#${INITIAL_USER}#g;
458          s#__DATABASE_PASSWORD__#${DATABASE_PASSWORD}#g;
459          s#__KEEPWEB_EXT_SSL_PORT__#${KEEPWEB_EXT_SSL_PORT}#g;
460          s#__KEEP_EXT_SSL_PORT__#${KEEP_EXT_SSL_PORT}#g;
461          s#__MANAGEMENT_TOKEN__#${MANAGEMENT_TOKEN}#g;
462          s#__RELEASE__#${RELEASE}#g;
463          s#__SYSTEM_ROOT_TOKEN__#${SYSTEM_ROOT_TOKEN}#g;
464          s#__VERSION__#${VERSION}#g;
465          s#__CLUSTER_INT_CIDR__#${CLUSTER_INT_CIDR}#g;
466          s#__CONTROLLER_INT_IP__#${CONTROLLER_INT_IP}#g;
467          s#__WEBSOCKET_INT_IP__#${WEBSOCKET_INT_IP}#g;
468          s#__KEEP_INT_IP__#${KEEP_INT_IP}#g;
469          s#__KEEPSTORE0_INT_IP__#${KEEPSTORE0_INT_IP}#g;
470          s#__KEEPSTORE1_INT_IP__#${KEEPSTORE1_INT_IP}#g;
471          s#__KEEPWEB_INT_IP__#${KEEPWEB_INT_IP}#g;
472          s#__WEBSHELL_INT_IP__#${WEBSHELL_INT_IP}#g;
473          s#__WORKBENCH1_INT_IP__#${WORKBENCH1_INT_IP}#g;
474          s#__WORKBENCH2_INT_IP__#${WORKBENCH2_INT_IP}#g;
475          s#__DATABASE_INT_IP__#${DATABASE_INT_IP}#g;
476          s#__WEBSHELL_EXT_SSL_PORT__#${WEBSHELL_EXT_SSL_PORT}#g;
477          s#__WEBSOCKET_EXT_SSL_PORT__#${WEBSOCKET_EXT_SSL_PORT}#g;
478          s#__WORKBENCH1_EXT_SSL_PORT__#${WORKBENCH1_EXT_SSL_PORT}#g;
479          s#__WORKBENCH2_EXT_SSL_PORT__#${WORKBENCH2_EXT_SSL_PORT}#g;
480          s#__WORKBENCH_SECRET_KEY__#${WORKBENCH_SECRET_KEY}#g" \
481     "${f}" > "${F_DIR}/extra/extra"/$(basename "${f}")
482   done
483 fi
484
485 # Now, we build the SALT states/pillars trees
486 # As we need to separate both states and pillars in case we want specific
487 # roles, we iterate on both at the same time
488
489 # States
490 cat > ${S_DIR}/top.sls << EOFTSLS
491 base:
492   '*':
493     - locale
494 EOFTSLS
495
496 # Pillars
497 cat > ${P_DIR}/top.sls << EOFPSLS
498 base:
499   '*':
500     - locale
501     - arvados
502 EOFPSLS
503
504 # States, extra states
505 if [ -d "${F_DIR}"/extra/extra ]; then
506   SKIP_SNAKE_OIL="snakeoil_certs"
507
508   if [[ "$DEV_MODE" = "yes" || "${SSL_MODE}" == "self-signed" ]] ; then
509     # In dev mode, we create some snake oil certs that we'll
510     # use as CUSTOM_CERTS, so we don't skip the states file.
511     # Same when using self-signed certificates.
512     SKIP_SNAKE_OIL="dont_add_snakeoil_certs"
513   fi
514   for f in $(ls "${F_DIR}"/extra/extra/*.sls | grep -v ${SKIP_SNAKE_OIL}); do
515   echo "    - extra.$(basename ${f} | sed 's/.sls$//g')" >> ${S_DIR}/top.sls
516   done
517   # Use byo or self-signed certificates
518   if [ "${SSL_MODE}" != "lets-encrypt" ]; then
519     mkdir -p "${F_DIR}"/extra/extra/files
520   fi
521 fi
522
523 # If we want specific roles for a node, just add the desired states
524 # and its dependencies
525 if [ -z "${ROLES}" ]; then
526   # States
527   echo "    - nginx.passenger" >> ${S_DIR}/top.sls
528   if [ "${SSL_MODE}" = "lets-encrypt" ]; then
529     if [ "${USE_LETSENCRYPT_ROUTE53}" = "yes" ]; then
530       grep -q "aws_credentials" ${S_DIR}/top.sls || echo "    - extra.aws_credentials" >> ${S_DIR}/top.sls
531     fi
532     grep -q "letsencrypt"     ${S_DIR}/top.sls || echo "    - letsencrypt" >> ${S_DIR}/top.sls
533   else
534     # Use custom certs, as both bring-your-own and self-signed are copied using this state
535     # Copy certs to formula extra/files
536     # In dev mode, the files will be created and put in the destination directory by the
537     # snakeoil_certs.sls state file
538     mkdir -p /srv/salt/certs
539     cp -rv ${CUSTOM_CERTS_DIR}/* /srv/salt/certs/
540     # We add the custom_certs state
541     grep -q "custom_certs"    ${S_DIR}/top.sls || echo "    - extra.custom_certs" >> ${S_DIR}/top.sls
542   fi
543
544   echo "    - postgres" >> ${S_DIR}/top.sls
545   echo "    - docker.software" >> ${S_DIR}/top.sls
546   echo "    - arvados" >> ${S_DIR}/top.sls
547
548   # Pillars
549   echo "    - docker" >> ${P_DIR}/top.sls
550   echo "    - nginx_api_configuration" >> ${P_DIR}/top.sls
551   echo "    - nginx_controller_configuration" >> ${P_DIR}/top.sls
552   echo "    - nginx_keepproxy_configuration" >> ${P_DIR}/top.sls
553   echo "    - nginx_keepweb_configuration" >> ${P_DIR}/top.sls
554   echo "    - nginx_passenger" >> ${P_DIR}/top.sls
555   echo "    - nginx_websocket_configuration" >> ${P_DIR}/top.sls
556   echo "    - nginx_webshell_configuration" >> ${P_DIR}/top.sls
557   echo "    - nginx_workbench2_configuration" >> ${P_DIR}/top.sls
558   echo "    - nginx_workbench_configuration" >> ${P_DIR}/top.sls
559   echo "    - postgresql" >> ${P_DIR}/top.sls
560
561   if [ "${SSL_MODE}" = "lets-encrypt" ]; then
562     if [ "${USE_LETSENCRYPT_ROUTE53}" = "yes" ]; then
563       grep -q "aws_credentials" ${P_DIR}/top.sls || echo "    - aws_credentials" >> ${P_DIR}/top.sls
564     fi
565     grep -q "letsencrypt" ${P_DIR}/top.sls || echo "    - letsencrypt" >> ${P_DIR}/top.sls
566
567     # As the pillar differ whether we use LE or custom certs, we need to do a final edition on them
568     for c in controller websocket workbench workbench2 webshell download collections keepproxy; do
569       sed -i "s/__CERT_REQUIRES__/cmd: create-initial-cert-${c}.${CLUSTER}.${DOMAIN}*/g;
570               s#__CERT_PEM__#/etc/letsencrypt/live/${c}.${CLUSTER}.${DOMAIN}/fullchain.pem#g;
571               s#__CERT_KEY__#/etc/letsencrypt/live/${c}.${CLUSTER}.${DOMAIN}/privkey.pem#g" \
572       ${P_DIR}/nginx_${c}_configuration.sls
573     done
574   else
575     # Use custom certs (either dev mode or prod)
576     grep -q "extra_custom_certs" ${P_DIR}/top.sls || echo "    - extra_custom_certs" >> ${P_DIR}/top.sls
577     # And add the certs in the custom_certs pillar
578     echo "extra_custom_certs_dir: /srv/salt/certs" > ${P_DIR}/extra_custom_certs.sls
579     echo "extra_custom_certs:" >> ${P_DIR}/extra_custom_certs.sls
580
581     for c in controller websocket workbench workbench2 webshell keepweb keepproxy; do
582       # Are we in a single-host-single-hostname env?
583       if [ "${USE_SINGLE_HOSTNAME}" = "yes" ]; then
584         # Are we in a single-host-single-hostname env?
585         CERT_NAME=${HOSTNAME_EXT}
586       else
587         # We are in a multiple-hostnames env
588         CERT_NAME=${c}
589       fi
590
591       if [[ "$SSL_MODE" == "bring-your-own" ]]; then
592         copy_custom_cert ${CUSTOM_CERTS_DIR} ${CERT_NAME}
593       fi
594
595       grep -q ${CERT_NAME} ${P_DIR}/extra_custom_certs.sls || echo "  - ${CERT_NAME}" >> ${P_DIR}/extra_custom_certs.sls
596
597       # As the pillar differs whether we use LE or custom certs, we need to do a final edition on them
598       sed -i "s/__CERT_REQUIRES__/file: extra_custom_certs_file_copy_arvados-${CERT_NAME}.pem/g;
599               s#__CERT_PEM__#/etc/nginx/ssl/arvados-${CERT_NAME}.pem#g;
600               s#__CERT_KEY__#/etc/nginx/ssl/arvados-${CERT_NAME}.key#g" \
601       ${P_DIR}/nginx_${c}_configuration.sls
602     done
603   fi
604 else
605   # If we add individual roles, make sure we add the repo first
606   echo "    - arvados.repo" >> ${S_DIR}/top.sls
607   # We add the extra_custom_certs state
608   grep -q "extra.custom_certs"    ${S_DIR}/top.sls || echo "    - extra.custom_certs" >> ${S_DIR}/top.sls
609
610   # And we add the basic part for the certs pillar
611   if [ "${SSL_MODE}" != "lets-encrypt" ]; then
612     # And add the certs in the custom_certs pillar
613     echo "extra_custom_certs_dir: /srv/salt/certs" > ${P_DIR}/extra_custom_certs.sls
614     echo "extra_custom_certs:" >> ${P_DIR}/extra_custom_certs.sls
615     grep -q "extra_custom_certs" ${P_DIR}/top.sls || echo "    - extra_custom_certs" >> ${P_DIR}/top.sls
616   fi
617
618   for R in ${ROLES}; do
619     case "${R}" in
620       "database")
621         # States
622         echo "    - postgres" >> ${S_DIR}/top.sls
623         # Pillars
624         echo '    - postgresql' >> ${P_DIR}/top.sls
625       ;;
626       "api")
627         # States
628         # FIXME: https://dev.arvados.org/issues/17352
629         grep -q "postgres.client" ${S_DIR}/top.sls || echo "    - postgres.client" >> ${S_DIR}/top.sls
630         grep -q "nginx.passenger" ${S_DIR}/top.sls || echo "    - nginx.passenger" >> ${S_DIR}/top.sls
631         ### If we don't install and run LE before arvados-api-server, it fails and breaks everything
632         ### after it. So we add this here as we are, after all, sharing the host for api and controller
633         # Currently, only available on config_examples/multi_host/aws
634         if [ "${SSL_MODE}" = "lets-encrypt" ]; then
635           if [ "${USE_LETSENCRYPT_ROUTE53}" = "yes" ]; then
636             grep -q "aws_credentials" ${S_DIR}/top.sls || echo "    - aws_credentials" >> ${S_DIR}/top.sls
637           fi
638           grep -q "letsencrypt" ${S_DIR}/top.sls || echo "    - letsencrypt" >> ${S_DIR}/top.sls
639         else
640           # Use custom certs
641           if [ "${SSL_MODE}" = "bring-your-own" ]; then
642             copy_custom_cert ${CUSTOM_CERTS_DIR} controller
643           fi
644           grep -q controller ${P_DIR}/extra_custom_certs.sls || echo "  - controller" >> ${P_DIR}/extra_custom_certs.sls
645         fi
646         grep -q "arvados.${R}" ${S_DIR}/top.sls    || echo "    - arvados.${R}" >> ${S_DIR}/top.sls
647         # Pillars
648         grep -q "aws_credentials" ${P_DIR}/top.sls          || echo "    - aws_credentials" >> ${P_DIR}/top.sls
649         grep -q "postgresql" ${P_DIR}/top.sls               || echo "    - postgresql" >> ${P_DIR}/top.sls
650         grep -q "nginx_passenger" ${P_DIR}/top.sls          || echo "    - nginx_passenger" >> ${P_DIR}/top.sls
651         grep -q "nginx_${R}_configuration" ${P_DIR}/top.sls || echo "    - nginx_${R}_configuration" >> ${P_DIR}/top.sls
652       ;;
653       "controller" | "websocket" | "workbench" | "workbench2" | "webshell" | "keepweb" | "keepproxy")
654         # States
655         grep -q "nginx.passenger" ${S_DIR}/top.sls || echo "    - nginx.passenger" >> ${S_DIR}/top.sls
656         # Currently, only available on config_examples/multi_host/aws
657         if [ "${SSL_MODE}" = "lets-encrypt" ]; then
658           if [ "x${USE_LETSENCRYPT_ROUTE53}" = "xyes" ]; then
659             grep -q "aws_credentials" ${S_DIR}/top.sls || echo "    - aws_credentials" >> ${S_DIR}/top.sls
660           fi
661           grep -q "letsencrypt"     ${S_DIR}/top.sls || echo "    - letsencrypt" >> ${S_DIR}/top.sls
662         else
663           # Use custom certs, special case for keepweb
664           if [ ${R} = "keepweb" ]; then
665             if [ "${SSL_MODE}" = "bring-your-own" ]; then
666               copy_custom_cert ${CUSTOM_CERTS_DIR} download
667               copy_custom_cert ${CUSTOM_CERTS_DIR} collections
668             fi
669           else
670             if [ "${SSL_MODE}" = "bring-your-own" ]; then
671               copy_custom_cert ${CUSTOM_CERTS_DIR} ${R}
672             fi
673           fi
674         fi
675         # webshell role is just a nginx vhost, so it has no state
676         if [ "${R}" != "webshell" ]; then
677           grep -q "arvados.${R}" ${S_DIR}/top.sls || echo "    - arvados.${R}" >> ${S_DIR}/top.sls
678         fi
679         # Pillars
680         grep -q "nginx_passenger" ${P_DIR}/top.sls          || echo "    - nginx_passenger" >> ${P_DIR}/top.sls
681         grep -q "nginx_${R}_configuration" ${P_DIR}/top.sls || echo "    - nginx_${R}_configuration" >> ${P_DIR}/top.sls
682         # Special case for keepweb
683         if [ ${R} = "keepweb" ]; then
684           grep -q "nginx_download_configuration" ${P_DIR}/top.sls || echo "    - nginx_download_configuration" >> ${P_DIR}/top.sls
685           grep -q "nginx_collections_configuration" ${P_DIR}/top.sls || echo "    - nginx_collections_configuration" >> ${P_DIR}/top.sls
686         fi
687
688         # Currently, only available on config_examples/multi_host/aws
689         if [ "${SSL_MODE}" = "lets-encrypt" ]; then
690           if [ "${USE_LETSENCRYPT_ROUTE53}" = "yes" ]; then
691             grep -q "aws_credentials" ${P_DIR}/top.sls || echo "    - aws_credentials" >> ${P_DIR}/top.sls
692           fi
693           grep -q "letsencrypt"     ${P_DIR}/top.sls || echo "    - letsencrypt" >> ${P_DIR}/top.sls
694           grep -q "letsencrypt_${R}_configuration" ${P_DIR}/top.sls || echo "    - letsencrypt_${R}_configuration" >> ${P_DIR}/top.sls
695
696           # As the pillar differ whether we use LE or custom certs, we need to do a final edition on them
697           # Special case for keepweb
698           if [ ${R} = "keepweb" ]; then
699             for kwsub in download collections; do
700               sed -i "s/__CERT_REQUIRES__/cmd: create-initial-cert-${kwsub}.${CLUSTER}.${DOMAIN}*/g;
701                       s#__CERT_PEM__#/etc/letsencrypt/live/${kwsub}.${CLUSTER}.${DOMAIN}/fullchain.pem#g;
702                       s#__CERT_KEY__#/etc/letsencrypt/live/${kwsub}.${CLUSTER}.${DOMAIN}/privkey.pem#g" \
703               ${P_DIR}/nginx_${kwsub}_configuration.sls
704             done
705           else
706             sed -i "s/__CERT_REQUIRES__/cmd: create-initial-cert-${R}.${CLUSTER}.${DOMAIN}*/g;
707                     s#__CERT_PEM__#/etc/letsencrypt/live/${R}.${CLUSTER}.${DOMAIN}/fullchain.pem#g;
708                     s#__CERT_KEY__#/etc/letsencrypt/live/${R}.${CLUSTER}.${DOMAIN}/privkey.pem#g" \
709             ${P_DIR}/nginx_${R}_configuration.sls
710           fi
711         else
712           # As the pillar differ whether we use LE or custom certs, we need to do a final edition on them
713           # Special case for keepweb
714           if [ ${R} = "keepweb" ]; then
715             for kwsub in download collections; do
716               sed -i "s/__CERT_REQUIRES__/file: extra_custom_certs_file_copy_arvados-${kwsub}.pem/g;
717                       s#__CERT_PEM__#/etc/nginx/ssl/arvados-${kwsub}.pem#g;
718                       s#__CERT_KEY__#/etc/nginx/ssl/arvados-${kwsub}.key#g" \
719               ${P_DIR}/nginx_${kwsub}_configuration.sls
720               grep -q ${kwsub} ${P_DIR}/extra_custom_certs.sls || echo "  - ${kwsub}" >> ${P_DIR}/extra_custom_certs.sls
721             done
722           else
723             sed -i "s/__CERT_REQUIRES__/file: extra_custom_certs_file_copy_arvados-${R}.pem/g;
724                     s#__CERT_PEM__#/etc/nginx/ssl/arvados-${R}.pem#g;
725                     s#__CERT_KEY__#/etc/nginx/ssl/arvados-${R}.key#g" \
726             ${P_DIR}/nginx_${R}_configuration.sls
727             grep -q ${R} ${P_DIR}/extra_custom_certs.sls || echo "  - ${R}" >> ${P_DIR}/extra_custom_certs.sls
728           fi
729         fi
730       ;;
731       "shell")
732         # States
733         grep -q "docker" ${S_DIR}/top.sls       || echo "    - docker.software" >> ${S_DIR}/top.sls
734         grep -q "arvados.${R}" ${S_DIR}/top.sls || echo "    - arvados.${R}" >> ${S_DIR}/top.sls
735         # Pillars
736         grep -q "docker" ${P_DIR}/top.sls       || echo "    - docker" >> ${P_DIR}/top.sls
737       ;;
738       "dispatcher" | "keepbalance" | "keepstore")
739         # States
740         grep -q "arvados.${R}" ${S_DIR}/top.sls || echo "    - arvados.${R}" >> ${S_DIR}/top.sls
741         # Pillars
742         # ATM, no specific pillar needed
743       ;;
744       *)
745         echo "Unknown role ${R}"
746         exit 1
747       ;;
748     esac
749   done
750 fi
751
752 if [ "${DUMP_CONFIG}" = "yes" ]; then
753   # We won't run the rest of the script because we're just dumping the config
754   exit 0
755 fi
756
757 # FIXME! #16992 Temporary fix for psql call in arvados-api-server
758 if [ -e /root/.psqlrc ]; then
759   if ! ( grep 'pset pager off' /root/.psqlrc ); then
760     RESTORE_PSQL="yes"
761     cp /root/.psqlrc /root/.psqlrc.provision.backup
762   fi
763 else
764   DELETE_PSQL="yes"
765 fi
766
767 echo '\pset pager off' >> /root/.psqlrc
768 # END FIXME! #16992 Temporary fix for psql call in arvados-api-server
769
770 # Now run the install
771 salt-call --local state.apply -l ${LOG_LEVEL}
772
773 # Finally, make sure that /etc/hosts is not overwritten on reboot
774 if [ -d /etc/cloud/cloud.cfg.d ]; then
775   # TODO: will this work on CentOS?
776   sed -i 's/^manage_etc_hosts: true/#manage_etc_hosts: true/g' /etc/cloud/cloud.cfg.d/*
777 fi
778
779 # FIXME! #16992 Temporary fix for psql call in arvados-api-server
780 if [ "x${DELETE_PSQL}" = "xyes" ]; then
781   echo "Removing .psql file"
782   rm /root/.psqlrc
783 fi
784
785 if [ "x${RESTORE_PSQL}" = "xyes" ]; then
786   echo "Restoring .psql file"
787   mv -v /root/.psqlrc.provision.backup /root/.psqlrc
788 fi
789 # END FIXME! #16992 Temporary fix for psql call in arvados-api-server
790
791 # Leave a copy of the Arvados CA so the user can copy it where it's required
792 if [ "$DEV_MODE" = "yes" ]; then
793   echo "Copying the Arvados CA certificate to the installer dir, so you can import it"
794   # If running in a vagrant VM, also add default user to docker group
795   if [ "x${VAGRANT}" = "xyes" ]; then
796     cp /etc/ssl/certs/arvados-snakeoil-ca.pem /vagrant/${CLUSTER}.${DOMAIN}-arvados-snakeoil-ca.pem
797
798     echo "Adding the vagrant user to the docker group"
799     usermod -a -G docker vagrant
800   else
801     cp /etc/ssl/certs/arvados-snakeoil-ca.pem ${SCRIPT_DIR}/${CLUSTER}.${DOMAIN}-arvados-snakeoil-ca.pem
802   fi
803 fi
804
805 # Test that the installation finished correctly
806 if [ "x${TEST}" = "xyes" ]; then
807   cd ${T_DIR}
808   # If we use RVM, we need to run this with it, or most ruby commands will fail
809   RVM_EXEC=""
810   if [ -x /usr/local/rvm/bin/rvm-exec ]; then
811     RVM_EXEC="/usr/local/rvm/bin/rvm-exec"
812   fi
813   ${RVM_EXEC} ./run-test.sh
814 fi