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