fix(provision): Add a CA and sign certificates with it
[arvados.git] / tools / salt-install / provision.sh
1 #!/bin/bash -x
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 ##########################################################
14 # This section are the basic parameters to configure the installation
15
16 # The 5 letters name you want to give your cluster
17 CLUSTER="arva2"
18 DOMAIN="arv.local"
19
20 INITIAL_USER="admin"
21
22 # If not specified, the initial user email will be composed as
23 # INITIAL_USER@CLUSTER.DOMAIN
24 INITIAL_USER_EMAIL="${INITIAL_USER}@${CLUSTER}.${DOMAIN}"
25 INITIAL_USER_PASSWORD="password"
26
27 # The example config you want to use. Currently, only "single_host" is
28 # available
29 CONFIG_DIR="single_host"
30
31 # Which release of Arvados repo you want to use
32 RELEASE="production"
33 # Which version of Arvados you want to install. Defaults to 'latest'
34 # in the desired repo
35 VERSION="latest"
36
37 # Host SSL port where you want to point your browser to access Arvados
38 # Defaults to 443 for regular runs, and to 8443 when called in Vagrant.
39 # You can point it to another port if desired
40 # In Vagrant, make sure it matches what you set in the Vagrantfile
41 # HOST_SSL_PORT=443
42
43 # This is a arvados-formula setting.
44 # If branch is set, the script will switch to it before running salt
45 # Usually not needed, only used for testing
46 # BRANCH="master"
47
48 ##########################################################
49 # Usually there's no need to modify things below this line
50
51 set -o pipefail
52
53 # capture the directory that the script is running from
54 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
55
56 usage() {
57   echo >&2
58   echo >&2 "Usage: ${0} [-h] [-h]"
59   echo >&2
60   echo >&2 "${0} options:"
61   echo >&2 "  -d, --debug             Run salt installation in debug mode"
62   echo >&2 "  -p <N>, --ssl-port <N>  SSL port to use for the web applications"
63   echo >&2 "  -t, --test              Test installation running a CWL workflow"
64   echo >&2 "  -h, --help              Display this help and exit"
65   echo >&2 "  -v, --vagrant           Run in vagrant and use the /vagrant shared dir"
66   echo >&2
67 }
68
69 arguments() {
70   # NOTE: This requires GNU getopt (part of the util-linux package on Debian-based distros).
71   TEMP=$(getopt -o dhp:tv \
72     --long debug,help,ssl-port:,test,vagrant \
73     -n "${0}" -- "${@}")
74
75   if [ ${?} != 0 ] ; then echo "GNU getopt missing? Use -h for help"; exit 1 ; fi
76   # Note the quotes around `$TEMP': they are essential!
77   eval set -- "$TEMP"
78
79   while [ ${#} -ge 1 ]; do
80     case ${1} in
81       -d | --debug)
82         LOG_LEVEL="debug"
83         shift
84         ;;
85       -t | --test)
86         TEST="yes"
87         shift
88         ;;
89       -v | --vagrant)
90         VAGRANT="yes"
91         shift
92         ;;
93       -p | --ssl-port)
94         HOST_SSL_PORT=${2}
95         shift 2
96         ;;
97       --)
98         shift
99         break
100         ;;
101       *)
102         usage
103         exit 1
104         ;;
105     esac
106   done
107 }
108
109 LOG_LEVEL="info"
110 HOST_SSL_PORT=443
111 TESTS_DIR="tests"
112
113 arguments ${@}
114
115 # Salt's dir
116 ## states
117 S_DIR="/srv/salt"
118 ## formulas
119 F_DIR="/srv/formulas"
120 ##pillars
121 P_DIR="/srv/pillars"
122
123 apt-get update
124 apt-get install -y curl git jq
125
126 dpkg -l |grep salt-minion
127 if [ ${?} -eq 0 ]; then
128   echo "Salt already installed"
129 else
130   curl -L https://bootstrap.saltstack.com -o /tmp/bootstrap_salt.sh
131   sh /tmp/bootstrap_salt.sh -XUdfP -x python3
132   /bin/systemctl disable salt-minion.service
133 fi
134
135 # Set salt to masterless mode
136 cat > /etc/salt/minion << EOFSM
137 file_client: local
138 file_roots:
139   base:
140     - ${S_DIR}
141     - ${F_DIR}/*
142     - ${F_DIR}/*/test/salt/states/examples
143
144 pillar_roots:
145   base:
146     - ${P_DIR}
147 EOFSM
148
149 mkdir -p ${S_DIR}
150 mkdir -p ${F_DIR}
151 mkdir -p ${P_DIR}
152
153 # States
154 cat > ${S_DIR}/top.sls << EOFTSLS
155 base:
156   '*':
157     - single_host.host_entries
158     - single_host.snakeoil_certs
159     - locale
160     - nginx.passenger
161     - postgres
162     - docker
163     - arvados
164 EOFTSLS
165
166 # Pillars
167 cat > ${P_DIR}/top.sls << EOFPSLS
168 base:
169   '*':
170     - arvados
171     - docker
172     - locale
173     - nginx_api_configuration
174     - nginx_controller_configuration
175     - nginx_keepproxy_configuration
176     - nginx_keepweb_configuration
177     - nginx_passenger
178     - nginx_websocket_configuration
179     - nginx_webshell_configuration
180     - nginx_workbench2_configuration
181     - nginx_workbench_configuration
182     - postgresql
183 EOFPSLS
184
185 # Get the formula and dependencies
186 cd ${F_DIR} || exit 1
187 for f in postgres arvados nginx docker locale; do
188   git clone https://github.com/saltstack-formulas/${f}-formula.git
189 done
190
191 if [ "x${BRANCH}" != "x" ]; then
192   cd ${F_DIR}/arvados-formula || exit 1
193   git checkout -t origin/"${BRANCH}"
194   cd -
195 fi
196
197 if [ "x${VAGRANT}" = "xyes" ]; then
198   SOURCE_PILLARS_DIR="/vagrant/${CONFIG_DIR}"
199   TESTS_DIR="/vagrant/${TESTS_DIR}"
200 else
201   SOURCE_PILLARS_DIR="${SCRIPT_DIR}/${CONFIG_DIR}"
202   TESTS_DIR="${SCRIPT_DIR}/${TESTS_DIR}"
203 fi
204
205 # Replace cluster and domain name in the example pillars and test files
206 for f in "${SOURCE_PILLARS_DIR}"/*; do
207   sed "s/__CLUSTER__/${CLUSTER}/g;
208        s/__DOMAIN__/${DOMAIN}/g;
209        s/__RELEASE__/${RELEASE}/g;
210        s/__HOST_SSL_PORT__/${HOST_SSL_PORT}/g;
211        s/__GUEST_SSL_PORT__/${GUEST_SSL_PORT}/g;
212        s/__INITIAL_USER__/${INITIAL_USER}/g;
213        s/__INITIAL_USER_EMAIL__/${INITIAL_USER_EMAIL}/g;
214        s/__INITIAL_USER_PASSWORD__/${INITIAL_USER_PASSWORD}/g;
215        s/__VERSION__/${VERSION}/g" \
216   "${f}" > "${P_DIR}"/$(basename "${f}")
217 done
218
219 mkdir -p /tmp/cluster_tests
220 # Replace cluster and domain name in the example pillars and test files
221 for f in "${TESTS_DIR}"/*; do
222   sed "s/__CLUSTER__/${CLUSTER}/g;
223        s/__DOMAIN__/${DOMAIN}/g;
224        s/__HOST_SSL_PORT__/${HOST_SSL_PORT}/g;
225        s/__INITIAL_USER__/${INITIAL_USER}/g;
226        s/__INITIAL_USER_EMAIL__/${INITIAL_USER_EMAIL}/g;
227        s/__INITIAL_USER_PASSWORD__/${INITIAL_USER_PASSWORD}/g" \
228   ${f} > /tmp/cluster_tests/$(basename ${f})
229 done
230 chmod 755 /tmp/cluster_tests/run-test.sh
231
232 # FIXME! #16992 Temporary fix for psql call in arvados-api-server
233 if [ -e /root/.psqlrc ]; then
234   if ! ( grep 'pset pager off' /root/.psqlrc ); then
235     RESTORE_PSQL="yes"
236     cp /root/.psqlrc /root/.psqlrc.provision.backup
237   fi
238 else
239   DELETE_PSQL="yes"
240 fi
241
242 echo '\pset pager off' >> /root/.psqlrc
243 # END FIXME! #16992 Temporary fix for psql call in arvados-api-server
244
245 # Now run the install
246 salt-call --local state.apply -l ${LOG_LEVEL}
247
248 # FIXME! #16992 Temporary fix for psql call in arvados-api-server
249 if [ "x${DELETE_PSQL}" = "xyes" ]; then
250   echo "Removing .psql file"
251   rm /root/.psqlrc
252 fi
253
254 if [ "x${RESTORE_PSQL}" = "xyes" ]; then
255   echo "Restoring .psql file"
256   mv -v /root/.psqlrc.provision.backup /root/.psqlrc
257 fi
258 # END FIXME! #16992 Temporary fix for psql call in arvados-api-server
259
260 # Leave a copy of the Arvados CA so the user can copy it where it's required
261 echo "Copying the Arvados CA file to the installer dir, so you can import it"
262 # If running in a vagrant VM, also add default user to docker group
263 if [ "x${VAGRANT}" = "xyes" ]; then
264   cp /etc/ssl/certs/arvados-snakeoil-ca.pem /vagrant
265
266   echo "Adding the vagrant user to the docker group"
267   usermod -a -G docker vagrant
268 else
269   cp /etc/ssl/certs/arvados-snakeoil-ca.pem ${SCRIPT_DIR}
270 fi
271
272 # Test that the installation finished correctly
273 if [ "x${TEST}" = "xyes" ]; then
274   cd /tmp/cluster_tests
275   ./run-test.sh
276 fi