Merge "Updates the real time kvm kernel from 4.4.6-rt14 to 4.4.50-rt62."
[apex.git] / lib / common-functions.sh
index e7041ac..709dbf9 100644 (file)
@@ -1,4 +1,13 @@
 #!/usr/bin/env bash
+##############################################################################
+# Copyright (c) 2015 Tim Rozet (Red Hat), Dan Radez (Red Hat) and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
 # Common Functions used by  OPNFV Apex
 # author: Tim Rozet (trozet@redhat.com)
 
@@ -12,21 +21,28 @@ function prefix2mask {
 }
 
 ##find ip of interface
-##params: interface name
+##params: interface name, address family
 function find_ip {
+  local af
   if [[ -z "$1" ]]; then
     return 1
   fi
+  if [[ -z "$2" ]]; then
+    af=4
+  else
+    af=$2
+  fi
 
-  python3.4 -B $CONFIG/lib/python/apex-python-utils.py find_ip -i $1
+  python3 -B $LIB/python/apex_python_utils.py find-ip -i $1 -af $af
 }
 
 ##attach interface to OVS and set the network config correctly
 ##params: bride to attach to, interface to attach, network type (optional)
-##public indicates attaching to a public interface
+##external indicates attaching to a external interface
 function attach_interface_to_ovs {
   local bridge interface
   local if_ip if_mask if_gw if_file ovs_file if_prefix
+  local if_metric if_dns1 if_dns2
 
   if [[ -z "$1" || -z "$2" ]]; then
     return 1
@@ -46,6 +62,9 @@ function attach_interface_to_ovs {
     if_ip=$(sed -n 's/^IPADDR=\(.*\)$/\1/p' ${if_file})
     if_mask=$(sed -n 's/^NETMASK=\(.*\)$/\1/p' ${if_file})
     if_gw=$(sed -n 's/^GATEWAY=\(.*\)$/\1/p' ${if_file})
+    if_metric=$(sed -n 's/^METRIC=\(.*\)$/\1/p' ${if_file})
+    if_dns1=$(sed -n 's/^DNS1=\(.*\)$/\1/p' ${if_file})
+    if_dns2=$(sed -n 's/^DNS2=\(.*\)$/\1/p' ${if_file})
   else
     echo "ERROR: ifcfg file missing for ${interface}"
     return 1
@@ -53,15 +72,15 @@ function attach_interface_to_ovs {
 
   if [ -z "$if_mask" ]; then
     # we can look for PREFIX here, then convert it to NETMASK
-    if_prefix=$(sed -n 's/^PREFIX=\(.*\)$/\1/p' ${if_file})
+    if_prefix=$(sed -n 's/^PREFIX=[^0-9]*\([0-9][0-9]*\)[^0-9]*$/\1/p' ${if_file})
     if_mask=$(prefix2mask ${if_prefix})
   fi
 
   if [[ -z "$if_ip" || -z "$if_mask" ]]; then
     echo "ERROR: IPADDR or NETMASK/PREFIX missing for ${interface}"
     return 1
-  elif [[ -z "$if_gw" && "$3" == "public_network" ]]; then
-    echo "ERROR: GATEWAY missing for ${interface}, which is public"
+  elif [[ -z "$if_gw" && "$3" == "external" ]]; then
+    echo "ERROR: GATEWAY missing for ${interface}, which is external"
     return 1
   fi
 
@@ -77,7 +96,7 @@ ONBOOT=yes
 OVS_BRIDGE=${bridge}
 PROMISC=yes" > ${if_file}
 
-  if [ -z ${if_gw} ]; then
+
   # create bridge cfg
   echo "DEVICE=${bridge}
 DEVICETYPE=ovs
@@ -89,17 +108,24 @@ TYPE=OVSBridge
 PROMISC=yes
 PEERDNS=no" > ${ovs_file}
 
-  else
-    echo "DEVICE=${bridge}
-DEVICETYPE=ovs
-IPADDR=${if_ip}
-NETMASK=${if_mask}
-BOOTPROTO=static
-ONBOOT=yes
-TYPE=OVSBridge
-PROMISC=yes
-GATEWAY=${if_gw}
-PEERDNS=no" > ${ovs_file}
+  if [ -n "$if_gw" ]; then
+    echo "GATEWAY=${if_gw}" >> ${ovs_file}
+  fi
+
+  if [ -n "$if_metric" ]; then
+    echo "METRIC=${if_metric}" >> ${ovs_file}
+  fi
+
+  if [[ -n "$if_dns1" || -n "$if_dns2" ]]; then
+    sed -i '/PEERDNS/c\PEERDNS=yes' ${ovs_file}
+
+    if [ -n "$if_dns1" ]; then
+      echo "DNS1=${if_dns1}" >> ${ovs_file}
+    fi
+
+    if [ -n "$if_dns2" ]; then
+      echo "DNS2=${if_dns2}" >> ${ovs_file}
+    fi
   fi
 
   sudo systemctl restart network
@@ -113,6 +139,7 @@ function detach_interface_from_ovs {
   local port_output ports_no_orig
   local net_path
   local if_ip if_mask if_gw if_prefix
+  local if_metric if_dns1 if_dns2
 
   net_path=/etc/sysconfig/network-scripts/
   if [[ -z "$1" ]]; then
@@ -134,12 +161,15 @@ function detach_interface_from_ovs {
     elif [ -e ${net_path}/ifcfg-${line}.orig ]; then
       mv -f ${net_path}/ifcfg-${line}.orig ${net_path}/ifcfg-${line}
     elif [ -e ${net_path}/ifcfg-${bridge} ]; then
-      if_ip=$(sed -n 's/^IPADDR=\(.*\)$/\1/p' ${if_file})
-      if_mask=$(sed -n 's/^NETMASK=\(.*\)$/\1/p' ${if_file})
-      if_gw=$(sed -n 's/^GATEWAY=\(.*\)$/\1/p' ${if_file})
+      if_ip=$(sed -n 's/^IPADDR=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
+      if_mask=$(sed -n 's/^NETMASK=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
+      if_gw=$(sed -n 's/^GATEWAY=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
+      if_metric=$(sed -n 's/^METRIC=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
+      if_dns1=$(sed -n 's/^DNS1=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
+      if_dns2=$(sed -n 's/^DNS2=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
 
       if [ -z "$if_mask" ]; then
-        if_prefix=$(sed -n 's/^PREFIX=\(.*\)$/\1/p' ${if_file})
+        if_prefix=$(sed -n 's/^PREFIX=[^0-9]*\([0-9][0-9]*\)[^0-9]*$/\1/p' ${net_path}/ifcfg-${bridge})
         if_mask=$(prefix2mask ${if_prefix})
       fi
 
@@ -148,26 +178,34 @@ function detach_interface_from_ovs {
         return 1
       fi
 
-      if [ -z ${if_gw} ]; then
-        # create if cfg
-        echo "DEVICE=${line}
-IPADDR=${if_ip}
-NETMASK=${if_mask}
-BOOTPROTO=static
-ONBOOT=yes
-TYPE=Ethernet
-NM_CONTROLLED=no
-PEERDNS=no" > ${net_path}/ifcfg-${line}
-      else
-        echo "DEVICE=${line}
+      # create if cfg
+      echo "DEVICE=${line}
 IPADDR=${if_ip}
 NETMASK=${if_mask}
 BOOTPROTO=static
 ONBOOT=yes
 TYPE=Ethernet
 NM_CONTROLLED=no
-GATEWAY=${if_gw}
 PEERDNS=no" > ${net_path}/ifcfg-${line}
+
+      if [ -n "$if_gw" ]; then
+        echo "GATEWAY=${if_gw}" >> ${net_path}/ifcfg-${line}
+      fi
+
+      if [ -n "$if_metric" ]; then
+        echo "METRIC=${if_metric}" >> ${net_path}/ifcfg-${line}
+      fi
+
+      if [[ -n "$if_dns1" || -n "$if_dns2" ]]; then
+        sed -i '/PEERDNS/c\PEERDNS=yes' ${net_path}/ifcfg-${line}
+
+        if [ -n "$if_dns1" ]; then
+          echo "DNS1=${if_dns1}" >> ${net_path}/ifcfg-${line}
+        fi
+
+        if [ -n "$if_dns2" ]; then
+          echo "DNS2=${if_dns2}" >> ${net_path}/ifcfg-${line}
+        fi
       fi
       break
     else
@@ -182,6 +220,10 @@ PEERDNS=no" > ${net_path}/ifcfg-${line}
   sudo sed -i 's/IPADDR=.*//' ${net_path}/ifcfg-${bridge}
   sudo sed -i 's/NETMASK=.*//' ${net_path}/ifcfg-${bridge}
   sudo sed -i 's/GATEWAY=.*//' ${net_path}/ifcfg-${bridge}
+  sudo sed -i 's/DNS1=.*//' ${net_path}/ifcfg-${bridge}
+  sudo sed -i 's/DNS2=.*//' ${net_path}/ifcfg-${bridge}
+  sudo sed -i 's/METRIC=.*//' ${net_path}/ifcfg-${bridge}
+  sudo sed -i 's/PEERDNS=.*//' ${net_path}/ifcfg-${bridge}
 
   sudo systemctl restart network
 }
@@ -221,3 +263,46 @@ function prompt_user {
     fi
   done
 }
+
+##checks if prefix exists in string
+##params: string, prefix
+##usage: contains_prefix "deploy_setting_launcher=1" "deploy_setting"
+contains_prefix() {
+  local mystr=$1
+  local prefix=$2
+  if echo $mystr | grep -E "^$prefix.*$" > /dev/null; then
+    return 0
+  else
+    return 1
+  fi
+}
+
+##verify internet connectivity
+#params: none
+function verify_internet {
+  if ping -c 2 $ping_site > /dev/null; then
+    if ping -c 2 $dnslookup_site > /dev/null; then
+      echo "${blue}Internet connectivity detected${reset}"
+      return 0
+    else
+      echo "${red}Internet connectivity detected, but DNS lookup failed${reset}"
+      return 1
+    fi
+  else
+    echo "${red}No internet connectivity detected${reset}"
+    return 1
+  fi
+}
+
+##tests if overcloud nodes have external connectivity
+#params:none
+function test_overcloud_connectivity {
+  for node in $(undercloud_connect stack ". stackrc && nova list" | grep -Eo "controller-[0-9]+|compute-[0-9]+" | tr -d -) ; do
+    if ! overcloud_connect $node "ping -c 2 $ping_site > /dev/null"; then
+      echo "${blue}Node ${node} was unable to ping site ${ping_site}${reset}"
+      return 1
+    fi
+  done
+  echo "${blue}Overcloud external connectivity OK${reset}"
+}
+