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