Merge branch '20680-cores-concurrency' refs #20680
[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 -eu
14 set -o pipefail
15
16 # capture the directory that the script is running from
17 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
18
19 usage() {
20   echo >&2
21   echo >&2 "Usage: ${0} [-h] [-h]"
22   echo >&2
23   echo >&2 "${0} options:"
24   echo >&2 "  -d, --debug                                 Run salt installation in debug mode"
25   echo >&2 "  -c <local.params>, --config <local.params>  Path to the local.params config file"
26   echo >&2 "  -t, --test                                  Test installation running a CWL workflow"
27   echo >&2 "  -r, --roles                                 List of Arvados roles to apply to the host, comma separated"
28   echo >&2 "                                              Possible values are:"
29   echo >&2 "                                                balancer"
30   echo >&2 "                                                controller"
31   echo >&2 "                                                dispatcher"
32   echo >&2 "                                                keepproxy"
33   echo >&2 "                                                keepbalance"
34   echo >&2 "                                                keepstore"
35   echo >&2 "                                                keepweb"
36   echo >&2 "                                                monitoring"
37   echo >&2 "                                                shell"
38   echo >&2 "                                                webshell"
39   echo >&2 "                                                websocket"
40   echo >&2 "                                                workbench"
41   echo >&2 "                                                workbench2"
42   echo >&2 "                                              Defaults to applying them all"
43   echo >&2 "  -h, --help                                  Display this help and exit"
44   echo >&2 "  --dump-config <dest_dir>                    Dumps the pillars and states to a directory"
45   echo >&2 "                                              This parameter does not perform any installation at all. It's"
46   echo >&2 "                                              intended to give you a parsed set of configuration files so"
47   echo >&2 "                                              you can inspect them or use them in you Saltstack infrastructure."
48   echo >&2 "                                              It"
49   echo >&2 "                                                - parses the pillar and states templates,"
50   echo >&2 "                                                - downloads the helper formulas with their desired versions,"
51   echo >&2 "                                                - prepares the 'top.sls' files both for pillars and states"
52   echo >&2 "                                                  for the selected role(s)"
53   echo >&2 "                                                - writes the resulting files into <dest_dir>"
54   echo >&2 "  -v, --vagrant                               Run in vagrant and use the /vagrant shared dir"
55   echo >&2 "  --development                               Run in dev mode, using snakeoil certs"
56   echo >&2
57 }
58
59 arguments() {
60   # NOTE: This requires GNU getopt (part of the util-linux package on Debian-based distros).
61   if ! which getopt > /dev/null; then
62     echo >&2 "GNU getopt is required to run this script. Please install it and re-reun it"
63     exit 1
64   fi
65
66   TEMP=$(getopt -o c:dhp:r:tv \
67     --long config:,debug,development,dump-config:,help,roles:,test,vagrant \
68     -n "${0}" -- "${@}")
69
70   if [ ${?} != 0 ];
71     then echo "Please check the parameters you entered and re-run again"
72     exit 1
73   fi
74   # Note the quotes around `$TEMP': they are essential!
75   eval set -- "$TEMP"
76
77   while [ ${#} -ge 1 ]; do
78     case ${1} in
79       -c | --config)
80         CONFIG_FILE=${2}
81         shift 2
82         ;;
83       -d | --debug)
84         LOG_LEVEL="debug"
85         shift
86         set -x
87         ;;
88       --dump-config)
89         if [[ ${2} = /* ]]; then
90           DUMP_SALT_CONFIG_DIR=${2}
91         else
92           DUMP_SALT_CONFIG_DIR=${PWD}/${2}
93         fi
94         ## states
95         S_DIR="${DUMP_SALT_CONFIG_DIR}/salt"
96         ## formulas
97         F_DIR="${DUMP_SALT_CONFIG_DIR}/formulas"
98         ## pillars
99         P_DIR="${DUMP_SALT_CONFIG_DIR}/pillars"
100         ## tests
101         T_DIR="${DUMP_SALT_CONFIG_DIR}/tests"
102         DUMP_CONFIG="yes"
103         shift 2
104         ;;
105       --development)
106         DEV_MODE="yes"
107         shift 1
108         ;;
109       -r | --roles)
110         for i in ${2//,/ }
111           do
112             # Verify the role exists
113             if [[ ! "database,balancer,controller,keepstore,websocket,keepweb,workbench2,webshell,keepbalance,keepproxy,shell,workbench,dispatcher,monitoring" == *"$i"* ]]; then
114               echo "The role '${i}' is not a valid role"
115               usage
116               exit 1
117             fi
118             ROLES="${ROLES:-} ${i}"
119           done
120           shift 2
121         ;;
122       -t | --test)
123         TEST="yes"
124         shift
125         ;;
126       -v | --vagrant)
127         VAGRANT="yes"
128         shift
129         ;;
130       --)
131         shift
132         break
133         ;;
134       *)
135         usage
136         exit 1
137         ;;
138     esac
139   done
140 }
141
142 copy_custom_cert() {
143   cert_dir=${1}
144   cert_name=${2}
145
146   mkdir -p --mode=0700 /srv/salt/certs
147
148   if [ -f ${cert_dir}/${cert_name}.crt ]; then
149     install --mode=0600 ${cert_dir}/${cert_name}.crt /srv/salt/certs/arvados-${cert_name}.pem
150   else
151     echo "${cert_dir}/${cert_name}.crt does not exist. Exiting"
152     exit 1
153   fi
154   if [ -f ${cert_dir}/${cert_name}.key ]; then
155     install --mode=0600 ${cert_dir}/${cert_name}.key /srv/salt/certs/arvados-${cert_name}.key
156   else
157     echo "${cert_dir}/${cert_name}.key does not exist. Exiting"
158     exit 1
159   fi
160 }
161
162 apply_var_substitutions() {
163   local SRCFILE=$1
164   local DSTFILE=$2
165   sed "s#__ANONYMOUS_USER_TOKEN__#${ANONYMOUS_USER_TOKEN}#g;
166        s#__BLOB_SIGNING_KEY__#${BLOB_SIGNING_KEY}#g;
167        s#__CONTROLLER_EXT_SSL_PORT__#${CONTROLLER_EXT_SSL_PORT}#g;
168        s#__CLUSTER__#${CLUSTER}#g;
169        s#__DOMAIN__#${DOMAIN}#g;
170        s#__HOSTNAME_EXT__#${HOSTNAME_EXT}#g;
171        s#__IP_INT__#${IP_INT}#g;
172        s#__INITIAL_USER_EMAIL__#${INITIAL_USER_EMAIL}#g;
173        s#__INITIAL_USER_PASSWORD__#${INITIAL_USER_PASSWORD}#g;
174        s#__INITIAL_USER__#${INITIAL_USER}#g;
175        s#__LE_AWS_REGION__#${LE_AWS_REGION:-}#g;
176        s#__LE_AWS_SECRET_ACCESS_KEY__#${LE_AWS_SECRET_ACCESS_KEY}#g;
177        s#__LE_AWS_ACCESS_KEY_ID__#${LE_AWS_ACCESS_KEY_ID}#g;
178        s#__DATABASE_PASSWORD__#${DATABASE_PASSWORD}#g;
179        s#__KEEPWEB_EXT_SSL_PORT__#${KEEPWEB_EXT_SSL_PORT}#g;
180        s#__KEEP_EXT_SSL_PORT__#${KEEP_EXT_SSL_PORT}#g;
181        s#__MANAGEMENT_TOKEN__#${MANAGEMENT_TOKEN}#g;
182        s#__RELEASE__#${RELEASE}#g;
183        s#__SYSTEM_ROOT_TOKEN__#${SYSTEM_ROOT_TOKEN}#g;
184        s#__VERSION__#${VERSION}#g;
185        s#__WEBSHELL_EXT_SSL_PORT__#${WEBSHELL_EXT_SSL_PORT}#g;
186        s#__WEBSOCKET_EXT_SSL_PORT__#${WEBSOCKET_EXT_SSL_PORT}#g;
187        s#__WORKBENCH1_EXT_SSL_PORT__#${WORKBENCH1_EXT_SSL_PORT}#g;
188        s#__WORKBENCH2_EXT_SSL_PORT__#${WORKBENCH2_EXT_SSL_PORT}#g;
189        s#__CLUSTER_INT_CIDR__#${CLUSTER_INT_CIDR}#g;
190        s#__CONTROLLER_INT_IP__#${CONTROLLER_INT_IP}#g;
191        s#__WEBSOCKET_INT_IP__#${WEBSOCKET_INT_IP}#g;
192        s#__KEEP_INT_IP__#${KEEP_INT_IP}#g;
193        s#__KEEPSTORE0_INT_IP__#${KEEPSTORE0_INT_IP}#g;
194        s#__KEEPWEB_INT_IP__#${KEEPWEB_INT_IP}#g;
195        s#__WEBSHELL_INT_IP__#${WEBSHELL_INT_IP}#g;
196        s#__SHELL_INT_IP__#${SHELL_INT_IP}#g;
197        s#__WORKBENCH1_INT_IP__#${WORKBENCH1_INT_IP}#g;
198        s#__WORKBENCH2_INT_IP__#${WORKBENCH2_INT_IP}#g;
199        s#__DATABASE_INT_IP__#${DATABASE_INT_IP}#g;
200        s#__WORKBENCH_SECRET_KEY__#${WORKBENCH_SECRET_KEY}#g;
201        s#__SSL_KEY_ENCRYPTED__#${SSL_KEY_ENCRYPTED}#g;
202        s#__SSL_KEY_AWS_REGION__#${SSL_KEY_AWS_REGION:-}#g;
203        s#__SSL_KEY_AWS_SECRET_NAME__#${SSL_KEY_AWS_SECRET_NAME}#g;
204        s#__CONTROLLER_MAX_WORKERS__#${CONTROLLER_MAX_WORKERS:-}#g;
205        s#__CONTROLLER_MAX_QUEUED_REQUESTS__#${CONTROLLER_MAX_QUEUED_REQUESTS:-128}#g;
206        s#__MONITORING_USERNAME__#${MONITORING_USERNAME}#g;
207        s#__MONITORING_EMAIL__#${MONITORING_EMAIL}#g;
208        s#__MONITORING_PASSWORD__#${MONITORING_PASSWORD}#g;
209        s#__DISPATCHER_SSH_PRIVKEY__#${DISPATCHER_SSH_PRIVKEY//$'\n'/\\n}#g;
210        s#__ENABLE_BALANCER__#${ENABLE_BALANCER}#g;
211        s#__DISABLED_CONTROLLER__#${DISABLED_CONTROLLER}#g;
212        s#__BALANCER_NODENAME__#${ROLE2NODES['balancer']:-}#g;
213        s#__PROMETHEUS_NODENAME__#${ROLE2NODES['monitoring']:-}#g;
214        s#__CONTROLLER_NODES__#${ROLE2NODES['controller']:-}#g;
215        s#__NODELIST__#${NODELIST}#g;
216        s#__DISPATCHER_INT_IP__#${DISPATCHER_INT_IP}#g;
217        s#__KEEPBALANCE_INT_IP__#${KEEPBALANCE_INT_IP}#g;
218        s#__COMPUTE_AMI__#${COMPUTE_AMI:-}#g;
219        s#__COMPUTE_SG__#${COMPUTE_SG:-}#g;
220        s#__COMPUTE_SUBNET__#${COMPUTE_SUBNET:-}#g;
221        s#__COMPUTE_AWS_REGION__#${COMPUTE_AWS_REGION:-}#g;
222        s#__COMPUTE_USER__#${COMPUTE_USER:-}#g;
223        s#__KEEP_AWS_REGION__#${KEEP_AWS_REGION:-}#g" \
224   "${SRCFILE}" > "${DSTFILE}"
225 }
226
227 DEV_MODE="no"
228 CONFIG_FILE="${SCRIPT_DIR}/local.params"
229 CONFIG_DIR="local_config_dir"
230 DUMP_CONFIG="no"
231 LOG_LEVEL="info"
232 CONTROLLER_EXT_SSL_PORT=443
233 TESTS_DIR="tests"
234
235 NGINX_INSTALL_SOURCE="install_from_repo"
236
237 CLUSTER=""
238 DOMAIN=""
239
240 # Hostnames/IPs used for single-host deploys
241 IP_INT="127.0.1.1"
242
243 # Initial user setup
244 INITIAL_USER=""
245 INITIAL_USER_EMAIL=""
246 INITIAL_USER_PASSWORD=""
247
248 CONTROLLER_EXT_SSL_PORT=8000
249 KEEP_EXT_SSL_PORT=25101
250 # Both for collections and downloads
251 KEEPWEB_EXT_SSL_PORT=9002
252 WEBSHELL_EXT_SSL_PORT=4202
253 WEBSOCKET_EXT_SSL_PORT=8002
254 WORKBENCH1_EXT_SSL_PORT=443
255 WORKBENCH2_EXT_SSL_PORT=3001
256
257 SSL_MODE="self-signed"
258 USE_LETSENCRYPT_ROUTE53="no"
259 CUSTOM_CERTS_DIR="${SCRIPT_DIR}/local_config_dir/certs"
260
261 GRAFANA_DASHBOARDS_DIR="${SCRIPT_DIR}/local_config_dir/dashboards"
262
263 ## These are ARVADOS-related parameters
264 # For a stable release, change RELEASE "production" and VERSION to the
265 # package version (including the iteration, e.g. X.Y.Z-1) of the
266 # release.
267 # The "local.params.example.*" files already set "RELEASE=production"
268 # to deploy  production-ready packages
269 RELEASE="development"
270 VERSION="latest"
271
272 # These are arvados-formula-related parameters
273 # An arvados-formula tag. For a stable release, this should be a
274 # branch name (e.g. X.Y-dev) or tag for the release.
275 # ARVADOS_TAG="2.2.0"
276 # BRANCH="main"
277
278 # We pin the salt version to avoid potential incompatibilities when a new
279 # stable version is released.
280 SALT_VERSION="3004"
281
282 # Other formula versions we depend on
283 #POSTGRES_TAG="v0.44.0"
284 #POSTGRES_URL="https://github.com/saltstack-formulas/postgres-formula.git"
285 POSTGRES_TAG="0.45.0-bugfix327"
286 POSTGRES_URL="https://github.com/arvados/postgres-formula.git"
287 NGINX_TAG="v2.8.1"
288 DOCKER_TAG="v2.4.2"
289 LOCALE_TAG="v0.3.4"
290 LETSENCRYPT_TAG="v2.1.0"
291 LOGROTATE_TAG="v0.14.0"
292 PROMETHEUS_TAG="v5.6.5"
293 GRAFANA_TAG="v3.1.3"
294
295 # Salt's dir
296 DUMP_SALT_CONFIG_DIR=""
297 ## states
298 S_DIR="/srv/salt"
299 STATES_TOP=${S_DIR}/top.sls
300 ## formulas
301 F_DIR="/srv/formulas"
302 ## pillars
303 P_DIR="/srv/pillars"
304 PILLARS_TOP=${P_DIR}/top.sls
305 ## tests
306 T_DIR="/tmp/cluster_tests"
307
308 arguments ${@}
309
310 declare -A NODES
311 declare -A ROLE2NODES
312 declare NODELIST
313
314 source common.sh
315
316 if [ ! -d ${CONFIG_DIR} ]; then
317   echo >&2 "You don't seem to have a config directory with pillars and states."
318   echo >&2 "Please create a '${CONFIG_DIR}' directory (as configured in your '${CONFIG_FILE}'). Please see"
319   echo >&2 "  * https://doc.arvados.org/install/salt-single-host.html#single_host, or"
320   echo >&2 "  * https://doc.arvados.org/install/salt-multi-host.html#multi_host_multi_hostnames"
321   exit 1
322 fi
323
324 if grep -rni 'fixme' ${CONFIG_FILE}.secrets ${CONFIG_FILE} ${CONFIG_DIR} ; then
325   echo >&2 "The config files has some parameters that need to be modified."
326   echo >&2 "Please, fix them and re-run the provision script."
327   exit 1
328 fi
329
330 if ! grep -qE '^[[:alnum:]]{5}$' <<<${CLUSTER} ; then
331   echo >&2 "ERROR: <CLUSTER> must be exactly 5 lowercase alphanumeric characters long"
332   echo >&2 "Fix the cluster name in the 'local.params' file and re-run the provision script"
333   exit 1
334 fi
335
336 # Only used in single_host/single_name deploys
337 if [ ! -z "${HOSTNAME_EXT:-}" ] ; then
338   # We need to add some extra control vars to manage a single certificate vs. multiple
339   USE_SINGLE_HOSTNAME="yes"
340   # Make sure that the value configured as IP_INT is a real IP on the system.
341   # If we don't error out early here when there is a mismatch, the formula will
342   # fail with hard to interpret nginx errors later on.
343   ip addr list |grep "${IP_INT}/" >/dev/null
344   if [[ $? -ne 0 ]]; then
345     echo "Unable to find the IP_INT address '${IP_INT}' on the system, please correct the value in local.params. Exiting..."
346     exit 1
347   fi
348 else
349   USE_SINGLE_HOSTNAME="no"
350   # We set this variable, anyway, so sed lines do not fail and we don't need to add more
351   # conditionals
352   HOSTNAME_EXT="${DOMAIN}"
353 fi
354
355 if [ "${DUMP_CONFIG}" = "yes" ]; then
356   echo "The provision installer will just dump a config under ${DUMP_SALT_CONFIG_DIR} and exit"
357 else
358   # Install a few dependency packages
359   # First, let's figure out the OS we're working on
360   OS_ID=$(grep ^ID= /etc/os-release |cut -f 2 -d=  |cut -f 2 -d \")
361   echo "Detected distro: ${OS_ID}"
362
363   case ${OS_ID} in
364     "centos")
365       echo "WARNING! Disabling SELinux, see https://dev.arvados.org/issues/18019"
366       sed -i 's/SELINUX=enforcing/SELINUX=permissive/g' /etc/sysconfig/selinux
367       setenforce permissive
368       yum install -y  curl git jq
369       ;;
370     "debian"|"ubuntu")
371       # Wait 2 minutes for any apt locks to clear
372       # This option is supported from apt 1.9.1 and ignored in older apt versions.
373       # Cf. https://blog.sinjakli.co.uk/2021/10/25/waiting-for-apt-locks-without-the-hacky-bash-scripts/
374       DEBIAN_FRONTEND=noninteractive apt -o DPkg::Lock::Timeout=120 update
375       DEBIAN_FRONTEND=noninteractive apt install -y curl git jq
376       ;;
377   esac
378
379   if which salt-call; then
380     echo "Salt already installed"
381   else
382     curl -L https://bootstrap.saltstack.com -o /tmp/bootstrap_salt.sh
383     sh /tmp/bootstrap_salt.sh -XdfP -x python3 old-stable ${SALT_VERSION}
384     /bin/systemctl stop salt-minion.service
385     /bin/systemctl disable salt-minion.service
386   fi
387
388   # Set salt to masterless mode
389   cat > /etc/salt/minion << EOFSM
390 failhard: "True"
391
392 file_client: local
393 file_roots:
394   base:
395     - ${S_DIR}
396     - ${F_DIR}/*
397
398 pillar_roots:
399   base:
400     - ${P_DIR}
401 EOFSM
402 fi
403
404 mkdir -p ${S_DIR} ${F_DIR} ${P_DIR} ${T_DIR}
405
406 # Get the formula and dependencies
407 cd ${F_DIR} || exit 1
408 echo "Cloning formulas"
409 test -d docker && ( cd docker && git fetch ) \
410   || git clone --quiet https://github.com/saltstack-formulas/docker-formula.git ${F_DIR}/docker
411 ( cd docker && git checkout --quiet tags/"${DOCKER_TAG}" )
412
413 echo "...locale"
414 test -d locale && ( cd locale && git fetch ) \
415   || git clone --quiet https://github.com/saltstack-formulas/locale-formula.git ${F_DIR}/locale
416 ( cd locale && git checkout --quiet tags/"${LOCALE_TAG}" )
417
418 echo "...nginx"
419 test -d nginx && ( cd nginx && git fetch ) \
420   || git clone --quiet https://github.com/saltstack-formulas/nginx-formula.git ${F_DIR}/nginx
421 ( cd nginx && git checkout --quiet tags/"${NGINX_TAG}" )
422
423 echo "...postgres"
424 test -d postgres && ( cd postgres && git fetch ) \
425   || git clone --quiet ${POSTGRES_URL} ${F_DIR}/postgres
426 ( cd postgres && git checkout --quiet tags/"${POSTGRES_TAG}" )
427
428 echo "...prometheus"
429 test -d prometheus && ( cd prometheus && git fetch ) \
430   || git clone --quiet https://github.com/saltstack-formulas/prometheus-formula.git ${F_DIR}/prometheus
431 ( cd prometheus && git checkout --quiet tags/"${PROMETHEUS_TAG}" )
432
433 echo "...grafana"
434 test -d grafana && ( cd grafana && git fetch ) \
435   || git clone --quiet https://github.com/saltstack-formulas/grafana-formula.git ${F_DIR}/grafana
436 ( cd grafana && git checkout --quiet "${GRAFANA_TAG}" )
437
438 echo "...letsencrypt"
439 test -d letsencrypt && ( cd letsencrypt && git fetch ) \
440   || git clone --quiet https://github.com/saltstack-formulas/letsencrypt-formula.git ${F_DIR}/letsencrypt
441 ( cd letsencrypt && git checkout --quiet tags/"${LETSENCRYPT_TAG}" )
442
443 echo "...logrotate"
444 test -d logrotate && ( cd logrotate && git fetch ) \
445   || git clone --quiet https://github.com/saltstack-formulas/logrotate-formula.git ${F_DIR}/logrotate
446 ( cd logrotate && git checkout --quiet tags/"${LOGROTATE_TAG}" )
447
448 echo "...arvados"
449 test -d arvados || git clone --quiet https://git.arvados.org/arvados-formula.git ${F_DIR}/arvados
450
451 # If we want to try a specific branch of the formula
452 if [ "x${BRANCH:-}" != "xmain" ]; then
453   ( cd ${F_DIR}/arvados && git checkout --quiet -t origin/"${BRANCH}" -b "${BRANCH}" )
454 elif [ "x${ARVADOS_TAG:-}" != "x" ]; then
455   ( cd ${F_DIR}/arvados && git checkout --quiet tags/"${ARVADOS_TAG}" -b "${ARVADOS_TAG}" )
456 fi
457
458 if [ "x${VAGRANT:-}" = "xyes" ]; then
459   EXTRA_STATES_DIR="/home/vagrant/${CONFIG_DIR}/states"
460   SOURCE_PILLARS_DIR="/home/vagrant/${CONFIG_DIR}/pillars"
461   SOURCE_TOFS_DIR="/home/vagrant/${CONFIG_DIR}/tofs"
462   SOURCE_TESTS_DIR="/home/vagrant/${TESTS_DIR}"
463 else
464   EXTRA_STATES_DIR="${SCRIPT_DIR}/${CONFIG_DIR}/states"
465   SOURCE_PILLARS_DIR="${SCRIPT_DIR}/${CONFIG_DIR}/pillars"
466   SOURCE_TOFS_DIR="${SCRIPT_DIR}/${CONFIG_DIR}/tofs"
467   SOURCE_TESTS_DIR="${SCRIPT_DIR}/${TESTS_DIR}"
468 fi
469
470 SOURCE_STATES_DIR="${EXTRA_STATES_DIR}"
471
472 echo "Writing pillars and states"
473
474 # Replace variables (cluster,  domain, etc) in the pillars, states and tests
475 # to ease deployment for newcomers
476 if [ ! -d "${SOURCE_PILLARS_DIR}" ]; then
477   echo "${SOURCE_PILLARS_DIR} does not exist or is not a directory. Exiting."
478   exit 1
479 fi
480 for f in $(ls "${SOURCE_PILLARS_DIR}"/*); do
481   apply_var_substitutions "${f}" "${P_DIR}"/$(basename "${f}")
482 done
483
484 if [ ! -d "${SOURCE_TESTS_DIR}" ]; then
485   echo "WARNING: The tests directory was not copied to \"${SOURCE_TESTS_DIR}\"."
486   if [ "x${TEST:-}" = "xyes" ]; then
487     echo "WARNING: Disabling tests for this installation."
488   fi
489   TEST="no"
490 else
491   mkdir -p ${T_DIR}
492   # Replace cluster and domain name in the test files
493   for f in $(ls "${SOURCE_TESTS_DIR}"/*); do
494     FILTERS="s#__CLUSTER__#${CLUSTER}#g;
495          s#__CONTROLLER_EXT_SSL_PORT__#${CONTROLLER_EXT_SSL_PORT}#g;
496          s#__DOMAIN__#${DOMAIN}#g;
497          s#__IP_INT__#${IP_INT}#g;
498          s#__INITIAL_USER_EMAIL__#${INITIAL_USER_EMAIL}#g;
499          s#__INITIAL_USER_PASSWORD__#${INITIAL_USER_PASSWORD}#g
500          s#__INITIAL_USER__#${INITIAL_USER}#g;
501          s#__DATABASE_PASSWORD__#${DATABASE_PASSWORD}#g;
502          s#__SYSTEM_ROOT_TOKEN__#${SYSTEM_ROOT_TOKEN}#g"
503     if [ "$USE_SINGLE_HOSTNAME" = "yes" ]; then
504       FILTERS="s#__CLUSTER__.__DOMAIN__#${HOSTNAME_EXT}#g;
505          $FILTERS"
506     fi
507     sed "$FILTERS" \
508       "${f}" > ${T_DIR}/$(basename "${f}")
509   done
510   chmod 755 ${T_DIR}/run-test.sh
511 fi
512
513 # Replace helper state files that differ from the formula's examples
514 if [ -d "${SOURCE_STATES_DIR}" ]; then
515   mkdir -p "${F_DIR}"/extra/extra
516   rm -rf "${F_DIR}"/extra/extra/*
517
518   for f in $(ls "${SOURCE_STATES_DIR}"/*); do
519     apply_var_substitutions "${f}" "${F_DIR}/extra/extra"/$(basename "${f}")
520   done
521 fi
522
523 # Now, we build the SALT states/pillars trees
524 # As we need to separate both states and pillars in case we want specific
525 # roles, we iterate on both at the same time
526
527 # Formula template overrides (TOFS)
528 # See: https://template-formula.readthedocs.io/en/latest/TOFS_pattern.html#template-override
529 if [ -d ${SOURCE_TOFS_DIR} ]; then
530   find ${SOURCE_TOFS_DIR} -mindepth 1 -maxdepth 1 -type d -exec cp -r "{}" ${S_DIR} \;
531 fi
532
533 # States
534 cat > ${STATES_TOP} << EOFTSLS
535 base:
536   '*':
537     - locale
538 EOFTSLS
539
540 # Pillars
541 cat > ${PILLARS_TOP} << EOFPSLS
542 base:
543   '*':
544     - locale
545     - arvados
546 EOFPSLS
547
548 # States, extra states
549 if [ -d "${F_DIR}"/extra/extra ]; then
550   SKIP_SNAKE_OIL="snakeoil_certs"
551
552   if [[ "$DEV_MODE" = "yes" || "${SSL_MODE}" == "self-signed" ]] ; then
553     # In dev mode, we create some snake oil certs that we'll
554     # use as CUSTOM_CERTS, so we don't skip the states file.
555     # Same when using self-signed certificates.
556     SKIP_SNAKE_OIL="dont_add_snakeoil_certs"
557   fi
558   for f in $(ls "${F_DIR}"/extra/extra/*.sls | egrep -v "${SKIP_SNAKE_OIL}|shell_"); do
559   echo "    - extra.$(basename ${f} | sed 's/.sls$//g')" >> ${STATES_TOP}
560   done
561   # Use byo or self-signed certificates
562   if [ "${SSL_MODE}" != "lets-encrypt" ]; then
563     mkdir -p "${F_DIR}"/extra/extra/files
564   fi
565 fi
566
567 # If we want specific roles for a node, just add the desired states
568 # and its dependencies
569 if [ -z "${ROLES:-}" ]; then
570   # States
571   echo "    - nginx.passenger" >> ${STATES_TOP}
572   if [ "${SSL_MODE}" = "lets-encrypt" ]; then
573     if [ "${USE_LETSENCRYPT_ROUTE53}" = "yes" ]; then
574       grep -q "aws_credentials" ${STATES_TOP} || echo "    - extra.aws_credentials" >> ${STATES_TOP}
575     fi
576     grep -q "letsencrypt" ${STATES_TOP} || echo "    - letsencrypt" >> ${STATES_TOP}
577   else
578     mkdir -p --mode=0700 /srv/salt/certs
579     if [ "${SSL_MODE}" = "bring-your-own" ]; then
580       # Copy certs to formula extra/files
581       install --mode=0600 ${CUSTOM_CERTS_DIR}/* /srv/salt/certs/
582       # We add the custom_certs state
583       grep -q "custom_certs" ${STATES_TOP} || echo "    - extra.custom_certs" >> ${STATES_TOP}
584       if [ "${SSL_KEY_ENCRYPTED}" = "yes" ]; then
585         grep -q "ssl_key_encrypted" ${STATES_TOP} || echo "    - extra.ssl_key_encrypted" >> ${STATES_TOP}
586       fi
587     fi
588     # In self-signed mode, the certificate files will be created and put in the
589     # destination directory by the snakeoil_certs.sls state file
590   fi
591
592   echo "    - postgres" >> ${STATES_TOP}
593   echo "    - logrotate" >> ${STATES_TOP}
594   echo "    - docker.software" >> ${STATES_TOP}
595   echo "    - arvados" >> ${STATES_TOP}
596   echo "    - extra.shell_sudo_passwordless" >> ${STATES_TOP}
597   echo "    - extra.shell_cron_add_login_sync" >> ${STATES_TOP}
598   echo "    - extra.passenger_rvm" >> ${STATES_TOP}
599
600   # Pillars
601   echo "    - docker" >> ${PILLARS_TOP}
602   echo "    - nginx_api_configuration" >> ${PILLARS_TOP}
603   echo "    - logrotate_api" >> ${PILLARS_TOP}
604   echo "    - nginx_controller_configuration" >> ${PILLARS_TOP}
605   echo "    - nginx_keepproxy_configuration" >> ${PILLARS_TOP}
606   echo "    - nginx_keepweb_configuration" >> ${PILLARS_TOP}
607   echo "    - nginx_passenger" >> ${PILLARS_TOP}
608   echo "    - nginx_websocket_configuration" >> ${PILLARS_TOP}
609   echo "    - nginx_webshell_configuration" >> ${PILLARS_TOP}
610   echo "    - nginx_workbench2_configuration" >> ${PILLARS_TOP}
611   echo "    - nginx_workbench_configuration" >> ${PILLARS_TOP}
612   echo "    - logrotate_wb1" >> ${PILLARS_TOP}
613   echo "    - postgresql" >> ${PILLARS_TOP}
614
615   # We need to tweak the Nginx's pillar depending whether we want plan nginx or nginx+passenger
616   NGINX_INSTALL_SOURCE="install_from_phusionpassenger"
617   sed -i "s/__NGINX_INSTALL_SOURCE__/${NGINX_INSTALL_SOURCE}/g" ${P_DIR}/nginx_passenger.sls
618
619   if [ "${SSL_MODE}" = "lets-encrypt" ]; then
620     if [ "${USE_LETSENCRYPT_ROUTE53}" = "yes" ]; then
621       grep -q "aws_credentials" ${PILLARS_TOP} || echo "    - aws_credentials" >> ${PILLARS_TOP}
622     fi
623     grep -q "letsencrypt" ${PILLARS_TOP} || echo "    - letsencrypt" >> ${PILLARS_TOP}
624
625     hosts=("controller" "websocket" "workbench" "workbench2" "webshell" "keepproxy")
626     if [ ${USE_SINGLE_HOSTNAME} = "no" ]; then
627       hosts+=("download" "collections")
628     else
629       hosts+=("keepweb")
630     fi
631
632     for c in "${hosts[@]}"; do
633       # Are we in a single-host-single-hostname env?
634       if [ "${USE_SINGLE_HOSTNAME}" = "yes" ]; then
635         # Are we in a single-host-single-hostname env?
636         CERT_NAME=${HOSTNAME_EXT}
637       else
638         # We are in a multiple-hostnames env
639         CERT_NAME=${c}.${DOMAIN}
640       fi
641
642       # As the pillar differs whether we use LE or custom certs, we need to do a final edition on them
643       sed -i "s/__CERT_REQUIRES__/cmd: create-initial-cert-${CERT_NAME}*/g;
644               s#__CERT_PEM__#/etc/letsencrypt/live/${CERT_NAME}/fullchain.pem#g;
645               s#__CERT_KEY__#/etc/letsencrypt/live/${CERT_NAME}/privkey.pem#g" \
646       ${P_DIR}/nginx_${c}_configuration.sls
647     done
648   else
649     # Use custom certs (either dev mode or prod)
650     grep -q "extra_custom_certs" ${PILLARS_TOP} || echo "    - extra_custom_certs" >> ${PILLARS_TOP}
651     # And add the certs in the custom_certs pillar
652     echo "extra_custom_certs_dir: /srv/salt/certs" > ${P_DIR}/extra_custom_certs.sls
653     echo "extra_custom_certs:" >> ${P_DIR}/extra_custom_certs.sls
654
655     for c in controller websocket workbench workbench2 webshell keepweb keepproxy; do
656       # Are we in a single-host-single-hostname env?
657       if [ "${USE_SINGLE_HOSTNAME}" = "yes" ]; then
658         # Are we in a single-host-single-hostname env?
659         CERT_NAME=${HOSTNAME_EXT}
660       else
661         # We are in a multiple-hostnames env
662         CERT_NAME=${c}
663       fi
664
665       if [[ "$SSL_MODE" == "bring-your-own" ]]; then
666         copy_custom_cert ${CUSTOM_CERTS_DIR} ${CERT_NAME}
667       fi
668
669       grep -q ${CERT_NAME} ${P_DIR}/extra_custom_certs.sls || echo "  - ${CERT_NAME}" >> ${P_DIR}/extra_custom_certs.sls
670
671       # As the pillar differs whether we use LE or custom certs, we need to do a final edition on them
672       sed -i "s/__CERT_REQUIRES__/file: extra_custom_certs_file_copy_arvados-${CERT_NAME}.pem/g;
673               s#__CERT_PEM__#/etc/nginx/ssl/arvados-${CERT_NAME}.pem#g;
674               s#__CERT_KEY__#/etc/nginx/ssl/arvados-${CERT_NAME}.key#g" \
675       ${P_DIR}/nginx_${c}_configuration.sls
676     done
677   fi
678 else
679   # If we add individual roles, make sure we add the repo first
680   echo "    - arvados.repo" >> ${STATES_TOP}
681   # We add the extra_custom_certs state
682   grep -q "extra.custom_certs"    ${STATES_TOP} || echo "    - extra.custom_certs" >> ${STATES_TOP}
683   if [ "${SSL_KEY_ENCRYPTED}" = "yes" ]; then
684     grep -q "ssl_key_encrypted" ${STATES_TOP} || echo "    - extra.ssl_key_encrypted" >> ${STATES_TOP}
685   fi
686
687   # And we add the basic part for the certs pillar
688   if [ "${SSL_MODE}" != "lets-encrypt" ]; then
689     # And add the certs in the custom_certs pillar
690     echo "extra_custom_certs_dir: /srv/salt/certs" > ${P_DIR}/extra_custom_certs.sls
691     echo "extra_custom_certs:" >> ${P_DIR}/extra_custom_certs.sls
692     grep -q "extra_custom_certs" ${PILLARS_TOP} || echo "    - extra_custom_certs" >> ${PILLARS_TOP}
693   fi
694
695   # Prometheus state on all nodes due to the node exporter below
696   grep -q "\- prometheus$" ${STATES_TOP} || echo "    - prometheus" >> ${STATES_TOP}
697   # Prometheus node exporter pillar
698   grep -q "prometheus_node_exporter" ${PILLARS_TOP} || echo "    - prometheus_node_exporter" >> ${PILLARS_TOP}
699
700   for R in ${ROLES:-}; do
701     case "${R}" in
702       "database")
703         # States
704         grep -q "\- postgres$" ${STATES_TOP} || echo "    - postgres" >> ${STATES_TOP}
705         grep -q "extra.prometheus_pg_exporter" ${STATES_TOP} || echo "    - extra.prometheus_pg_exporter" >> ${STATES_TOP}
706         # Pillars
707         grep -q "postgresql" ${PILLARS_TOP} || echo "    - postgresql" >> ${PILLARS_TOP}
708         grep -q "prometheus_pg_exporter" ${PILLARS_TOP} || echo "    - prometheus_pg_exporter" >> ${PILLARS_TOP}
709       ;;
710       "monitoring")
711         ### Support files ###
712         GRAFANA_DASHBOARDS_DEST_DIR=/srv/salt/dashboards
713         mkdir -p "${GRAFANA_DASHBOARDS_DEST_DIR}"
714         rm -f "${GRAFANA_DASHBOARDS_DEST_DIR}"/*
715         # "ArvadosPromDataSource" is the hardcoded UID for Prometheus' datasource
716         # in Grafana.
717         for f in $(ls "${GRAFANA_DASHBOARDS_DIR}"/*.json); do
718           sed 's#${DS_PROMETHEUS}#ArvadosPromDataSource#g' \
719           "${f}" > "${GRAFANA_DASHBOARDS_DEST_DIR}"/$(basename "${f}")
720         done
721
722         ### States ###
723         grep -q "\- nginx$" ${STATES_TOP} || echo "    - nginx" >> ${STATES_TOP}
724         grep -q "extra.nginx_prometheus_configuration" ${STATES_TOP} || echo "    - extra.nginx_prometheus_configuration" >> ${STATES_TOP}
725
726         grep -q "\- grafana$" ${STATES_TOP} || echo "    - grafana" >> ${STATES_TOP}
727         grep -q "extra.grafana_datasource" ${STATES_TOP} || echo "    - extra.grafana_datasource" >> ${STATES_TOP}
728         grep -q "extra.grafana_dashboards" ${STATES_TOP} || echo "    - extra.grafana_dashboards" >> ${STATES_TOP}
729         grep -q "extra.grafana_admin_user" ${STATES_TOP} || echo "    - extra.grafana_admin_user" >> ${STATES_TOP}
730
731         if [ "${SSL_MODE}" = "lets-encrypt" ]; then
732           grep -q "letsencrypt"     ${STATES_TOP} || echo "    - letsencrypt" >> ${STATES_TOP}
733           if [ "x${USE_LETSENCRYPT_ROUTE53:-}" = "xyes" ]; then
734             grep -q "aws_credentials" ${STATES_TOP} || echo "    - aws_credentials" >> ${STATES_TOP}
735           fi
736         elif [ "${SSL_MODE}" = "bring-your-own" ]; then
737           for SVC in grafana prometheus; do
738             copy_custom_cert ${CUSTOM_CERTS_DIR} ${SVC}
739           done
740         fi
741         ### Pillars ###
742         grep -q "prometheus_server" ${PILLARS_TOP} || echo "    - prometheus_server" >> ${PILLARS_TOP}
743         grep -q "grafana" ${PILLARS_TOP} || echo "    - grafana" >> ${PILLARS_TOP}
744         for SVC in grafana prometheus; do
745           grep -q "nginx_${SVC}_configuration" ${PILLARS_TOP} || echo "    - nginx_${SVC}_configuration" >> ${PILLARS_TOP}
746         done
747         if [ "${SSL_MODE}" = "lets-encrypt" ]; then
748           grep -q "letsencrypt"     ${PILLARS_TOP} || echo "    - letsencrypt" >> ${PILLARS_TOP}
749           for SVC in grafana prometheus; do
750             grep -q "letsencrypt_${SVC}_configuration" ${PILLARS_TOP} || echo "    - letsencrypt_${SVC}_configuration" >> ${PILLARS_TOP}
751             sed -i "s/__CERT_REQUIRES__/cmd: create-initial-cert-${SVC}.${DOMAIN}*/g;
752                     s#__CERT_PEM__#/etc/letsencrypt/live/${SVC}.${DOMAIN}/fullchain.pem#g;
753                     s#__CERT_KEY__#/etc/letsencrypt/live/${SVC}.${DOMAIN}/privkey.pem#g" \
754             ${P_DIR}/nginx_${SVC}_configuration.sls
755           done
756           if [ "${USE_LETSENCRYPT_ROUTE53}" = "yes" ]; then
757             grep -q "aws_credentials" ${PILLARS_TOP} || echo "    - aws_credentials" >> ${PILLARS_TOP}
758           fi
759         elif [ "${SSL_MODE}" = "bring-your-own" ]; then
760           grep -q "ssl_key_encrypted" ${PILLARS_TOP} || echo "    - ssl_key_encrypted" >> ${PILLARS_TOP}
761           for SVC in grafana prometheus; do
762             sed -i "s/__CERT_REQUIRES__/file: extra_custom_certs_file_copy_arvados-${SVC}.pem/g;
763                     s#__CERT_PEM__#/etc/nginx/ssl/arvados-${SVC}.pem#g;
764                     s#__CERT_KEY__#/etc/nginx/ssl/arvados-${SVC}.key#g" \
765               ${P_DIR}/nginx_${SVC}_configuration.sls
766             grep -q ${SVC} ${P_DIR}/extra_custom_certs.sls || echo "  - ${SVC}" >> ${P_DIR}/extra_custom_certs.sls
767           done
768         fi
769       ;;
770       "balancer")
771         ### States ###
772         grep -q "\- nginx$" ${STATES_TOP} || echo "    - nginx" >> ${STATES_TOP}
773
774         if [ "${SSL_MODE}" = "lets-encrypt" ]; then
775           grep -q "letsencrypt"     ${STATES_TOP} || echo "    - letsencrypt" >> ${STATES_TOP}
776           if [ "x${USE_LETSENCRYPT_ROUTE53:-}" = "xyes" ]; then
777             grep -q "aws_credentials" ${STATES_TOP} || echo "    - aws_credentials" >> ${STATES_TOP}
778           fi
779         elif [ "${SSL_MODE}" = "bring-your-own" ]; then
780           copy_custom_cert ${CUSTOM_CERTS_DIR} ${R}
781         fi
782
783         ### Pillars ###
784         grep -q "nginx_${R}_configuration" ${PILLARS_TOP} || echo "    - nginx_${R}_configuration" >> ${PILLARS_TOP}
785
786         if [ "${SSL_MODE}" = "lets-encrypt" ]; then
787           grep -q "letsencrypt"     ${PILLARS_TOP} || echo "    - letsencrypt" >> ${PILLARS_TOP}
788
789           grep -q "letsencrypt_${R}_configuration" ${PILLARS_TOP} || echo "    - letsencrypt_${R}_configuration" >> ${PILLARS_TOP}
790           sed -i "s/__CERT_REQUIRES__/cmd: create-initial-cert-${ROLE2NODES['balancer']}*/g;
791                   s#__CERT_PEM__#/etc/letsencrypt/live/${ROLE2NODES['balancer']}/fullchain.pem#g;
792                   s#__CERT_KEY__#/etc/letsencrypt/live/${ROLE2NODES['balancer']}/privkey.pem#g" \
793           ${P_DIR}/nginx_${R}_configuration.sls
794
795           if [ "${USE_LETSENCRYPT_ROUTE53}" = "yes" ]; then
796             grep -q "aws_credentials" ${PILLARS_TOP} || echo "    - aws_credentials" >> ${PILLARS_TOP}
797           fi
798         elif [ "${SSL_MODE}" = "bring-your-own" ]; then
799           grep -q "ssl_key_encrypted" ${PILLARS_TOP} || echo "    - ssl_key_encrypted" >> ${PILLARS_TOP}
800           sed -i "s/__CERT_REQUIRES__/file: extra_custom_certs_file_copy_arvados-${R}.pem/g;
801                   s#__CERT_PEM__#/etc/nginx/ssl/arvados-${R}.pem#g;
802                   s#__CERT_KEY__#/etc/nginx/ssl/arvados-${R}.key#g" \
803             ${P_DIR}/nginx_${R}_configuration.sls
804           grep -q "${R}" ${P_DIR}/extra_custom_certs.sls || echo "  - ${R}" >> ${P_DIR}/extra_custom_certs.sls
805         fi
806       ;;
807       "controller")
808         ### States ###
809         grep -q "    - logrotate" ${STATES_TOP} || echo "    - logrotate" >> ${STATES_TOP}
810         if grep -q "    - nginx.*$" ${STATES_TOP}; then
811           sed -i s/"^    - nginx.*$"/"    - nginx.passenger"/g ${STATES_TOP}
812         else
813           echo "    - nginx.passenger" >> ${STATES_TOP}
814         fi
815         echo "    - extra.passenger_rvm" >> ${STATES_TOP}
816
817         ### If we don't install and run LE before arvados-api-server, it fails and breaks everything
818         ### after it. So we add this here as we are, after all, sharing the host for api and controller
819         if [ "${ENABLE_BALANCER}" == "no" ]; then
820           if [ "${SSL_MODE}" = "lets-encrypt" ]; then
821             if [ "x${USE_LETSENCRYPT_ROUTE53:-}" = "xyes" ]; then
822               grep -q "aws_credentials" ${STATES_TOP} || echo "    - aws_credentials" >> ${STATES_TOP}
823             fi
824             grep -q "letsencrypt"     ${STATES_TOP} || echo "    - letsencrypt" >> ${STATES_TOP}
825           elif [ "${SSL_MODE}" = "bring-your-own" ]; then
826             copy_custom_cert ${CUSTOM_CERTS_DIR} ${R}
827             grep -q controller ${P_DIR}/extra_custom_certs.sls || echo "  - controller" >> ${P_DIR}/extra_custom_certs.sls
828           fi
829         fi
830         grep -q "arvados.api" ${STATES_TOP} || echo "    - arvados.api" >> ${STATES_TOP}
831         grep -q "arvados.controller" ${STATES_TOP} || echo "    - arvados.controller" >> ${STATES_TOP}
832
833         ### Pillars ###
834         grep -q "logrotate_api" ${PILLARS_TOP}            || echo "    - logrotate_api" >> ${PILLARS_TOP}
835         grep -q "aws_credentials" ${PILLARS_TOP}          || echo "    - aws_credentials" >> ${PILLARS_TOP}
836         grep -q "postgresql" ${PILLARS_TOP}               || echo "    - postgresql" >> ${PILLARS_TOP}
837         grep -q "nginx_passenger" ${PILLARS_TOP}          || echo "    - nginx_passenger" >> ${PILLARS_TOP}
838         grep -q "nginx_api_configuration" ${PILLARS_TOP} || echo "    - nginx_api_configuration" >> ${PILLARS_TOP}
839         grep -q "nginx_controller_configuration" ${PILLARS_TOP} || echo "    - nginx_controller_configuration" >> ${PILLARS_TOP}
840
841         if [ "${ENABLE_BALANCER}" == "no" ]; then
842           if [ "${SSL_MODE}" = "lets-encrypt" ]; then
843             if [ "${USE_LETSENCRYPT_ROUTE53}" = "yes" ]; then
844               grep -q "aws_credentials" ${PILLARS_TOP} || echo "    - aws_credentials" >> ${PILLARS_TOP}
845             fi
846
847             grep -q "letsencrypt"     ${PILLARS_TOP} || echo "    - letsencrypt" >> ${PILLARS_TOP}
848             grep -q "letsencrypt_${R}_configuration" ${PILLARS_TOP} || echo "    - letsencrypt_${R}_configuration" >> ${PILLARS_TOP}
849             sed -i "s/__CERT_REQUIRES__/cmd: create-initial-cert-${R}.${DOMAIN}*/g;
850                     s#__CERT_PEM__#/etc/letsencrypt/live/${R}.${DOMAIN}/fullchain.pem#g;
851                     s#__CERT_KEY__#/etc/letsencrypt/live/${R}.${DOMAIN}/privkey.pem#g" \
852             ${P_DIR}/nginx_${R}_configuration.sls
853           else
854             grep -q "ssl_key_encrypted" ${PILLARS_TOP} || echo "    - ssl_key_encrypted" >> ${PILLARS_TOP}
855             sed -i "s/__CERT_REQUIRES__/file: extra_custom_certs_file_copy_arvados-${R}.pem/g;
856                     s#__CERT_PEM__#/etc/nginx/ssl/arvados-${R}.pem#g;
857                     s#__CERT_KEY__#/etc/nginx/ssl/arvados-${R}.key#g" \
858             ${P_DIR}/nginx_${R}_configuration.sls
859             grep -q ${R} ${P_DIR}/extra_custom_certs.sls || echo "  - ${R}" >> ${P_DIR}/extra_custom_certs.sls
860           fi
861         fi
862         # We need to tweak the Nginx's pillar depending whether we want plain nginx or nginx+passenger
863         NGINX_INSTALL_SOURCE="install_from_phusionpassenger"
864         sed -i "s/__NGINX_INSTALL_SOURCE__/${NGINX_INSTALL_SOURCE}/g" ${P_DIR}/nginx_passenger.sls
865       ;;
866       "websocket" | "workbench" | "workbench2" | "webshell" | "keepweb" | "keepproxy")
867         ### States ###
868         if [ "${R}" = "workbench" ]; then
869           grep -q "    - logrotate" ${STATES_TOP} || echo "    - logrotate" >> ${STATES_TOP}
870           NGINX_INSTALL_SOURCE="install_from_phusionpassenger"
871           if grep -q "    - nginx$" ${STATES_TOP}; then
872             sed -i s/"^    - nginx.*$"/"    - nginx.passenger"/g ${STATES_TOP}
873           else
874             echo "    - nginx.passenger" >> ${STATES_TOP}
875           fi
876         else
877           grep -q "\- nginx$" ${STATES_TOP} || echo "    - nginx" >> ${STATES_TOP}
878         fi
879
880         if [ "${SSL_MODE}" = "lets-encrypt" ]; then
881           if [ "x${USE_LETSENCRYPT_ROUTE53:-}" = "xyes" ]; then
882             grep -q "aws_credentials" ${STATES_TOP} || echo "    - aws_credentials" >> ${STATES_TOP}
883           fi
884           grep -q "letsencrypt"     ${STATES_TOP} || echo "    - letsencrypt" >> ${STATES_TOP}
885         else
886           # Use custom certs, special case for keepweb
887           if [ ${R} = "keepweb" ]; then
888             if [ "${SSL_MODE}" = "bring-your-own" ]; then
889               copy_custom_cert ${CUSTOM_CERTS_DIR} download
890               copy_custom_cert ${CUSTOM_CERTS_DIR} collections
891             fi
892           else
893             if [ "${SSL_MODE}" = "bring-your-own" ]; then
894               copy_custom_cert ${CUSTOM_CERTS_DIR} ${R}
895             fi
896           fi
897         fi
898
899         # webshell role is just a nginx vhost, so it has no state
900         if [ "${R}" != "webshell" ]; then
901           grep -q "arvados.${R}" ${STATES_TOP} || echo "    - arvados.${R}" >> ${STATES_TOP}
902         fi
903
904         ### Pillars ###
905         if [ "${R}" = "workbench" ]; then
906           grep -q "logrotate_wb1" ${PILLARS_TOP} || echo "    - logrotate_wb1" >> ${PILLARS_TOP}
907         fi
908         grep -q "nginx_passenger" ${PILLARS_TOP}          || echo "    - nginx_passenger" >> ${PILLARS_TOP}
909         grep -q "nginx_${R}_configuration" ${PILLARS_TOP} || echo "    - nginx_${R}_configuration" >> ${PILLARS_TOP}
910         # Special case for keepweb
911         if [ ${R} = "keepweb" ]; then
912           grep -q "nginx_download_configuration" ${PILLARS_TOP} || echo "    - nginx_download_configuration" >> ${PILLARS_TOP}
913           grep -q "nginx_collections_configuration" ${PILLARS_TOP} || echo "    - nginx_collections_configuration" >> ${PILLARS_TOP}
914         fi
915
916         if [ "${SSL_MODE}" = "lets-encrypt" ]; then
917           if [ "${USE_LETSENCRYPT_ROUTE53}" = "yes" ]; then
918             grep -q "aws_credentials" ${PILLARS_TOP} || echo "    - aws_credentials" >> ${PILLARS_TOP}
919           fi
920           grep -q "letsencrypt"     ${PILLARS_TOP} || echo "    - letsencrypt" >> ${PILLARS_TOP}
921           grep -q "letsencrypt_${R}_configuration" ${PILLARS_TOP} || echo "    - letsencrypt_${R}_configuration" >> ${PILLARS_TOP}
922
923           # As the pillar differ whether we use LE or custom certs, we need to do a final edition on them
924           # Special case for keepweb
925           if [ ${R} = "keepweb" ]; then
926             for kwsub in download collections; do
927               sed -i "s/__CERT_REQUIRES__/cmd: create-initial-cert-${kwsub}.${DOMAIN}*/g;
928                       s#__CERT_PEM__#/etc/letsencrypt/live/${kwsub}.${DOMAIN}/fullchain.pem#g;
929                       s#__CERT_KEY__#/etc/letsencrypt/live/${kwsub}.${DOMAIN}/privkey.pem#g" \
930               ${P_DIR}/nginx_${kwsub}_configuration.sls
931             done
932           else
933             sed -i "s/__CERT_REQUIRES__/cmd: create-initial-cert-${R}.${DOMAIN}*/g;
934                     s#__CERT_PEM__#/etc/letsencrypt/live/${R}.${DOMAIN}/fullchain.pem#g;
935                     s#__CERT_KEY__#/etc/letsencrypt/live/${R}.${DOMAIN}/privkey.pem#g" \
936             ${P_DIR}/nginx_${R}_configuration.sls
937           fi
938         else
939           grep -q "ssl_key_encrypted" ${PILLARS_TOP} || echo "    - ssl_key_encrypted" >> ${PILLARS_TOP}
940           # As the pillar differ whether we use LE or custom certs, we need to do a final edition on them
941           # Special case for keepweb
942           if [ ${R} = "keepweb" ]; then
943             for kwsub in download collections; do
944               sed -i "s/__CERT_REQUIRES__/file: extra_custom_certs_file_copy_arvados-${kwsub}.pem/g;
945                       s#__CERT_PEM__#/etc/nginx/ssl/arvados-${kwsub}.pem#g;
946                       s#__CERT_KEY__#/etc/nginx/ssl/arvados-${kwsub}.key#g" \
947               ${P_DIR}/nginx_${kwsub}_configuration.sls
948               grep -q ${kwsub} ${P_DIR}/extra_custom_certs.sls || echo "  - ${kwsub}" >> ${P_DIR}/extra_custom_certs.sls
949             done
950           else
951             sed -i "s/__CERT_REQUIRES__/file: extra_custom_certs_file_copy_arvados-${R}.pem/g;
952                     s#__CERT_PEM__#/etc/nginx/ssl/arvados-${R}.pem#g;
953                     s#__CERT_KEY__#/etc/nginx/ssl/arvados-${R}.key#g" \
954             ${P_DIR}/nginx_${R}_configuration.sls
955             grep -q ${R} ${P_DIR}/extra_custom_certs.sls || echo "  - ${R}" >> ${P_DIR}/extra_custom_certs.sls
956           fi
957         fi
958         # We need to tweak the Nginx's pillar depending whether we want plain nginx or nginx+passenger
959         sed -i "s/__NGINX_INSTALL_SOURCE__/${NGINX_INSTALL_SOURCE}/g" ${P_DIR}/nginx_passenger.sls
960       ;;
961       "shell")
962         # States
963         echo "    - extra.shell_sudo_passwordless" >> ${STATES_TOP}
964         echo "    - extra.shell_cron_add_login_sync" >> ${STATES_TOP}
965         grep -q "docker" ${STATES_TOP}       || echo "    - docker.software" >> ${STATES_TOP}
966         grep -q "arvados.${R}" ${STATES_TOP} || echo "    - arvados.${R}" >> ${STATES_TOP}
967         # Pillars
968         grep -q "docker" ${PILLARS_TOP}       || echo "    - docker" >> ${PILLARS_TOP}
969       ;;
970       "dispatcher" | "keepbalance" | "keepstore")
971         # States
972         grep -q "arvados.${R}" ${STATES_TOP} || echo "    - arvados.${R}" >> ${STATES_TOP}
973         # Pillars
974         # ATM, no specific pillar needed
975       ;;
976       *)
977         echo "Unknown role ${R}"
978         exit 1
979       ;;
980     esac
981   done
982 fi
983
984 if [ "${DUMP_CONFIG}" = "yes" ]; then
985   # We won't run the rest of the script because we're just dumping the config
986   exit 0
987 fi
988
989 # Now run the install
990 salt-call --state-output=mixed --local state.apply -l ${LOG_LEVEL}
991
992 # Finally, make sure that /etc/hosts is not overwritten on reboot
993 if [ -d /etc/cloud/cloud.cfg.d ]; then
994   # TODO: will this work on CentOS?
995   sed -i 's/^manage_etc_hosts: true/#manage_etc_hosts: true/g' /etc/cloud/cloud.cfg.d/*
996 fi
997
998 # Leave a copy of the Arvados CA so the user can copy it where it's required
999 if [ "${SSL_MODE}" = "self-signed" ]; then
1000   echo "Copying the Arvados CA certificate '${DOMAIN}-arvados-snakeoil-ca.crt' to the installer dir, so you can import it"
1001   if [ "x${VAGRANT:-}" = "xyes" ]; then
1002     cp /etc/ssl/certs/arvados-snakeoil-ca.pem /vagrant/${DOMAIN}-arvados-snakeoil-ca.pem
1003   else
1004     cp /etc/ssl/certs/arvados-snakeoil-ca.pem ${SCRIPT_DIR}/${DOMAIN}-arvados-snakeoil-ca.crt
1005   fi
1006 fi
1007
1008 if [ "x${VAGRANT:-}" = "xyes" ]; then
1009     # If running in a vagrant VM, also add default user to docker group
1010     echo "Adding the vagrant user to the docker group"
1011     usermod -a -G docker vagrant
1012 fi
1013
1014 # Test that the installation finished correctly
1015 if [ "x${TEST:-}" = "xyes" ]; then
1016   cd ${T_DIR}
1017   # If we use RVM, we need to run this with it, or most ruby commands will fail
1018   RVM_EXEC=""
1019   if [ -x /usr/local/rvm/bin/rvm-exec ]; then
1020     RVM_EXEC="/usr/local/rvm/bin/rvm-exec"
1021   fi
1022   ${RVM_EXEC} ./run-test.sh
1023 fi