add ccsdk-sli-northbound
[infra/cicd.git] / jjb / cloud-infra / scripts / cleanup-heat-stacks.sh
1 #!/bin/bash
2
3 # ============LICENSE_START=======================================================
4 #  Copyright (C) 2020 The Nordix Foundation. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # SPDX-License-Identifier: Apache-2.0
19 # ============LICENSE_END=========================================================
20
21 set -o nounset
22 set -o errexit
23 set -o pipefail
24
25 #-------------------------------------------------------------------------------
26 # In some cases, it is useful to see all the output generated by commands so
27 # this function makes it possible for users to achieve that by not redirecting
28 # output to /dev/null when verbosity is enabled
29 #-------------------------------------------------------------------------------
30 redirect_cmd() {
31
32   if [[ "$VERBOSITY" == "false" ]]; then
33     "$@" > /dev/null 2>&1
34   else
35     "$@"
36   fi
37
38 }
39
40 VERBOSITY=${VERBOSITY:-false}
41 # ensure dependencies are present
42 echo "Info  : Install python3.7-minimal python3-distutils virtualenv using apt"
43 redirect_cmd sudo apt update
44 redirect_cmd sudo apt install -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confnew -y python3.7-minimal python3-distutils virtualenv
45
46 # prepare venv for openstack cli
47 /bin/rm -rf $WORKSPACE/.venv
48 virtualenv --python python3 --never-download $WORKSPACE/.venv
49 set +u
50 source $WORKSPACE/.venv/bin/activate
51 set -u
52 pip install --quiet --upgrade pip
53 pip install --quiet python-cinderclient==6.0.0
54 pip install --quiet python-glanceclient==3.0.0
55 pip install --quiet python-heatclient==2.0.0
56 pip install --quiet python-ironic-inspector-client==4.0.0
57 pip install --quiet python-ironicclient==4.0.0
58 pip install --quiet python-keystoneclient==3.22.0
59 pip install --quiet python-neutronclient==7.1.0
60 pip install --quiet python-novaclient==16.0.0
61 pip install --quiet python-openstackclient==5.1.0
62 pip install --quiet python-swiftclient==3.9.0
63
64 echo "Info: Find Stacks $STACK_START_STR and keypair keypair-$STACK_START_STR"
65 echo "-------------------------------------------------------------------------"
66 source $OPENRC_FILE
67 STACKS=$(openstack stack list -c "Stack Name" -c "Creation Time" -c ID -f value)
68 KEYPAIRS=$(openstack keypair list -c "Name" -f value)
69 # iternate of all the stacks
70 while IFS= read -r stackline
71 do
72   # split line which comes in order ID,"Stack Name","Creation Time"
73   stackdetails=($stackline)
74   # find stacks starting with the search string
75   if [[ ${stackdetails[1]} =~ ^$STACK_START_STR.* ]]
76   then
77     whenstackcreated=$(date -d ${stackdetails[2]} +%s)
78     nowinsecond=$(date +%s)
79     ageofstack=$(($nowinsecond-$whenstackcreated))
80     # find stacks older than the allowed time
81     if [[ $ageofstack > $TIMETOLIVE*60 ]]
82     then
83       echo "Info: Going to delete stack " ${stackdetails[1]} " ID " ${stackdetails[0]}
84       openstack stack delete ${stackdetails[0]} --wait --yes
85       echo "Info: Deleted stack " ${stackdetails[1]} " ID " ${stackdetails[0]}
86       # check keypair is there before trying to delete
87       if [[ "${KEYPAIRS[@]}" =~ "keypair-${stackdetails[1]}" ]]; then
88          echo "Info: Going to delete keypair " keypair-${stackdetails[1]}
89          openstack keypair delete keypair-${stackdetails[1]}
90          echo "Info: Deleted keypair " keypair-${stackdetails[1]}
91       fi
92     fi
93   fi
94 done <<< $(echo "$STACKS")
95
96 echo "-------------------------------------------------------------------------"
97 echo "Info: Finished deleting stacks and keypairs"
98
99 deactivate
100 # vim: set ts=2 sw=2 expandtab: