19215: Sets up the provision.sh script to use our own postgres formula fork.
[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 set 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="development"
201 VERSION="latest"
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 # We pin the salt version to avoid potential incompatibilities when a new
210 # stable version is released.
211 SALT_VERSION="3004"
212
213 # Other formula versions we depend on
214 #POSTGRES_TAG="v0.44.0"
215 #POSTGRES_URL="https://github.com/saltstack-formulas/postgres-formula.git"
216 POSTGRES_TAG="0.45.0-bugfix327"
217 POSTGRES_URL="https://github.com/arvados/postgres-formula.git"
218 NGINX_TAG="v2.8.1"
219 DOCKER_TAG="v2.4.2"
220 LOCALE_TAG="v0.3.4"
221 LETSENCRYPT_TAG="v2.1.0"
222 LOGROTATE_TAG="v0.14.0"
223
224 # Salt's dir
225 DUMP_SALT_CONFIG_DIR=""
226 ## states
227 S_DIR="/srv/salt"
228 ## formulas
229 F_DIR="/srv/formulas"
230 ## pillars
231 P_DIR="/srv/pillars"
232 ## tests
233 T_DIR="/tmp/cluster_tests"
234
235 arguments ${@}
236
237 declare -A NODES
238
239 if [ -s ${CONFIG_FILE} ]; then
240   source ${CONFIG_FILE}
241 else
242   echo >&2 "You don't seem to have a config file with initial values."
243   echo >&2 "Please create a '${CONFIG_FILE}' file as described in"
244   echo >&2 "  * https://doc.arvados.org/install/salt-single-host.html#single_host, or"
245   echo >&2 "  * https://doc.arvados.org/install/salt-multi-host.html#multi_host_multi_hostnames"
246   exit 1
247 fi
248
249 if [ ! -d ${CONFIG_DIR} ]; then
250   echo >&2 "You don't seem to have a config directory with pillars and states."
251   echo >&2 "Please create a '${CONFIG_DIR}' directory (as configured in your '${CONFIG_FILE}'). Please see"
252   echo >&2 "  * https://doc.arvados.org/install/salt-single-host.html#single_host, or"
253   echo >&2 "  * https://doc.arvados.org/install/salt-multi-host.html#multi_host_multi_hostnames"
254   exit 1
255 fi
256
257 if grep -rni 'fixme' ${CONFIG_FILE} ${CONFIG_DIR} ; then
258   echo >&2 "The config file ${CONFIG_FILE} has some parameters that need to be modified."
259   echo >&2 "Please, fix them and re-run the provision script."
260   exit 1
261 fi
262
263 if ! grep -qE '^[[:alnum:]]{5}$' <<<${CLUSTER} ; then
264   echo >&2 "ERROR: <CLUSTER> must be exactly 5 lowercase alphanumeric characters long"
265   echo >&2 "Fix the cluster name in the 'local.params' file and re-run the provision script"
266   exit 1
267 fi
268
269 # Only used in single_host/single_name deploys
270 if [ ! -z "${HOSTNAME_EXT}" ] ; then
271   # We need to add some extra control vars to manage a single certificate vs. multiple
272   USE_SINGLE_HOSTNAME="yes"
273   # Make sure that the value configured as IP_INT is a real IP on the system.
274   # If we don't error out early here when there is a mismatch, the formula will
275   # fail with hard to interpret nginx errors later on.
276   ip addr list |grep "${IP_INT}/" >/dev/null
277   if [[ $? -ne 0 ]]; then
278     echo "Unable to find the IP_INT address '${IP_INT}' on the system, please correct the value in local.params. Exiting..."
279     exit 1
280   fi
281 else
282   USE_SINGLE_HOSTNAME="no"
283   # We set this variable, anyway, so sed lines do not fail and we don't need to add more
284   # conditionals
285   HOSTNAME_EXT="${CLUSTER}.${DOMAIN}"
286 fi
287
288 if [ "${DUMP_CONFIG}" = "yes" ]; then
289   echo "The provision installer will just dump a config under ${DUMP_SALT_CONFIG_DIR} and exit"
290 else
291   # Install a few dependency packages
292   # First, let's figure out the OS we're working on
293   OS_ID=$(grep ^ID= /etc/os-release |cut -f 2 -d=  |cut -f 2 -d \")
294   echo "Detected distro: ${OS_ID}"
295
296   case ${OS_ID} in
297     "centos")
298       echo "WARNING! Disabling SELinux, see https://dev.arvados.org/issues/18019"
299       sed -i 's/SELINUX=enforcing/SELINUX=permissive/g' /etc/sysconfig/selinux
300       setenforce permissive
301       yum install -y  curl git jq
302       ;;
303     "debian"|"ubuntu")
304       # Wait 2 minutes for any apt locks to clear
305       # This option is supported from apt 1.9.1 and ignored in older apt versions.
306       # Cf. https://blog.sinjakli.co.uk/2021/10/25/waiting-for-apt-locks-without-the-hacky-bash-scripts/
307       DEBIAN_FRONTEND=noninteractive apt -o DPkg::Lock::Timeout=120 update
308       DEBIAN_FRONTEND=noninteractive apt install -y curl git jq
309       ;;
310   esac
311
312   if which salt-call; then
313     echo "Salt already installed"
314   else
315     curl -L https://bootstrap.saltstack.com -o /tmp/bootstrap_salt.sh
316     sh /tmp/bootstrap_salt.sh -XdfP -x python3 stable ${SALT_VERSION}
317     /bin/systemctl stop salt-minion.service
318     /bin/systemctl disable salt-minion.service
319   fi
320
321   # Set salt to masterless mode
322   cat > /etc/salt/minion << EOFSM
323 failhard: "True"
324
325 file_client: local
326 file_roots:
327   base:
328     - ${S_DIR}
329     - ${F_DIR}/*
330
331 pillar_roots:
332   base:
333     - ${P_DIR}
334 EOFSM
335 fi
336
337 mkdir -p ${S_DIR} ${F_DIR} ${P_DIR} ${T_DIR}
338
339 # Get the formula and dependencies
340 cd ${F_DIR} || exit 1
341 echo "Cloning formulas"
342 test -d docker && ( cd docker && git fetch ) \
343   || git clone --quiet https://github.com/saltstack-formulas/docker-formula.git ${F_DIR}/docker
344 ( cd docker && git checkout --quiet tags/"${DOCKER_TAG}" )
345
346 echo "...locale"
347 test -d locale && ( cd locale && git fetch ) \
348   || git clone --quiet https://github.com/saltstack-formulas/locale-formula.git ${F_DIR}/locale
349 ( cd locale && git checkout --quiet tags/"${LOCALE_TAG}" )
350
351 echo "...nginx"
352 test -d nginx && ( cd nginx && git fetch ) \
353   || git clone --quiet https://github.com/saltstack-formulas/nginx-formula.git ${F_DIR}/nginx
354 ( cd nginx && git checkout --quiet tags/"${NGINX_TAG}" )
355
356 echo "...postgres"
357 test -d postgres && ( cd postgres && git fetch ) \
358   || git clone --quiet ${POSTGRES_URL} ${F_DIR}/postgres
359 ( cd postgres && git checkout --quiet tags/"${POSTGRES_TAG}" )
360
361 echo "...letsencrypt"
362 test -d letsencrypt && ( cd letsencrypt && git fetch ) \
363   || git clone --quiet https://github.com/saltstack-formulas/letsencrypt-formula.git ${F_DIR}/letsencrypt
364 ( cd letsencrypt && git checkout --quiet tags/"${LETSENCRYPT_TAG}" )
365
366 echo "...logrotate"
367 test -d logrotate && ( cd logrotate && git fetch ) \
368   || git clone --quiet https://github.com/saltstack-formulas/logrotate-formula.git ${F_DIR}/logrotate
369 ( cd logrotate && git checkout --quiet tags/"${LOGROTATE_TAG}" )
370
371 echo "...arvados"
372 test -d arvados || git clone --quiet https://git.arvados.org/arvados-formula.git ${F_DIR}/arvados
373
374 # If we want to try a specific branch of the formula
375 if [ "x${BRANCH}" != "x" ]; then
376   ( cd ${F_DIR}/arvados && git checkout --quiet -t origin/"${BRANCH}" -b "${BRANCH}" )
377 elif [ "x${ARVADOS_TAG}" != "x" ]; then
378   ( cd ${F_DIR}/arvados && git checkout --quiet tags/"${ARVADOS_TAG}" -b "${ARVADOS_TAG}" )
379 fi
380
381 if [ "x${VAGRANT}" = "xyes" ]; then
382   EXTRA_STATES_DIR="/home/vagrant/${CONFIG_DIR}/states"
383   SOURCE_PILLARS_DIR="/home/vagrant/${CONFIG_DIR}/pillars"
384   SOURCE_TESTS_DIR="/home/vagrant/${TESTS_DIR}"
385 else
386   EXTRA_STATES_DIR="${SCRIPT_DIR}/${CONFIG_DIR}/states"
387   SOURCE_PILLARS_DIR="${SCRIPT_DIR}/${CONFIG_DIR}/pillars"
388   SOURCE_TESTS_DIR="${SCRIPT_DIR}/${TESTS_DIR}"
389 fi
390
391 SOURCE_STATES_DIR="${EXTRA_STATES_DIR}"
392
393 echo "Writing pillars and states"
394
395 # Replace variables (cluster,  domain, etc) in the pillars, states and tests
396 # to ease deployment for newcomers
397 if [ ! -d "${SOURCE_PILLARS_DIR}" ]; then
398   echo "${SOURCE_PILLARS_DIR} does not exist or is not a directory. Exiting."
399   exit 1
400 fi
401 for f in $(ls "${SOURCE_PILLARS_DIR}"/*); do
402   sed "s#__ANONYMOUS_USER_TOKEN__#${ANONYMOUS_USER_TOKEN}#g;
403        s#__BLOB_SIGNING_KEY__#${BLOB_SIGNING_KEY}#g;
404        s#__CONTROLLER_EXT_SSL_PORT__#${CONTROLLER_EXT_SSL_PORT}#g;
405        s#__CLUSTER__#${CLUSTER}#g;
406        s#__DOMAIN__#${DOMAIN}#g;
407        s#__HOSTNAME_EXT__#${HOSTNAME_EXT}#g;
408        s#__IP_INT__#${IP_INT}#g;
409        s#__INITIAL_USER_EMAIL__#${INITIAL_USER_EMAIL}#g;
410        s#__INITIAL_USER_PASSWORD__#${INITIAL_USER_PASSWORD}#g;
411        s#__INITIAL_USER__#${INITIAL_USER}#g;
412        s#__LE_AWS_REGION__#${LE_AWS_REGION}#g;
413        s#__LE_AWS_SECRET_ACCESS_KEY__#${LE_AWS_SECRET_ACCESS_KEY}#g;
414        s#__LE_AWS_ACCESS_KEY_ID__#${LE_AWS_ACCESS_KEY_ID}#g;
415        s#__DATABASE_PASSWORD__#${DATABASE_PASSWORD}#g;
416        s#__KEEPWEB_EXT_SSL_PORT__#${KEEPWEB_EXT_SSL_PORT}#g;
417        s#__KEEP_EXT_SSL_PORT__#${KEEP_EXT_SSL_PORT}#g;
418        s#__MANAGEMENT_TOKEN__#${MANAGEMENT_TOKEN}#g;
419        s#__RELEASE__#${RELEASE}#g;
420        s#__SYSTEM_ROOT_TOKEN__#${SYSTEM_ROOT_TOKEN}#g;
421        s#__VERSION__#${VERSION}#g;
422        s#__WEBSHELL_EXT_SSL_PORT__#${WEBSHELL_EXT_SSL_PORT}#g;
423        s#__WEBSOCKET_EXT_SSL_PORT__#${WEBSOCKET_EXT_SSL_PORT}#g;
424        s#__WORKBENCH1_EXT_SSL_PORT__#${WORKBENCH1_EXT_SSL_PORT}#g;
425        s#__WORKBENCH2_EXT_SSL_PORT__#${WORKBENCH2_EXT_SSL_PORT}#g;
426        s#__CLUSTER_INT_CIDR__#${CLUSTER_INT_CIDR}#g;
427        s#__CONTROLLER_INT_IP__#${CONTROLLER_INT_IP}#g;
428        s#__WEBSOCKET_INT_IP__#${WEBSOCKET_INT_IP}#g;
429        s#__KEEP_INT_IP__#${KEEP_INT_IP}#g;
430        s#__KEEPSTORE0_INT_IP__#${KEEPSTORE0_INT_IP}#g;
431        s#__KEEPSTORE1_INT_IP__#${KEEPSTORE1_INT_IP}#g;
432        s#__KEEPWEB_INT_IP__#${KEEPWEB_INT_IP}#g;
433        s#__WEBSHELL_INT_IP__#${WEBSHELL_INT_IP}#g;
434        s#__SHELL_INT_IP__#${SHELL_INT_IP}#g;
435        s#__WORKBENCH1_INT_IP__#${WORKBENCH1_INT_IP}#g;
436        s#__WORKBENCH2_INT_IP__#${WORKBENCH2_INT_IP}#g;
437        s#__DATABASE_INT_IP__#${DATABASE_INT_IP}#g;
438        s#__WORKBENCH_SECRET_KEY__#${WORKBENCH_SECRET_KEY}#g" \
439   "${f}" > "${P_DIR}"/$(basename "${f}")
440 done
441
442 if [ ! -d "${SOURCE_TESTS_DIR}" ]; then
443   echo "WARNING: The tests directory was not copied to \"${SOURCE_TESTS_DIR}\"."
444   if [ "x${TEST}" = "xyes" ]; then
445     echo "WARNING: Disabling tests for this installation."
446   fi
447   TEST="no"
448 else
449   mkdir -p ${T_DIR}
450   # Replace cluster and domain name in the test files
451   for f in $(ls "${SOURCE_TESTS_DIR}"/*); do
452     FILTERS="s#__CLUSTER__#${CLUSTER}#g;
453          s#__CONTROLLER_EXT_SSL_PORT__#${CONTROLLER_EXT_SSL_PORT}#g;
454          s#__DOMAIN__#${DOMAIN}#g;
455          s#__IP_INT__#${IP_INT}#g;
456          s#__INITIAL_USER_EMAIL__#${INITIAL_USER_EMAIL}#g;
457          s#__INITIAL_USER_PASSWORD__#${INITIAL_USER_PASSWORD}#g
458          s#__INITIAL_USER__#${INITIAL_USER}#g;
459          s#__DATABASE_PASSWORD__#${DATABASE_PASSWORD}#g;
460          s#__SYSTEM_ROOT_TOKEN__#${SYSTEM_ROOT_TOKEN}#g"
461     if [ "$USE_SINGLE_HOSTNAME" = "yes" ]; then
462       FILTERS="s#__CLUSTER__.__DOMAIN__#${HOSTNAME_EXT}#g;
463          $FILTERS"
464     fi
465     sed "$FILTERS" \
466       "${f}" > ${T_DIR}/$(basename "${f}")
467   done
468   chmod 755 ${T_DIR}/run-test.sh
469 fi
470
471 # Replace helper state files that differ from the formula's examples
472 if [ -d "${SOURCE_STATES_DIR}" ]; then
473   mkdir -p "${F_DIR}"/extra/extra
474
475   for f in $(ls "${SOURCE_STATES_DIR}"/*); do
476     sed "s#__ANONYMOUS_USER_TOKEN__#${ANONYMOUS_USER_TOKEN}#g;
477          s#__CLUSTER__#${CLUSTER}#g;
478          s#__BLOB_SIGNING_KEY__#${BLOB_SIGNING_KEY}#g;
479          s#__CONTROLLER_EXT_SSL_PORT__#${CONTROLLER_EXT_SSL_PORT}#g;
480          s#__DOMAIN__#${DOMAIN}#g;
481          s#__HOSTNAME_EXT__#${HOSTNAME_EXT}#g;
482          s#__IP_INT__#${IP_INT}#g;
483          s#__INITIAL_USER_EMAIL__#${INITIAL_USER_EMAIL}#g;
484          s#__INITIAL_USER_PASSWORD__#${INITIAL_USER_PASSWORD}#g;
485          s#__INITIAL_USER__#${INITIAL_USER}#g;
486          s#__DATABASE_PASSWORD__#${DATABASE_PASSWORD}#g;
487          s#__KEEPWEB_EXT_SSL_PORT__#${KEEPWEB_EXT_SSL_PORT}#g;
488          s#__KEEP_EXT_SSL_PORT__#${KEEP_EXT_SSL_PORT}#g;
489          s#__MANAGEMENT_TOKEN__#${MANAGEMENT_TOKEN}#g;
490          s#__RELEASE__#${RELEASE}#g;
491          s#__SYSTEM_ROOT_TOKEN__#${SYSTEM_ROOT_TOKEN}#g;
492          s#__VERSION__#${VERSION}#g;
493          s#__CLUSTER_INT_CIDR__#${CLUSTER_INT_CIDR}#g;
494          s#__CONTROLLER_INT_IP__#${CONTROLLER_INT_IP}#g;
495          s#__WEBSOCKET_INT_IP__#${WEBSOCKET_INT_IP}#g;
496          s#__KEEP_INT_IP__#${KEEP_INT_IP}#g;
497          s#__KEEPSTORE0_INT_IP__#${KEEPSTORE0_INT_IP}#g;
498          s#__KEEPSTORE1_INT_IP__#${KEEPSTORE1_INT_IP}#g;
499          s#__KEEPWEB_INT_IP__#${KEEPWEB_INT_IP}#g;
500          s#__WEBSHELL_INT_IP__#${WEBSHELL_INT_IP}#g;
501          s#__WORKBENCH1_INT_IP__#${WORKBENCH1_INT_IP}#g;
502          s#__WORKBENCH2_INT_IP__#${WORKBENCH2_INT_IP}#g;
503          s#__DATABASE_INT_IP__#${DATABASE_INT_IP}#g;
504          s#__WEBSHELL_EXT_SSL_PORT__#${WEBSHELL_EXT_SSL_PORT}#g;
505          s#__SHELL_INT_IP__#${SHELL_INT_IP}#g;
506          s#__WEBSOCKET_EXT_SSL_PORT__#${WEBSOCKET_EXT_SSL_PORT}#g;
507          s#__WORKBENCH1_EXT_SSL_PORT__#${WORKBENCH1_EXT_SSL_PORT}#g;
508          s#__WORKBENCH2_EXT_SSL_PORT__#${WORKBENCH2_EXT_SSL_PORT}#g;
509          s#__WORKBENCH_SECRET_KEY__#${WORKBENCH_SECRET_KEY}#g" \
510     "${f}" > "${F_DIR}/extra/extra"/$(basename "${f}")
511   done
512 fi
513
514 # Now, we build the SALT states/pillars trees
515 # As we need to separate both states and pillars in case we want specific
516 # roles, we iterate on both at the same time
517
518 # States
519 cat > ${S_DIR}/top.sls << EOFTSLS
520 base:
521   '*':
522     - locale
523 EOFTSLS
524
525 # Pillars
526 cat > ${P_DIR}/top.sls << EOFPSLS
527 base:
528   '*':
529     - locale
530     - arvados
531 EOFPSLS
532
533 # States, extra states
534 if [ -d "${F_DIR}"/extra/extra ]; then
535   SKIP_SNAKE_OIL="snakeoil_certs"
536
537   if [[ "$DEV_MODE" = "yes" || "${SSL_MODE}" == "self-signed" ]] ; then
538     # In dev mode, we create some snake oil certs that we'll
539     # use as CUSTOM_CERTS, so we don't skip the states file.
540     # Same when using self-signed certificates.
541     SKIP_SNAKE_OIL="dont_add_snakeoil_certs"
542   fi
543   for f in $(ls "${F_DIR}"/extra/extra/*.sls | egrep -v "${SKIP_SNAKE_OIL}|shell_"); do
544   echo "    - extra.$(basename ${f} | sed 's/.sls$//g')" >> ${S_DIR}/top.sls
545   done
546   # Use byo or self-signed certificates
547   if [ "${SSL_MODE}" != "lets-encrypt" ]; then
548     mkdir -p "${F_DIR}"/extra/extra/files
549   fi
550 fi
551
552 # If we want specific roles for a node, just add the desired states
553 # and its dependencies
554 if [ -z "${ROLES}" ]; then
555   # States
556   echo "    - nginx.passenger" >> ${S_DIR}/top.sls
557   if [ "${SSL_MODE}" = "lets-encrypt" ]; then
558     if [ "${USE_LETSENCRYPT_ROUTE53}" = "yes" ]; then
559       grep -q "aws_credentials" ${S_DIR}/top.sls || echo "    - extra.aws_credentials" >> ${S_DIR}/top.sls
560     fi
561     grep -q "letsencrypt"     ${S_DIR}/top.sls || echo "    - letsencrypt" >> ${S_DIR}/top.sls
562   else
563     mkdir -p /srv/salt/certs
564     if [ "${SSL_MODE}" = "bring-your-own" ]; then
565       # Copy certs to formula extra/files
566       cp -rv ${CUSTOM_CERTS_DIR}/* /srv/salt/certs/
567       # We add the custom_certs state
568       grep -q "custom_certs"    ${S_DIR}/top.sls || echo "    - extra.custom_certs" >> ${S_DIR}/top.sls
569     fi
570     # In self-signed mode, the certificate files will be created and put in the
571     # destination directory by the snakeoil_certs.sls state file
572   fi
573
574   echo "    - postgres" >> ${S_DIR}/top.sls
575   echo "    - logrotate" >> ${S_DIR}/top.sls
576   echo "    - docker.software" >> ${S_DIR}/top.sls
577   echo "    - arvados" >> ${S_DIR}/top.sls
578   echo "    - extra.shell_sudo_passwordless" >> ${S_DIR}/top.sls
579   echo "    - extra.shell_cron_add_login_sync" >> ${S_DIR}/top.sls
580   echo "    - extra.passenger_rvm" >> ${S_DIR}/top.sls
581
582   # Pillars
583   echo "    - docker" >> ${P_DIR}/top.sls
584   echo "    - nginx_api_configuration" >> ${P_DIR}/top.sls
585   echo "    - logrotate_api" >> ${P_DIR}/top.sls
586   echo "    - nginx_controller_configuration" >> ${P_DIR}/top.sls
587   echo "    - nginx_keepproxy_configuration" >> ${P_DIR}/top.sls
588   echo "    - nginx_keepweb_configuration" >> ${P_DIR}/top.sls
589   echo "    - nginx_passenger" >> ${P_DIR}/top.sls
590   echo "    - nginx_websocket_configuration" >> ${P_DIR}/top.sls
591   echo "    - nginx_webshell_configuration" >> ${P_DIR}/top.sls
592   echo "    - nginx_workbench2_configuration" >> ${P_DIR}/top.sls
593   echo "    - nginx_workbench_configuration" >> ${P_DIR}/top.sls
594   echo "    - logrotate_wb1" >> ${P_DIR}/top.sls
595   echo "    - postgresql" >> ${P_DIR}/top.sls
596
597   # We need to tweak the Nginx's pillar depending whether we want plan nginx or nginx+passenger
598   NGINX_INSTALL_SOURCE="install_from_phusionpassenger"
599   sed -i "s/__NGINX_INSTALL_SOURCE__/${NGINX_INSTALL_SOURCE}/g" ${P_DIR}/nginx_passenger.sls
600
601   if [ "${SSL_MODE}" = "lets-encrypt" ]; then
602     if [ "${USE_LETSENCRYPT_ROUTE53}" = "yes" ]; then
603       grep -q "aws_credentials" ${P_DIR}/top.sls || echo "    - aws_credentials" >> ${P_DIR}/top.sls
604     fi
605     grep -q "letsencrypt" ${P_DIR}/top.sls || echo "    - letsencrypt" >> ${P_DIR}/top.sls
606
607     hosts=("controller" "websocket" "workbench" "workbench2" "webshell" "keepproxy")
608     if [ ${USE_SINGLE_HOSTNAME} = "no" ]; then
609       hosts+=("download" "collections")
610     else
611       hosts+=("keepweb")
612     fi
613
614     for c in "${hosts[@]}"; do
615       # Are we in a single-host-single-hostname env?
616       if [ "${USE_SINGLE_HOSTNAME}" = "yes" ]; then
617         # Are we in a single-host-single-hostname env?
618         CERT_NAME=${HOSTNAME_EXT}
619       else
620         # We are in a multiple-hostnames env
621         CERT_NAME=${c}.${CLUSTER}.${DOMAIN}
622       fi
623
624       # As the pillar differs whether we use LE or custom certs, we need to do a final edition on them
625       sed -i "s/__CERT_REQUIRES__/cmd: create-initial-cert-${CERT_NAME}*/g;
626               s#__CERT_PEM__#/etc/letsencrypt/live/${CERT_NAME}/fullchain.pem#g;
627               s#__CERT_KEY__#/etc/letsencrypt/live/${CERT_NAME}/privkey.pem#g" \
628       ${P_DIR}/nginx_${c}_configuration.sls
629     done
630   else
631     # Use custom certs (either dev mode or prod)
632     grep -q "extra_custom_certs" ${P_DIR}/top.sls || echo "    - extra_custom_certs" >> ${P_DIR}/top.sls
633     # And add the certs in the custom_certs pillar
634     echo "extra_custom_certs_dir: /srv/salt/certs" > ${P_DIR}/extra_custom_certs.sls
635     echo "extra_custom_certs:" >> ${P_DIR}/extra_custom_certs.sls
636
637     for c in controller websocket workbench workbench2 webshell keepweb keepproxy; do
638       # Are we in a single-host-single-hostname env?
639       if [ "${USE_SINGLE_HOSTNAME}" = "yes" ]; then
640         # Are we in a single-host-single-hostname env?
641         CERT_NAME=${HOSTNAME_EXT}
642       else
643         # We are in a multiple-hostnames env
644         CERT_NAME=${c}
645       fi
646
647       if [[ "$SSL_MODE" == "bring-your-own" ]]; then
648         copy_custom_cert ${CUSTOM_CERTS_DIR} ${CERT_NAME}
649       fi
650
651       grep -q ${CERT_NAME} ${P_DIR}/extra_custom_certs.sls || echo "  - ${CERT_NAME}" >> ${P_DIR}/extra_custom_certs.sls
652
653       # As the pillar differs whether we use LE or custom certs, we need to do a final edition on them
654       sed -i "s/__CERT_REQUIRES__/file: extra_custom_certs_file_copy_arvados-${CERT_NAME}.pem/g;
655               s#__CERT_PEM__#/etc/nginx/ssl/arvados-${CERT_NAME}.pem#g;
656               s#__CERT_KEY__#/etc/nginx/ssl/arvados-${CERT_NAME}.key#g" \
657       ${P_DIR}/nginx_${c}_configuration.sls
658     done
659   fi
660 else
661   # If we add individual roles, make sure we add the repo first
662   echo "    - arvados.repo" >> ${S_DIR}/top.sls
663   # We add the extra_custom_certs state
664   grep -q "extra.custom_certs"    ${S_DIR}/top.sls || echo "    - extra.custom_certs" >> ${S_DIR}/top.sls
665
666   # And we add the basic part for the certs pillar
667   if [ "${SSL_MODE}" != "lets-encrypt" ]; then
668     # And add the certs in the custom_certs pillar
669     echo "extra_custom_certs_dir: /srv/salt/certs" > ${P_DIR}/extra_custom_certs.sls
670     echo "extra_custom_certs:" >> ${P_DIR}/extra_custom_certs.sls
671     grep -q "extra_custom_certs" ${P_DIR}/top.sls || echo "    - extra_custom_certs" >> ${P_DIR}/top.sls
672   fi
673
674   for R in ${ROLES}; do
675     case "${R}" in
676       "database")
677         # States
678         echo "    - postgres" >> ${S_DIR}/top.sls
679         # Pillars
680         echo '    - postgresql' >> ${P_DIR}/top.sls
681       ;;
682       "api")
683         # States
684         grep -q "    - logrotate" ${S_DIR}/top.sls || echo "    - logrotate" >> ${S_DIR}/top.sls
685         if grep -q "    - nginx.*$" ${S_DIR}/top.sls; then
686           sed -i s/"^    - nginx.*$"/"    - nginx.passenger"/g ${S_DIR}/top.sls
687         else
688           echo "    - nginx.passenger" >> ${S_DIR}/top.sls
689         fi
690         echo "    - extra.passenger_rvm" >> ${S_DIR}/top.sls
691         ### If we don't install and run LE before arvados-api-server, it fails and breaks everything
692         ### after it. So we add this here as we are, after all, sharing the host for api and controller
693         if [ "${SSL_MODE}" = "lets-encrypt" ]; then
694           if [ "${USE_LETSENCRYPT_ROUTE53}" = "yes" ]; then
695             grep -q "aws_credentials" ${S_DIR}/top.sls || echo "    - aws_credentials" >> ${S_DIR}/top.sls
696           fi
697           grep -q "letsencrypt" ${S_DIR}/top.sls || echo "    - letsencrypt" >> ${S_DIR}/top.sls
698         else
699           # Use custom certs
700           if [ "${SSL_MODE}" = "bring-your-own" ]; then
701             copy_custom_cert ${CUSTOM_CERTS_DIR} controller
702           fi
703           grep -q controller ${P_DIR}/extra_custom_certs.sls || echo "  - controller" >> ${P_DIR}/extra_custom_certs.sls
704         fi
705         grep -q "arvados.${R}" ${S_DIR}/top.sls    || echo "    - arvados.${R}" >> ${S_DIR}/top.sls
706         # Pillars
707         grep -q "logrotate_api" ${P_DIR}/top.sls            || echo "    - logrotate_api" >> ${P_DIR}/top.sls
708         grep -q "aws_credentials" ${P_DIR}/top.sls          || echo "    - aws_credentials" >> ${P_DIR}/top.sls
709         grep -q "postgresql" ${P_DIR}/top.sls               || echo "    - postgresql" >> ${P_DIR}/top.sls
710         grep -q "nginx_passenger" ${P_DIR}/top.sls          || echo "    - nginx_passenger" >> ${P_DIR}/top.sls
711         grep -q "nginx_${R}_configuration" ${P_DIR}/top.sls || echo "    - nginx_${R}_configuration" >> ${P_DIR}/top.sls
712
713         # We need to tweak the Nginx's pillar depending whether we want plain nginx or nginx+passenger
714         NGINX_INSTALL_SOURCE="install_from_phusionpassenger"
715         sed -i "s/__NGINX_INSTALL_SOURCE__/${NGINX_INSTALL_SOURCE}/g" ${P_DIR}/nginx_passenger.sls
716       ;;
717       "controller" | "websocket" | "workbench" | "workbench2" | "webshell" | "keepweb" | "keepproxy")
718         # States
719         if [ "${R}" = "workbench" ]; then
720           grep -q "    - logrotate" ${S_DIR}/top.sls || echo "    - logrotate" >> ${S_DIR}/top.sls
721           NGINX_INSTALL_SOURCE="install_from_phusionpassenger"
722           if grep -q "    - nginx$" ${S_DIR}/top.sls; then
723             sed -i s/"^    - nginx.*$"/"    - nginx.passenger"/g ${S_DIR}/top.sls
724           else
725             echo "    - nginx.passenger" >> ${S_DIR}/top.sls
726           fi
727         else
728           grep -q "nginx" ${S_DIR}/top.sls || echo "    - nginx" >> ${S_DIR}/top.sls
729         fi
730         if [ "${SSL_MODE}" = "lets-encrypt" ]; then
731           if [ "x${USE_LETSENCRYPT_ROUTE53}" = "xyes" ]; then
732             grep -q "aws_credentials" ${S_DIR}/top.sls || echo "    - aws_credentials" >> ${S_DIR}/top.sls
733           fi
734           grep -q "letsencrypt"     ${S_DIR}/top.sls || echo "    - letsencrypt" >> ${S_DIR}/top.sls
735         else
736           # Use custom certs, special case for keepweb
737           if [ ${R} = "keepweb" ]; then
738             if [ "${SSL_MODE}" = "bring-your-own" ]; then
739               copy_custom_cert ${CUSTOM_CERTS_DIR} download
740               copy_custom_cert ${CUSTOM_CERTS_DIR} collections
741             fi
742           else
743             if [ "${SSL_MODE}" = "bring-your-own" ]; then
744               copy_custom_cert ${CUSTOM_CERTS_DIR} ${R}
745             fi
746           fi
747         fi
748         # webshell role is just a nginx vhost, so it has no state
749         if [ "${R}" != "webshell" ]; then
750           grep -q "arvados.${R}" ${S_DIR}/top.sls || echo "    - arvados.${R}" >> ${S_DIR}/top.sls
751         fi
752         # Pillars
753         if [ "${R}" = "workbench" ]; then
754           grep -q "logrotate_wb1" ${P_DIR}/top.sls || echo "    - logrotate_wb1" >> ${P_DIR}/top.sls
755         fi
756         grep -q "nginx_passenger" ${P_DIR}/top.sls          || echo "    - nginx_passenger" >> ${P_DIR}/top.sls
757         grep -q "nginx_${R}_configuration" ${P_DIR}/top.sls || echo "    - nginx_${R}_configuration" >> ${P_DIR}/top.sls
758         # Special case for keepweb
759         if [ ${R} = "keepweb" ]; then
760           grep -q "nginx_download_configuration" ${P_DIR}/top.sls || echo "    - nginx_download_configuration" >> ${P_DIR}/top.sls
761           grep -q "nginx_collections_configuration" ${P_DIR}/top.sls || echo "    - nginx_collections_configuration" >> ${P_DIR}/top.sls
762         fi
763
764         if [ "${SSL_MODE}" = "lets-encrypt" ]; then
765           if [ "${USE_LETSENCRYPT_ROUTE53}" = "yes" ]; then
766             grep -q "aws_credentials" ${P_DIR}/top.sls || echo "    - aws_credentials" >> ${P_DIR}/top.sls
767           fi
768           grep -q "letsencrypt"     ${P_DIR}/top.sls || echo "    - letsencrypt" >> ${P_DIR}/top.sls
769           grep -q "letsencrypt_${R}_configuration" ${P_DIR}/top.sls || echo "    - letsencrypt_${R}_configuration" >> ${P_DIR}/top.sls
770
771           # As the pillar differ whether we use LE or custom certs, we need to do a final edition on them
772           # Special case for keepweb
773           if [ ${R} = "keepweb" ]; then
774             for kwsub in download collections; do
775               sed -i "s/__CERT_REQUIRES__/cmd: create-initial-cert-${kwsub}.${CLUSTER}.${DOMAIN}*/g;
776                       s#__CERT_PEM__#/etc/letsencrypt/live/${kwsub}.${CLUSTER}.${DOMAIN}/fullchain.pem#g;
777                       s#__CERT_KEY__#/etc/letsencrypt/live/${kwsub}.${CLUSTER}.${DOMAIN}/privkey.pem#g" \
778               ${P_DIR}/nginx_${kwsub}_configuration.sls
779             done
780           else
781             sed -i "s/__CERT_REQUIRES__/cmd: create-initial-cert-${R}.${CLUSTER}.${DOMAIN}*/g;
782                     s#__CERT_PEM__#/etc/letsencrypt/live/${R}.${CLUSTER}.${DOMAIN}/fullchain.pem#g;
783                     s#__CERT_KEY__#/etc/letsencrypt/live/${R}.${CLUSTER}.${DOMAIN}/privkey.pem#g" \
784             ${P_DIR}/nginx_${R}_configuration.sls
785           fi
786         else
787           # As the pillar differ whether we use LE or custom certs, we need to do a final edition on them
788           # Special case for keepweb
789           if [ ${R} = "keepweb" ]; then
790             for kwsub in download collections; do
791               sed -i "s/__CERT_REQUIRES__/file: extra_custom_certs_file_copy_arvados-${kwsub}.pem/g;
792                       s#__CERT_PEM__#/etc/nginx/ssl/arvados-${kwsub}.pem#g;
793                       s#__CERT_KEY__#/etc/nginx/ssl/arvados-${kwsub}.key#g" \
794               ${P_DIR}/nginx_${kwsub}_configuration.sls
795               grep -q ${kwsub} ${P_DIR}/extra_custom_certs.sls || echo "  - ${kwsub}" >> ${P_DIR}/extra_custom_certs.sls
796             done
797           else
798             sed -i "s/__CERT_REQUIRES__/file: extra_custom_certs_file_copy_arvados-${R}.pem/g;
799                     s#__CERT_PEM__#/etc/nginx/ssl/arvados-${R}.pem#g;
800                     s#__CERT_KEY__#/etc/nginx/ssl/arvados-${R}.key#g" \
801             ${P_DIR}/nginx_${R}_configuration.sls
802             grep -q ${R} ${P_DIR}/extra_custom_certs.sls || echo "  - ${R}" >> ${P_DIR}/extra_custom_certs.sls
803           fi
804         fi
805         # We need to tweak the Nginx's pillar depending whether we want plain nginx or nginx+passenger
806         sed -i "s/__NGINX_INSTALL_SOURCE__/${NGINX_INSTALL_SOURCE}/g" ${P_DIR}/nginx_passenger.sls
807       ;;
808       "shell")
809         # States
810         echo "    - extra.shell_sudo_passwordless" >> ${S_DIR}/top.sls
811         echo "    - extra.shell_cron_add_login_sync" >> ${S_DIR}/top.sls
812         grep -q "docker" ${S_DIR}/top.sls       || echo "    - docker.software" >> ${S_DIR}/top.sls
813         grep -q "arvados.${R}" ${S_DIR}/top.sls || echo "    - arvados.${R}" >> ${S_DIR}/top.sls
814         # Pillars
815         grep -q "docker" ${P_DIR}/top.sls       || echo "    - docker" >> ${P_DIR}/top.sls
816       ;;
817       "dispatcher" | "keepbalance" | "keepstore")
818         # States
819         grep -q "arvados.${R}" ${S_DIR}/top.sls || echo "    - arvados.${R}" >> ${S_DIR}/top.sls
820         # Pillars
821         # ATM, no specific pillar needed
822       ;;
823       *)
824         echo "Unknown role ${R}"
825         exit 1
826       ;;
827     esac
828   done
829 fi
830
831 if [ "${DUMP_CONFIG}" = "yes" ]; then
832   # We won't run the rest of the script because we're just dumping the config
833   exit 0
834 fi
835
836 # Now run the install
837 salt-call --state-output=mixed --local state.apply -l ${LOG_LEVEL}
838
839 # Finally, make sure that /etc/hosts is not overwritten on reboot
840 if [ -d /etc/cloud/cloud.cfg.d ]; then
841   # TODO: will this work on CentOS?
842   sed -i 's/^manage_etc_hosts: true/#manage_etc_hosts: true/g' /etc/cloud/cloud.cfg.d/*
843 fi
844
845 # Leave a copy of the Arvados CA so the user can copy it where it's required
846 if [ "${SSL_MODE}" = "self-signed" ]; then
847   echo "Copying the Arvados CA certificate '${CLUSTER}.${DOMAIN}-arvados-snakeoil-ca.crt' to the installer dir, so you can import it"
848   if [ "x${VAGRANT}" = "xyes" ]; then
849     cp /etc/ssl/certs/arvados-snakeoil-ca.pem /vagrant/${CLUSTER}.${DOMAIN}-arvados-snakeoil-ca.pem
850   else
851     cp /etc/ssl/certs/arvados-snakeoil-ca.pem ${SCRIPT_DIR}/${CLUSTER}.${DOMAIN}-arvados-snakeoil-ca.crt
852   fi
853 fi
854
855 if [ "x${VAGRANT}" = "xyes" ]; then
856     # If running in a vagrant VM, also add default user to docker group
857     echo "Adding the vagrant user to the docker group"
858     usermod -a -G docker vagrant
859 fi
860
861 # Test that the installation finished correctly
862 if [ "x${TEST}" = "xyes" ]; then
863   cd ${T_DIR}
864   # If we use RVM, we need to run this with it, or most ruby commands will fail
865   RVM_EXEC=""
866   if [ -x /usr/local/rvm/bin/rvm-exec ]; then
867     RVM_EXEC="/usr/local/rvm/bin/rvm-exec"
868   fi
869   ${RVM_EXEC} ./run-test.sh
870 fi