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