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