blob: d9e49b9c3866da6054e3c2d1819c5092d10c8d74 [file] [log] [blame]
afennerb252d142020-08-11 17:02:41 +01001#!/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
21set -o nounset
22set -o errexit
23set -o pipefail
24
25# prepare venv for openstack cli
26/bin/rm -rf $WORKSPACE/.venv
27virtualenv --python python3 --never-download $WORKSPACE/.venv
28set +u
29source $WORKSPACE/.venv
30set -u
31pip install --quiet --upgrade pip
32pip install --quiet python-cinderclient==6.0.0
33pip install --quiet python-glanceclient==3.0.0
34pip install --quiet python-heatclient==2.0.0
35pip install --quiet python-ironic-inspector-client==4.0.0
36pip install --quiet python-ironicclient==4.0.0
37pip install --quiet python-keystoneclient==3.22.0
38pip install --quiet python-neutronclient==7.1.0
39pip install --quiet python-novaclient==16.0.0
40pip install --quiet python-openstackclient==5.1.0
41pip install --quiet python-swiftclient==3.9.0
42
43echo "Info: Find Stacks $STACK_START_STR and keypair keypair-$STACK_START_STR"
44echo "-------------------------------------------------------------------------"
45source $OPENRC_FILE
46STACKS=$(openstack stack list -c "Stack Name" -c "Creation Time" -c ID -f value)
47KEYPAIRS=$(openstack keypair list -c "Name" -f value)
48# iternate of all the stacks
49while IFS= read -r stackline
50do
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
73done <<< $(echo "$STACKS")
74
75echo "-------------------------------------------------------------------------"
76echo "Info: Finished deleting stacks and keypairs"
77
78deactivate
79# vim: set ts=2 sw=2 expandtab: