-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.bash
165 lines (143 loc) · 6.48 KB
/
entrypoint.bash
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
#!/bin/bash
echo "[bento_gateway] [entrypoint] ========== begin gateway entrypoint =========="
export BENTO_GATEWAY_CONF_DIR='/usr/local/openresty/nginx/conf'
# WORKDIR: /gateway
# Utility function - historically there's been some inconsistency in what value we use for "true"
# for feature flags / some other environment variables.
# This function ensures that all of those values get cast to '1', so only one value needs to be checked.
function true_values_to_1 () {
if [[ "$1" == 1 || "$1" == "true" || "$1" == "True" || "$1" == "yes" ]]; then
echo "1"
else
echo "0"
fi
}
# Utility function - process service templates into finalized NGINX configs
function service_templates_to_confs () {
# $1 = "services" or "public_services"
for f in $(ls /gateway/$1/*.conf.tpl); do
filename=$(basename -- "$f")
outfile="${filename%%.*}.conf"
enable_check="$(python /gateway/src/service_conf_check.py < $f | tr -d '\n')"
echo "[bento_gateway] [entrypoint] ${filename}: enable_check=${enable_check}"
if [[ "${enable_check}" == "true" ]]; then
echo "[bento_gateway] [entrypoint] writing ${outfile}"
envsubst "$(cat ./VARIABLES)" \
< "${f}" \
> "${BENTO_GATEWAY_CONF_DIR}/bento_${1}/${outfile}"
else
echo "[bento_gateway] [entrypoint] not enabling ${filename}"
fi
done
}
# Check required environment variables that may accidentally be unset (i.e., need to be set by hand)
if [[ -z "${BENTOV2_SESSION_SECRET}" ]]; then
echo "[bento_gateway] [entrypoint] BENTOV2_SESSION_SECRET is not set. Exiting..." 1>&2
exit 1
fi
# Gather a list of all environment variables to use for the NGINX conf. template:
touch ./VARIABLES
echo "[bento_gateway] [entrypoint] gathering environment variables for configuration templates"
for v in $(env | awk -F "=" '{print $1}' | grep "BENTO*"); do
echo "\${${v}}" >> ./VARIABLES
done
for v in $(env | awk -F "=" '{print $1}' | grep "GATEWAY*"); do
echo "\${${v}}" >> ./VARIABLES
done
# Process the NGINX configuration templates, using only the selected variables: ---------------------------------------
echo "[bento_gateway] [entrypoint] writing NGINX configuration files"
echo "[bento_gateway] [entrypoint] creating cbioportal.conf.pre"
envsubst "$(cat ./VARIABLES)" \
< ./conf/cbioportal.conf.tpl \
> ./cbioportal.conf.pre
CORS_PATH="${BENTO_GATEWAY_CONF_DIR}/cors.conf"
echo "[bento_gateway] [entrypoint] creating ${CORS_PATH}"
envsubst "$(cat ./VARIABLES)" \
< ./conf/cors.conf.tpl \
> "${CORS_PATH}"
echo "[bento_gateway] [entrypoint] creating nginx.conf.pre"
envsubst "$(cat ./VARIABLES)" \
< ./conf/nginx.conf.tpl \
> ./nginx.conf.pre
# ----------------------------------------------------------------------------------------------------------------------
# Run "fine-tuning", i.e., processing the configuration files to *remove* chunks that aren't relevant to the environment
# variable settings for this instance. ---------------------------------------------------------------------------------
use_tls="$(true_values_to_1 $BENTO_GATEWAY_USE_TLS)"
# Run fine-tuning on cbioportal.conf.pre
if [[ "${use_tls}" == 0 ]]; then
echo "[bento_gateway] [entrypoint] Fine-tuning cbioportal.conf to not use TLS"
sed -i.bak \
'/tpl__tls_yes__start/,/tpl__tls_yes__end/d' \
./cbioportal.conf.pre
else
echo "[bento_gateway] [entrypoint] Fine-tuning cbioportal.conf to use TLS"
sed -i.bak \
'/tpl__tls_no__start/,/tpl__tls_no__end/d' \
./cbioportal.conf.pre
fi
# Run fine-tuning on nginx.conf.pre
if [[ "${use_tls}" == 0 ]]; then
echo "[bento_gateway] [entrypoint] Fine-tuning nginx.conf to not use TLS"
sed -i.bak \
'/tpl__tls_yes__start/,/tpl__tls_yes__end/d' \
./nginx.conf.pre
else
echo "[bento_gateway] [entrypoint] Fine-tuning nginx.conf to use TLS"
sed -i.bak \
'/tpl__tls_no__start/,/tpl__tls_no__end/d' \
./nginx.conf.pre
fi
if [[ "$(true_values_to_1 $BENTOV2_USE_EXTERNAL_IDP)" == 1 ]]; then
echo "[bento_gateway] [entrypoint] Fine-tuning nginx.conf to use an external IDP"
sed -i.bak \
'/tpl__internal_idp__start/,/tpl__internal_idp__end/d' \
./nginx.conf.pre
else
echo "[bento_gateway] [entrypoint] Fine-tuning nginx.conf to use an internal IDP"
fi
if [[ "$(true_values_to_1 $BENTOV2_USE_BENTO_PUBLIC)" == 1 ]]; then
echo "[bento_gateway] [entrypoint] Fine-tuning nginx.conf to use bento_public"
sed -i.bak \
'/tpl__do_not_use_bento_public__start/,/tpl__do_not_use_bento_public__end/d' \
./nginx.conf.pre
else
echo "[bento_gateway] [entrypoint] Fine-tuning nginx.conf to disable bento_public"
sed -i.bak \
'/tpl__use_bento_public__start/,/tpl__use_bento_public__end/d' \
./nginx.conf.pre
fi
if [[ "$(true_values_to_1 $BENTO_CBIOPORTAL_ENABLED)" == 1 ]]; then
echo "[bento_gateway] [entrypoint] Fine-tuning nginx.conf to use cBioPortal"
else
echo "[bento_gateway] [entrypoint] Fine-tuning nginx.conf to disable cBioPortal"
sed -i.bak \
'/tpl__use_cbioportal__start/,/tpl__use_cbioportal__end/d' \
./nginx.conf.pre
fi
if [[ "$(true_values_to_1 $BENTO_USE_DOMAIN_REDIRECT)" == 1 ]]; then
echo "[bento_gateway] [entrypoint] Fine-tuning nginx.conf to use domain redirect"
else
echo "[bento_gateway] [entrypoint] Fine-tuning nginx.conf to disable domain redirect"
sed -i.bak \
'/tpl__redirect_yes__start/,/tpl__redirect_yes__end/d' \
./nginx.conf.pre
fi
# ----------------------------------------------------------------------------------------------------------------------
# Generate final configuration files / locations -----------------------------------------------------------------------
# - Move cbioportal.conf into position
cp ./cbioportal.conf.pre "${BENTO_GATEWAY_CONF_DIR}/cbioportal.conf"
# - Move nginx.conf into position
cp ./nginx.conf.pre "${BENTO_GATEWAY_CONF_DIR}/nginx.conf"
# - Remove pre-final configuration files + any backups
rm ./*.conf.pre*
# ----------------------------------------------------------------------------------------------------------------------
cat "${BENTO_GATEWAY_CONF_DIR}/nginx.conf"
# Process any public service templates, using only the selected variables:
echo "[bento_gateway] [entrypoint] writing public service NGINX configuration"
service_templates_to_confs "public_services"
# Process any private service templates, using only the selected variables:
echo "[bento_gateway] [entrypoint] writing private service NGINX configuration"
service_templates_to_confs "services"
# Start OpenResty
echo "[bento_gateway] [entrypoint] starting OpenResty"
/usr/local/openresty/bin/openresty -g 'daemon off;'