Search in sources :

Example 1 with Images

use of com.woorea.openstack.glance.model.v2.Images in project ovirt-engine by oVirt.

the class OpenStackImageProviderProxy method createImageFromDiskImage.

public String createImageFromDiskImage(DiskImage diskImage) {
    Image glanceImage = new Image();
    glanceImage.setName(diskImage.getDiskAlias());
    if (diskImage.getVolumeFormat() == VolumeFormat.RAW) {
        glanceImage.setDiskFormat(GlanceImageFormat.RAW.getValue());
    } else if (diskImage.getVolumeFormat() == VolumeFormat.COW) {
        glanceImage.setDiskFormat(GlanceImageFormat.COW.getValue());
    } else {
        throw new OpenStackImageException(OpenStackImageException.ErrorType.UNSUPPORTED_DISK_FORMAT, "Unknown disk format: " + diskImage.getVolumeFormat());
    }
    glanceImage.setContainerFormat(GlanceImageContainer.BARE.getValue());
    Image retGlanceImage = getClient().images().create(glanceImage).execute();
    return retGlanceImage.getId();
}
Also used : DiskImage(org.ovirt.engine.core.common.businessentities.storage.DiskImage) Image(com.woorea.openstack.glance.model.v2.Image) RepoImage(org.ovirt.engine.core.common.businessentities.storage.RepoImage)

Example 2 with Images

use of com.woorea.openstack.glance.model.v2.Images in project ovirt-engine by oVirt.

the class OpenStackImageProviderProxy method setCowVirtualSizeAndQcowCompat.

private void setCowVirtualSizeAndQcowCompat(DiskImage diskImage, Image glanceImage) {
    // For the qcow2 format we need to download the image header and read the virtual size from there
    byte[] imgContent = new byte[72];
    ImageDownload downloadImage = getClient().images().download(glanceImage.getId()).execute();
    try (InputStream inputStream = downloadImage.getInputStream()) {
        int bytesRead = inputStream.read(imgContent, 0, imgContent.length);
        if (bytesRead != imgContent.length) {
            throw new OpenStackImageException(OpenStackImageException.ErrorType.UNABLE_TO_DOWNLOAD_IMAGE, "Unable to read image header: " + bytesRead);
        }
    } catch (IOException e) {
        throw new OpenStackImageException(OpenStackImageException.ErrorType.UNABLE_TO_DOWNLOAD_IMAGE, "Unable to download image");
    }
    ByteBuffer b = ByteBuffer.wrap(imgContent);
    int qcow2Signature = b.getInt();
    int qcow2Version = b.getInt();
    QcowCompat qcowCompat = QcowCompat.forQcowHeaderVersion(qcow2Version);
    if (qcow2Signature == QCOW2_SIGNATURE && qcowCompat != null && qcowCompat != QcowCompat.Undefined) {
        b.position(QCOW2_SIZE_OFFSET);
        diskImage.setSize(b.getLong());
        diskImage.setQcowCompat(qcowCompat);
    } else {
        throw new OpenStackImageException(OpenStackImageException.ErrorType.UNRECOGNIZED_IMAGE_FORMAT, "Unable to recognize QCOW2 format");
    }
}
Also used : QcowCompat(org.ovirt.engine.core.common.businessentities.storage.QcowCompat) InputStream(java.io.InputStream) ImageDownload(com.woorea.openstack.glance.model.v2.ImageDownload) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 3 with Images

use of com.woorea.openstack.glance.model.v2.Images in project ovirt-engine by oVirt.

the class OpenStackImageProviderProxy method getAllImagesAsRepoImages.

public List<RepoImage> getAllImagesAsRepoImages(Integer listSize, Integer totalListSize) {
    ArrayList<RepoImage> repoImages = new ArrayList<>();
    long currentTime = System.currentTimeMillis();
    Images images = null;
    do {
        OpenStackRequest<Images> listRequest = getClient().images().list(true).queryParam("limit", listSize).queryParam("sort_key", "name").queryParam("sort_dir", "asc");
        if (images != null) {
            listRequest.queryParam("marker", images.getList().get(images.getList().size() - 1).getId());
        }
        images = listRequest.execute();
        for (Image glanceImage : images) {
            RepoImage repoImage = imageToRepoImage(glanceImage);
            repoImage.setLastRefreshed(currentTime);
            repoImages.add(repoImage);
        }
    } while (images.getList().size() >= listSize && totalListSize != null && repoImages.size() < totalListSize);
    return repoImages;
}
Also used : RepoImage(org.ovirt.engine.core.common.businessentities.storage.RepoImage) Images(com.woorea.openstack.glance.model.v2.Images) ArrayList(java.util.ArrayList) DiskImage(org.ovirt.engine.core.common.businessentities.storage.DiskImage) Image(com.woorea.openstack.glance.model.v2.Image) RepoImage(org.ovirt.engine.core.common.businessentities.storage.RepoImage)

Example 4 with Images

use of com.woorea.openstack.glance.model.v2.Images in project ovirt-engine by oVirt.

the class OpenStackImageProviderProxy method getImageAsDiskImage.

public DiskImage getImageAsDiskImage(String id) {
    DiskImage diskImage = new DiskImage();
    Image glanceImage;
    try {
        glanceImage = getClient().images().show(id).execute();
    } catch (OpenStackResponseException e) {
        log.debug("Exception:", e);
        throw new OpenStackImageException(OpenStackImageException.ErrorType.IMAGE_NOT_FOUND, "Cannot find the specified image.");
    } catch (RuntimeException rte) {
        log.error("Exception:", rte);
        throw new RuntimeException("Failed to import from the repository.");
    }
    validateContainerFormat(glanceImage);
    String shortHash = glanceImage.getId().substring(0, 7);
    if (glanceImage.getName() != null) {
        diskImage.setDiskDescription(glanceImage.getName() + " (" + shortHash + ")");
    } else {
        diskImage.setDiskDescription("Glance disk: " + shortHash);
    }
    setDiskAttributes(diskImage, glanceImage);
    if (glanceImage.getDiskFormat().equals(GlanceImageFormat.RAW.getValue())) {
        diskImage.setVolumeFormat(VolumeFormat.RAW);
    } else if (glanceImage.getDiskFormat().equals(GlanceImageFormat.COW.getValue())) {
        diskImage.setVolumeFormat(VolumeFormat.COW);
    } else {
        throw new OpenStackImageException(OpenStackImageException.ErrorType.UNSUPPORTED_DISK_FORMAT, "Unknown disk format: " + glanceImage.getDiskFormat());
    }
    return diskImage;
}
Also used : OpenStackResponseException(com.woorea.openstack.base.client.OpenStackResponseException) DiskImage(org.ovirt.engine.core.common.businessentities.storage.DiskImage) Image(com.woorea.openstack.glance.model.v2.Image) RepoImage(org.ovirt.engine.core.common.businessentities.storage.RepoImage) DiskImage(org.ovirt.engine.core.common.businessentities.storage.DiskImage)

Aggregations

Image (com.woorea.openstack.glance.model.v2.Image)3 DiskImage (org.ovirt.engine.core.common.businessentities.storage.DiskImage)3 RepoImage (org.ovirt.engine.core.common.businessentities.storage.RepoImage)3 OpenStackResponseException (com.woorea.openstack.base.client.OpenStackResponseException)1 ImageDownload (com.woorea.openstack.glance.model.v2.ImageDownload)1 Images (com.woorea.openstack.glance.model.v2.Images)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 QcowCompat (org.ovirt.engine.core.common.businessentities.storage.QcowCompat)1