use of org.platformlayer.ops.images.CloudImage 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.CloudImage in project platformlayer by platformlayer.
the class ImageFactory method getImageInfo.
private CloudImage getImageInfo(DiskImage recipe) throws OpsException {
final String imageId = Tag.IMAGE_ID.findUnique(recipe.getTags());
if (imageId == null) {
throw new OpsException("Image is not yet built: " + recipe).setRetry(TimeSpan.ONE_MINUTE);
}
final ImageFormat imageFormat = ImageFormat.valueOf(recipe.getFormat());
String compression = null;
switch(imageFormat) {
case Tar:
compression = "gzip";
break;
case DiskRaw:
compression = "gzip";
break;
}
return new CloudImage() {
@Override
public String getId() {
return imageId;
}
@Override
public ImageFormat getFormat() {
return imageFormat;
}
};
}
use of org.platformlayer.ops.images.CloudImage 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);
}
use of org.platformlayer.ops.images.CloudImage in project platformlayer by platformlayer.
the class DirectImageStore method findImages.
@Override
public List<CloudImage> findImages(List<Tag> tags) throws OpsException {
List<CloudImage> images = Lists.newArrayList();
for (String imageId : fileStore.find(tags)) {
Properties properties = fileStore.readProperties(imageId);
Tags imageTags = fileStore.asTags(properties);
images.add(new DirectCloudImage(this, imageId, imageTags));
}
return images;
}
use of org.platformlayer.ops.images.CloudImage in project platformlayer by platformlayer.
the class OpenstackCloudContext method createInstance.
public Server createInstance(OpenstackCloud cloud, String serverName, MachineCreationRequest request) throws OpsException {
OpenstackComputeClient computeClient = getComputeClient(cloud);
try {
Image foundImage = null;
CloudBehaviours cloudBehaviours = new CloudBehaviours(cloud);
if (!cloudBehaviours.canUploadImages()) {
// For now, we presume this is the HP cloud and hard-code the name
// if (!cloudBehaviours.isHpCloud()) {
// throw new UnsupportedOperationException();
// }
DiskImageRecipe recipe = null;
if (request.recipeId != null) {
recipe = platformLayerClient.getItem(request.recipeId, DiskImageRecipe.class);
}
OperatingSystemRecipe operatingSystem = null;
if (recipe != null) {
operatingSystem = recipe.getOperatingSystem();
}
log.info("Listing images to pick best image");
Iterable<Image> images = computeClient.root().images().list();
if (cloudBehaviours.isHpCloud()) {
// TODO: We need a better solution here!!
Set<String> imageNames = Sets.newHashSet("Debian Squeeze 6.0.3 Server 64-bit 20120123");
log.warn("Hard coding image name (presuming HP cloud)");
// TODO: Match OS
for (Image image : images) {
if (imageNames.contains(image.getName())) {
foundImage = image;
break;
}
}
} else if (cloudBehaviours.isRackspaceCloud()) {
if (operatingSystem == null) {
operatingSystem = new OperatingSystemRecipe();
operatingSystem.setDistribution("debian");
operatingSystem.setVersion("squeeze");
}
for (Image image : images) {
boolean matchesDistribution = false;
boolean matchesVersion = false;
for (Image.ImageMetadata.ImageMetadataItem item : image.getMetadata()) {
if (item.getKey().equals("os_distro")) {
if (operatingSystem != null && operatingSystem.getDistribution() != null) {
if (Comparisons.equalsIgnoreCase(operatingSystem.getDistribution(), item.getValue())) {
matchesDistribution = true;
}
}
}
if (item.getKey().equals("os_version")) {
if (operatingSystem != null && operatingSystem.getVersion() != null) {
if (Comparisons.equalsIgnoreCase(operatingSystem.getVersion(), item.getValue())) {
matchesVersion = true;
} else if (Comparisons.equalsIgnoreCase(operatingSystem.getDistribution(), "debian")) {
if (Comparisons.equalsIgnoreCase(operatingSystem.getVersion(), "squeeze") && Comparisons.equalsIgnoreCase(item.getValue(), "6")) {
matchesVersion = true;
} else {
matchesVersion = false;
}
} else if (Comparisons.equalsIgnoreCase(operatingSystem.getDistribution(), "ubuntu")) {
if (Comparisons.equalsIgnoreCase(operatingSystem.getVersion(), "lucid") && Comparisons.equalsIgnoreCase(item.getValue(), "10.04LTS")) {
matchesVersion = true;
} else {
matchesVersion = false;
}
} else {
matchesVersion = false;
}
}
}
}
if (matchesDistribution && matchesVersion) {
foundImage = image;
break;
}
}
} else {
for (Image image : images) {
boolean isMatch = false;
for (Image.ImageMetadata.ImageMetadataItem item : image.getMetadata()) {
if (item.getKey().equals(Tag.IMAGE_OS_DISTRIBUTION)) {
if (operatingSystem != null && operatingSystem.getDistribution() != null) {
if (!Comparisons.equalsIgnoreCase(operatingSystem.getDistribution(), item.getValue())) {
isMatch = false;
}
}
}
if (item.getKey().equals(Tag.IMAGE_OS_VERSION)) {
if (operatingSystem != null && operatingSystem.getVersion() != null) {
if (!Comparisons.equalsIgnoreCase(operatingSystem.getVersion(), item.getValue())) {
isMatch = false;
}
}
}
}
if (isMatch) {
foundImage = image;
break;
}
}
}
if (foundImage == null) {
throw new IllegalArgumentException("Could not find image");
}
} else {
List<ImageFormat> formats = Collections.singletonList(ImageFormat.DiskQcow2);
CloudImage image = imageFactory.getOrCreateImageId(cloud, formats, request.recipeId);
String imageId = image.getId();
log.info("Getting image details for image: " + imageId);
foundImage = computeClient.root().images().image(imageId).show();
if (foundImage == null) {
throw new IllegalArgumentException("Could not find image: " + imageId);
}
}
SecurityGroup createdSecurityGroup = null;
if (cloudBehaviours.supportsSecurityGroups()) {
SecurityGroup createTemplate = new SecurityGroup();
createTemplate.setName(SECURITY_GROUP_PREFIX + serverName);
createTemplate.setDescription("Security group for instance: " + serverName);
try {
log.info("Creating security group: " + createTemplate.getName());
createdSecurityGroup = computeClient.root().securityGroups().create(createTemplate);
} catch (OpenstackException e) {
for (SecurityGroup candidate : computeClient.root().securityGroups().list()) {
if (Objects.equal(candidate.getName(), createTemplate.getName())) {
createdSecurityGroup = candidate;
break;
}
}
if (createdSecurityGroup != null) {
// Ignore
log.warn("Ignoring 'security group already exists' error: " + e.getMessage());
} else {
throw new OpsException("Error creating security group", e);
}
}
{
CreateSecurityGroupRuleRequest newRule = new CreateSecurityGroupRuleRequest();
newRule.setCidr("0.0.0.0/0");
newRule.setFromPort(22);
newRule.setToPort(22);
newRule.setIpProtocol("tcp");
newRule.setParentGroupId(createdSecurityGroup.getId());
try {
log.info("Creating security group rule for port: " + newRule.getToPort());
SecurityGroupRule createdRule = computeClient.root().securityGroupRules().create(newRule);
} catch (OpenstackException e) {
String message = e.getMessage();
if (message != null && message.contains("This rule already exists")) {
log.warn("Ignoring 'rule already exists': " + e.getMessage());
} else {
throw new OpsException("Error creating security group access", e);
}
}
}
}
AsyncServerOperation createServerOperation;
{
ServerForCreate create = new ServerForCreate();
create.setName(serverName);
if (request.sshPublicKey != null) {
if (cloudBehaviours.supportsPublicKeys()) {
OpenstackCloudHelpers cloudHelpers = new OpenstackCloudHelpers();
KeyPair keyPair = cloudHelpers.ensurePublicKeyUploaded(computeClient, request.sshPublicKeyName, request.sshPublicKey);
create.setKeyName(keyPair.getName());
} else if (cloudBehaviours.supportsFileInjection()) {
String fileContents = SshKeys.serialize(request.sshPublicKey);
create.addUploadFile("/root/.ssh/authorized_keys", Utf8.getBytes(fileContents));
} else {
throw new OpsException("No supported SSH key mechanism on cloud");
}
}
create.setImageRef(foundImage.getId());
Flavor flavor = getClosestInstanceType(computeClient, request);
if (flavor == null) {
throw new OpsException("Cannot determine instance type for request");
}
create.setFlavorRef(flavor.getId());
if (request.securityGroups != null) {
// TODO: Reimplement if needed
throw new UnsupportedOperationException();
}
if (createdSecurityGroup != null) {
ServerForCreate.SecurityGroup serverSecurityGroup = new ServerForCreate.SecurityGroup();
serverSecurityGroup.setName(createdSecurityGroup.getName());
create.getSecurityGroups().add(serverSecurityGroup);
}
create.setConfigDrive(cloudBehaviours.useConfigDrive());
log.info("Launching new server: " + create.getName());
createServerOperation = computeClient.createServer(create);
}
log.info("Waiting for server to be ready");
Server server = createServerOperation.waitComplete();
Server instanceInfo = null;
String stateName = null;
while (true) {
instanceInfo = getInstanceInfo(computeClient, server.getId());
stateName = instanceInfo.getStatus();
log.info("Instance state: " + stateName);
//
if (stateName.equals("BUILD")) {
break;
}
if (stateName.equals("ACTIVE")) {
break;
}
Thread.sleep(1000);
}
// Even if the machine is in 'error' state, we still want to associate it with us
if (request.tags != null) {
Server newServerInfo = new Server();
Metadata metadata = new Metadata();
for (Tag tag : request.tags) {
Metadata.Item meta = new Metadata.Item();
meta.setKey(tag.getKey());
meta.setValue(tag.getValue());
metadata.getItems().add(meta);
}
newServerInfo.setMetadata(metadata);
log.info("Tagging server: " + server.getId());
computeClient.root().servers().server(server.getId()).update(newServerInfo);
}
return server;
} catch (InterruptedException e) {
ExceptionUtils.handleInterrupted(e);
throw new OpsException("Error building server", e);
} catch (OpenstackException e) {
throw new OpsException("Error building server", e);
}
}
Aggregations