Merge "Feat: Added a script to cleanup generic openstack objects" v0.82.0
authorAndrew Grimberg <agrimberg@linuxfoundation.org>
Tue, 11 Oct 2022 19:10:20 +0000 (19:10 +0000)
committerGerrit Code Review <gerrit@linuxfoundation.org>
Tue, 11 Oct 2022 19:10:20 +0000 (19:10 +0000)
releasenotes/notes/fix-docker-push-109df3ce32505b1c.yaml [new file with mode: 0644]
releasenotes/notes/fix-docker-push-4113e45cde55fc56.yaml [new file with mode: 0644]
releasenotes/notes/fix-openstack-ports-cleanup-7b5e01d5867b5a79.yaml [new file with mode: 0644]
shell/docker-push.sh
shell/openstack-cleanup-orphaned-ports.sh [changed mode: 0644->0755]

diff --git a/releasenotes/notes/fix-docker-push-109df3ce32505b1c.yaml b/releasenotes/notes/fix-docker-push-109df3ce32505b1c.yaml
new file mode 100644 (file)
index 0000000..94c434f
--- /dev/null
@@ -0,0 +1,5 @@
+---
+fixes:
+  - |
+    Fix the docker-push script which is broken due to a synxtax error causing
+    the docker_push_command variable to not be set.
diff --git a/releasenotes/notes/fix-docker-push-4113e45cde55fc56.yaml b/releasenotes/notes/fix-docker-push-4113e45cde55fc56.yaml
new file mode 100644 (file)
index 0000000..5474cfc
--- /dev/null
@@ -0,0 +1,5 @@
+---
+fixes:
+  - |
+    Remove line break in the docker_push_command variable
+    causing the varible to not be set properly
diff --git a/releasenotes/notes/fix-openstack-ports-cleanup-7b5e01d5867b5a79.yaml b/releasenotes/notes/fix-openstack-ports-cleanup-7b5e01d5867b5a79.yaml
new file mode 100644 (file)
index 0000000..8c81154
--- /dev/null
@@ -0,0 +1,10 @@
+---
+fixes:
+  - |
+    Addresses failures when cleaning up orphaned openstack ports.
+    The main "openstack <object> list" command no longer accepts
+    the "-c created_at" option, which has been moved to a property
+    of the object and must now be queried with "openstack show object
+    UUID". Also, the created_at parameter sometimes returns "None"
+    instead of a timestamp, and the existing version of the script
+    does not catch this condition.
index 9348e8c..d8efac6 100644 (file)
@@ -15,8 +15,7 @@ echo "---> docker-push.sh"
 set -ue -o pipefail
 docker --version
 echo "Pushing image: $CONTAINER_PUSH_REGISTRY/$DOCKER_NAME:$DOCKER_IMAGE_TAG"
-docker_push_command="docker push" \
-    "$CONTAINER_PUSH_REGISTRY/$DOCKER_NAME:$DOCKER_IMAGE_TAG"
+docker_push_command="docker push $CONTAINER_PUSH_REGISTRY/$DOCKER_NAME:$DOCKER_IMAGE_TAG"
 echo "$docker_push_command"
 eval "$docker_push_command"
 echo "---> docker-push.sh ends"
old mode 100644 (file)
new mode 100755 (executable)
index c25ba59..8549737
@@ -14,35 +14,57 @@ echo "---> Orphaned ports"
 # shellcheck disable=SC1090
 source ~/lf-env.sh
 
+lf-activate-venv --python python3 "lftools[openstack]" \
+        python-openstackclient
+
 os_cloud="${OS_CLOUD:-vex}"
 
-lf-activate-venv --python python3 \
-    python-heatclient \
-    python-openstackclient
-
-set -eux -o pipefail
-
-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_ts[@]} -eq 0 ]; then
-    echo "No orphaned ports found."
-else
-    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"
+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
-    done
-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 | awk '{print $1}'> "$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..."
+
+# Export variables and send to parallel for processing
+export -f _cleanup
+export os_cloud cutoff age
+parallel --progress --retries 3 -j "$threads" _cleanup < "$tmpfile"
+
+rm "$tmpfile"