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