blob: 9638842e94bb30bbedd7c87079feb4a0fa9b4218 [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
Oleksandr Savatieiev722396b2019-01-29 12:53:21 +01008stack_batch_size=10
9# 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"
17 printf "\t-S\tSet servers to ACTIVE before deletion (bare metal reqiured)\n"
18 printf "\t-F\tForce purge deleted stacks. Batch size: %s, >%s %s\n" ${stack_batch_size} ${stack_granularity_value} ${stack_granularity}
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +020019}
20
21OPTIND=1 # Reset in case getopts has been used previously in the shell.
Oleksandr Savatieiev722396b2019-01-29 12:53:21 +010022while getopts "h?:tSPF" opt; do
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +020023 case "$opt" in
24 h|\?)
25 show_help
26 exit 0
27 ;;
28 t) dry_run=true
29 printf "Running in dry-run mode\n"
30 ;;
Oleksandr Savatieiev41df4eb2018-11-08 16:22:30 +010031 S) make_servers_active=true
32 printf "Servers will be set to ACTIVE before deletion\n"
33 ;;
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +020034 P) clean_projects=true
35 printf "Project cleanning enabled\n"
36 ;;
Oleksandr Savatieiev722396b2019-01-29 12:53:21 +010037 F) purge_deleted_stacks=true
38 printf "Purging stacks deleted >$stack_granularity_value $stack_granularity ago enabled, batch size %s\n" $stack_batch_size
39 ;;
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +020040 esac
41done
42
43shift $((OPTIND-1))
44[ "${1:-}" = "--" ] && shift
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060045
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020046### Execute collected commands and flush the temp file
47function _clean_and_flush {
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +020048 if [ "$dry_run" = true ] ; then
49 return 0
50 fi
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020051 if [ -s ${cmds} ]; then
52 echo "Processing $(cat ${cmds} | wc -l) commands"
Oleksandr Savatieiev41df4eb2018-11-08 16:22:30 +010053 cat ${cmds} | openstack
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020054 truncate -s 0 ${cmds}
55 fi
56}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060057
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020058### Users
59function _clean_users {
60 users=( $(openstack user list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
61 echo "-> ${#users[@]} users containing '${mask}' found"
62 printf "%s\n" ${users[@]} | xargs -I{} echo user delete {} >>${cmds}
63 _clean_and_flush
64}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060065
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020066### Roles
67function _clean_roles {
68 roles=( $(openstack role list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
69 echo "-> ${#roles[@]} roles containing '${mask}' found"
70 printf "%s\n" ${roles[@]} | xargs -I{} echo role delete {} >>${cmds}
71 _clean_and_flush
72}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060073
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020074### Projects
75function _clean_projects {
76 projects=( $(openstack project list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
77 echo "-> ${#projects[@]} projects containing '${mask}' found"
78 printf "%s\n" ${projects[@]} | xargs -I{} echo project delete {} >>${cmds}
79 _clean_and_flush
80}
Valentyn Khalinbdf3ba92018-07-13 18:14:20 +030081
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020082### Servers
83function _clean_servers {
Oleksandr Savatieiev0a1db222019-01-29 10:31:19 +010084 servers=( $(openstack server list -c ID -c Name -f value --all | grep "${mask}" | cut -d' ' -f1) )
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020085 echo "-> ${#servers[@]} servers containing '${mask}' found"
Oleksandr Savatieiev41df4eb2018-11-08 16:22:30 +010086 if [ "$make_servers_active" = true ]; then
87 printf "%s\n" ${servers[@]} | xargs -I{} echo server set --state active {} >>${cmds}
88 fi
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020089 printf "%s\n" ${servers[@]} | xargs -I{} echo server delete {} >>${cmds}
90 _clean_and_flush
91}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060092
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020093### Reset snapshot state and delete
94function _clean_snapshots {
95 snapshots=( $(openstack volume snapshot list --all -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
96 echo "-> ${#snapshots[@]} snapshots containing '${mask}' found"
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +020097 printf "%s\n" ${snapshots[@]} | xargs -I{} echo volume snapshot set --state available {} >>${cmds}
98 printf "%s\n" ${snapshots[@]} | xargs -I{} echo volume snapshot delete {} >>${cmds}
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020099 _clean_and_flush
100}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600101
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200102function _clean_volumes {
103 volumes=( $(openstack volume list --all -c ID -c Name -c Type -f value | grep ${mask} | cut -d' ' -f1) )
104 echo "-> ${#volumes[@]} volumes containing '${mask}' found"
105 printf "%s\n" ${volumes[@]} | xargs -I{} echo volume set --state available {} >>${cmds}
106 printf "%s\n" ${volumes[@]} | xargs -I{} echo volume delete {} >>${cmds}
107 _clean_and_flush
108}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600109
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200110### Volume types
111function _clean_volume_types {
112 vtypes=( $(openstack volume type list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
113 echo "-> ${#vtypes[@]} volume types containing '${mask}' found"
114 printf "%s\n" ${vtypes[@]} | xargs -I{} echo volume type delete {} >>${cmds}
115 _clean_and_flush
116}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600117
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200118### Images
119function _clean_images {
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +0200120 images=( $(openstack image list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200121 echo "-> ${#images[@]} images containing '${mask}' found"
122 printf "%s\n" ${images[@]} | xargs -I{} echo image delete {} >>${cmds}
123 _clean_and_flush
124}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600125
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200126### Sec groups
127function _clean_sec_groups {
128# openstack project list -c ID -c Name -f value | grep rally | cut -d' ' -f1 | xargs -I{} /bin/bash -c "openstack security group list | grep {}"
129 projects=( $(openstack project list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
130 sgroups=( $(printf "%s\n" ${projects[@]} | xargs -I{} /bin/bash -c "openstack security group list -c ID -c Project -f value | grep {} | cut -d' ' -f1") )
131 echo "-> ${#sgroups[@]} security groups for project containing '${mask}' found"
132 printf "%s\n" ${sgroups[@]} | xargs -I{} echo security group delete {} >>${cmds}
133 _clean_and_flush
134}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600135
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200136### Keypairs
137function _clean_keypairs {
138 keypairs=( $(openstack keypair list -c Name -f value | grep ${mask}) )
139 echo "-> ${#keypairs[@]} keypairs containing '${mask}' found"
140 printf "%s\n" ${keypairs[@]} | xargs -I{} echo keypair delete {} >>${cmds}
141 _clean_and_flush
142}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600143
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200144### Routers and Networks
145function _clean_routers_and_networks {
146 routers=( $(openstack router list -c ID -c Name -f value | grep ${mask} | cut -d ' ' -f1) )
147 if [ ${#routers[@]} -eq 0 ]; then
148 echo "-> No routers containing '${mask}' found"
149 else
150 echo "-> ${#routers[@]} routers containing '${mask}' found"
151 echo "...unsetting gateways"
152 printf "%s\n" ${routers[@]} | xargs -I{} echo router unset --external-gateway {} | openstack
153 for router in ${routers[@]}; do
154 r_ports=( $(openstack port list --router ${router} -f value -c ID) )
155 if [ ${#r_ports[@]} -eq 0 ]; then
156 echo "...no ports to unplug for ${router}"
157 else
158 for r_port in ${r_ports[@]}; do
159 echo "...removing port '${r_port}' from router '${router}'"
160 openstack router remove port ${router} ${r_port}
161 done
162 fi
163 done
164 printf "%s\n" ${routers[@]} | xargs -I{} echo router delete {} >>${cmds}
165 fi
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600166
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200167 networks=( $(openstack network list | grep "${mask}" | cut -d' ' -f2) )
168 if [ ${#networks[@]} -eq 0 ]; then
169 echo "-> No networks containing '${mask}' found"
170 else
171 ports=()
172 subnets=()
173 for((idx=0;idx<${#networks[@]};idx++)) do
174 ports+=( $(openstack port list --network ${networks[idx]} -c ID -f value) )
175 subnets+=( $(openstack subnet list --network ${networks[idx]} -c ID -f value) )
176 echo "-> $((${idx}+1)) of ${#networks[@]}, total ${#ports[@]} ports, ${#subnets[@]} subnets"
177 done
178 printf "%s\n" ${ports[@]} | xargs -I{} echo port delete {} >>${cmds}
179 printf "%s\n" ${subnets[@]} | xargs -I{} echo subnet delete {} >>${cmds}
180 echo network delete ${networks[@]} >>${cmds}
181 echo "-> ${#routers[@]} routers, ${#ports[@]} ports, ${#subnets[@]} subnets, ${#networks[@]} networks"
182 fi
183 _clean_and_flush
184}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600185
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200186### Regions
187function _clean_regions {
188 regions=( $(openstack region list -c Region -f value | grep ${mask}) )
189 echo "-> ${#regions[@]} regions containing '${mask}' found"
190 printf "%s\n" ${regions[@]} | xargs -I{} echo region delete {} >>${cmds}
191 _clean_and_flush
192}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600193
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200194### Stacks
195function _clean_stacks {
196 # By default openstack denies use of global_index for everyone.
197 # In case you want to have handy cleanup, consider updating policy.json here:
198 # root@ctl0x:~# cat -n /etc/heat/policy.json | grep global_index
199 # 48 "stacks:global_index": "rule:deny_everybody",
200 # 73 "software_configs:global_index": "rule:deny_everybody",
201 # After this you will be able to use --all option
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600202
Oleksandr Savatieiev722396b2019-01-29 12:53:21 +0100203 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 +0200204 echo "-> ${#stacks[@]} stacks containing '${mask}' found"
205 printf "%s\n" ${stacks[@]} | xargs -I{} echo stack check {} >>${cmds}
206 printf "%s\n" ${stacks[@]} | xargs -I{} echo stack delete -y {} >>${cmds}
207 _clean_and_flush
Oleksandr Savatieiev722396b2019-01-29 12:53:21 +0100208 if [ "$purge_deleted_stacks" = true ]; then
209 heat-manage purge_deleted -g ${stack_granularity} -b ${stack_batch_size} ${stack_granularity_value} | wc -l | xargs -I{} echo "-> Purged {} stacks"
210 fi
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200211}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600212
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200213### Containers
214function _clean_containers {
215 containers=( $(openstack container list --all -c ID -c Name -f value | grep ${mask}) )
216 echo "-> ${#containers[@]} containers containing '${mask}' found"
217 printf "%s\n" ${containers[@]} | xargs -I{} echo container delete {} >>${cmds}
218 _clean_and_flush
219}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600220
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200221###################
222### Main
223###################
224# temp file for commands
225cmds=$(mktemp)
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +0200226trap "rm -f ${cmds}" EXIT
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200227echo "Using tempfile: '${cmds}'"
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600228
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200229# Consider cleaning contrail resources carefully
230# ...and only after that - clean projects
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600231
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200232_clean_stacks
233_clean_servers
234
235_clean_users
236_clean_roles
237_clean_snapshots
238_clean_volumes
239_clean_volume_types
240_clean_images
241_clean_sec_groups
242_clean_keypairs
243_clean_routers_and_networks
244_clean_regions
245_clean_containers
246
247# project cleaning disabled by default
248# Coz cleaning Contrail with no projects is a hard task
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +0200249if [ "$clean_projects" = true ]; then
250 _clean_projects
251fi
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200252
253# remove temp file
254rm ${cmds}