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