Beispiel #1
0
    def _disconnect(self, tmp_file_path, session, ds_ref, dc_ref, vmdk_path):
        # The restored volume is in compressed (streamOptimized) format.
        # So we upload it to a temporary location in vCenter datastore and copy
        # the compressed vmdk to the volume vmdk. The copy operation
        # decompresses the disk to a format suitable for attaching to Nova
        # instances in vCenter.
        dstore = datastore.get_datastore_by_ref(session, ds_ref)
        ds_path = dstore.build_path(
            VmdkConnector.TMP_IMAGES_DATASTORE_FOLDER_PATH,
            os.path.basename(tmp_file_path))
        self._create_temp_ds_folder(session, six.text_type(ds_path.parent),
                                    dc_ref)

        with open(tmp_file_path, "rb") as tmp_file:
            dc_name = session.invoke_api(vim_util, 'get_object_property',
                                         session.vim, dc_ref, 'name')
            cookies = session.vim.client.options.transport.cookiejar
            cacerts = self._ca_file if self._ca_file else not self._insecure
            self._upload_vmdk(tmp_file, self._ip, self._port, dc_name,
                              dstore.name, cookies, ds_path.rel_path,
                              os.path.getsize(tmp_file_path), cacerts,
                              self._timeout)

        # Delete the current volume vmdk because the copy operation does not
        # overwrite.
        LOG.debug("Deleting %s", vmdk_path)
        disk_mgr = session.vim.service_content.virtualDiskManager
        task = session.invoke_api(session.vim,
                                  'DeleteVirtualDisk_Task',
                                  disk_mgr,
                                  name=vmdk_path,
                                  datacenter=dc_ref)
        session.wait_for_task(task)

        src = six.text_type(ds_path)
        LOG.debug("Copying %(src)s to %(dest)s", {
            'src': src,
            'dest': vmdk_path
        })
        task = session.invoke_api(session.vim,
                                  'CopyVirtualDisk_Task',
                                  disk_mgr,
                                  sourceName=src,
                                  sourceDatacenter=dc_ref,
                                  destName=vmdk_path,
                                  destDatacenter=dc_ref)
        session.wait_for_task(task)

        # Delete the compressed vmdk at the temporary location.
        LOG.debug("Deleting %s", src)
        file_mgr = session.vim.service_content.fileManager
        task = session.invoke_api(session.vim,
                                  'DeleteDatastoreFile_Task',
                                  file_mgr,
                                  name=src,
                                  datacenter=dc_ref)
        session.wait_for_task(task)
Beispiel #2
0
    def _disconnect(self, tmp_file_path, session, ds_ref, dc_ref, vmdk_path):
        # The restored volume is in compressed (streamOptimized) format.
        # So we upload it to a temporary location in vCenter datastore and copy
        # the compressed vmdk to the volume vmdk. The copy operation
        # decompresses the disk to a format suitable for attaching to Nova
        # instances in vCenter.
        dstore = datastore.get_datastore_by_ref(session, ds_ref)
        ds_path = dstore.build_path(
            VmdkConnector.TMP_IMAGES_DATASTORE_FOLDER_PATH,
            os.path.basename(tmp_file_path))
        self._create_temp_ds_folder(
            session, six.text_type(ds_path.parent), dc_ref)

        with open(tmp_file_path, "rb") as tmp_file:
            dc_name = session.invoke_api(
                vim_util, 'get_object_property', session.vim, dc_ref, 'name')
            cookies = session.vim.client.options.transport.cookiejar
            cacerts = self._ca_file if self._ca_file else not self._insecure
            self._upload_vmdk(
                tmp_file, self._ip, self._port, dc_name, dstore.name, cookies,
                ds_path.rel_path, os.path.getsize(tmp_file_path), cacerts,
                self._timeout)

        # Delete the current volume vmdk because the copy operation does not
        # overwrite.
        LOG.debug("Deleting %s", vmdk_path)
        disk_mgr = session.vim.service_content.virtualDiskManager
        task = session.invoke_api(session.vim,
                                  'DeleteVirtualDisk_Task',
                                  disk_mgr,
                                  name=vmdk_path,
                                  datacenter=dc_ref)
        session.wait_for_task(task)

        src = six.text_type(ds_path)
        LOG.debug("Copying %(src)s to %(dest)s", {'src': src,
                                                  'dest': vmdk_path})
        task = session.invoke_api(session.vim,
                                  'CopyVirtualDisk_Task',
                                  disk_mgr,
                                  sourceName=src,
                                  sourceDatacenter=dc_ref,
                                  destName=vmdk_path,
                                  destDatacenter=dc_ref)
        session.wait_for_task(task)

        # Delete the compressed vmdk at the temporary location.
        LOG.debug("Deleting %s", src)
        file_mgr = session.vim.service_content.fileManager
        task = session.invoke_api(session.vim,
                                  'DeleteDatastoreFile_Task',
                                  file_mgr,
                                  name=src,
                                  datacenter=dc_ref)
        session.wait_for_task(task)
 def _get_datastore(self, datacenter_path, datastore_name):
     dc_obj = self._get_datacenter(datacenter_path)
     datastore_ret = self.session.invoke_api(
         vim_util, 'get_object_property', self.session.vim, dc_obj.ref,
         'datastore')
     if datastore_ret:
         datastore_refs = datastore_ret.ManagedObjectReference
         for ds_ref in datastore_refs:
             ds_obj = oslo_datastore.get_datastore_by_ref(self.session,
                                                          ds_ref)
             if ds_obj.name == datastore_name:
                 ds_obj.datacenter = dc_obj
                 return ds_obj
 def _get_datastore(self, datacenter_path, datastore_name):
     dc_obj = self._get_datacenter(datacenter_path)
     datastore_ret = self.session.invoke_api(
         vim_util, 'get_object_property', self.session.vim, dc_obj.ref,
         'datastore')
     if datastore_ret:
         datastore_refs = datastore_ret.ManagedObjectReference
         for ds_ref in datastore_refs:
             ds_obj = oslo_datastore.get_datastore_by_ref(self.session,
                                                          ds_ref)
             if ds_obj.name == datastore_name:
                 ds_obj.datacenter = dc_obj
                 return ds_obj
 def test_get_datastore_by_ref(self):
     session = mock.Mock()
     ds_ref = mock.Mock()
     expected_props = {'summary.name': 'datastore1',
                       'summary.type': 'NFS',
                       'summary.freeSpace': 1000,
                       'summary.capacity': 2000}
     session.invoke_api = mock.Mock()
     session.invoke_api.return_value = expected_props
     ds_obj = datastore.get_datastore_by_ref(session, ds_ref)
     self.assertEqual(expected_props['summary.name'], ds_obj.name)
     self.assertEqual(expected_props['summary.type'], ds_obj.type)
     self.assertEqual(expected_props['summary.freeSpace'], ds_obj.freespace)
     self.assertEqual(expected_props['summary.capacity'], ds_obj.capacity)
 def test_get_datastore_by_ref(self):
     session = mock.Mock()
     ds_ref = mock.Mock()
     expected_props = {
         'summary.name': 'datastore1',
         'summary.type': 'NFS',
         'summary.freeSpace': 1000,
         'summary.capacity': 2000
     }
     session.invoke_api = mock.Mock()
     session.invoke_api.return_value = expected_props
     ds_obj = datastore.get_datastore_by_ref(session, ds_ref)
     self.assertEqual(expected_props['summary.name'], ds_obj.name)
     self.assertEqual(expected_props['summary.type'], ds_obj.type)
     self.assertEqual(expected_props['summary.freeSpace'], ds_obj.freespace)
     self.assertEqual(expected_props['summary.capacity'], ds_obj.capacity)