e35b95b1df034e7b0c5fedcba7d2e690eb888bb0
[apex.git] / ci / clean.sh
1 #!/usr/bin/env bash
2 ##############################################################################
3 # Copyright (c) 2015 Tim Rozet (Red Hat), Dan Radez (Red Hat) and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 #Clean script to uninstall provisioning server for Apex
12 #author: Dan Radez (dradez@redhat.com)
13 #author: Tim Rozet (trozet@redhat.com)
14
15 reset=$(tput sgr0 || echo "")
16 blue=$(tput setaf 4 || echo "")
17 red=$(tput setaf 1 || echo "")
18 green=$(tput setaf 2 || echo "")
19
20 vm_index=4
21 ovs_bridges="br-admin br-tenant br-external br-storage"
22 ovs_bridges+=" br-private br-public" # Legacy names, remove in E river
23
24 #OPNFV_NETWORK_TYPES=$(python3 -c 'from apex.common.constants import OPNFV_NETWORK_TYPES; print(" ".join(OPNFV_NETWORK_TYPES))')
25 OPNFV_NETWORK_TYPES+=" admin tenant external storage api"
26 OPNFV_NETWORK_TYPES+=" admin_network private_network public_network storage_network api_network" # Legecy names, remove in E river
27
28 ##detach interface from OVS and set the network config correctly
29 ##params: bridge to detach from
30 ##assumes only 1 real interface attached to OVS
31 function detach_interface_from_ovs {
32   local bridge
33   local port_output ports_no_orig
34   local net_path
35   local if_ip if_mask if_gw if_prefix
36   local if_metric if_dns1 if_dns2
37
38   net_path=/etc/sysconfig/network-scripts/
39   if [[ -z "$1" ]]; then
40     return 1
41   else
42     bridge=$1
43   fi
44
45   # if no interfaces attached then return
46   if ! ovs-vsctl list-ports ${bridge} | grep -Ev "vnet[0-9]*"; then
47     return 0
48   fi
49
50   # look for .orig ifcfg files  to use
51   port_output=$(ovs-vsctl list-ports ${bridge} | grep -Ev "vnet[0-9]*")
52   while read -r line; do
53     if [ -z "$line" ]; then
54       continue
55     elif [ -e ${net_path}/ifcfg-${line}.orig ]; then
56       mv -f ${net_path}/ifcfg-${line}.orig ${net_path}/ifcfg-${line}
57     elif [ -e ${net_path}/ifcfg-${bridge} ]; then
58       if_ip=$(sed -n 's/^IPADDR=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
59       if_mask=$(sed -n 's/^NETMASK=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
60       if_gw=$(sed -n 's/^GATEWAY=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
61       if_metric=$(sed -n 's/^METRIC=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
62       if_dns1=$(sed -n 's/^DNS1=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
63       if_dns2=$(sed -n 's/^DNS2=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
64
65       if [ -z "$if_mask" ]; then
66         if_prefix=$(sed -n 's/^PREFIX=[^0-9]*\([0-9][0-9]*\)[^0-9]*$/\1/p' ${net_path}/ifcfg-${bridge})
67         if_mask=$(prefix2mask ${if_prefix})
68       fi
69
70       if [[ -z "$if_ip" || -z "$if_mask" ]]; then
71         echo "ERROR: IPADDR or PREFIX/NETMASK missing for ${bridge} and no .orig file for interface ${line}"
72         return 1
73       fi
74
75       # create if cfg
76       echo "DEVICE=${line}
77 IPADDR=${if_ip}
78 NETMASK=${if_mask}
79 BOOTPROTO=static
80 ONBOOT=yes
81 TYPE=Ethernet
82 NM_CONTROLLED=no
83 PEERDNS=no" > ${net_path}/ifcfg-${line}
84
85       if [ -n "$if_gw" ]; then
86         echo "GATEWAY=${if_gw}" >> ${net_path}/ifcfg-${line}
87       fi
88
89       if [ -n "$if_metric" ]; then
90         echo "METRIC=${if_metric}" >> ${net_path}/ifcfg-${line}
91       fi
92
93       if [[ -n "$if_dns1" || -n "$if_dns2" ]]; then
94         sed -i '/PEERDNS/c\PEERDNS=yes' ${net_path}/ifcfg-${line}
95
96         if [ -n "$if_dns1" ]; then
97           echo "DNS1=${if_dns1}" >> ${net_path}/ifcfg-${line}
98         fi
99
100         if [ -n "$if_dns2" ]; then
101           echo "DNS2=${if_dns2}" >> ${net_path}/ifcfg-${line}
102         fi
103       fi
104       break
105     else
106       echo "ERROR: Real interface ${line} attached to bridge, but no interface or ${bridge} ifcfg file exists"
107       return 1
108     fi
109
110   done <<< "$port_output"
111
112   # modify the bridge ifcfg file
113   # to remove IP params
114   sudo sed -i 's/IPADDR=.*//' ${net_path}/ifcfg-${bridge}
115   sudo sed -i 's/NETMASK=.*//' ${net_path}/ifcfg-${bridge}
116   sudo sed -i 's/GATEWAY=.*//' ${net_path}/ifcfg-${bridge}
117   sudo sed -i 's/DNS1=.*//' ${net_path}/ifcfg-${bridge}
118   sudo sed -i 's/DNS2=.*//' ${net_path}/ifcfg-${bridge}
119   sudo sed -i 's/METRIC=.*//' ${net_path}/ifcfg-${bridge}
120   sudo sed -i 's/PEERDNS=.*//' ${net_path}/ifcfg-${bridge}
121
122   sudo systemctl restart network
123 }
124
125 display_usage() {
126   echo -e "Usage:\n$0 [arguments] \n"
127   echo -e "   -i|--inventory : Full path to inventory yaml file. Required only for baremetal node clean"
128 }
129
130 ##translates the command line parameters into variables
131 ##params: $@ the entire command line is passed
132 ##usage: parse_cmd_line() "$@"
133 parse_cmdline() {
134   echo -e "\n\n${blue}This script is used to clean an Apex environment${reset}\n\n"
135   echo "Use -h to display help"
136   sleep 2
137
138   while [ "${1:0:1}" = "-" ]
139   do
140     case "$1" in
141         -h|--help)
142                 display_usage
143                 exit 0
144             ;;
145         -i|--inventory)
146                 INVENTORY_FILE=$2
147                 shift 2
148             ;;
149         *)
150                 display_usage
151                 exit 1
152             ;;
153     esac
154   done
155
156   if [[ ! -z "$INVENTORY_FILE" && ! -f "$INVENTORY_FILE" ]]; then
157     echo -e "{$red}ERROR: Inventory File: ${INVENTORY_FILE} does not exist! Exiting...${reset}"
158     exit 1
159   fi
160 }
161
162 parse_cmdline "$@"
163
164 if [ -n "$INVENTORY_FILE" ]; then
165   echo -e "${blue}INFO: Parsing inventory file...${reset}"
166   # hack for now (until we switch fully over to clean.py) to tell if
167   # we should install apex from python or if rpm is being used
168   if ! rpm -q opnfv-apex-common > /dev/null; then
169     pushd ../ && python3 setup.py install > /dev/null
170     popd
171   fi
172   if ! python3 -m apex.clean -f ${INVENTORY_FILE}; then
173     echo -e "${red}WARN: Unable to shutdown all nodes! Please check /var/log/apex.log${reset}"
174   else
175     echo -e "${blue}INFO: Node shutdown complete...${reset}"
176   fi
177 fi
178
179 # Clean off instack/undercloud VM
180 for vm in instack undercloud; do
181   virsh destroy $vm 2> /dev/null | xargs echo -n
182   virsh undefine --nvram $vm 2> /dev/null | xargs echo -n
183   /usr/bin/touch /var/lib/libvirt/images/${vm}.qcow2
184   virsh vol-delete ${vm}.qcow2 --pool default 2> /dev/null | xargs echo -n
185   rm -f /var/lib/libvirt/images/${vm}.qcow2 2> /dev/null
186 done
187
188 # Clean off baremetal VMs in case they exist
189 for i in $(seq 0 $vm_index); do
190   virsh destroy baremetal$i 2> /dev/null | xargs echo -n
191   virsh undefine baremetal$i 2> /dev/null | xargs echo -n
192   /usr/bin/touch /var/lib/libvirt/images/baremetal${i}.qcow2
193   virsh vol-delete baremetal${i}.qcow2 --pool default 2> /dev/null | xargs echo -n
194   rm -f /var/lib/libvirt/images/baremetal${i}.qcow2 2> /dev/null
195   if [ -e /root/.vbmc/baremetal$i ]; then vbmc delete baremetal$i; fi
196 done
197
198 for network in ${OPNFV_NETWORK_TYPES}; do
199   virsh net-destroy ${network} 2> /dev/null
200   virsh net-undefine ${network} 2> /dev/null
201 done
202
203 # Clean off created bridges
204 for bridge in ${ovs_bridges}; do
205   if detach_interface_from_ovs ${bridge} 2> /dev/null; then
206     ovs-vsctl del-br ${bridge} 2> /dev/null
207     rm -f /etc/sysconfig/network-scripts/ifcfg-${bridge}
208   fi
209 done
210
211 # clean pub keys from root's auth keys
212 sed -i '/stack@undercloud.localdomain/d' /root/.ssh/authorized_keys
213
214
215 # force storage cleanup
216 virsh pool-refresh default
217
218 # remove temporary files
219 rm -f /tmp/network-environment.yaml
220
221 echo "Cleanup Completed"