X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=blobdiff_plain;f=shell%2Fopenstack-cleanup-orphaned-ports.sh;h=080b5907c50a839c6953b5237f35430d039538e0;hb=a86c6e8d376c95aa00b179fce9d207e61c8f72d0;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 index fac8d202..080b5907 100644 --- a/shell/openstack-cleanup-orphaned-ports.sh +++ b/shell/openstack-cleanup-orphaned-ports.sh @@ -15,13 +15,27 @@ os_cloud="${OS_CLOUD:-vex}" set -eux -o pipefail -mapfile -t os_ports < <(openstack --os-cloud "$os_cloud" port list -f value -c ID -c status | egrep DOWN | awk '{print $1}') +mapfile -t os_ports_ts < <(openstack --os-cloud "$os_cloud" port list \ + -f value \ + -c ID \ + -c status \ + -c created_at \ + | grep -E DOWN \ + | awk -F' ' '{print $1 " " $3}') -if [ ${#os_ports[@]} -eq 0 ]; then +if [ ${#os_ports_ts[@]} -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" + cutoff=$(date -d "30 minutes ago" +%s) + for port_ts in "${os_ports_ts[@]}"; do + created_at_isots="${port_ts#* }" + port_uuid="${port_ts% *}" + echo "checking port uuid: ${port_uuid} with TS: ${created_at_isots}" + created_at_uxts=$(date -d "${created_at_isots}" +"%s") + # Clean up ports where created_at > 30 minutes + if [[ "$created_at_uxts" -gt "$cutoff" ]]; then + echo "Removing orphaned port $port_uuid created_at ts > 30 minutes." + openstack --os-cloud "$os_cloud" port delete "$port_uuid" + fi done fi