Browse code

Merge "Use default route to find HOST_IP"

Jenkins authored on 2012/08/11 06:43:48
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() {
... ...
@@ -279,13 +279,30 @@ INSTANCE_NAME_PREFIX=${INSTANCE_NAME_PREFIX:-instance-}
279 279
 # cases.
280 280
 SCHEDULER=${SCHEDULER:-nova.scheduler.filter_scheduler.FilterScheduler}
281 281
 
282
-HOST_IP_IFACE=${HOST_IP_IFACE:-eth0}
283
-# Use the eth0 IP unless an explicit is set by ``HOST_IP`` environment variable
282
+# Set fixed and floating range here so we can make sure not to use addresses
283
+# from either range when attempting to guess the ip to use for the host
284
+FIXED_RANGE=${FIXED_RANGE:-10.0.0.0/24}
285
+FLOATING_RANGE=${FLOATING_RANGE:-172.24.4.224/28}
286
+
287
+# Find the interface used for the default route
288
+HOST_IP_IFACE=${HOST_IP_IFACE:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }')}
289
+# Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
284 290
 if [ -z "$HOST_IP" -o "$HOST_IP" == "dhcp" ]; then
285
-    HOST_IP=`LC_ALL=C ip -f inet addr show ${HOST_IP_IFACE} | awk '/inet/ {split($2,parts,"/");  print parts[1]}' | head -n1`
286
-    if [ "$HOST_IP" = "" ]; then
291
+    HOST_IP=""
292
+    HOST_IPS=`LC_ALL=C ip -f inet addr show ${HOST_IP_IFACE} | awk '/inet/ {split($2,parts,"/");  print parts[1]}'`
293
+    for IP in $HOST_IPS; do
294
+        # Attempt to filter out ip addresses that are part of the fixed and
295
+        # floating range. Note that this method only works if the 'netaddr'
296
+        # python library is installed. If it is not installed, an error
297
+        # will be printed and the first ip from the interface will be used.
298
+        if ! (address_in_net $IP $FIXED_RANGE || address_in_net $IP $FLOATING_RANGE); then
299
+            HOST_IP=$IP
300
+            break;
301
+        fi
302
+    done
303
+    if [ "$HOST_IP" == "" ]; then
287 304
         echo "Could not determine host ip address."
288
-        echo "Either localrc specified dhcp on ${HOST_IP_IFACE} or defaulted to eth0"
305
+        echo "Either localrc specified dhcp on ${HOST_IP_IFACE} or defaulted"
289 306
         exit 1
290 307
     fi
291 308
 fi
... ...
@@ -364,11 +381,8 @@ else
364 364
 fi
365 365
 
366 366
 PUBLIC_INTERFACE=${PUBLIC_INTERFACE:-$PUBLIC_INTERFACE_DEFAULT}
367
-PUBLIC_INTERFACE=${PUBLIC_INTERFACE:-br100}
368
-FIXED_RANGE=${FIXED_RANGE:-10.0.0.0/24}
369 367
 FIXED_NETWORK_SIZE=${FIXED_NETWORK_SIZE:-256}
370 368
 NETWORK_GATEWAY=${NETWORK_GATEWAY:-10.0.0.1}
371
-FLOATING_RANGE=${FLOATING_RANGE:-172.24.4.224/28}
372 369
 NET_MAN=${NET_MAN:-FlatDHCPManager}
373 370
 EC2_DMZ_HOST=${EC2_DMZ_HOST:-$SERVICE_HOST}
374 371
 FLAT_NETWORK_BRIDGE=${FLAT_NETWORK_BRIDGE:-$FLAT_NETWORK_BRIDGE_DEFAULT}