2 # The following environment variables may be set to substitute in a
3 # custom bridge or interface name. Normally these are provided by the calling
4 # SoftwareConfig resource, but they may also be set manually for testing.
5 # $bridge_name : The bridge device name to apply
6 # $interface_name : The interface name to apply
8 # Also this token is replaced via a str_replace in the SoftwareConfig running
9 # the script - in future we may extend this to also work with a variable, e.g
10 # a deployment input via input_values
11 # $network_config : the json serialized os-net-config config to apply
15 function get_metadata_ip() {
19 # Look for a variety of Heat transports
20 # FIXME: Heat should provide a way to obtain this in a single place
21 for URL in os-collect-config.cfn.metadata_url os-collect-config.heat.auth_url os-collect-config.request.metadata_url os-collect-config.zaqar.auth_url; do
22 METADATA_IP=$(os-apply-config --key $URL --key-default '' --type raw 2>/dev/null | sed -e 's|http.*://\([^:]*\).*|\1|')
23 [ -n "$METADATA_IP" ] && break
30 function is_local_ip() {
32 if ip -o a | grep "inet6\? $IP_TO_CHECK/" &>/dev/null; then
39 function ping_metadata_ip() {
40 local METADATA_IP=$(get_metadata_ip)
42 if [ -n "$METADATA_IP" ] && ! is_local_ip $METADATA_IP; then
44 echo -n "Trying to ping metadata IP ${METADATA_IP}..."
47 until ping -c 1 $METADATA_IP &> /dev/null; do
48 COUNT=$(( $COUNT + 1 ))
49 if [ $COUNT -eq 10 ]; then
51 echo "$METADATA_IP is not pingable." >&2
58 echo "No metadata IP found. Skipping."
62 function configure_safe_defaults() {
64 [[ $? == 0 ]] && return 0
66 cat > /etc/os-net-config/dhcp_all_interfaces.yaml <<EOF_CAT
67 # This file is an autogenerated safe defaults file for os-net-config
68 # which runs DHCP on all discovered interfaces to ensure connectivity
69 # back to the undercloud for updates
73 for iface in $(ls /sys/class/net | grep -v ^lo$); do
74 local mac_addr_type="$(cat /sys/class/net/${iface}/addr_assign_type)"
75 if [ "$mac_addr_type" != "0" ]; then
76 echo "Device has generated MAC, skipping."
78 ip link set dev $iface up &>/dev/null
79 HAS_LINK="$(cat /sys/class/net/${iface}/carrier)"
82 while [ "$HAS_LINK" == "0" -a $TRIES -gt 0 ]; do
83 HAS_LINK="$(cat /sys/class/net/${iface}/carrier)"
84 if [ "$HAS_LINK" == "1" ]; then
89 TRIES=$(( TRIES - 1 ))
91 if [ "$HAS_LINK" == "1" ] ; then
92 cat >> /etc/os-net-config/dhcp_all_interfaces.yaml <<EOF_CAT
102 os-net-config -c /etc/os-net-config/dhcp_all_interfaces.yaml -v --detailed-exit-codes --cleanup
105 if [[ $RETVAL == 2 ]]; then
107 elif [[ $RETVAL != 0 ]]; then
108 echo "ERROR: configuration of safe defaults failed."
112 if [ -n '$network_config' ]; then
113 if [ -z "${disable_configure_safe_defaults:-''}" ]; then
114 trap configure_safe_defaults EXIT
117 mkdir -p /etc/os-net-config
118 # Note these variables come from the calling heat SoftwareConfig
119 echo '$network_config' > /etc/os-net-config/config.json
121 if [ "$(type -t network_config_hook)" = "function" ]; then
125 sed -i "s/bridge_name/${bridge_name:-''}/" /etc/os-net-config/config.json
126 sed -i "s/interface_name/${interface_name:-''}/" /etc/os-net-config/config.json
129 os-net-config -c /etc/os-net-config/config.json -v --detailed-exit-codes
133 if [[ $RETVAL == 2 ]]; then
136 #NOTE: dprince this udev rule can apparently leak DHCP processes?
137 # https://bugs.launchpad.net/tripleo/+bug/1538259
138 # until we discover the root cause we can simply disable the
139 # rule because networking has already been configured at this point
140 if [ -f /etc/udev/rules.d/99-dhcp-all-interfaces.rules ]; then
141 rm /etc/udev/rules.d/99-dhcp-all-interfaces.rules
144 elif [[ $RETVAL != 0 ]]; then
145 echo "ERROR: os-net-config configuration failed." >&2