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);
}
}
}
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);
}
}
Aggregations