system-config/tools/clean-leaked-bfv.py

27 lines
1.1 KiB
Python

# Tool to clean up leaked Boot-From-Volume Volumes
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'])))