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