3 #Clean script to uninstall provisioning server for Foreman/QuickStack
4 #author: Tim Rozet (trozet@redhat.com)
6 #Removes Libvirt, KVM, Vagrant, VirtualBox
8 #Destroys Vagrant VMs running in $vm_dir/
9 #Shuts down all nodes found in Khaleesi settings
10 #Removes hypervisor kernel modules (VirtualBox & KVM/Libvirt)
22 echo -e "\n\n${blue}This script is used to uninstall and clean the OPNFV Target System${reset}\n\n"
23 echo -e "\nUsage:\n$0 [arguments] \n"
24 echo -e "\n -no_parse : No variable parsing into config. Flag. \n"
25 echo -e "\n -base_config : Full path of ksgen settings file to parse. Required. Will provide BMC info to shutdown hosts. Example: -base_config /opt/myinventory.yml \n"
30 if [[ ( $1 == "--help") || $1 == "-h" ]]; then
35 echo -e "\n\n${blue}This script is used to uninstall and clean the OPNFV Target System${reset}\n\n"
36 echo "Use -h to display help"
39 while [ "`echo $1 | cut -c1`" = "-" ]
54 if [ ! -z "$base_config" ]; then
56 # Major version is pinned to force some consistency for Arno
57 if ! yum list installed | grep -i ipmitool; then
58 if ! yum -y install ipmitool-1*; then
59 echo "${red}Unable to install ipmitool!${reset}"
63 echo "${blue}Skipping ipmitool as it is already installed!${reset}"
66 ###find all the bmc IPs and number of nodes
68 output=`grep bmc_ip $base_config | grep -Eo '[0-9]+.[0-9]+.[0-9]+.[0-9]+'`
69 for line in ${output} ; do
70 bmc_ip[$node_counter]=$line
74 max_nodes=$((node_counter-1))
76 ###find bmc_users per node
78 output=`grep bmc_user $base_config | sed 's/\s*bmc_user:\s*//'`
79 for line in ${output} ; do
80 bmc_user[$node_counter]=$line
84 ###find bmc_pass per node
86 output=`grep bmc_pass $base_config | sed 's/\s*bmc_pass:\s*//'`
87 for line in ${output} ; do
88 bmc_pass[$node_counter]=$line
91 for mynode in `seq 0 $max_nodes`; do
92 echo "${blue}Node: ${bmc_ip[$mynode]} ${bmc_user[$mynode]} ${bmc_pass[$mynode]} ${reset}"
93 if ipmitool -I lanplus -P ${bmc_pass[$mynode]} -U ${bmc_user[$mynode]} -H ${bmc_ip[$mynode]} chassis power off; then
94 echo "${blue}Node: $mynode, ${bmc_ip[$mynode]} powered off!${reset}"
96 echo "${red}Error: Unable to power off $mynode, ${bmc_ip[$mynode]} ${reset}"
101 echo "${blue}Skipping Baremetal node poweroff as base_config was not provided${reset}"
103 ###check to see if vbox is installed
104 vboxpkg=`rpm -qa | grep VirtualBox`
105 if [ $? -eq 0 ]; then
111 ###legacy VM location check
113 if [ -d /tmp/bgs_vagrant ]; then
116 rm -rf /tmp/bgs_vagrant
120 if [ $skip_vagrant -eq 0 ]; then
121 if [ -d $vm_dir ]; then
123 for vm in $( ls $vm_dir ); do
125 if vagrant destroy -f; then
126 echo "${blue}Successfully destroyed $vm Vagrant VM ${reset}"
128 echo "${red}Unable to destroy $vm Vagrant VM! Attempting to killall vagrant if process is hung ${reset}"
130 echo "${blue}Checking if vagrant was already destroyed and no process is active...${reset}"
131 if ps axf | grep vagrant | grep -v 'grep'; then
132 echo "${red}Vagrant process still exists after kill...exiting ${reset}"
135 echo "${blue}Vagrant process doesn't exist. Moving on... ${reset}"
139 ##Vagrant boxes appear as VboxHeadless processes
140 ##try to gracefully destroy the VBox VM if it still exists
141 if vboxmanage list runningvms | grep $vm; then
142 echo "${red} $vm VBoxHeadless process still exists...Removing${reset}"
143 vbox_id=$(vboxmanage list runningvms | grep $vm | awk '{print $1}' | sed 's/"//g')
144 vboxmanage controlvm $vbox_id poweroff
145 if vboxmanage unregistervm --delete $vbox_id; then
146 echo "${blue}$vm VM is successfully deleted! ${reset}"
148 echo "${red} Unable to delete VM $vm ...Exiting ${reset}"
152 echo "${blue}$vm VM is successfully deleted! ${reset}"
156 echo "${blue}${vm_dir} doesn't exist, no VMs in OPNFV directory to destroy! ${reset}"
159 echo "${blue}Checking for any remaining virtual box processes...${reset}"
161 if ps axf | grep virtualbox | grep -v 'grep'; then
162 echo "${blue}virtualbox processes are still running. Killing any remaining VirtualBox processes...${reset}"
166 ###kill any leftover VMs (brute force)
167 if ps axf | grep VBoxHeadless | grep -v 'grep'; then
168 echo "${blue}VBoxHeadless processes are still running. Killing any remaining VBoxHeadless processes...${reset}"
173 echo "${blue}Removing VirtualBox... ${reset}"
174 yum -y remove $vboxpkg
177 echo "${blue}Skipping Vagrant destroy + VBox Removal as VirtualBox package is already removed ${reset}"
180 ###remove working vm directory
181 echo "${blue}Removing working VM directory: $vm_dir ${reset}"
184 ###check to see if libvirt is installed
185 echo "${blue}Checking if libvirt/KVM is installed"
186 if rpm -qa | grep -iE 'libvirt|kvm'; then
187 echo "${blue}Libvirt/KVM is installed${reset}"
188 echo "${blue}Checking for any QEMU/KVM VMs...${reset}"
190 while read -r line; do ((vm_count++)); done < <(virsh list --all | sed 1,2d | head -n -1)
191 if [ $vm_count -gt 0 ]; then
192 echo "${blue}VMs Found: $vm_count${reset}"
194 while read -r line; do ((vm_running++)); done < <(virsh list --all | sed 1,2d | head -n -1| grep -i running)
195 echo "${blue}Powering off $vm_running VM(s)${reset}"
197 if ! virsh destroy $vm; then
198 echo "${red}WARNING: Unable to power off VM ${vm}${reset}"
200 echo "${blue}VM $vm powered off!${reset}"
202 done < <(virsh list --all | sed 1,2d | head -n -1| grep -i running | sed 's/^[ \t]*//' | awk '{print $2}')
203 echo "${blue}Destroying libvirt VMs...${reset}"
205 if ! virsh undefine --remove-all-storage $vm; then
206 echo "${red}ERROR: Unable to remove the VM ${vm}${reset}"
209 echo "${blue}VM $vm removed!${reset}"
211 done < <(virsh list --all | sed 1,2d | head -n -1| awk '{print $2}')
213 echo "${blue}No VMs found for removal"
215 echo "${blue}Removing libvirt and kvm packages"
216 yum -y remove libvirt-*
219 echo "${blue}libvirt/KVM is not installed${reset}"
222 ###remove kernel modules
223 echo "${blue}Removing kernel modules ${reset}"
224 for kernel_mod in vboxnetadp vboxnetflt vboxpci vboxdrv kvm_intel kvm; do
225 if ! rmmod $kernel_mod; then
226 if rmmod $kernel_mod 2>&1 | grep -i 'not currently loaded'; then
227 echo "${blue} $kernel_mod is not currently loaded! ${reset}"
229 echo "${red}Error trying to remove Kernel Module: $kernel_mod ${reset}"
233 echo "${blue}Removed Kernel Module: $kernel_mod ${reset}"