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