Adds NAT capability and other fixes for deploy.sh
[genesis.git] / foreman / ci / deploy.sh
index d1bb24c..ae585b0 100755 (executable)
@@ -24,6 +24,7 @@ blue=`tput setaf 4`
 red=`tput setaf 1`
 green=`tput setaf 2`
 
+declare -A interface_arr
 ##END VARS
 
 ##FUNCTIONS
@@ -41,6 +42,28 @@ function find_ip {
   ip addr show $1 | grep -Eo '^\s+inet\s+[\.0-9]+' | awk '{print $2}'
 }
 
+##finds subnet of ip and netmask
+##params: ip, netmask
+function find_subnet {
+  IFS=. read -r i1 i2 i3 i4 <<< "$1"
+  IFS=. read -r m1 m2 m3 m4 <<< "$2"
+  printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))"
+}
+
+##finds netmask of interface
+##params: interface
+##returns long format 255.255.x.x
+function find_netmask {
+  ifconfig $1 | grep -Eo 'netmask\s+[\.0-9]+' | awk '{print $2}'
+}
+
+##finds short netmask of interface
+##params: interface
+##returns short format, ex: /21
+function find_short_netmask {
+  echo "/$(ip addr show $1 | grep -Eo '^\s+inet\s+[\/\.0-9]+' | awk '{print $2}' | cut -d / -f2)"
+}
+
 ##increments next IP
 ##params: ip
 ##assumes a /24 subnet
@@ -184,6 +207,14 @@ else
   printf '%s\n' 'deploy.sh: Skipping kernel module for virtualbox.  Already Installed'
 fi
 
+##install Ansible
+if ! yum list installed | grep -i ansible; then
+  if ! yum -y install ansible; then
+    printf '%s\n' 'deploy.sh: Unable to install Ansible package' >&2
+    exit 1
+  fi
+fi
+
 ##install Vagrant
 if ! rpm -qa | grep vagrant; then
   if ! rpm -Uvh https://dl.bintray.com/mitchellh/vagrant/vagrant_1.7.2_x86_64.rpm; then
@@ -230,7 +261,7 @@ cd bgs_vagrant
 echo "${blue}Detecting network configuration...${reset}"
 ##detect host 1 or 3 interface configuration
 #output=`ip link show | grep -E "^[0-9]" | grep -Ev ": lo|tun|virbr|vboxnet" | awk '{print $2}' | sed 's/://'`
-output=`ifconfig | grep -E "^[a-Z0-9]+:"| grep -Ev "lo|tun|virbr|vboxnet:" | awk '{print $1}' | sed 's/://'`
+output=`ifconfig | grep -E "^[a-zA-Z0-9]+:"| grep -Ev "lo|tun|virbr|vboxnet" | awk '{print $1}' | sed 's/://'`
 
 if [ ! "$output" ]; then
   printf '%s\n' 'deploy.sh: Unable to detect interfaces to bridge to' >&2
@@ -252,8 +283,17 @@ for interface in ${output}; do
   if [ ! "$new_ip" ]; then
     continue
   fi
+  interface_arr[$interface]=$if_counter
   interface_ip_arr[$if_counter]=$new_ip
-  sed -i 's/^.*eth_replace'"$if_counter"'.*$/  config.vm.network "public_network", ip: '\""$new_ip"\"', bridge: '\'"$interface"\''/' Vagrantfile
+  subnet_mask=$(find_netmask $interface)
+  if [ "$if_counter" -eq 1 ]; then
+    private_subnet_mask=$subnet_mask
+    private_short_subnet_mask=$(find_short_netmask $interface)
+  fi
+  if [ "$if_counter" -eq 3 ]; then
+    storage_subnet_mask=$subnet_mask
+  fi
+  sed -i 's/^.*eth_replace'"$if_counter"'.*$/  config.vm.network "public_network", ip: '\""$new_ip"\"', bridge: '\'"$interface"\'', netmask: '\""$subnet_mask"\"'/' Vagrantfile
   ((if_counter++))
 done
 
@@ -280,19 +320,49 @@ fi
 echo "${blue}Network detected: ${deployment_type}! ${reset}"
 
 if route | grep default; then
-  defaultgw=$(route | grep default | awk '{print $2}')
-  echo "${blue}Default gateway detected: $defaultgw ${reset}"
-  sed -i 's/^.*default_gw =.*$/  default_gw = '\""$defaultgw"\"'/' Vagrantfile
+  echo "${blue}Default Gateway Detected ${reset}"
+  host_default_gw=$(ip route | grep default | awk '{print $3}')
+  echo "${blue}Default Gateway: $host_default_gw ${reset}"
+  default_gw_interface=$(ip route get $host_default_gw | awk '{print $3}')
+  case "${interface_arr[$default_gw_interface]}" in
+           0)
+             echo "${blue}Default Gateway Detected on Admin Interface!${reset}"
+             sed -i 's/^.*default_gw =.*$/  default_gw = '\""$host_default_gw"\"'/' Vagrantfile
+             node_default_gw=$host_default_gw
+             ;;
+           1)
+             echo "${red}Default Gateway Detected on Private Interface!${reset}"
+             echo "${red}Private subnet should be private and not have Internet access!${reset}"
+             exit 1
+             ;;
+           2)
+             echo "${blue}Default Gateway Detected on Public Interface!${reset}"
+             sed -i 's/^.*default_gw =.*$/  default_gw = '\""$host_default_gw"\"'/' Vagrantfile
+             echo "${blue}Will setup NAT from Admin -> Public Network on VM!${reset}"
+             sed -i 's/^.*nat_flag =.*$/  nat_flag = true/' Vagrantfile
+             echo "${blue}Setting node gateway to be VM Admin IP${reset}"
+             node_default_gw=${interface_ip_arr[0]}
+             ;;
+           3)
+             echo "${red}Default Gateway Detected on Storage Interface!${reset}"
+             echo "${red}Storage subnet should be private and not have Internet access!${reset}"
+             exit 1
+             ;;
+           *)
+             echo "${red}Unable to determine which interface default gateway is on..Exiting!${reset}"
+             exit 1
+             ;;
+  esac
 else
-  defaultgw=`echo ${interface_arr_ip[0]} | cut -d. -f1-3`
+  #assumes 24 bit mask
+  defaultgw=`echo ${interface_ip_arr[0]} | cut -d. -f1-3`
   firstip=.1
   defaultgw=$defaultgw$firstip
   echo "${blue}Unable to find default gateway.  Assuming it is $defaultgw ${reset}"
   sed -i 's/^.*default_gw =.*$/  default_gw = '\""$defaultgw"\"'/' Vagrantfile
+  node_default_gw=$defaultgw
 fi
 
-sed -i 's/^.*default_gw:.*$/default_gw:'" $defaultgw"'/' opnfv_ksgen_settings.yml
-
 if [ $base_config ]; then
   if ! cp -f $base_config opnfv_ksgen_settings.yml; then
     echo "{red}ERROR: Unable to copy $base_config to opnfv_ksgen_settings.yml${reset}"
@@ -311,12 +381,13 @@ echo "${blue}Gathering network parameters for Target System...this may take a fe
 ##if single node deployment all the variables will have the same ip
 ##interface names will be enp0s3, enp0s8, enp0s9 in chef/centos7
 
+sed -i 's/^.*default_gw:.*$/default_gw:'" $node_default_gw"'/' opnfv_ksgen_settings.yml
+
 ##replace private interface parameter
 ##private interface will be of hosts, so we need to know the provisioned host interface name
 ##we add biosdevname=0, net.ifnames=0 to the kickstart to use regular interface naming convention on hosts
 ##replace IP for parameters with next IP that will be given to controller
 if [ "$deployment_type" == "single_network" ]; then
-  sed -i 's/^.*ovs_tunnel_if:.*$/  ovs_tunnel_if: eth0/' opnfv_ksgen_settings.yml
   ##we also need to assign IP addresses to nodes
   ##for single node, foreman is managing the single network, so we can't reserve them
   ##not supporting single network anymore for now
@@ -324,13 +395,9 @@ if [ "$deployment_type" == "single_network" ]; then
   exit 0
 
 elif [[ "$deployment_type" == "multi_network" || "$deployment_type" == "three_network" ]]; then
-  sed -i 's/^.*ovs_tunnel_if:.*$/  ovs_tunnel_if: eth1/' opnfv_ksgen_settings.yml
 
   if [ "$deployment_type" == "three_network" ]; then
-    sed -i 's/^.*storage_iface:.*$/  storage_iface: eth1/' opnfv_ksgen_settings.yml
-    sed -i 's/^.*network_type:.*$/  network_type: three_network/' opnfv_ksgen_settings.yml
-  else
-    sed -i 's/^.*storage_iface:.*$/  storage_iface: eth3/' opnfv_ksgen_settings.yml
+    sed -i 's/^.*network_type:.*$/network_type: three_network/' opnfv_ksgen_settings.yml
   fi
 
   ##get ip addresses for private network on controllers to make dhcp entries
@@ -365,8 +432,10 @@ elif [[ "$deployment_type" == "multi_network" || "$deployment_type" == "three_ne
     fi
   done
 
-  ##replace public_vips
+  ##replace foreman site
   next_public_ip=${interface_ip_arr[2]}
+  sed -i 's/^.*foreman_url:.*$/  foreman_url:'" https:\/\/$next_public_ip"'\/api\/v2\//' opnfv_ksgen_settings.yml
+  ##replace public vips
   next_public_ip=$(increment_ip $next_public_ip 10)
   grep -E '*public_vip' opnfv_ksgen_settings.yml | while read -r line ; do
     sed -i 's/^.*'"$line"'.*$/  '"$line $next_public_ip"'/' opnfv_ksgen_settings.yml
@@ -377,11 +446,21 @@ elif [[ "$deployment_type" == "multi_network" || "$deployment_type" == "three_ne
     fi
   done
 
+  ##replace private_network param
+  private_subnet=$(find_subnet $next_private_ip $private_subnet_mask)
+  sed -i 's/^.*private_network:.*$/  private_network:'" $private_subnet"'/' opnfv_ksgen_settings.yml
+  ##replace storage_network
+  if [ "$deployment_type" == "three_network" ]; then
+    sed -i 's/^.*storage_network:.*$/  storage_network:'" $private_subnet"'/' opnfv_ksgen_settings.yml
+  else
+    next_storage_ip=${interface_ip_arr[3]}
+    storage_subnet=$(find_subnet $next_storage_ip $storage_subnet_mask)
+    sed -i 's/^.*storage_network:.*$/  storage_network:'" $storage_subnet"'/' opnfv_ksgen_settings.yml
+  fi
+
   ##replace private_subnet param
-  network=.0
-  baseaddr="$(echo $next_private_ip | cut -d. -f1-3)"
-  private_subnet=$baseaddr$network
-  sed -i 's/^.*private_subnet:.*$/  private_subnet:'" $private_subnet"''"\/24"'/' opnfv_ksgen_settings.yml
+  private_subnet=$private_subnet'\'$private_short_subnet_mask
+  sed -i 's/^.*private_subnet:.*$/  private_subnet:'" $private_subnet"'/' opnfv_ksgen_settings.yml
 else
   printf '%s\n' 'deploy.sh: Unknown network type: $deployment_type' >&2
   exit 1