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