Feat: Added a script to cleanup generic openstack objects
[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
47 _cleanup()
48 {
49     uuid=$1
50     created_at=$(openstack --os-cloud "$os_cloud" "${object}" show -f value -c created_at "$uuid")
51
52     if [ "$created_at" == "None" ]; then
53         echo "No value for ${object} creation time; skipping: $uuid"
54     else
55         created_at_uxts=$(date -d "$created_at" +%s)
56
57         # For debugging only; this outout usually disabled
58         # echo "${uuid} created at ${created_at} / ${created_at_uxts} / cutoff: ${cutoff}"
59
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"
66             fi
67         else
68             echo "Date variable failed numeric test; deletion not possible"
69         fi
70     fi
71 }
72
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 | awk '{print $1}'> "$tmpfile"
78 else
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}'> "$tmpfile"
82 fi
83
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..."
88
89 # Export variables and send to parallel for processing
90 export -f _cleanup
91 export os_cloud cutoff age object
92 # parallel --progress --retries 3 -j "$threads" _cleanup < "$tmpfile"
93 parallel --retries 3 -j "$threads" _cleanup < "$tmpfile"
94
95 rm "$tmpfile"