tripleo-image-elements/elements/os-net-config/os-refresh-config/configure.d/20-os-net-config
Dan Prince cae4740ee2 os-net-config: add configure_safe_defaults
This patch adds a new configure_safe_defaults function that
is called on EXIT. If the exit code is non-zero the function
executes code to configure DHCP on all active interfaces.

Since DHCP is used to initially bootstrap all TripleO instances
via the provisioning network this should always be a safe default
and allow communication with Heat to persist if an invalid
(breaking) network configuration change is pushed out. This would
facilitate immediate Heat notification of the failure and subsequent
Heat updates to potentially help correct the invalid network
configuration.

Change-Id: Ibccba1ee77aa13c85a78a75d2e6cd179f87d7d16
Depends-on: Ibe0e32bc09979bc68b92a722b2bfa383e77502a9
2015-09-17 21:05:22 -04:00

112 lines
3.1 KiB
Bash
Executable File

#!/bin/bash
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
}
function configure_safe_defaults() {
[[ $? == 0 ]] && return 0
cat > /etc/os-net-config/dhcp_all_interfaces.yaml <<EOF_CAT
# This file is an autogenerated safe defaults file for os-net-config
# which runs DHCP on all discovered interfaces to ensure connectivity
# back to the undercloud for updates
network_config:
EOF_CAT
for iface in $(ls /sys/class/net | grep -v ^lo$); do
local mac_addr_type="$(cat /sys/class/net/${iface}/addr_assign_type)"
if [ "$mac_addr_type" != "0" ]; then
echo "Device has generated MAC, skipping."
else
ip link set dev $iface up &>/dev/null
HAS_LINK="$(cat /sys/class/net/${iface}/carrier)"
TRIES=10
while [ "$HAS_LINK" == "0" -a $TRIES -gt 0 ]; do
HAS_LINK="$(cat /sys/class/net/${iface}/carrier)"
if [ "$HAS_LINK" == "1" ]; then
break
else
sleep 1
fi
TRIES=$(( TRIES - 1 ))
done
if [ "$HAS_LINK" == "1" ] ; then
cat >> /etc/os-net-config/dhcp_all_interfaces.yaml <<EOF_CAT
-
type: interface
name: $iface
use_dhcp: true
EOF_CAT
fi
fi
done
os-net-config -c /etc/os-net-config/dhcp_all_interfaces.yaml -v --detailed-exit-codes --cleanup
RETVAL=$?
if [[ $RETVAL == 2 ]]; then
ping_metadata_ip
elif [[ $RETVAL != 0 ]]; then
echo "ERROR: configuration of safe defaults failed."
fi
}
NET_CONFIG=$(os-apply-config --key os_net_config --type raw --key-default '')
if [ -n "$NET_CONFIG" ]; then
trap configure_safe_defaults EXIT
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