-
Notifications
You must be signed in to change notification settings - Fork 28
/
init.sh
executable file
·404 lines (350 loc) · 12.1 KB
/
init.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/bin/bash
export rsa_key_size=4096
export data_path="local/certbot"
export nginx_server_file="local/nginx/conf.d/00-katalyst.conf"
export nginx_server_template_http="local/nginx/conf.d/katalyst-http.conf.template"
export nginx_server_template_https="local/nginx/conf.d/katalyst-https.conf.template"
# ensure we have wide path options to run in different environments
export PATH="$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
####
# Functions
#####
leCertEmit () {
if [ ! -e "$data_path/conf/options-ssl-nginx.conf" ] || [ ! -e "$data_path/conf/ssl-dhparams.pem" ]; then
echo "## Downloading recommended TLS parameters ..."
mkdir -p "$data_path/conf"
curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot-nginx/certbot_nginx/_internal/tls_configs/options-ssl-nginx.conf > "$data_path/conf/options-ssl-nginx.conf"
curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot/certbot/ssl-dhparams.pem > "$data_path/conf/ssl-dhparams.pem"
echo
fi
echo " Creating dummy certificate for $nginx_url..."
path="/etc/letsencrypt/live/$nginx_url"
mkdir -p "$data_path/conf/live/$nginx_url"
docker-compose run --rm --entrypoint "\
openssl req -x509 -nodes -newkey rsa:1024 -days 1\
-keyout '$path/privkey.pem' \
-out '$path/fullchain.pem' \
-subj '/CN=localhost'" certbot
echo -n "## Starting nginx ..."
docker-compose -f docker-compose.yml -f "platform.$(uname -s).yml" up --remove-orphans --force-recreate -d nginx
if test $? -ne 0; then
echo -n "Failed to start nginx... "
printMessage failed
exit 1
else
echo -n "## Dummy certificates created ..."
printMessage ok
fi
echo -n "## Deleting dummy certificate for $nginx_url ..."
docker-compose run --rm --entrypoint "\
rm -Rf /etc/letsencrypt/live/$nginx_url && \
rm -Rf /etc/letsencrypt/archive/$nginx_url && \
rm -Rf /etc/letsencrypt/renewal/$nginx_url.conf" certbot
echo
if test $? -ne 0; then
echo -n "Failed to remove files... "
printMessage failed
exit 1
else
echo -n "## Files deleted ... "
printMessage ok
fi
echo "## Requesting Let's Encrypt certificate for $nginx_url... "
domain_args=""
domain_args="$domain_args -d ${nginx_url}"
staging_arg="--staging"
if [ "$CATALYST_OWNER_CHANNEL" = "stable" ]; then
staging_arg=""
fi
# Select appropriate EMAIL arg
case "$EMAIL" in
"") email_arg="--register-unsafely-without-email" ;;
*) email_arg="--email $EMAIL" ;;
esac
# wait until the server responds
serverAlive=10
until [ $serverAlive -lt 1 ]; do
echo "Checking server liveness: ${CATALYST_URL}"
statusCode=$(curl --insecure -I -vv -s --http1.1 --output /dev/stderr --write-out "%{http_code}" "${CATALYST_URL}")
returnCode=$?
echo ">> statusCode: ${statusCode} returnCode: ${returnCode}"
if [ "$statusCode" -lt 500 ] && [ "$returnCode" -eq 0 ]; then
serverAlive=0
echo ">> Success"
else
((serverAlive=serverAlive+1))
echo ">> Waiting..."
sleep 6
fi
done
docker-compose run --rm --entrypoint "\
certbot certonly --webroot -w /var/www/certbot \
--no-eff-email \
$staging_arg \
$email_arg \
$domain_args \
--rsa-key-size $rsa_key_size \
--agree-tos \
--force-renewal" certbot
if test $? -ne 0; then
echo -n "Failed to request certificates... "
printMessage failed
exit 1
else
echo -n "## Certificates issued "
printMessage ok
fi
echo "## Reloading nginx ..."
docker-compose restart nginx
if test $? -ne 0; then
echo -n "Failed to reload nginx... "
printMessage failed
exit 1
else
echo -n "## Nginx Reloaded"
printMessage ok
fi
echo "## Going for the real certs..."
docker-compose run --rm --entrypoint "\
certbot certonly --webroot -w /var/www/certbot \
$email_arg \
$domain_args \
--no-eff-email \
--rsa-key-size $rsa_key_size \
--agree-tos \
--force-renewal" certbot
if test $? -ne 0; then
echo -n "Failed to request certificates. Handshake failed?, the URL is pointing to this server?: "
printMessage failed
exit 1
else
echo -n "Real certificates emited "
printMessage ok
fi
echo "## Reloading nginx with real certs..."
docker-compose restart nginx
if test $? -ne 0; then
echo -n "Failed to reload nginx... "
printMessage failed
exit 1
else
echo -n "## Nginx Reloaded"
printMessage ok
fi
}
printMessage () {
Type=$1
case ${Type} in
ok) echo -e "[\e[92m OK \e[39m]" ;;
failed) echo -e "[\e[91m FAILED \e[39m]" ;;
*) echo "";;
esac
}
##
# Main program
##
clear
echo -n "## Loading env variables... "
if ! [ -f ".env" ]; then
echo -n "Error: .env does not exist" >&2
printMessage failed
exit 1
else
source ".env"
printMessage ok
fi
if ! [ -f ".env-advanced" ]; then
echo -n "Error: .env-advanced does not exist" >&2
printMessage failed
exit 1
else
source ".env-advanced"
printMessage ok
fi
echo -n "## Checking if email is configured... "
if test ${EMAIL}; then
printMessage ok
else
echo -n "Failed to check the email."
printMessage failed
exit 1
fi
echo -n "## Checking if storage is configured... "
if test -d ${CONTENT_SERVER_STORAGE}; then
printMessage ok
else
echo -n "Failed to check the storage."
printMessage failed
exit 1
fi
echo -n "## Checking if catalyst url is configured... "
if test ${CATALYST_URL}; then
printMessage ok
else
echo -n "Failed to check the catalyst url."
printMessage failed
exit 1
fi
echo -n "## Checking if realm name is configured... "
if test ${REALM_NAME}; then
printMessage ok
else
printMessage failed
echo -e "\033[33m WARNING: REALM_NAME variable is undefined \033[39m"
fi
# Define defaults
export DOCKER_TAG=${DOCKER_TAG:-latest}
export LAMB2_DOCKER_TAG=${LAMB2_DOCKER_TAG:-latest}
REGENERATE=${REGENERATE:-0}
SLEEP_TIME=${SLEEP_TIME:-5}
MAINTENANCE_MODE=${MAINTENANCE_MODE:-0}
if [ "$DOCKER_TAG" != "latest" ]; then
echo -e "\033[33m WARNING: You are not running latest image of Catalyst's Content and Catalyst's Lambdas Nodes. \033[39m"
fi
if [ "$LAMB2_DOCKER_TAG" != "latest" ]; then
echo -e "\033[33m WARNING: You are not running latest image of Lamb2 Node. \033[39m"
fi
echo -n " - DOCKER_TAG: " ; echo -e "\033[33m ${DOCKER_TAG} \033[39m"
echo -n " - LAMB2_DOCKER_TAG: " ; echo -e "\033[33m ${LAMB2_DOCKER_TAG} \033[39m"
echo -n " - CATALYST_URL: " ; echo -e "\033[33m ${CATALYST_URL} \033[39m"
echo -n " - CONTENT_SERVER_STORAGE: " ; echo -e "\033[33m ${CONTENT_SERVER_STORAGE} \033[39m"
echo -n " - EMAIL: " ; echo -e "\033[33m ${EMAIL} \033[39m"
echo -n " - ETH_NETWORK: " ; echo -e "\033[33m ${ETH_NETWORK} \033[39m"
echo -n " - REGENERATE: " ; echo -e "\033[33m ${REGENERATE} \033[39m"
echo ""
echo "Starting in ${SLEEP_TIME} seconds... " && sleep "$SLEEP_TIME"
# Check if docker compose is installed
if ! [ -x "$(command -v docker-compose)" ]; then
echo -n "Error: docker-compose is not installed..." >&2
printMessage failed
exit 1
fi
if ! [ -f ".env-database-admin" ]; then
ROOT_PASSWORD="$(openssl rand -hex 8)"
{
echo "POSTGRES_USER=postgres"
echo "POSTGRES_PASSWORD=${ROOT_PASSWORD}"
echo "POSTGRES_DB=postgres"
echo "POSTGRES_HOST=postgres"
echo "POSTGRES_PORT=5432"
} > .env-database-admin
fi
source ".env-database-admin"
if ! [ -f ".env-database-metrics" ]; then
{
echo "DATA_SOURCE_NAME=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}?sslmode=disable"
echo "DATA_SOURCE_URI=${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}?sslmode=disable"
echo "DATA_SOURCE_USER=${POSTGRES_USER}"
echo "DATA_SOURCE_PASS=${POSTGRES_PASSWORD}"
echo "PG_EXPORTER_AUTO_DISCOVER_DATABASES=true"
} > .env-database-metrics
fi
source ".env-database-metrics"
if ! [ -f ".env-database-content" ]; then
USER="cs$(openssl rand -hex 4)"
PASSWORD="$(openssl rand -hex 8)"
{
echo "POSTGRES_CONTENT_USER=${USER}"
echo "POSTGRES_CONTENT_PASSWORD=${PASSWORD}"
echo "POSTGRES_CONTENT_DB=content"
} > .env-database-content
fi
source ".env-database-content"
if [ -z "$POSTGRES_CONTENT_PASSWORD" ]; then
echo "Empty POSTGRES_CONTENT_PASSWORD"
printMessage failed
exit 1
fi
if [ -z "$POSTGRES_CONTENT_USER" ]; then
echo "Empty POSTGRES_CONTENT_USER"
printMessage failed
exit 1
fi
if [ -z "$POSTGRES_PASSWORD" ]; then
echo "Empty POSTGRES_PASSWORD"
printMessage failed
exit 1
fi
docker-compose pull "nginx"
if [ $? -ne 0 ]; then
echo -n "Failed to pull nginx"
printMessage failed
exit 1
fi
docker pull "quay.io/decentraland/catalyst:${DOCKER_TAG:-latest}"
if [ $? -ne 0 ]; then
echo -n "Failed to pull the content's docker image with tag ${DOCKER_TAG:-latest}"
printMessage failed
exit 1
fi
docker pull "quay.io/decentraland/lamb2:${LAMB2_DOCKER_TAG:-latest}"
if [ $? -ne 0 ]; then
echo -n "Failed to pull the lamb2' docker image with tag ${LAMB2_DOCKER_TAG:-latest}"
printMessage failed
exit 1
fi
if [ $? -ne 0 ]; then
echo -n "Failed to pull the lamb2' docker image with tag ${LAMB2_DOCKER_TAG:-latest}"
printMessage failed
exit 1
fi
docker-compose stop nginx
if [ $? -ne 0 ]; then
echo -n "Failed to stop nginx! "
printMessage failed
exit 1
fi
# If the server is localhost, do not enable https
# Setup the nginx conf file with plain http
# else, create new certs
export nginx_url="$(echo "${CATALYST_URL##*/}")"
if [ "${CATALYST_URL}" != "http://localhost" ]; then
echo "## Using HTTPS."
echo -n "## Replacing value \"\$katalyst_host\" on nginx server file ${nginx_url}... "
sed "s/\$katalyst_host/${nginx_url}/g" ${nginx_server_template_https} > ${nginx_server_file}
# This is the URL without the 'http/s'
# Needed to place the server on nginx conf file
if [ -d "$data_path/conf/live/$nginx_url" ]; then
echo "Existing data found for \$nginx_url."
if test ${REGENERATE} -eq 1; then
leCertEmit $nginx_url
else
echo "## Current certificates will be used."
fi
else
echo "## No certificates found. Performing certificate creation... "
leCertEmit $nginx_url
if test $? -ne 0; then
printMessage failed
echo -n "Failed to deploy certificates. Take a look above for errors!"
exit 1
fi
fi
echo -n "## Finalizing Let's Encrypt setup... "
printMessage ok
else
echo "## Using HTTP because CATALYST_URL is set to http://localhost"
echo -n "## Replacing value \$katalyst_host on nginx server file... "
sed "s/\$katalyst_host/${nginx_url}/g" ${nginx_server_template_http} > ${nginx_server_file}
printMessage ok
fi
matches=$(cat ${nginx_server_file} | grep ${nginx_url} | wc -l)
if test $matches -eq 0; then
printMessage failed
echo "Failed to perform changes on nginx server file, no changes found. Look into ${nginx_server_file} for more information"
exit 1
fi
echo "## Restarting containers... "
docker-compose down
if test ${MAINTENANCE_MODE} -eq 1; then
echo 'Running maintenance...'
docker-compose -f docker-compose-maintenance.yml up -d
else
docker-compose -f docker-compose.yml -f "platform.$(uname -s).yml" up --remove-orphans -d nginx
if test $? -ne 0; then
echo -n "Failed to start catalyst node"
printMessage failed
exit 1
fi
echo "## Catalyst server is up and running at $CATALYST_URL"
fi