blob: 817499bf50282bd104fb91de29b24b06dc45da2f [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
afennerb8c241e2020-08-13 10:46:02 +010025#-------------------------------------------------------------------------------
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#-------------------------------------------------------------------------------
30redirect_cmd() {
31
32 if [[ "$VERBOSITY" == "false" ]]; then
33 "$@" > /dev/null 2>&1
34 else
35 "$@"
36 fi
37
38}
39
40VERBOSITY=${VERBOSITY:-false}
41# ensure dependencies are present
42echo "Info : Install python3.7-minimal python3-distutils virtualenv using apt"
43redirect_cmd sudo apt update
44redirect_cmd sudo apt install -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confnew -y python3.7-minimal python3-distutils virtualenv
45
afennerb252d142020-08-11 17:02:41 +010046# prepare venv for openstack cli
47/bin/rm -rf $WORKSPACE/.venv
48virtualenv --python python3 --never-download $WORKSPACE/.venv
49set +u
afenner6537f652020-08-14 13:26:18 +010050source $WORKSPACE/.venv/bin/activate
afennerb252d142020-08-11 17:02:41 +010051set -u
52pip install --quiet --upgrade pip
53pip install --quiet python-cinderclient==6.0.0
54pip install --quiet python-glanceclient==3.0.0
55pip install --quiet python-heatclient==2.0.0
56pip install --quiet python-ironic-inspector-client==4.0.0
57pip install --quiet python-ironicclient==4.0.0
58pip install --quiet python-keystoneclient==3.22.0
59pip install --quiet python-neutronclient==7.1.0
60pip install --quiet python-novaclient==16.0.0
61pip install --quiet python-openstackclient==5.1.0
62pip install --quiet python-swiftclient==3.9.0
63
64echo "Info: Find Stacks $STACK_START_STR and keypair keypair-$STACK_START_STR"
65echo "-------------------------------------------------------------------------"
66source $OPENRC_FILE
67STACKS=$(openstack stack list -c "Stack Name" -c "Creation Time" -c ID -f value)
68KEYPAIRS=$(openstack keypair list -c "Name" -f value)
69# iternate of all the stacks
70while IFS= read -r stackline
71do
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
94done <<< $(echo "$STACKS")
95
96echo "-------------------------------------------------------------------------"
97echo "Info: Finished deleting stacks and keypairs"
98
99deactivate
100# vim: set ts=2 sw=2 expandtab: