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