Browse code

Use default route to find HOST_IP

When running devstack, nova moves the host ip from eth0 onto the
bridge. This causes devstack to fail on the second run unless you
explicitly set HOST_IP in localrc.

This patch searches for an ip on the interface that is used for
the default route. This will be eth0 (or en0) in most cases, but
it will search br100 instead if nova has moved the ip, since it
moves the default route as well.

It also will filter out ips from the potential list that are part
of the fixed range and floating range if the netaddr library is
installed. This allows us to find the proper ip even if we have
accidentally left a floating ip or fixed ip on the bridge.

Change-Id: I13288e53ee2786c5ae0edb3f9ab457be8303f1f6

Vishvananda Ishaya authored on 2012/07/04 05:29:01
Showing 2 changed files
... ...
@@ -9,6 +9,18 @@ XTRACE=$(set +o | grep xtrace)
9 9
 set +o xtrace
10 10
 
11 11
 
12
+# Exit 0 if address is in network or 1 if
13
+# address is not in network or netaddr library
14
+# is not installed.
15
+function address_in_net() {
16
+    python -c "
17
+import netaddr
18
+import sys
19
+sys.exit(netaddr.IPAddress('$1') not in netaddr.IPNetwork('$2'))
20
+"
21
+}
22
+
23
+
12 24
 # apt-get wrapper to set arguments correctly
13 25
 # apt_get operation package [package ...]
14 26
 function apt_get() {
... ...
@@ -283,13 +283,30 @@ LIBVIRT_TYPE=${LIBVIRT_TYPE:-kvm}
283 283
 # cases.
284 284
 SCHEDULER=${SCHEDULER:-nova.scheduler.filter_scheduler.FilterScheduler}
285 285
 
286
-HOST_IP_IFACE=${HOST_IP_IFACE:-eth0}
287
-# Use the eth0 IP unless an explicit is set by ``HOST_IP`` environment variable
286
+# Set fixed and floating range here so we can make sure not to use addresses
287
+# from either range when attempting to guess the ip to use for the host
288
+FIXED_RANGE=${FIXED_RANGE:-10.0.0.0/24}
289
+FLOATING_RANGE=${FLOATING_RANGE:-172.24.4.224/28}
290
+
291
+# Find the interface used for the default route
292
+HOST_IP_IFACE=${HOST_IP_IFACE:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }')}
293
+# Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
288 294
 if [ -z "$HOST_IP" -o "$HOST_IP" == "dhcp" ]; then
289
-    HOST_IP=`LC_ALL=C ip -f inet addr show ${HOST_IP_IFACE} | awk '/inet/ {split($2,parts,"/");  print parts[1]}' | head -n1`
290
-    if [ "$HOST_IP" = "" ]; then
295
+    HOST_IP=""
296
+    HOST_IPS=`LC_ALL=C ip -f inet addr show ${HOST_IP_IFACE} | awk '/inet/ {split($2,parts,"/");  print parts[1]}'`
297
+    for IP in $HOST_IPS; do
298
+        # Attempt to filter out ip addresses that are part of the fixed and
299
+        # floating range. Note that this method only works if the 'netaddr'
300
+        # python library is installed. If it is not installed, an error
301
+        # will be printed and the first ip from the interface will be used.
302
+        if ! (address_in_net $IP $FIXED_RANGE || address_in_net $IP $FLOATING_RANGE); then
303
+            HOST_IP=$IP
304
+            break;
305
+        fi
306
+    done
307
+    if [ "$HOST_IP" == "" ]; then
291 308
         echo "Could not determine host ip address."
292
-        echo "Either localrc specified dhcp on ${HOST_IP_IFACE} or defaulted to eth0"
309
+        echo "Either localrc specified dhcp on ${HOST_IP_IFACE} or defaulted"
293 310
         exit 1
294 311
     fi
295 312
 fi
... ...
@@ -368,11 +385,8 @@ else
368 368
 fi
369 369
 
370 370
 PUBLIC_INTERFACE=${PUBLIC_INTERFACE:-$PUBLIC_INTERFACE_DEFAULT}
371
-PUBLIC_INTERFACE=${PUBLIC_INTERFACE:-br100}
372
-FIXED_RANGE=${FIXED_RANGE:-10.0.0.0/24}
373 371
 FIXED_NETWORK_SIZE=${FIXED_NETWORK_SIZE:-256}
374 372
 NETWORK_GATEWAY=${NETWORK_GATEWAY:-10.0.0.1}
375
-FLOATING_RANGE=${FLOATING_RANGE:-172.24.4.224/28}
376 373
 NET_MAN=${NET_MAN:-FlatDHCPManager}
377 374
 EC2_DMZ_HOST=${EC2_DMZ_HOST:-$SERVICE_HOST}
378 375
 FLAT_NETWORK_BRIDGE=${FLAT_NETWORK_BRIDGE:-$FLAT_NETWORK_BRIDGE_DEFAULT}