|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Helper script that will attempt to delete leftover resources from tempest testing. |
| 4 | +# Usage: tempest-cleanup.sh [--dry-run] |
| 5 | + |
| 6 | +# NOTE: This script is provided as a convenience and may not cover all |
| 7 | +# resources created by tempest. In particular, it does not attempt to delete |
| 8 | +# floating IPs and can have issues with ports attached to other resources. Use |
| 9 | +# with caution. |
| 10 | + |
| 11 | +DRY_RUN=false |
| 12 | + |
| 13 | +if [[ $1 == "--dry-run" ]]; then |
| 14 | + DRY_RUN=true |
| 15 | + echo "Dry run mode activated. No resources will be deleted." |
| 16 | +fi |
| 17 | + |
| 18 | +# Ensure openstack CLI is available |
| 19 | +if ! command -v openstack > /dev/null; then |
| 20 | + echo "openstack command not found. Ensure you have sourced a virtual environment with the openstack CLI installed." |
| 21 | + exit 1 |
| 22 | +elif ! openstack network list > /dev/null; then |
| 23 | + echo "openstack command failed. Ensure you have sourced an openrc file." |
| 24 | + exit 1 |
| 25 | +fi |
| 26 | + |
| 27 | +# Function to delete ports based on subnet IDs |
| 28 | +function delete_ports { |
| 29 | + local subnet_ids=$1 |
| 30 | + for subnet_id in $subnet_ids; do |
| 31 | + port_ids=$(openstack port list --fixed-ip subnet=$subnet_id -f value -c ID) |
| 32 | + if [[ -z $port_ids ]]; then |
| 33 | + echo "No ports found for subnet $subnet_id" |
| 34 | + continue |
| 35 | + fi |
| 36 | + for port_id in $port_ids; do |
| 37 | + local port_name=$(openstack port show $port_id -f value -c name) |
| 38 | + local device_owner=$(openstack port show $port_id -f value -c device_owner) |
| 39 | + if [[ $device_owner == "network:router_interface" ]] || [[ $device_owner == "network:ha_router_replicated_interface" ]]; then |
| 40 | + local router_id=$(openstack port show $port_id -f value -c device_id) |
| 41 | + if $DRY_RUN; then |
| 42 | + echo "Would remove router interface on port $port_id $port_name from router $router_id" |
| 43 | + else |
| 44 | + echo "Removing router interface on port $port_id $port_name from router $router_id..." |
| 45 | + openstack router remove port $router_id $port_id || echo "Failed to remove router interface on port $port_id $port_name from router $router_id" |
| 46 | + fi |
| 47 | + fi |
| 48 | + if $DRY_RUN; then |
| 49 | + echo "Would delete port $port_id $port_name" |
| 50 | + else |
| 51 | + echo "Deleting port $port_id $port_name..." |
| 52 | + openstack port delete $port_id || echo "Failed to delete port $port_id $port_name" |
| 53 | + fi |
| 54 | + done |
| 55 | + done |
| 56 | +} |
| 57 | + |
| 58 | +# Function to delete resources based on type and name prefix |
| 59 | +function delete_resources { |
| 60 | + local resource_type=$1 |
| 61 | + local name_prefix=$2 |
| 62 | + # If resource type is server, volume, or image, list all |
| 63 | + if [[ $resource_type == "server" ]] || [[ $resource_type == "volume" ]] || [[ $resource_type == "image" ]]; then |
| 64 | + id_name_list=$(openstack $resource_type list --all -f value -c ID -c Name | awk -v prefix="$name_prefix" '$2 ~ "^"prefix {print $1, $2}') |
| 65 | + else |
| 66 | + id_name_list=$(openstack $resource_type list -f value -c ID -c Name | awk -v prefix="$name_prefix" '$2 ~ "^"prefix {print $1, $2}') |
| 67 | + fi |
| 68 | + if [[ -z $id_name_list ]]; then |
| 69 | + echo "No $resource_type resources found with prefix $name_prefix" |
| 70 | + return |
| 71 | + fi |
| 72 | + while IFS= read -r line; do |
| 73 | + local resource_id=$(echo $line | awk '{print $1}') |
| 74 | + local resource_name=$(echo $line | awk '{$1=""; print $0}') |
| 75 | + if $DRY_RUN; then |
| 76 | + echo "Would delete $resource_type $resource_id $resource_name" |
| 77 | + else |
| 78 | + echo "Deleting $resource_type $resource_id $resource_name..." |
| 79 | + openstack $resource_type delete $resource_id || echo "Failed to delete $resource_type $resource_id $resource_name" |
| 80 | + fi |
| 81 | + done <<< "$id_name_list" |
| 82 | +} |
| 83 | + |
| 84 | +function cleanup { |
| 85 | + local name_prefix=$1 |
| 86 | + echo "Getting network and subnet IDs with $name_prefix prefix (may take a while)" |
| 87 | + |
| 88 | + # Get subnet IDs associated with tempest networks |
| 89 | + TEMPEST_NETWORK_IDS=$(openstack network list -f value -c ID -c Name | awk -v prefix="$TEMPEST_PREFIX" '$2 ~ "^"prefix {print $1}') |
| 90 | + TEMPEST_SUBNET_IDS="" |
| 91 | + for network_id in $TEMPEST_NETWORK_IDS; do |
| 92 | + TEMPEST_SUBNET_IDS+=" $(openstack subnet list --network $network_id -f value -c ID)" |
| 93 | + done |
| 94 | + |
| 95 | + # Delete resources in the specified order |
| 96 | + delete_resources server $name_prefix |
| 97 | + delete_ports "$TEMPEST_SUBNET_IDS" |
| 98 | + delete_resources volume $name_prefix |
| 99 | + delete_resources router $name_prefix |
| 100 | + delete_resources network $name_prefix |
| 101 | + delete_resources image $name_prefix |
| 102 | + delete_resources user $name_prefix |
| 103 | + delete_resources project $name_prefix |
| 104 | +} |
| 105 | + |
| 106 | +cleanup tempest- |
| 107 | +cleanup rally_ |
| 108 | + |
| 109 | +echo "Operation completed." |
0 commit comments