-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·54 lines (41 loc) · 1.28 KB
/
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
#!/bin/sh
set -exo pipefail
if [ "$#" -eq 0 ] || [ "${1#-}" != "$1" ]; then
set -- nats-server "$@"
fi
# Enable jetstream if the env var is set
if ! [ -z "$NATS_JETSTREAM" ]; then
cat << EOF >> /etc/nats/nats-server.conf
# Enable JetStream
jetstream {
store_dir = /data/jetstream
}
EOF
fi
# If we're running in Fly, enable clustering and ensure the hostname is set
if ! [ -z "$FLY_APP_NAME" ]; then
export HOSTNAME=$FLY_MACHINE_ID
cat << EOF >> /etc/nats/nats-server.conf
# Advertise our machine's DNS name
client_advertise = ${FLY_ALLOC_ID}.vm.${FLY_APP_NAME}.internal:4222
# Enable clustering
cluster {
name = ${FLY_APP_NAME}
# Route connections to be received on any interface on port 6222
host = ::
port = 6222
# Advertise our machine's DNS name
cluster_advertise = ${FLY_ALLOC_ID}.vm.${FLY_APP_NAME}.internal:6222
# Routes are actively solicited and connected to from this server
routes = [
EOF
local_ip=$(dig +short AAAA $FLY_ALLOC_ID.vm.$FLY_APP_NAME.internal)
# Add all routes except our own
for route in $(dig +short AAAA top3.nearest.of.$FLY_APP_NAME.internal); do
if [ "$route" != "$local_ip" ]; then
echo " \"nats://[$route]:6222\"" >> /etc/nats/nats-server.conf
fi
done
echo -e " ]\n}" >> /etc/nats/nats-server.conf
fi
exec "$@"