def _create_image(context, instance, image_id): """Fetch image from glance and copy it to the remote system.""" try: file_name = '.'.join([image_id, 'gz']) file_path = os.path.join(FLAGS.powervm_img_local_path, file_name) LOG.debug(_("Fetching image '%s' from glance") % image_id) images.fetch_to_raw(context, image_id, file_path, instance['user_id'], project_id=instance['project_id']) LOG.debug(_("Copying image '%s' to IVM") % file_path) remote_path = FLAGS.powervm_img_remote_path remote_file_name, size = self._operator.copy_image_file( file_path, remote_path) # Logical volume LOG.debug(_("Creating logical volume")) lpar_id = self._operator.get_lpar(instance['name'])['lpar_id'] vhost = self._operator.get_vhost_by_instance_id(lpar_id) disk_name = self._operator.create_logical_volume(size) self._operator.attach_disk_to_vhost(disk_name, vhost) LOG.debug(_("Copying image to the device '%s'") % disk_name) self._operator.copy_file_to_device(remote_file_name, disk_name) except Exception, e: LOG.exception(_("PowerVM image creation failed: %s") % str(e)) raise exception.PowerVMImageCreationFailed()
def _create_image(context, instance, image_id): """Fetch image from glance and copy it to the remote system.""" try: root_volume = self._disk_adapter.create_volume_from_image( context, instance, image_id) self._disk_adapter.attach_volume_to_host(root_volume) lpar_id = self._operator.get_lpar(instance['name'])['lpar_id'] vhost = self._operator.get_vhost_by_instance_id(lpar_id) self._operator.attach_disk_to_vhost( root_volume['device_name'], vhost) except Exception, e: LOG.exception(_("PowerVM image creation failed: %s") % str(e)) raise exception.PowerVMImageCreationFailed()
def test_spawn_cleanup_on_fail(self): self.flags(powervm_img_local_path='/images/') self.stubs.Set(images, 'fetch', lambda *x, **y: None) self.stubs.Set( self.powervm_connection._powervm._disk_adapter, 'create_volume_from_image', lambda *x, **y: raise_(exception.PowerVMImageCreationFailed())) self.stubs.Set( self.powervm_connection._powervm, '_cleanup', lambda *x, **y: raise_(Exception('This should be logged.'))) fake_net_info = network_model.NetworkInfo( [fake_network_cache_model.new_vif()]) self.assertRaises(exception.PowerVMImageCreationFailed, self.powervm_connection.spawn, context.get_admin_context(), self.instance, {'id': 'ANY_ID'}, [], 's3cr3t', fake_net_info)
def test_spawn_cleanup_on_fail(self): # Verify on a failed spawn, we get the original exception raised. # helper function def raise_(ex): raise ex self.flags(powervm_img_local_path='/images/') self.stubs.Set(images, 'fetch_to_raw', lambda *x, **y: None) self.stubs.Set( self.powervm_connection._powervm._disk_adapter, 'create_volume_from_image', lambda *x, **y: raise_(exception.PowerVMImageCreationFailed())) self.stubs.Set( self.powervm_connection._powervm, '_cleanup', lambda *x, **y: raise_(Exception('This should be logged.'))) self.assertRaises(exception.PowerVMImageCreationFailed, self.powervm_connection.spawn, context.get_admin_context(), self.instance, {'id': 'ANY_ID'}, 's3cr3t', [])