From 2aebe4e09f3e91ba08b25bb19384dd1ca3153f25 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Fri, 10 Jan 2020 16:54:26 -0600 Subject: [PATCH] Add quick script for cleaning boot from volume leaks Sometimes we leak boot from volume volumes. This will clean them up. Change-Id: I45182c1dcad0cdcbc327aaef3a63d37947f8a66d --- tools/clean-leaked-bfv.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 tools/clean-leaked-bfv.py diff --git a/tools/clean-leaked-bfv.py b/tools/clean-leaked-bfv.py new file mode 100644 index 0000000000..af4694ffb5 --- /dev/null +++ b/tools/clean-leaked-bfv.py @@ -0,0 +1,28 @@ +# Tool to clean up leaked Boot-From-Volume Volumes +# Run this with: +# OS_CLIENT_CONFIG_FILE=/etc/openstack/all-clouds.yaml python3 clean-volumes.py +from openstack import connection +import openstack +import datetime +import sys +# openstack.enable_logging(http_debug=True) + +c = openstack.connect(cloud='openstackjenkins-vexxhost', region_name='sjc1') +now = datetime.datetime.utcnow() + +server_ids = [s.id for s in c.list_servers()] +for vol in c.list_volumes(): + vol_age = datetime.datetime.strptime(vol['created_at'][:19], '%Y-%m-%dT%H:%M:%S') - now + if (vol.size == 80 + and vol_age.seconds > 3600 + and vol.attachments + and vol.attachments[0]['server_id'] not in server_ids): + att = vol.attachments[0] + print('DELETE /attachments/{attachment} for {server}'.format( + server=att['server_id'], + attachment=att['attachment_id'])) + print(c.block_storage.delete( + '/attachments/{attachment}'.format(attachment=att['attachment_id']), + microversion='3.31')) + print('DELETE volume {vol}'.format(vol=att['volume_id'])) + print(c.delete_volume(dict(id=att['volume_id'])))