public ImageService.Image createImage(Project project, InstanceData instance, CreateImageRequest request)
throws IOException, CloudException {
// TODO: The spec (?) says this call is async.
// We would probably do better to spend the effort to make it really
// fast instead (BtrFS?)
SchedulerHost host = scheduler.findHost(instance.getHostId());
if (host == null) {
throw new IllegalStateException();
}
String hostCookie = instance.getHostCookie();
log.warn("createImage is inefficient - we should support side-load");
Map<String, String> metadata = Maps.newHashMap();
if (request.metadata != null) {
metadata.putAll(request.metadata);
}
if (request.name != null) {
metadata.put(ImageService.METADATA_KEY_NAME, request.name);
}
metadata.put(ImageService.METADATA_KEY_CONTAINER_FORMAT, "tar");
metadata.put(ImageService.METADATA_KEY_DISK_FORMAT, "raw");
UUID containerId = UUID.fromString(hostCookie);
try (TempFile snapshot = host.createImage(containerId)) {
ImageService.Image image = imageService.createImage(instance.getProjectId(), metadata);
BlobData blobData = BlobData.build(snapshot.getFile());
image = imageService.uploadData(image, blobData);
return image;
}