Fix 2 for moving ips to be static
[genesis.git] / foreman / ci / clean.sh
1 #!/usr/bin/env bash
2
3 #Clean script to uninstall provisioning server for Foreman/QuickStack
4 #author: Tim Rozet (trozet@redhat.com)
5 #
6 #Removes Libvirt, KVM, Vagrant, VirtualBox
7 #
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)
11
12 ##VARS
13 reset=`tput sgr0`
14 blue=`tput setaf 4`
15 red=`tput setaf 1`
16 green=`tput setaf 2`
17 vm_dir=/var/opt/opnfv
18 ##END VARS
19
20 ##FUNCTIONS
21 display_usage() {
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"
26 }
27
28 ##END FUNCTIONS
29
30 if [[ ( $1 == "--help") ||  $1 == "-h" ]]; then
31     display_usage
32     exit 0
33 fi
34
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"
37 sleep 2
38
39 while [ "`echo $1 | cut -c1`" = "-" ]
40 do
41     echo $1
42     case "$1" in
43         -base_config)
44                 base_config=$2
45                 shift 2
46             ;;
47         *)
48                 display_usage
49                 exit 1
50             ;;
51 esac
52 done
53
54 if [ ! -z "$base_config" ]; then
55   # Install ipmitool
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}"
60       exit 1
61     fi
62   else
63     echo "${blue}Skipping ipmitool as it is already installed!${reset}"
64   fi
65
66   ###find all the bmc IPs and number of nodes
67   node_counter=0
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
71     ((node_counter++))
72   done
73
74   max_nodes=$((node_counter-1))
75
76   ###find bmc_users per node
77   node_counter=0
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
81     ((node_counter++))
82   done
83
84   ###find bmc_pass per node
85   node_counter=0
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
89     ((node_counter++))
90   done
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}"
95     else
96       echo "${red}Error: Unable to power off $mynode, ${bmc_ip[$mynode]} ${reset}"
97       exit 1
98     fi
99   done
100 else
101   echo "${blue}Skipping Baremetal node poweroff as base_config was not provided${reset}"
102 fi
103 ###check to see if vbox is installed
104 vboxpkg=`rpm -qa | grep VirtualBox`
105 if [ $? -eq 0 ]; then
106   skip_vagrant=0
107 else
108   skip_vagrant=1
109 fi
110
111 ###legacy VM location check
112 ###remove me later
113 if [ -d /tmp/bgs_vagrant ]; then
114   cd /tmp/bgs_vagrant
115   vagrant destroy -f
116   rm -rf /tmp/bgs_vagrant
117 fi
118
119 ###destroy vagrant
120 if [ $skip_vagrant -eq 0 ]; then
121   if [ -d $vm_dir ]; then
122     ##all vm directories
123     for vm in $( ls $vm_dir ); do
124       cd $vm_dir/$vm
125       if vagrant destroy -f; then
126         echo "${blue}Successfully destroyed $vm Vagrant VM ${reset}"
127       else
128         echo "${red}Unable to destroy $vm Vagrant VM! Attempting to killall vagrant if process is hung ${reset}"
129         killall vagrant
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}"
133           exit 1
134         else
135           echo "${blue}Vagrant process doesn't exist.  Moving on... ${reset}"
136         fi
137       fi
138
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}"
147         else
148           echo "${red} Unable to delete VM $vm ...Exiting ${reset}"
149           exit 1
150         fi
151       else
152         echo "${blue}$vm VM is successfully deleted! ${reset}"
153       fi
154     done
155   else
156     echo "${blue}${vm_dir} doesn't exist, no VMs in OPNFV directory to destroy! ${reset}"
157   fi
158
159   echo "${blue}Checking for any remaining virtual box processes...${reset}"
160   ###kill virtualbox
161   if ps axf | grep virtualbox | grep -v 'grep'; then
162     echo "${blue}virtualbox processes are still running. Killing any remaining VirtualBox processes...${reset}"
163     killall virtualbox
164   fi
165
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}"
169     killall VBoxHeadless
170   fi
171
172   ###remove virtualbox
173   echo "${blue}Removing VirtualBox... ${reset}"
174   yum -y remove $vboxpkg
175
176 else
177   echo "${blue}Skipping Vagrant destroy + VBox Removal as VirtualBox package is already removed ${reset}"
178 fi
179
180 ###remove working vm directory
181 echo "${blue}Removing working VM directory: $vm_dir ${reset}"
182 rm -rf $vm_dir
183
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}"
189   vm_count=0
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}"
193     vm_runnning=0
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}"
196     while read -r vm; do
197       if ! virsh destroy $vm; then
198         echo "${red}WARNING: Unable to power off VM ${vm}${reset}"
199       else
200         echo "${blue}VM $vm powered off!${reset}"
201       fi
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}"
204     while read -r vm; do
205       if ! virsh undefine --remove-all-storage $vm; then
206         echo "${red}ERROR: Unable to remove the VM ${vm}${reset}"
207         exit 1
208       else
209         echo "${blue}VM $vm removed!${reset}"
210       fi
211     done < <(virsh list --all | sed 1,2d | head -n -1| awk '{print $2}')
212   else
213     echo "${blue}No VMs found for removal"
214   fi
215   echo "${blue}Removing libvirt and kvm packages"
216   yum -y remove libvirt-*
217   yum -y remove *qemu*
218 else
219   echo "${blue}libvirt/KVM is not installed${reset}"
220 fi
221
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}"
228     else
229       echo "${red}Error trying to remove Kernel Module: $kernel_mod ${reset}"
230       exit 1
231     fi
232   else
233     echo "${blue}Removed Kernel Module: $kernel_mod ${reset}"
234   fi
235 done