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