blob: 15e7ef9c83bca779a22db554ca457ffd683aceae [file] [log] [blame]
Oleksii Zhurba1580fc52017-11-14 15:20:44 -06001#!/bin/bash
Oleksii Zhurba1580fc52017-11-14 15:20:44 -06002export OS_INTERFACE='admin'
Oleksii Zhurba1580fc52017-11-14 15:20:44 -06003mask='rally_\|tempest_\|tempest-'
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +02004dry_run=false
5clean_projects=false
6
7function show_help {
8 printf "Resource cleaning script\nMask is: %s\n\t-h, -?\tShow this help\n\t-t\tDry run mode, no cleaning done\n\t-P\tForce cleaning of projects\n" ${mask}
9}
10
11OPTIND=1 # Reset in case getopts has been used previously in the shell.
12while getopts "h?:tP" opt; do
13 case "$opt" in
14 h|\?)
15 show_help
16 exit 0
17 ;;
18 t) dry_run=true
19 printf "Running in dry-run mode\n"
20 ;;
21 P) clean_projects=true
22 printf "Project cleanning enabled\n"
23 ;;
24 esac
25done
26
27shift $((OPTIND-1))
28[ "${1:-}" = "--" ] && shift
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060029
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020030### Execute collected commands and flush the temp file
31function _clean_and_flush {
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +020032 if [ "$dry_run" = true ] ; then
33 return 0
34 fi
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020035 if [ -s ${cmds} ]; then
36 echo "Processing $(cat ${cmds} | wc -l) commands"
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +020037 #cat ${cmds} | openstack
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020038 truncate -s 0 ${cmds}
39 fi
40}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060041
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020042### Users
43function _clean_users {
44 users=( $(openstack user list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
45 echo "-> ${#users[@]} users containing '${mask}' found"
46 printf "%s\n" ${users[@]} | xargs -I{} echo user delete {} >>${cmds}
47 _clean_and_flush
48}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060049
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020050### Roles
51function _clean_roles {
52 roles=( $(openstack role list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
53 echo "-> ${#roles[@]} roles containing '${mask}' found"
54 printf "%s\n" ${roles[@]} | xargs -I{} echo role delete {} >>${cmds}
55 _clean_and_flush
56}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060057
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020058### Projects
59function _clean_projects {
60 projects=( $(openstack project list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
61 echo "-> ${#projects[@]} projects containing '${mask}' found"
62 printf "%s\n" ${projects[@]} | xargs -I{} echo project delete {} >>${cmds}
63 _clean_and_flush
64}
Valentyn Khalinbdf3ba92018-07-13 18:14:20 +030065
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020066### Servers
67function _clean_servers {
68 servers=( $(openstack server list --name ${mask} -c ID -f value) )
69 echo "-> ${#servers[@]} servers containing '${mask}' found"
70 printf "%s\n" ${servers[@]} | xargs -I{} echo server delete {} >>${cmds}
71 _clean_and_flush
72}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060073
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020074### Reset snapshot state and delete
75function _clean_snapshots {
76 snapshots=( $(openstack volume snapshot list --all -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
77 echo "-> ${#snapshots[@]} snapshots containing '${mask}' found"
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +020078 printf "%s\n" ${snapshots[@]} | xargs -I{} echo volume snapshot set --state available {} >>${cmds}
79 printf "%s\n" ${snapshots[@]} | xargs -I{} echo volume snapshot delete {} >>${cmds}
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020080 _clean_and_flush
81}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060082
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020083function _clean_volumes {
84 volumes=( $(openstack volume list --all -c ID -c Name -c Type -f value | grep ${mask} | cut -d' ' -f1) )
85 echo "-> ${#volumes[@]} volumes containing '${mask}' found"
86 printf "%s\n" ${volumes[@]} | xargs -I{} echo volume set --state available {} >>${cmds}
87 printf "%s\n" ${volumes[@]} | xargs -I{} echo volume delete {} >>${cmds}
88 _clean_and_flush
89}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060090
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020091### Volume types
92function _clean_volume_types {
93 vtypes=( $(openstack volume type list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
94 echo "-> ${#vtypes[@]} volume types containing '${mask}' found"
95 printf "%s\n" ${vtypes[@]} | xargs -I{} echo volume type delete {} >>${cmds}
96 _clean_and_flush
97}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060098
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020099### Images
100function _clean_images {
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +0200101 images=( $(openstack image list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200102 echo "-> ${#images[@]} images containing '${mask}' found"
103 printf "%s\n" ${images[@]} | xargs -I{} echo image delete {} >>${cmds}
104 _clean_and_flush
105}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600106
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200107### Sec groups
108function _clean_sec_groups {
109# openstack project list -c ID -c Name -f value | grep rally | cut -d' ' -f1 | xargs -I{} /bin/bash -c "openstack security group list | grep {}"
110 projects=( $(openstack project list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
111 sgroups=( $(printf "%s\n" ${projects[@]} | xargs -I{} /bin/bash -c "openstack security group list -c ID -c Project -f value | grep {} | cut -d' ' -f1") )
112 echo "-> ${#sgroups[@]} security groups for project containing '${mask}' found"
113 printf "%s\n" ${sgroups[@]} | xargs -I{} echo security group delete {} >>${cmds}
114 _clean_and_flush
115}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600116
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200117### Keypairs
118function _clean_keypairs {
119 keypairs=( $(openstack keypair list -c Name -f value | grep ${mask}) )
120 echo "-> ${#keypairs[@]} keypairs containing '${mask}' found"
121 printf "%s\n" ${keypairs[@]} | xargs -I{} echo keypair delete {} >>${cmds}
122 _clean_and_flush
123}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600124
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200125### Routers and Networks
126function _clean_routers_and_networks {
127 routers=( $(openstack router list -c ID -c Name -f value | grep ${mask} | cut -d ' ' -f1) )
128 if [ ${#routers[@]} -eq 0 ]; then
129 echo "-> No routers containing '${mask}' found"
130 else
131 echo "-> ${#routers[@]} routers containing '${mask}' found"
132 echo "...unsetting gateways"
133 printf "%s\n" ${routers[@]} | xargs -I{} echo router unset --external-gateway {} | openstack
134 for router in ${routers[@]}; do
135 r_ports=( $(openstack port list --router ${router} -f value -c ID) )
136 if [ ${#r_ports[@]} -eq 0 ]; then
137 echo "...no ports to unplug for ${router}"
138 else
139 for r_port in ${r_ports[@]}; do
140 echo "...removing port '${r_port}' from router '${router}'"
141 openstack router remove port ${router} ${r_port}
142 done
143 fi
144 done
145 printf "%s\n" ${routers[@]} | xargs -I{} echo router delete {} >>${cmds}
146 fi
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600147
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200148 networks=( $(openstack network list | grep "${mask}" | cut -d' ' -f2) )
149 if [ ${#networks[@]} -eq 0 ]; then
150 echo "-> No networks containing '${mask}' found"
151 else
152 ports=()
153 subnets=()
154 for((idx=0;idx<${#networks[@]};idx++)) do
155 ports+=( $(openstack port list --network ${networks[idx]} -c ID -f value) )
156 subnets+=( $(openstack subnet list --network ${networks[idx]} -c ID -f value) )
157 echo "-> $((${idx}+1)) of ${#networks[@]}, total ${#ports[@]} ports, ${#subnets[@]} subnets"
158 done
159 printf "%s\n" ${ports[@]} | xargs -I{} echo port delete {} >>${cmds}
160 printf "%s\n" ${subnets[@]} | xargs -I{} echo subnet delete {} >>${cmds}
161 echo network delete ${networks[@]} >>${cmds}
162 echo "-> ${#routers[@]} routers, ${#ports[@]} ports, ${#subnets[@]} subnets, ${#networks[@]} networks"
163 fi
164 _clean_and_flush
165}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600166
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200167### Regions
168function _clean_regions {
169 regions=( $(openstack region list -c Region -f value | grep ${mask}) )
170 echo "-> ${#regions[@]} regions containing '${mask}' found"
171 printf "%s\n" ${regions[@]} | xargs -I{} echo region delete {} >>${cmds}
172 _clean_and_flush
173}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600174
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200175### Stacks
176function _clean_stacks {
177 # By default openstack denies use of global_index for everyone.
178 # In case you want to have handy cleanup, consider updating policy.json here:
179 # root@ctl0x:~# cat -n /etc/heat/policy.json | grep global_index
180 # 48 "stacks:global_index": "rule:deny_everybody",
181 # 73 "software_configs:global_index": "rule:deny_everybody",
182 # After this you will be able to use --all option
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600183
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200184 stacks=( $(openstack stack list --deleted --nested --hidden -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
185 echo "-> ${#stacks[@]} stacks containing '${mask}' found"
186 printf "%s\n" ${stacks[@]} | xargs -I{} echo stack check {} >>${cmds}
187 printf "%s\n" ${stacks[@]} | xargs -I{} echo stack delete -y {} >>${cmds}
188 _clean_and_flush
189}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600190
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200191### Containers
192function _clean_containers {
193 containers=( $(openstack container list --all -c ID -c Name -f value | grep ${mask}) )
194 echo "-> ${#containers[@]} containers containing '${mask}' found"
195 printf "%s\n" ${containers[@]} | xargs -I{} echo container delete {} >>${cmds}
196 _clean_and_flush
197}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600198
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200199###################
200### Main
201###################
202# temp file for commands
203cmds=$(mktemp)
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +0200204trap "rm -f ${cmds}" EXIT
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200205echo "Using tempfile: '${cmds}'"
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600206
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200207# Consider cleaning contrail resources carefully
208# ...and only after that - clean projects
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600209
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200210_clean_stacks
211_clean_servers
212
213_clean_users
214_clean_roles
215_clean_snapshots
216_clean_volumes
217_clean_volume_types
218_clean_images
219_clean_sec_groups
220_clean_keypairs
221_clean_routers_and_networks
222_clean_regions
223_clean_containers
224
225# project cleaning disabled by default
226# Coz cleaning Contrail with no projects is a hard task
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +0200227if [ "$clean_projects" = true ]; then
228 _clean_projects
229fi
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200230
231# remove temp file
232rm ${cmds}