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