Adds check to ensure 3 control nodes are defined with HA
[genesis.git] / foreman / ci / deploy.sh
index edad2a4..5746e10 100755 (executable)
@@ -28,6 +28,8 @@ declare -A interface_arr
 declare -A controllers_ip_arr
 declare -A admin_ip_arr
 declare -A public_ip_arr
+
+vm_dir=/var/opt/opnfv
 ##END VARS
 
 ##FUNCTIONS
@@ -44,6 +46,24 @@ display_usage() {
   echo -e "\n   -floating_ip_count : number of IP address from the public range to be used for floating IP. Default is 20.\n"
 }
 
+##verify vm dir exists
+##params: none
+function verify_vm_dir {
+  if [ -d "$vm_dir" ]; then
+    echo -e "\n\n${red}ERROR: VM Directory: $vm_dir already exists.  Environment not clean.  Please use clean.sh.  Exiting${reset}\n\n"
+    exit 1
+  else
+    mkdir -p $vm_dir
+  fi
+
+  chmod 700 $vm_dir
+
+  if [ ! -d $vm_dir ]; then
+    echo -e "\n\n${red}ERROR: Unable to create VM Directory: $vm_dir  Exiting${reset}\n\n"
+    exit -1
+  fi
+}
+
 ##find ip of interface
 ##params: interface name
 function find_ip {
@@ -58,6 +78,31 @@ function find_subnet {
   printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))"
 }
 
+##verify subnet has at least n IPs
+##params: subnet mask, n IPs
+function verify_subnet_size {
+  IFS=. read -r i1 i2 i3 i4 <<< "$1"
+  num_ips_required=$2
+
+  ##this function assumes you would never need more than 254
+  ##we check here to make sure
+  if [ "$num_ips_required" -ge 254 ]; then
+    echo -e "\n\n${red}ERROR: allocating more than 254 IPs is unsupported...Exiting${reset}\n\n"
+    return 1
+  fi
+
+  ##we just return if 3rd octet is not 255
+  ##because we know the subnet is big enough
+  if [ "$i3" -ne 255 ]; then
+    return 0
+  elif [ $((254-$i4)) -ge "$num_ips_required" ]; then
+    return 0
+  else
+    echo -e "\n\n${red}ERROR: Subnet is too small${reset}\n\n"
+    return 1
+  fi
+}
+
 ##finds last usable ip (broadcast minus 1) of a subnet from an IP and netmask
 ## Warning: This function only works for IPv4 at the moment.
 ##params: ip, netmask
@@ -104,6 +149,19 @@ function next_ip {
   echo $baseaddr.$lsv
 }
 
+##subtracts a value from an IP address
+##params: last ip, ip_count
+##assumes ip_count is less than the last octect of the address
+subtract_ip() {
+  IFS=. read -r i1 i2 i3 i4 <<< "$1"
+  ip_count=$2
+  if [ $i4 -lt $ip_count ]; then
+    echo -e "\n\n${red}ERROR: Can't subtract $ip_count from IP address $1  Exiting${reset}\n\n"
+    exit 1
+  fi
+  printf "%d.%d.%d.%d\n" "$i1" "$i2" "$i3" "$((i4 - $ip_count ))"
+}
+
 ##removes the network interface config from Vagrantfile
 ##params: interface
 ##assumes you are in the directory of Vagrantfile
@@ -353,27 +411,31 @@ install_vagrant() {
 ##params: none
 ##usage: clean_tmp()
 clean_tmp() {
-  rm -rf /tmp/bgs_vagrant
+  rm -rf $vm_dir/foreman_vm
 }
 
-##clone bgs vagrant version 1.0 using git
+##clone genesis and move to node vm dir
 ##params: none
 ##usage: clone_bgs
 clone_bgs() {
   cd /tmp/
+  rm -rf /tmp/genesis/
 
-  ##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
+  ##clone artifacts and move into foreman_vm dir
+  if ! GIT_SSL_NO_VERIFY=true git clone https://gerrit.opnfv.org/gerrit/genesis.git; then
+    printf '%s\n' 'deploy.sh: Unable to clone genesis repo' >&2
     exit 1
   fi
+
+  mv -f /tmp/genesis/foreman/ci $vm_dir/foreman_vm
+  rm -rf /tmp/genesis/
 }
 
 ##validates the network settings and update VagrantFile with network settings
 ##params: none
 ##usage: configure_network()
 configure_network() {
-  cd /tmp/bgs_vagrant
+  cd $vm_dir/foreman_vm
 
   echo "${blue}Detecting network configuration...${reset}"
   ##detect host 1 or 3 interface configuration
@@ -417,6 +479,11 @@ configure_network() {
     public_subnet_mask=$subnet_mask
     public_short_subnet_mask=$(find_short_netmask $interface)
 
+    if ! verify_subnet_size $public_subnet_mask 25; then
+      echo "${red} Not enough IPs in public subnet: $interface_ip_arr[2] ${public_subnet_mask}.  Need at least 25 IPs.  Please resize subnet! Exiting ${reset}"
+      exit 1
+    fi
+
     ##set that interface to be public
     sed -i 's/^.*eth_replace2.*$/  config.vm.network "public_network", ip: '\""$new_ip"\"', bridge: '\'"$interface"\'', netmask: '\""$subnet_mask"\"'/' Vagrantfile
     if_counter=1
@@ -441,14 +508,34 @@ configure_network() {
       subnet_mask=$(find_netmask $interface)
       if [ "$if_counter" -eq 0 ]; then
         admin_subnet_mask=$subnet_mask
+        if ! verify_subnet_size $admin_subnet_mask 5; then
+          echo "${red} Not enough IPs in admin subnet: ${interface_ip_arr[$if_counter]} ${admin_subnet_mask}.  Need at least 5 IPs.  Please resize subnet! Exiting ${reset}"
+          exit 1
+        fi
+
       elif [ "$if_counter" -eq 1 ]; then
         private_subnet_mask=$subnet_mask
         private_short_subnet_mask=$(find_short_netmask $interface)
+
+        if ! verify_subnet_size $private_subnet_mask 15; then
+          echo "${red} Not enough IPs in private subnet: ${interface_ip_arr[$if_counter]} ${private_subnet_mask}.  Need at least 15 IPs.  Please resize subnet! Exiting ${reset}"
+          exit 1
+        fi
       elif [ "$if_counter" -eq 2 ]; then
         public_subnet_mask=$subnet_mask
         public_short_subnet_mask=$(find_short_netmask $interface)
+
+        if ! verify_subnet_size $public_subnet_mask 25; then
+          echo "${red} Not enough IPs in public subnet: ${interface_ip_arr[$if_counter]} ${public_subnet_mask}.  Need at least 25 IPs.  Please resize subnet! Exiting ${reset}"
+          exit 1
+        fi
       elif [ "$if_counter" -eq 3 ]; then
         storage_subnet_mask=$subnet_mask
+
+        if ! verify_subnet_size $storage_subnet_mask 10; then
+          echo "${red} Not enough IPs in storage subnet: ${interface_ip_arr[$if_counter]} ${storage_subnet_mask}.  Need at least 10 IPs.  Please resize subnet! Exiting ${reset}"
+          exit 1
+        fi
       else
         echo "${red}ERROR: interface counter outside valid range of 0 to 3: $if_counter ! ${reset}"
         exit 1
@@ -563,6 +650,20 @@ configure_network() {
     fi
   fi
 
+  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'`
+  controller_nodes=`echo $nodes | tr " " "\n" | grep controller | tr "\n" " "`
+  echo "${blue}Controller nodes found in settings: ${controller_nodes}${reset}"
+  my_controller_array=( $controller_nodes )
+  num_control_nodes=${#my_controller_array[@]}
+  if [ "$num_control_nodes" -ne 3 ]; then
+    if cat opnfv_ksgen_settings.yml | grep ha_flag | grep true; then
+      echo "${red}Error: You must define exactly 3 control nodes when HA flag is true!${reset}"
+      exit 1
+    fi
+  else
+    echo "${blue}Number of Controller nodes detected: ${num_control_nodes}${reset}"
+  fi
+
   if [ $no_parse ]; then
     echo "${blue}Skipping parsing variables into settings file as no_parse flag is set${reset}"
 
@@ -679,11 +780,11 @@ configure_network() {
       ##replace foreman site
       sed -i 's/^.*foreman_url:.*$/  foreman_url:'" https:\/\/$foreman_ip"'\/api\/v2\//' opnfv_ksgen_settings.yml
       ##replace public vips
-      ##no need to do this if virtual and no dhcp
-      if [ ! -z "$enable_virtual_dhcp" ]; then
-        next_public_ip=$(increment_ip $next_public_ip 10)
-      else
+      ##no need to do this if no dhcp
+      if [[ -z "$enable_virtual_dhcp" && ! -z "$virtual" ]]; then
         next_public_ip=$(next_usable_ip $next_public_ip)
+      else
+        next_public_ip=$(increment_ip $next_public_ip 10)
       fi
 
       public_output=$(grep -E '*public_vip' opnfv_ksgen_settings.yml)
@@ -751,15 +852,13 @@ configure_network() {
         else
           public_allocation_start=$(next_ip $next_public_ip)
           public_allocation_end=$static_ip_range_end
-          echo "${blue}Neutron Floating IP range: $public_allocation_start to $public_allocation_end ${reset}"
         fi
       else
         last_ip_subnet=$(find_last_ip_subnet $next_public_ip $public_subnet_mask)
-        public_allocation_start=$(increment_subnet $public_subnet $(( $last_ip_subnet - $floating_ip_count )) )
-        public_allocation_end=$(increment_subnet $public_subnet $(( $last_ip_subnet )) )
-        echo "${blue}Neutron Floating IP range: $public_allocation_start to $public_allocation_end ${reset}"
-        echo "${blue}Foreman VM is up! ${reset}"
+        public_allocation_start=$(subtract_ip $last_ip_subnet $floating_ip_count )
+        public_allocation_end=${last_ip_subnet}
       fi
+      echo "${blue}Neutron Floating IP range: $public_allocation_start to $public_allocation_end ${reset}"
 
       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
@@ -792,7 +891,7 @@ start_foreman() {
 
   ##stand up vagrant
   if ! vagrant up; then
-    printf '%s\n' 'deploy.sh: Unable to start vagrant' >&2
+    printf '%s\n' 'deploy.sh: Unable to complete Foreman VM install' >&2
     exit 1
   else
     echo "${blue}Foreman VM is up! ${reset}"
@@ -819,19 +918,22 @@ start_virtual_nodes() {
     compute_wait_completed=false
 
     for node in ${nodes}; do
-      cd /tmp
+      cd /tmp/
 
       ##remove VM nodes incase it wasn't cleaned up
-      rm -rf /tmp/$node
+      rm -rf $vm_dir/$node
+      rm -rf /tmp/genesis/
 
-      ##clone bgs vagrant
-      ##will change this to be opnfv repo when commit is done
-      if ! git clone https://github.com/trozet/bgs_vagrant.git $node; then
+      ##clone genesis and move into node folder
+      if ! GIT_SSL_NO_VERIFY=true git clone https://gerrit.opnfv.org/gerrit/genesis.git; then
         printf '%s\n' 'deploy.sh: Unable to clone vagrant repo' >&2
         exit 1
       fi
 
-      cd $node
+      mv -f /tmp/genesis/foreman/ci $vm_dir/$node
+      rm -rf /tmp/genesis/
+
+      cd $vm_dir/$node
 
       if [ $base_config ]; then
         if ! cp -f $base_config opnfv_ksgen_settings.yml; then
@@ -994,7 +1096,7 @@ start_virtual_nodes() {
     echo "${blue} Waiting for puppet to complete on the nodes... ${reset}"
     ##check puppet is complete
     ##ssh into foreman server, run check to verify puppet is complete
-    pushd /tmp/bgs_vagrant
+    pushd $vm_dir/foreman_vm
     if ! vagrant ssh -c "/opt/khaleesi/run.sh --no-logs --use /vagrant/opnfv_ksgen_settings.yml /opt/khaleesi/playbooks/validate_opnfv-vm.yml"; then
       echo "${red} Failed to validate puppet completion on nodes ${reset}"
       exit 1
@@ -1004,7 +1106,7 @@ start_virtual_nodes() {
     popd
     ##add routes back to nodes
     for node in ${nodes}; do
-      pushd /tmp/$node
+      pushd $vm_dir/$node
       if ! vagrant ssh -c "route | grep default | grep $this_default_gw"; then
         echo "${blue} Adding public route back to $node! ${reset}"
         vagrant ssh -c "route add default gw $this_default_gw"
@@ -1029,6 +1131,7 @@ main() {
   install_ansible
   install_vagrant
   clean_tmp
+  verify_vm_dir
   clone_bgs
   configure_network
   configure_virtual