Guillaume Lambert | 85b1492 | 2021-03-12 13:53:18 +0100 | [diff] [blame] | 1 | #!/bin/sh |
Guillaume Lambert | f345486 | 2021-03-10 16:09:31 +0100 | [diff] [blame] | 2 | |
mahendrr | 8b20f77 | 2019-05-03 06:50:10 +0000 | [diff] [blame] | 3 | set -e |
| 4 | |
| 5 | # first arg is `-f` or `--some-option` |
| 6 | # or there are no args |
| 7 | if [ "$#" -eq 0 ] || [ "${1#-}" != "$1" ]; then |
| 8 | set -- cassandra -f "$@" |
| 9 | fi |
| 10 | |
| 11 | # allow the container to be started with `--user` |
| 12 | if [ "$1" = 'cassandra' -a "$(id -u)" = '0' ]; then |
| 13 | find /var/lib/cassandra /var/log/cassandra "$CASSANDRA_CONFIG" \ |
| 14 | \! -user cassandra -exec chown cassandra '{}' + |
Guillaume Lambert | 134188d | 2021-03-11 13:42:23 +0100 | [diff] [blame] | 15 | exec gosu cassandra "$0" "$@" |
mahendrr | 8b20f77 | 2019-05-03 06:50:10 +0000 | [diff] [blame] | 16 | fi |
| 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 Lambert | c2dd744 | 2021-03-12 11:42:42 +0100 | [diff] [blame] | 31 | _sed_in_place() { |
guillaume.lambert | f657a81 | 2021-12-07 20:21:17 +0100 | [diff] [blame] | 32 | local filename |
| 33 | filename="$1"; shift |
mahendrr | 8b20f77 | 2019-05-03 06:50:10 +0000 | [diff] [blame] | 34 | local tempFile |
| 35 | tempFile="$(mktemp)" |
| 36 | sed "$@" "$filename" > "$tempFile" |
| 37 | cat "$tempFile" > "$filename" |
| 38 | rm "$tempFile" |
| 39 | } |
| 40 | |
| 41 | if [ "$1" = 'cassandra' ]; then |
| 42 | : ${CASSANDRA_RPC_ADDRESS='0.0.0.0'} |
| 43 | |
| 44 | : ${CASSANDRA_LISTEN_ADDRESS='auto'} |
| 45 | if [ "$CASSANDRA_LISTEN_ADDRESS" = 'auto' ]; then |
| 46 | CASSANDRA_LISTEN_ADDRESS="$(_ip_address)" |
| 47 | fi |
| 48 | |
| 49 | : ${CASSANDRA_BROADCAST_ADDRESS="$CASSANDRA_LISTEN_ADDRESS"} |
| 50 | |
| 51 | if [ "$CASSANDRA_BROADCAST_ADDRESS" = 'auto' ]; then |
| 52 | CASSANDRA_BROADCAST_ADDRESS="$(_ip_address)" |
| 53 | fi |
| 54 | : ${CASSANDRA_BROADCAST_RPC_ADDRESS:=$CASSANDRA_BROADCAST_ADDRESS} |
| 55 | |
| 56 | if [ -n "${CASSANDRA_NAME:+1}" ]; then |
| 57 | : ${CASSANDRA_SEEDS:="cassandra"} |
| 58 | fi |
| 59 | : ${CASSANDRA_SEEDS:="$CASSANDRA_BROADCAST_ADDRESS"} |
| 60 | |
Guillaume Lambert | c2dd744 | 2021-03-12 11:42:42 +0100 | [diff] [blame] | 61 | _sed_in_place "$CASSANDRA_CONFIG/cassandra.yaml" \ |
mahendrr | 8b20f77 | 2019-05-03 06:50:10 +0000 | [diff] [blame] | 62 | -r 's/(- seeds:).*/\1 "'"$CASSANDRA_SEEDS"'"/' |
| 63 | |
| 64 | for yaml in \ |
| 65 | broadcast_address \ |
| 66 | broadcast_rpc_address \ |
| 67 | cluster_name \ |
| 68 | endpoint_snitch \ |
| 69 | listen_address \ |
| 70 | num_tokens \ |
| 71 | rpc_address \ |
| 72 | start_rpc \ |
| 73 | authenticator \ |
| 74 | ; do |
Guillaume Lambert | 05c791d | 2021-03-12 10:59:34 +0100 | [diff] [blame] | 75 | var="CASSANDRA_$(echo $yaml | tr '[:lower:]' '[:upper:]')" |
Guillaume Lambert | f345486 | 2021-03-10 16:09:31 +0100 | [diff] [blame] | 76 | # eval presents no security issue here because of limited possible values of var |
| 77 | eval val=\$$var |
mahendrr | 8b20f77 | 2019-05-03 06:50:10 +0000 | [diff] [blame] | 78 | if [ "$val" ]; then |
Guillaume Lambert | c2dd744 | 2021-03-12 11:42:42 +0100 | [diff] [blame] | 79 | _sed_in_place "$CASSANDRA_CONFIG/cassandra.yaml" \ |
mahendrr | 8b20f77 | 2019-05-03 06:50:10 +0000 | [diff] [blame] | 80 | -r 's/^(# )?('"$yaml"':).*/\2 '"$val"'/' |
| 81 | fi |
| 82 | done |
| 83 | |
| 84 | for rackdc in dc rack; do |
Guillaume Lambert | 05c791d | 2021-03-12 10:59:34 +0100 | [diff] [blame] | 85 | var="CASSANDRA_$(echo $rackdc | tr '[:lower:]' '[:upper:]')" |
Guillaume Lambert | f345486 | 2021-03-10 16:09:31 +0100 | [diff] [blame] | 86 | # eval presents no security issue here because of limited possible values of var |
| 87 | eval val=\$$var |
mahendrr | 8b20f77 | 2019-05-03 06:50:10 +0000 | [diff] [blame] | 88 | if [ "$val" ]; then |
Guillaume Lambert | c2dd744 | 2021-03-12 11:42:42 +0100 | [diff] [blame] | 89 | _sed_in_place "$CASSANDRA_CONFIG/cassandra-rackdc.properties" \ |
mahendrr | 8b20f77 | 2019-05-03 06:50:10 +0000 | [diff] [blame] | 90 | -r 's/^('"$rackdc"'=).*/\1 '"$val"'/' |
| 91 | fi |
| 92 | done |
| 93 | fi |
| 94 | |
| 95 | exec "$@" |
| 96 | |