Fix: Abort script earlier when no objects to cleanup 80/70880/10
authorMatthew Watkins <mwatkins@linuxfoundation.org>
Mon, 17 Oct 2022 20:55:55 +0000 (21:55 +0100)
committerMatthew Watkins <mwatkins@linuxfoundation.org>
Mon, 24 Oct 2022 09:54:39 +0000 (10:54 +0100)
Issue-ID: RELENG-4483
Signed-off-by: Matthew Watkins <mwatkins@linuxfoundation.org>
Change-Id: I3311fc09a61f4d30fff8dd52e387c11dbcdc2aea

releasenotes/notes/more-script-improvements-8a4106897e92ad0b.yaml [new file with mode: 0644]
shell/openstack-cleanup-orphaned-objects.sh
shell/openstack-cleanup-orphaned-ports.sh

diff --git a/releasenotes/notes/more-script-improvements-8a4106897e92ad0b.yaml b/releasenotes/notes/more-script-improvements-8a4106897e92ad0b.yaml
new file mode 100644 (file)
index 0000000..3ce026a
--- /dev/null
@@ -0,0 +1,4 @@
+---
+fixes:
+  - |
+    Minor changes to improve openstack cleanup scripts.
index 6d845b7..59e1edf 100755 (executable)
@@ -43,6 +43,7 @@ echo "Filters: ${filters}"
 tmpfile=$(mktemp --suffix -openstack-"${object}"s.txt)
 cores=$(nproc --all)
 threads=$((3*cores))
+regex_created_at='^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})Z$'
 
 _cleanup()
 {
@@ -50,26 +51,35 @@ _cleanup()
     created_at=$(openstack --os-cloud "$os_cloud" "${object}" show -f value -c created_at "$uuid")
 
     if [ "$created_at" == "None" ]; then
+        # This is a valid result for some objects; do not stop processing
         echo "No value for ${object} creation time; skipping: $uuid"
-    else
+
+    elif echo "$created_at" | grep -qP "$regex_created_at"; then
+
         created_at_uxts=$(date -d "$created_at" +%s)
 
-        # For debugging only; this outout usually disabled
-        # echo "${uuid} created at ${created_at} / ${created_at_uxts} / cutoff: ${cutoff}"
-
-        # Validate timing values are numeric
-        if [[ "$created_at_uxts" -eq "$created_at_uxts" ]]; then
-            # Clean up objects when created_at > specified age
-            if [[ "$created_at_uxts" -lt "$cutoff" ]]; then
-                echo "Removing orphaned ${object} $uuid created $created_at_uxts > $age"
-                openstack --os-cloud "$os_cloud" "${object}" delete "$uuid"
-            fi
-        else
-            echo "Date variable failed numeric test; deletion not possible"
+        # Cleanup objects where created_at is older than specified cutoff time
+        # created_at_uxts is measured against UNIX epoch; lower values are older
+        if [[ "$created_at_uxts" -lt "$cutoff" ]]; then
+            echo "Removing orphaned ${object} $uuid created $created_at_uxts > $age"
+            openstack --os-cloud "$os_cloud" "${object}" delete "$uuid"
         fi
+    else
+        # Don't stop the job, but warn about unexpected value
+        echo "Unknown/unexpected value for created_at: ${created_at}"
     fi
 }
 
+_rmtemp()
+{
+    if [ -f "$tmpfile" ]; then
+        # Removes temporary file on script exit
+        rm -f "$tmpfile"
+    fi
+}
+
+trap _rmtemp EXIT
+
 # Output the initial list of object UUIDs to a temporary file
 if [[ -n ${filters} ]]; then
     # If a filter/match condition is requested/set
@@ -83,6 +93,12 @@ fi
 
 # Count the number of objects to process
 total=$(wc -l "$tmpfile" | awk '{print $1}')
+
+if [ "$total" -eq 0 ]; then
+    echo "No orphaned objects to process."
+    exit 0
+fi
+
 echo "Processing $total ${object} object(s); current time: $current age limit: $cutoff"
 echo "Using $threads parallel processes..."
 
@@ -91,5 +107,3 @@ export -f _cleanup
 export os_cloud cutoff age object
 # Add --progress flag to the command below for additional debug output
 parallel --retries 3 -j "$threads" _cleanup < "$tmpfile"
-
-rm "$tmpfile"
index b5c0af5..65ca372 100755 (executable)
@@ -24,6 +24,7 @@ set -eu -o pipefail
 tmpfile=$(mktemp --suffix -openstack-ports.txt)
 cores=$(nproc --all)
 threads=$((3*cores))
+regex_created_at='^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})Z$'
 
 # Set age for deletion/removal
 age="30 minutes ago"
@@ -33,33 +34,49 @@ _cleanup()
 {
     uuid=$1
     created_at=$(openstack --os-cloud "$os_cloud" port show -f value -c created_at "$uuid")
+
     if [ "$created_at" == "None" ]; then
+        # This is a valid result for some objects; do not stop processing
         echo "No value for port creation time; skipping: $uuid"
-    else
+
+    elif echo "$created_at" | grep -qP "$regex_created_at"; then
+
         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"
+        # Cleanup objects where created_at is older than specified cutoff time
+        # created_at_uxts is measured against UNIX epoch; lower values are older
+        if [[ "$created_at_uxts" -lt "$cutoff" ]]; then
+            echo "Removing orphaned port $uuid created $created_at_uxts > $age"
+            openstack --os-cloud "$os_cloud" port delete "$uuid"
         fi
+    else
+        # Don't stop the job, but warn about unexpected value
+        echo "Unknown/unexpected value for created_at: ${created_at}"
     fi
 }
 
+_rmtemp()
+{
+    if [ -f "$tmpfile" ]; then
+        # Removes temporary file on script exit
+        rm -f "$tmpfile"
+    fi
+}
+
+trap _rmtemp EXIT
+
 # 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}')
+
+if [ "$total" -eq 0 ]; then
+    echo "No orphaned ports to process."
+    exit 0
+fi
+
 echo "Ports to process: $total; age limit: $cutoff"
 echo "Using $threads parallel processes..."
 
@@ -67,5 +84,3 @@ echo "Using $threads parallel processes..."
 export -f _cleanup
 export os_cloud cutoff age
 parallel --progress --retries 3 -j "$threads" _cleanup < "$tmpfile"
-
-rm "$tmpfile"