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