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(read_handle, write_handle, timeout_secs) 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 export_stream_optimized_vmdk(session, vm_name, vmdk_path=None): """ """ vm_ref = vm_util._get_vm_ref_from_name(session, vm_name) def _get_vm_and_vmdk_attribs(): # Get the vmdk info that the VM is pointing to vmdk = vm_util.get_vmdk_info(session, vm_ref) if not vmdk.path: print "No root disk defined. Unable to snapshot." raise error_util.NoRootDiskDefined() lst_properties = ["datastore", "summary.config.guestId"] props = session._call_method(vutil, "get_object_properties_dict", vm_ref, lst_properties) os_type = props['summary.config.guestId'] datastores = props['datastore'] return (vmdk, datastores, os_type) vmdk, datastores, os_type = _get_vm_and_vmdk_attribs() file_size = vmdk.capacity_in_bytes read_handle = rw_handles.VmdkReadHandle(session, session._host, session._port, vm_ref, None, file_size) if not vmdk_path: vmdk_path = "%s.vmdk" % vm_name write_handle = open(vmdk_path, 'w') images.start_transfer(None, read_handle, file_size, write_file_handle=write_handle)
def test_read(self): chunk_size = rw_handles.READ_CHUNKSIZE session = self._create_mock_session() handle = rw_handles.VmdkReadHandle(session, '10.1.2.3', 443, 'vm-1', '[ds] disk1.vmdk', chunk_size * 10) data = handle.read(chunk_size) self.assertEqual('fake-data', data)
def test_close_with_error(self): session = self._create_mock_session() 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.close) self._resp.close.assert_called_once_with()
def test_update_progress(self): chunk_size = len('fake-data') vmdk_size = chunk_size * 10 session = self._create_mock_session(True, 10) handle = rw_handles.VmdkReadHandle(session, '10.1.2.3', 443, 'vm-1', '[ds] disk1.vmdk', vmdk_size) data = handle.read(chunk_size) handle.update_progress() self.assertEqual('fake-data', data)
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) # TODO(vbala) Remove this after we delete the keyword argument 'is_public' # from all client code. if 'is_public' in kwargs: LOG.debug("Ignoring keyword argument 'is_public'.") if 'image_version' in kwargs: LOG.warning("The keyword argument 'image_version' is deprecated " "and will be ignored in the next release.") image_ver = six.text_type(kwargs.get('image_version')) image_metadata = { 'disk_format': 'vmdk', 'name': kwargs.get('image_name'), 'properties': { 'vmware_image_version': image_ver, 'vmware_disktype': 'streamOptimized', 'owner_id': owner_id } } updater = loopingcall.FixedIntervalLoopingCall(read_handle.update_progress) store_id = kwargs.get('store_id') try: updater.start(interval=NFC_LEASE_UPDATE_PERIOD) image_service.update(context, image_id, image_metadata, data=read_handle, store_id=store_id) finally: updater.stop() read_handle.close() LOG.debug("Uploaded image: %s.", image_id)
def test_read_small(self): read_data = 'fake' session = self._create_mock_session(read_data=read_data) read_size = len(read_data) handle = rw_handles.VmdkReadHandle(session, '10.1.2.3', 443, 'vm-1', '[ds] disk1.vmdk', read_size * 10) handle.read(read_size) self.assertEqual(read_size, handle._bytes_read)
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._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) # TODO(vbala) Remove this after we delete the keyword argument 'is_public' # from all client code. if 'is_public' in kwargs: LOG.debug("Ignoring keyword argument 'is_public'.") # 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', 'name': kwargs.get('image_name'), 'size': 0, 'properties': { 'vmware_image_version': kwargs.get('image_version'), 'vmware_disktype': 'streamOptimized', 'owner_id': owner_id } } updater = loopingcall.FixedIntervalLoopingCall(read_handle.update_progress) try: updater.start(interval=NFC_LEASE_UPDATE_PERIOD) image_service.update(context, image_id, image_metadata, data=read_handle) finally: updater.stop() read_handle.close() LOG.debug("Uploaded image: %s.", image_id)
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 == 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, **kwargs): """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': constants.DISK_FORMAT_VMDK, 'is_public': metadata['is_public'], 'name': metadata['name'], 'status': 'active', 'container_format': constants.CONTAINER_FORMAT_BARE, 'size': 0, 'properties': { 'vmware_image_version': 1, 'vmware_disktype': 'streamOptimized', 'owner_id': instance.project_id } } # PF9 properties if 'pf9_vsphere_template' in kwargs: image_metadata['properties']['pf9_vpshere_template'] = \ kwargs['pf9_vpshere_template'] if 'os_type' in kwargs: image_metadata['properties']['vmware_ostype'] = kwargs['os_type'] # 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)
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': constants.DISK_FORMAT_VMDK, 'is_public': metadata['is_public'], 'name': metadata['name'], 'status': 'active', 'container_format': constants.CONTAINER_FORMAT_BARE, 'size': 0, 'properties': { 'vmware_image_version': 1, 'vmware_disktype': 'streamOptimized', 'owner_id': instance.project_id } } updater = loopingcall.FixedIntervalLoopingCall(read_handle.update_progress) try: updater.start(interval=NFC_LEASE_UPDATE_PERIOD) IMAGE_API.update(context, image_id, image_metadata, data=read_handle) finally: updater.stop() read_handle.close() LOG.debug("Uploaded image %s to the Glance image server", image_id, instance=instance)