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