os-net-config: add ping_metadata_ip function

Updates os-net-config to use --detailed-exit-codes so that when
a network change has been detected we can perform a network validation
via a new ping_metadata_ip function. This function performs a simple ping
test back to the IP address of the Heat API which should always be
available after any network configuration change.

The motivation for this change is to allow network configuration
changes time to settle. When making changes to the ctlplane (which
is typically used to contact the undercloud Heat API for subsequent
configuration notifications and updates) it is sometimes desirable to
wait a few seconds before proceeding. This avoids a race where the
ctlplane was switch to a static IP address and the very next
Heat notification would sometimes fail.

Depends-On: I8f22fa15335d1276f4e444a6454a24ff486e1495

Change-Id: I257e1cba6dee16f73f75512d1284e1e3b9d4c831
This commit is contained in:
Dan Prince 2015-08-16 18:46:07 -04:00
parent 0a9eafad0d
commit 4769640d92

View File

@ -1,8 +1,62 @@
#!/bin/bash
set -eux
set -ux
function get_metadata_ip() {
local METADATA_IP
# Look for a variety of Heat transports
# FIXME: Heat should provide a way to obtain this in a single place
for URL in os-collect-config.cfn.metadata_url os-collect-config.heat.auth_url os-collect-config.swift.metadata_put_url os-collect-config.zaqar.auth_url; do
METADATA_IP=$(os-apply-config --key $URL --key-default '' --type raw 2>/dev/null | sed -e 's|http.*://\([^:]*\).*|\1|')
[ -n "$METADATA_IP" ] && break
done
echo $METADATA_IP
}
function is_local_ip() {
local IP_TO_CHECK=$1
if ip a | grep ^\ *inet | grep $IP_TO_CHECK &>/dev/null; then
return 0
else
return 1
fi
}
function ping_metadata_ip() {
local METADATA_IP=$(get_metadata_ip)
if [ -n "$METADATA_IP" ] && ! is_local_ip $METADATA_IP; then
echo -n "Trying to ping metadata IP ${METADATA_IP}..."
local COUNT=0
until ping -c 1 $METADATA_IP &> /dev/null; do
COUNT=$(( $COUNT + 1 ))
if [ $COUNT -eq 10 ]; then
echo "FAILURE"
echo "$METADATA_IP is not pingable." >&2
exit 1
fi
done
echo "SUCCESS"
else
echo "No metadata IP found. Skipping."
fi
}
NET_CONFIG=$(os-apply-config --key os_net_config --type raw --key-default '')
if [ -n "$NET_CONFIG" ]; then
os-net-config -c /etc/os-net-config/config.json -v
os-net-config -c /etc/os-net-config/config.json -v --detailed-exit-codes
RETVAL=$?
if [[ $RETVAL == 2 ]]; then
ping_metadata_ip
elif [[ $RETVAL != 0 ]]; then
echo "ERROR: os-net-config configuration failed." >&2
exit 1
fi
fi