Browse code

Extract HOST_IP default process to a function

This process is required by Grenade also...

Change-Id: I263a4351e70b9cfb0965e57e518fe5fd377f4b43

Dean Troyer authored on 2013/03/14 04:06:13
Showing 2 changed files
... ...
@@ -123,6 +123,37 @@ function get_field() {
123 123
 }
124 124
 
125 125
 
126
+# Get the default value for HOST_IP
127
+# get_default_host_ip fixed_range floating_range host_ip_iface host_ip
128
+function get_default_host_ip() {
129
+    local fixed_range=$1
130
+    local floating_range=$2
131
+    local host_ip_iface=$3
132
+    local host_ip=$4
133
+
134
+    # Find the interface used for the default route
135
+    host_ip_iface=${host_ip_iface:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
136
+    # Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
137
+    if [ -z "$host_ip" -o "$host_ip" == "dhcp" ]; then
138
+        host_ip=""
139
+        host_ips=`LC_ALL=C ip -f inet addr show ${host_ip_iface} | awk '/inet/ {split($2,parts,"/");  print parts[1]}'`
140
+        for IP in $host_ips; do
141
+            # Attempt to filter out IP addresses that are part of the fixed and
142
+            # floating range. Note that this method only works if the ``netaddr``
143
+            # python library is installed. If it is not installed, an error
144
+            # will be printed and the first IP from the interface will be used.
145
+            # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
146
+            # address.
147
+            if ! (address_in_net $IP $fixed_range || address_in_net $IP $floating_range); then
148
+                host_ip=$IP
149
+                break;
150
+            fi
151
+        done
152
+    fi
153
+    echo $host_ip
154
+}
155
+
156
+
126 157
 function _get_package_dir() {
127 158
     local pkg_dir
128 159
     if is_ubuntu; then
... ...
@@ -223,27 +223,9 @@ FIXED_RANGE=${FIXED_RANGE:-10.0.0.0/24}
223 223
 FIXED_NETWORK_SIZE=${FIXED_NETWORK_SIZE:-256}
224 224
 NETWORK_GATEWAY=${NETWORK_GATEWAY:-10.0.0.1}
225 225
 
226
-# Find the interface used for the default route
227
-HOST_IP_IFACE=${HOST_IP_IFACE:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }' | head -1)}
228
-# Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
229
-if [ -z "$HOST_IP" -o "$HOST_IP" == "dhcp" ]; then
230
-    HOST_IP=""
231
-    HOST_IPS=`LC_ALL=C ip -f inet addr show ${HOST_IP_IFACE} | awk '/inet/ {split($2,parts,"/");  print parts[1]}'`
232
-    for IP in $HOST_IPS; do
233
-        # Attempt to filter out IP addresses that are part of the fixed and
234
-        # floating range. Note that this method only works if the ``netaddr``
235
-        # python library is installed. If it is not installed, an error
236
-        # will be printed and the first IP from the interface will be used.
237
-        # If that is not correct set ``HOST_IP`` in ``localrc`` to the correct
238
-        # address.
239
-        if ! (address_in_net $IP $FIXED_RANGE || address_in_net $IP $FLOATING_RANGE); then
240
-            HOST_IP=$IP
241
-            break;
242
-        fi
243
-    done
244
-    if [ "$HOST_IP" == "" ]; then
245
-        die $LINENO "Could not determine host ip address. Either localrc specified dhcp on ${HOST_IP_IFACE} or defaulted"
246
-    fi
226
+HOST_IP=$(get_default_host_ip $FIXED_RANGE $FLOATING_RANGE "$HOST_IP_IFACE" "$HOST_IP")
227
+if [ "$HOST_IP" == "" ]; then
228
+    die $LINENO "Could not determine host ip address. Either localrc specified dhcp on ${HOST_IP_IFACE} or defaulted"
247 229
 fi
248 230
 
249 231
 # Allow the use of an alternate hostname (such as localhost/127.0.0.1) for service endpoints.