-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathstale.sh
executable file
·217 lines (199 loc) · 6.34 KB
/
stale.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env bash
set -Eeuo pipefail
unknown_command() {
echo "Unknown command: $*"
exit 1
}
print_help() {
echo 'https://github.com/shiftstack/shiftstack-ci/blob/main/stale.sh'
echo
echo 'Prints the IDs of the stale resources, with the timestamp of the last update and the resource name.'
echo
echo 'Usage:'
echo "$0 [-un|-q] <resource_type>"
echo
echo '"resource_type" can be any openstack resource type.'
echo 'Resources are identified as stale if they were last updated more than 5 hours ago.'
echo
echo '-u Only print the ID and the timestamp of the last update'
echo '-n Only print the ID and the resource name'
echo '-q Only print the ID'
echo
echo 'Examples:'
echo "$0 port"
echo "$0 -q floating ip"
}
declare \
print_name=no \
print_updated=no \
quiet=no
while getopts nuqh o; do
case "$o" in
n) print_name=yes ;;
u) print_updated=yes ;;
q) quiet=yes ;;
h) print_help; exit ;;
*) unknown_command "$@" ;;
esac
done
if [[ $print_name == 'yes' ]] || [[ $print_updated == 'yes' ]]; then
if [[ $quiet == 'yes' ]]; then
>&2 echo '-q can not be used with -n or -u.'
exit 2
fi
else
if [[ $quiet != 'yes' ]]; then
print_name=yes
print_updated=yes
fi
fi
shift $((OPTIND - 1))
if [[ $# -eq 0 ]]; then
print_help
exit
fi
declare -r resource_type="$*"
declare valid_limit
valid_limit="$(date --date='-7 hours' +%s)"
readonly valid_limit
list_server() {
for resource_id in $(openstack server list -f value -c ID); do
res="$(openstack server show -f json -c updated -c name "$resource_id")"
update_time="$(jq -r '.updated' <<< "$res")"
name="$(jq -r '.name' <<< "$res")"
printf '%s %s %s\n' "$resource_id" "$update_time" "$name"
done
}
list_port() {
openstack port list -f json -c ID -c Name -c 'Updated At' \
| jq -r '.[] | "\(.ID) \(."Updated At") \(.Name)"'
}
list_security_group() {
for resource_id in $(openstack security group list -f value -c ID); do
res="$(openstack security group show -f json -c updated_at -c name "$resource_id")"
update_time="$(jq -r '.updated_at' <<< "$res")"
name="$(jq -r '.name' <<< "$res")"
# we can't and don't want to remove the default security group
if [ "$name" != "default" ]; then
printf '%s %s %s\n' "$resource_id" "$update_time" "$name"
fi
done
}
list_loadbalancer() {
declare rt="loadbalancer"
for resource_id in $(openstack "$rt" list -f value -c id); do
res="$(openstack "$rt" show -f json -c updated_at -c name "$resource_id")"
update_time="$(jq -r '.updated_at' <<< "$res")"
name="$(jq -r '.name' <<< "$res")"
printf '%s %s %s\n' "$resource_id" "$update_time" "$name"
done
}
list_generic() {
declare rt="$1"
for resource_id in $(openstack "$rt" list -f value -c ID); do
res="$(openstack "$rt" show -f json -c updated_at -c name "$resource_id")"
update_time="$(jq -r '.updated_at' <<< "$res")"
name="$(jq -r '.name' <<< "$res")"
printf '%s %s %s\n' "$resource_id" "$update_time" "$name"
done
}
list_network() {
for resource_id in $(openstack network list -f value -c ID); do
# For networks we look at creation time, since removing a subnet updates the network
res="$(openstack network show -f json -c created_at -c name "$resource_id")"
creation_time="$(jq -r '.created_at' <<< "$res")"
name="$(jq -r '.name' <<< "$res")"
if [[ "$name" = *"hostonly"* ]] || [[ "$name" = "external"* ]] || [[ "$name" = *"sahara-access"* ]] || [[ "$name" = *"mellanox"* ]] || [[ "$name" = *"intel"* ]] || [[ "$name" = *"public"* ]] || [[ "$name" = *"slaac"* ]]; then
continue
fi
printf '%s %s %s\n' "$resource_id" "$creation_time" "$name"
done
}
list_subnet() {
for resource_id in $(openstack subnet list -f value -c ID); do
res="$(openstack subnet show -f json -c updated_at -c name "$resource_id")"
update_time="$(jq -r '.updated_at' <<< "$res")"
name="$(jq -r '.name' <<< "$res")"
if [[ "$name" = *"hostonly"* ]] || [[ "$name" = "external"* ]] || [[ "$name" = *"public"* ]] || [[ "$name" = *"mellanox"* ]] || [[ "$name" = *"intel"* ]] || [[ "$name" = *"slaac"* ]]; then
continue
fi
printf '%s %s %s\n' "$resource_id" "$update_time" "$name"
done
}
list_keypair() {
for resource_id in $(openstack keypair list -f value -c Name); do
res="$(openstack keypair show -f json -c created_at -c Name "$resource_id")"
update_time="$(jq -r '.created_at' <<< "$res")"
name="$(jq -r '.name' <<< "$res")"
printf '%s %s %s\n' "$resource_id" "$update_time" "$name"
done
}
list_image() {
for resource_id in $(openstack image list -f value -c ID); do
res="$(openstack image show -f json -c updated_at -c name "$resource_id")"
update_time="$(jq -r '.updated_at' <<< "$res")"
name="$(jq -r '.name' <<< "$res")"
# Match images based on known patterns:
# IPI bootstrap payload -- .{8}-.{5}-.{5}-ignition
# IPI RHCOS image -- .{8}-.{5}-.{5}-rhcos
# UPI bootstrap payload -- bootstrap-ign-.{8}-.{5}-.{5}
# UPI RHCOS image -- rhcos-.{8}-.{5}
if [[ "$name" =~ .{8}-.{5}-.{5}-ignition ]] || [[ "$name" =~ .{8}-.{5}-.{5}-rhcos ]] || [[ "$name" =~ bootstrap-ign-.{8}-.{5}-.{5} ]] || [[ "$name" =~ rhcos-.{8}-.{5} ]]; then
printf '%s %s %s\n' "$resource_id" "$update_time" "$name"
else
continue
fi
done
}
list_router() {
for resource_id in $(openstack router list -f value -c ID); do
res="$(openstack router show -f json -c updated_at -c name "$resource_id")"
update_time="$(jq -r '.updated_at' <<< "$res")"
name="$(jq -r '.name' <<< "$res")"
if [ "$name" == "dualstack" ]; then
continue
fi
printf '%s %s %s\n' "$resource_id" "$update_time" "$name"
done
}
case $resource_type in
server)
list_server ;;
port)
list_port ;;
'network trunk'|'floating ip'|'volume'|'volume snapshot')
list_generic "$resource_type" ;;
'router')
list_router;;
'network')
list_network;;
'subnet')
list_subnet;;
'loadbalancer')
list_loadbalancer ;;
'security group')
list_security_group ;;
'keypair')
list_keypair ;;
'image')
list_image ;;
'server group')
>&2 printf 'Creation date is not available for %s.' "$resource_type"
exit 3
;;
*)
>&2 printf 'Resource "%s" not implemented.' "$resource_type"
exit 3
;;
esac | while read -r resource_id update_time name; do
if [[ "$(date --date="$update_time" +%s)" -lt "$valid_limit" ]]; then
printf '%s' "$resource_id"
if [[ $print_updated == 'yes' ]]; then
printf ' %s' "$update_time"
fi
if [[ $print_name == 'yes' ]]; then
printf ' %s' "$name"
fi
printf '\n'
fi
done