oem-exoscale: make DHCP address retrieval more resilient

When retrievent the DHCP IP address (to get user data), two steps were
used: the first one waited for one lease file to become present and the
second one extracted and validated the server IP address. If for some
reason the second step failed, an empty IP was provided (and notably,
coreos-setup-environment.service never finishes correctly).

To avoid that, we combine the two steps into a loop: for each available
lease file, extract the IP and validate it. If it succeeds, return
it. If not continue. If nothing was found, restart the same process
after sleeping a bit.
This commit is contained in:
Vincent Bernat 2016-04-19 11:49:07 +02:00
parent e329cfbbd3
commit a8ca51d73d
2 changed files with 13 additions and 15 deletions

View File

@ -2,22 +2,20 @@
get_dhcp_ip() {
local leases_dir="/run/systemd/netif/leases"
local found=0
while true; do
if [[ "$(find "${leases_dir}" -type f -size +1c)" ]]; then
break
fi
for leasefile in $(find "${leases_dir}" -type f -size +1c); do
dhcp_server_ip=$(cat $leasefile | awk -F= '/SERVER_ADDRESS/ { print $2 }')
if [[ -n "${dhcp_server_ip}" ]]; then
metadata_url="http://${dhcp_server_ip}/latest/meta-data/"
if curl --fail -s "${metadata_url}" >/dev/null; then
echo $dhcp_server_ip
found=1
break
fi
fi
done
[[ $found -eq 0 ]] || break
sleep .5
done
for leasefile in "${leases_dir}/"*; do
dhcp_server_ip=$(cat $leasefile | awk -F= '/SERVER_ADDRESS/ { print $2 }')
if [[ -n "${dhcp_server_ip}" ]]; then
metadata_url="http://${dhcp_server_ip}/latest/meta-data/"
curl --fail -s "${metadata_url}" >/dev/null
if [[ $? -eq 0 ]]; then
echo $dhcp_server_ip
break
fi
fi
done
}