Search in sources :

Example 1 with DiskImage

use of org.platformlayer.images.model.DiskImage in project platformlayer by platformlayer.

the class ImageFactory method getOrCreateImageId.

public CloudImage getOrCreateImageId(MachineProvider targetCloud, List<ImageFormat> formats, PlatformLayerKey recipeKey) throws OpsException {
    if (recipeKey == null) {
        log.debug("Looking for bootstrap image");
        for (ImageFormat format : formats) {
            CloudImage bootstrapImage = findBootstrapImage(format, cloud.getImageStore(targetCloud));
            if (bootstrapImage != null) {
                return bootstrapImage;
            // Tags tags = bootstrapImage.getTags();
            // String compression = tags.findUnique("org.openstack.sync__1__expand");
            // return new ImageInfo(bootstrapImage.getId(), format, compression);
            }
        }
        throw new OpsException("Cannot find bootstrap image for format " + Joiner.on(",").join(formats));
    }
    DiskImage imageTemplate = new DiskImage();
    imageTemplate.setFormat(formats.get(0).name());
    imageTemplate.setRecipeId(recipeKey);
    String id = "image-" + recipeKey.getItemId().getKey();
    imageTemplate.setKey(PlatformLayerKey.fromId(id));
    PlatformLayerKey cloudKey = targetCloud.getModel().getKey();
    imageTemplate.setCloud(cloudKey);
    DiskImage image = getOrCreateImage(imageTemplate);
    return getImageInfo(image);
}
Also used : OpsException(org.platformlayer.ops.OpsException) CloudImage(org.platformlayer.ops.images.CloudImage) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) DiskImage(org.platformlayer.images.model.DiskImage) ImageFormat(org.platformlayer.ops.images.ImageFormat)

Example 2 with DiskImage

use of org.platformlayer.images.model.DiskImage in project platformlayer by platformlayer.

the class ImageFactory method getOrCreateImage.

public DiskImage getOrCreateImage(DiskImage template) throws OpsException {
    DiskImage best = null;
    try {
        for (DiskImage candidate : platformLayer.listItems(DiskImage.class)) {
            if (isMatch(candidate, template)) {
                best = candidate;
                break;
            }
        }
        if (best == null) {
            // We should be owned by the recipe
            PlatformLayerKey recipeKey = template.getRecipeId();
            if (recipeKey != null) {
                template.getTags().add(Tag.buildParentTag(recipeKey));
            }
            best = platformLayer.putItem(template);
        }
    } catch (PlatformLayerClientException e) {
        throw new OpsException("Error fetching or building image", e);
    }
    return best;
}
Also used : PlatformLayerClientException(org.platformlayer.PlatformLayerClientException) OpsException(org.platformlayer.ops.OpsException) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) DiskImage(org.platformlayer.images.model.DiskImage)

Example 3 with DiskImage

use of org.platformlayer.images.model.DiskImage in project platformlayer by platformlayer.

the class ImageFactory method isMatch.

private boolean isMatch(DiskImage a, DiskImage b) {
    // TODO: Don't be evil
    DiskImage aCopy = cloneThroughJaxb(a);
    DiskImage bCopy = cloneThroughJaxb(b);
    aCopy.setKey(null);
    bCopy.setKey(null);
    aCopy.tags = null;
    bCopy.tags = null;
    aCopy.version = 0;
    bCopy.version = 0;
    aCopy.state = null;
    bCopy.state = null;
    aCopy.secret = null;
    bCopy.secret = null;
    try {
        // TODO: What if e.g. package order is different
        String aXml = JaxbHelper.toXml(aCopy, false);
        String bXml = JaxbHelper.toXml(bCopy, false);
        return Objects.equal(aXml, bXml);
    } catch (Exception e) {
        throw new IllegalArgumentException("Error comparing objects", e);
    }
}
Also used : DiskImage(org.platformlayer.images.model.DiskImage) OpsException(org.platformlayer.ops.OpsException) PlatformLayerClientException(org.platformlayer.PlatformLayerClientException)

Aggregations

DiskImage (org.platformlayer.images.model.DiskImage)3 OpsException (org.platformlayer.ops.OpsException)3 PlatformLayerClientException (org.platformlayer.PlatformLayerClientException)2 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)2 CloudImage (org.platformlayer.ops.images.CloudImage)1 ImageFormat (org.platformlayer.ops.images.ImageFormat)1