Oleksii Zhurba | 1580fc5 | 2017-11-14 15:20:44 -0600 | [diff] [blame] | 1 | #!/bin/bash |
Oleksii Zhurba | 1580fc5 | 2017-11-14 15:20:44 -0600 | [diff] [blame] | 2 | export OS_INTERFACE='admin' |
Oleksii Zhurba | 1580fc5 | 2017-11-14 15:20:44 -0600 | [diff] [blame] | 3 | mask='rally_\|tempest_\|tempest-' |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 4 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 5 | ### Execute collected commands and flush the temp file |
| 6 | function _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 Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 13 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 14 | ### Users |
| 15 | function _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 Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 21 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 22 | ### Roles |
| 23 | function _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 Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 29 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 30 | ### Projects |
| 31 | function _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 Khalin | bdf3ba9 | 2018-07-13 18:14:20 +0300 | [diff] [blame] | 37 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 38 | ### Servers |
| 39 | function _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 Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 45 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 46 | ### Reset snapshot state and delete |
| 47 | function _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 Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 54 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 55 | function _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 Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 62 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 63 | ### Volume types |
| 64 | function _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 Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 70 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 71 | ### Images |
| 72 | function _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 Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 78 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 79 | ### Sec groups |
| 80 | function _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 Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 88 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 89 | ### Keypairs |
| 90 | function _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 Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 96 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 97 | ### Routers and Networks |
| 98 | function _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 Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 119 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 120 | 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 Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 138 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 139 | ### Regions |
| 140 | function _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 Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 146 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 147 | ### Stacks |
| 148 | function _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 Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 155 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 156 | 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 Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 162 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 163 | ### Containers |
| 164 | function _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 Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 170 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 171 | ################### |
| 172 | ### Main |
| 173 | ################### |
| 174 | # temp file for commands |
| 175 | cmds=$(mktemp) |
| 176 | echo "Using tempfile: '${cmds}'" |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 177 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 178 | # Consider cleaning contrail resources carefully |
| 179 | # ...and only after that - clean projects |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 180 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 181 | _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 |
| 201 | rm ${cmds} |