2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2019 The Linux Foundation and others.
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11 # Scans OpenStack for orphaned ports
12 echo "---> Orphaned ports"
14 # shellcheck disable=SC1090
17 lf-activate-venv --python python3 "lftools[openstack]" \
18 python-openstackclient
20 os_cloud="${OS_CLOUD:-vex}"
24 tmpfile=$(mktemp --suffix -openstack-ports.txt)
27 regex_created_at='^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})Z$'
29 # Set age for deletion/removal
31 cutoff=$(date -d "$age" +%s)
36 created_at=$(openstack --os-cloud "$os_cloud" port show -f value -c created_at "$uuid")
38 if [ "$created_at" == "None" ]; then
39 # This is a valid result for some objects; do not stop processing
40 echo "No value for port creation time; skipping: $uuid"
42 elif echo "$created_at" | grep -qP "$regex_created_at"; then
44 created_at_uxts=$(date -d "$created_at" +"%s")
46 # Cleanup objects where created_at is older than specified cutoff time
47 # created_at_uxts is measured against UNIX epoch; lower values are older
48 if [[ "$created_at_uxts" -lt "$cutoff" ]]; then
49 echo "Removing orphaned port $uuid created $created_at_uxts > $age"
50 openstack --os-cloud "$os_cloud" port delete "$uuid"
53 # Don't stop the job, but warn about unexpected value
54 echo "Unknown/unexpected value for created_at: ${created_at}"
60 if [ -f "$tmpfile" ]; then
61 # Removes temporary file on script exit
68 # Output the initial list of port UUIDs to a temporary file
69 openstack --os-cloud "$os_cloud" port list -f value -c ID -c status \
70 | { grep -e DOWN || true; } | { awk '{print $1}' || true; } > "$tmpfile"
72 # Count the number to process
73 total=$(wc -l "$tmpfile" | awk '{print $1}')
75 if [ "$total" -eq 0 ]; then
76 echo "No orphaned ports to process."
80 echo "Ports to process: $total; age limit: $cutoff"
81 echo "Using $threads parallel processes..."
83 # Export variables and send to parallel for processing
85 export os_cloud cutoff age
86 parallel --progress --retries 3 -j "$threads" _cleanup < "$tmpfile"