Merge "Extract HOST_IP default process to a function"

This commit is contained in:
Jenkins
2013-03-15 14:38:27 +00:00
committed by Gerrit Code Review
2 changed files with 34 additions and 21 deletions

@ -123,6 +123,37 @@ function get_field() {
}
# Get the default value for HOST_IP
# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
function get_default_host_ip() {
local fixed_range=$1
local floating_range=$2
local host_ip_iface=$3
local host_ip=$4
# Find the interface used for the default route
host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
# Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
host_ip=""
host_ips=`LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/"); print parts[1]}'`
for IP in $host_ips; do
# Attempt to filter out IP addresses that are part of the fixed and
# floating range. Note that this method only works if the ``netaddr``
# python library is installed. If it is not installed, an error
# will be printed and the first IP from the interface will be used.
# If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
# address.
if ! (address_in_net $IP $fixed_range || address_in_net $IP $floating_range); then
host_ip=$IP
break;
fi
done
fi
echo $host_ip
}
function _get_package_dir() {
local pkg_dir
if is_ubuntu; then