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' |
Alex Savatieiev | ec70369 | 2019-06-07 15:33:31 -0500 | [diff] [blame] | 3 | mask='cvp\|s_rally\|rally_\|tempest_\|tempest-' |
Alex Savatieiev | 27c2c41 | 2019-04-29 18:11:50 -0500 | [diff] [blame] | 4 | stack_alt=false |
| 5 | stack_regex='api-[0-9]+-[a-z]+' |
Oleksandr Savatieiev | c1bbead | 2018-10-18 14:39:25 +0200 | [diff] [blame] | 6 | dry_run=false |
| 7 | clean_projects=false |
Oleksandr Savatieiev | 41df4eb | 2018-11-08 16:22:30 +0100 | [diff] [blame] | 8 | make_servers_active=false |
Oleksii Zhurba | 0b736c7 | 2019-04-29 17:13:47 -0500 | [diff] [blame] | 9 | serial=false |
Alex Savatieiev | 788193b | 2019-03-12 11:39:08 -0500 | [diff] [blame] | 10 | batch_size=10 |
Oleksandr Savatieiev | 722396b | 2019-01-29 12:53:21 +0100 | [diff] [blame] | 11 | # granularity values: days,hours,minutes,seconds |
| 12 | stack_granularity=days |
| 13 | stack_granularity_value=1 |
| 14 | |
Oleksandr Savatieiev | c1bbead | 2018-10-18 14:39:25 +0200 | [diff] [blame] | 15 | function show_help { |
Oleksandr Savatieiev | 722396b | 2019-01-29 12:53:21 +0100 | [diff] [blame] | 16 | printf "Resource cleaning script\nMask is: %s\n\t-h, -?\tShow this help\n" ${mask} |
| 17 | printf "\t-t\tDry run mode, no cleaning done\n" |
| 18 | printf "\t-P\tForce cleaning of projects\n" |
Alex Savatieiev | 788193b | 2019-03-12 11:39:08 -0500 | [diff] [blame] | 19 | printf "\t-s\tUse single thread of 'openstack' client for cleanup\n" |
Oleksandr Savatieiev | 722396b | 2019-01-29 12:53:21 +0100 | [diff] [blame] | 20 | printf "\t-S\tSet servers to ACTIVE before deletion (bare metal reqiured)\n" |
Alex Savatieiev | 27c2c41 | 2019-04-29 18:11:50 -0500 | [diff] [blame] | 21 | printf "\t-f\tForce stack cleanup with an additional mask of '%s'\n" ${stack_regex} |
Alex Savatieiev | 788193b | 2019-03-12 11:39:08 -0500 | [diff] [blame] | 22 | printf "\t-F\tForce purge deleted stacks. Batch size: %s, >%s %s\n" ${batch_size} ${stack_granularity_value} ${stack_granularity} |
Oleksandr Savatieiev | c1bbead | 2018-10-18 14:39:25 +0200 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | OPTIND=1 # Reset in case getopts has been used previously in the shell. |
Alex Savatieiev | 27c2c41 | 2019-04-29 18:11:50 -0500 | [diff] [blame] | 26 | while getopts "h?:tsSPfF" opt; do |
Oleksandr Savatieiev | c1bbead | 2018-10-18 14:39:25 +0200 | [diff] [blame] | 27 | case "$opt" in |
| 28 | h|\?) |
| 29 | show_help |
| 30 | exit 0 |
| 31 | ;; |
| 32 | t) dry_run=true |
| 33 | printf "Running in dry-run mode\n" |
| 34 | ;; |
Alex Savatieiev | 788193b | 2019-03-12 11:39:08 -0500 | [diff] [blame] | 35 | s) serial=true |
| 36 | printf "Single threaded mode enabled\n" |
| 37 | ;; |
Oleksandr Savatieiev | 41df4eb | 2018-11-08 16:22:30 +0100 | [diff] [blame] | 38 | S) make_servers_active=true |
| 39 | printf "Servers will be set to ACTIVE before deletion\n" |
| 40 | ;; |
Oleksandr Savatieiev | c1bbead | 2018-10-18 14:39:25 +0200 | [diff] [blame] | 41 | P) clean_projects=true |
| 42 | printf "Project cleanning enabled\n" |
| 43 | ;; |
Alex Savatieiev | 27c2c41 | 2019-04-29 18:11:50 -0500 | [diff] [blame] | 44 | f) stack_alt=true |
| 45 | printf "Cleaning stacks using additional mask '%s'\n" ${stack_regex} |
| 46 | ;; |
Oleksandr Savatieiev | 722396b | 2019-01-29 12:53:21 +0100 | [diff] [blame] | 47 | F) purge_deleted_stacks=true |
| 48 | printf "Purging stacks deleted >$stack_granularity_value $stack_granularity ago enabled, batch size %s\n" $stack_batch_size |
| 49 | ;; |
Oleksandr Savatieiev | c1bbead | 2018-10-18 14:39:25 +0200 | [diff] [blame] | 50 | esac |
| 51 | done |
| 52 | |
| 53 | shift $((OPTIND-1)) |
| 54 | [ "${1:-}" = "--" ] && shift |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 55 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 56 | ### Execute collected commands and flush the temp file |
| 57 | function _clean_and_flush { |
Oleksandr Savatieiev | c1bbead | 2018-10-18 14:39:25 +0200 | [diff] [blame] | 58 | if [ "$dry_run" = true ] ; then |
| 59 | return 0 |
| 60 | fi |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 61 | if [ -s ${cmds} ]; then |
Oleksii Zhurba | 0b736c7 | 2019-04-29 17:13:47 -0500 | [diff] [blame] | 62 | if [ "${serial}" = false ] ; then |
Alex Savatieiev | 788193b | 2019-03-12 11:39:08 -0500 | [diff] [blame] | 63 | echo "... processing $(cat ${cmds} | wc -l) commands, worker threads ${batch_size}" |
Alex Savatieiev | 6d8fda0 | 2019-05-10 16:20:55 -0500 | [diff] [blame] | 64 | cat ${cmds} | tr '\n' '\0' | xargs -P ${batch_size} -n 1 -0 echo | openstack |
Alex Savatieiev | 788193b | 2019-03-12 11:39:08 -0500 | [diff] [blame] | 65 | #cat ${cmds} | openstack |
| 66 | truncate -s 0 ${cmds} |
| 67 | else |
| 68 | echo "... processing $(cat ${cmds} | wc -l) commands" |
Alex Savatieiev | 6d8fda0 | 2019-05-10 16:20:55 -0500 | [diff] [blame] | 69 | cat ${cmds} | tr '\n' '\0' | xargs -P 1 -n 1 -0 echo | openstack |
Oleksii Zhurba | 0b736c7 | 2019-04-29 17:13:47 -0500 | [diff] [blame] | 70 | truncate -s 0 ${cmds} |
| 71 | fi |
| 72 | fi |
| 73 | } |
| 74 | |
| 75 | function _clean_and_flush_cinder { |
| 76 | if [ "$dry_run" = true ] ; then |
| 77 | return 0 |
| 78 | fi |
| 79 | if [ -s ${cmds} ]; then |
| 80 | if [ "${serial}" = false ] ; then |
| 81 | echo "... processing $(cat ${cmds} | wc -l) commands, worker threads ${batch_size}" |
| 82 | cat ${cmds} | tr '\n' '\0' | xargs -I{} -P ${batch_size} -n 1 -0 /bin/bash -c 'cinder --os-volume-api-version 3.43 {}' |
| 83 | #cat ${cmds} | cinder --os-volume-api-version 3.43 |
| 84 | truncate -s 0 ${cmds} |
| 85 | else |
| 86 | echo "... processing $(cat ${cmds} | wc -l) commands" |
| 87 | cat ${cmds} | tr '\n' '\0' | xargs -I{} -P 1 -n 1 -0 /bin/bash -c 'cinder --os-volume-api-version 3.43 {}' |
Alex Savatieiev | 788193b | 2019-03-12 11:39:08 -0500 | [diff] [blame] | 88 | truncate -s 0 ${cmds} |
| 89 | fi |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 90 | fi |
| 91 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 92 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 93 | ### Users |
| 94 | function _clean_users { |
| 95 | users=( $(openstack user list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) ) |
| 96 | echo "-> ${#users[@]} users containing '${mask}' found" |
| 97 | printf "%s\n" ${users[@]} | xargs -I{} echo user delete {} >>${cmds} |
| 98 | _clean_and_flush |
| 99 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 100 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 101 | ### Roles |
| 102 | function _clean_roles { |
| 103 | roles=( $(openstack role list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) ) |
| 104 | echo "-> ${#roles[@]} roles containing '${mask}' found" |
| 105 | printf "%s\n" ${roles[@]} | xargs -I{} echo role delete {} >>${cmds} |
| 106 | _clean_and_flush |
| 107 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 108 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 109 | ### Projects |
| 110 | function _clean_projects { |
| 111 | projects=( $(openstack project list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) ) |
| 112 | echo "-> ${#projects[@]} projects containing '${mask}' found" |
| 113 | printf "%s\n" ${projects[@]} | xargs -I{} echo project delete {} >>${cmds} |
| 114 | _clean_and_flush |
| 115 | } |
Valentyn Khalin | bdf3ba9 | 2018-07-13 18:14:20 +0300 | [diff] [blame] | 116 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 117 | ### Servers |
| 118 | function _clean_servers { |
Oleksandr Savatieiev | 0a1db22 | 2019-01-29 10:31:19 +0100 | [diff] [blame] | 119 | servers=( $(openstack server list -c ID -c Name -f value --all | grep "${mask}" | cut -d' ' -f1) ) |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 120 | echo "-> ${#servers[@]} servers containing '${mask}' found" |
Oleksandr Savatieiev | 41df4eb | 2018-11-08 16:22:30 +0100 | [diff] [blame] | 121 | if [ "$make_servers_active" = true ]; then |
| 122 | printf "%s\n" ${servers[@]} | xargs -I{} echo server set --state active {} >>${cmds} |
| 123 | fi |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 124 | printf "%s\n" ${servers[@]} | xargs -I{} echo server delete {} >>${cmds} |
| 125 | _clean_and_flush |
| 126 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 127 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 128 | ### Reset snapshot state and delete |
| 129 | function _clean_snapshots { |
| 130 | snapshots=( $(openstack volume snapshot list --all -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) ) |
| 131 | echo "-> ${#snapshots[@]} snapshots containing '${mask}' found" |
Oleksandr Savatieiev | c1bbead | 2018-10-18 14:39:25 +0200 | [diff] [blame] | 132 | printf "%s\n" ${snapshots[@]} | xargs -I{} echo volume snapshot set --state available {} >>${cmds} |
| 133 | printf "%s\n" ${snapshots[@]} | xargs -I{} echo volume snapshot delete {} >>${cmds} |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 134 | _clean_and_flush |
| 135 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 136 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 137 | function _clean_volumes { |
| 138 | volumes=( $(openstack volume list --all -c ID -c Name -c Type -f value | grep ${mask} | cut -d' ' -f1) ) |
| 139 | echo "-> ${#volumes[@]} volumes containing '${mask}' found" |
| 140 | printf "%s\n" ${volumes[@]} | xargs -I{} echo volume set --state available {} >>${cmds} |
| 141 | printf "%s\n" ${volumes[@]} | xargs -I{} echo volume delete {} >>${cmds} |
| 142 | _clean_and_flush |
| 143 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 144 | |
Oleksii Zhurba | 0b736c7 | 2019-04-29 17:13:47 -0500 | [diff] [blame] | 145 | function _clean_volume_groups { |
| 146 | groups=( $(cinder --os-volume-api-version 3.43 group-list --all-tenants 1 | grep ${mask} | awk '{print $2}') ) |
| 147 | echo "-> ${#groups[@]} groups containing '${mask}' found" |
| 148 | printf "%s\n" ${groups[@]} | xargs -I{} echo group-delete {} >>${cmds} |
| 149 | _clean_and_flush_cinder |
| 150 | } |
| 151 | |
| 152 | function _clean_volume_group_types { |
| 153 | group_types=( $(cinder --os-volume-api-version 3.43 group-type-list | grep ${mask} | awk '{print $2}') ) |
| 154 | echo "-> ${#group_types[@]} group types containing '${mask}' found" |
| 155 | printf "%s\n" ${group_types[@]} | xargs -I{} echo group-type-delete {} >>${cmds} |
| 156 | _clean_and_flush_cinder |
| 157 | } |
| 158 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 159 | ### Volume types |
| 160 | function _clean_volume_types { |
| 161 | vtypes=( $(openstack volume type list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) ) |
| 162 | echo "-> ${#vtypes[@]} volume types containing '${mask}' found" |
| 163 | printf "%s\n" ${vtypes[@]} | xargs -I{} echo volume type delete {} >>${cmds} |
| 164 | _clean_and_flush |
| 165 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 166 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 167 | ### Images |
| 168 | function _clean_images { |
Oleksandr Savatieiev | c1bbead | 2018-10-18 14:39:25 +0200 | [diff] [blame] | 169 | images=( $(openstack image list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) ) |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 170 | echo "-> ${#images[@]} images containing '${mask}' found" |
| 171 | printf "%s\n" ${images[@]} | xargs -I{} echo image delete {} >>${cmds} |
| 172 | _clean_and_flush |
| 173 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 174 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 175 | ### Sec groups |
| 176 | function _clean_sec_groups { |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 177 | projects=( $(openstack project list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) ) |
| 178 | sgroups=( $(printf "%s\n" ${projects[@]} | xargs -I{} /bin/bash -c "openstack security group list -c ID -c Project -f value | grep {} | cut -d' ' -f1") ) |
| 179 | echo "-> ${#sgroups[@]} security groups for project containing '${mask}' found" |
| 180 | printf "%s\n" ${sgroups[@]} | xargs -I{} echo security group delete {} >>${cmds} |
| 181 | _clean_and_flush |
Alex Savatieiev | 788193b | 2019-03-12 11:39:08 -0500 | [diff] [blame] | 182 | |
| 183 | # Additional step to cleanup 'hanged' groups |
| 184 | sgroups_raw=( $(openstack security group list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) ) |
| 185 | echo "-> ${#sgroups_raw[@]} security groups for '${mask}' found" |
| 186 | printf "%s\n" ${sgroups_raw[@]} | xargs -I{} echo security group delete {} >>${cmds} |
| 187 | _clean_and_flush |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 188 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 189 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 190 | ### Keypairs |
| 191 | function _clean_keypairs { |
| 192 | keypairs=( $(openstack keypair list -c Name -f value | grep ${mask}) ) |
| 193 | echo "-> ${#keypairs[@]} keypairs containing '${mask}' found" |
| 194 | printf "%s\n" ${keypairs[@]} | xargs -I{} echo keypair delete {} >>${cmds} |
| 195 | _clean_and_flush |
| 196 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 197 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 198 | ### Routers and Networks |
| 199 | function _clean_routers_and_networks { |
| 200 | routers=( $(openstack router list -c ID -c Name -f value | grep ${mask} | cut -d ' ' -f1) ) |
| 201 | if [ ${#routers[@]} -eq 0 ]; then |
| 202 | echo "-> No routers containing '${mask}' found" |
| 203 | else |
| 204 | echo "-> ${#routers[@]} routers containing '${mask}' found" |
Alex Savatieiev | 788193b | 2019-03-12 11:39:08 -0500 | [diff] [blame] | 205 | echo "... unsetting gateways" |
| 206 | printf "%s\n" ${routers[@]} | xargs -I{} echo router unset --external-gateway {} >>${cmds} |
| 207 | _clean_and_flush |
| 208 | |
| 209 | echo "... removing ports" |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 210 | for router in ${routers[@]}; do |
| 211 | r_ports=( $(openstack port list --router ${router} -f value -c ID) ) |
| 212 | if [ ${#r_ports[@]} -eq 0 ]; then |
Alex Savatieiev | 788193b | 2019-03-12 11:39:08 -0500 | [diff] [blame] | 213 | echo "... no ports to unplug for ${router}" |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 214 | else |
| 215 | for r_port in ${r_ports[@]}; do |
Alex Savatieiev | 788193b | 2019-03-12 11:39:08 -0500 | [diff] [blame] | 216 | echo "... queued removal of port '${r_port}' from router '${router}'" |
| 217 | echo "router remove port ${router} ${r_port}" >>${cmds} |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 218 | done |
| 219 | fi |
| 220 | done |
Alex Savatieiev | 788193b | 2019-03-12 11:39:08 -0500 | [diff] [blame] | 221 | _clean_and_flush |
| 222 | |
| 223 | echo "... deleting routers" |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 224 | printf "%s\n" ${routers[@]} | xargs -I{} echo router delete {} >>${cmds} |
Alex Savatieiev | 788193b | 2019-03-12 11:39:08 -0500 | [diff] [blame] | 225 | _clean_and_flush |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 226 | fi |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 227 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 228 | networks=( $(openstack network list | grep "${mask}" | cut -d' ' -f2) ) |
| 229 | if [ ${#networks[@]} -eq 0 ]; then |
| 230 | echo "-> No networks containing '${mask}' found" |
| 231 | else |
| 232 | ports=() |
| 233 | subnets=() |
| 234 | for((idx=0;idx<${#networks[@]};idx++)) do |
| 235 | ports+=( $(openstack port list --network ${networks[idx]} -c ID -f value) ) |
| 236 | subnets+=( $(openstack subnet list --network ${networks[idx]} -c ID -f value) ) |
| 237 | echo "-> $((${idx}+1)) of ${#networks[@]}, total ${#ports[@]} ports, ${#subnets[@]} subnets" |
| 238 | done |
| 239 | printf "%s\n" ${ports[@]} | xargs -I{} echo port delete {} >>${cmds} |
| 240 | printf "%s\n" ${subnets[@]} | xargs -I{} echo subnet delete {} >>${cmds} |
| 241 | echo network delete ${networks[@]} >>${cmds} |
| 242 | echo "-> ${#routers[@]} routers, ${#ports[@]} ports, ${#subnets[@]} subnets, ${#networks[@]} networks" |
| 243 | fi |
| 244 | _clean_and_flush |
| 245 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 246 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 247 | ### Regions |
| 248 | function _clean_regions { |
| 249 | regions=( $(openstack region list -c Region -f value | grep ${mask}) ) |
| 250 | echo "-> ${#regions[@]} regions containing '${mask}' found" |
| 251 | printf "%s\n" ${regions[@]} | xargs -I{} echo region delete {} >>${cmds} |
| 252 | _clean_and_flush |
| 253 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 254 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 255 | ### Stacks |
| 256 | function _clean_stacks { |
| 257 | # By default openstack denies use of global_index for everyone. |
| 258 | # In case you want to have handy cleanup, consider updating policy.json here: |
| 259 | # root@ctl0x:~# cat -n /etc/heat/policy.json | grep global_index |
| 260 | # 48 "stacks:global_index": "rule:deny_everybody", |
| 261 | # 73 "software_configs:global_index": "rule:deny_everybody", |
| 262 | # After this you will be able to use --all option |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 263 | |
Oleksandr Savatieiev | 722396b | 2019-01-29 12:53:21 +0100 | [diff] [blame] | 264 | stacks=( $(openstack stack list --nested --hidden -c ID -c "Stack Name" -f value | grep ${mask} | cut -d' ' -f1) ) |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 265 | echo "-> ${#stacks[@]} stacks containing '${mask}' found" |
| 266 | printf "%s\n" ${stacks[@]} | xargs -I{} echo stack check {} >>${cmds} |
| 267 | printf "%s\n" ${stacks[@]} | xargs -I{} echo stack delete -y {} >>${cmds} |
| 268 | _clean_and_flush |
Alex Savatieiev | 27c2c41 | 2019-04-29 18:11:50 -0500 | [diff] [blame] | 269 | |
| 270 | if [ "$stack_alt" = true ]; then |
| 271 | stacks=( $(openstack stack list --nested --hidden -c ID -c "Stack Name" -f value | grep -E ${stack_regex} | cut -d' ' -f1) ) |
| 272 | echo "-> ${#stacks[@]} stacks containing '${stack_regex}' found" |
| 273 | printf "%s\n" ${stacks[@]} | xargs -I{} echo stack check {} >>${cmds} |
| 274 | printf "%s\n" ${stacks[@]} | xargs -I{} echo stack delete -y {} >>${cmds} |
| 275 | _clean_and_flush |
| 276 | fi |
| 277 | |
Oleksandr Savatieiev | 722396b | 2019-01-29 12:53:21 +0100 | [diff] [blame] | 278 | if [ "$purge_deleted_stacks" = true ]; then |
Alex Savatieiev | 788193b | 2019-03-12 11:39:08 -0500 | [diff] [blame] | 279 | heat-manage purge_deleted -g ${stack_granularity} -b ${batch_size} ${stack_granularity_value} | wc -l | xargs -I{} echo "-> Purged {} stacks" |
Oleksandr Savatieiev | 722396b | 2019-01-29 12:53:21 +0100 | [diff] [blame] | 280 | fi |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 281 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 282 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 283 | ### Containers |
| 284 | function _clean_containers { |
Alex Savatieiev | ec70369 | 2019-06-07 15:33:31 -0500 | [diff] [blame] | 285 | containers=( $(openstack container list --all -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) ) |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 286 | echo "-> ${#containers[@]} containers containing '${mask}' found" |
| 287 | printf "%s\n" ${containers[@]} | xargs -I{} echo container delete {} >>${cmds} |
| 288 | _clean_and_flush |
| 289 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 290 | |
Alex Savatieiev | ec70369 | 2019-06-07 15:33:31 -0500 | [diff] [blame] | 291 | function _clean_flavors { |
| 292 | flavors=( $(openstack flavor list --all -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) ) |
| 293 | echo "-> ${#flavors[@]} flavors containing '${mask}' found" |
| 294 | printf "%s\n" ${flavors[@]} | xargs -I{} echo flavor delete {} >>${cmds} |
| 295 | _clean_and_flush |
| 296 | } |
| 297 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 298 | ################### |
| 299 | ### Main |
| 300 | ################### |
| 301 | # temp file for commands |
| 302 | cmds=$(mktemp) |
Oleksandr Savatieiev | c1bbead | 2018-10-18 14:39:25 +0200 | [diff] [blame] | 303 | trap "rm -f ${cmds}" EXIT |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 304 | echo "Using tempfile: '${cmds}'" |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 305 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 306 | # Consider cleaning contrail resources carefully |
| 307 | # ...and only after that - clean projects |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 308 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 309 | _clean_stacks |
| 310 | _clean_servers |
| 311 | |
| 312 | _clean_users |
| 313 | _clean_roles |
| 314 | _clean_snapshots |
| 315 | _clean_volumes |
Oleksii Zhurba | 0b736c7 | 2019-04-29 17:13:47 -0500 | [diff] [blame] | 316 | _clean_volume_groups |
| 317 | _clean_volume_group_types |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 318 | _clean_volume_types |
| 319 | _clean_images |
| 320 | _clean_sec_groups |
| 321 | _clean_keypairs |
| 322 | _clean_routers_and_networks |
| 323 | _clean_regions |
| 324 | _clean_containers |
| 325 | |
| 326 | # project cleaning disabled by default |
| 327 | # Coz cleaning Contrail with no projects is a hard task |
Oleksandr Savatieiev | c1bbead | 2018-10-18 14:39:25 +0200 | [diff] [blame] | 328 | if [ "$clean_projects" = true ]; then |
| 329 | _clean_projects |
| 330 | fi |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 331 | |
| 332 | # remove temp file |
| 333 | rm ${cmds} |