Fixes clean to remove all VMs and be more effective
[genesis.git] / foreman / ci / clean.sh
index 25352a8..1a16efd 100755 (executable)
@@ -5,7 +5,7 @@
 #
 #Uses Vagrant and VirtualBox
 #
-#Destroys Vagrant VM running in /tmp/bgs_vagrant
+#Destroys Vagrant VMs running in $vm_dir/
 #Shuts down all nodes found in Khaleesi settings
 #Removes hypervisor kernel modules (VirtualBox)
 
@@ -14,6 +14,8 @@ reset=`tput sgr0`
 blue=`tput setaf 4`
 red=`tput setaf 1`
 green=`tput setaf 2`
+
+vm_dir=/var/opt/opnfv
 ##END VARS
 
 ##FUNCTIONS
@@ -51,9 +53,10 @@ esac
 done
 
 
-##install ipmitool
+# Install ipmitool
+# Major version is pinned to force some consistency for Arno
 if ! yum list installed | grep -i ipmitool; then
-  if ! yum -y install ipmitool; then
+  if ! yum -y install ipmitool-1*; then
     echo "${red}Unable to install ipmitool!${reset}"
     exit 1
   fi
@@ -84,7 +87,7 @@ 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++)) 
+  ((node_counter++))
 done
 
 for mynode in `seq 0 $max_nodes`; do
@@ -105,35 +108,78 @@ else
   skip_vagrant=1
 fi
 
+###legacy VM location check
+###remove me later
+if [ -d /tmp/bgs_vagrant ]; then
+  cd /tmp/bgs_vagrant
+  vagrant destroy -f
+  rm -rf /tmp/bgs_vagrant
+fi
+
 ###destroy vagrant
 if [ $skip_vagrant -eq 0 ]; then
-  cd /tmp/bgs_vagrant
-  if vagrant destroy -f; then
-    echo "${blue}Successfully destroyed Foreman VM ${reset}"
+  if [ -d $vm_dir ]; then
+    ##all vm directories
+    for vm in $( ls $vm_dir ); do
+      cd $vm_dir/$vm
+      if vagrant destroy -f; then
+        echo "${blue}Successfully destroyed $vm Vagrant VM ${reset}"
+      else
+        echo "${red}Unable to destroy $vm Vagrant VM! Attempting to killall vagrant if process is hung ${reset}"
+        killall vagrant
+        echo "${blue}Checking if vagrant was already destroyed and no process is active...${reset}"
+        if ps axf | grep vagrant; then
+          echo "${red}Vagrant process still exists after kill...exiting ${reset}"
+          exit 1
+        else
+          echo "${blue}Vagrant process doesn't exist.  Moving on... ${reset}"
+        fi
+      fi
+
+      ##Vagrant boxes appear as VboxHeadless processes
+      ##try to gracefully destroy the VBox VM if it still exists
+      if vboxmanage list runningvms | grep $vm; then
+        echo "${red} $vm VBoxHeadless process still exists...Removing${reset}"
+        vbox_id=$(vboxmanage list runningvms | grep $vm | awk '{print $1}' | sed 's/"//g')
+        vboxmanage controlvm $vbox_id poweroff
+        if vboxmanage unregistervm --delete $vbox_id; then
+          echo "${blue}$vm VM is successfully deleted! ${reset}"
+        else
+          echo "${red} Unable to delete VM $vm ...Exiting ${reset}"
+          exit 1
+        fi
+      else
+        echo "${blue}$vm VM is successfully deleted! ${reset}"
+      fi
+    done
   else
-    echo "${red}Unable to destroy Foreman VM ${reset}"
-    echo "${blue}Checking if vagrant was already destroyed and no process is active...${reset}"
-    if ps axf | grep vagrant; then
-      echo "${red}Vagrant VM still exists...exiting ${reset}"
-      exit 1
-    else
-      echo "${blue}Vagrant process doesn't exist.  Moving on... ${reset}"
-    fi
+    echo "${blue}${vm_dir} doesn't exist, no VMs in OPNFV directory to destroy! ${reset}"
   fi
 
+  echo "${blue}Checking for any remaining virtual box processes...${reset}"
   ###kill virtualbox
-  echo "${blue}Killing VirtualBox ${reset}"
-  killall virtualbox
-  killall VboxHeadless
+  if ps axf | grep virtualbox; then
+    echo "${blue}virtualbox processes are still running. Killing any remaining VirtualBox processes...${reset}"
+    killall virtualbox
+  fi
+
+  ###kill any leftover VMs (brute force)
+  if ps axf | grep VBoxHeadless; then
+    echo "${blue}VBoxHeadless processes are still running. Killing any remaining VBoxHeadless processes...${reset}"
+    killall VBoxHeadless
+  fi
 
   ###remove virtualbox
-  echo "${blue}Removing VirtualBox ${reset}"
+  echo "${blue}Removing VirtualBox... ${reset}"
   yum -y remove $vboxpkg
 
 else
-  echo "${blue}Skipping Vagrant destroy + Vbox Removal as VirtualBox package is already removed ${reset}"
+  echo "${blue}Skipping Vagrant destroy + VBox Removal as VirtualBox package is already removed ${reset}"
 fi
 
+###remove working vm directory
+echo "${blue}Removing working VM directory: $vm_dir ${reset}"
+rm -rf $vm_dir
 
 ###remove kernel modules
 echo "${blue}Removing kernel modules ${reset}"