mahendrr | 8b20f77 | 2019-05-03 06:50:10 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | set -e |
| 3 | |
| 4 | # first arg is `-f` or `--some-option` |
| 5 | # or there are no args |
| 6 | if [ "$#" -eq 0 ] || [ "${1#-}" != "$1" ]; then |
| 7 | set -- cassandra -f "$@" |
| 8 | fi |
| 9 | |
| 10 | # allow the container to be started with `--user` |
| 11 | if [ "$1" = 'cassandra' -a "$(id -u)" = '0' ]; then |
| 12 | find /var/lib/cassandra /var/log/cassandra "$CASSANDRA_CONFIG" \ |
| 13 | \! -user cassandra -exec chown cassandra '{}' + |
Guillaume Lambert | 134188d | 2021-03-11 13:42:23 +0100 | [diff] [blame^] | 14 | exec gosu cassandra "$0" "$@" |
mahendrr | 8b20f77 | 2019-05-03 06:50:10 +0000 | [diff] [blame] | 15 | fi |
| 16 | |
| 17 | _ip_address() { |
| 18 | # scrape the first non-localhost IP address of the container |
| 19 | # in Swarm Mode, we often get two IPs -- the container IP, and the (shared) VIP, and the container IP should always be first |
| 20 | ip address | awk ' |
| 21 | $1 == "inet" && $NF != "lo" { |
| 22 | gsub(/\/.+$/, "", $2) |
| 23 | print $2 |
| 24 | exit |
| 25 | } |
| 26 | ' |
| 27 | } |
| 28 | |
| 29 | # "sed -i", but without "mv" (which doesn't work on a bind-mounted file, for example) |
| 30 | _sed-in-place() { |
| 31 | local filename="$1"; shift |
| 32 | local tempFile |
| 33 | tempFile="$(mktemp)" |
| 34 | sed "$@" "$filename" > "$tempFile" |
| 35 | cat "$tempFile" > "$filename" |
| 36 | rm "$tempFile" |
| 37 | } |
| 38 | |
| 39 | if [ "$1" = 'cassandra' ]; then |
| 40 | : ${CASSANDRA_RPC_ADDRESS='0.0.0.0'} |
| 41 | |
| 42 | : ${CASSANDRA_LISTEN_ADDRESS='auto'} |
| 43 | if [ "$CASSANDRA_LISTEN_ADDRESS" = 'auto' ]; then |
| 44 | CASSANDRA_LISTEN_ADDRESS="$(_ip_address)" |
| 45 | fi |
| 46 | |
| 47 | : ${CASSANDRA_BROADCAST_ADDRESS="$CASSANDRA_LISTEN_ADDRESS"} |
| 48 | |
| 49 | if [ "$CASSANDRA_BROADCAST_ADDRESS" = 'auto' ]; then |
| 50 | CASSANDRA_BROADCAST_ADDRESS="$(_ip_address)" |
| 51 | fi |
| 52 | : ${CASSANDRA_BROADCAST_RPC_ADDRESS:=$CASSANDRA_BROADCAST_ADDRESS} |
| 53 | |
| 54 | if [ -n "${CASSANDRA_NAME:+1}" ]; then |
| 55 | : ${CASSANDRA_SEEDS:="cassandra"} |
| 56 | fi |
| 57 | : ${CASSANDRA_SEEDS:="$CASSANDRA_BROADCAST_ADDRESS"} |
| 58 | |
| 59 | _sed-in-place "$CASSANDRA_CONFIG/cassandra.yaml" \ |
| 60 | -r 's/(- seeds:).*/\1 "'"$CASSANDRA_SEEDS"'"/' |
| 61 | |
| 62 | for yaml in \ |
| 63 | broadcast_address \ |
| 64 | broadcast_rpc_address \ |
| 65 | cluster_name \ |
| 66 | endpoint_snitch \ |
| 67 | listen_address \ |
| 68 | num_tokens \ |
| 69 | rpc_address \ |
| 70 | start_rpc \ |
| 71 | authenticator \ |
| 72 | ; do |
| 73 | var="CASSANDRA_${yaml^^}" |
| 74 | val="${!var}" |
| 75 | if [ "$val" ]; then |
| 76 | _sed-in-place "$CASSANDRA_CONFIG/cassandra.yaml" \ |
| 77 | -r 's/^(# )?('"$yaml"':).*/\2 '"$val"'/' |
| 78 | fi |
| 79 | done |
| 80 | |
| 81 | for rackdc in dc rack; do |
| 82 | var="CASSANDRA_${rackdc^^}" |
| 83 | val="${!var}" |
| 84 | if [ "$val" ]; then |
| 85 | _sed-in-place "$CASSANDRA_CONFIG/cassandra-rackdc.properties" \ |
| 86 | -r 's/^('"$rackdc"'=).*/\1 '"$val"'/' |
| 87 | fi |
| 88 | done |
| 89 | fi |
| 90 | |
| 91 | exec "$@" |
| 92 | |