Chore: Upgrade Jenkins-job-builder to 6.3.0
[releng/global-jjb.git] / shell / openstack-cleanup-orphaned-ports.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 for orphaned ports
12 echo "---> Orphaned ports"
13
14 # shellcheck disable=SC1090
15 source ~/lf-env.sh
16
17 lf-activate-venv --python python3 "lftools[openstack]" \
18         python-openstackclient
19
20 os_cloud="${OS_CLOUD:-vex}"
21
22 set -eu -o pipefail
23
24 tmpfile=$(mktemp --suffix -openstack-ports.txt)
25 cores=$(nproc --all)
26 threads=$((3*cores))
27 regex_created_at='^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})Z$'
28
29 # Set age for deletion/removal
30 age="30 minutes ago"
31 cutoff=$(date -d "$age" +%s)
32
33 _cleanup()
34 {
35     uuid=$1
36     created_at=$(openstack --os-cloud "$os_cloud" port show -f value -c created_at "$uuid")
37
38     if [ "$created_at" == "None" ]; then
39         # This is a valid result for some objects; do not stop processing
40         echo "No value for port creation time; skipping: $uuid"
41
42     elif echo "$created_at" | grep -qP "$regex_created_at"; then
43
44         created_at_uxts=$(date -d "$created_at" +"%s")
45
46         # Cleanup objects where created_at is older than specified cutoff time
47         # created_at_uxts is measured against UNIX epoch; lower values are older
48         if [[ "$created_at_uxts" -lt "$cutoff" ]]; then
49             echo "Removing orphaned port $uuid created $created_at_uxts > $age"
50             openstack --os-cloud "$os_cloud" port delete "$uuid"
51         fi
52     else
53         # Don't stop the job, but warn about unexpected value
54         echo "Unknown/unexpected value for created_at: ${created_at}"
55     fi
56 }
57
58 _rmtemp()
59 {
60     if [ -f "$tmpfile" ]; then
61         # Removes temporary file on script exit
62         rm -f "$tmpfile"
63     fi
64 }
65
66 trap _rmtemp EXIT
67
68 # Output the initial list of port UUIDs to a temporary file
69 openstack --os-cloud "$os_cloud" port list -f value -c ID -c status \
70     | { grep -e DOWN || true; } | { awk '{print $1}' || true; } > "$tmpfile"
71
72 # Count the number to process
73 total=$(wc -l "$tmpfile" | awk '{print $1}')
74
75 if [ "$total" -eq 0 ]; then
76     echo "No orphaned ports to process."
77     exit 0
78 fi
79
80 echo "Ports to process: $total; age limit: $cutoff"
81 echo "Using $threads parallel processes..."
82
83 # Export variables and send to parallel for processing
84 export -f _cleanup
85 export os_cloud cutoff age
86 parallel --progress --retries 3 -j "$threads" _cleanup < "$tmpfile"