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