Extract HOST_IP default process to a function

This process is required by Grenade also...

Change-Id: I263a4351e70b9cfb0965e57e518fe5fd377f4b43
diff --git a/functions b/functions
index 8cb703c..5fae560 100644
--- a/functions
+++ b/functions
@@ -123,6 +123,37 @@
 }
 
 
+# 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