blob: 4dcebc88830057c24ce4ef7fb76e6d6aba38c6a3 [file] [log] [blame]
Guillaume Lambert85b14922021-03-12 13:53:18 +01001#!/bin/sh
Guillaume Lambertf3454862021-03-10 16:09:31 +01002
mahendrr8b20f772019-05-03 06:50:10 +00003set -e
4
5# first arg is `-f` or `--some-option`
6# or there are no args
7if [ "$#" -eq 0 ] || [ "${1#-}" != "$1" ]; then
8 set -- cassandra -f "$@"
9fi
10
11# allow the container to be started with `--user`
12if [ "$1" = 'cassandra' -a "$(id -u)" = '0' ]; then
13 find /var/lib/cassandra /var/log/cassandra "$CASSANDRA_CONFIG" \
14 \! -user cassandra -exec chown cassandra '{}' +
Guillaume Lambert134188d2021-03-11 13:42:23 +010015 exec gosu cassandra "$0" "$@"
mahendrr8b20f772019-05-03 06:50:10 +000016fi
17
18_ip_address() {
19 # scrape the first non-localhost IP address of the container
20 # in Swarm Mode, we often get two IPs -- the container IP, and the (shared) VIP, and the container IP should always be first
21 ip address | awk '
22 $1 == "inet" && $NF != "lo" {
23 gsub(/\/.+$/, "", $2)
24 print $2
25 exit
26 }
27 '
28}
29
30# "sed -i", but without "mv" (which doesn't work on a bind-mounted file, for example)
Guillaume Lambertc2dd7442021-03-12 11:42:42 +010031_sed_in_place() {
mahendrr8b20f772019-05-03 06:50:10 +000032 local filename="$1"; shift
33 local tempFile
34 tempFile="$(mktemp)"
35 sed "$@" "$filename" > "$tempFile"
36 cat "$tempFile" > "$filename"
37 rm "$tempFile"
38}
39
40if [ "$1" = 'cassandra' ]; then
41 : ${CASSANDRA_RPC_ADDRESS='0.0.0.0'}
42
43 : ${CASSANDRA_LISTEN_ADDRESS='auto'}
44 if [ "$CASSANDRA_LISTEN_ADDRESS" = 'auto' ]; then
45 CASSANDRA_LISTEN_ADDRESS="$(_ip_address)"
46 fi
47
48 : ${CASSANDRA_BROADCAST_ADDRESS="$CASSANDRA_LISTEN_ADDRESS"}
49
50 if [ "$CASSANDRA_BROADCAST_ADDRESS" = 'auto' ]; then
51 CASSANDRA_BROADCAST_ADDRESS="$(_ip_address)"
52 fi
53 : ${CASSANDRA_BROADCAST_RPC_ADDRESS:=$CASSANDRA_BROADCAST_ADDRESS}
54
55 if [ -n "${CASSANDRA_NAME:+1}" ]; then
56 : ${CASSANDRA_SEEDS:="cassandra"}
57 fi
58 : ${CASSANDRA_SEEDS:="$CASSANDRA_BROADCAST_ADDRESS"}
59
Guillaume Lambertc2dd7442021-03-12 11:42:42 +010060 _sed_in_place "$CASSANDRA_CONFIG/cassandra.yaml" \
mahendrr8b20f772019-05-03 06:50:10 +000061 -r 's/(- seeds:).*/\1 "'"$CASSANDRA_SEEDS"'"/'
62
63 for yaml in \
64 broadcast_address \
65 broadcast_rpc_address \
66 cluster_name \
67 endpoint_snitch \
68 listen_address \
69 num_tokens \
70 rpc_address \
71 start_rpc \
72 authenticator \
73 ; do
Guillaume Lambert05c791d2021-03-12 10:59:34 +010074 var="CASSANDRA_$(echo $yaml | tr '[:lower:]' '[:upper:]')"
Guillaume Lambertf3454862021-03-10 16:09:31 +010075 # eval presents no security issue here because of limited possible values of var
76 eval val=\$$var
mahendrr8b20f772019-05-03 06:50:10 +000077 if [ "$val" ]; then
Guillaume Lambertc2dd7442021-03-12 11:42:42 +010078 _sed_in_place "$CASSANDRA_CONFIG/cassandra.yaml" \
mahendrr8b20f772019-05-03 06:50:10 +000079 -r 's/^(# )?('"$yaml"':).*/\2 '"$val"'/'
80 fi
81 done
82
83 for rackdc in dc rack; do
Guillaume Lambert05c791d2021-03-12 10:59:34 +010084 var="CASSANDRA_$(echo $rackdc | tr '[:lower:]' '[:upper:]')"
Guillaume Lambertf3454862021-03-10 16:09:31 +010085 # eval presents no security issue here because of limited possible values of var
86 eval val=\$$var
mahendrr8b20f772019-05-03 06:50:10 +000087 if [ "$val" ]; then
Guillaume Lambertc2dd7442021-03-12 11:42:42 +010088 _sed_in_place "$CASSANDRA_CONFIG/cassandra-rackdc.properties" \
mahendrr8b20f772019-05-03 06:50:10 +000089 -r 's/^('"$rackdc"'=).*/\1 '"$val"'/'
90 fi
91 done
92fi
93
94exec "$@"
95