Adds baremetal node power check before attempting deploy
[genesis.git] / foreman / ci / deploy.sh
index a607350..344356c 100755 (executable)
@@ -78,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
@@ -124,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
@@ -441,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
@@ -465,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
@@ -703,11 +766,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)
@@ -775,15 +838,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
@@ -1046,11 +1107,79 @@ start_virtual_nodes() {
   fi
 }
 
+##check to make sure nodes are powered off
+##this function does nothing if virtual
+##params: none
+##usage: check_baremetal_nodes()
+check_baremetal_nodes() {
+  if [ $virtual ]; then
+    echo "${blue}Skipping Baremetal node power status check as deployment is virtual ${reset}"
+  else
+    echo "${blue}Checking Baremetal nodes power state... ${reset}"
+    if [ ! -z "$base_config" ]; then
+      # Install ipmitool
+      # Major version is pinned to force some consistency for Arno
+      if ! yum list installed | grep -i ipmitool; then
+        echo "${blue}Installing ipmitool...${reset}"
+        if ! yum -y install ipmitool-1*; then
+          echo "${red}Failed to install ipmitool!${reset}"
+          exit 1
+        fi
+      fi
+
+        ###find all the bmc IPs and number of nodes
+      node_counter=0
+      output=`grep bmc_ip $base_config | grep -Eo '[0-9]+.[0-9]+.[0-9]+.[0-9]+'`
+      for line in ${output} ; do
+        bmc_ip[$node_counter]=$line
+        ((node_counter++))
+      done
+
+      max_nodes=$((node_counter-1))
+
+      ###find bmc_users per node
+      node_counter=0
+      output=`grep bmc_user $base_config | sed 's/\s*bmc_user:\s*//'`
+      for line in ${output} ; do
+        bmc_user[$node_counter]=$line
+        ((node_counter++))
+      done
+
+      ###find bmc_pass per node
+      node_counter=0
+      output=`grep bmc_pass $base_config | sed 's/\s*bmc_pass:\s*//'`
+      for line in ${output} ; do
+        bmc_pass[$node_counter]=$line
+        ((node_counter++))
+      done
+
+      for mynode in `seq 0 $max_nodes`; do
+        echo "${blue}Node: ${bmc_ip[$mynode]} ${bmc_user[$mynode]} ${bmc_pass[$mynode]} ${reset}"
+        ipmi_output=`ipmitool -I lanplus -P ${bmc_pass[$mynode]} -U ${bmc_user[$mynode]} -H ${bmc_ip[$mynode]} chassis status \
+                    | grep "System Power" | cut -d ':' -f2 | tr -d [:blank:]`
+        if [ "$ipmi_output" == "on" ]; then
+          echo "${red}Error: Node is powered on: ${bmc_ip[$mynode]} ${reset}"
+          echo "${red}Please run clean.sh before running deploy! ${reset}"
+          exit 1
+        elif [ "$ipmi_output" == "off" ]; then
+          echo "${blue}Node: ${bmc_ip[$mynode]} is powered off${reset}"
+        else
+          echo "${red}Warning: Unable to detect node power state: ${bmc_ip[$mynode]} ${reset}"
+        fi
+      done
+    else
+      echo "${red}base_config was not provided for a baremetal install! Exiting${reset}"
+      exit 1
+    fi
+  fi
+}
+
 ##END FUNCTIONS
 
 main() {
   parse_cmdline "$@"
   disable_selinux
+  check_baremetal_nodes
   install_EPEL
   install_vbox
   install_ansible