Removing Deprecated ENV handling
[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 # Use default if no param passed
16 BASE=${BASE:-'/var/opt/opnfv'}
17 IMAGES=${IMAGES:-"$BASE/images"}
18 LIB=${LIB:-"$BASE/lib"}
19 reset=$(tput sgr0 || echo "")
20 blue=$(tput setaf 4 || echo "")
21 red=$(tput setaf 1 || echo "")
22 green=$(tput setaf 2 || echo "")
23
24 ##LIBRARIES
25 for lib in common-functions parse-functions; do
26   if ! source $LIB/${lib}.sh; then
27     echo "Failed to source $LIB/${lib}.sh"
28     exit 1
29   fi
30 done
31
32 vm_index=4
33 ovs_bridges="br-admin br-tenant br-external br-storage"
34 ovs_bridges+=" br-private br-public" # Legacy names, remove in E river
35
36 #OPNFV_NETWORK_TYPES=$(python3 -c 'from apex.common.constants import OPNFV_NETWORK_TYPES; print(" ".join(OPNFV_NETWORK_TYPES))')
37 OPNFV_NETWORK_TYPES+=" admin tenant external storage api"
38 OPNFV_NETWORK_TYPES+=" admin_network private_network public_network storage_network api_network" # Legecy names, remove in E river
39
40
41 display_usage() {
42   echo -e "Usage:\n$0 [arguments] \n"
43   echo -e "   -i|--inventory : Full path to inventory yaml file. Required only for baremetal node clean"
44 }
45
46 ##translates the command line parameters into variables
47 ##params: $@ the entire command line is passed
48 ##usage: parse_cmd_line() "$@"
49 parse_cmdline() {
50   echo -e "\n\n${blue}This script is used to deploy the Apex Installer and Provision OPNFV Target System${reset}\n\n"
51   echo "Use -h to display help"
52   sleep 2
53
54   while [ "${1:0:1}" = "-" ]
55   do
56     case "$1" in
57         -h|--help)
58                 display_usage
59                 exit 0
60             ;;
61         -i|--inventory)
62                 INVENTORY_FILE=$2
63                 shift 2
64             ;;
65         *)
66                 display_usage
67                 exit 1
68             ;;
69     esac
70   done
71
72   if [[ ! -z "$INVENTORY_FILE" && ! -f "$INVENTORY_FILE" ]]; then
73     echo -e "{$red}ERROR: Inventory File: ${INVENTORY_FILE} does not exist! Exiting...${reset}"
74     exit 1
75   fi
76 }
77
78 parse_cmdline "$@"
79
80 if [ -n "$INVENTORY_FILE" ]; then
81   echo -e "${blue}INFO: Parsing inventory file...${reset}"
82   if ! python3 -B $LIB/python/apex_python_utils.py clean -f ${INVENTORY_FILE}; then
83     echo -e "${red}WARN: Unable to shutdown all nodes! Please check /var/log/apex.log${reset}"
84   else
85     echo -e "${blue}INFO: Node shutdown complete...${reset}"
86   fi
87 fi
88
89 # Clean off instack/undercloud VM
90 for vm in instack undercloud; do
91   virsh destroy $vm 2> /dev/null | xargs echo -n
92   virsh undefine $vm 2> /dev/null | xargs echo -n
93   /usr/bin/touch /var/lib/libvirt/images/${vm}.qcow2
94   virsh vol-delete ${vm}.qcow2 --pool default 2> /dev/null | xargs echo -n
95   rm -f /var/lib/libvirt/images/${vm}.qcow2 2> /dev/null
96 done
97
98 # Clean off baremetal VMs in case they exist
99 for i in $(seq 0 $vm_index); do
100   virsh destroy baremetal$i 2> /dev/null | xargs echo -n
101   virsh undefine baremetal$i 2> /dev/null | xargs echo -n
102   /usr/bin/touch /var/lib/libvirt/images/baremetal${i}.qcow2
103   virsh vol-delete baremetal${i}.qcow2 --pool default 2> /dev/null | xargs echo -n
104   rm -f /var/lib/libvirt/images/baremetal${i}.qcow2 2> /dev/null
105 done
106
107 for network in ${OPNFV_NETWORK_TYPES}; do
108   virsh net-destroy ${network} 2> /dev/null
109   virsh net-undefine ${network} 2> /dev/null
110 done
111
112 # Clean off created bridges
113 for bridge in ${ovs_bridges}; do
114   if detach_interface_from_ovs ${bridge} 2> /dev/null; then
115     ovs-vsctl del-br ${bridge} 2> /dev/null
116     rm -f /etc/sysconfig/network-scripts/ifcfg-${bridge}
117   fi
118 done
119
120 # clean pub keys from root's auth keys
121 sed -i '/stack@undercloud.localdomain/d' /root/.ssh/authorized_keys
122 sed -i '/virtual-power-key/d' /root/.ssh/authorized_keys
123
124
125 # force storage cleanup
126 virsh pool-refresh default
127
128 # remove temporary files
129 rm -f /tmp/network-environment.yaml
130
131 echo "Cleanup Completed"