blob: 3b6c112329de806b79a67518216a1ca073468c2a [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-'
Oleksii Zhurba0aa46702018-02-07 16:17:39 -06004
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +02005### Execute collected commands and flush the temp file
6function _clean_and_flush {
7 if [ -s ${cmds} ]; then
8 echo "Processing $(cat ${cmds} | wc -l) commands"
9 cat ${cmds} | openstack
10 truncate -s 0 ${cmds}
11 fi
12}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060013
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020014### Users
15function _clean_users {
16 users=( $(openstack user list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
17 echo "-> ${#users[@]} users containing '${mask}' found"
18 printf "%s\n" ${users[@]} | xargs -I{} echo user delete {} >>${cmds}
19 _clean_and_flush
20}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060021
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020022### Roles
23function _clean_roles {
24 roles=( $(openstack role list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
25 echo "-> ${#roles[@]} roles containing '${mask}' found"
26 printf "%s\n" ${roles[@]} | xargs -I{} echo role delete {} >>${cmds}
27 _clean_and_flush
28}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060029
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020030### Projects
31function _clean_projects {
32 projects=( $(openstack project list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
33 echo "-> ${#projects[@]} projects containing '${mask}' found"
34 printf "%s\n" ${projects[@]} | xargs -I{} echo project delete {} >>${cmds}
35 _clean_and_flush
36}
Valentyn Khalinbdf3ba92018-07-13 18:14:20 +030037
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020038### Servers
39function _clean_servers {
40 servers=( $(openstack server list --name ${mask} -c ID -f value) )
41 echo "-> ${#servers[@]} servers containing '${mask}' found"
42 printf "%s\n" ${servers[@]} | xargs -I{} echo server delete {} >>${cmds}
43 _clean_and_flush
44}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060045
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020046### Reset snapshot state and delete
47function _clean_snapshots {
48 snapshots=( $(openstack volume snapshot list --all -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
49 echo "-> ${#snapshots[@]} snapshots containing '${mask}' found"
50 printf "%s\n" ${snapshots[@]} | xargs -I{} echo snapshot set --state available {} >>${cmds}
51 printf "%s\n" ${snapshots[@]} | xargs -I{} echo snapshot delete {} >>${cmds}
52 _clean_and_flush
53}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060054
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020055function _clean_volumes {
56 volumes=( $(openstack volume list --all -c ID -c Name -c Type -f value | grep ${mask} | cut -d' ' -f1) )
57 echo "-> ${#volumes[@]} volumes containing '${mask}' found"
58 printf "%s\n" ${volumes[@]} | xargs -I{} echo volume set --state available {} >>${cmds}
59 printf "%s\n" ${volumes[@]} | xargs -I{} echo volume delete {} >>${cmds}
60 _clean_and_flush
61}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060062
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020063### Volume types
64function _clean_volume_types {
65 vtypes=( $(openstack volume type list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
66 echo "-> ${#vtypes[@]} volume types containing '${mask}' found"
67 printf "%s\n" ${vtypes[@]} | xargs -I{} echo volume type delete {} >>${cmds}
68 _clean_and_flush
69}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060070
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020071### Images
72function _clean_images {
73 images=( $(openstack image list --name ${mask} -c ID -f value) )
74 echo "-> ${#images[@]} images containing '${mask}' found"
75 printf "%s\n" ${images[@]} | xargs -I{} echo image delete {} >>${cmds}
76 _clean_and_flush
77}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060078
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020079### Sec groups
80function _clean_sec_groups {
81# openstack project list -c ID -c Name -f value | grep rally | cut -d' ' -f1 | xargs -I{} /bin/bash -c "openstack security group list | grep {}"
82 projects=( $(openstack project list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
83 sgroups=( $(printf "%s\n" ${projects[@]} | xargs -I{} /bin/bash -c "openstack security group list -c ID -c Project -f value | grep {} | cut -d' ' -f1") )
84 echo "-> ${#sgroups[@]} security groups for project containing '${mask}' found"
85 printf "%s\n" ${sgroups[@]} | xargs -I{} echo security group delete {} >>${cmds}
86 _clean_and_flush
87}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060088
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020089### Keypairs
90function _clean_keypairs {
91 keypairs=( $(openstack keypair list -c Name -f value | grep ${mask}) )
92 echo "-> ${#keypairs[@]} keypairs containing '${mask}' found"
93 printf "%s\n" ${keypairs[@]} | xargs -I{} echo keypair delete {} >>${cmds}
94 _clean_and_flush
95}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060096
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020097### Routers and Networks
98function _clean_routers_and_networks {
99 routers=( $(openstack router list -c ID -c Name -f value | grep ${mask} | cut -d ' ' -f1) )
100 if [ ${#routers[@]} -eq 0 ]; then
101 echo "-> No routers containing '${mask}' found"
102 else
103 echo "-> ${#routers[@]} routers containing '${mask}' found"
104 echo "...unsetting gateways"
105 printf "%s\n" ${routers[@]} | xargs -I{} echo router unset --external-gateway {} | openstack
106 for router in ${routers[@]}; do
107 r_ports=( $(openstack port list --router ${router} -f value -c ID) )
108 if [ ${#r_ports[@]} -eq 0 ]; then
109 echo "...no ports to unplug for ${router}"
110 else
111 for r_port in ${r_ports[@]}; do
112 echo "...removing port '${r_port}' from router '${router}'"
113 openstack router remove port ${router} ${r_port}
114 done
115 fi
116 done
117 printf "%s\n" ${routers[@]} | xargs -I{} echo router delete {} >>${cmds}
118 fi
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600119
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200120 networks=( $(openstack network list | grep "${mask}" | cut -d' ' -f2) )
121 if [ ${#networks[@]} -eq 0 ]; then
122 echo "-> No networks containing '${mask}' found"
123 else
124 ports=()
125 subnets=()
126 for((idx=0;idx<${#networks[@]};idx++)) do
127 ports+=( $(openstack port list --network ${networks[idx]} -c ID -f value) )
128 subnets+=( $(openstack subnet list --network ${networks[idx]} -c ID -f value) )
129 echo "-> $((${idx}+1)) of ${#networks[@]}, total ${#ports[@]} ports, ${#subnets[@]} subnets"
130 done
131 printf "%s\n" ${ports[@]} | xargs -I{} echo port delete {} >>${cmds}
132 printf "%s\n" ${subnets[@]} | xargs -I{} echo subnet delete {} >>${cmds}
133 echo network delete ${networks[@]} >>${cmds}
134 echo "-> ${#routers[@]} routers, ${#ports[@]} ports, ${#subnets[@]} subnets, ${#networks[@]} networks"
135 fi
136 _clean_and_flush
137}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600138
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200139### Regions
140function _clean_regions {
141 regions=( $(openstack region list -c Region -f value | grep ${mask}) )
142 echo "-> ${#regions[@]} regions containing '${mask}' found"
143 printf "%s\n" ${regions[@]} | xargs -I{} echo region delete {} >>${cmds}
144 _clean_and_flush
145}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600146
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200147### Stacks
148function _clean_stacks {
149 # By default openstack denies use of global_index for everyone.
150 # In case you want to have handy cleanup, consider updating policy.json here:
151 # root@ctl0x:~# cat -n /etc/heat/policy.json | grep global_index
152 # 48 "stacks:global_index": "rule:deny_everybody",
153 # 73 "software_configs:global_index": "rule:deny_everybody",
154 # After this you will be able to use --all option
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600155
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200156 stacks=( $(openstack stack list --deleted --nested --hidden -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
157 echo "-> ${#stacks[@]} stacks containing '${mask}' found"
158 printf "%s\n" ${stacks[@]} | xargs -I{} echo stack check {} >>${cmds}
159 printf "%s\n" ${stacks[@]} | xargs -I{} echo stack delete -y {} >>${cmds}
160 _clean_and_flush
161}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600162
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200163### Containers
164function _clean_containers {
165 containers=( $(openstack container list --all -c ID -c Name -f value | grep ${mask}) )
166 echo "-> ${#containers[@]} containers containing '${mask}' found"
167 printf "%s\n" ${containers[@]} | xargs -I{} echo container delete {} >>${cmds}
168 _clean_and_flush
169}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600170
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200171###################
172### Main
173###################
174# temp file for commands
175cmds=$(mktemp)
176echo "Using tempfile: '${cmds}'"
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600177
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200178# Consider cleaning contrail resources carefully
179# ...and only after that - clean projects
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600180
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200181_clean_stacks
182_clean_servers
183
184_clean_users
185_clean_roles
186_clean_snapshots
187_clean_volumes
188_clean_volume_types
189_clean_images
190_clean_sec_groups
191_clean_keypairs
192_clean_routers_and_networks
193_clean_regions
194_clean_containers
195
196# project cleaning disabled by default
197# Coz cleaning Contrail with no projects is a hard task
198#_clean_projects
199
200# remove temp file
201rm ${cmds}