blob: 6b295a09706bbb5ef2c0911847baa7fd92b7b800 [file] [log] [blame]
Oleksii Zhurba1580fc52017-11-14 15:20:44 -06001#!/bin/bash
Oleksii Zhurba1580fc52017-11-14 15:20:44 -06002export OS_INTERFACE='admin'
Oleksandr Savatieiev0a1db222019-01-29 10:31:19 +01003mask='s_rally\|rally_\|tempest_\|tempest-'
Alex Savatieiev27c2c412019-04-29 18:11:50 -05004stack_alt=false
5stack_regex='api-[0-9]+-[a-z]+'
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +02006dry_run=false
7clean_projects=false
Oleksandr Savatieiev41df4eb2018-11-08 16:22:30 +01008make_servers_active=false
Oleksii Zhurba0b736c72019-04-29 17:13:47 -05009serial=false
Alex Savatieiev788193b2019-03-12 11:39:08 -050010batch_size=10
Oleksandr Savatieiev722396b2019-01-29 12:53:21 +010011# granularity values: days,hours,minutes,seconds
12stack_granularity=days
13stack_granularity_value=1
14
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +020015function show_help {
Oleksandr Savatieiev722396b2019-01-29 12:53:21 +010016 printf "Resource cleaning script\nMask is: %s\n\t-h, -?\tShow this help\n" ${mask}
17 printf "\t-t\tDry run mode, no cleaning done\n"
18 printf "\t-P\tForce cleaning of projects\n"
Alex Savatieiev788193b2019-03-12 11:39:08 -050019 printf "\t-s\tUse single thread of 'openstack' client for cleanup\n"
Oleksandr Savatieiev722396b2019-01-29 12:53:21 +010020 printf "\t-S\tSet servers to ACTIVE before deletion (bare metal reqiured)\n"
Alex Savatieiev27c2c412019-04-29 18:11:50 -050021 printf "\t-f\tForce stack cleanup with an additional mask of '%s'\n" ${stack_regex}
Alex Savatieiev788193b2019-03-12 11:39:08 -050022 printf "\t-F\tForce purge deleted stacks. Batch size: %s, >%s %s\n" ${batch_size} ${stack_granularity_value} ${stack_granularity}
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +020023}
24
25OPTIND=1 # Reset in case getopts has been used previously in the shell.
Alex Savatieiev27c2c412019-04-29 18:11:50 -050026while getopts "h?:tsSPfF" opt; do
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +020027 case "$opt" in
28 h|\?)
29 show_help
30 exit 0
31 ;;
32 t) dry_run=true
33 printf "Running in dry-run mode\n"
34 ;;
Alex Savatieiev788193b2019-03-12 11:39:08 -050035 s) serial=true
36 printf "Single threaded mode enabled\n"
37 ;;
Oleksandr Savatieiev41df4eb2018-11-08 16:22:30 +010038 S) make_servers_active=true
39 printf "Servers will be set to ACTIVE before deletion\n"
40 ;;
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +020041 P) clean_projects=true
42 printf "Project cleanning enabled\n"
43 ;;
Alex Savatieiev27c2c412019-04-29 18:11:50 -050044 f) stack_alt=true
45 printf "Cleaning stacks using additional mask '%s'\n" ${stack_regex}
46 ;;
Oleksandr Savatieiev722396b2019-01-29 12:53:21 +010047 F) purge_deleted_stacks=true
48 printf "Purging stacks deleted >$stack_granularity_value $stack_granularity ago enabled, batch size %s\n" $stack_batch_size
49 ;;
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +020050 esac
51done
52
53shift $((OPTIND-1))
54[ "${1:-}" = "--" ] && shift
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060055
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020056### Execute collected commands and flush the temp file
57function _clean_and_flush {
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +020058 if [ "$dry_run" = true ] ; then
59 return 0
60 fi
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020061 if [ -s ${cmds} ]; then
Oleksii Zhurba0b736c72019-04-29 17:13:47 -050062 if [ "${serial}" = false ] ; then
Alex Savatieiev788193b2019-03-12 11:39:08 -050063 echo "... processing $(cat ${cmds} | wc -l) commands, worker threads ${batch_size}"
Alex Savatieiev6d8fda02019-05-10 16:20:55 -050064 cat ${cmds} | tr '\n' '\0' | xargs -P ${batch_size} -n 1 -0 echo | openstack
Alex Savatieiev788193b2019-03-12 11:39:08 -050065 #cat ${cmds} | openstack
66 truncate -s 0 ${cmds}
67 else
68 echo "... processing $(cat ${cmds} | wc -l) commands"
Alex Savatieiev6d8fda02019-05-10 16:20:55 -050069 cat ${cmds} | tr '\n' '\0' | xargs -P 1 -n 1 -0 echo | openstack
Oleksii Zhurba0b736c72019-04-29 17:13:47 -050070 truncate -s 0 ${cmds}
71 fi
72 fi
73}
74
75function _clean_and_flush_cinder {
76 if [ "$dry_run" = true ] ; then
77 return 0
78 fi
79 if [ -s ${cmds} ]; then
80 if [ "${serial}" = false ] ; then
81 echo "... processing $(cat ${cmds} | wc -l) commands, worker threads ${batch_size}"
82 cat ${cmds} | tr '\n' '\0' | xargs -I{} -P ${batch_size} -n 1 -0 /bin/bash -c 'cinder --os-volume-api-version 3.43 {}'
83 #cat ${cmds} | cinder --os-volume-api-version 3.43
84 truncate -s 0 ${cmds}
85 else
86 echo "... processing $(cat ${cmds} | wc -l) commands"
87 cat ${cmds} | tr '\n' '\0' | xargs -I{} -P 1 -n 1 -0 /bin/bash -c 'cinder --os-volume-api-version 3.43 {}'
Alex Savatieiev788193b2019-03-12 11:39:08 -050088 truncate -s 0 ${cmds}
89 fi
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020090 fi
91}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060092
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020093### Users
94function _clean_users {
95 users=( $(openstack user list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
96 echo "-> ${#users[@]} users containing '${mask}' found"
97 printf "%s\n" ${users[@]} | xargs -I{} echo user delete {} >>${cmds}
98 _clean_and_flush
99}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600100
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200101### Roles
102function _clean_roles {
103 roles=( $(openstack role list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
104 echo "-> ${#roles[@]} roles containing '${mask}' found"
105 printf "%s\n" ${roles[@]} | xargs -I{} echo role delete {} >>${cmds}
106 _clean_and_flush
107}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600108
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200109### Projects
110function _clean_projects {
111 projects=( $(openstack project list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
112 echo "-> ${#projects[@]} projects containing '${mask}' found"
113 printf "%s\n" ${projects[@]} | xargs -I{} echo project delete {} >>${cmds}
114 _clean_and_flush
115}
Valentyn Khalinbdf3ba92018-07-13 18:14:20 +0300116
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200117### Servers
118function _clean_servers {
Oleksandr Savatieiev0a1db222019-01-29 10:31:19 +0100119 servers=( $(openstack server list -c ID -c Name -f value --all | grep "${mask}" | cut -d' ' -f1) )
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200120 echo "-> ${#servers[@]} servers containing '${mask}' found"
Oleksandr Savatieiev41df4eb2018-11-08 16:22:30 +0100121 if [ "$make_servers_active" = true ]; then
122 printf "%s\n" ${servers[@]} | xargs -I{} echo server set --state active {} >>${cmds}
123 fi
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200124 printf "%s\n" ${servers[@]} | xargs -I{} echo server delete {} >>${cmds}
125 _clean_and_flush
126}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600127
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200128### Reset snapshot state and delete
129function _clean_snapshots {
130 snapshots=( $(openstack volume snapshot list --all -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
131 echo "-> ${#snapshots[@]} snapshots containing '${mask}' found"
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +0200132 printf "%s\n" ${snapshots[@]} | xargs -I{} echo volume snapshot set --state available {} >>${cmds}
133 printf "%s\n" ${snapshots[@]} | xargs -I{} echo volume snapshot delete {} >>${cmds}
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200134 _clean_and_flush
135}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600136
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200137function _clean_volumes {
138 volumes=( $(openstack volume list --all -c ID -c Name -c Type -f value | grep ${mask} | cut -d' ' -f1) )
139 echo "-> ${#volumes[@]} volumes containing '${mask}' found"
140 printf "%s\n" ${volumes[@]} | xargs -I{} echo volume set --state available {} >>${cmds}
141 printf "%s\n" ${volumes[@]} | xargs -I{} echo volume delete {} >>${cmds}
142 _clean_and_flush
143}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600144
Oleksii Zhurba0b736c72019-04-29 17:13:47 -0500145function _clean_volume_groups {
146 groups=( $(cinder --os-volume-api-version 3.43 group-list --all-tenants 1 | grep ${mask} | awk '{print $2}') )
147 echo "-> ${#groups[@]} groups containing '${mask}' found"
148 printf "%s\n" ${groups[@]} | xargs -I{} echo group-delete {} >>${cmds}
149 _clean_and_flush_cinder
150}
151
152function _clean_volume_group_types {
153 group_types=( $(cinder --os-volume-api-version 3.43 group-type-list | grep ${mask} | awk '{print $2}') )
154 echo "-> ${#group_types[@]} group types containing '${mask}' found"
155 printf "%s\n" ${group_types[@]} | xargs -I{} echo group-type-delete {} >>${cmds}
156 _clean_and_flush_cinder
157}
158
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200159### Volume types
160function _clean_volume_types {
161 vtypes=( $(openstack volume type list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
162 echo "-> ${#vtypes[@]} volume types containing '${mask}' found"
163 printf "%s\n" ${vtypes[@]} | xargs -I{} echo volume type delete {} >>${cmds}
164 _clean_and_flush
165}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600166
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200167### Images
168function _clean_images {
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +0200169 images=( $(openstack image list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200170 echo "-> ${#images[@]} images containing '${mask}' found"
171 printf "%s\n" ${images[@]} | xargs -I{} echo image delete {} >>${cmds}
172 _clean_and_flush
173}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600174
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200175### Sec groups
176function _clean_sec_groups {
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200177 projects=( $(openstack project list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
178 sgroups=( $(printf "%s\n" ${projects[@]} | xargs -I{} /bin/bash -c "openstack security group list -c ID -c Project -f value | grep {} | cut -d' ' -f1") )
179 echo "-> ${#sgroups[@]} security groups for project containing '${mask}' found"
180 printf "%s\n" ${sgroups[@]} | xargs -I{} echo security group delete {} >>${cmds}
181 _clean_and_flush
Alex Savatieiev788193b2019-03-12 11:39:08 -0500182
183 # Additional step to cleanup 'hanged' groups
184 sgroups_raw=( $(openstack security group list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
185 echo "-> ${#sgroups_raw[@]} security groups for '${mask}' found"
186 printf "%s\n" ${sgroups_raw[@]} | xargs -I{} echo security group delete {} >>${cmds}
187 _clean_and_flush
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200188}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600189
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200190### Keypairs
191function _clean_keypairs {
192 keypairs=( $(openstack keypair list -c Name -f value | grep ${mask}) )
193 echo "-> ${#keypairs[@]} keypairs containing '${mask}' found"
194 printf "%s\n" ${keypairs[@]} | xargs -I{} echo keypair delete {} >>${cmds}
195 _clean_and_flush
196}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600197
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200198### Routers and Networks
199function _clean_routers_and_networks {
200 routers=( $(openstack router list -c ID -c Name -f value | grep ${mask} | cut -d ' ' -f1) )
201 if [ ${#routers[@]} -eq 0 ]; then
202 echo "-> No routers containing '${mask}' found"
203 else
204 echo "-> ${#routers[@]} routers containing '${mask}' found"
Alex Savatieiev788193b2019-03-12 11:39:08 -0500205 echo "... unsetting gateways"
206 printf "%s\n" ${routers[@]} | xargs -I{} echo router unset --external-gateway {} >>${cmds}
207 _clean_and_flush
208
209 echo "... removing ports"
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200210 for router in ${routers[@]}; do
211 r_ports=( $(openstack port list --router ${router} -f value -c ID) )
212 if [ ${#r_ports[@]} -eq 0 ]; then
Alex Savatieiev788193b2019-03-12 11:39:08 -0500213 echo "... no ports to unplug for ${router}"
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200214 else
215 for r_port in ${r_ports[@]}; do
Alex Savatieiev788193b2019-03-12 11:39:08 -0500216 echo "... queued removal of port '${r_port}' from router '${router}'"
217 echo "router remove port ${router} ${r_port}" >>${cmds}
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200218 done
219 fi
220 done
Alex Savatieiev788193b2019-03-12 11:39:08 -0500221 _clean_and_flush
222
223 echo "... deleting routers"
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200224 printf "%s\n" ${routers[@]} | xargs -I{} echo router delete {} >>${cmds}
Alex Savatieiev788193b2019-03-12 11:39:08 -0500225 _clean_and_flush
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200226 fi
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600227
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200228 networks=( $(openstack network list | grep "${mask}" | cut -d' ' -f2) )
229 if [ ${#networks[@]} -eq 0 ]; then
230 echo "-> No networks containing '${mask}' found"
231 else
232 ports=()
233 subnets=()
234 for((idx=0;idx<${#networks[@]};idx++)) do
235 ports+=( $(openstack port list --network ${networks[idx]} -c ID -f value) )
236 subnets+=( $(openstack subnet list --network ${networks[idx]} -c ID -f value) )
237 echo "-> $((${idx}+1)) of ${#networks[@]}, total ${#ports[@]} ports, ${#subnets[@]} subnets"
238 done
239 printf "%s\n" ${ports[@]} | xargs -I{} echo port delete {} >>${cmds}
240 printf "%s\n" ${subnets[@]} | xargs -I{} echo subnet delete {} >>${cmds}
241 echo network delete ${networks[@]} >>${cmds}
242 echo "-> ${#routers[@]} routers, ${#ports[@]} ports, ${#subnets[@]} subnets, ${#networks[@]} networks"
243 fi
244 _clean_and_flush
245}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600246
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200247### Regions
248function _clean_regions {
249 regions=( $(openstack region list -c Region -f value | grep ${mask}) )
250 echo "-> ${#regions[@]} regions containing '${mask}' found"
251 printf "%s\n" ${regions[@]} | xargs -I{} echo region delete {} >>${cmds}
252 _clean_and_flush
253}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600254
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200255### Stacks
256function _clean_stacks {
257 # By default openstack denies use of global_index for everyone.
258 # In case you want to have handy cleanup, consider updating policy.json here:
259 # root@ctl0x:~# cat -n /etc/heat/policy.json | grep global_index
260 # 48 "stacks:global_index": "rule:deny_everybody",
261 # 73 "software_configs:global_index": "rule:deny_everybody",
262 # After this you will be able to use --all option
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600263
Oleksandr Savatieiev722396b2019-01-29 12:53:21 +0100264 stacks=( $(openstack stack list --nested --hidden -c ID -c "Stack Name" -f value | grep ${mask} | cut -d' ' -f1) )
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200265 echo "-> ${#stacks[@]} stacks containing '${mask}' found"
266 printf "%s\n" ${stacks[@]} | xargs -I{} echo stack check {} >>${cmds}
267 printf "%s\n" ${stacks[@]} | xargs -I{} echo stack delete -y {} >>${cmds}
268 _clean_and_flush
Alex Savatieiev27c2c412019-04-29 18:11:50 -0500269
270 if [ "$stack_alt" = true ]; then
271 stacks=( $(openstack stack list --nested --hidden -c ID -c "Stack Name" -f value | grep -E ${stack_regex} | cut -d' ' -f1) )
272 echo "-> ${#stacks[@]} stacks containing '${stack_regex}' found"
273 printf "%s\n" ${stacks[@]} | xargs -I{} echo stack check {} >>${cmds}
274 printf "%s\n" ${stacks[@]} | xargs -I{} echo stack delete -y {} >>${cmds}
275 _clean_and_flush
276 fi
277
Oleksandr Savatieiev722396b2019-01-29 12:53:21 +0100278 if [ "$purge_deleted_stacks" = true ]; then
Alex Savatieiev788193b2019-03-12 11:39:08 -0500279 heat-manage purge_deleted -g ${stack_granularity} -b ${batch_size} ${stack_granularity_value} | wc -l | xargs -I{} echo "-> Purged {} stacks"
Oleksandr Savatieiev722396b2019-01-29 12:53:21 +0100280 fi
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200281}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600282
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200283### Containers
284function _clean_containers {
285 containers=( $(openstack container list --all -c ID -c Name -f value | grep ${mask}) )
286 echo "-> ${#containers[@]} containers containing '${mask}' found"
287 printf "%s\n" ${containers[@]} | xargs -I{} echo container delete {} >>${cmds}
288 _clean_and_flush
289}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600290
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200291###################
292### Main
293###################
294# temp file for commands
295cmds=$(mktemp)
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +0200296trap "rm -f ${cmds}" EXIT
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200297echo "Using tempfile: '${cmds}'"
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600298
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200299# Consider cleaning contrail resources carefully
300# ...and only after that - clean projects
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600301
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200302_clean_stacks
303_clean_servers
304
305_clean_users
306_clean_roles
307_clean_snapshots
308_clean_volumes
Oleksii Zhurba0b736c72019-04-29 17:13:47 -0500309_clean_volume_groups
310_clean_volume_group_types
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200311_clean_volume_types
312_clean_images
313_clean_sec_groups
314_clean_keypairs
315_clean_routers_and_networks
316_clean_regions
317_clean_containers
318
319# project cleaning disabled by default
320# Coz cleaning Contrail with no projects is a hard task
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +0200321if [ "$clean_projects" = true ]; then
322 _clean_projects
323fi
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200324
325# remove temp file
326rm ${cmds}