Merge branch '16471-install-using-salt' into master
[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 usage() {
54   echo >&2
55   echo >&2 "Usage: $0 [-h] [-h]"
56   echo >&2
57   echo >&2 "$0 options:"
58   echo >&2 "  -v, --vagrant           Run in vagrant and use the /vagrant shared dir"
59   echo >&2 "  -p <N>, --ssl-port <N>  SSL port to use for the web applications"
60   echo >&2 "  -h, --help              Display this help and exit"
61   echo >&2
62 }
63
64 arguments() {
65   # NOTE: This requires GNU getopt (part of the util-linux package on Debian-based distros).
66   TEMP=`getopt -o hvp: \
67     --long help,vagrant,ssl-port: \
68     -n "$0" -- "$@"`
69
70   if [ $? != 0 ] ; then echo "GNU getopt missing? Use -h for help"; exit 1 ; 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       -v | --vagrant)
77         VAGRANT="yes"
78         shift
79         ;;
80       -p | --ssl-port)
81         HOST_SSL_PORT=${2}
82         shift 2
83         ;;
84       --)
85         shift
86         break
87         ;;
88       *)
89         usage
90         exit 1
91         ;;
92     esac
93   done
94 }
95
96 HOST_SSL_PORT=443
97
98 arguments $@
99
100 # Salt's dir
101 ## states
102 S_DIR="/srv/salt"
103 ## formulas
104 F_DIR="/srv/formulas"
105 ##pillars
106 P_DIR="/srv/pillars"
107
108 apt-get update
109 apt-get install -y curl git
110
111 dpkg -l |grep salt-minion
112 if [ ${?} -eq 0 ]; then
113   echo "Salt already installed"
114 else
115   curl -L https://bootstrap.saltstack.com -o /tmp/bootstrap_salt.sh
116   sh /tmp/bootstrap_salt.sh -XUdfP -x python3
117   /bin/systemctl disable salt-minion.service
118 fi
119
120 # Set salt to masterless mode
121 cat > /etc/salt/minion << EOFSM
122 file_client: local
123 file_roots:
124   base:
125     - ${S_DIR}
126     - ${F_DIR}/*
127     - ${F_DIR}/*/test/salt/states
128
129 pillar_roots:
130   base:
131     - ${P_DIR}
132 EOFSM
133
134 mkdir -p ${S_DIR}
135 mkdir -p ${F_DIR}
136 mkdir -p ${P_DIR}
137
138 # States
139 cat > ${S_DIR}/top.sls << EOFTSLS
140 base:
141   '*':
142     - example_add_snakeoil_certs
143     - locale
144     - nginx.passenger
145     - postgres
146     - docker
147     - arvados
148 EOFTSLS
149
150 # Pillars
151 cat > ${P_DIR}/top.sls << EOFPSLS
152 base:
153   '*':
154     - arvados
155     - locale
156     - nginx_api_configuration
157     - nginx_controller_configuration
158     - nginx_keepproxy_configuration
159     - nginx_keepweb_configuration
160     - nginx_passenger
161     - nginx_websocket_configuration
162     - nginx_webshell_configuration
163     - nginx_workbench2_configuration
164     - nginx_workbench_configuration
165     - postgresql
166 EOFPSLS
167
168
169 # Get the formula and dependencies
170 cd ${F_DIR} || exit 1
171 for f in postgres arvados nginx docker locale; do
172   git clone https://github.com/saltstack-formulas/${f}-formula.git
173 done
174
175 if [ "x${BRANCH}" != "x" ]; then
176   cd ${F_DIR}/arvados-formula
177   git checkout -t origin/${BRANCH}
178   cd -
179 fi
180
181 # sed "s/__DOMAIN__/${DOMAIN}/g; s/__CLUSTER__/${CLUSTER}/g; s/__RELEASE__/${RELEASE}/g; s/__VERSION__/${VERSION}/g" \
182 #   ${CONFIG_DIR}/arvados_dev.sls > ${P_DIR}/arvados.sls
183
184 if [ "x${VAGRANT}" = "xyes" ]; then
185   SOURCE_PILLARS_DIR="/vagrant/${CONFIG_DIR}"
186 else
187   SOURCE_PILLARS_DIR="./${CONFIG_DIR}"
188 fi
189
190 # Replace cluster and domain name in the example pillars
191 for f in ${SOURCE_PILLARS_DIR}/*; do
192   # sed "s/example.net/${DOMAIN}/g; s/fixme/${CLUSTER}/g" \
193   sed "s/__DOMAIN__/${DOMAIN}/g;
194        s/__CLUSTER__/${CLUSTER}/g;
195        s/__RELEASE__/${RELEASE}/g;
196        s/__HOST_SSL_PORT__/${HOST_SSL_PORT}/g;
197        s/__GUEST_SSL_PORT__/${GUEST_SSL_PORT}/g;
198        s/__INITIAL_USER__/${INITIAL_USER}/g;
199        s/__INITIAL_USER_EMAIL__/${INITIAL_USER_EMAIL}/g;
200        s/__INITIAL_USER_PASSWORD__/${INITIAL_USER_PASSWORD}/g;
201        s/__VERSION__/${VERSION}/g" \
202   ${f} > ${P_DIR}/$(basename ${f})
203 done
204
205 # Let's write an /etc/hosts file that points all the hosts to localhost
206
207 echo "127.0.0.2 api keep keep0 collections download ws workbench workbench2 ${CLUSTER}.${DOMAIN} api.${CLUSTER}.${DOMAIN} keep.${CLUSTER}.${DOMAIN} keep0.${CLUSTER}.${DOMAIN} collections.${CLUSTER}.${DOMAIN} download.${CLUSTER}.${DOMAIN} ws.${CLUSTER}.${DOMAIN} workbench.${CLUSTER}.${DOMAIN} workbench2.${CLUSTER}.${DOMAIN}" >> /etc/hosts
208
209 # FIXME! #16992 Temporary fix for psql call in arvados-api-server
210 if [ -e /root/.psqlrc ]; then
211   if ! ( grep 'pset pager off' /root/.psqlrc ); then
212     RESTORE_PSQL="yes"
213     cp /root/.psqlrc /root/.psqlrc.provision.backup
214   fi
215 else
216   DELETE_PSQL="yes"
217 fi
218
219 echo '\pset pager off' >> /root/.psqlrc
220 # END FIXME! #16992 Temporary fix for psql call in arvados-api-server
221
222 # Now run the install
223 salt-call --local state.apply -l debug
224
225 # FIXME! #16992 Temporary fix for psql call in arvados-api-server
226 if [ "x${DELETE_PSQL}" = "xyes" ]; then
227   echo "Removing .psql file"
228   rm /root/.psqlrc
229 fi
230
231 if [ "x${RESTORE_PSQL}" = "xyes" ]; then
232   echo "Restroting .psql file"
233   mv -v /root/.psqlrc.provision.backup /root/.psqlrc
234 fi
235 # END FIXME! #16992 Temporary fix for psql call in arvados-api-server