Allows domain name to be configured
[genesis.git] / foreman / ci / deploy.sh
index 862077e..83d1b81 100755 (executable)
@@ -29,7 +29,10 @@ declare -A controllers_ip_arr
 declare -A admin_ip_arr
 declare -A public_ip_arr
 
+vagrant_box_dir=~/.vagrant.d/boxes/opnfv-VAGRANTSLASH-centos-7.0/1.0.0/virtualbox/
+vagrant_box_vmdk=box-disk1.vmdk
 vm_dir=/var/opt/opnfv
+script=`realpath $0`
 ##END VARS
 
 ##FUNCTIONS
@@ -324,7 +327,7 @@ parse_cmdline() {
   fi
 
   ##Validate nic args
-  if [ $nic_arg_flag -eq 1 ]; then
+  if [[ $nic_arg_flag -eq 1 ]]; then
     if [ -z "$virtual" ]; then
       for nic_type in admin_nic private_nic public_nic; do
         eval "nic_value=\$$nic_type"
@@ -448,8 +451,8 @@ install_vagrant() {
   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
+  if ! vagrant box list | grep opnfv/centos-7.0; then
+    if ! vagrant box add opnfv/centos-7.0 --provider virtualbox; then
       printf '%s\n' 'deploy.sh: Unable to download centos7 box for Vagrant' >&2
       exit 1
     fi
@@ -476,20 +479,12 @@ clean_tmp() {
 }
 
 ##clone genesis and move to node vm dir
-##params: none
-##usage: clone_bgs
+##params: destination directory
+##usage: clone_bgs /tmp/myvm/
 clone_bgs() {
-  cd /tmp/
-  rm -rf /tmp/genesis/
-
-  ##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/
+  script_dir="`dirname "$script"`"
+  cp -fr $script_dir/ $1
+  cp -fr $script_dir/../../common/puppet-opnfv $1
 }
 
 ##validates the network settings and update VagrantFile with network settings
@@ -500,7 +495,7 @@ configure_network() {
 
   ##if nic_arg_flag is set, then we don't figure out
   ##NICs dynamically
-  if [ $nic_arg_flag -eq 1 ]; then
+  if [[ $nic_arg_flag -eq 1 ]]; then
     echo "${blue}Static Network Interfaces Defined.  Updating Vagrantfile...${reset}"
     if [ $virtual ]; then
       nic_list="$public_nic"
@@ -516,7 +511,8 @@ configure_network() {
     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/://'`
+    #output=`/bin/ls -l /sys/class/net | tail -n +2 | grep -v virtual | cut -d " " -f10`
+    output=`/bin/ls -l /sys/class/net | tail -n +2 | grep -v virtual | awk {'print $9'}`
   fi
 
   if [ ! "$output" ]; then
@@ -546,6 +542,13 @@ configure_network() {
     ##set variable info
     if [ ! -z "$static_ip_range" ]; then
       new_ip=$(echo $static_ip_range | cut -d , -f1)
+      subnet_mask=$(find_netmask $this_default_gw_interface)
+      host_subnet=$(find_subnet $interface_ip $subnet_mask)
+      ip_range_subnet=$(find_subnet $new_ip $subnet_mask)
+      if [ "$ip_range_subnet" != "$host_subnet" ]; then
+        echo "${red}static_ip_range: ${static_ip_range} is not in the same subnet as your default gateway interface: ${host_subnet}.  Please use a correct range!${reset}"
+        exit 1
+      fi
     else
       new_ip=$(next_usable_ip $interface_ip)
       if [ ! "$new_ip" ]; then
@@ -732,6 +735,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}"
 
@@ -986,20 +1003,13 @@ start_virtual_nodes() {
     compute_wait_completed=false
 
     for node in ${nodes}; do
-      cd /tmp/
 
       ##remove VM nodes incase it wasn't cleaned up
       rm -rf $vm_dir/$node
       rm -rf /tmp/genesis/
 
       ##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
-
-      mv -f /tmp/genesis/foreman/ci $vm_dir/$node
-      rm -rf /tmp/genesis/
+      clone_bgs $vm_dir/$node
 
       cd $vm_dir/$node
 
@@ -1016,6 +1026,19 @@ start_virtual_nodes() {
       node_type=config_nodes_${node}_type
       node_type=$(eval echo \$$node_type)
 
+      ##modify memory and cpu
+      node_memory=$(eval echo \${config_nodes_${node}_memory})
+      node_vcpus=$(eval echo \${config_nodes_${node}_cpus})
+      node_storage=$(eval echo \${config_nodes_${node}_disk})
+
+      sed -i 's/^.*vb.memory =.*$/     vb.memory = '"$node_memory"'/' Vagrantfile
+      sed -i 's/^.*vb.cpus =.*$/     vb.cpus = '"$node_vcpus"'/' Vagrantfile
+
+      if ! resize_vagrant_disk $node_storage; then
+        echo "${red}Error while resizing vagrant box to size $node_storage for $node! ${reset}"
+        exit 1
+      fi
+
       ##trozet test make compute nodes wait 20 minutes
       if [ "$compute_wait_completed" = false ] && [ "$node_type" != "controller" ]; then
         echo "${blue}Waiting 20 minutes for Control nodes to install before continuing with Compute nodes..."
@@ -1084,8 +1107,12 @@ start_virtual_nodes() {
 
       ##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)
+      host_string=config_nodes_${node}_short_name
+      short_host_name=$(eval echo \$$host_string)
+      ##substitute domain_name
+      domain_name=$config_domain_name
+      sed -i 's/^domain_name=REPLACE/domain_name='$domain_name'/' vm_nodes_provision.sh
+      host_name=${short_host_name}.${domain_name}
       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
@@ -1094,11 +1121,6 @@ start_virtual_nodes() {
       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
-      ##if node type is controller
-      if [ "$node_type" == "controller" ]; then
-        sed -i 's/^.*vb.memory =.*$/     vb.memory = 4096/' Vagrantfile
-      fi
       echo "${blue}Starting Vagrant Node $node! ${reset}"
       ##stand up vagrant
       if ! vagrant up; then
@@ -1126,29 +1148,183 @@ start_virtual_nodes() {
       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"
+        vagrant ssh -c "route delete default gw 10.0.2.2"
       fi
       popd
     done
     if [ ! -z "$horizon_public_vip" ]; then
       echo "${blue} Virtual deployment SUCCESS!! Foreman URL:  http://${foreman_ip}, Horizon URL: http://${horizon_public_vip} ${reset}"
     else
-      echo "${blue} Virtual deployment SUCCESS!! Foreman URL:  http://${foreman_ip}, Horizon URL: http://${odl_control_ip} ${reset}"
+      ##Find public IP of controller
+      for node in ${nodes}; do
+        node_type=config_nodes_${node}_type
+        node_type=$(eval echo \$$node_type)
+        if [ "$node_type" == "controller" ]; then
+          pushd $vm_dir/$node
+          horizon_ip=`vagrant ssh -c "ifconfig enp0s10" | grep -Eo "inet [0-9\.]+" | awk {'print $2'}`
+          popd
+          break
+        fi
+      done
+      if [ -z "$horizon_ip" ]; then
+        echo "${red}Warn: Unable to determine horizon IP, please login to your controller node to find it${reset}"
+      fi
+      echo "${blue} Virtual deployment SUCCESS!! Foreman URL:  http://${foreman_ip}, Horizon URL: http://${horizon_ip} ${reset}"
     fi
   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
+}
+
+##resizes vagrant disk (cannot shrink)
+##params: size in GB
+##usage: resize_vagrant_disk 100
+resize_vagrant_disk() {
+  if [[ "$1" < 40 ]]; then
+    echo "${blue}Warn: Requested disk size cannot be less than 40, using 40 as new size${reset}"
+    new_size_gb=40
+  else
+    new_size_gb=$1
+  fi
+
+  if ! vagrant box list | grep opnfv; then
+    vagrant box remove -f opnfv/centos-7.0
+    if ! vagrant box add opnfv/centos-7.0 --provider virtualbox; then
+      echo "${red}Unable to reclone vagrant box! Exiting...${reset}"
+      exit 1
+    fi
+  fi
+
+  pushd $vagrant_box_dir
+
+  # Close medium to make sure we can modify it
+  vboxmanage closemedium disk $vagrant_box_vmdk
+
+  cur_size=$(vboxmanage showhdinfo $vagrant_box_vmdk | grep -i capacity | grep -Eo [0-9]+)
+  cur_size_gb=$((cur_size / 1024))
+
+  if [ "$cur_size_gb" -eq "$new_size_gb" ]; then
+    echo "${blue}Info: Disk size already ${cur_size_gb} ${reset}"
+    popd
+    return
+  elif [[ "$new_size_gb" < "$cur_size_gb" ]] ; then
+    echo "${blue}Info: Requested disk is less than ${cur_size_gb} ${reset}"
+    echo "${blue}Re-adding vagrant box${reset}"
+    if vagrant box list | grep opnfv; then
+      popd
+      vagrant box remove -f opnfv/centos-7.0
+      if ! vagrant box add opnfv/centos-7.0 --provider virtualbox; then
+        echo "${red}Unable to reclone vagrant box! Exiting...${reset}"
+        exit 1
+      fi
+      pushd $vagrant_box_dir
+    fi
+  fi
+
+  new_size=$((new_size_gb * 1024))
+  if ! vboxmanage clonehd $vagrant_box_vmdk tmp-disk.vdi --format vdi; then
+    echo "${red}Error: Unable to clone ${vagrant_box_vmdk}${reset}"
+    popd
+    return 1
+  fi
+
+  if ! vboxmanage modifyhd tmp-disk.vdi --resize $new_size; then
+    echo "${red}Error: Unable modify tmp-disk.vdi to ${new_size}${reset}"
+    popd
+    return 1
+  fi
+
+  if  ! vboxmanage clonehd tmp-disk.vdi resized-disk.vmdk --format vmdk; then
+    echo "${red}Error: Unable clone tmp-disk.vdi to vmdk${reset}"
+    popd
+    return 1
+  fi
+
+  vboxmanage closemedium disk tmp-disk.vdi --delete
+  rm -f tmp-disk.vdi $vagrant_box_vmdk
+  cp -f resized-disk.vmdk $vagrant_box_vmdk
+  vboxmanage closemedium disk resized-disk.vmdk --delete
+  popd
+}
+
 ##END FUNCTIONS
 
 main() {
   parse_cmdline "$@"
   disable_selinux
+  check_baremetal_nodes
   install_EPEL
   install_vbox
   install_ansible
   install_vagrant
   clean_tmp
   verify_vm_dir
-  clone_bgs
+  clone_bgs $vm_dir/foreman_vm
   configure_network
   configure_virtual
   start_foreman