#!/bin/bash # ============LICENSE_START======================================================= # Copyright (C) 2020 The Nordix Foundation. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # SPDX-License-Identifier: Apache-2.0 # ============LICENSE_END========================================================= set -o nounset set -o errexit set -o pipefail #------------------------------------------------------------------------------- # In some cases, it is useful to see all the output generated by commands so # this function makes it possible for users to achieve that by not redirecting # output to /dev/null when verbosity is enabled #------------------------------------------------------------------------------- redirect_cmd() { if [[ "$VERBOSITY" == "false" ]]; then "$@" > /dev/null 2>&1 else "$@" fi } VERBOSITY=${VERBOSITY:-false} # ensure dependencies are present echo "Info : Install python3.7-minimal python3-distutils virtualenv using apt" redirect_cmd sudo apt update redirect_cmd sudo apt install -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confnew -y python3.7-minimal python3-distutils virtualenv # prepare venv for openstack cli /bin/rm -rf $WORKSPACE/.venv virtualenv --python python3 --never-download $WORKSPACE/.venv set +u source $WORKSPACE/.venv set -u pip install --quiet --upgrade pip pip install --quiet python-cinderclient==6.0.0 pip install --quiet python-glanceclient==3.0.0 pip install --quiet python-heatclient==2.0.0 pip install --quiet python-ironic-inspector-client==4.0.0 pip install --quiet python-ironicclient==4.0.0 pip install --quiet python-keystoneclient==3.22.0 pip install --quiet python-neutronclient==7.1.0 pip install --quiet python-novaclient==16.0.0 pip install --quiet python-openstackclient==5.1.0 pip install --quiet python-swiftclient==3.9.0 echo "Info: Find Stacks $STACK_START_STR and keypair keypair-$STACK_START_STR" echo "-------------------------------------------------------------------------" source $OPENRC_FILE STACKS=$(openstack stack list -c "Stack Name" -c "Creation Time" -c ID -f value) KEYPAIRS=$(openstack keypair list -c "Name" -f value) # iternate of all the stacks while IFS= read -r stackline do # split line which comes in order ID,"Stack Name","Creation Time" stackdetails=($stackline) # find stacks starting with the search string if [[ ${stackdetails[1]} =~ ^$STACK_START_STR.* ]] then whenstackcreated=$(date -d ${stackdetails[2]} +%s) nowinsecond=$(date +%s) ageofstack=$(($nowinsecond-$whenstackcreated)) # find stacks older than the allowed time if [[ $ageofstack > $TIMETOLIVE*60 ]] then echo "Info: Going to delete stack " ${stackdetails[1]} " ID " ${stackdetails[0]} openstack stack delete ${stackdetails[0]} --wait --yes echo "Info: Deleted stack " ${stackdetails[1]} " ID " ${stackdetails[0]} # check keypair is there before trying to delete if [[ "${KEYPAIRS[@]}" =~ "keypair-${stackdetails[1]}" ]]; then echo "Info: Going to delete keypair " keypair-${stackdetails[1]} openstack keypair delete keypair-${stackdetails[1]} echo "Info: Deleted keypair " keypair-${stackdetails[1]} fi fi fi done <<< $(echo "$STACKS") echo "-------------------------------------------------------------------------" echo "Info: Finished deleting stacks and keypairs" deactivate # vim: set ts=2 sw=2 expandtab: