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