upd: more logs

master
kvaps 2018-01-26 21:58:50 +01:00
parent c7701796f5
commit 0f2230dac5
1 changed files with 32 additions and 18 deletions

View File

@ -44,8 +44,8 @@ log() {
debug() { debug() {
if [ "$DEBUG" == 1 ]; then if [ "$DEBUG" == 1 ]; then
echo -en "[$(date '+%Y-%m-%d %H:%M:%S')] DEBUG:\t" >&2 echo -en "[$(date '+%Y-%m-%d %H:%M:%S')] DEBUG:\t"
echo "$1" >&2 echo "$1"
fi fi
} }
@ -91,32 +91,30 @@ address_is_free(){
# Kill tcpdump # Kill tcpdump
kill "$!" && wait "$!" kill "$!" && wait "$!"
local TCPDUMP_COUNT="$(awk '$3 == "received" {print $1}' /tmp/tcpdump.out; rm -f /tmp/tcpdump.out)" local TCPDUMP_COUNT="$(awk '$3 == "received" {print $1}' /tmp/tcpdump.out)"
local ARPING_COUNT="$(echo "$ARPING_CHECK" | awk '{print $1+$2}')" local ARPING_COUNT="$(echo "$ARPING_CHECK" | awk '{print $1+$2}')"
local ARPING_SEND="$(echo "$ARPING_CHECK" | awk '{print $1}')" local ARPING_SEND="$(echo "$ARPING_CHECK" | awk '{print $1}')"
local ARPING_RECEIVED="$(echo "$ARPING_CHECK" | awk '{print $2}')" local ARPING_RECEIVED="$(echo "$ARPING_CHECK" | awk '{print $2}')"
debug "TCPDUMP_COUNT=$TCPDUMP_COUNT"
debug "ARPING_COUNT=$ARPING_COUNT"
debug "ARPING_SEND=$ARPING_SEND"
debug "ARPING_RECEIVED=$ARPING_RECEIVED"
debug "$(cat /tmp/tcpdump.out)"
rm -f /tmp/tcpdump.out
if [ "$ARPING_RECEIVED" == "0" ] && [ "$TCPDUMP_COUNT" == "$ARPING_COUNT" ]; then if [ "$ARPING_RECEIVED" == "0" ] && [ "$TCPDUMP_COUNT" == "$ARPING_COUNT" ]; then
debug "[ ARPING_RECEIVED == 0 ] && [ TCPDUMP_COUNT == ARPING_COUNT ]"
return 0 return 0
else else
debug "[ ARPING_RECEIVED != 0 ] && [ TCPDUMP_COUNT != ARPING_COUNT ]"
return 1 return 1
fi fi
} }
unused_gateway() { unused_gateway() {
if [ -f "$CNI_CONFIG" ]; then
local UNUSED_GATEWAY=$(sed -n 's/.*"gateway": "\(.*\)",/\1/p' "$CNI_CONFIG")
rm -f "$CNI_CONFIG"
fi
if [ -z $UNUSED_GATEWAY ] || ! gateway_is_right "$UNUSED_GATEWAY"; then
UNUSED_GATEWAY="$(random_gateway)"
fi
while ! address_is_free "$UNUSED_GATEWAY"; do
local UNUSED_GATEWAY="$(random_gateway)"
done
echo "$UNUSED_GATEWAY"
} }
gateway_is_right() { gateway_is_right() {
@ -247,9 +245,25 @@ IPADDR="$(ip -f inet -o addr show "$BRIDGE" | grep -o -m1 'inet [^ /]*' | cut -d
# If ip not exist # If ip not exist
if [ -z "$IPADDR" ]; then if [ -z "$IPADDR" ]; then
log "Retrieving IP-address" if [ -f "$CNI_CONFIG" ]; then
IPADDR="$(unused_gateway)" CHECKING_IP=$(sed -n 's/.*"gateway": "\(.*\)",/\1/p' "$CNI_CONFIG")
log "Successful retrived $IPADDR" log "Cni config found, taking old address $CHECKING_IP"
rm -f "$CNI_CONFIG"
fi
if [ -z $CHECKING_IP ] || ! gateway_is_right "$CHECKING_IP"; then
CHECKING_IP="$(random_gateway)"
log "New address generated $CHECKING_IP"
fi
log "Checking $CHECKING_IP"
while ! address_is_free "$CHECKING_IP"; do
log "Address $CHECKING_IP is not free"
CHECKING_IP="$(random_gateway)"
log "Taking another one $CHECKING_IP"
done
log "Address $CHECKING_IP is free, using it as gateway"
IPADDR="$CHECKING_IP"
log "Configuring $IPADDR/$POD_PREFIX on $BRIDGE" log "Configuring $IPADDR/$POD_PREFIX on $BRIDGE"
ip addr change "$IPADDR/$POD_PREFIX" dev "$BRIDGE" ip addr change "$IPADDR/$POD_PREFIX" dev "$BRIDGE"
@ -309,4 +323,4 @@ debug "$(cat "$CNI_CONFIG")"
# ------------------------------------------------------------------------------------ # ------------------------------------------------------------------------------------
# Sleep gently # Sleep gently
# ------------------------------------------------------------------------------------ # ------------------------------------------------------------------------------------
tail -f /dev/null exec tail -f /dev/null