Skip to content

Instantly share code, notes, and snippets.

@omgjlk
Created May 21, 2015 00:19
Show Gist options
  • Select an option

  • Save omgjlk/5295e114792c2bba2cb9 to your computer and use it in GitHub Desktop.

Select an option

Save omgjlk/5295e114792c2bba2cb9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#
# Prune deleted instance content out of a nova database
#
# Authors: Jesse Keating <[email protected]>
#
# License: MIT
import argparse
import MySQLdb
import os
import sys
def error(msg='An error occurred'):
# this should probably use logging instead of sys
sys.stderr.write(msg + '\n')
sys.exit(1)
# What do I need to know?
# database user, default of root
# database password, default of None
# if no user/pass, read ~/.my.cnf
# database name, default of nova
parser = argparse.ArgumentParser(description='Nova Pruner')
parser.add_argument('--host', default='localhost',
help='Host of the database server (default: %(default)s)')
parser.add_argument('--user', help='Login user')
parser.add_argument('--password', help='Login password')
parser.add_argument('--dbname', default='nova',
help='Name of the Nova database (default: %(default)s)')
args = parser.parse_args()
# see if we need to mine a my.cnf file
login_user = args.user
login_password = args.password or ''
# create a db object
if login_user:
db = MySQLdb.connect(host=args.host, db=args.dbname, user=login_user,
passwd=login_password)
else:
mycnf = os.path.expanduser('~/.my.cnf')
if os.path.exists(mycnf):
db = MySQLdb.connect(host=args.host, db=args.dbname,
read_default_file=mycnf)
else:
error(msg='User was not provided, nor is ~/.my.cnf readable.')
# walk through each block of things to delete
## things to delete from
# migrations
# instance_extra
# block_device_mapping
# instance_actions_events
# instance_actions
# instance_info_caches
# instance_system_metadata
# instance_faults
# instances
queries = ["DELETE migrations FROM migrations JOIN instances ON \
migrations.instance_uuid = instances.uuid WHERE \
instances.vm_state = %s",
"DELETE instance_extra from instance_extra JOIN instances ON \
instance_extra.instance_uuid = instances.uuid WHERE \
instances.vm_state = %s",
"DELETE block_device_mapping FROM block_device_mapping JOIN \
instances ON block_device_mapping.instance_uuid = instances.uuid \
WHERE instances.vm_state = %s",
"DELETE instance_actions_events FROM instance_actions_events JOIN \
instance_actions JOIN instances ON instance_actions.instance_uuid \
= instances.uuid WHERE instances.vm_state = %s",
"DELETE instance_actions from instance_actions JOIN instances ON \
instance_actions.instance_uuid = instances.uuid WHERE \
instances.vm_state = %s",
"DELETE instance_info_caches from instance_info_caches JOIN \
instances ON instance_info_caches.instance_uuid = instances.uuid \
WHERE instances.vm_state = %s",
"DELETE instance_system_metadata from instance_system_metadata JOIN \
instances ON instance_system_metadata.instance_uuid = instances.uuid \
WHERE instances.vm_state = %s",
"DELETE instance_faults from instance_faults JOIN instances ON \
instance_faults.instance_uuid = instances.uuid WHERE \
instances.vm_state = %s",
"DELETE from instances where vm_state = %s"]
# get a cursor
curse = db.cursor()
exit_code = 0
# Do all the deletes
try:
for query in queries:
curse.execute(query, ('deleted'))
# commit them all
db.commit()
except Exception, e:
sys.stderr.write(e + '\n')
exit_code = 1
finally:
curse.close()
db.close()
sys.exit(exit_code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment