Search in sources :

Example 1 with ImageStore

use of org.platformlayer.ops.images.ImageStore in project platformlayer by platformlayer.

the class DownloadImage method handler.

@Handler
public void handler(OpsTarget target) throws OpsException {
    if (target.getFilesystemInfoFile(imageFile) == null) {
        DirectCloud cloud = OpsContext.get().getInstance(DirectCloud.class);
        if (cloud == null) {
            throw new IllegalStateException("Cloud instance not found");
        }
        ImageStore imageStore = cloudHelpers.getImageStore(cloud);
        MachineProvider machineProvider = providers.toInterface(cloud, MachineProvider.class);
        CloudImage imageInfo = imageFactory.getOrCreateImageId(machineProvider, imageFormats, recipeKey);
        if (imageStore == null) {
            throw new OpsException("Image store not configured");
        }
        String fileName = imageInfo.getId() + ".image." + imageInfo.getFormat().name();
        // TODO: We don't need rawImage; delete or just request a read-only version
        File rawImage = new File(IMAGES_DIR, fileName);
        target.mkdir(IMAGES_DIR);
        // TODO: Caching / reuse ... need to check md5 though
        imageStore.bringToMachine(imageInfo.getId(), target, rawImage);
        ImageFormat imageFormat = imageInfo.getFormat();
        switch(imageFormat) {
            case Tar:
                {
                    target.mkdir(imageFile);
                    target.executeCommand(Command.build("cd {0}; tar jxf {1}", imageFile, rawImage).setTimeout(TimeSpan.FIVE_MINUTES));
                    break;
                }
            case DiskRaw:
                {
                    Command expand = Command.build("gunzip -c {0} | cp --sparse=always /proc/self/fd/0 {1}", rawImage, imageFile);
                    target.executeCommand(expand.setTimeout(TimeSpan.FIVE_MINUTES));
                    break;
                }
            case DiskQcow2:
                {
                    Command expand = Command.build("cp {0} {1}", rawImage, imageFile);
                    target.executeCommand(expand.setTimeout(TimeSpan.FIVE_MINUTES));
                    break;
                }
            default:
                throw new OpsException("Unknown image format: " + imageFormat);
        }
    }
}
Also used : DirectCloud(org.platformlayer.service.cloud.direct.model.DirectCloud) MachineProvider(org.platformlayer.ops.machines.MachineProvider) OpsException(org.platformlayer.ops.OpsException) Command(org.platformlayer.ops.Command) CloudImage(org.platformlayer.ops.images.CloudImage) File(java.io.File) ImageStore(org.platformlayer.ops.images.ImageStore) ImageFormat(org.platformlayer.ops.images.ImageFormat) Handler(org.platformlayer.ops.Handler)

Example 2 with ImageStore

use of org.platformlayer.ops.images.ImageStore in project platformlayer by platformlayer.

the class DiskImageController method build.

@Handler
public void build(DiskImage image) throws OpsException, IOException {
    String imageId = Tag.IMAGE_ID.findUnique(image.getTags());
    if (imageId == null) {
        // Check for existing image
        MachineProvider targetCloud = cloudHelpers.getCloud(image.cloud);
        DiskImageRecipe recipe = platformLayer.getItem(image.recipeId, DiskImageRecipe.class);
        ImageStore imageStore = cloud.getImageStore(targetCloud);
        List<CloudImage> existingImages = imageStore.findImages(Collections.<Tag>emptyList());
        for (CloudImage existingImage : existingImages) {
            // TODO: Fetch the parent, fetch the description, see if it's a match??
            log.info("Image found, but not know whether we can re-use: " + existingImage);
        }
    }
    if (imageId == null) {
        buildImage(image);
    }
}
Also used : MachineProvider(org.platformlayer.ops.machines.MachineProvider) DiskImageRecipe(org.platformlayer.images.model.DiskImageRecipe) CloudImage(org.platformlayer.ops.images.CloudImage) ImageStore(org.platformlayer.ops.images.ImageStore) Handler(org.platformlayer.ops.Handler)

Aggregations

Handler (org.platformlayer.ops.Handler)2 CloudImage (org.platformlayer.ops.images.CloudImage)2 ImageStore (org.platformlayer.ops.images.ImageStore)2 MachineProvider (org.platformlayer.ops.machines.MachineProvider)2 File (java.io.File)1 DiskImageRecipe (org.platformlayer.images.model.DiskImageRecipe)1 Command (org.platformlayer.ops.Command)1 OpsException (org.platformlayer.ops.OpsException)1 ImageFormat (org.platformlayer.ops.images.ImageFormat)1 DirectCloud (org.platformlayer.service.cloud.direct.model.DirectCloud)1