59e1edff659706371c40044780a3f7119930decc
[releng/global-jjb.git] / shell / openstack-cleanup-orphaned-objects.sh
1 #!/bin/bash -l
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2019 The Linux Foundation and others.
5 #
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
12
13 # shellcheck disable=SC1090
14 source ~/lf-env.sh
15
16 lf-activate-venv --python python3 "lftools[openstack]" \
17         python-openstackclient
18
19 set -eu -o pipefail
20
21 # OBJECT_TYPE is a mandatory external variable
22 object="${OBJECT_TYPE}"
23 echo "---> Orphaned Openstack ${object}s"
24
25 os_cloud="${OS_CLOUD:-vex}"
26
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}"
30 current=$(date +%s)
31 cutoff=$(date -d "$age" +%s)
32
33 # Example attributes/filters to match orphaned port objects
34 # attributes="${ATTRIBUTES:="-c status"}"
35 # filters="${FILTERS:="grep -e DOWN"}"
36
37 # Set attributes/filters to match specific Openstack objects
38 attributes="${ATTRIBUTES:=""}"
39 filters="${FILTERS:=""}"
40 echo "Object attributes: ${attributes}"
41 echo "Filters: ${filters}"
42
43 tmpfile=$(mktemp --suffix -openstack-"${object}"s.txt)
44 cores=$(nproc --all)
45 threads=$((3*cores))
46 regex_created_at='^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})Z$'
47
48 _cleanup()
49 {
50     uuid=$1
51     created_at=$(openstack --os-cloud "$os_cloud" "${object}" show -f value -c created_at "$uuid")
52
53     if [ "$created_at" == "None" ]; then
54         # This is a valid result for some objects; do not stop processing
55         echo "No value for ${object} creation time; skipping: $uuid"
56
57     elif echo "$created_at" | grep -qP "$regex_created_at"; then
58
59         created_at_uxts=$(date -d "$created_at" +%s)
60
61         # Cleanup objects where created_at is older than specified cutoff time
62         # created_at_uxts is measured against UNIX epoch; lower values are older
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"
66         fi
67     else
68         # Don't stop the job, but warn about unexpected value
69         echo "Unknown/unexpected value for created_at: ${created_at}"
70     fi
71 }
72
73 _rmtemp()
74 {
75     if [ -f "$tmpfile" ]; then
76         # Removes temporary file on script exit
77         rm -f "$tmpfile"
78     fi
79 }
80
81 trap _rmtemp EXIT
82
83 # Output the initial list of object UUIDs to a temporary file
84 if [[ -n ${filters} ]]; then
85     # If a filter/match condition is requested/set
86     openstack --os-cloud "$os_cloud" "${object}" list -f value -c ID $attributes \
87      | { $filters || true; } | { awk '{print $1}' || true; } > "$tmpfile"
88 else
89     # Otherwise don't pipe through an additional command
90     openstack --os-cloud "$os_cloud" "${object}" list -f value -c ID $attributes \
91      | { awk '{print $1}' || true; } > "$tmpfile"
92 fi
93
94 # Count the number of objects to process
95 total=$(wc -l "$tmpfile" | awk '{print $1}')
96
97 if [ "$total" -eq 0 ]; then
98     echo "No orphaned objects to process."
99     exit 0
100 fi
101
102 echo "Processing $total ${object} object(s); current time: $current age limit: $cutoff"
103 echo "Using $threads parallel processes..."
104
105 # Export variables and send to parallel for processing
106 export -f _cleanup
107 export os_cloud cutoff age object
108 # Add --progress flag to the command below for additional debug output
109 parallel --retries 3 -j "$threads" _cleanup < "$tmpfile"