def migrate_vms_from_image_with_network_mapping(id_file): ids = utils.read_ids_from_file(id_file) nova_from = get_nova("from") to_vms = get_vm_list('to') for uuid in ids: try: server = nova_from.servers.get(uuid) if server.status == 'SHUTOFF': print "Finding image for server with UUID:", uuid new_name = "migration_vm_image_" + server.id # print new_name image = glance_common.get_image_by_name("to", new_name) if image: print "Found image with name: ", image.name # need to check for VMs that were already re-created on the TO side: dup_vms = filter(lambda to_vms: to_vms.name == server.name, to_vms) duplicate = False for dup in dup_vms: if dup.metadata['original_vm_id'] == server.id: print "Duplicate VM on TO side already found, skipping VM:", server.name, server.id duplicate = True if duplicate is False: create_vm(server, image=image) else: print "Did not find image in 'to' environment with name:", new_name else: print "1 Server with UUID:", uuid, " is not shutoff. It must be in SHUTOFF status for this action." except nova_exc.NotFound: print "2 Server with UUID", uuid, "not found"
def boot_from_vms_from_image_with_network_mapping(id_file, custom_network='none', key='default', user_data='default'): ids = utils.read_ids_from_file(id_file) nova_from = get_nova("from") to_vms = get_vm_list('to') for uuid in ids: try: server = nova_from.servers.get(uuid) if server.status == 'SHUTOFF': image_name = "migration_vm_image_" + server.id image = glance_common.get_image_by_name('to', image_name) # # need to check for VMs that were already re-created on the TO side: dup_vms = filter(lambda to_vms: to_vms.name == server.name, to_vms) duplicate = False for dup in dup_vms: if 'original_vm_id' in dup.metadata: if dup.metadata['original_vm_id'] == server.id: print "Duplicate VM on TO side already found, skipping VM:", server.name, server.id duplicate = True if duplicate is False: create_vm_from_image_with_network_mapping(server, image, network_name=custom_network, key=key, user_data=user_data) else: print "1 Server with UUID:", uuid, " is not shutoff. It must be in SHUTOFF status for this action." except nova_exc.NotFound: print "2 Server with UUID", uuid, "not found"
def upload_volume_to_image_by_vm(destination, volume): cinder = get_cinder(destination) old_image = glance_common.get_image_by_name('from', volume.name) if old_image: image_name = volume.name container_format = old_image.container_format disk_format = old_image.disk_format cinder.volumes.upload_to_image(volume, force=True, image_name=image_name, container_format=container_format, disk_format=disk_format) print "Image " + image_name + " created from volume ID " + volume.id
def upload_volume_to_image_by_vm(destination, volume): cinder = get_cinder(destination) old_image = glance_common.get_image_by_name('from', volume.name) if old_image: image_name = volume.name container_format = old_image.container_format disk_format = old_image.disk_format cinder.volumes.upload_to_image(volume, force=True, image_name=image_name, container_format=container_format, disk_format=disk_format) print "Image " + image_name + " created from volume ID " + volume.id
return myvol def create_volume_from_image(destination, volume, single=False): cinder = get_cinder(destination) try: from_tenant = volume.__dict__['os-vol-tenant-attr:tenant_id'] tenant = keystone_common.find_opposite_tenant_id(from_tenant) except Exception, e: tenant = None # user = keystone_common.find_opposite_user_id(volume.user_id) if single: image_name = "single_migration_volume_image_" + volume.id else: image_name = "migration_volume_image_" + volume.id image = glance_common.get_image_by_name('to', image_name) if image: if volume.volume_type == 'None': myvol = cinder.volumes.create(size=volume.size, snapshot_id=volume.snapshot_id, name=volume.name, description=volume.description, #volume_type=volume.volume_type, # user_id=user, #todo:fixthis project_id=tenant, availability_zone=volume.availability_zone, metadata=volume.metadata, imageRef=image.id, source_volid=volume.source_volid ) else:
return myvol # on the "TO" side only def create_volume_from_image(destination, volume, single=False): cinder = get_cinder(destination) try: from_tenant = volume.__dict__['os-vol-tenant-attr:tenant_id'] tenant = keystone_common.find_opposite_tenant_id(from_tenant) except Exception, e: tenant = None # user = keystone_common.find_opposite_user_id(volume.user_id) if single: image_name = "single_migration_volume_image_" + volume.id else: image_name = "migration_volume_image_" + volume.id image = glance_common.get_image_by_name('to', image_name) if image: meta = volume.metadata meta.update({'original_volume_id': volume.id}) meta.update({'original_boot_status': volume.bootable}) if volume.attachments: for att in volume.attachments: meta.update({'original_vm_id': att['server_id']}) meta.update({'original_device': att['device']}) version = float(cinder.version) if version >= 2.0: if volume.volume_type == 'None': myvol = cinder.volumes.create(size=volume.size, snapshot_id=volume.snapshot_id, name=volume.name,