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