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' |
Oleksandr Savatieiev | 0a1db22 | 2019-01-29 10:31:19 +0100 | [diff] [blame] | 3 | mask='s_rally\|rally_\|tempest_\|tempest-' |
Oleksandr Savatieiev | c1bbead | 2018-10-18 14:39:25 +0200 | [diff] [blame] | 4 | dry_run=false |
| 5 | clean_projects=false |
Oleksandr Savatieiev | 41df4eb | 2018-11-08 16:22:30 +0100 | [diff] [blame] | 6 | make_servers_active=false |
Oleksandr Savatieiev | c1bbead | 2018-10-18 14:39:25 +0200 | [diff] [blame] | 7 | |
Oleksandr Savatieiev | 722396b | 2019-01-29 12:53:21 +0100 | [diff] [blame] | 8 | stack_batch_size=10 |
| 9 | # granularity values: days,hours,minutes,seconds |
| 10 | stack_granularity=days |
| 11 | stack_granularity_value=1 |
| 12 | |
Oleksandr Savatieiev | c1bbead | 2018-10-18 14:39:25 +0200 | [diff] [blame] | 13 | function show_help { |
Oleksandr Savatieiev | 722396b | 2019-01-29 12:53:21 +0100 | [diff] [blame] | 14 | printf "Resource cleaning script\nMask is: %s\n\t-h, -?\tShow this help\n" ${mask} |
| 15 | printf "\t-t\tDry run mode, no cleaning done\n" |
| 16 | printf "\t-P\tForce cleaning of projects\n" |
| 17 | printf "\t-S\tSet servers to ACTIVE before deletion (bare metal reqiured)\n" |
| 18 | printf "\t-F\tForce purge deleted stacks. Batch size: %s, >%s %s\n" ${stack_batch_size} ${stack_granularity_value} ${stack_granularity} |
Oleksandr Savatieiev | c1bbead | 2018-10-18 14:39:25 +0200 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | OPTIND=1 # Reset in case getopts has been used previously in the shell. |
Oleksandr Savatieiev | 722396b | 2019-01-29 12:53:21 +0100 | [diff] [blame] | 22 | while getopts "h?:tSPF" opt; do |
Oleksandr Savatieiev | c1bbead | 2018-10-18 14:39:25 +0200 | [diff] [blame] | 23 | case "$opt" in |
| 24 | h|\?) |
| 25 | show_help |
| 26 | exit 0 |
| 27 | ;; |
| 28 | t) dry_run=true |
| 29 | printf "Running in dry-run mode\n" |
| 30 | ;; |
Oleksandr Savatieiev | 41df4eb | 2018-11-08 16:22:30 +0100 | [diff] [blame] | 31 | S) make_servers_active=true |
| 32 | printf "Servers will be set to ACTIVE before deletion\n" |
| 33 | ;; |
Oleksandr Savatieiev | c1bbead | 2018-10-18 14:39:25 +0200 | [diff] [blame] | 34 | P) clean_projects=true |
| 35 | printf "Project cleanning enabled\n" |
| 36 | ;; |
Oleksandr Savatieiev | 722396b | 2019-01-29 12:53:21 +0100 | [diff] [blame] | 37 | F) purge_deleted_stacks=true |
| 38 | printf "Purging stacks deleted >$stack_granularity_value $stack_granularity ago enabled, batch size %s\n" $stack_batch_size |
| 39 | ;; |
Oleksandr Savatieiev | c1bbead | 2018-10-18 14:39:25 +0200 | [diff] [blame] | 40 | esac |
| 41 | done |
| 42 | |
| 43 | shift $((OPTIND-1)) |
| 44 | [ "${1:-}" = "--" ] && shift |
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 | ### Execute collected commands and flush the temp file |
| 47 | function _clean_and_flush { |
Oleksandr Savatieiev | c1bbead | 2018-10-18 14:39:25 +0200 | [diff] [blame] | 48 | if [ "$dry_run" = true ] ; then |
| 49 | return 0 |
| 50 | fi |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 51 | if [ -s ${cmds} ]; then |
| 52 | echo "Processing $(cat ${cmds} | wc -l) commands" |
Oleksandr Savatieiev | 41df4eb | 2018-11-08 16:22:30 +0100 | [diff] [blame] | 53 | cat ${cmds} | openstack |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 54 | truncate -s 0 ${cmds} |
| 55 | fi |
| 56 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 57 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 58 | ### Users |
| 59 | function _clean_users { |
| 60 | users=( $(openstack user list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) ) |
| 61 | echo "-> ${#users[@]} users containing '${mask}' found" |
| 62 | printf "%s\n" ${users[@]} | xargs -I{} echo user delete {} >>${cmds} |
| 63 | _clean_and_flush |
| 64 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 65 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 66 | ### Roles |
| 67 | function _clean_roles { |
| 68 | roles=( $(openstack role list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) ) |
| 69 | echo "-> ${#roles[@]} roles containing '${mask}' found" |
| 70 | printf "%s\n" ${roles[@]} | xargs -I{} echo role delete {} >>${cmds} |
| 71 | _clean_and_flush |
| 72 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 73 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 74 | ### Projects |
| 75 | function _clean_projects { |
| 76 | projects=( $(openstack project list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) ) |
| 77 | echo "-> ${#projects[@]} projects containing '${mask}' found" |
| 78 | printf "%s\n" ${projects[@]} | xargs -I{} echo project delete {} >>${cmds} |
| 79 | _clean_and_flush |
| 80 | } |
Valentyn Khalin | bdf3ba9 | 2018-07-13 18:14:20 +0300 | [diff] [blame] | 81 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 82 | ### Servers |
| 83 | function _clean_servers { |
Oleksandr Savatieiev | 0a1db22 | 2019-01-29 10:31:19 +0100 | [diff] [blame] | 84 | 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] | 85 | echo "-> ${#servers[@]} servers containing '${mask}' found" |
Oleksandr Savatieiev | 41df4eb | 2018-11-08 16:22:30 +0100 | [diff] [blame] | 86 | if [ "$make_servers_active" = true ]; then |
| 87 | printf "%s\n" ${servers[@]} | xargs -I{} echo server set --state active {} >>${cmds} |
| 88 | fi |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 89 | printf "%s\n" ${servers[@]} | xargs -I{} echo server delete {} >>${cmds} |
| 90 | _clean_and_flush |
| 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 | ### Reset snapshot state and delete |
| 94 | function _clean_snapshots { |
| 95 | snapshots=( $(openstack volume snapshot list --all -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) ) |
| 96 | echo "-> ${#snapshots[@]} snapshots containing '${mask}' found" |
Oleksandr Savatieiev | c1bbead | 2018-10-18 14:39:25 +0200 | [diff] [blame] | 97 | printf "%s\n" ${snapshots[@]} | xargs -I{} echo volume snapshot set --state available {} >>${cmds} |
| 98 | printf "%s\n" ${snapshots[@]} | xargs -I{} echo volume snapshot delete {} >>${cmds} |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 99 | _clean_and_flush |
| 100 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 101 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 102 | function _clean_volumes { |
| 103 | volumes=( $(openstack volume list --all -c ID -c Name -c Type -f value | grep ${mask} | cut -d' ' -f1) ) |
| 104 | echo "-> ${#volumes[@]} volumes containing '${mask}' found" |
| 105 | printf "%s\n" ${volumes[@]} | xargs -I{} echo volume set --state available {} >>${cmds} |
| 106 | printf "%s\n" ${volumes[@]} | xargs -I{} echo volume delete {} >>${cmds} |
| 107 | _clean_and_flush |
| 108 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 109 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 110 | ### Volume types |
| 111 | function _clean_volume_types { |
| 112 | vtypes=( $(openstack volume type list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) ) |
| 113 | echo "-> ${#vtypes[@]} volume types containing '${mask}' found" |
| 114 | printf "%s\n" ${vtypes[@]} | xargs -I{} echo volume type delete {} >>${cmds} |
| 115 | _clean_and_flush |
| 116 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 117 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 118 | ### Images |
| 119 | function _clean_images { |
Oleksandr Savatieiev | c1bbead | 2018-10-18 14:39:25 +0200 | [diff] [blame] | 120 | 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] | 121 | echo "-> ${#images[@]} images containing '${mask}' found" |
| 122 | printf "%s\n" ${images[@]} | xargs -I{} echo image delete {} >>${cmds} |
| 123 | _clean_and_flush |
| 124 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 125 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 126 | ### Sec groups |
| 127 | function _clean_sec_groups { |
| 128 | # openstack project list -c ID -c Name -f value | grep rally | cut -d' ' -f1 | xargs -I{} /bin/bash -c "openstack security group list | grep {}" |
| 129 | projects=( $(openstack project list -c ID -c Name -f value | grep ${mask} | cut -d' ' -f1) ) |
| 130 | sgroups=( $(printf "%s\n" ${projects[@]} | xargs -I{} /bin/bash -c "openstack security group list -c ID -c Project -f value | grep {} | cut -d' ' -f1") ) |
| 131 | echo "-> ${#sgroups[@]} security groups for project containing '${mask}' found" |
| 132 | printf "%s\n" ${sgroups[@]} | xargs -I{} echo security group delete {} >>${cmds} |
| 133 | _clean_and_flush |
| 134 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 135 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 136 | ### Keypairs |
| 137 | function _clean_keypairs { |
| 138 | keypairs=( $(openstack keypair list -c Name -f value | grep ${mask}) ) |
| 139 | echo "-> ${#keypairs[@]} keypairs containing '${mask}' found" |
| 140 | printf "%s\n" ${keypairs[@]} | xargs -I{} echo keypair delete {} >>${cmds} |
| 141 | _clean_and_flush |
| 142 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 143 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 144 | ### Routers and Networks |
| 145 | function _clean_routers_and_networks { |
| 146 | routers=( $(openstack router list -c ID -c Name -f value | grep ${mask} | cut -d ' ' -f1) ) |
| 147 | if [ ${#routers[@]} -eq 0 ]; then |
| 148 | echo "-> No routers containing '${mask}' found" |
| 149 | else |
| 150 | echo "-> ${#routers[@]} routers containing '${mask}' found" |
| 151 | echo "...unsetting gateways" |
| 152 | printf "%s\n" ${routers[@]} | xargs -I{} echo router unset --external-gateway {} | openstack |
| 153 | for router in ${routers[@]}; do |
| 154 | r_ports=( $(openstack port list --router ${router} -f value -c ID) ) |
| 155 | if [ ${#r_ports[@]} -eq 0 ]; then |
| 156 | echo "...no ports to unplug for ${router}" |
| 157 | else |
| 158 | for r_port in ${r_ports[@]}; do |
| 159 | echo "...removing port '${r_port}' from router '${router}'" |
| 160 | openstack router remove port ${router} ${r_port} |
| 161 | done |
| 162 | fi |
| 163 | done |
| 164 | printf "%s\n" ${routers[@]} | xargs -I{} echo router delete {} >>${cmds} |
| 165 | fi |
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 | networks=( $(openstack network list | grep "${mask}" | cut -d' ' -f2) ) |
| 168 | if [ ${#networks[@]} -eq 0 ]; then |
| 169 | echo "-> No networks containing '${mask}' found" |
| 170 | else |
| 171 | ports=() |
| 172 | subnets=() |
| 173 | for((idx=0;idx<${#networks[@]};idx++)) do |
| 174 | ports+=( $(openstack port list --network ${networks[idx]} -c ID -f value) ) |
| 175 | subnets+=( $(openstack subnet list --network ${networks[idx]} -c ID -f value) ) |
| 176 | echo "-> $((${idx}+1)) of ${#networks[@]}, total ${#ports[@]} ports, ${#subnets[@]} subnets" |
| 177 | done |
| 178 | printf "%s\n" ${ports[@]} | xargs -I{} echo port delete {} >>${cmds} |
| 179 | printf "%s\n" ${subnets[@]} | xargs -I{} echo subnet delete {} >>${cmds} |
| 180 | echo network delete ${networks[@]} >>${cmds} |
| 181 | echo "-> ${#routers[@]} routers, ${#ports[@]} ports, ${#subnets[@]} subnets, ${#networks[@]} networks" |
| 182 | fi |
| 183 | _clean_and_flush |
| 184 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 185 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 186 | ### Regions |
| 187 | function _clean_regions { |
| 188 | regions=( $(openstack region list -c Region -f value | grep ${mask}) ) |
| 189 | echo "-> ${#regions[@]} regions containing '${mask}' found" |
| 190 | printf "%s\n" ${regions[@]} | xargs -I{} echo region delete {} >>${cmds} |
| 191 | _clean_and_flush |
| 192 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 193 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 194 | ### Stacks |
| 195 | function _clean_stacks { |
| 196 | # By default openstack denies use of global_index for everyone. |
| 197 | # In case you want to have handy cleanup, consider updating policy.json here: |
| 198 | # root@ctl0x:~# cat -n /etc/heat/policy.json | grep global_index |
| 199 | # 48 "stacks:global_index": "rule:deny_everybody", |
| 200 | # 73 "software_configs:global_index": "rule:deny_everybody", |
| 201 | # After this you will be able to use --all option |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 202 | |
Oleksandr Savatieiev | 722396b | 2019-01-29 12:53:21 +0100 | [diff] [blame] | 203 | 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] | 204 | echo "-> ${#stacks[@]} stacks containing '${mask}' found" |
| 205 | printf "%s\n" ${stacks[@]} | xargs -I{} echo stack check {} >>${cmds} |
| 206 | printf "%s\n" ${stacks[@]} | xargs -I{} echo stack delete -y {} >>${cmds} |
| 207 | _clean_and_flush |
Oleksandr Savatieiev | 722396b | 2019-01-29 12:53:21 +0100 | [diff] [blame] | 208 | if [ "$purge_deleted_stacks" = true ]; then |
| 209 | heat-manage purge_deleted -g ${stack_granularity} -b ${stack_batch_size} ${stack_granularity_value} | wc -l | xargs -I{} echo "-> Purged {} stacks" |
| 210 | fi |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 211 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 212 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 213 | ### Containers |
| 214 | function _clean_containers { |
| 215 | containers=( $(openstack container list --all -c ID -c Name -f value | grep ${mask}) ) |
| 216 | echo "-> ${#containers[@]} containers containing '${mask}' found" |
| 217 | printf "%s\n" ${containers[@]} | xargs -I{} echo container delete {} >>${cmds} |
| 218 | _clean_and_flush |
| 219 | } |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 220 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 221 | ################### |
| 222 | ### Main |
| 223 | ################### |
| 224 | # temp file for commands |
| 225 | cmds=$(mktemp) |
Oleksandr Savatieiev | c1bbead | 2018-10-18 14:39:25 +0200 | [diff] [blame] | 226 | trap "rm -f ${cmds}" EXIT |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 227 | echo "Using tempfile: '${cmds}'" |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 228 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 229 | # Consider cleaning contrail resources carefully |
| 230 | # ...and only after that - clean projects |
Oleksii Zhurba | 0aa4670 | 2018-02-07 16:17:39 -0600 | [diff] [blame] | 231 | |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 232 | _clean_stacks |
| 233 | _clean_servers |
| 234 | |
| 235 | _clean_users |
| 236 | _clean_roles |
| 237 | _clean_snapshots |
| 238 | _clean_volumes |
| 239 | _clean_volume_types |
| 240 | _clean_images |
| 241 | _clean_sec_groups |
| 242 | _clean_keypairs |
| 243 | _clean_routers_and_networks |
| 244 | _clean_regions |
| 245 | _clean_containers |
| 246 | |
| 247 | # project cleaning disabled by default |
| 248 | # Coz cleaning Contrail with no projects is a hard task |
Oleksandr Savatieiev | c1bbead | 2018-10-18 14:39:25 +0200 | [diff] [blame] | 249 | if [ "$clean_projects" = true ]; then |
| 250 | _clean_projects |
| 251 | fi |
Oleksandr Savatieiev | 91355c8 | 2018-09-07 12:14:34 +0200 | [diff] [blame] | 252 | |
| 253 | # remove temp file |
| 254 | rm ${cmds} |