Merge branch '22226-empty-output-panic'
[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_url="https://repo.saltproject.io/salt/py3/$_OS_ID/$_OS_VERSION_ID/$(dpkg --print-architecture)"
403         salt_apt_key=SALT-PROJECT-GPG-PUBKEY-2023.gpg
404         install -d -m 755 /etc/apt/keyrings
405         curl -fsSL -o "/etc/apt/keyrings/$salt_apt_key" "$salt_apt_url/$salt_apt_key"
406         chmod go+r "/etc/apt/keyrings/$salt_apt_key"
407         install -b -m 644 /dev/stdin "/etc/apt/sources.list.d/salt$SALT_VERSION.sources" <<EOFSOURCES
408 Types: deb
409 URIs: $salt_apt_url/$SALT_VERSION
410 Suites: $_OS_VERSION_CODENAME
411 Components: main
412 Signed-by: /etc/apt/keyrings/$salt_apt_key
413 EOFSOURCES
414         DEBIAN_FRONTEND=noninteractive apt-get -o DPkg::Lock::Timeout=120 update
415         DEBIAN_FRONTEND=noninteractive apt-get install -y salt-minion
416         break
417         ;;
418     esac
419   done
420
421   # Set salt to masterless mode
422   systemctl disable --now salt-minion.service
423   cat > /etc/salt/minion << EOFSM
424 failhard: "True"
425
426 file_client: local
427 file_roots:
428   base:
429     - ${S_DIR}
430     - ${F_DIR}/*
431
432 pillar_roots:
433   base:
434     - ${P_DIR}
435 EOFSM
436 fi
437
438 mkdir -p ${S_DIR} ${F_DIR} ${P_DIR} ${T_DIR}
439
440 # Get the formula and dependencies
441 cd ${F_DIR} || exit 1
442 echo "Cloning formulas"
443 test -d docker && ( cd docker && git fetch ) \
444   || git clone --quiet https://github.com/saltstack-formulas/docker-formula.git ${F_DIR}/docker
445 ( cd docker && git checkout --quiet tags/"${DOCKER_TAG}" )
446
447 echo "...locale"
448 test -d locale && ( cd locale && git fetch ) \
449   || git clone --quiet https://github.com/saltstack-formulas/locale-formula.git ${F_DIR}/locale
450 ( cd locale && git checkout --quiet tags/"${LOCALE_TAG}" )
451
452 echo "...nginx"
453 test -d nginx && ( cd nginx && git fetch ) \
454   || git clone --quiet https://github.com/saltstack-formulas/nginx-formula.git ${F_DIR}/nginx
455 ( cd nginx && git checkout --quiet tags/"${NGINX_TAG}" )
456
457 echo "...postgres"
458 test -d postgres && ( cd postgres && git fetch ) \
459   || git clone --quiet ${POSTGRES_URL} ${F_DIR}/postgres
460 ( cd postgres && git checkout --quiet "${POSTGRES_TAG}" )
461
462 echo "...prometheus"
463 test -d prometheus && ( cd prometheus && git fetch ) \
464   || git clone --quiet https://github.com/saltstack-formulas/prometheus-formula.git ${F_DIR}/prometheus
465 ( cd prometheus && git checkout --quiet tags/"${PROMETHEUS_TAG}" )
466
467 echo "...grafana"
468 test -d grafana && ( cd grafana && git fetch ) \
469   || git clone --quiet https://github.com/saltstack-formulas/grafana-formula.git ${F_DIR}/grafana
470 ( cd grafana && git checkout --quiet "${GRAFANA_TAG}" )
471
472 echo "...letsencrypt"
473 test -d letsencrypt && ( cd letsencrypt && git fetch ) \
474   || git clone --quiet https://github.com/saltstack-formulas/letsencrypt-formula.git ${F_DIR}/letsencrypt
475 ( cd letsencrypt && git checkout --quiet tags/"${LETSENCRYPT_TAG}" )
476
477 echo "...logrotate"
478 test -d logrotate && ( cd logrotate && git fetch ) \
479   || git clone --quiet https://github.com/saltstack-formulas/logrotate-formula.git ${F_DIR}/logrotate
480 ( cd logrotate && git checkout --quiet tags/"${LOGROTATE_TAG}" )
481
482 echo "...arvados"
483 test -d arvados || git clone --quiet https://git.arvados.org/arvados-formula.git ${F_DIR}/arvados
484
485 # If we want to try a specific branch of the formula
486 if [[ ! -z "${BRANCH:-}" && "x${BRANCH}" != "xmain" ]]; then
487   ( cd ${F_DIR}/arvados && git fetch && git checkout --quiet "${BRANCH}" || git checkout --quiet -t origin/"${BRANCH}" -b "${BRANCH}" )
488 elif [ "x${ARVADOS_TAG:-}" != "x" ]; then
489   ( cd ${F_DIR}/arvados && git checkout --quiet tags/"${ARVADOS_TAG}" -b "${ARVADOS_TAG}" )
490 fi
491
492 if [ "x${VAGRANT:-}" = "xyes" ]; then
493   EXTRA_STATES_DIR="/home/vagrant/${CONFIG_DIR}/states"
494   SOURCE_PILLARS_DIR="/home/vagrant/${CONFIG_DIR}/pillars"
495   SOURCE_TOFS_DIR="/home/vagrant/${CONFIG_DIR}/tofs"
496   SOURCE_TESTS_DIR="/home/vagrant/${TESTS_DIR}"
497 else
498   EXTRA_STATES_DIR="${SCRIPT_DIR}/${CONFIG_DIR}/states"
499   SOURCE_PILLARS_DIR="${SCRIPT_DIR}/${CONFIG_DIR}/pillars"
500   SOURCE_TOFS_DIR="${SCRIPT_DIR}/${CONFIG_DIR}/tofs"
501   SOURCE_TESTS_DIR="${SCRIPT_DIR}/${TESTS_DIR}"
502 fi
503
504 SOURCE_STATES_DIR="${EXTRA_STATES_DIR}"
505
506 echo "Writing pillars and states"
507
508 # Replace variables (cluster,  domain, etc) in the pillars, states and tests
509 # to ease deployment for newcomers
510 if [ ! -d "${SOURCE_PILLARS_DIR}" ]; then
511   echo "${SOURCE_PILLARS_DIR} does not exist or is not a directory. Exiting."
512   exit 1
513 fi
514 for f in $(ls "${SOURCE_PILLARS_DIR}"/*); do
515   apply_var_substitutions "${f}" "${P_DIR}"/$(basename "${f}")
516 done
517
518 if [ ! -d "${SOURCE_TESTS_DIR}" ]; then
519   echo "WARNING: The tests directory was not copied to \"${SOURCE_TESTS_DIR}\"."
520   if [ "x${TEST:-}" = "xyes" ]; then
521     echo "WARNING: Disabling tests for this installation."
522   fi
523   TEST="no"
524 else
525   mkdir -p ${T_DIR}
526   # Replace cluster and domain name in the test files
527   for f in $(ls "${SOURCE_TESTS_DIR}"/*); do
528     FILTERS="s#__CLUSTER__#${CLUSTER}#g;
529          s#__CONTROLLER_EXT_SSL_PORT__#${CONTROLLER_EXT_SSL_PORT}#g;
530          s#__DOMAIN__#${DOMAIN}#g;
531          s#__IP_INT__#${IP_INT}#g;
532          s#__INITIAL_USER_EMAIL__#${INITIAL_USER_EMAIL}#g;
533          s#__INITIAL_USER_PASSWORD__#${INITIAL_USER_PASSWORD}#g
534          s#__INITIAL_USER__#${INITIAL_USER}#g;
535          s#__DATABASE_PASSWORD__#${DATABASE_PASSWORD}#g;
536          s#__SYSTEM_ROOT_TOKEN__#${SYSTEM_ROOT_TOKEN}#g"
537     if [ "$USE_SINGLE_HOSTNAME" = "yes" ]; then
538       FILTERS="s#__CLUSTER__.__DOMAIN__#${HOSTNAME_EXT}#g;
539          $FILTERS"
540     fi
541     sed "$FILTERS" \
542       "${f}" > ${T_DIR}/$(basename "${f}")
543   done
544   chmod 755 ${T_DIR}/run-test.sh
545 fi
546
547 # Replace helper state files that differ from the formula's examples
548 if [ -d "${SOURCE_STATES_DIR}" ]; then
549   mkdir -p "${F_DIR}"/extra/extra
550   rm -rf "${F_DIR}"/extra/extra/*
551
552   for f in $(ls "${SOURCE_STATES_DIR}"/*); do
553     apply_var_substitutions "${f}" "${F_DIR}/extra/extra"/$(basename "${f}")
554   done
555 fi
556
557 # Now, we build the SALT states/pillars trees
558 # As we need to separate both states and pillars in case we want specific
559 # roles, we iterate on both at the same time
560
561 # Formula template overrides (TOFS)
562 # See: https://template-formula.readthedocs.io/en/latest/TOFS_pattern.html#template-override
563 if [ -d ${SOURCE_TOFS_DIR} ]; then
564   find ${SOURCE_TOFS_DIR} -mindepth 1 -maxdepth 1 -type d -exec cp -r "{}" ${S_DIR} \;
565 fi
566
567 # States
568 cat > ${STATES_TOP} << EOFTSLS
569 base:
570   '*':
571     - locale
572 EOFTSLS
573
574 # Pillars
575 cat > ${PILLARS_TOP} << EOFPSLS
576 base:
577   '*':
578     - locale
579     - arvados
580 EOFPSLS
581
582 # States, extra states
583 if [ -d "${F_DIR}"/extra/extra ]; then
584   SKIP_SNAKE_OIL="snakeoil_certs"
585
586   if [[ "$DEV_MODE" = "yes" || "${SSL_MODE}" == "self-signed" ]] ; then
587     # In dev mode, we create some snake oil certs that we'll
588     # use as CUSTOM_CERTS, so we don't skip the states file.
589     # Same when using self-signed certificates.
590     SKIP_SNAKE_OIL="dont_add_snakeoil_certs"
591   fi
592   for f in $(ls "${F_DIR}"/extra/extra/*.sls | egrep -v "${SKIP_SNAKE_OIL}|shell_"); do
593   echo "    - extra.$(basename ${f} | sed 's/.sls$//g')" >> ${STATES_TOP}
594   done
595   # Use byo or self-signed certificates
596   if [ "${SSL_MODE}" != "lets-encrypt" ]; then
597     mkdir -p "${F_DIR}"/extra/extra/files
598   fi
599 fi
600
601 # If we want specific roles for a node, just add the desired states
602 # and its dependencies
603 if [ -z "${ROLES:-}" ]; then
604   # States
605   echo "    - nginx.passenger" >> ${STATES_TOP}
606   if [ "${SSL_MODE}" = "lets-encrypt" ]; then
607     if [ "${USE_LETSENCRYPT_ROUTE53}" = "yes" ]; then
608       grep -q "aws_credentials" ${STATES_TOP} || echo "    - extra.aws_credentials" >> ${STATES_TOP}
609     fi
610     grep -q "letsencrypt" ${STATES_TOP} || echo "    - letsencrypt" >> ${STATES_TOP}
611   else
612     mkdir -p --mode=0700 /srv/salt/certs
613     if [ "${SSL_MODE}" = "bring-your-own" ]; then
614       # Copy certs to formula extra/files
615       install --mode=0600 ${CUSTOM_CERTS_DIR}/* /srv/salt/certs/
616       # We add the custom_certs state
617       grep -q "custom_certs" ${STATES_TOP} || echo "    - extra.custom_certs" >> ${STATES_TOP}
618       if [ "${SSL_KEY_ENCRYPTED}" = "yes" ]; then
619         grep -q "ssl_key_encrypted" ${STATES_TOP} || echo "    - extra.ssl_key_encrypted" >> ${STATES_TOP}
620       fi
621     fi
622     # In self-signed mode, the certificate files will be created and put in the
623     # destination directory by the snakeoil_certs.sls state file
624   fi
625
626   echo "    - postgres" >> ${STATES_TOP}
627   echo "    - logrotate" >> ${STATES_TOP}
628   echo "    - docker.software" >> ${STATES_TOP}
629   echo "    - arvados.repo" >> ${STATES_TOP}
630   echo "    - arvados.config" >> ${STATES_TOP}
631   echo "    - arvados.ruby" >> ${STATES_TOP}
632   echo "    - arvados.api" >> ${STATES_TOP}
633   echo "    - arvados.controller" >> ${STATES_TOP}
634   echo "    - arvados.keepstore" >> ${STATES_TOP}
635   echo "    - arvados.websocket" >> ${STATES_TOP}
636   echo "    - arvados.keepweb" >> ${STATES_TOP}
637   echo "    - arvados.workbench2" >> ${STATES_TOP}
638   echo "    - arvados.keepproxy" >> ${STATES_TOP}
639   echo "    - arvados.shell" >> ${STATES_TOP}
640   echo "    - arvados.dispatcher" >> ${STATES_TOP}
641   echo "    - extra.shell_sudo_passwordless" >> ${STATES_TOP}
642   echo "    - extra.shell_cron_add_login_sync" >> ${STATES_TOP}
643   echo "    - extra.passenger_rvm" >> ${STATES_TOP}
644   echo "    - extra.workbench1_uninstall" >> ${STATES_TOP}
645
646   # Pillars
647   echo "    - docker" >> ${PILLARS_TOP}
648   echo "    - nginx_api_configuration" >> ${PILLARS_TOP}
649   echo "    - logrotate" >> ${PILLARS_TOP}
650   echo "    - logrotate_api" >> ${PILLARS_TOP}
651   echo "    - nginx_controller_configuration" >> ${PILLARS_TOP}
652   echo "    - nginx_keepproxy_configuration" >> ${PILLARS_TOP}
653   echo "    - nginx_keepweb_configuration" >> ${PILLARS_TOP}
654   echo "    - nginx_passenger" >> ${PILLARS_TOP}
655   echo "    - nginx_websocket_configuration" >> ${PILLARS_TOP}
656   echo "    - nginx_webshell_configuration" >> ${PILLARS_TOP}
657   echo "    - nginx_workbench2_configuration" >> ${PILLARS_TOP}
658   echo "    - nginx_workbench_configuration" >> ${PILLARS_TOP}
659   echo "    - logrotate_wb1" >> ${PILLARS_TOP}
660   echo "    - postgresql" >> ${PILLARS_TOP}
661
662   # We need to tweak the Nginx's pillar depending whether we want plan nginx or nginx+passenger
663   NGINX_INSTALL_SOURCE="install_from_phusionpassenger"
664   sed -i "s/__NGINX_INSTALL_SOURCE__/${NGINX_INSTALL_SOURCE}/g" ${P_DIR}/nginx_passenger.sls
665
666   if [ "${SSL_MODE}" = "lets-encrypt" ]; then
667     if [ "${USE_LETSENCRYPT_ROUTE53}" = "yes" ]; then
668       grep -q "aws_credentials" ${PILLARS_TOP} || echo "    - aws_credentials" >> ${PILLARS_TOP}
669     fi
670     grep -q "letsencrypt" ${PILLARS_TOP} || echo "    - letsencrypt" >> ${PILLARS_TOP}
671
672     hosts=("controller" "websocket" "workbench" "workbench2" "webshell" "keepproxy")
673     if [ ${USE_SINGLE_HOSTNAME} = "no" ]; then
674       hosts+=("download" "collections")
675     else
676       hosts+=("keepweb")
677     fi
678
679     for c in "${hosts[@]}"; do
680       # Are we in a single-host-single-hostname env?
681       if [ "${USE_SINGLE_HOSTNAME}" = "yes" ]; then
682         # Are we in a single-host-single-hostname env?
683         CERT_NAME=${HOSTNAME_EXT}
684       else
685         # We are in a multiple-hostnames env
686         CERT_NAME=${c}.${DOMAIN}
687       fi
688
689       # As the pillar differs whether we use LE or custom certs, we need to do a final edition on them
690       sed -i "s/__CERT_REQUIRES__/cmd: create-initial-cert-${CERT_NAME}*/g;
691               s#__CERT_PEM__#/etc/letsencrypt/live/${CERT_NAME}/fullchain.pem#g;
692               s#__CERT_KEY__#/etc/letsencrypt/live/${CERT_NAME}/privkey.pem#g" \
693       ${P_DIR}/nginx_${c}_configuration.sls
694     done
695   else
696     # Use custom certs (either dev mode or prod)
697     grep -q "extra_custom_certs" ${PILLARS_TOP} || echo "    - extra_custom_certs" >> ${PILLARS_TOP}
698     # And add the certs in the custom_certs pillar
699     echo "extra_custom_certs_dir: /srv/salt/certs" > ${P_DIR}/extra_custom_certs.sls
700     echo "extra_custom_certs:" >> ${P_DIR}/extra_custom_certs.sls
701
702     for c in controller websocket workbench workbench2 webshell keepweb keepproxy; do
703       # Are we in a single-host-single-hostname env?
704       if [ "${USE_SINGLE_HOSTNAME}" = "yes" ]; then
705         # Are we in a single-host-single-hostname env?
706         CERT_NAME=${HOSTNAME_EXT}
707       else
708         # We are in a multiple-hostnames env
709         CERT_NAME=${c}
710       fi
711
712       if [[ "$SSL_MODE" == "bring-your-own" ]]; then
713         copy_custom_cert ${CUSTOM_CERTS_DIR} ${CERT_NAME}
714       fi
715
716       grep -q ${CERT_NAME} ${P_DIR}/extra_custom_certs.sls || echo "  - ${CERT_NAME}" >> ${P_DIR}/extra_custom_certs.sls
717
718       # As the pillar differs whether we use LE or custom certs, we need to do a final edition on them
719       sed -i "s/__CERT_REQUIRES__/file: extra_custom_certs_${CERT_NAME}_cert_file_copy/g;
720               s#__CERT_PEM__#/etc/nginx/ssl/arvados-${CERT_NAME}.pem#g;
721               s#__CERT_KEY__#/etc/nginx/ssl/arvados-${CERT_NAME}.key#g" \
722       ${P_DIR}/nginx_${c}_configuration.sls
723     done
724   fi
725 else
726   # If we add individual roles, make sure we add the repo first
727   echo "    - arvados.repo" >> ${STATES_TOP}
728   # We add the extra_custom_certs state
729   grep -q "extra.custom_certs"    ${STATES_TOP} || echo "    - extra.custom_certs" >> ${STATES_TOP}
730   if [ "${SSL_KEY_ENCRYPTED}" = "yes" ]; then
731     grep -q "ssl_key_encrypted" ${STATES_TOP} || echo "    - extra.ssl_key_encrypted" >> ${STATES_TOP}
732   fi
733
734   # And we add the basic part for the certs pillar
735   if [ "${SSL_MODE}" != "lets-encrypt" ]; then
736     # And add the certs in the custom_certs pillar
737     echo "extra_custom_certs_dir: /srv/salt/certs" > ${P_DIR}/extra_custom_certs.sls
738     echo "extra_custom_certs:" >> ${P_DIR}/extra_custom_certs.sls
739     grep -q "extra_custom_certs" ${PILLARS_TOP} || echo "    - extra_custom_certs" >> ${PILLARS_TOP}
740   fi
741
742   # Prometheus state on all nodes due to the node exporter below
743   grep -q "\- prometheus$" ${STATES_TOP} || echo "    - prometheus" >> ${STATES_TOP}
744   # Prometheus node exporter pillar
745   grep -q "prometheus_node_exporter" ${PILLARS_TOP} || echo "    - prometheus_node_exporter" >> ${PILLARS_TOP}
746   # Grafana Alloy OpenTelemetry client state & pillar on all nodes
747   grep -q "extra.alloy_install" ${STATES_TOP} || echo "    - extra.alloy_install" >> ${STATES_TOP}
748   grep -q "alloy" ${PILLARS_TOP} || echo "    - alloy" >> ${PILLARS_TOP}
749
750   for R in ${ROLES:-}; do
751     case "${R}" in
752       "database")
753         # Skip if using an external service
754         if [[ "${DATABASE_EXTERNAL_SERVICE_HOST_OR_IP:-}" != "" ]]; then
755           continue
756         fi
757         # States
758         grep -q "\- postgres$" ${STATES_TOP} || echo "    - postgres" >> ${STATES_TOP}
759         grep -q "extra.prometheus_pg_exporter" ${STATES_TOP} || echo "    - extra.prometheus_pg_exporter" >> ${STATES_TOP}
760         # Pillars
761         grep -q "postgresql" ${PILLARS_TOP} || echo "    - postgresql" >> ${PILLARS_TOP}
762         grep -q "prometheus_pg_exporter" ${PILLARS_TOP} || echo "    - prometheus_pg_exporter" >> ${PILLARS_TOP}
763       ;;
764       "monitoring")
765         ### Support files ###
766         GRAFANA_DASHBOARDS_DEST_DIR=/srv/salt/dashboards
767         mkdir -p "${GRAFANA_DASHBOARDS_DEST_DIR}"
768         rm -f "${GRAFANA_DASHBOARDS_DEST_DIR}"/*
769         # "ArvadosPromDataSource" is the hardcoded UID for Prometheus' datasource
770         # in Grafana.
771         # "ArvadosLokiDataSource" if Loki's UID in Grafana
772         for f in $(ls "${GRAFANA_DASHBOARDS_DIR}"/*.json); do
773           sed "s#__TLS_EXPIRATION_YELLOW__#${TLS_EXPIRATION_YELLOW}#g;
774                s#__TLS_EXPIRATION_GREEN__#${TLS_EXPIRATION_GREEN}#g;
775                s#\${DS_PROMETHEUS}#ArvadosPromDataSource#g;
776                s#\${DS_LOKI}#ArvadosLokiDataSource#g" \
777           "${f}" > "${GRAFANA_DASHBOARDS_DEST_DIR}"/$(basename "${f}")
778         done
779
780         ### States ###
781         grep -q "\- nginx$" ${STATES_TOP} || echo "    - nginx" >> ${STATES_TOP}
782         grep -q "extra.nginx_prometheus_configuration" ${STATES_TOP} || echo "    - extra.nginx_prometheus_configuration" >> ${STATES_TOP}
783
784         grep -q "\- grafana$" ${STATES_TOP} || echo "    - grafana" >> ${STATES_TOP}
785         grep -q "extra.grafana_datasource" ${STATES_TOP} || echo "    - extra.grafana_datasource" >> ${STATES_TOP}
786         grep -q "extra.grafana_dashboards" ${STATES_TOP} || echo "    - extra.grafana_dashboards" >> ${STATES_TOP}
787         grep -q "extra.grafana_admin_user" ${STATES_TOP} || echo "    - extra.grafana_admin_user" >> ${STATES_TOP}
788
789         grep -q "extra.loki_install" ${STATES_TOP} || echo "    - extra.loki_install" >> ${STATES_TOP}
790
791         if [ "${SSL_MODE}" = "lets-encrypt" ]; then
792           grep -q "letsencrypt"     ${STATES_TOP} || echo "    - letsencrypt" >> ${STATES_TOP}
793           if [ "x${USE_LETSENCRYPT_ROUTE53:-}" = "xyes" ]; then
794             grep -q "aws_credentials" ${STATES_TOP} || echo "    - aws_credentials" >> ${STATES_TOP}
795           fi
796         elif [ "${SSL_MODE}" = "bring-your-own" ]; then
797           for SVC in grafana prometheus loki; do
798             copy_custom_cert ${CUSTOM_CERTS_DIR} ${SVC}
799           done
800         fi
801         ### Pillars ###
802         grep -q "prometheus_server" ${PILLARS_TOP} || echo "    - prometheus_server" >> ${PILLARS_TOP}
803         grep -q "grafana" ${PILLARS_TOP} || echo "    - grafana" >> ${PILLARS_TOP}
804         grep -q "loki" ${PILLARS_TOP} || echo "    - loki" >> ${PILLARS_TOP}
805         for SVC in grafana prometheus loki; do
806           grep -q "nginx_${SVC}_configuration" ${PILLARS_TOP} || echo "    - nginx_${SVC}_configuration" >> ${PILLARS_TOP}
807         done
808         grep -q "nginx_snippets" ${PILLARS_TOP} || echo "    - nginx_snippets" >> ${PILLARS_TOP}
809         if [ "${SSL_MODE}" = "lets-encrypt" ]; then
810           grep -q "letsencrypt"     ${PILLARS_TOP} || echo "    - letsencrypt" >> ${PILLARS_TOP}
811           for SVC in grafana prometheus loki; do
812             grep -q "letsencrypt_${SVC}_configuration" ${PILLARS_TOP} || echo "    - letsencrypt_${SVC}_configuration" >> ${PILLARS_TOP}
813             sed -i "s/__CERT_REQUIRES__/cmd: create-initial-cert-${SVC}.${DOMAIN}*/g;
814                     s#__CERT_PEM__#/etc/letsencrypt/live/${SVC}.${DOMAIN}/fullchain.pem#g;
815                     s#__CERT_KEY__#/etc/letsencrypt/live/${SVC}.${DOMAIN}/privkey.pem#g" \
816             ${P_DIR}/nginx_${SVC}_configuration.sls
817           done
818           if [ "${USE_LETSENCRYPT_ROUTE53}" = "yes" ]; then
819             grep -q "aws_credentials" ${PILLARS_TOP} || echo "    - aws_credentials" >> ${PILLARS_TOP}
820           fi
821         elif [ "${SSL_MODE}" = "bring-your-own" ]; then
822           grep -q "ssl_key_encrypted" ${PILLARS_TOP} || echo "    - ssl_key_encrypted" >> ${PILLARS_TOP}
823           for SVC in grafana prometheus loki; do
824             sed -i "s/__CERT_REQUIRES__/file: extra_custom_certs_${SVC}_cert_file_copy/g;
825                     s#__CERT_PEM__#/etc/nginx/ssl/arvados-${SVC}.pem#g;
826                     s#__CERT_KEY__#/etc/nginx/ssl/arvados-${SVC}.key#g" \
827               ${P_DIR}/nginx_${SVC}_configuration.sls
828             grep -q ${SVC} ${P_DIR}/extra_custom_certs.sls || echo "  - ${SVC}" >> ${P_DIR}/extra_custom_certs.sls
829           done
830         fi
831       ;;
832       "balancer")
833         ### States ###
834         grep -q "\- nginx$" ${STATES_TOP} || echo "    - nginx" >> ${STATES_TOP}
835
836         if [ "${SSL_MODE}" = "lets-encrypt" ]; then
837           grep -q "letsencrypt"     ${STATES_TOP} || echo "    - letsencrypt" >> ${STATES_TOP}
838           if [ "x${USE_LETSENCRYPT_ROUTE53:-}" = "xyes" ]; then
839             grep -q "aws_credentials" ${STATES_TOP} || echo "    - aws_credentials" >> ${STATES_TOP}
840           fi
841         elif [ "${SSL_MODE}" = "bring-your-own" ]; then
842           copy_custom_cert ${CUSTOM_CERTS_DIR} ${R}
843         fi
844
845         ### Pillars ###
846         grep -q "nginx_${R}_configuration" ${PILLARS_TOP} || echo "    - nginx_${R}_configuration" >> ${PILLARS_TOP}
847
848         if [ "${SSL_MODE}" = "lets-encrypt" ]; then
849           grep -q "letsencrypt"     ${PILLARS_TOP} || echo "    - letsencrypt" >> ${PILLARS_TOP}
850
851           grep -q "letsencrypt_${R}_configuration" ${PILLARS_TOP} || echo "    - letsencrypt_${R}_configuration" >> ${PILLARS_TOP}
852           sed -i "s/__CERT_REQUIRES__/cmd: create-initial-cert-${ROLE2NODES['balancer']}*/g;
853                   s#__CERT_PEM__#/etc/letsencrypt/live/${ROLE2NODES['balancer']}/fullchain.pem#g;
854                   s#__CERT_KEY__#/etc/letsencrypt/live/${ROLE2NODES['balancer']}/privkey.pem#g" \
855           ${P_DIR}/nginx_${R}_configuration.sls
856
857           if [ "${USE_LETSENCRYPT_ROUTE53}" = "yes" ]; then
858             grep -q "aws_credentials" ${PILLARS_TOP} || echo "    - aws_credentials" >> ${PILLARS_TOP}
859           fi
860         elif [ "${SSL_MODE}" = "bring-your-own" ]; then
861           grep -q "ssl_key_encrypted" ${PILLARS_TOP} || echo "    - ssl_key_encrypted" >> ${PILLARS_TOP}
862           sed -i "s/__CERT_REQUIRES__/file: extra_custom_certs_${R}_cert_file_copy/g;
863                   s#__CERT_PEM__#/etc/nginx/ssl/arvados-${R}.pem#g;
864                   s#__CERT_KEY__#/etc/nginx/ssl/arvados-${R}.key#g" \
865             ${P_DIR}/nginx_${R}_configuration.sls
866           grep -q "${R}" ${P_DIR}/extra_custom_certs.sls || echo "  - ${R}" >> ${P_DIR}/extra_custom_certs.sls
867         fi
868       ;;
869       "controller")
870         ### States ###
871         grep -q "    - logrotate" ${STATES_TOP} || echo "    - logrotate" >> ${STATES_TOP}
872         if grep -q "    - nginx.*$" ${STATES_TOP}; then
873           sed -i s/"^    - nginx.*$"/"    - nginx.passenger"/g ${STATES_TOP}
874         else
875           echo "    - nginx.passenger" >> ${STATES_TOP}
876         fi
877         echo "    - extra.passenger_rvm" >> ${STATES_TOP}
878         grep -q "^    - postgres\\.client$" ${STATES_TOP} || echo "    - postgres.client" >> ${STATES_TOP}
879         if [[ "${DATABASE_EXTERNAL_SERVICE_HOST_OR_IP:-}" != "" ]]; then
880           grep -q "    - extra.postgresql_external" ${STATES_TOP} || echo "    - extra.postgresql_external" >> ${STATES_TOP}
881         fi
882
883         ### If we don't install and run LE before arvados-api-server, it fails and breaks everything
884         ### after it. So we add this here as we are, after all, sharing the host for api and controller
885         if [ "${ENABLE_BALANCER}" == "no" ]; then
886           if [ "${SSL_MODE}" = "lets-encrypt" ]; then
887             if [ "x${USE_LETSENCRYPT_ROUTE53:-}" = "xyes" ]; then
888               grep -q "aws_credentials" ${STATES_TOP} || echo "    - aws_credentials" >> ${STATES_TOP}
889             fi
890             grep -q "letsencrypt"     ${STATES_TOP} || echo "    - letsencrypt" >> ${STATES_TOP}
891           elif [ "${SSL_MODE}" = "bring-your-own" ]; then
892             copy_custom_cert ${CUSTOM_CERTS_DIR} ${R}
893             grep -q controller ${P_DIR}/extra_custom_certs.sls || echo "  - controller" >> ${P_DIR}/extra_custom_certs.sls
894           fi
895         fi
896         grep -q "arvados.api" ${STATES_TOP} || echo "    - arvados.api" >> ${STATES_TOP}
897         grep -q "arvados.controller" ${STATES_TOP} || echo "    - arvados.controller" >> ${STATES_TOP}
898
899         ### Pillars ###
900         grep -q "logrotate" ${PILLARS_TOP}                || echo "    - logrotate" >> ${PILLARS_TOP}
901         grep -q "logrotate_api" ${PILLARS_TOP}            || echo "    - logrotate_api" >> ${PILLARS_TOP}
902         grep -q "aws_credentials" ${PILLARS_TOP}          || echo "    - aws_credentials" >> ${PILLARS_TOP}
903         grep -q "postgresql" ${PILLARS_TOP}               || echo "    - postgresql" >> ${PILLARS_TOP}
904         grep -q "nginx_passenger" ${PILLARS_TOP}          || echo "    - nginx_passenger" >> ${PILLARS_TOP}
905         grep -q "nginx_snippets" ${PILLARS_TOP}           || echo "    - nginx_snippets" >> ${PILLARS_TOP}
906         grep -q "nginx_api_configuration" ${PILLARS_TOP} || echo "    - nginx_api_configuration" >> ${PILLARS_TOP}
907         grep -q "nginx_controller_configuration" ${PILLARS_TOP} || echo "    - nginx_controller_configuration" >> ${PILLARS_TOP}
908
909         if [[ "${DATABASE_EXTERNAL_SERVICE_HOST_OR_IP:-}" != "" ]]; then
910           grep -q "    - postgresql_external" ${PILLARS_TOP} || echo "    - postgresql_external" >> ${PILLARS_TOP}
911         fi
912
913         if [ "${ENABLE_BALANCER}" == "no" ]; then
914           if [ "${SSL_MODE}" = "lets-encrypt" ]; then
915             if [ "${USE_LETSENCRYPT_ROUTE53}" = "yes" ]; then
916               grep -q "aws_credentials" ${PILLARS_TOP} || echo "    - aws_credentials" >> ${PILLARS_TOP}
917             fi
918
919             grep -q "letsencrypt"     ${PILLARS_TOP} || echo "    - letsencrypt" >> ${PILLARS_TOP}
920             grep -q "letsencrypt_${R}_configuration" ${PILLARS_TOP} || echo "    - letsencrypt_${R}_configuration" >> ${PILLARS_TOP}
921             sed -i "s/__CERT_REQUIRES__/cmd: create-initial-cert-${R}.${DOMAIN}*/g;
922                     s#__CERT_PEM__#/etc/letsencrypt/live/${R}.${DOMAIN}/fullchain.pem#g;
923                     s#__CERT_KEY__#/etc/letsencrypt/live/${R}.${DOMAIN}/privkey.pem#g" \
924             ${P_DIR}/nginx_${R}_configuration.sls
925           else
926             grep -q "ssl_key_encrypted" ${PILLARS_TOP} || echo "    - ssl_key_encrypted" >> ${PILLARS_TOP}
927             sed -i "s/__CERT_REQUIRES__/file: extra_custom_certs_${R}_cert_file_copy/g;
928                     s#__CERT_PEM__#/etc/nginx/ssl/arvados-${R}.pem#g;
929                     s#__CERT_KEY__#/etc/nginx/ssl/arvados-${R}.key#g" \
930             ${P_DIR}/nginx_${R}_configuration.sls
931             grep -q ${R} ${P_DIR}/extra_custom_certs.sls || echo "  - ${R}" >> ${P_DIR}/extra_custom_certs.sls
932           fi
933         fi
934         # We need to tweak the Nginx's pillar depending whether we want plain nginx or nginx+passenger
935         NGINX_INSTALL_SOURCE="install_from_phusionpassenger"
936         sed -i "s/__NGINX_INSTALL_SOURCE__/${NGINX_INSTALL_SOURCE}/g" ${P_DIR}/nginx_passenger.sls
937       ;;
938       "websocket" | "workbench" | "workbench2" | "webshell" | "keepweb" | "keepproxy")
939         ### States ###
940         grep -q "\- nginx$" ${STATES_TOP} || echo "    - nginx" >> ${STATES_TOP}
941
942         if [ "${SSL_MODE}" = "lets-encrypt" ]; then
943           if [ "x${USE_LETSENCRYPT_ROUTE53:-}" = "xyes" ]; then
944             grep -q "aws_credentials" ${STATES_TOP} || echo "    - aws_credentials" >> ${STATES_TOP}
945           fi
946           grep -q "letsencrypt"     ${STATES_TOP} || echo "    - letsencrypt" >> ${STATES_TOP}
947         else
948           # Use custom certs, special case for keepweb
949           if [ ${R} = "keepweb" ]; then
950             if [ "${SSL_MODE}" = "bring-your-own" ]; then
951               copy_custom_cert ${CUSTOM_CERTS_DIR} download
952               copy_custom_cert ${CUSTOM_CERTS_DIR} collections
953             fi
954           else
955             if [ "${SSL_MODE}" = "bring-your-own" ]; then
956               copy_custom_cert ${CUSTOM_CERTS_DIR} ${R}
957             fi
958           fi
959         fi
960
961         # webshell role is just a nginx vhost, so it has no state
962         # workbench role is deprecated since 2.7.0
963         if [[ "${R}" != "webshell" && "${R}" != "workbench" ]]; then
964           grep -q "arvados.${R}" ${STATES_TOP} || echo "    - arvados.${R}" >> ${STATES_TOP}
965         fi
966         # Make sure wb1's package get uninstalled
967         if [[ "${R}" == "workbench" ]]; then
968           grep -q "workbench1_uninstall" ${STATES_TOP} || echo "    - extra.workbench1_uninstall" >> ${STATES_TOP}
969         fi
970
971         ### Pillars ###
972         grep -q "nginx_${R}_configuration" ${PILLARS_TOP} || echo "    - nginx_${R}_configuration" >> ${PILLARS_TOP}
973         grep -q "nginx_snippets" ${PILLARS_TOP} || echo "    - nginx_snippets" >> ${PILLARS_TOP}
974         # Special case for keepweb
975         if [ ${R} = "keepweb" ]; then
976           grep -q "nginx_download_configuration" ${PILLARS_TOP} || echo "    - nginx_download_configuration" >> ${PILLARS_TOP}
977           grep -q "nginx_collections_configuration" ${PILLARS_TOP} || echo "    - nginx_collections_configuration" >> ${PILLARS_TOP}
978         fi
979
980         if [ "${SSL_MODE}" = "lets-encrypt" ]; then
981           if [ "${USE_LETSENCRYPT_ROUTE53}" = "yes" ]; then
982             grep -q "aws_credentials" ${PILLARS_TOP} || echo "    - aws_credentials" >> ${PILLARS_TOP}
983           fi
984           grep -q "letsencrypt"     ${PILLARS_TOP} || echo "    - letsencrypt" >> ${PILLARS_TOP}
985           grep -q "letsencrypt_${R}_configuration" ${PILLARS_TOP} || echo "    - letsencrypt_${R}_configuration" >> ${PILLARS_TOP}
986
987           # As the pillar differ whether we use LE or custom certs, we need to do a final edition on them
988           # Special case for keepweb
989           if [ ${R} = "keepweb" ]; then
990             for kwsub in download collections; do
991               sed -i "s/__CERT_REQUIRES__/cmd: create-initial-cert-${kwsub}.${DOMAIN}*/g;
992                       s#__CERT_PEM__#/etc/letsencrypt/live/${kwsub}.${DOMAIN}/fullchain.pem#g;
993                       s#__CERT_KEY__#/etc/letsencrypt/live/${kwsub}.${DOMAIN}/privkey.pem#g" \
994               ${P_DIR}/nginx_${kwsub}_configuration.sls
995             done
996           else
997             sed -i "s/__CERT_REQUIRES__/cmd: create-initial-cert-${R}.${DOMAIN}*/g;
998                     s#__CERT_PEM__#/etc/letsencrypt/live/${R}.${DOMAIN}/fullchain.pem#g;
999                     s#__CERT_KEY__#/etc/letsencrypt/live/${R}.${DOMAIN}/privkey.pem#g" \
1000             ${P_DIR}/nginx_${R}_configuration.sls
1001           fi
1002         else
1003           grep -q "ssl_key_encrypted" ${PILLARS_TOP} || echo "    - ssl_key_encrypted" >> ${PILLARS_TOP}
1004           # As the pillar differ whether we use LE or custom certs, we need to do a final edition on them
1005           # Special case for keepweb
1006           if [ ${R} = "keepweb" ]; then
1007             for kwsub in download collections; do
1008               sed -i "s/__CERT_REQUIRES__/file: extra_custom_certs_${kwsub}_cert_file_copy/g;
1009                       s#__CERT_PEM__#/etc/nginx/ssl/arvados-${kwsub}.pem#g;
1010                       s#__CERT_KEY__#/etc/nginx/ssl/arvados-${kwsub}.key#g" \
1011               ${P_DIR}/nginx_${kwsub}_configuration.sls
1012               grep -q ${kwsub} ${P_DIR}/extra_custom_certs.sls || echo "  - ${kwsub}" >> ${P_DIR}/extra_custom_certs.sls
1013             done
1014           else
1015             sed -i "s/__CERT_REQUIRES__/file: extra_custom_certs_${R}_cert_file_copy/g;
1016                     s#__CERT_PEM__#/etc/nginx/ssl/arvados-${R}.pem#g;
1017                     s#__CERT_KEY__#/etc/nginx/ssl/arvados-${R}.key#g" \
1018             ${P_DIR}/nginx_${R}_configuration.sls
1019             grep -q ${R} ${P_DIR}/extra_custom_certs.sls || echo "  - ${R}" >> ${P_DIR}/extra_custom_certs.sls
1020           fi
1021         fi
1022         # We need to tweak the Nginx's pillar depending whether we want plain nginx or nginx+passenger
1023         sed -i "s/__NGINX_INSTALL_SOURCE__/${NGINX_INSTALL_SOURCE}/g" ${P_DIR}/nginx_passenger.sls
1024       ;;
1025       "shell")
1026         # States
1027         echo "    - extra.shell_sudo_passwordless" >> ${STATES_TOP}
1028         echo "    - extra.shell_cron_add_login_sync" >> ${STATES_TOP}
1029         grep -q "docker" ${STATES_TOP}       || echo "    - docker.software" >> ${STATES_TOP}
1030         grep -q "arvados.${R}" ${STATES_TOP} || echo "    - arvados.${R}" >> ${STATES_TOP}
1031         # Pillars
1032         grep -q "docker" ${PILLARS_TOP}       || echo "    - docker" >> ${PILLARS_TOP}
1033       ;;
1034       "dispatcher" | "keepbalance" | "keepstore")
1035         # States
1036         grep -q "arvados.${R}" ${STATES_TOP} || echo "    - arvados.${R}" >> ${STATES_TOP}
1037         # Pillars
1038         # ATM, no specific pillar needed
1039       ;;
1040       *)
1041         echo "Unknown role ${R}"
1042         exit 1
1043       ;;
1044     esac
1045   done
1046 fi
1047
1048 if [ "${DUMP_CONFIG}" = "yes" ]; then
1049   # We won't run the rest of the script because we're just dumping the config
1050   exit 0
1051 fi
1052
1053 # Now run the install
1054 salt-call --state-output=mixed --local state.apply -l ${LOG_LEVEL}
1055
1056 # Finally, make sure that /etc/hosts is not overwritten on reboot
1057 if [ -d /etc/cloud/cloud.cfg.d ]; then
1058   # TODO: will this work on CentOS?
1059   sed -i 's/^manage_etc_hosts: true/#manage_etc_hosts: true/g' /etc/cloud/cloud.cfg.d/*
1060 fi
1061
1062 # Leave a copy of the Arvados CA so the user can copy it where it's required
1063 if [ "${SSL_MODE}" = "self-signed" ]; then
1064   echo "Copying the Arvados CA certificate '${DOMAIN}-arvados-snakeoil-ca.crt' to the installer dir, so you can import it"
1065   if [ "x${VAGRANT:-}" = "xyes" ]; then
1066     cp /etc/ssl/certs/arvados-snakeoil-ca.pem /vagrant/${DOMAIN}-arvados-snakeoil-ca.pem
1067   else
1068     cp /etc/ssl/certs/arvados-snakeoil-ca.pem ${SCRIPT_DIR}/${DOMAIN}-arvados-snakeoil-ca.crt
1069   fi
1070 fi
1071
1072 if [ "x${VAGRANT:-}" = "xyes" ]; then
1073     # If running in a vagrant VM, also add default user to docker group
1074     echo "Adding the vagrant user to the docker group"
1075     usermod -a -G docker vagrant
1076 fi
1077
1078 # Test that the installation finished correctly
1079 if [ "x${TEST:-}" = "xyes" ]; then
1080   cd ${T_DIR}
1081   ./run-test.sh
1082 fi