d9e49b9c3866da6054e3c2d1819c5092d10c8d74
[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 # prepare venv for openstack cli
26 /bin/rm -rf $WORKSPACE/.venv
27 virtualenv --python python3 --never-download $WORKSPACE/.venv
28 set +u
29 source $WORKSPACE/.venv
30 set -u
31 pip install --quiet --upgrade pip
32 pip install --quiet python-cinderclient==6.0.0
33 pip install --quiet python-glanceclient==3.0.0
34 pip install --quiet python-heatclient==2.0.0
35 pip install --quiet python-ironic-inspector-client==4.0.0
36 pip install --quiet python-ironicclient==4.0.0
37 pip install --quiet python-keystoneclient==3.22.0
38 pip install --quiet python-neutronclient==7.1.0
39 pip install --quiet python-novaclient==16.0.0
40 pip install --quiet python-openstackclient==5.1.0
41 pip install --quiet python-swiftclient==3.9.0
42
43 echo "Info: Find Stacks $STACK_START_STR and keypair keypair-$STACK_START_STR"
44 echo "-------------------------------------------------------------------------"
45 source $OPENRC_FILE
46 STACKS=$(openstack stack list -c "Stack Name" -c "Creation Time" -c ID -f value)
47 KEYPAIRS=$(openstack keypair list -c "Name" -f value)
48 # iternate of all the stacks
49 while IFS= read -r stackline
50 do
51   # split line which comes in order ID,"Stack Name","Creation Time"
52   stackdetails=($stackline)
53   # find stacks starting with the search string
54   if [[ ${stackdetails[1]} =~ ^$STACK_START_STR.* ]]
55   then
56     whenstackcreated=$(date -d ${stackdetails[2]} +%s)
57     nowinsecond=$(date +%s)
58     ageofstack=$(($nowinsecond-$whenstackcreated))
59     # find stacks older than the allowed time
60     if [[ $ageofstack > $TIMETOLIVE*60 ]]
61     then
62       echo "Info: Going to delete stack " ${stackdetails[1]} " ID " ${stackdetails[0]}
63       openstack stack delete ${stackdetails[0]} --wait --yes
64       echo "Info: Deleted stack " ${stackdetails[1]} " ID " ${stackdetails[0]}
65       # check keypair is there before trying to delete
66       if [[ "${KEYPAIRS[@]}" =~ "keypair-${stackdetails[1]}" ]]; then
67          echo "Info: Going to delete keypair " keypair-${stackdetails[1]}
68          openstack keypair delete keypair-${stackdetails[1]}
69          echo "Info: Deleted keypair " keypair-${stackdetails[1]}
70       fi
71     fi
72   fi
73 done <<< $(echo "$STACKS")
74
75 echo "-------------------------------------------------------------------------"
76 echo "Info: Finished deleting stacks and keypairs"
77
78 deactivate
79 # vim: set ts=2 sw=2 expandtab: