def copy_stream_optimized_disk( context, timeout_secs, write_handle, **kwargs): """Copy virtual disk from VMware server to the given write handle. :param context: context :param timeout_secs: time in seconds to wait for the copy to complete :param write_handle: copy destination :param kwargs: keyword arguments to configure the source VMDK read handle :raises: VimException, VimFaultException, VimAttributeException, VimSessionOverLoadException, VimConnectionException, ImageTransferException, ValueError """ vmdk_file_path = kwargs.get('vmdk_file_path') LOG.debug("Copying virtual disk: %(vmdk_path)s to %(dest)s.", {'vmdk_path': vmdk_file_path, 'dest': write_handle.name}) file_size = kwargs.get('vmdk_size') read_handle = rw_handles.VmdkReadHandle(kwargs.get('session'), kwargs.get('host'), kwargs.get('port'), kwargs.get('vm'), kwargs.get('vmdk_file_path'), file_size) _start_transfer(context, timeout_secs, read_handle, file_size, write_file_handle=write_handle) LOG.debug("Downloaded virtual disk: %s.", vmdk_file_path)
def test_update_progress_with_error(self): session = self._create_mock_session(True, 10) handle = rw_handles.VmdkReadHandle(session, '10.1.2.3', 443, 'vm-1', '[ds] disk1.vmdk', 100) session.invoke_api.side_effect = exceptions.VimException(None) self.assertRaises(exceptions.VimException, handle.update_progress)
def test_init_failure(self): session = self._create_mock_session(False) self.assertRaises(exceptions.VimException, lambda: rw_handles.VmdkReadHandle(session, '10.1.2.3', 'vm-1', '[ds] disk1.vmdk', 100))
def test_read(self): chunk_size = rw_handles.READ_CHUNKSIZE session = self._create_mock_session() self._response.raw.read.return_value = [1] * chunk_size handle = rw_handles.VmdkReadHandle(session, '10.1.2.3', 443, 'vm-1', '[ds] disk1.vmdk', chunk_size * 10) handle.read(chunk_size) self.assertEqual(chunk_size, handle._bytes_read) self._response.raw.read.assert_called_once_with(chunk_size)
def test_update_progress(self): chunk_size = rw_handles.READ_CHUNKSIZE vmdk_size = chunk_size * 10 session = self._create_mock_session(True, 10) self._conn.read.return_value = [1] * chunk_size handle = rw_handles.VmdkReadHandle(session, '10.1.2.3', 'vm-1', '[ds] disk1.vmdk', vmdk_size) handle.read(chunk_size) handle.update_progress()
def test_update_progress(self): chunk_size = rw_handles.READ_CHUNKSIZE vmdk_size = chunk_size * 10 session = self._create_mock_session(True, 10) self._response.raw.read.return_value = [1] * chunk_size handle = rw_handles.VmdkReadHandle(session, '10.1.2.3', 443, 'vm-1', '[ds] disk1.vmdk', vmdk_size) handle.read(chunk_size) handle.update_progress() self._response.raw.read.assert_called_once_with(chunk_size)
def upload_image(context, timeout_secs, image_service, image_id, owner_id, **kwargs): """Upload the VM's disk file to image service. :param context: image service write context :param timeout_secs: time in seconds to wait for the upload to complete :param image_service: image service handle :param image_id: upload destination image ID :param kwargs: keyword arguments to configure the source VMDK read handle :raises: VimException, VimFaultException, VimAttributeException, VimSessionOverLoadException, VimConnectionException, ImageTransferException, ValueError """ LOG.debug("Uploading to image: %s.", image_id) file_size = kwargs.get('vmdk_size') read_handle = rw_handles.VmdkReadHandle(kwargs.get('session'), kwargs.get('host'), kwargs.get('port'), kwargs.get('vm'), kwargs.get('vmdk_file_path'), file_size) # Set the image properties. It is important to set the 'size' to 0. # Otherwise, the image service client will use the VM's disk capacity # which will not be the image size after upload, since it is converted # to a stream-optimized sparse disk. image_metadata = { 'disk_format': 'vmdk', 'is_public': kwargs.get('is_public'), 'name': kwargs.get('image_name'), 'status': 'active', 'container_format': 'bare', 'size': 0, 'properties': { 'vmware_image_version': kwargs.get('image_version'), 'vmware_disktype': 'streamOptimized', 'owner_id': owner_id } } # Passing 0 as the file size since data size to be transferred cannot be # predetermined. _start_transfer(context, timeout_secs, read_handle, 0, image_service=image_service, image_id=image_id, image_meta=image_metadata) LOG.debug("Uploaded image: %s.", image_id)
def test_close(self): session = self._create_mock_session() handle = rw_handles.VmdkReadHandle(session, '10.1.2.3', 443, 'vm-1', '[ds] disk1.vmdk', 100) def session_invoke_api_side_effect(module, method, *args, **kwargs): if module == new_vim_util and method == 'get_object_property': return 'ready' self.assertEqual(session.vim, module) self.assertEqual('HttpNfcLeaseComplete', method) session.invoke_api = mock.Mock( side_effect=session_invoke_api_side_effect) handle.close() self.assertEqual(2, session.invoke_api.call_count)
def upload_image_stream_optimized(context, image_id, instance, session, vm, vmdk_size): """Upload the snapshotted vm disk file to Glance image server.""" LOG.debug("Uploading image %s", image_id, instance=instance) metadata = IMAGE_API.get(context, image_id) read_handle = rw_handles.VmdkReadHandle(session, session._host, session._port, vm, None, vmdk_size) # Set the image properties. It is important to set the 'size' to 0. # Otherwise, the image service client will use the VM's disk capacity # which will not be the image size after upload, since it is converted # to a stream-optimized sparse disk. image_metadata = { 'disk_format': 'vmdk', 'is_public': metadata['is_public'], 'name': metadata['name'], 'status': 'active', 'container_format': 'bare', 'size': 0, 'properties': { 'vmware_image_version': 1, 'vmware_disktype': 'streamOptimized', 'owner_id': instance.project_id } } # Passing 0 as the file size since data size to be transferred cannot be # predetermined. start_transfer(context, read_handle, 0, image_id=image_id, image_meta=image_metadata) LOG.debug("Uploaded image %s to the Glance image server", image_id, instance=instance)