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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 # SPDX-License-Identifier: Apache-2.0
19 # ============LICENSE_END=========================================================
25 # prepare venv for openstack cli
26 /bin/rm -rf $WORKSPACE/.venv
27 virtualenv --python python3 --never-download $WORKSPACE/.venv
29 source $WORKSPACE/.venv
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
43 echo "Info: Find Stacks $STACK_START_STR and keypair keypair-$STACK_START_STR"
44 echo "-------------------------------------------------------------------------"
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
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.* ]]
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 ]]
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]}
73 done <<< $(echo "$STACKS")
75 echo "-------------------------------------------------------------------------"
76 echo "Info: Finished deleting stacks and keypairs"
79 # vim: set ts=2 sw=2 expandtab: