X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=blobdiff_plain;f=shell%2Fopenstack-cleanup-orphaned-ports.sh;h=b5c0af526f958a14cb03684d96a3c3f85108b2c2;hb=98090d2e52c78ada3ae2bc3fe4cb918df309a207;hp=fac8d202c5c0b208918207fa80d254dfa1b75de8;hpb=59ef2e78b9af7977117c06f154ed5faf92adff6f;p=releng%2Fglobal-jjb.git diff --git a/shell/openstack-cleanup-orphaned-ports.sh b/shell/openstack-cleanup-orphaned-ports.sh old mode 100644 new mode 100755 index fac8d202..b5c0af52 --- a/shell/openstack-cleanup-orphaned-ports.sh +++ b/shell/openstack-cleanup-orphaned-ports.sh @@ -11,17 +11,61 @@ # Scans OpenStack for orphaned ports echo "---> Orphaned ports" +# shellcheck disable=SC1090 +source ~/lf-env.sh + +lf-activate-venv --python python3 "lftools[openstack]" \ + python-openstackclient + os_cloud="${OS_CLOUD:-vex}" -set -eux -o pipefail +set -eu -o pipefail + +tmpfile=$(mktemp --suffix -openstack-ports.txt) +cores=$(nproc --all) +threads=$((3*cores)) + +# Set age for deletion/removal +age="30 minutes ago" +cutoff=$(date -d "$age" +%s) + +_cleanup() +{ + uuid=$1 + created_at=$(openstack --os-cloud "$os_cloud" port show -f value -c created_at "$uuid") + if [ "$created_at" == "None" ]; then + echo "No value for port creation time; skipping: $uuid" + else + created_at_uxts=$(date -d "$created_at" +"%s") + + # For debugging only; this outout usually disabled + # echo "Port: ${uuid} created at ${created_at} / ${created_at_uxts}" + + # Validate timing values are numeric + if [[ "$created_at_uxts" -eq "$created_at_uxts" ]]; then + # Clean up ports where created_at > 30 minutes + if [[ "$created_at_uxts" -lt "$cutoff" ]]; then + echo "Removing orphaned port $uuid created > $age" + openstack --os-cloud "$os_cloud" port delete "$uuid" + fi + else + echo "Date variable failed numeric test; deletion not possible" + fi + fi +} + +# Output the initial list of port UUIDs to a temporary file +openstack --os-cloud "$os_cloud" port list -f value -c ID -c status \ + | { grep -e DOWN || true; } | { awk '{print $1}' || true; } > "$tmpfile" + +# Count the number to process +total=$(wc -l "$tmpfile" | awk '{print $1}') +echo "Ports to process: $total; age limit: $cutoff" +echo "Using $threads parallel processes..." -mapfile -t os_ports < <(openstack --os-cloud "$os_cloud" port list -f value -c ID -c status | egrep DOWN | awk '{print $1}') +# Export variables and send to parallel for processing +export -f _cleanup +export os_cloud cutoff age +parallel --progress --retries 3 -j "$threads" _cleanup < "$tmpfile" -if [ ${#os_ports[@]} -eq 0 ]; then - echo "No orphaned ports found." -else - for port in "${os_ports[@]}"; do - echo "Removing orphaned port $port" - openstack --os-cloud "$os_cloud" port delete "$port" - done -fi +rm "$tmpfile"