From 01786683a1b060f813bce5539b1743695f9cc043 Mon Sep 17 00:00:00 2001 From: Tianon Gravi Date: Tue, 24 Apr 2018 12:47:45 -0700 Subject: [PATCH] Swap from "hostname --ip-address" to "ip address" for IP address scraping (especially for Swarm Mode where we sometimes have an additional shared VIP) --- 2.1/docker-entrypoint.sh | 16 ++++++++++++++-- 2.2/docker-entrypoint.sh | 16 ++++++++++++++-- 3.0/docker-entrypoint.sh | 16 ++++++++++++++-- 3.11/docker-entrypoint.sh | 16 ++++++++++++++-- docker-entrypoint.sh | 16 ++++++++++++++-- 5 files changed, 70 insertions(+), 10 deletions(-) diff --git a/2.1/docker-entrypoint.sh b/2.1/docker-entrypoint.sh index 151adad..871f7f4 100755 --- a/2.1/docker-entrypoint.sh +++ b/2.1/docker-entrypoint.sh @@ -13,18 +13,30 @@ if [ "$1" = 'cassandra' -a "$(id -u)" = '0' ]; then exec gosu cassandra "$BASH_SOURCE" "$@" fi +_ip_address() { + # scrape the first non-localhost IP address of the container + # in Swarm Mode, we often get two IPs -- the container IP, and the (shared) VIP, and the container IP should always be first + ip address | awk ' + $1 == "inet" && $2 !~ /^127[.]/ { + gsub(/\/.+$/, "", $2) + print $2 + exit + } + ' +} + if [ "$1" = 'cassandra' ]; then : ${CASSANDRA_RPC_ADDRESS='0.0.0.0'} : ${CASSANDRA_LISTEN_ADDRESS='auto'} if [ "$CASSANDRA_LISTEN_ADDRESS" = 'auto' ]; then - CASSANDRA_LISTEN_ADDRESS="$(hostname --ip-address)" + CASSANDRA_LISTEN_ADDRESS="$(_ip_address)" fi : ${CASSANDRA_BROADCAST_ADDRESS="$CASSANDRA_LISTEN_ADDRESS"} if [ "$CASSANDRA_BROADCAST_ADDRESS" = 'auto' ]; then - CASSANDRA_BROADCAST_ADDRESS="$(hostname --ip-address)" + CASSANDRA_BROADCAST_ADDRESS="$(_ip_address)" fi : ${CASSANDRA_BROADCAST_RPC_ADDRESS:=$CASSANDRA_BROADCAST_ADDRESS} diff --git a/2.2/docker-entrypoint.sh b/2.2/docker-entrypoint.sh index 151adad..871f7f4 100755 --- a/2.2/docker-entrypoint.sh +++ b/2.2/docker-entrypoint.sh @@ -13,18 +13,30 @@ if [ "$1" = 'cassandra' -a "$(id -u)" = '0' ]; then exec gosu cassandra "$BASH_SOURCE" "$@" fi +_ip_address() { + # scrape the first non-localhost IP address of the container + # in Swarm Mode, we often get two IPs -- the container IP, and the (shared) VIP, and the container IP should always be first + ip address | awk ' + $1 == "inet" && $2 !~ /^127[.]/ { + gsub(/\/.+$/, "", $2) + print $2 + exit + } + ' +} + if [ "$1" = 'cassandra' ]; then : ${CASSANDRA_RPC_ADDRESS='0.0.0.0'} : ${CASSANDRA_LISTEN_ADDRESS='auto'} if [ "$CASSANDRA_LISTEN_ADDRESS" = 'auto' ]; then - CASSANDRA_LISTEN_ADDRESS="$(hostname --ip-address)" + CASSANDRA_LISTEN_ADDRESS="$(_ip_address)" fi : ${CASSANDRA_BROADCAST_ADDRESS="$CASSANDRA_LISTEN_ADDRESS"} if [ "$CASSANDRA_BROADCAST_ADDRESS" = 'auto' ]; then - CASSANDRA_BROADCAST_ADDRESS="$(hostname --ip-address)" + CASSANDRA_BROADCAST_ADDRESS="$(_ip_address)" fi : ${CASSANDRA_BROADCAST_RPC_ADDRESS:=$CASSANDRA_BROADCAST_ADDRESS} diff --git a/3.0/docker-entrypoint.sh b/3.0/docker-entrypoint.sh index 151adad..871f7f4 100755 --- a/3.0/docker-entrypoint.sh +++ b/3.0/docker-entrypoint.sh @@ -13,18 +13,30 @@ if [ "$1" = 'cassandra' -a "$(id -u)" = '0' ]; then exec gosu cassandra "$BASH_SOURCE" "$@" fi +_ip_address() { + # scrape the first non-localhost IP address of the container + # in Swarm Mode, we often get two IPs -- the container IP, and the (shared) VIP, and the container IP should always be first + ip address | awk ' + $1 == "inet" && $2 !~ /^127[.]/ { + gsub(/\/.+$/, "", $2) + print $2 + exit + } + ' +} + if [ "$1" = 'cassandra' ]; then : ${CASSANDRA_RPC_ADDRESS='0.0.0.0'} : ${CASSANDRA_LISTEN_ADDRESS='auto'} if [ "$CASSANDRA_LISTEN_ADDRESS" = 'auto' ]; then - CASSANDRA_LISTEN_ADDRESS="$(hostname --ip-address)" + CASSANDRA_LISTEN_ADDRESS="$(_ip_address)" fi : ${CASSANDRA_BROADCAST_ADDRESS="$CASSANDRA_LISTEN_ADDRESS"} if [ "$CASSANDRA_BROADCAST_ADDRESS" = 'auto' ]; then - CASSANDRA_BROADCAST_ADDRESS="$(hostname --ip-address)" + CASSANDRA_BROADCAST_ADDRESS="$(_ip_address)" fi : ${CASSANDRA_BROADCAST_RPC_ADDRESS:=$CASSANDRA_BROADCAST_ADDRESS} diff --git a/3.11/docker-entrypoint.sh b/3.11/docker-entrypoint.sh index 151adad..871f7f4 100755 --- a/3.11/docker-entrypoint.sh +++ b/3.11/docker-entrypoint.sh @@ -13,18 +13,30 @@ if [ "$1" = 'cassandra' -a "$(id -u)" = '0' ]; then exec gosu cassandra "$BASH_SOURCE" "$@" fi +_ip_address() { + # scrape the first non-localhost IP address of the container + # in Swarm Mode, we often get two IPs -- the container IP, and the (shared) VIP, and the container IP should always be first + ip address | awk ' + $1 == "inet" && $2 !~ /^127[.]/ { + gsub(/\/.+$/, "", $2) + print $2 + exit + } + ' +} + if [ "$1" = 'cassandra' ]; then : ${CASSANDRA_RPC_ADDRESS='0.0.0.0'} : ${CASSANDRA_LISTEN_ADDRESS='auto'} if [ "$CASSANDRA_LISTEN_ADDRESS" = 'auto' ]; then - CASSANDRA_LISTEN_ADDRESS="$(hostname --ip-address)" + CASSANDRA_LISTEN_ADDRESS="$(_ip_address)" fi : ${CASSANDRA_BROADCAST_ADDRESS="$CASSANDRA_LISTEN_ADDRESS"} if [ "$CASSANDRA_BROADCAST_ADDRESS" = 'auto' ]; then - CASSANDRA_BROADCAST_ADDRESS="$(hostname --ip-address)" + CASSANDRA_BROADCAST_ADDRESS="$(_ip_address)" fi : ${CASSANDRA_BROADCAST_RPC_ADDRESS:=$CASSANDRA_BROADCAST_ADDRESS} diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 151adad..871f7f4 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -13,18 +13,30 @@ if [ "$1" = 'cassandra' -a "$(id -u)" = '0' ]; then exec gosu cassandra "$BASH_SOURCE" "$@" fi +_ip_address() { + # scrape the first non-localhost IP address of the container + # in Swarm Mode, we often get two IPs -- the container IP, and the (shared) VIP, and the container IP should always be first + ip address | awk ' + $1 == "inet" && $2 !~ /^127[.]/ { + gsub(/\/.+$/, "", $2) + print $2 + exit + } + ' +} + if [ "$1" = 'cassandra' ]; then : ${CASSANDRA_RPC_ADDRESS='0.0.0.0'} : ${CASSANDRA_LISTEN_ADDRESS='auto'} if [ "$CASSANDRA_LISTEN_ADDRESS" = 'auto' ]; then - CASSANDRA_LISTEN_ADDRESS="$(hostname --ip-address)" + CASSANDRA_LISTEN_ADDRESS="$(_ip_address)" fi : ${CASSANDRA_BROADCAST_ADDRESS="$CASSANDRA_LISTEN_ADDRESS"} if [ "$CASSANDRA_BROADCAST_ADDRESS" = 'auto' ]; then - CASSANDRA_BROADCAST_ADDRESS="$(hostname --ip-address)" + CASSANDRA_BROADCAST_ADDRESS="$(_ip_address)" fi : ${CASSANDRA_BROADCAST_RPC_ADDRESS:=$CASSANDRA_BROADCAST_ADDRESS}