2 ##############################################################################
3 # Copyright (c) 2015 Tim Rozet (Red Hat), Dan Radez (Red Hat) and others.
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 ##############################################################################
11 #Clean script to uninstall provisioning server for Apex
12 #author: Dan Radez (dradez@redhat.com)
13 #author: Tim Rozet (trozet@redhat.com)
15 reset=$(tput sgr0 || echo "")
16 blue=$(tput setaf 4 || echo "")
17 red=$(tput setaf 1 || echo "")
18 green=$(tput setaf 2 || echo "")
21 ovs_bridges="br-admin br-tenant br-external br-storage"
22 ovs_bridges+=" br-private br-public" # Legacy names, remove in E river
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
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 {
33 local port_output ports_no_orig
35 local if_ip if_mask if_gw if_prefix
36 local if_metric if_dns1 if_dns2
38 net_path=/etc/sysconfig/network-scripts/
39 if [[ -z "$1" ]]; then
45 # if no interfaces attached then return
46 if ! ovs-vsctl list-ports ${bridge} | grep -Ev "vnet[0-9]*"; then
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
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})
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})
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}"
83 PEERDNS=no" > ${net_path}/ifcfg-${line}
85 if [ -n "$if_gw" ]; then
86 echo "GATEWAY=${if_gw}" >> ${net_path}/ifcfg-${line}
89 if [ -n "$if_metric" ]; then
90 echo "METRIC=${if_metric}" >> ${net_path}/ifcfg-${line}
93 if [[ -n "$if_dns1" || -n "$if_dns2" ]]; then
94 sed -i '/PEERDNS/c\PEERDNS=yes' ${net_path}/ifcfg-${line}
96 if [ -n "$if_dns1" ]; then
97 echo "DNS1=${if_dns1}" >> ${net_path}/ifcfg-${line}
100 if [ -n "$if_dns2" ]; then
101 echo "DNS2=${if_dns2}" >> ${net_path}/ifcfg-${line}
106 echo "ERROR: Real interface ${line} attached to bridge, but no interface or ${bridge} ifcfg file exists"
110 done <<< "$port_output"
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}
122 sudo systemctl restart network
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"
130 ##translates the command line parameters into variables
131 ##params: $@ the entire command line is passed
132 ##usage: parse_cmd_line() "$@"
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"
138 while [ "${1:0:1}" = "-" ]
156 if [[ ! -z "$INVENTORY_FILE" && ! -f "$INVENTORY_FILE" ]]; then
157 echo -e "{$red}ERROR: Inventory File: ${INVENTORY_FILE} does not exist! Exiting...${reset}"
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
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}"
175 echo -e "${blue}INFO: Node shutdown complete...${reset}"
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
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
198 for network in ${OPNFV_NETWORK_TYPES}; do
199 virsh net-destroy ${network} 2> /dev/null
200 virsh net-undefine ${network} 2> /dev/null
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}
211 # clean pub keys from root's auth keys
212 sed -i '/stack@undercloud.localdomain/d' /root/.ssh/authorized_keys
215 # force storage cleanup
216 virsh pool-refresh default
218 # remove temporary files
219 rm -f /tmp/network-environment.yaml
221 echo "Cleanup Completed"