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