blob: dd2cb28f7a53f1764da0a92eaf86db3855905eb3 [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-'
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +02004dry_run=false
5clean_projects=false
Oleksandr Savatieiev41df4eb2018-11-08 16:22:30 +01006make_servers_active=false
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +02007
Alex Savatieiev788193b2019-03-12 11:39:08 -05008batch_size=10
Oleksandr Savatieiev722396b2019-01-29 12:53:21 +01009# granularity values: days,hours,minutes,seconds
10stack_granularity=days
11stack_granularity_value=1
12
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +020013function show_help {
Oleksandr Savatieiev722396b2019-01-29 12:53:21 +010014 printf "Resource cleaning script\nMask is: %s\n\t-h, -?\tShow this help\n" ${mask}
15 printf "\t-t\tDry run mode, no cleaning done\n"
16 printf "\t-P\tForce cleaning of projects\n"
Alex Savatieiev788193b2019-03-12 11:39:08 -050017 printf "\t-s\tUse single thread of 'openstack' client for cleanup\n"
Oleksandr Savatieiev722396b2019-01-29 12:53:21 +010018 printf "\t-S\tSet servers to ACTIVE before deletion (bare metal reqiured)\n"
Alex Savatieiev788193b2019-03-12 11:39:08 -050019 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 +020020}
21
22OPTIND=1 # Reset in case getopts has been used previously in the shell.
Alex Savatieiev788193b2019-03-12 11:39:08 -050023while getopts "h?:tsSPF" opt; do
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +020024 case "$opt" in
25 h|\?)
26 show_help
27 exit 0
28 ;;
29 t) dry_run=true
30 printf "Running in dry-run mode\n"
31 ;;
Alex Savatieiev788193b2019-03-12 11:39:08 -050032 s) serial=true
33 printf "Single threaded mode enabled\n"
34 ;;
Oleksandr Savatieiev41df4eb2018-11-08 16:22:30 +010035 S) make_servers_active=true
36 printf "Servers will be set to ACTIVE before deletion\n"
37 ;;
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +020038 P) clean_projects=true
39 printf "Project cleanning enabled\n"
40 ;;
Oleksandr Savatieiev722396b2019-01-29 12:53:21 +010041 F) purge_deleted_stacks=true
42 printf "Purging stacks deleted >$stack_granularity_value $stack_granularity ago enabled, batch size %s\n" $stack_batch_size
43 ;;
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +020044 esac
45done
46
47shift $((OPTIND-1))
48[ "${1:-}" = "--" ] && shift
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060049
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020050### Execute collected commands and flush the temp file
51function _clean_and_flush {
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +020052 if [ "$dry_run" = true ] ; then
53 return 0
54 fi
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020055 if [ -s ${cmds} ]; then
Alex Savatieiev788193b2019-03-12 11:39:08 -050056 if [ "${serial}" = true ] ; then
57 echo "... processing $(cat ${cmds} | wc -l) commands, worker threads ${batch_size}"
58 cat ${cmds} | tr '\n' '\0' | xargs -v -P ${batch_size} -n 1 -0 openstack
59 #cat ${cmds} | openstack
60 truncate -s 0 ${cmds}
61 else
62 echo "... processing $(cat ${cmds} | wc -l) commands"
63 cat ${cmds} | openstack
64 truncate -s 0 ${cmds}
65 fi
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020066 fi
67}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060068
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020069### Users
70function _clean_users {
71 users=( $(openstack user list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
72 echo "-> ${#users[@]} users containing '${mask}' found"
73 printf "%s\n" ${users[@]} | xargs -I{} echo user delete {} >>${cmds}
74 _clean_and_flush
75}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060076
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020077### Roles
78function _clean_roles {
79 roles=( $(openstack role list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
80 echo "-> ${#roles[@]} roles containing '${mask}' found"
81 printf "%s\n" ${roles[@]} | xargs -I{} echo role delete {} >>${cmds}
82 _clean_and_flush
83}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060084
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020085### Projects
86function _clean_projects {
87 projects=( $(openstack project list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
88 echo "-> ${#projects[@]} projects containing '${mask}' found"
89 printf "%s\n" ${projects[@]} | xargs -I{} echo project delete {} >>${cmds}
90 _clean_and_flush
91}
Valentyn Khalinbdf3ba92018-07-13 18:14:20 +030092
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020093### Servers
94function _clean_servers {
Oleksandr Savatieiev0a1db222019-01-29 10:31:19 +010095 servers=( $(openstack server list -c ID -c Name -f value --all | grep "${mask}" | cut -d' ' -f1) )
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020096 echo "-> ${#servers[@]} servers containing '${mask}' found"
Oleksandr Savatieiev41df4eb2018-11-08 16:22:30 +010097 if [ "$make_servers_active" = true ]; then
98 printf "%s\n" ${servers[@]} | xargs -I{} echo server set --state active {} >>${cmds}
99 fi
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200100 printf "%s\n" ${servers[@]} | xargs -I{} echo server delete {} >>${cmds}
101 _clean_and_flush
102}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600103
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200104### Reset snapshot state and delete
105function _clean_snapshots {
106 snapshots=( $(openstack volume snapshot list --all -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
107 echo "-> ${#snapshots[@]} snapshots containing '${mask}' found"
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +0200108 printf "%s\n" ${snapshots[@]} | xargs -I{} echo volume snapshot set --state available {} >>${cmds}
109 printf "%s\n" ${snapshots[@]} | xargs -I{} echo volume snapshot delete {} >>${cmds}
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200110 _clean_and_flush
111}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600112
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200113function _clean_volumes {
114 volumes=( $(openstack volume list --all -c ID -c Name -c Type -f value | grep ${mask} | cut -d' ' -f1) )
115 echo "-> ${#volumes[@]} volumes containing '${mask}' found"
116 printf "%s\n" ${volumes[@]} | xargs -I{} echo volume set --state available {} >>${cmds}
117 printf "%s\n" ${volumes[@]} | xargs -I{} echo volume delete {} >>${cmds}
118 _clean_and_flush
119}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600120
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200121### Volume types
122function _clean_volume_types {
123 vtypes=( $(openstack volume type list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
124 echo "-> ${#vtypes[@]} volume types containing '${mask}' found"
125 printf "%s\n" ${vtypes[@]} | xargs -I{} echo volume type delete {} >>${cmds}
126 _clean_and_flush
127}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600128
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200129### Images
130function _clean_images {
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +0200131 images=( $(openstack image list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200132 echo "-> ${#images[@]} images containing '${mask}' found"
133 printf "%s\n" ${images[@]} | xargs -I{} echo image delete {} >>${cmds}
134 _clean_and_flush
135}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600136
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200137### Sec groups
138function _clean_sec_groups {
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200139 projects=( $(openstack project list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
140 sgroups=( $(printf "%s\n" ${projects[@]} | xargs -I{} /bin/bash -c "openstack security group list -c ID -c Project -f value | grep {} | cut -d' ' -f1") )
141 echo "-> ${#sgroups[@]} security groups for project containing '${mask}' found"
142 printf "%s\n" ${sgroups[@]} | xargs -I{} echo security group delete {} >>${cmds}
143 _clean_and_flush
Alex Savatieiev788193b2019-03-12 11:39:08 -0500144
145 # Additional step to cleanup 'hanged' groups
146 sgroups_raw=( $(openstack security group list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
147 echo "-> ${#sgroups_raw[@]} security groups for '${mask}' found"
148 printf "%s\n" ${sgroups_raw[@]} | xargs -I{} echo security group delete {} >>${cmds}
149 _clean_and_flush
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200150}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600151
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200152### Keypairs
153function _clean_keypairs {
154 keypairs=( $(openstack keypair list -c Name -f value | grep ${mask}) )
155 echo "-> ${#keypairs[@]} keypairs containing '${mask}' found"
156 printf "%s\n" ${keypairs[@]} | xargs -I{} echo keypair delete {} >>${cmds}
157 _clean_and_flush
158}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600159
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200160### Routers and Networks
161function _clean_routers_and_networks {
162 routers=( $(openstack router list -c ID -c Name -f value | grep ${mask} | cut -d ' ' -f1) )
163 if [ ${#routers[@]} -eq 0 ]; then
164 echo "-> No routers containing '${mask}' found"
165 else
166 echo "-> ${#routers[@]} routers containing '${mask}' found"
Alex Savatieiev788193b2019-03-12 11:39:08 -0500167 echo "... unsetting gateways"
168 printf "%s\n" ${routers[@]} | xargs -I{} echo router unset --external-gateway {} >>${cmds}
169 _clean_and_flush
170
171 echo "... removing ports"
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200172 for router in ${routers[@]}; do
173 r_ports=( $(openstack port list --router ${router} -f value -c ID) )
174 if [ ${#r_ports[@]} -eq 0 ]; then
Alex Savatieiev788193b2019-03-12 11:39:08 -0500175 echo "... no ports to unplug for ${router}"
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200176 else
177 for r_port in ${r_ports[@]}; do
Alex Savatieiev788193b2019-03-12 11:39:08 -0500178 echo "... queued removal of port '${r_port}' from router '${router}'"
179 echo "router remove port ${router} ${r_port}" >>${cmds}
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200180 done
181 fi
182 done
Alex Savatieiev788193b2019-03-12 11:39:08 -0500183 _clean_and_flush
184
185 echo "... deleting routers"
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200186 printf "%s\n" ${routers[@]} | xargs -I{} echo router delete {} >>${cmds}
Alex Savatieiev788193b2019-03-12 11:39:08 -0500187 _clean_and_flush
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200188 fi
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600189
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200190 networks=( $(openstack network list | grep "${mask}" | cut -d' ' -f2) )
191 if [ ${#networks[@]} -eq 0 ]; then
192 echo "-> No networks containing '${mask}' found"
193 else
194 ports=()
195 subnets=()
196 for((idx=0;idx<${#networks[@]};idx++)) do
197 ports+=( $(openstack port list --network ${networks[idx]} -c ID -f value) )
198 subnets+=( $(openstack subnet list --network ${networks[idx]} -c ID -f value) )
199 echo "-> $((${idx}+1)) of ${#networks[@]}, total ${#ports[@]} ports, ${#subnets[@]} subnets"
200 done
201 printf "%s\n" ${ports[@]} | xargs -I{} echo port delete {} >>${cmds}
202 printf "%s\n" ${subnets[@]} | xargs -I{} echo subnet delete {} >>${cmds}
203 echo network delete ${networks[@]} >>${cmds}
204 echo "-> ${#routers[@]} routers, ${#ports[@]} ports, ${#subnets[@]} subnets, ${#networks[@]} networks"
205 fi
206 _clean_and_flush
207}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600208
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200209### Regions
210function _clean_regions {
211 regions=( $(openstack region list -c Region -f value | grep ${mask}) )
212 echo "-> ${#regions[@]} regions containing '${mask}' found"
213 printf "%s\n" ${regions[@]} | xargs -I{} echo region delete {} >>${cmds}
214 _clean_and_flush
215}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600216
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200217### Stacks
218function _clean_stacks {
219 # By default openstack denies use of global_index for everyone.
220 # In case you want to have handy cleanup, consider updating policy.json here:
221 # root@ctl0x:~# cat -n /etc/heat/policy.json | grep global_index
222 # 48 "stacks:global_index": "rule:deny_everybody",
223 # 73 "software_configs:global_index": "rule:deny_everybody",
224 # After this you will be able to use --all option
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600225
Oleksandr Savatieiev722396b2019-01-29 12:53:21 +0100226 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 +0200227 echo "-> ${#stacks[@]} stacks containing '${mask}' found"
228 printf "%s\n" ${stacks[@]} | xargs -I{} echo stack check {} >>${cmds}
229 printf "%s\n" ${stacks[@]} | xargs -I{} echo stack delete -y {} >>${cmds}
230 _clean_and_flush
Oleksandr Savatieiev722396b2019-01-29 12:53:21 +0100231 if [ "$purge_deleted_stacks" = true ]; then
Alex Savatieiev788193b2019-03-12 11:39:08 -0500232 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 +0100233 fi
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200234}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600235
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200236### Containers
237function _clean_containers {
238 containers=( $(openstack container list --all -c ID -c Name -f value | grep ${mask}) )
239 echo "-> ${#containers[@]} containers containing '${mask}' found"
240 printf "%s\n" ${containers[@]} | xargs -I{} echo container delete {} >>${cmds}
241 _clean_and_flush
242}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600243
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200244###################
245### Main
246###################
247# temp file for commands
248cmds=$(mktemp)
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +0200249trap "rm -f ${cmds}" EXIT
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200250echo "Using tempfile: '${cmds}'"
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600251
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200252# Consider cleaning contrail resources carefully
253# ...and only after that - clean projects
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600254
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200255_clean_stacks
256_clean_servers
257
258_clean_users
259_clean_roles
260_clean_snapshots
261_clean_volumes
262_clean_volume_types
263_clean_images
264_clean_sec_groups
265_clean_keypairs
266_clean_routers_and_networks
267_clean_regions
268_clean_containers
269
270# project cleaning disabled by default
271# Coz cleaning Contrail with no projects is a hard task
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +0200272if [ "$clean_projects" = true ]; then
273 _clean_projects
274fi
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200275
276# remove temp file
277rm ${cmds}