def upload_image(self): vdi_ref = self._get_vdi_ref() with vm_utils.vdi_attached_here(self.session, vdi_ref, read_only=True) as dev: devpath = utils.make_dev_path(dev) with utils.temporary_chown(devpath): self._perform_upload(devpath)
def test_temporary_chown(self): def fake_execute(*args, **kwargs): if args[0] == 'chown': fake_execute.uid = args[1] self.stubs.Set(utils, 'execute', fake_execute) with tempfile.NamedTemporaryFile() as f: with utils.temporary_chown(f.name, owner_uid=2): self.assertEqual(fake_execute.uid, 2) self.assertEqual(fake_execute.uid, os.getuid())
def test_temporary_chown(self): def fake_execute(*args, **kwargs): if args[0] == "chown": fake_execute.uid = args[1] self.stub_out("nova.utils.execute", fake_execute) with tempfile.NamedTemporaryFile() as f: with utils.temporary_chown(f.name, owner_uid=2): self.assertEqual(fake_execute.uid, 2) self.assertEqual(fake_execute.uid, os.getuid())
def stream_blockdev_to_glance(context, image_api, image_id, metadata, devpath): """Stream the entire contents of a block device to a glance image. :param context: Nova security context. :param image_api: Handle to the glance image API. :param image_id: UUID of the prepared glance image. :param metadata: Dictionary of metadata for the image. :param devpath: String path to device file of block device to be uploaded, e.g. "/dev/sde". """ # Make the device file owned by the current user for the duration of the # operation. with utils.temporary_chown(devpath), open(devpath, 'rb') as stream: # Stream it. This is synchronous. image_api.update(context, image_id, metadata, stream)
def stream_blockdev_to_glance(context, image_api, image_id, metadata, devpath): """Stream the entire contents of a block device to a glance image. :param context: Nova security context :param image_api: Handle to the glance image API. :param image_id: UUID of the prepared glance image. :param metadata: Dictionary of metadata for the image. :param devpath: String path to device file of block device to be uploaded, e.g. "/dev/sde". """ # Make the device file owned by the current user for the duration of the # operation. with utils.temporary_chown(devpath), open(devpath, 'rb') as stream: # Stream it. This is synchronous. image_api.update(context, image_id, metadata, stream)
def copy_volume_to_image(self, context, volume, image_service, image_id): """Copy the volume to the specified image.""" volume_path = self.local_path(volume) with utils.temporary_chown(volume_path): with utils.file_open(volume_path) as volume_file: image_service.update(context, image_id, {}, volume_file)
def copy_image_to_volume(self, context, volume, image_service, image_id): """Fetch the image from image_service and write it to the volume.""" volume_path = self.local_path(volume) with utils.temporary_chown(volume_path): with utils.file_open(volume_path, "wb") as image_file: image_service.download(context, image_id, image_file)
def test_temporary_chown(self, mock_chown): with tempfile.NamedTemporaryFile() as f: with utils.temporary_chown(f.name, owner_uid=2): mock_chown.assert_called_once_with(f.name, uid=2) mock_chown.reset_mock() mock_chown.assert_called_once_with(f.name, uid=os.getuid())