blob: 05d154feab777d708d310bcd11edfdf8cc5bd9e8 [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
Oleksandr Savatieiev41df4eb2018-11-08 16:22:30 +01006make_servers_active=false
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +02007
8function show_help {
9 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}
10}
11
12OPTIND=1 # Reset in case getopts has been used previously in the shell.
13while getopts "h?:tP" opt; do
14 case "$opt" in
15 h|\?)
16 show_help
17 exit 0
18 ;;
19 t) dry_run=true
20 printf "Running in dry-run mode\n"
21 ;;
Oleksandr Savatieiev41df4eb2018-11-08 16:22:30 +010022 S) make_servers_active=true
23 printf "Servers will be set to ACTIVE before deletion\n"
24 ;;
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +020025 P) clean_projects=true
26 printf "Project cleanning enabled\n"
27 ;;
28 esac
29done
30
31shift $((OPTIND-1))
32[ "${1:-}" = "--" ] && shift
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060033
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020034### Execute collected commands and flush the temp file
35function _clean_and_flush {
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +020036 if [ "$dry_run" = true ] ; then
37 return 0
38 fi
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020039 if [ -s ${cmds} ]; then
40 echo "Processing $(cat ${cmds} | wc -l) commands"
Oleksandr Savatieiev41df4eb2018-11-08 16:22:30 +010041 cat ${cmds} | openstack
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020042 truncate -s 0 ${cmds}
43 fi
44}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060045
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020046### Users
47function _clean_users {
48 users=( $(openstack user list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
49 echo "-> ${#users[@]} users containing '${mask}' found"
50 printf "%s\n" ${users[@]} | xargs -I{} echo user delete {} >>${cmds}
51 _clean_and_flush
52}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060053
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020054### Roles
55function _clean_roles {
56 roles=( $(openstack role list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
57 echo "-> ${#roles[@]} roles containing '${mask}' found"
58 printf "%s\n" ${roles[@]} | xargs -I{} echo role delete {} >>${cmds}
59 _clean_and_flush
60}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060061
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020062### Projects
63function _clean_projects {
64 projects=( $(openstack project list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
65 echo "-> ${#projects[@]} projects containing '${mask}' found"
66 printf "%s\n" ${projects[@]} | xargs -I{} echo project delete {} >>${cmds}
67 _clean_and_flush
68}
Valentyn Khalinbdf3ba92018-07-13 18:14:20 +030069
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020070### Servers
71function _clean_servers {
72 servers=( $(openstack server list --name ${mask} -c ID -f value) )
73 echo "-> ${#servers[@]} servers containing '${mask}' found"
Oleksandr Savatieiev41df4eb2018-11-08 16:22:30 +010074 if [ "$make_servers_active" = true ]; then
75 printf "%s\n" ${servers[@]} | xargs -I{} echo server set --state active {} >>${cmds}
76 fi
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020077 printf "%s\n" ${servers[@]} | xargs -I{} echo server delete {} >>${cmds}
78 _clean_and_flush
79}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060080
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020081### Reset snapshot state and delete
82function _clean_snapshots {
83 snapshots=( $(openstack volume snapshot list --all -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
84 echo "-> ${#snapshots[@]} snapshots containing '${mask}' found"
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +020085 printf "%s\n" ${snapshots[@]} | xargs -I{} echo volume snapshot set --state available {} >>${cmds}
86 printf "%s\n" ${snapshots[@]} | xargs -I{} echo volume snapshot delete {} >>${cmds}
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020087 _clean_and_flush
88}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060089
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020090function _clean_volumes {
91 volumes=( $(openstack volume list --all -c ID -c Name -c Type -f value | grep ${mask} | cut -d' ' -f1) )
92 echo "-> ${#volumes[@]} volumes containing '${mask}' found"
93 printf "%s\n" ${volumes[@]} | xargs -I{} echo volume set --state available {} >>${cmds}
94 printf "%s\n" ${volumes[@]} | xargs -I{} echo volume delete {} >>${cmds}
95 _clean_and_flush
96}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -060097
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +020098### Volume types
99function _clean_volume_types {
100 vtypes=( $(openstack volume type list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
101 echo "-> ${#vtypes[@]} volume types containing '${mask}' found"
102 printf "%s\n" ${vtypes[@]} | xargs -I{} echo volume type delete {} >>${cmds}
103 _clean_and_flush
104}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600105
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200106### Images
107function _clean_images {
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +0200108 images=( $(openstack image list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200109 echo "-> ${#images[@]} images containing '${mask}' found"
110 printf "%s\n" ${images[@]} | xargs -I{} echo image delete {} >>${cmds}
111 _clean_and_flush
112}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600113
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200114### Sec groups
115function _clean_sec_groups {
116# openstack project list -c ID -c Name -f value | grep rally | cut -d' ' -f1 | xargs -I{} /bin/bash -c "openstack security group list | grep {}"
117 projects=( $(openstack project list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
118 sgroups=( $(printf "%s\n" ${projects[@]} | xargs -I{} /bin/bash -c "openstack security group list -c ID -c Project -f value | grep {} | cut -d' ' -f1") )
119 echo "-> ${#sgroups[@]} security groups for project containing '${mask}' found"
120 printf "%s\n" ${sgroups[@]} | xargs -I{} echo security group delete {} >>${cmds}
121 _clean_and_flush
122}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600123
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200124### Keypairs
125function _clean_keypairs {
126 keypairs=( $(openstack keypair list -c Name -f value | grep ${mask}) )
127 echo "-> ${#keypairs[@]} keypairs containing '${mask}' found"
128 printf "%s\n" ${keypairs[@]} | xargs -I{} echo keypair delete {} >>${cmds}
129 _clean_and_flush
130}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600131
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200132### Routers and Networks
133function _clean_routers_and_networks {
134 routers=( $(openstack router list -c ID -c Name -f value | grep ${mask} | cut -d ' ' -f1) )
135 if [ ${#routers[@]} -eq 0 ]; then
136 echo "-> No routers containing '${mask}' found"
137 else
138 echo "-> ${#routers[@]} routers containing '${mask}' found"
139 echo "...unsetting gateways"
140 printf "%s\n" ${routers[@]} | xargs -I{} echo router unset --external-gateway {} | openstack
141 for router in ${routers[@]}; do
142 r_ports=( $(openstack port list --router ${router} -f value -c ID) )
143 if [ ${#r_ports[@]} -eq 0 ]; then
144 echo "...no ports to unplug for ${router}"
145 else
146 for r_port in ${r_ports[@]}; do
147 echo "...removing port '${r_port}' from router '${router}'"
148 openstack router remove port ${router} ${r_port}
149 done
150 fi
151 done
152 printf "%s\n" ${routers[@]} | xargs -I{} echo router delete {} >>${cmds}
153 fi
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600154
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200155 networks=( $(openstack network list | grep "${mask}" | cut -d' ' -f2) )
156 if [ ${#networks[@]} -eq 0 ]; then
157 echo "-> No networks containing '${mask}' found"
158 else
159 ports=()
160 subnets=()
161 for((idx=0;idx<${#networks[@]};idx++)) do
162 ports+=( $(openstack port list --network ${networks[idx]} -c ID -f value) )
163 subnets+=( $(openstack subnet list --network ${networks[idx]} -c ID -f value) )
164 echo "-> $((${idx}+1)) of ${#networks[@]}, total ${#ports[@]} ports, ${#subnets[@]} subnets"
165 done
166 printf "%s\n" ${ports[@]} | xargs -I{} echo port delete {} >>${cmds}
167 printf "%s\n" ${subnets[@]} | xargs -I{} echo subnet delete {} >>${cmds}
168 echo network delete ${networks[@]} >>${cmds}
169 echo "-> ${#routers[@]} routers, ${#ports[@]} ports, ${#subnets[@]} subnets, ${#networks[@]} networks"
170 fi
171 _clean_and_flush
172}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600173
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200174### Regions
175function _clean_regions {
176 regions=( $(openstack region list -c Region -f value | grep ${mask}) )
177 echo "-> ${#regions[@]} regions containing '${mask}' found"
178 printf "%s\n" ${regions[@]} | xargs -I{} echo region delete {} >>${cmds}
179 _clean_and_flush
180}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600181
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200182### Stacks
183function _clean_stacks {
184 # By default openstack denies use of global_index for everyone.
185 # In case you want to have handy cleanup, consider updating policy.json here:
186 # root@ctl0x:~# cat -n /etc/heat/policy.json | grep global_index
187 # 48 "stacks:global_index": "rule:deny_everybody",
188 # 73 "software_configs:global_index": "rule:deny_everybody",
189 # After this you will be able to use --all option
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600190
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200191 stacks=( $(openstack stack list --deleted --nested --hidden -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) )
192 echo "-> ${#stacks[@]} stacks containing '${mask}' found"
193 printf "%s\n" ${stacks[@]} | xargs -I{} echo stack check {} >>${cmds}
194 printf "%s\n" ${stacks[@]} | xargs -I{} echo stack delete -y {} >>${cmds}
195 _clean_and_flush
196}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600197
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200198### Containers
199function _clean_containers {
200 containers=( $(openstack container list --all -c ID -c Name -f value | grep ${mask}) )
201 echo "-> ${#containers[@]} containers containing '${mask}' found"
202 printf "%s\n" ${containers[@]} | xargs -I{} echo container delete {} >>${cmds}
203 _clean_and_flush
204}
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600205
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200206###################
207### Main
208###################
209# temp file for commands
210cmds=$(mktemp)
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +0200211trap "rm -f ${cmds}" EXIT
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200212echo "Using tempfile: '${cmds}'"
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600213
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200214# Consider cleaning contrail resources carefully
215# ...and only after that - clean projects
Oleksii Zhurba0aa46702018-02-07 16:17:39 -0600216
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200217_clean_stacks
218_clean_servers
219
220_clean_users
221_clean_roles
222_clean_snapshots
223_clean_volumes
224_clean_volume_types
225_clean_images
226_clean_sec_groups
227_clean_keypairs
228_clean_routers_and_networks
229_clean_regions
230_clean_containers
231
232# project cleaning disabled by default
233# Coz cleaning Contrail with no projects is a hard task
Oleksandr Savatieievc1bbead2018-10-18 14:39:25 +0200234if [ "$clean_projects" = true ]; then
235 _clean_projects
236fi
Oleksandr Savatieiev91355c82018-09-07 12:14:34 +0200237
238# remove temp file
239rm ${cmds}