-
Notifications
You must be signed in to change notification settings - Fork 14
/
cleanup.sh
343 lines (308 loc) · 13.4 KB
/
cleanup.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/bin/bash
export OS_INTERFACE='admin'
mask='cvp\|s_rally\|rally_\|tempest_\|tempest-\|spt-'
exclude='manual\|-static-'
stack_alt=false
stack_regex='api-[0-9]+-[a-z]+'
dry_run=false
clean_projects=false
make_servers_active=false
serial=false
batch_size=10
# granularity values: days,hours,minutes,seconds
stack_granularity=days
stack_granularity_value=1
function show_help {
printf "Resource cleaning script\nMask is: %s\n\t-h, -?\tShow this help\n" ${mask}
printf "\t-t\tDry run mode, no cleaning done\n"
printf "\t-P\tForce cleaning of projects\n"
printf "\t-s\tUse single thread of 'openstack' client for cleanup\n"
printf "\t-S\tSet servers to ACTIVE before deletion (bare metal reqiured)\n"
printf "\t-f\tForce stack cleanup with an additional mask of '%s'\n" ${stack_regex}
printf "\t-F\tForce purge deleted stacks. Batch size: %s, >%s %s\n" ${batch_size} ${stack_granularity_value} ${stack_granularity}
}
OPTIND=1 # Reset in case getopts has been used previously in the shell.
while getopts "h?:tsSPfF" opt; do
case "$opt" in
h|\?)
show_help
exit 0
;;
t) dry_run=true
printf "Running in dry-run mode\n"
;;
s) serial=true
printf "Single threaded mode enabled\n"
;;
S) make_servers_active=true
printf "Servers will be set to ACTIVE before deletion\n"
;;
P) clean_projects=true
printf "Project cleanning enabled\n"
;;
f) stack_alt=true
printf "Cleaning stacks using additional mask '%s'\n" ${stack_regex}
;;
F) purge_deleted_stacks=true
printf "Purging stacks deleted >$stack_granularity_value $stack_granularity ago enabled, batch size %s\n" $stack_batch_size
;;
esac
done
shift $((OPTIND-1))
[ "${1:-}" = "--" ] && shift
### Execute collected commands and flush the temp file
function _clean_and_flush {
if [ "$dry_run" = true ] ; then
return 0
fi
if [ -s ${cmds} ]; then
if [ "${serial}" = false ] ; then
echo "... processing $(cat ${cmds} | wc -l) commands, worker threads ${batch_size}"
cat ${cmds} | tr '\n' '\0' | xargs -P ${batch_size} -n 1 -0 echo | openstack
#cat ${cmds} | openstack
truncate -s 0 ${cmds}
else
echo "... processing $(cat ${cmds} | wc -l) commands"
cat ${cmds} | tr '\n' '\0' | xargs -P 1 -n 1 -0 echo | openstack
truncate -s 0 ${cmds}
fi
fi
}
function _clean_and_flush_cinder {
if [ "$dry_run" = true ] ; then
return 0
fi
if [ -s ${cmds} ]; then
if [ "${serial}" = false ] ; then
echo "... processing $(cat ${cmds} | wc -l) commands, worker threads ${batch_size}"
cat ${cmds} | tr '\n' '\0' | xargs -I{} -P ${batch_size} -n 1 -0 /bin/bash -c 'cinder --os-volume-api-version 3.43 {}'
#cat ${cmds} | cinder --os-volume-api-version 3.43
truncate -s 0 ${cmds}
else
echo "... processing $(cat ${cmds} | wc -l) commands"
cat ${cmds} | tr '\n' '\0' | xargs -I{} -P 1 -n 1 -0 /bin/bash -c 'cinder --os-volume-api-version 3.43 {}'
truncate -s 0 ${cmds}
fi
fi
}
### Users
function _clean_users {
users=( $(openstack user list -c ID -c Name -f value | grep ${mask} | grep -v ${exclude} | cut -d' ' -f1) )
echo "-> ${#users[@]} users containing '${mask}' found"
printf "%s\n" ${users[@]} | xargs -I{} echo user delete {} >>${cmds}
_clean_and_flush
}
### Roles
function _clean_roles {
roles=( $(openstack role list -c ID -c Name -f value | grep ${mask} | grep -v ${exclude} | cut -d' ' -f1) )
echo "-> ${#roles[@]} roles containing '${mask}' found"
printf "%s\n" ${roles[@]} | xargs -I{} echo role delete {} >>${cmds}
_clean_and_flush
}
### Projects
function _clean_projects {
projects=( $(openstack project list -c ID -c Name -f value | grep ${mask} | grep -v ${exclude} | cut -d' ' -f1) )
echo "-> ${#projects[@]} projects containing '${mask}' found"
printf "%s\n" ${projects[@]} | xargs -I{} echo project delete {} >>${cmds}
_clean_and_flush
}
### Servers
function _clean_servers {
servers=( $(openstack server list -c ID -c Name -f value --all | grep "${mask}" | grep -v ${exclude} | cut -d' ' -f1) )
echo "-> ${#servers[@]} servers containing '${mask}' found"
if [ "$make_servers_active" = true ]; then
printf "%s\n" ${servers[@]} | xargs -I{} echo server set --state active {} >>${cmds}
fi
printf "%s\n" ${servers[@]} | xargs -I{} echo server delete {} >>${cmds}
_clean_and_flush
}
### Reset snapshot state and delete
function _clean_snapshots {
snapshots=( $(openstack volume snapshot list --all -c ID -c Name -f value | grep ${mask} | grep -v ${exclude} | cut -d' ' -f1) )
echo "-> ${#snapshots[@]} snapshots containing '${mask}' found"
printf "%s\n" ${snapshots[@]} | xargs -I{} echo volume snapshot set --state available {} >>${cmds}
printf "%s\n" ${snapshots[@]} | xargs -I{} echo volume snapshot delete {} >>${cmds}
_clean_and_flush
}
function _clean_volumes {
volumes=( $(openstack volume list --all -c ID -c Name -c Type -f value | grep ${mask} | grep -v ${exclude} | cut -d' ' -f1) )
echo "-> ${#volumes[@]} volumes containing '${mask}' found"
printf "%s\n" ${volumes[@]} | xargs -I{} echo volume set --state available {} >>${cmds}
printf "%s\n" ${volumes[@]} | xargs -I{} echo volume delete {} >>${cmds}
_clean_and_flush
}
function _clean_volume_groups {
groups=( $(cinder --os-volume-api-version 3.43 group-list --all-tenants 1 | grep ${mask} | grep -v ${exclude} | awk '{print $2}') )
echo "-> ${#groups[@]} groups containing '${mask}' found"
printf "%s\n" ${groups[@]} | xargs -I{} echo group-delete {} >>${cmds}
_clean_and_flush_cinder
}
function _clean_volume_group_types {
group_types=( $(cinder --os-volume-api-version 3.43 group-type-list | grep ${mask} | grep -v ${exclude} | awk '{print $2}') )
echo "-> ${#group_types[@]} group types containing '${mask}' found"
printf "%s\n" ${group_types[@]} | xargs -I{} echo group-type-delete {} >>${cmds}
_clean_and_flush_cinder
}
### Volume types
function _clean_volume_types {
vtypes=( $(openstack volume type list -c ID -c Name -f value | grep ${mask} | grep -v ${exclude} | cut -d' ' -f1) )
echo "-> ${#vtypes[@]} volume types containing '${mask}' found"
printf "%s\n" ${vtypes[@]} | xargs -I{} echo volume type delete {} >>${cmds}
_clean_and_flush
}
### Images
function _clean_images {
images=( $(openstack image list -c ID -c Name -f value | grep ${mask} | grep -v ${exclude} | cut -d' ' -f1) )
echo "-> ${#images[@]} images containing '${mask}' found"
printf "%s\n" ${images[@]} | xargs -I{} echo image delete {} >>${cmds}
_clean_and_flush
}
### Sec groups
function _clean_sec_groups {
projects=( $(openstack project list -c ID -c Name -f value | grep ${mask} | grep -v ${exclude} | cut -d' ' -f1) )
sgroups=( $(printf "%s\n" ${projects[@]} | xargs -I{} /bin/bash -c "openstack security group list -c ID -c Project -f value | grep {} | cut -d' ' -f1") )
echo "-> ${#sgroups[@]} security groups for project containing '${mask}' found"
printf "%s\n" ${sgroups[@]} | xargs -I{} echo security group delete {} >>${cmds}
_clean_and_flush
# Additional step to cleanup 'hanged' groups
sgroups_raw=( $(openstack security group list -c ID -c Name -f value | grep ${mask} | grep -v ${exclude} | cut -d' ' -f1) )
echo "-> ${#sgroups_raw[@]} security groups for '${mask}' found"
printf "%s\n" ${sgroups_raw[@]} | xargs -I{} echo security group delete {} >>${cmds}
_clean_and_flush
}
### Keypairs
function _clean_keypairs {
keypairs=( $(openstack keypair list -c Name -f value | grep ${mask} | grep -v ${exclude}) )
echo "-> ${#keypairs[@]} keypairs containing '${mask}' found"
printf "%s\n" ${keypairs[@]} | xargs -I{} echo keypair delete {} >>${cmds}
_clean_and_flush
}
### Routers and Networks
function _clean_routers_and_networks {
routers=( $(openstack router list -c ID -c Name -f value | grep ${mask} | grep -v ${exclude} | cut -d ' ' -f1) )
if [ ${#routers[@]} -eq 0 ]; then
echo "-> No routers containing '${mask}' found"
else
echo "-> ${#routers[@]} routers containing '${mask}' found"
echo "... unsetting gateways"
printf "%s\n" ${routers[@]} | xargs -I{} echo router unset --external-gateway {} >>${cmds}
_clean_and_flush
echo "... removing ports"
for router in ${routers[@]}; do
r_ports=( $(openstack port list --router ${router} -f value -c ID) )
if [ ${#r_ports[@]} -eq 0 ]; then
echo "... no ports to unplug for ${router}"
else
for r_port in ${r_ports[@]}; do
echo "... queued removal of port '${r_port}' from router '${router}'"
echo "router remove port ${router} ${r_port}" >>${cmds}
done
fi
done
_clean_and_flush
echo "... deleting routers"
printf "%s\n" ${routers[@]} | xargs -I{} echo router delete {} >>${cmds}
_clean_and_flush
fi
networks=( $(openstack network list | grep "${mask}" | grep -v ${exclude} | cut -d' ' -f2) )
if [ ${#networks[@]} -eq 0 ]; then
echo "-> No networks containing '${mask}' found"
else
ports=()
subnets=()
for((idx=0;idx<${#networks[@]};idx++)) do
ports+=( $(openstack port list --network ${networks[idx]} -c ID -f value) )
subnets+=( $(openstack subnet list --network ${networks[idx]} -c ID -f value) )
echo "-> $((${idx}+1)) of ${#networks[@]}, total ${#ports[@]} ports, ${#subnets[@]} subnets"
done
printf "%s\n" ${ports[@]} | xargs -I{} echo port delete {} >>${cmds}
printf "%s\n" ${subnets[@]} | xargs -I{} echo subnet delete {} >>${cmds}
echo network delete ${networks[@]} >>${cmds}
echo "-> ${#routers[@]} routers, ${#ports[@]} ports, ${#subnets[@]} subnets, ${#networks[@]} networks"
fi
_clean_and_flush
}
### Regions
function _clean_regions {
regions=( $(openstack region list -c Region -f value | grep ${mask} | grep -v ${exclude}) )
echo "-> ${#regions[@]} regions containing '${mask}' found"
printf "%s\n" ${regions[@]} | xargs -I{} echo region delete {} >>${cmds}
_clean_and_flush
}
### Services
function _clean_services {
services=( $(openstack service list -c Name -f value | grep ${mask} | grep -v ${exclude}) )
echo "-> ${#services[@]} services containing '${mask}' found"
printf "%s\n" ${services[@]} | xargs -I{} echo service delete {} >>${cmds}
_clean_and_flush
}
### Stacks
function _clean_stacks {
# By default openstack denies use of global_index for everyone.
# In case you want to have handy cleanup, consider updating policy.json here:
# root@ctl0x:~# cat -n /etc/heat/policy.json | grep global_index
# 48 "stacks:global_index": "rule:deny_everybody",
# 73 "software_configs:global_index": "rule:deny_everybody",
# After this you will be able to use --all option
stacks=( $(openstack stack list --nested --hidden -c ID -c "Stack Name" -f value | grep ${mask} | grep -v ${exclude} | cut -d' ' -f1) )
echo "-> ${#stacks[@]} stacks containing '${mask}' found"
printf "%s\n" ${stacks[@]} | xargs -I{} echo stack check {} >>${cmds}
printf "%s\n" ${stacks[@]} | xargs -I{} echo stack delete -y {} >>${cmds}
_clean_and_flush
if [ "$stack_alt" = true ]; then
stacks=( $(openstack stack list --nested --hidden -c ID -c "Stack Name" -f value | grep -E ${stack_regex} | cut -d' ' -f1) )
echo "-> ${#stacks[@]} stacks containing '${stack_regex}' found"
printf "%s\n" ${stacks[@]} | xargs -I{} echo stack check {} >>${cmds}
printf "%s\n" ${stacks[@]} | xargs -I{} echo stack delete -y {} >>${cmds}
_clean_and_flush
fi
if [ "$purge_deleted_stacks" = true ]; then
heat-manage purge_deleted -g ${stack_granularity} -b ${batch_size} ${stack_granularity_value} | wc -l | xargs -I{} echo "-> Purged {} stacks"
fi
}
### Containers
function _clean_containers {
containers=( $(openstack container list --all -c ID -c Name -f value | grep ${mask} | grep -v ${exclude} | cut -d' ' -f1) )
echo "-> ${#containers[@]} containers containing '${mask}' found"
printf "%s\n" ${containers[@]} | xargs -I{} echo container delete {} >>${cmds}
_clean_and_flush
}
function _clean_flavors {
flavors=( $(openstack flavor list --all -c ID -c Name -f value | grep ${mask} | grep -v ${exclude} | cut -d' ' -f1) )
echo "-> ${#flavors[@]} flavors containing '${mask}' found"
printf "%s\n" ${flavors[@]} | xargs -I{} echo flavor delete {} >>${cmds}
_clean_and_flush
}
###################
### Main
###################
# temp file for commands
cmds=$(mktemp)
trap "rm -f ${cmds}" EXIT
echo "Using tempfile: '${cmds}'"
# Consider cleaning contrail resources carefully
# ...and only after that - clean projects
_clean_stacks
_clean_servers
_clean_flavors
_clean_users
_clean_roles
_clean_snapshots
_clean_volumes
_clean_volume_groups
_clean_volume_group_types
_clean_volume_types
_clean_images
_clean_sec_groups
_clean_keypairs
_clean_routers_and_networks
_clean_regions
_clean_services
_clean_containers
# project cleaning disabled by default
# Coz cleaning Contrail with no projects is a hard task
if [ "$clean_projects" = true ]; then
_clean_projects
fi
# remove temp file
rm ${cmds}