feat(provision): allow to install individual roles
[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 CONFIG_DIR="single_host"
19 RELEASE="production"
20 VERSION="latest"
21 ARVADOS_TAG="v1.1.4"
22 POSTGRES_TAG="v0.41.3"
23 NGINX_TAG="v2.4.0"
24 DOCKER_TAG="v1.0.0"
25 LOCALE_TAG="v0.3.4"
26
27 if [ -s ${SCRIPT_DIR}/local.params ]; then
28   source ${SCRIPT_DIR}/local.params
29 else
30   echo >&2 "Please create a '${SCRIPT_DIR}/local.params' file with initial values, as described in FIXME_URL_TO_DESCR"
31   exit 1
32 fi
33
34 usage() {
35   echo >&2
36   echo >&2 "Usage: ${0} [-h] [-h]"
37   echo >&2
38   echo >&2 "${0} options:"
39   echo >&2 "  -d, --debug             Run salt installation in debug mode"
40   echo >&2 "  -p <N>, --ssl-port <N>  SSL port to use for the web applications"
41   echo >&2 "  -t, --test              Test installation running a CWL workflow"
42   echo >&2 "  -r, --roles             List of Arvados roles to apply to the host, comma separated"
43   echo >&2 "                          Possible values are:"
44   echo >&2 "                            api"
45   echo >&2 "                            controller"
46   echo >&2 "                            keepstore"
47   echo >&2 "                            websocket"
48   echo >&2 "                            keepweb"
49   echo >&2 "                            workbench2"
50   echo >&2 "                            keepproxy"
51   echo >&2 "                            shell"
52   echo >&2 "                            workbench"
53   echo >&2 "                            dispatcher"
54   echo >&2 "                          Defaults to applying them all"
55   echo >&2 "  -h, --help              Display this help and exit"
56   echo >&2 "  -v, --vagrant           Run in vagrant and use the /vagrant shared dir"
57   echo >&2
58 }
59
60 arguments() {
61   # NOTE: This requires GNU getopt (part of the util-linux package on Debian-based distros).
62   TEMP=$(getopt -o dhp:r:tv \
63     --long debug,help,ssl-port:,roles:,test,vagrant \
64     -n "${0}" -- "${@}")
65
66   if [ ${?} != 0 ] ; then echo "GNU getopt missing? Use -h for help"; exit 1 ; fi
67   # Note the quotes around `$TEMP': they are essential!
68   eval set -- "$TEMP"
69
70   while [ ${#} -ge 1 ]; do
71     case ${1} in
72       -d | --debug)
73         LOG_LEVEL="debug"
74         shift
75         ;;
76       -p | --ssl-port)
77         HOST_SSL_PORT=${2}
78         shift 2
79         ;;
80       -r | --roles)
81         for i in ${2//,/ }
82           do
83             # Verify the role exists
84             if [[ ! "api,controller,keepstore,websocket,keepweb,workbench2,keepproxy,shell,workbench,dispatcher" == *"$i"* ]]; then
85               echo "The role '${i}' is not a valid role"
86               usage
87               exit 1
88             fi
89             ROLES="${ROLES} ${i}"
90           done
91           shift 2
92         ;;
93       -t | --test)
94         TEST="yes"
95         shift
96         ;;
97       -v | --vagrant)
98         VAGRANT="yes"
99         shift
100         ;;
101       --)
102         shift
103         break
104         ;;
105       *)
106         usage
107         exit 1
108         ;;
109     esac
110   done
111 }
112
113 LOG_LEVEL="info"
114 HOST_SSL_PORT=443
115 TESTS_DIR="tests"
116
117 arguments ${@}
118
119 # Salt's dir
120 ## states
121 S_DIR="/srv/salt"
122 ## formulas
123 F_DIR="/srv/formulas"
124 ##pillars
125 P_DIR="/srv/pillars"
126
127 apt-get update
128 apt-get install -y curl git jq
129
130 dpkg -l |grep salt-minion
131 if [ ${?} -eq 0 ]; then
132   echo "Salt already installed"
133 else
134   curl -L https://bootstrap.saltstack.com -o /tmp/bootstrap_salt.sh
135   sh /tmp/bootstrap_salt.sh -XUdfP -x python3
136   /bin/systemctl disable salt-minion.service
137 fi
138
139 # Set salt to masterless mode
140 cat > /etc/salt/minion << EOFSM
141 file_client: local
142 file_roots:
143   base:
144     - ${S_DIR}
145     - ${F_DIR}/*
146     - ${F_DIR}/*/test/salt/states/examples
147
148 pillar_roots:
149   base:
150     - ${P_DIR}
151 EOFSM
152
153 mkdir -p ${S_DIR}
154 mkdir -p ${F_DIR}
155 mkdir -p ${P_DIR}
156
157 # States
158 cat > ${S_DIR}/top.sls << EOFTSLS
159 base:
160   '*':
161     - single_host.host_entries
162     - single_host.snakeoil_certs
163     - locale
164     - nginx.passenger
165     - postgres
166     - docker
167 EOFTSLS
168
169 # If we want specific roles for a node, just add those states
170 if [ -z "${ROLES}" ]; then
171   echo '    - arvados' >> ${S_DIR}/top.sls
172 else
173   for R in ${ROLES}; do
174     echo "    - arvados.${R}" >> ${S_DIR}/top.sls
175   done
176 fi
177
178 # Pillars
179 cat > ${P_DIR}/top.sls << EOFPSLS
180 base:
181   '*':
182     - arvados
183     - docker
184     - locale
185     - nginx_api_configuration
186     - nginx_controller_configuration
187     - nginx_keepproxy_configuration
188     - nginx_keepweb_configuration
189     - nginx_passenger
190     - nginx_websocket_configuration
191     - nginx_webshell_configuration
192     - nginx_workbench2_configuration
193     - nginx_workbench_configuration
194     - postgresql
195 EOFPSLS
196
197 # Get the formula and dependencies
198 cd ${F_DIR} || exit 1
199 git clone --branch "${ARVADOS_TAG}" https://github.com/arvados/arvados-formula.git
200 git clone --branch "${DOCKER_TAG}" https://github.com/saltstack-formulas/docker-formula.git
201 git clone --branch "${LOCALE_TAG}" https://github.com/saltstack-formulas/locale-formula.git
202 git clone --branch "${NGINX_TAG}" https://github.com/saltstack-formulas/nginx-formula.git
203 git clone --branch "${POSTGRES_TAG}" https://github.com/saltstack-formulas/postgres-formula.git
204
205 if [ "x${BRANCH}" != "x" ]; then
206   cd ${F_DIR}/arvados-formula || exit 1
207   git checkout -t origin/"${BRANCH}"
208   cd -
209 fi
210
211 if [ "x${VAGRANT}" = "xyes" ]; then
212   SOURCE_PILLARS_DIR="/vagrant/${CONFIG_DIR}"
213   TESTS_DIR="/vagrant/${TESTS_DIR}"
214 else
215   SOURCE_PILLARS_DIR="${SCRIPT_DIR}/${CONFIG_DIR}"
216   TESTS_DIR="${SCRIPT_DIR}/${TESTS_DIR}"
217 fi
218
219 # Replace cluster and domain name in the example pillars and test files
220 for f in "${SOURCE_PILLARS_DIR}"/*; do
221   sed "s/__CLUSTER__/${CLUSTER}/g;
222        s/__DOMAIN__/${DOMAIN}/g;
223        s/__RELEASE__/${RELEASE}/g;
224        s/__HOST_SSL_PORT__/${HOST_SSL_PORT}/g;
225        s/__GUEST_SSL_PORT__/${GUEST_SSL_PORT}/g;
226        s/__INITIAL_USER__/${INITIAL_USER}/g;
227        s/__INITIAL_USER_EMAIL__/${INITIAL_USER_EMAIL}/g;
228        s/__INITIAL_USER_PASSWORD__/${INITIAL_USER_PASSWORD}/g;
229        s/__VERSION__/${VERSION}/g" \
230   "${f}" > "${P_DIR}"/$(basename "${f}")
231 done
232
233 mkdir -p /tmp/cluster_tests
234 # Replace cluster and domain name in the example pillars and test files
235 for f in "${TESTS_DIR}"/*; do
236   sed "s/__CLUSTER__/${CLUSTER}/g;
237        s/__DOMAIN__/${DOMAIN}/g;
238        s/__HOST_SSL_PORT__/${HOST_SSL_PORT}/g;
239        s/__INITIAL_USER__/${INITIAL_USER}/g;
240        s/__INITIAL_USER_EMAIL__/${INITIAL_USER_EMAIL}/g;
241        s/__INITIAL_USER_PASSWORD__/${INITIAL_USER_PASSWORD}/g" \
242   ${f} > /tmp/cluster_tests/$(basename ${f})
243 done
244 chmod 755 /tmp/cluster_tests/run-test.sh
245
246 # FIXME! #16992 Temporary fix for psql call in arvados-api-server
247 if [ -e /root/.psqlrc ]; then
248   if ! ( grep 'pset pager off' /root/.psqlrc ); then
249     RESTORE_PSQL="yes"
250     cp /root/.psqlrc /root/.psqlrc.provision.backup
251   fi
252 else
253   DELETE_PSQL="yes"
254 fi
255
256 echo '\pset pager off' >> /root/.psqlrc
257 # END FIXME! #16992 Temporary fix for psql call in arvados-api-server
258
259 # Now run the install
260 salt-call --local state.apply -l ${LOG_LEVEL}
261
262 # FIXME! #16992 Temporary fix for psql call in arvados-api-server
263 if [ "x${DELETE_PSQL}" = "xyes" ]; then
264   echo "Removing .psql file"
265   rm /root/.psqlrc
266 fi
267
268 if [ "x${RESTORE_PSQL}" = "xyes" ]; then
269   echo "Restoring .psql file"
270   mv -v /root/.psqlrc.provision.backup /root/.psqlrc
271 fi
272 # END FIXME! #16992 Temporary fix for psql call in arvados-api-server
273
274 # Leave a copy of the Arvados CA so the user can copy it where it's required
275 echo "Copying the Arvados CA certificate to the installer dir, so you can import it"
276 # If running in a vagrant VM, also add default user to docker group
277 if [ "x${VAGRANT}" = "xyes" ]; then
278   cp /etc/ssl/certs/arvados-snakeoil-ca.pem /vagrant
279
280   echo "Adding the vagrant user to the docker group"
281   usermod -a -G docker vagrant
282 else
283   cp /etc/ssl/certs/arvados-snakeoil-ca.pem ${SCRIPT_DIR}
284 fi
285
286 # Test that the installation finished correctly
287 if [ "x${TEST}" = "xyes" ]; then
288   cd /tmp/cluster_tests
289   ./run-test.sh
290 fi