This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
docker-entrypoint.sh
executable file
·436 lines (392 loc) · 11 KB
/
docker-entrypoint.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#!/bin/bash
set -eu
# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
exit 1
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}
# allow the container to be started with `--user`
if [[ "$1" == rabbitmq* ]] && [ "$(id -u)" = '0' ]; then
if [ "$1" = 'rabbitmq-server' ]; then
chown -R rabbitmq /var/lib/rabbitmq
fi
exec su-exec rabbitmq "$BASH_SOURCE" "$@"
fi
# backwards compatibility for old environment variables
: "${RABBITMQ_SSL_CERTFILE:=${RABBITMQ_SSL_CERT_FILE:-}}"
: "${RABBITMQ_SSL_KEYFILE:=${RABBITMQ_SSL_KEY_FILE:-}}"
: "${RABBITMQ_SSL_CACERTFILE:=${RABBITMQ_SSL_CA_FILE:-}}"
# "management" SSL config should default to using the same certs
: "${RABBITMQ_MANAGEMENT_SSL_CACERTFILE:=$RABBITMQ_SSL_CACERTFILE}"
: "${RABBITMQ_MANAGEMENT_SSL_CERTFILE:=$RABBITMQ_SSL_CERTFILE}"
: "${RABBITMQ_MANAGEMENT_SSL_KEYFILE:=$RABBITMQ_SSL_KEYFILE}"
# Allowed env vars that will be read from mounted files (i.e. Docker Secrets):
fileEnvKeys=(
default_user
default_pass
)
# https://www.rabbitmq.com/configure.html
sslConfigKeys=(
cacertfile
certfile
depth
fail_if_no_peer_cert
keyfile
verify
)
managementConfigKeys=(
"${sslConfigKeys[@]/#/ssl_}"
)
rabbitConfigKeys=(
default_pass
default_user
default_vhost
hipe_compile
vm_memory_high_watermark
)
mqttConfigKeys=(
vhost
exchange
default_user
default_pass
)
fileConfigKeys=(
management_ssl_cacertfile
management_ssl_certfile
management_ssl_keyfile
ssl_cacertfile
ssl_certfile
ssl_keyfile
)
allConfigKeys=(
"${managementConfigKeys[@]/#/management_}"
"${rabbitConfigKeys[@]}"
"${sslConfigKeys[@]/#/ssl_}"
"${mqttConfigKeys[@]/#/mqtt_}"
)
declare -A configDefaults=(
[management_ssl_fail_if_no_peer_cert]='false'
[management_ssl_verify]='verify_none'
[ssl_fail_if_no_peer_cert]='false'
[ssl_verify]='verify_peer'
[mqtt_default_user]='guest'
[mqtt_default_pass]='guest'
[mqtt_vhost]='/'
[mqtt_exchange]='amq.topic'
)
haveConfig=
haveSslConfig=
haveManagementSslConfig=
for fileEnvKey in "${fileEnvKeys[@]}"; do file_env "RABBITMQ_${fileEnvKey^^}"; done
for conf in "${allConfigKeys[@]}"; do
var="RABBITMQ_${conf^^}"
val="${!var:-}"
if [ "$val" ]; then
if [ "${configDefaults[$conf]:-}" ] && [ "${configDefaults[$conf]}" = "$val" ]; then
# if the value set is the same as the default, treat it as if it isn't set
continue
fi
haveConfig=1
case "$conf" in
ssl_*) haveSslConfig=1 ;;
management_ssl_*) haveManagementSslConfig=1 ;;
esac
fi
done
if [ "$haveSslConfig" ]; then
missing=()
for sslConf in cacertfile certfile keyfile; do
var="RABBITMQ_SSL_${sslConf^^}"
val="${!var}"
if [ -z "$val" ]; then
missing+=( "$var" )
fi
done
if [ "${#missing[@]}" -gt 0 ]; then
{
echo
echo 'error: SSL requested, but missing required configuration'
for miss in "${missing[@]}"; do
echo " - $miss"
done
echo
} >&2
exit 1
fi
fi
missingFiles=()
for conf in "${fileConfigKeys[@]}"; do
var="RABBITMQ_${conf^^}"
val="${!var}"
if [ "$val" ] && [ ! -f "$val" ]; then
missingFiles+=( "$val ($var)" )
fi
done
if [ "${#missingFiles[@]}" -gt 0 ]; then
{
echo
echo 'error: files specified, but missing'
for miss in "${missingFiles[@]}"; do
echo " - $miss"
done
echo
} >&2
exit 1
fi
# set defaults for missing values (but only after we're done with all our checking so we don't throw any of that off)
for conf in "${!configDefaults[@]}"; do
default="${configDefaults[$conf]}"
var="RABBITMQ_${conf^^}"
[ -z "${!var:-}" ] || continue
eval "export $var=\"\$default\""
done
# If long & short hostnames are not the same, use long hostnames
if [ "$(hostname)" != "$(hostname -s)" ]; then
: "${RABBITMQ_USE_LONGNAME:=true}"
fi
if [ "${RABBITMQ_ERLANG_COOKIE:-}" ]; then
cookieFile='/var/lib/rabbitmq/.erlang.cookie'
if [ -e "$cookieFile" ]; then
if [ "$(cat "$cookieFile" 2>/dev/null)" != "$RABBITMQ_ERLANG_COOKIE" ]; then
echo >&2
echo >&2 "warning: $cookieFile contents do not match RABBITMQ_ERLANG_COOKIE"
echo >&2
fi
else
echo "$RABBITMQ_ERLANG_COOKIE" > "$cookieFile"
fi
chmod 600 "$cookieFile"
fi
# prints "$2$1$3$1...$N"
join() {
local sep="$1"; shift
local out; printf -v out "${sep//%/%%}%s" "$@"
echo "${out#$sep}"
}
indent() {
if [ "$#" -gt 0 ]; then
echo "$@"
else
cat
fi | sed 's/^/\t/g'
}
rabbit_array() {
echo -n '['
case "$#" in
0) echo -n ' ' ;;
1) echo -n " $1 " ;;
*)
local vals="$(join $',\n' "$@")"
echo
indent "$vals"
esac
echo -n ']'
}
rabbit_env_config() {
local prefix="$1"; shift
local ret=()
local conf
for conf; do
local var="rabbitmq${prefix:+_$prefix}_$conf"
var="${var^^}"
local val="${!var:-}"
local rawVal=
case "$conf" in
verify|fail_if_no_peer_cert|depth)
[ "$val" ] || continue
rawVal="$val"
;;
hipe_compile)
[ "$val" ] && rawVal='true' || rawVal='false'
;;
cacertfile|certfile|keyfile)
[ "$val" ] || continue
rawVal='"'"$val"'"'
;;
*)
[ "$val" ] || continue
rawVal='<<"'"$val"'">>'
;;
esac
[ "$rawVal" ] || continue
ret+=( "{ $conf, $rawVal }" )
done
join $'\n' "${ret[@]}"
}
shouldWriteConfig="$haveConfig"
if [ ! -f /etc/rabbitmq/rabbitmq.config ]; then
shouldWriteConfig=1
fi
if [ "$1" = 'rabbitmq-server' ] && [ "$shouldWriteConfig" ]; then
fullConfig=()
rabbitConfig=(
"{ loopback_users, $(rabbit_array) }"
"{ tcp_listeners, $(rabbit_array 5672) }"
)
# determine whether to set "vm_memory_high_watermark" (based on cgroups)
memTotalKb=
if [ -r /proc/meminfo ]; then
memTotalKb="$(awk -F ':? +' '$1 == "MemTotal" { print $2; exit }' /proc/meminfo)"
fi
memLimitB=
if [ -r /sys/fs/cgroup/memory/memory.limit_in_bytes ]; then
# "18446744073709551615" is a valid value for "memory.limit_in_bytes", which is too big for Bash math to handle
# "$(( 18446744073709551615 / 1024 ))" = 0; "$(( 18446744073709551615 * 40 / 100 ))" = 0
memLimitB="$(awk -v totKb="$memTotalKb" '{
limB = $0;
limKb = limB / 1024;
if (!totKb || limKb < totKb) {
printf "%.0f\n", limB;
}
}' /sys/fs/cgroup/memory/memory.limit_in_bytes)"
fi
if [ -n "$memLimitB" ]; then
# if we have a cgroup memory limit, let's inform RabbitMQ of what it is (so it can calculate vm_memory_high_watermark properly)
# https://github.com/rabbitmq/rabbitmq-server/pull/1234
rabbitConfig+=( "{ total_memory_available_override_value, $memLimitB }" )
fi
if [ "${RABBITMQ_VM_MEMORY_HIGH_WATERMARK:-}" ]; then
# https://github.com/docker-library/rabbitmq/pull/105#issuecomment-242165822
vmMemoryHighWatermark="$(
awk '
/^[0-9]*[.][0-9]+$|^[0-9]+([.][0-9]+)?%$/ {
perc = $0;
if (perc ~ /%$/) {
gsub(/%$/, "", perc);
perc = perc / 100;
}
if (perc > 1.0 || perc <= 0.0) {
printf "error: invalid percentage for vm_memory_high_watermark: %s (must be > 0%%, <= 100%%)\n", $0 > "/dev/stderr";
exit 1;
}
printf "%0.03f\n", perc;
next;
}
/^[0-9]+$/ {
printf "{ absolute, %s }\n", $0;
next;
}
/^[0-9]+([.][0-9]+)?[a-zA-Z]+$/ {
printf "{ absolute, \"%s\" }\n", $0;
next;
}
{
printf "error: unexpected input for vm_memory_high_watermark: %s\n", $0;
exit 1;
}
' <(echo "$RABBITMQ_VM_MEMORY_HIGH_WATERMARK")
)"
if [ "$vmMemoryHighWatermark" ]; then
# https://www.rabbitmq.com/memory.html#memsup-usage
rabbitConfig+=( "{ vm_memory_high_watermark, $vmMemoryHighWatermark }" )
fi
fi
if [ "$haveSslConfig" ]; then
IFS=$'\n'
rabbitSslOptions=( $(rabbit_env_config 'ssl' "${sslConfigKeys[@]}") )
unset IFS
rabbitConfig+=(
"{ ssl_listeners, $(rabbit_array 5671) }"
"{ ssl_options, $(rabbit_array "${rabbitSslOptions[@]}") }"
)
else
rabbitConfig+=(
"{ ssl_listeners, $(rabbit_array) }"
)
fi
IFS=$'\n'
rabbitConfig+=( $(rabbit_env_config '' "${rabbitConfigKeys[@]}") )
unset IFS
fullConfig+=( "{ rabbit, $(rabbit_array "${rabbitConfig[@]}") }" )
# if management plugin is installed, generate config for it
# https://www.rabbitmq.com/management.html#configuration
if [ "$(rabbitmq-plugins list -m -e rabbitmq_management)" ]; then
rabbitManagementConfig=()
if [ "$haveManagementSslConfig" ]; then
IFS=$'\n'
rabbitManagementSslOptions=( $(rabbit_env_config 'management_ssl' "${sslConfigKeys[@]}") )
unset IFS
rabbitManagementListenerConfig+=(
'{ port, 15671 }'
'{ ssl, true }'
"{ ssl_opts, $(rabbit_array "${rabbitManagementSslOptions[@]}") }"
)
else
rabbitManagementListenerConfig+=(
'{ port, 15672 }'
'{ ssl, false }'
)
fi
rabbitManagementConfig+=(
"{ listener, $(rabbit_array "${rabbitManagementListenerConfig[@]}") }"
)
# if definitions file exists, then load it
# https://www.rabbitmq.com/management.html#load-definitions
managementDefinitionsFile='/etc/rabbitmq/definitions.json'
if [ -f "${managementDefinitionsFile}" ]; then
# see also https://github.com/docker-library/rabbitmq/pull/112#issuecomment-271485550
rabbitManagementConfig+=(
"{ load_definitions, \"$managementDefinitionsFile\" }"
)
fi
fullConfig+=(
"{ rabbitmq_management, $(rabbit_array "${rabbitManagementConfig[@]}") }"
)
fi
# if mqtt plugin is installed, generate config for it
# https://www.rabbitmq.com/mqtt.html#config
if [ "$(rabbitmq-plugins list -m -e rabbitmq_mqtt)" ]; then
rabbitMQTTConfig=()
IFS=$'\n'
rabbitMQTTConfig+=( $(rabbit_env_config 'mqtt' "${mqttConfigKeys[@]}") )
unset IFS
rabbitMQTTConfig+=(
"{ tcp_listeners, $(rabbit_array "1883") }"
)
if [ "$haveSslConfig" ]; then
rabbitMQTTConfig+=(
"{ ssl_listeners, $(rabbit_array "8883") }"
)
else
rabbitMQTTConfig+=(
"{ ssl_listeners, $(rabbit_array) }"
)
fi
fullConfig+=(
"{ rabbitmq_mqtt, $(rabbit_array "${rabbitMQTTConfig[@]}") }"
)
fi
echo "$(rabbit_array "${fullConfig[@]}")." > /etc/rabbitmq/rabbitmq.config
fi
combinedSsl='/tmp/combined.pem'
if [ "$haveSslConfig" ] && [[ "$1" == rabbitmq* ]] && [ ! -f "$combinedSsl" ]; then
# Create combined cert
cat "$RABBITMQ_SSL_CERTFILE" "$RABBITMQ_SSL_KEYFILE" > "$combinedSsl"
chmod 0400 "$combinedSsl"
fi
if [ "$haveSslConfig" ] && [ -f "$combinedSsl" ]; then
# More ENV vars for make clustering happiness
# we don't handle clustering in this script, but these args should ensure
# clustered SSL-enabled members will talk nicely
export ERL_SSL_PATH="$(erl -eval 'io:format("~p", [code:lib_dir(ssl, ebin)]),halt().' -noshell)"
export RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS="-pa $ERL_SSL_PATH -proto_dist inet_tls -ssl_dist_opt server_certfile $combinedSsl -ssl_dist_opt server_secure_renegotiate true client_secure_renegotiate true"
export RABBITMQ_CTL_ERL_ARGS="$RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS"
fi
cat /etc/rabbitmq/rabbitmq.config
exec "$@"