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 environment for orphaned objects
13 # shellcheck disable=SC1090
16 lf-activate-venv --python python3 "lftools[openstack]" \
17 python-openstackclient
21 # OBJECT_TYPE is a mandatory external variable
22 object="${OBJECT_TYPE}"
23 echo "---> Orphaned Openstack ${object}s"
25 os_cloud="${OS_CLOUD:-vex}"
27 # Source age for deletion/removal from environment, otherwise use default value
28 age="${CLEANUP_AGE:="30 minutes ago"}"
29 echo "Cleanup age set to: ${age}"
31 cutoff=$(date -d "$age" +%s)
33 # Example attributes/filters to match orphaned port objects
34 # attributes="${ATTRIBUTES:="-c status"}"
35 # filters="${FILTERS:="grep -e DOWN"}"
37 # Set attributes/filters to match specific Openstack objects
38 attributes="${ATTRIBUTES:=""}"
39 filters="${FILTERS:=""}"
40 echo "Object attributes: ${attributes}"
41 echo "Filters: ${filters}"
43 tmpfile=$(mktemp --suffix -openstack-"${object}"s.txt)
50 created_at=$(openstack --os-cloud "$os_cloud" "${object}" show -f value -c created_at "$uuid")
52 if [ "$created_at" == "None" ]; then
53 echo "No value for ${object} creation time; skipping: $uuid"
55 created_at_uxts=$(date -d "$created_at" +%s)
57 # For debugging only; this outout usually disabled
58 # echo "${uuid} created at ${created_at} / ${created_at_uxts} / cutoff: ${cutoff}"
60 # Validate timing values are numeric
61 if [[ "$created_at_uxts" -eq "$created_at_uxts" ]]; then
62 # Clean up objects when created_at > specified age
63 if [[ "$created_at_uxts" -lt "$cutoff" ]]; then
64 echo "Removing orphaned ${object} $uuid created $created_at_uxts > $age"
65 openstack --os-cloud "$os_cloud" "${object}" delete "$uuid"
68 echo "Date variable failed numeric test; deletion not possible"
73 # Output the initial list of object UUIDs to a temporary file
74 if [[ -n ${filters} ]]; then
75 # If a filter/match condition is requested/set
76 openstack --os-cloud "$os_cloud" "${object}" list -f value -c ID $attributes \
77 | { $filters || true; } | { awk '{print $1}' || true; } > "$tmpfile"
79 # Otherwise don't pipe through an additional command
80 openstack --os-cloud "$os_cloud" "${object}" list -f value -c ID $attributes \
81 | { awk '{print $1}' || true; } > "$tmpfile"
84 # Count the number of objects to process
85 total=$(wc -l "$tmpfile" | awk '{print $1}')
86 echo "Processing $total ${object} object(s); current time: $current age limit: $cutoff"
87 echo "Using $threads parallel processes..."
89 # Export variables and send to parallel for processing
91 export os_cloud cutoff age object
92 # Add --progress flag to the command below for additional debug output
93 parallel --retries 3 -j "$threads" _cleanup < "$tmpfile"