Skip to content

Instantly share code, notes, and snippets.

@fcharlier
Created January 8, 2015 17:08
Show Gist options
  • Save fcharlier/135507034ac307208ae7 to your computer and use it in GitHub Desktop.
Save fcharlier/135507034ac307208ae7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Copyright (c) 2015 François Charlier <francois.charlier@redhat.com>
# All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import MySQLdb
import MySQLdb.cursors
import pprint
db_nova = MySQLdb.connect(read_default_file="/root/.my.cnf", db="nova", cursorclass=MySQLdb.cursors.DictCursor)
db_glance = MySQLdb.connect(read_default_file="/root/.my.cnf", db="glance", cursorclass=MySQLdb.cursors.DictCursor)
instances = db_nova.cursor()
instances.execute("""select uuid, instance_type_id, image_ref as base_image_ref from instances where deleted = 0 and uuid not in (select distinct instance_uuid from instance_system_metadata)""")
for instance in instances:
image_meta_cursor = db_glance.cursor()
image_meta_cursor.execute("""select id as base_image_ref, container_format, disk_format, min_ram, min_disk from images where id = %s""", (instance['base_image_ref'],))
image_meta = image_meta_cursor.fetchone()
type_meta_cursor = db_nova.cursor()
type_meta_cursor.execute("""select id, ephemeral_gb, flavorid, memory_mb, name, root_gb, rxtx_factor, swap, vcpus, vcpu_weight from instance_types where id = %s""", (instance['instance_type_id'],))
type_meta = type_meta_cursor.fetchone()
meta = { 'instance_type_%s' % k: v for k, v in type_meta.iteritems() }
meta.update({ 'image_%s' % k: v for k, v in image_meta.iteritems() })
meta.update({ 'network_allocated': 'True' })
fix_cursor = db_nova.cursor()
fix_cursor.executemany(
"""insert into instance_system_metadata (
deleted,
instance_system_metadata.created_at,
instance_system_metadata.instance_uuid,
instance_system_metadata.key,
instance_system_metadata.value
) values (
0,
now(),
%(uuid)s,
%(key)s,
%(value)s
)""",
[ {'uuid': instance['uuid'], 'key': k, 'value': v} for k, v in meta.iteritems() ]
)
db_nova.commit()
# vim: set ts=4 sw=4 sts=4 et
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment