1a16efd6aa68c0d495c77f6a4e17c789b401ca54
[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 #Uses Vagrant and 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)
11
12 ##VARS
13 reset=`tput sgr0`
14 blue=`tput setaf 4`
15 red=`tput setaf 1`
16 green=`tput setaf 2`
17
18 vm_dir=/var/opt/opnfv
19 ##END VARS
20
21 ##FUNCTIONS
22 display_usage() {
23   echo -e "\n\n${blue}This script is used to uninstall Foreman/QuickStack Installer and Clean OPNFV Target System${reset}\n\n"
24   echo -e "\nUsage:\n$0 [arguments] \n"
25   echo -e "\n   -no_parse : No variable parsing into config. Flag. \n"
26   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"
27 }
28
29 ##END FUNCTIONS
30
31 if [[ ( $1 == "--help") ||  $1 == "-h" ]]; then
32     display_usage
33     exit 0
34 fi
35
36 echo -e "\n\n${blue}This script is used to uninstall Foreman/QuickStack Installer and Clean OPNFV Target System${reset}\n\n"
37 echo "Use -h to display help"
38 sleep 2
39
40 while [ "`echo $1 | cut -c1`" = "-" ]
41 do
42     echo $1
43     case "$1" in
44         -base_config)
45                 base_config=$2
46                 shift 2
47             ;;
48         *)
49                 display_usage
50                 exit 1
51             ;;
52 esac
53 done
54
55
56 # Install ipmitool
57 # Major version is pinned to force some consistency for Arno
58 if ! yum list installed | grep -i ipmitool; then
59   if ! yum -y install ipmitool-1*; then
60     echo "${red}Unable to install ipmitool!${reset}"
61     exit 1
62   fi
63 else
64   echo "${blue}Skipping ipmitool as it is already installed!${reset}"
65 fi
66
67 ###find all the bmc IPs and number of nodes
68 node_counter=0
69 output=`grep bmc_ip $base_config | grep -Eo '[0-9]+.[0-9]+.[0-9]+.[0-9]+'`
70 for line in ${output} ; do
71   bmc_ip[$node_counter]=$line
72   ((node_counter++))
73 done
74
75 max_nodes=$((node_counter-1))
76
77 ###find bmc_users per node
78 node_counter=0
79 output=`grep bmc_user $base_config | sed 's/\s*bmc_user:\s*//'`
80 for line in ${output} ; do
81   bmc_user[$node_counter]=$line
82   ((node_counter++))
83 done
84
85 ###find bmc_pass per node
86 node_counter=0
87 output=`grep bmc_pass $base_config | sed 's/\s*bmc_pass:\s*//'`
88 for line in ${output} ; do
89   bmc_pass[$node_counter]=$line
90   ((node_counter++))
91 done
92
93 for mynode in `seq 0 $max_nodes`; do
94   echo "${blue}Node: ${bmc_ip[$mynode]} ${bmc_user[$mynode]} ${bmc_pass[$mynode]} ${reset}"
95   if ipmitool -I lanplus -P ${bmc_pass[$mynode]} -U ${bmc_user[$mynode]} -H ${bmc_ip[$mynode]} chassis power off; then
96     echo "${blue}Node: $mynode, ${bmc_ip[$mynode]} powered off!${reset}"
97   else
98     echo "${red}Error: Unable to power off $mynode, ${bmc_ip[$mynode]} ${reset}"
99     exit 1
100   fi
101 done
102
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; 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; 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; 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 ###remove kernel modules
185 echo "${blue}Removing kernel modules ${reset}"
186 for kernel_mod in vboxnetadp vboxnetflt vboxpci vboxdrv; do
187   if ! rmmod $kernel_mod; then
188     if rmmod $kernel_mod 2>&1 | grep -i 'not currently loaded'; then
189       echo "${blue} $kernel_mod is not currently loaded! ${reset}"
190     else
191       echo "${red}Error trying to remove Kernel Module: $kernel_mod ${reset}"
192       exit 1
193     fi
194   else
195     echo "${blue}Removed Kernel Module: $kernel_mod ${reset}"
196   fi
197 done