Moved 80% of Foreman deploy.sh into functions
[genesis.git] / foreman / ci / deploy.sh
index 31d41d2..46ba80e 100755 (executable)
@@ -149,19 +149,21 @@ parse_yaml() {
    }'
 }
 
-##END FUNCTIONS
-
-if [[ ( $1 == "--help") ||  $1 == "-h" ]]; then
+##translates the command line paramaters into variables
+##params: $@ the entire command line is passed
+##usage: parse_cmd_line() "$@"
+parse_cmdline() {
+  if [[ ( $1 == "--help") ||  $1 == "-h" ]]; then
     display_usage
     exit 0
-fi
+  fi
 
-echo -e "\n\n${blue}This script is used to deploy Foreman/QuickStack Installer and Provision OPNFV Target System${reset}\n\n"
-echo "Use -h to display help"
-sleep 2
+  echo -e "\n\n${blue}This script is used to deploy Foreman/QuickStack Installer and Provision OPNFV Target System${reset}\n\n"
+  echo "Use -h to display help"
+  sleep 2
 
-while [ "`echo $1 | cut -c1`" = "-" ]
-do
+  while [ "`echo $1 | cut -c1`" = "-" ]
+  do
     echo $1
     case "$1" in
         -base_config)
@@ -180,31 +182,45 @@ do
                 display_usage
                 exit 1
             ;;
-esac
-done
+    esac
+  done
+}
 
 ##disable selinux
-/sbin/setenforce 0
-
-# Install EPEL repo for access to many other yum repos
-# Major version is pinned to force some consistency for Arno
-yum install -y epel-release-7*
-
-# Install other required packages
-# Major versions are pinned to force some consistency for Arno
-if ! yum install -y binutils-2* gcc-4* make-3* patch-2* libgomp-4* glibc-headers-2* glibc-devel-2* kernel-headers-3* kernel-devel-3* dkms-2* psmisc-22*; then
-  printf '%s\n' 'deploy.sh: Unable to install depdency packages' >&2
-  exit 1
-fi
-
-##install VirtualBox repo
-if cat /etc/*release | grep -i "Fedora release"; then
-  vboxurl=http://download.virtualbox.org/virtualbox/rpm/fedora/\$releasever/\$basearch
-else
-  vboxurl=http://download.virtualbox.org/virtualbox/rpm/el/\$releasever/\$basearch
-fi
-
-cat > /etc/yum.repos.d/virtualbox.repo << EOM
+##params: none
+##usage: disable_selinux()
+disable_selinux() {
+  /sbin/setenforce 0
+}
+
+##Install the EPEL repository and additional packages
+##params: none
+##usage: install_EPEL()
+install_EPEL() {
+  # Install EPEL repo for access to many other yum repos
+  # Major version is pinned to force some consistency for Arno
+  yum install -y epel-release-7*
+
+  # Install other required packages
+  # Major versions are pinned to force some consistency for Arno
+  if ! yum install -y binutils-2* gcc-4* make-3* patch-2* libgomp-4* glibc-headers-2* glibc-devel-2* kernel-headers-3* kernel-devel-3* dkms-2* psmisc-22*; then
+    printf '%s\n' 'deploy.sh: Unable to install depdency packages' >&2
+    exit 1
+  fi
+}
+
+##Download and install virtual box
+##params: none
+##usage: install_vbox()
+install_vbox() {
+  ##install VirtualBox repo
+  if cat /etc/*release | grep -i "Fedora release"; then
+    vboxurl=http://download.virtualbox.org/virtualbox/rpm/fedora/\$releasever/\$basearch
+  else
+    vboxurl=http://download.virtualbox.org/virtualbox/rpm/el/\$releasever/\$basearch
+  fi
+
+  cat > /etc/yum.repos.d/virtualbox.repo << EOM
 [virtualbox]
 name=Oracle Linux / RHEL / CentOS-\$releasever / \$basearch - VirtualBox
 baseurl=$vboxurl
@@ -215,380 +231,101 @@ skip_if_unavailable = 1
 keepcache = 0
 EOM
 
-##install VirtualBox
-if ! yum list installed | grep -i virtualbox; then
-  if ! yum -y install VirtualBox-4.3; then
-    printf '%s\n' 'deploy.sh: Unable to install virtualbox package' >&2
-    exit 1
+  ##install VirtualBox
+  if ! yum list installed | grep -i virtualbox; then
+    if ! yum -y install VirtualBox-4.3; then
+      printf '%s\n' 'deploy.sh: Unable to install virtualbox package' >&2
+      exit 1
+    fi
   fi
-fi
 
-##install kmod-VirtualBox
-if ! lsmod | grep vboxdrv; then
-  sudo /etc/init.d/vboxdrv setup
+  ##install kmod-VirtualBox
   if ! lsmod | grep vboxdrv; then
-    printf '%s\n' 'deploy.sh: Unable to install kernel module for virtualbox' >&2
-    exit 1
-  fi
-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-1*; then
-    printf '%s\n' 'deploy.sh: Unable to install Ansible package' >&2
-    exit 1
+    sudo /etc/init.d/vboxdrv setup
+    if ! lsmod | grep vboxdrv; then
+      printf '%s\n' 'deploy.sh: Unable to install kernel module for virtualbox' >&2
+      exit 1
+    fi
+  else
+    printf '%s\n' 'deploy.sh: Skipping kernel module for virtualbox.  Already Installed'
   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
-    printf '%s\n' 'deploy.sh: Unable to install vagrant package' >&2
-    exit 1
-  fi
-else
-  printf '%s\n' 'deploy.sh: Skipping Vagrant install as it is already installed.'
-fi
-
-##add centos 7 box to vagrant
-if ! vagrant box list | grep chef/centos-7.0; then
-  if ! vagrant box add chef/centos-7.0 --provider virtualbox; then
-    printf '%s\n' 'deploy.sh: Unable to download centos7 box for Vagrant' >&2
-    exit 1
-  fi
-else
-  printf '%s\n' 'deploy.sh: Skipping Vagrant box add as centos-7.0 is already installed.'
-fi
-
-##install workaround for centos7
-if ! vagrant plugin list | grep vagrant-centos7_fix; then
-  if ! vagrant plugin install vagrant-centos7_fix; then
-    printf '%s\n' 'deploy.sh: Warning: unable to install vagrant centos7 workaround' >&2
+##install Ansible using yum
+##params: none
+##usage: install_anible()
+install_ansible() {
+  if ! yum list installed | grep -i ansible; then
+    if ! yum -y install ansible-1*; then
+      printf '%s\n' 'deploy.sh: Unable to install Ansible package' >&2
+      exit 1
+    fi
   fi
-else
-  printf '%s\n' 'deploy.sh: Skipping Vagrant plugin as centos7 workaround is already installed.'
-fi
-
-cd /tmp/
-
-##remove bgs vagrant incase it wasn't cleaned up
-rm -rf /tmp/bgs_vagrant
-
-##clone bgs vagrant
-##will change this to be opnfv repo when commit is done
-if ! git clone -b v1.0 https://github.com/trozet/bgs_vagrant.git; then
-  printf '%s\n' 'deploy.sh: Unable to clone vagrant repo' >&2
-  exit 1
-fi
-
-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-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
-  exit 1
-fi
-
-##find number of interfaces with ip and substitute in VagrantFile
-if_counter=0
-for interface in ${output}; do
+}
 
-  if [ "$if_counter" -ge 4 ]; then
-    break
-  fi
-  interface_ip=$(find_ip $interface)
-  if [ ! "$interface_ip" ]; then
-    continue
-  fi
-  new_ip=$(next_usable_ip $interface_ip)
-  if [ ! "$new_ip" ]; then
-    continue
-  fi
-  interface_arr[$interface]=$if_counter
-  interface_ip_arr[$if_counter]=$new_ip
-  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 2 ]; then
-    public_subnet_mask=$subnet_mask
-    public_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
-
-##now remove interface config in Vagrantfile for 1 node
-##if 1, 3, or 4 interfaces set deployment type
-##if 2 interfaces remove 2nd interface and set deployment type
-if [ "$if_counter" == 1 ]; then
-  deployment_type="single_network"
-  remove_vagrant_network eth_replace1
-  remove_vagrant_network eth_replace2
-  remove_vagrant_network eth_replace3
-elif [ "$if_counter" == 2 ]; then
-  deployment_type="single_network"
-  second_interface=`echo $output | awk '{print $2}'`
-  remove_vagrant_network $second_interface
-  remove_vagrant_network eth_replace2
-elif [ "$if_counter" == 3 ]; then
-  deployment_type="three_network"
-  remove_vagrant_network eth_replace3
-else
-  deployment_type="multi_network"
-fi
-
-echo "${blue}Network detected: ${deployment_type}! ${reset}"
-
-if route | grep default; then
-  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]}
-             public_gateway=$default_gw
-             ;;
-           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
-  #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
-
-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}"
-    exit 1
+##install Vagrant RPM directly with the bintray.com site
+##params: none
+##usage: install_vagrant()
+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
+      printf '%s\n' 'deploy.sh: Unable to install vagrant package' >&2
+      exit 1
+    fi
+  else
+    printf '%s\n' 'deploy.sh: Skipping Vagrant install as it is already installed.'
   fi
-fi
-
-if [ $no_parse ]; then
-echo "${blue}Skipping parsing variables into settings file as no_parse flag is set${reset}"
-
-else
-
-echo "${blue}Gathering network parameters for Target System...this may take a few minutes${reset}"
-##Edit the ksgen settings appropriately
-##ksgen settings will be stored in /vagrant on the vagrant machine
-##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
-  ##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
-  echo "{blue}Single Network type is unsupported right now.  Please check your interface configuration.  Exiting. ${reset}"
-  exit 0
-
-elif [[ "$deployment_type" == "multi_network" || "$deployment_type" == "three_network" ]]; then
-
-  if [ "$deployment_type" == "three_network" ]; then
-    sed -i 's/^.*network_type:.*$/network_type: three_network/' opnfv_ksgen_settings.yml
+  ##add centos 7 box to vagrant
+  if ! vagrant box list | grep chef/centos-7.0; then
+    if ! vagrant box add chef/centos-7.0 --provider virtualbox; then
+      printf '%s\n' 'deploy.sh: Unable to download centos7 box for Vagrant' >&2
+      exit 1
+    fi
+  else
+    printf '%s\n' 'deploy.sh: Skipping Vagrant box add as centos-7.0 is already installed.'
   fi
 
-  sed -i 's/^.*deployment_type:.*$/  deployment_type: '"$deployment_type"'/' opnfv_ksgen_settings.yml
-
-  ##get ip addresses for private network on controllers to make dhcp entries
-  ##required for controllers_ip_array global param
-  next_private_ip=${interface_ip_arr[1]}
-  type=_private
-  for node in controller1 controller2 controller3; do
-    next_private_ip=$(next_usable_ip $next_private_ip)
-    if [ ! "$next_private_ip" ]; then
-       printf '%s\n' 'deploy.sh: Unable to find next ip for private network for control nodes' >&2
-       exit 1
+  ##install workaround for centos7
+  if ! vagrant plugin list | grep vagrant-centos7_fix; then
+    if ! vagrant plugin install vagrant-centos7_fix; then
+      printf '%s\n' 'deploy.sh: Warning: unable to install vagrant centos7 workaround' >&2
     fi
-    sed -i 's/'"$node$type"'/'"$next_private_ip"'/g' opnfv_ksgen_settings.yml
-    controller_ip_array=$controller_ip_array$next_private_ip,
-  done
+  else
+    printf '%s\n' 'deploy.sh: Skipping Vagrant plugin as centos7 workaround is already installed.'
+  fi
+}
 
-  ##replace global param for contollers_ip_array
-  controller_ip_array=${controller_ip_array%?}
-  sed -i 's/^.*controllers_ip_array:.*$/  controllers_ip_array: '"$controller_ip_array"'/' opnfv_ksgen_settings.yml
-
-  ##now replace all the VIP variables.  admin//private can be the same IP
-  ##we have to use IP's here that won't be allocated to hosts at provisioning time
-  ##therefore we increment the ip by 10 to make sure we have a safe buffer
-  next_private_ip=$(increment_ip $next_private_ip 10)
-
-  grep -E '*private_vip|loadbalancer_vip|db_vip|amqp_vip|*admin_vip' opnfv_ksgen_settings.yml | while read -r line ; do
-    sed -i 's/^.*'"$line"'.*$/  '"$line $next_private_ip"'/' opnfv_ksgen_settings.yml
-    next_private_ip=$(next_usable_ip $next_private_ip)
-    if [ ! "$next_private_ip" ]; then
-       printf '%s\n' 'deploy.sh: Unable to find next ip for private network for vip replacement' >&2
-       exit 1
-    fi
-  done
 
-  ##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
-    next_public_ip=$(next_usable_ip $next_public_ip)
-    if [ ! "$next_public_ip" ]; then
-       printf '%s\n' 'deploy.sh: Unable to find next ip for public network for vip replcement' >&2
-       exit 1
-    fi
-  done
+##remove bgs vagrant incase it wasn't cleaned up
+##params: none
+##usage: clean_tmp()
+clean_tmp() {
+  rm -rf /tmp/bgs_vagrant
+}
 
-  ##replace public_network param
-  public_subnet=$(find_subnet $next_public_ip $public_subnet_mask)
-  sed -i 's/^.*public_network:.*$/  public_network:'" $public_subnet"'/' opnfv_ksgen_settings.yml
-  ##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
+##clone bgs vagrant version 1.0 using git
+##params: none
+##usage: clone_bgs
+clone_bgs() {
+  cd /tmp/
 
-  ##replace public_subnet param
-  public_subnet=$public_subnet'\'$public_short_subnet_mask
-  sed -i 's/^.*public_subnet:.*$/  public_subnet:'" $public_subnet"'/' opnfv_ksgen_settings.yml
-  ##replace private_subnet param
-  private_subnet=$private_subnet'\'$private_short_subnet_mask
-  sed -i 's/^.*private_subnet:.*$/  private_subnet:'" $private_subnet"'/' opnfv_ksgen_settings.yml
-
-  ##replace public_dns param to be foreman server
-  sed -i 's/^.*public_dns:.*$/  public_dns: '${interface_ip_arr[2]}'/' opnfv_ksgen_settings.yml
-
-  ##replace public_gateway
-  if [ -z "$public_gateway" ]; then
-    ##if unset then we assume its the first IP in the public subnet
-    public_subnet=$(find_subnet $next_public_ip $public_subnet_mask)
-    public_gateway=$(increment_subnet $public_subnet 1)
-  fi
-  sed -i 's/^.*public_gateway:.*$/  public_gateway:'" $public_gateway"'/' opnfv_ksgen_settings.yml
-
-  ##we have to define an allocation range of the public subnet to give
-  ##to neutron to use as floating IPs
-  ##we should control this subnet, so this range should work .150-200
-  ##but generally this is a bad idea and we are assuming at least a /24 subnet here
-  public_subnet=$(find_subnet $next_public_ip $public_subnet_mask)
-  public_allocation_start=$(increment_subnet $public_subnet 150)
-  public_allocation_end=$(increment_subnet $public_subnet 200)
-
-  sed -i 's/^.*public_allocation_start:.*$/  public_allocation_start:'" $public_allocation_start"'/' opnfv_ksgen_settings.yml
-  sed -i 's/^.*public_allocation_end:.*$/  public_allocation_end:'" $public_allocation_end"'/' opnfv_ksgen_settings.yml
-
-else
-  printf '%s\n' 'deploy.sh: Unknown network type: $deployment_type' >&2
-  exit 1
-fi
-
-echo "${blue}Parameters Complete.  Settings have been set for Foreman. ${reset}"
-
-fi
-
-if [ $virtual ]; then
-  echo "${blue} Virtual flag detected, setting Khaleesi playbook to be opnfv-vm.yml ${reset}"
-  sed -i 's/opnfv.yml/opnfv-vm.yml/' bootstrap.sh
-fi
-
-echo "${blue}Starting Vagrant! ${reset}"
-
-##stand up vagrant
-if ! vagrant up; then
-  printf '%s\n' 'deploy.sh: Unable to start vagrant' >&2
-  exit 1
-else
-  echo "${blue}Foreman VM is up! ${reset}"
-fi
-
-if [ $virtual ]; then
-
-##Bring up VM nodes
-echo "${blue}Setting VMs up... ${reset}"
-nodes=`sed -nr '/nodes:/{:start /workaround/!{N;b start};//p}' opnfv_ksgen_settings.yml | sed -n '/^  [A-Za-z0-9]\+:$/p' | sed 's/\s*//g' | sed 's/://g'`
-##due to ODL Helium bug of OVS connecting to ODL too early, we need controllers to install first
-##this is fix kind of assumes more than I would like to, but for now it should be OK as we always have
-##3 static controllers
-compute_nodes=`echo $nodes | tr " " "\n" | grep -v controller | tr "\n" " "`
-controller_nodes=`echo $nodes | tr " " "\n" | grep controller | tr "\n" " "`
-nodes=${controller_nodes}${compute_nodes}
-
-for node in ${nodes}; do
-  cd /tmp
-
-  ##remove VM nodes incase it wasn't cleaned up
-  rm -rf /tmp/$node
-
-  ##clone bgs vagrant
   ##will change this to be opnfv repo when commit is done
-  if ! git clone -b v1.0 https://github.com/trozet/bgs_vagrant.git $node; then
+  if ! git clone -b v1.0 https://github.com/trozet/bgs_vagrant.git; then
     printf '%s\n' 'deploy.sh: Unable to clone vagrant repo' >&2
     exit 1
   fi
+}
 
-  cd $node
-
-  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}"
-      exit 1
-    fi
-  fi
-
-  ##parse yaml into variables
-  eval $(parse_yaml opnfv_ksgen_settings.yml "config_")
-  ##find node type
-  node_type=config_nodes_${node}_type
-  node_type=$(eval echo \$$node_type)
+##validates the netork settings and update VagrantFile with network settings
+##params: none
+##usage: configure_network()
+configure_network() {
+  cd /tmp/bgs_vagrant
 
-  ##find number of interfaces with ip and substitute in VagrantFile
+  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-zA-Z0-9]+:"| grep -Ev "lo|tun|virbr|vboxnet" | awk '{print $1}' | sed 's/://'`
 
   if [ ! "$output" ]; then
@@ -596,7 +333,7 @@ for node in ${nodes}; do
     exit 1
   fi
 
-
+  ##find number of interfaces with ip and substitute in VagrantFile
   if_counter=0
   for interface in ${output}; do
 
@@ -607,36 +344,25 @@ for node in ${nodes}; do
     if [ ! "$interface_ip" ]; then
       continue
     fi
-    case "${if_counter}" in
-           0)
-             mac_string=config_nodes_${node}_mac_address
-             mac_addr=$(eval echo \$$mac_string)
-             mac_addr=$(echo $mac_addr | sed 's/:\|-//g')
-             if [ $mac_addr == "" ]; then
-                 echo "${red} Unable to find mac_address for $node! ${reset}"
-                 exit 1
-             fi
-             ;;
-           1)
-             if [ "$node_type" == "controller" ]; then
-               mac_string=config_nodes_${node}_private_mac
-               mac_addr=$(eval echo \$$mac_string)
-               if [ $mac_addr == "" ]; then
-                 echo "${red} Unable to find private_mac for $node! ${reset}"
-                 exit 1
-               fi
-             else
-               ##generate random mac
-               mac_addr=$(echo -n 00-60-2F; dd bs=1 count=3 if=/dev/random 2>/dev/null |hexdump -v -e '/1 "-%02X"')
-             fi
-             mac_addr=$(echo $mac_addr | sed 's/:\|-//g')
-             ;;
-           *)
-             mac_addr=$(echo -n 00-60-2F; dd bs=1 count=3 if=/dev/random 2>/dev/null |hexdump -v -e '/1 "-%02X"')
-             mac_addr=$(echo $mac_addr | sed 's/:\|-//g')
-             ;;
-    esac
-    sed -i 's/^.*eth_replace'"$if_counter"'.*$/  config.vm.network "public_network", bridge: '\'"$interface"\'', :mac => '\""$mac_addr"\"'/' Vagrantfile
+    new_ip=$(next_usable_ip $interface_ip)
+    if [ ! "$new_ip" ]; then
+      continue
+    fi
+    interface_arr[$interface]=$if_counter
+    interface_ip_arr[$if_counter]=$new_ip
+    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 2 ]; then
+      public_subnet_mask=$subnet_mask
+      public_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
 
@@ -660,36 +386,384 @@ for node in ${nodes}; do
     deployment_type="multi_network"
   fi
 
-  ##modify provisioning to do puppet install, config, and foreman check-in
-  ##substitute host_name and dns_server in the provisioning script
-  host_string=config_nodes_${node}_hostname
-  host_name=$(eval echo \$$host_string)
-  sed -i 's/^host_name=REPLACE/host_name='$host_name'/' vm_nodes_provision.sh
-  ##dns server should be the foreman server
-  sed -i 's/^dns_server=REPLACE/dns_server='${interface_ip_arr[0]}'/' vm_nodes_provision.sh
+  echo "${blue}Network detected: ${deployment_type}! ${reset}"
+
+  if route | grep default; then
+    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]}
+        public_gateway=$default_gw
+        ;;
+      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
+    #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
+
+  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}"
+      exit 1
+    fi
+  fi
+
+  if [ $no_parse ]; then
+    echo "${blue}Skipping parsing variables into settings file as no_parse flag is set${reset}"
+
+  else
+
+    echo "${blue}Gathering network parameters for Target System...this may take a few minutes${reset}"
+    ##Edit the ksgen settings appropriately
+    ##ksgen settings will be stored in /vagrant on the vagrant machine
+    ##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
+      ##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
+      echo "{blue}Single Network type is unsupported right now.  Please check your interface configuration.  Exiting. ${reset}"
+      exit 0
+
+    elif [[ "$deployment_type" == "multi_network" || "$deployment_type" == "three_network" ]]; then
+
+      if [ "$deployment_type" == "three_network" ]; then
+        sed -i 's/^.*network_type:.*$/network_type: three_network/' opnfv_ksgen_settings.yml
+      fi
+
+      sed -i 's/^.*deployment_type:.*$/  deployment_type: '"$deployment_type"'/' opnfv_ksgen_settings.yml
+
+      ##get ip addresses for private network on controllers to make dhcp entries
+      ##required for controllers_ip_array global param
+      next_private_ip=${interface_ip_arr[1]}
+      type=_private
+      for node in controller1 controller2 controller3; do
+        next_private_ip=$(next_usable_ip $next_private_ip)
+        if [ ! "$next_private_ip" ]; then
+           printf '%s\n' 'deploy.sh: Unable to find next ip for private network for control nodes' >&2
+           exit 1
+        fi
+        sed -i 's/'"$node$type"'/'"$next_private_ip"'/g' opnfv_ksgen_settings.yml
+        controller_ip_array=$controller_ip_array$next_private_ip,
+      done
+
+      ##replace global param for contollers_ip_array
+      controller_ip_array=${controller_ip_array%?}
+      sed -i 's/^.*controllers_ip_array:.*$/  controllers_ip_array: '"$controller_ip_array"'/' opnfv_ksgen_settings.yml
+
+      ##now replace all the VIP variables.  admin//private can be the same IP
+      ##we have to use IP's here that won't be allocated to hosts at provisioning time
+      ##therefore we increment the ip by 10 to make sure we have a safe buffer
+      next_private_ip=$(increment_ip $next_private_ip 10)
+
+      grep -E '*private_vip|loadbalancer_vip|db_vip|amqp_vip|*admin_vip' opnfv_ksgen_settings.yml | while read -r line ; do
+        sed -i 's/^.*'"$line"'.*$/  '"$line $next_private_ip"'/' opnfv_ksgen_settings.yml
+        next_private_ip=$(next_usable_ip $next_private_ip)
+        if [ ! "$next_private_ip" ]; then
+          printf '%s\n' 'deploy.sh: Unable to find next ip for private network for vip replacement' >&2
+          exit 1
+          fi
+      done
+
+      ##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
+        next_public_ip=$(next_usable_ip $next_public_ip)
+        if [ ! "$next_public_ip" ]; then
+          printf '%s\n' 'deploy.sh: Unable to find next ip for public network for vip replcement' >&2
+          exit 1
+        fi
+      done
+
+      ##replace public_network param
+      public_subnet=$(find_subnet $next_public_ip $public_subnet_mask)
+      sed -i 's/^.*public_network:.*$/  public_network:'" $public_subnet"'/' opnfv_ksgen_settings.yml
+      ##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 public_subnet param
+      public_subnet=$public_subnet'\'$public_short_subnet_mask
+      sed -i 's/^.*public_subnet:.*$/  public_subnet:'" $public_subnet"'/' opnfv_ksgen_settings.yml
+      ##replace private_subnet param
+      private_subnet=$private_subnet'\'$private_short_subnet_mask
+      sed -i 's/^.*private_subnet:.*$/  private_subnet:'" $private_subnet"'/' opnfv_ksgen_settings.yml
+
+      ##replace public_dns param to be foreman server
+      sed -i 's/^.*public_dns:.*$/  public_dns: '${interface_ip_arr[2]}'/' opnfv_ksgen_settings.yml
+
+      ##replace public_gateway
+      if [ -z "$public_gateway" ]; then
+        ##if unset then we assume its the first IP in the public subnet
+        public_subnet=$(find_subnet $next_public_ip $public_subnet_mask)
+        public_gateway=$(increment_subnet $public_subnet 1)
+      fi
+      sed -i 's/^.*public_gateway:.*$/  public_gateway:'" $public_gateway"'/' opnfv_ksgen_settings.yml
+
+      ##we have to define an allocation range of the public subnet to give
+      ##to neutron to use as floating IPs
+      ##we should control this subnet, so this range should work .150-200
+      ##but generally this is a bad idea and we are assuming at least a /24 subnet here
+      public_subnet=$(find_subnet $next_public_ip $public_subnet_mask)
+      public_allocation_start=$(increment_subnet $public_subnet 150)
+      public_allocation_end=$(increment_subnet $public_subnet 200)
+
+      sed -i 's/^.*public_allocation_start:.*$/  public_allocation_start:'" $public_allocation_start"'/' opnfv_ksgen_settings.yml
+      sed -i 's/^.*public_allocation_end:.*$/  public_allocation_end:'" $public_allocation_end"'/' opnfv_ksgen_settings.yml
+
+    else
+      printf '%s\n' 'deploy.sh: Unknown network type: $deployment_type' >&2
+      exit 1
+    fi
 
-  ## remove bootstrap and NAT provisioning
-  sed -i '/nat_setup.sh/d' Vagrantfile
-  sed -i 's/bootstrap.sh/vm_nodes_provision.sh/' Vagrantfile
+    echo "${blue}Parameters Complete.  Settings have been set for Foreman. ${reset}"
 
-  ## modify default_gw to be node_default_gw
-  sed -i 's/^.*default_gw =.*$/  default_gw = '\""$node_default_gw"\"'/' Vagrantfile
+  fi
+}
 
-  ## modify VM memory to be 4gig
-  sed -i 's/^.*vb.memory =.*$/     vb.memory = 4096/' Vagrantfile
+##Configure bootstrap.sh to use the virtual Khaleesi playbook
+##params: none
+##usage: configure_virtual()
+configure_virtual() {
+  if [ $virtual ]; then
+    echo "${blue} Virtual flag detected, setting Khaleesi playbook to be opnfv-vm.yml ${reset}"
+    sed -i 's/opnfv.yml/opnfv-vm.yml/' bootstrap.sh
+  fi
+}
 
-  echo "${blue}Starting Vagrant Node $node! ${reset}"
+##Starts for forement VM with Vagrant
+##params: none
+##usage: start_vagrant()
+start_foreman() {
+  echo "${blue}Starting Vagrant! ${reset}"
 
   ##stand up vagrant
   if ! vagrant up; then
-    echo "${red} Unable to start $node ${reset}"
+    printf '%s\n' 'deploy.sh: Unable to start vagrant' >&2
     exit 1
   else
-    echo "${blue} $node VM is up! ${reset}"
+    echo "${blue}Foreman VM is up! ${reset}"
   fi
+}
 
-done
+##start the VM if this is a virtual installaion
+##this function does nothing if baremetal servers are being used
+##params: none
+##usage: start_virtual_nodes()
+start_virutal_nodes() {
+  if [ $virtual ]; then
+
+    ##Bring up VM nodes
+    echo "${blue}Setting VMs up... ${reset}"
+    nodes=`sed -nr '/nodes:/{:start /workaround/!{N;b start};//p}' opnfv_ksgen_settings.yml | sed -n '/^  [A-Za-z0-9]\+:$/p' | sed 's/\s*//g' | sed 's/://g'`
+    ##due to ODL Helium bug of OVS connecting to ODL too early, we need controllers to install first
+    ##this is fix kind of assumes more than I would like to, but for now it should be OK as we always have
+    ##3 static controllers
+    compute_nodes=`echo $nodes | tr " " "\n" | grep -v controller | tr "\n" " "`
+    controller_nodes=`echo $nodes | tr " " "\n" | grep controller | tr "\n" " "`
+    nodes=${controller_nodes}${compute_nodes}
+
+    for node in ${nodes}; do
+      cd /tmp
+
+      ##remove VM nodes incase it wasn't cleaned up
+      rm -rf /tmp/$node
+
+      ##clone bgs vagrant
+      ##will change this to be opnfv repo when commit is done
+      if ! git clone -b v1.0 https://github.com/trozet/bgs_vagrant.git $node; then
+        printf '%s\n' 'deploy.sh: Unable to clone vagrant repo' >&2
+        exit 1
+      fi
+
+      cd $node
+
+      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}"
+          exit 1
+        fi
+      fi
+
+      ##parse yaml into variables
+      eval $(parse_yaml opnfv_ksgen_settings.yml "config_")
+      ##find node type
+      node_type=config_nodes_${node}_type
+      node_type=$(eval echo \$$node_type)
+
+      ##find number of interfaces with ip and substitute in VagrantFile
+      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
+        exit 1
+      fi
+
+
+      if_counter=0
+      for interface in ${output}; do
+
+        if [ "$if_counter" -ge 4 ]; then
+          break
+        fi
+        interface_ip=$(find_ip $interface)
+        if [ ! "$interface_ip" ]; then
+          continue
+        fi
+        case "${if_counter}" in
+          0)
+            mac_string=config_nodes_${node}_mac_address
+            mac_addr=$(eval echo \$$mac_string)
+            mac_addr=$(echo $mac_addr | sed 's/:\|-//g')
+            if [ $mac_addr == "" ]; then
+              echo "${red} Unable to find mac_address for $node! ${reset}"
+              exit 1
+            fi
+            ;;
+          1)
+            if [ "$node_type" == "controller" ]; then
+              mac_string=config_nodes_${node}_private_mac
+              mac_addr=$(eval echo \$$mac_string)
+              if [ $mac_addr == "" ]; then
+                echo "${red} Unable to find private_mac for $node! ${reset}"
+                exit 1
+              fi
+            else
+              ##generate random mac
+              mac_addr=$(echo -n 00-60-2F; dd bs=1 count=3 if=/dev/random 2>/dev/null |hexdump -v -e '/1 "-%02X"')
+            fi
+            mac_addr=$(echo $mac_addr | sed 's/:\|-//g')
+            ;;
+          *)
+            mac_addr=$(echo -n 00-60-2F; dd bs=1 count=3 if=/dev/random 2>/dev/null |hexdump -v -e '/1 "-%02X"')
+            mac_addr=$(echo $mac_addr | sed 's/:\|-//g')
+            ;;
+        esac
+          sed -i 's/^.*eth_replace'"$if_counter"'.*$/  config.vm.network "public_network", bridge: '\'"$interface"\'', :mac => '\""$mac_addr"\"'/' Vagrantfile
+          ((if_counter++))
+      done
+
+      ##now remove interface config in Vagrantfile for 1 node
+      ##if 1, 3, or 4 interfaces set deployment type
+      ##if 2 interfaces remove 2nd interface and set deployment type
+      if [ "$if_counter" == 1 ]; then
+        deployment_type="single_network"
+        remove_vagrant_network eth_replace1
+        remove_vagrant_network eth_replace2
+        remove_vagrant_network eth_replace3
+      elif [ "$if_counter" == 2 ]; then
+        deployment_type="single_network"
+        second_interface=`echo $output | awk '{print $2}'`
+        remove_vagrant_network $second_interface
+        remove_vagrant_network eth_replace2
+      elif [ "$if_counter" == 3 ]; then
+        deployment_type="three_network"
+        remove_vagrant_network eth_replace3
+      else
+        deployment_type="multi_network"
+      fi
+
+      ##modify provisioning to do puppet install, config, and foreman check-in
+      ##substitute host_name and dns_server in the provisioning script
+      host_string=config_nodes_${node}_hostname
+      host_name=$(eval echo \$$host_string)
+      sed -i 's/^host_name=REPLACE/host_name='$host_name'/' vm_nodes_provision.sh
+      ##dns server should be the foreman server
+      sed -i 's/^dns_server=REPLACE/dns_server='${interface_ip_arr[0]}'/' vm_nodes_provision.sh
+
+      ## remove bootstrap and NAT provisioning
+      sed -i '/nat_setup.sh/d' Vagrantfile
+      sed -i 's/bootstrap.sh/vm_nodes_provision.sh/' Vagrantfile
+
+      ## modify default_gw to be node_default_gw
+      sed -i 's/^.*default_gw =.*$/  default_gw = '\""$node_default_gw"\"'/' Vagrantfile
+
+      ## modify VM memory to be 4gig
+      sed -i 's/^.*vb.memory =.*$/     vb.memory = 4096/' Vagrantfile
+
+      echo "${blue}Starting Vagrant Node $node! ${reset}"
+
+      ##stand up vagrant
+      if ! vagrant up; then
+        echo "${red} Unable to start $node ${reset}"
+        exit 1
+      else
+        echo "${blue} $node VM is up! ${reset}"
+      fi
+
+    done
+
+    echo "${blue} All VMs are UP! ${reset}"
+
+  fi
+}
 
- echo "${blue} All VMs are UP! ${reset}"
+##END FUNCTIONS
+
+main() {
+  parse_cmdline "$@"
+  disable_selinux
+  install_EPEL
+  install_vbox
+  install_ansible
+  install_vagrant
+  clean_tmp
+  clone_bgs
+  configure_network
+  configure_virtual
+  start_foreman
+  start_virutal_nodes
+}
 
-fi
+main "$@"