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