# Get list of all cells
api_cur.execute("SELECT id, name, database_connection FROM cell_mappings")
CELLS = [{'id': id, 'name': name, 'db': _get_conn(db)} for id, name, db in api_cur.fetchall()]
# Get list of all unmapped instances
api_cur.execute("SELECT instance_uuid FROM instance_mappings WHERE cell_id IS NULL")
print "Number of unmapped instances: %s" % api_cur.rowcount
# Go over all unmapped instances
unmapped_instances = api_cur.fetchall()
for (instance_uuid,) in unmapped_instances:
instance_cell = None
# Check if a build request exists, if so, skip.
api_cur.execute("SELECT id FROM build_requests WHERE instance_uuid = %s", (instance_uuid,))
if api_cur.rowcount != 0:
print "%s: build request exists, skipping" % (instance_uuid,)
break
# Check which cell contains this instance
for cell in CELLS:
cell['db'].execute("SELECT id FROM instances WHERE uuid = %s", (instance_uuid,))
if cell['db'].rowcount != 0:
instance_cell = cell
break
# Update to the correct cell
if instance_cell:
print "UPDATE instance_mappings SET cell_id = '%s' WHERE instance_uuid = '%s';" % (instance_cell['id'], instance_uuid)
continue
# If we reach this point, it's not in any cell?!
print "%s: not found in any cell" % (instance_uuid)
================================================================================
I added an updated version of the script that checks if build requests exist which should make it a tad more user friendly, and fix cell_id
======= ======= ======= ======= ======= ======= ======= ======= ======= ======= ======= ===
#!/usr/bin/env python
import urlparse
import pymysql
# Connect to databases connect( host='xxxxxx' , port=3306, user='nova_api', passwd='xxxx', db='nova_api')
api_conn = pymysql.
api_cur = api_conn.cursor()
def _get_conn(db): urlparse( db) connect( host=parsed_ url.hostname, user=parsed_ url.username, passwd= parsed_ url.password, db=parsed_ url.path[ 1:])
parsed_url = urlparse.
conn = pymysql.
return conn.cursor()
# Get list of all cells execute( "SELECT id, name, database_connection FROM cell_mappings")
api_cur.
CELLS = [{'id': id, 'name': name, 'db': _get_conn(db)} for id, name, db in api_cur.fetchall()]
# Get list of all unmapped instances execute( "SELECT instance_uuid FROM instance_mappings WHERE cell_id IS NULL")
api_cur.
print "Number of unmapped instances: %s" % api_cur.rowcount
# Go over all unmapped instances
unmapped_instances = api_cur.fetchall()
for (instance_uuid,) in unmapped_instances:
instance_cell = None
# Check if a build request exists, if so, skip. execute( "SELECT id FROM build_requests WHERE instance_uuid = %s", (instance_uuid,))
api_cur.
if api_cur.rowcount != 0:
print "%s: build request exists, skipping" % (instance_uuid,)
break
# Check which cell contains this instance 'db'].execute( "SELECT id FROM instances WHERE uuid = %s", (instance_uuid,))
for cell in CELLS:
cell[
if cell['db'].rowcount != 0:
instance_cell = cell
break
# Update to the correct cell cell['id' ], instance_uuid)
if instance_cell:
print "UPDATE instance_mappings SET cell_id = '%s' WHERE instance_uuid = '%s';" % (instance_
continue
# If we reach this point, it's not in any cell?! ======= ======= ======= ======= ======= ======= ======= ======= ======= ======= ===
print "%s: not found in any cell" % (instance_uuid)
=======