refine strategy to spot ip address, keep on calling guest_ipv4
[build.git] / lbuild-bridge.sh
old mode 100644 (file)
new mode 100755 (executable)
index ecf218b..d3b2ab7
@@ -1,51 +1,65 @@
 #!/bin/bash
 
-# taking this bridge-initialization code out of lbuild-initvm.sh 
-# so we can use it on our libvirt/lxc local infra 
-# there's something very similar in 
+# taking this bridge-initialization code out of lbuild-initvm.sh
+# so we can use it on our libvirt/lxc local infra
+# there's something very similar in
 # tests/system/template-qemu/qemu-bridge-init
-# that the current code was actually based on, but 
-# nobody was ever bold enough to reconcile these two 
+# that the current code was actually based on, but
+# nobody was ever bold enough to reconcile these two
 
-# hard-wired 
+# hard-wired
 DEFAULT_PUBLIC_BRIDGE=br0
 
 ##############################
 # use /proc/net/dev instead of a hard-wired list
 function gather_interfaces () {
-    python <<EOF
-for line in file("/proc/net/dev"):
+    python3 <<EOF
+with open("/proc/net/dev") as feed:
+  for line in feed:
     if ':' not in line: continue
-    ifname=line.replace(" ","").split(":")[0]
-    if ifname.find("lo")==0: continue
-    if ifname.find("br")==0: continue
-    if ifname.find("virbr")==0: continue
-    if ifname.find("veth")==0: continue
-    if ifname.find("tap")==0: continue
-    print ifname
+    ifname = line.replace(" ","").split(":")[0]
+    if ifname.startswith("lo"): continue
+    if ifname.startswith("br"): continue
+    if ifname.startswith("virbr"): continue
+    if ifname.startswith("veth"): continue
+    if ifname.startswith("tap"): continue
+    if ifname.startswith("vif"): continue
+    print(ifname)
 EOF
 }
 
 function discover_interface () {
     for ifname in $(gather_interfaces); do
-       ip link show $ifname | grep -qi 'state UP' && { echo $ifname; return; }
+        ip link show $ifname | grep -qi 'state UP' && { echo $ifname; return; }
     done
     # still not found ? that's bad
     echo unknown
 }
 
+##############################
+function check_yum_installed () {
+    package=$1; shift
+    rpm -q $package >& /dev/null || yum -y install $package
+}
+
+# not used apparently
+function check_yumgroup_installed () {
+    group="$1"; shift
+    yum grouplist "$group" | grep -q Installed || { yum -y groupinstall "$group" ; }
+}
+
 #################### bridge initialization
 function create_bridge_if_needed() {
 
-    # turn on verbosity
-    set -x
+    # do not turn on verbosity
+    set -x
 
     public_bridge=$1; shift
 
     # already created ? - we're done
     ip addr show $public_bridge >& /dev/null && {
-       echo "Bridge already set up - skipping create_bridge_if_needed"
-       return 0
+        echo "Bridge already set up - skipping create_bridge_if_needed"
+        return 0
     }
 
     # find out the physical interface to bridge onto
@@ -74,7 +88,7 @@ function create_bridge_if_needed() {
     sysctl net.bridge.bridge-nf-call-ip6tables=0
     sysctl net.bridge.bridge-nf-call-arptables=0
 
-    
+
     #Getting host IP/masklen
     address=$(ip addr show $if_lan | grep -v inet6 | grep inet | head --lines=1 | awk '{print $2;}')
     [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $if_lan" ; exit 1 ; }
@@ -93,7 +107,7 @@ function create_bridge_if_needed() {
     echo "Activating promiscuous mode if_lan=$if_lan"
     ip link set $if_lan up promisc on
     sleep 2
-    # rely on dhcp to re assign IP.. 
+    # rely on dhcp to re assign IP..
     echo "Starting dhclient on $public_bridge"
     dhclient $public_bridge
     sleep 1
@@ -123,10 +137,10 @@ function create_bridge_if_needed() {
 }
 
 function main () {
-    if [[ -n "$@" ]] ; then 
-       public_bridge="$1"; shift
+    if [[ -n "$@" ]] ; then
+        public_bridge="$1"; shift
     else
-       public_bridge="$DEFAULT_PUBLIC_BRIDGE"
+        public_bridge="$DEFAULT_PUBLIC_BRIDGE"
     fi
     create_bridge_if_needed $public_bridge
 }